-
Notifications
You must be signed in to change notification settings - Fork 57
Add changeDpiBuffer method #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| exports.changeDpiBuffer = changeDpiBuffer; | ||
| exports.changeDpiBlob = changeDpiBlob; | ||
| exports.changeDpiDataUrl = changeDpiDataUrl; | ||
|
|
||
|
|
@@ -51,6 +52,40 @@ var _H = 'H'.charCodeAt(0); | |
| var _Y = 'Y'.charCodeAt(0); | ||
| var _S = 's'.charCodeAt(0); | ||
|
|
||
| function isBufferPNG(buffer) { | ||
| if (buffer.length < 8) return; | ||
| return buffer[0] === 137 && buffer[1] === 80 && buffer[2] === 78 && buffer[3] === 71 && buffer[4] === 13 && buffer[5] === 10 && buffer[6] === 26 && buffer[7] === 10; | ||
| } | ||
|
|
||
| function isBufferJPEG(buffer) { | ||
| if (buffer.length < 3) return; | ||
| return buffer[0] === 255 && buffer[1] === 216 && buffer[2] === 255; | ||
| } | ||
|
|
||
| function getType(buffer) { | ||
| if (isBufferJPEG(buffer)) { | ||
| return JPEG; | ||
| } | ||
| if (isBufferPNG(buffer)) { | ||
| return PNG; | ||
| } | ||
| } | ||
|
|
||
| function mergedTypedArrays(a, b) { | ||
| var c = new a.constructor(a.length + b.length); | ||
| c.set(a); | ||
| c.set(b, a.length); | ||
| return c; | ||
| } | ||
|
|
||
| function changeDpiBuffer(buffer, dpi) { | ||
| var headerChunk = new Uint8Array(buffer.slice(0, 33)); | ||
| var rest = buffer.slice(33); | ||
| var type = getType(buffer); | ||
| var changedHeader = changeDpiOnArray(headerChunk, dpi, type); | ||
| return mergedTypedArrays(changedHeader, rest); | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happen when this gets used in a browsers? can we stop the code reaching this point if Buffer is not supported? Can we add as a peer dependency some module that would allow to use this function in the browser too?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated this to be browser/node agnostic. |
||
|
|
||
| function changeDpiBlob(blob, dpi) { | ||
| // 33 bytes are ok for pngs and jpegs | ||
| // to contain the information. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,40 @@ const _H = 'H'.charCodeAt(0); | |
| const _Y = 'Y'.charCodeAt(0); | ||
| const _S = 's'.charCodeAt(0); | ||
|
|
||
| function isBufferPNG(buffer) { | ||
| if (buffer.length < 8) return; | ||
| return buffer[0] === 137 && buffer[1] === 80 && buffer[2] === 78 && buffer[3] === 71 && buffer[4] === 13 && buffer[5] === 10 && buffer[6] === 26 && buffer[7] === 10; | ||
| } | ||
|
|
||
| function isBufferJPEG(buffer) { | ||
| if (buffer.length < 3) return; | ||
| return buffer[0] === 255 && buffer[1] === 216 && buffer[2] === 255; | ||
| } | ||
|
|
||
| function getType(buffer) { | ||
| if (isBufferJPEG(buffer)) { | ||
| return JPEG; | ||
| } | ||
| if (isBufferPNG(buffer)) { | ||
| return PNG; | ||
| } | ||
| } | ||
|
|
||
| function mergedTypedArrays(a, b) { | ||
| var c = new a.constructor(a.length + b.length); | ||
| c.set(a); | ||
| c.set(b, a.length); | ||
| return c; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so basically this avoid us from referencing Buffer, but giving us buffer compatibility ? or will take in buffer and spit back a typed array? I can imagine a.constructor to be always the Uint8Array, while the b.constructor can be both Buffer and Uint8Array? I'm asking because i played very little with buffers in node.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I m also wondering about ie11 compatibility here. the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, you're right about
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we could accept the fact that we mutate the original Buffer we could just use .set on the original object for the new head. Do you know if Buffer.slice returns a copy or a reference to the data? if it returns a copy we can just copy the argument that comes and then set the new head over it, and return it. Did not try it yet, so i m still throwing ideas out
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It returns a view into the original buffer. Mutating it will mutate the original.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well mutation approach would not work with PNG since the header becomes bigger if the phys chunk is not there. |
||
| } | ||
|
|
||
| export function changeDpiBuffer(buffer, dpi) { | ||
| const headerChunk = new Uint8Array(buffer.slice(0, 33)); | ||
| const rest = buffer.slice(33); | ||
| const type = getType(buffer); | ||
| const changedHeader = changeDpiOnArray(headerChunk, dpi, type); | ||
| return mergedTypedArrays(changedHeader, rest); | ||
| } | ||
|
|
||
| export function changeDpiBlob(blob, dpi) { | ||
| // 33 bytes are ok for pngs and jpegs | ||
| // to contain the information. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
once the first 33 bytes are converted to array, can we autoDetect the type? the png signature is fixed and the jpeg more or less too. In this way we can find out the type and having the function require an argument the other do not required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure!
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added
getTypemethod that checks the relevant few header bytes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
like this is the png start
137 80 78 71 13 10 26 10<--- decimal values of the first 8 bytes.