Skip to content
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

Add changeDpiBuffer method #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

bmathews
Copy link

This adds support for using buffers directly instead of FileReader/Blobs.

@asturur
Copy link
Collaborator

asturur commented Oct 26, 2018

Hi @bmathews, i have a couple of questions i left in the PR code comments

@@ -51,6 +52,13 @@ var _H = 'H'.charCodeAt(0);
var _Y = 'Y'.charCodeAt(0);
var _S = 's'.charCodeAt(0);

function changeDpiBuffer(buffer, dpi, type) {
var headerChunk = new Uint8Array(buffer.slice(0, 33));
Copy link
Collaborator

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure!

Copy link
Author

@bmathews bmathews Oct 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added getType method that checks the relevant few header bytes.

Copy link
Collaborator

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.

var rest = buffer.slice(33);
var changedHeader = changeDpiOnArray(headerChunk, dpi, type);
return Buffer.concat([changedHeader, rest]);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated this to be browser/node agnostic.

@asturur
Copy link
Collaborator

asturur commented Oct 26, 2018

please let the dist/index.js out of the changes, i ll build it as soon as i bump up the version number.
Thanks for the contribution so far

@bmathews
Copy link
Author

Removed dist/index.js.

Thanks!

var c = new a.constructor(a.length + b.length);
c.set(a);
c.set(b, a.length);
return c;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator

@asturur asturur Oct 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I m also wondering about ie11 compatibility here. the new x.constructor never works for me in ie11., this i will test.

Copy link
Author

@bmathews bmathews Oct 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you're right about a.constructor always being a Uint8Array. My goal is to maintain whatever type they passed in originally. But I'm remembering now that in node, we wouldn't want to do something equivalent to new Buffer() anyways, since that's deprecated. I'll have to think about this more.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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

Copy link
Author

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator

@asturur asturur Oct 31, 2018

Choose a reason for hiding this comment

The 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.

@asturur
Copy link
Collaborator

asturur commented Nov 16, 2018

Hi @bmathews let the dist unbuilt. i will go up with the version bump.

We have still that Buffer question to solve. Monday i ll consult with someone since i do not run this library alone.

Did you get some idea meantime?

@asturur
Copy link
Collaborator

asturur commented Nov 20, 2018

I was chatting about this a bit.
Supporting Buffer properly outside node would mean add a dependency for the Buffer module.
I thin the best part would be just convert the buffer to array and pretend this new method works on array.
Leaving the conversion from and to Buffer outside the lib.

What do you think?

@onoranzefunebricloud
Copy link

+1

@asturur
Copy link
Collaborator

asturur commented Jun 12, 2019

i guess we can do something for this

@asturur asturur mentioned this pull request Apr 27, 2020
@nateisleychamp
Copy link

Is this PR going to be merged?

@asturur
Copy link
Collaborator

asturur commented Jan 29, 2021

hi @nateisleychamp i have to admit i forgot about it.
I'll do my best to have a native node buffer method asap.

@plaa
Copy link

plaa commented Oct 20, 2021

Would appreciate this too, to avoid converting ArrayBuffer --> Blob --> ArrayBuffer in Node.

(Edit: I ended up using bmathews's version directly as I couldn't get Blob polyfill to work. Works like a charm. 👍)

@stephan-fischer
Copy link

Which pull request will now be merged? @bmathews ? And where is the readDPI method? 😉

@asturur
Copy link
Collaborator

asturur commented Feb 26, 2022

Hello i lost access to this repo for a while, but i should be able to fix things now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants