-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Added support for Uint8Array, Uint8ClampedArray #2511
Conversation
const input = Uint8Array.from([1, 1, 1, 2, 2, 2]); // or Uint8ClampedArray const image = sharp(input, { raw: { width: 2, height: 1, channels: 3 } }); image.toUint8Array().then((output) => { assert.deepStrictEqual(input, output); });
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.
Hi Leon, thank you for this PR, I've left a few questions/comments inline.
This fixed the hidden problem of the callback version returning the unmodified version.
Removed nested then calls and used async await instead. Also fixed bug where Buffer objects will always satisfy the instanceof Uint8Array checks.
Question, advice?Should we discard the Having looked at it for a bit, maybe it's not needed. What do you think @lovell ? |
Great, happy to keep it simple; the addition of real world examples to the documentation is always a good thing, thank you. There's value in supporting Uint8Array (and Uint8ClampedArray) on the input side so please can we keep this bit. |
Did the changes we discussed. |
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.
Thanks for the updates, there are a couple of lines failing the linter checks. Please can you fix these.
lib/output.js
Outdated
* // output the raw pixels | ||
* .raw() | ||
* .toBuffer(); | ||
* |
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.
/__w/sharp/sharp/lib/output.js:112:3: Trailing spaces not allowed.
lib/output.js
Outdated
* // this will not copy the data, instead it will change `data`s underlying ArrayBuffer | ||
* // so `data` and `pixelArray` point to the same memory location | ||
* const pixelArray = new Uint8ClampedArray(data.buffer); | ||
* |
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.
/__w/sharp/sharp/lib/output.js:117:3: Trailing spaces not allowed.
Sorry, fixed it now |
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.
Brilliant, thank you.
Wow @leon, I'm actually really happy to see this, thank you and @lovell. Feels great to see something I struggled with and couldn't finish, actually being finished. Awesome. This was the last piece for my project to go completely node.js-buffer-free (on my end at least). Really hyped to try this out when the new release is here. ❤️ |
@nnmrts I'm happy you liked it 😃 |
This allows us to work with raw pixels data easier.
It is a continuation of: #2256
Regarding memory efficiency. I've done research and have come up with
This will create a
Buffer
which points to the underlying Uint8Arrays buffer.They will share the same memory.
And for the other way around its:
Example
So the full around circle would be:
I have added both unit tests and documentation for the new feature.
All tests are passing.
Performance wise, the only changed code path is another couple of
instanceof
checks in the input handler, which should not be a problem.Let me know if I need to do any changes 👍🏻