-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Support canvas.getBuffer('raw') #819
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
Conversation
This should help interface with custom image libraries like LodePNG or WebP.
Along with https://github.com/huffpostdata/node-lightnpng, I've achieved a 50% speedup in PNG compression. Before: After: |
Also, it works a treat with
That's faster than node-canvas's JPEG encoding, and it's actually synchronous. |
This is great!! I'm hoping to review this very soon. If you're interested, I also maintain some lodepng bindings here: https://github.com/LinusU/node-lodepng |
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.
A few small nits, but this looks great! I agree that toBuffer
never should return PNG data, but as said that will be for the next major version.
I actually want to move all non-standard APIs away from the Canvas
and Context
and keep them as close to the spec as possible
var buf = canvas.toBuffer('raw'); | ||
var stride = canvas.stride; | ||
|
||
var isBE = (function() { |
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.
This can be replaced with os.endianness()
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.
os.endianness()
was introduced in Node v0.9.4, but Travis runs the tests on Node v0.8.
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.
Good catch
// Buffer doesn't have readUInt32(): it only has readUInt32LE() and | ||
// readUInt32BE(). | ||
var px = buf.readUInt32BE(y * stride + x * 4); | ||
if (isBE) { |
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.
How about something like this?
var postfix = os.endianness()
var px = buf['readUInt32' + postfix](y * stride + x * 4)
@@ -248,6 +257,16 @@ NAN_METHOD(Canvas::ToBuffer) { | |||
return; | |||
} | |||
|
|||
if (info.Length() == 1 && info[0]->StrictEquals(Nan::New<String>("raw").ToLocalChecked())) { |
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.
How about info.Length() >= 1
to support .toBuffer('raw', undefined, undefined)
? (might be used if someone wraps .toBuffer
)
Good advice! Tweaked. |
Thank you for this! |
I am curious to understand how is this different from EDIT: Found the answer here: #306 (comment) |
* it's synchronous
* It returns a Buffer, not an ImageData
* It does not accept arguments
* It does not do any color conversion (like argb->rgba)
In other words: if you use node-canvas to emulate HTML, choose
getImageData. If you use node-canvas as a Cairo+Pango wrapper, code
getBuffer('raw').
I suspect lots of node-canvas users choose it for the same reason I did:
it's the only viable drawing API on Node. HTML compatibility is a burden
for that use case.
On Wed, Dec 20, 2017, 3:37 AM Param Aggarwal, ***@***.***> wrote:
@adamhooper <https://github.com/adamhooper> - I am curious to understand
how is this different from ctx.getImageData() which also returns the raw
pixel data?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#819 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAAqPLu4RQg-ZrvQDV9bNd8mukNtdvCBks5tCMc4gaJpZM4KQl-Y>
.
--
Adam Hooper
http://adamhooper.com
+1-929-332-3503
|
This should help interface with custom image libraries like LodePNG or
WebP.
refs #306. Also, I think users could handle #562 themselves, using https://github.com/lovell/sharp plus some bit-shifts. (I haven't seen a Node library that does bulk bit-shifts on Buffer data, but it'd be straightforward to write one.)