@@ -306,6 +306,73 @@ var canvas = new Canvas(200, 500, 'svg');
306306fs .writeFile (' out.svg' , canvas .toBuffer ());
307307```
308308
309+ ## Image pixel formats (experimental)
310+
311+ node-canvas has experimental support for additional pixel formats, roughly
312+ following the [ Canvas color space proposal] ( https://github.com/WICG/canvas-color-space/blob/master/CanvasColorSpaceProposal.md ) .
313+
314+ ``` js
315+ var canvas = new Canvas (200 , 200 );
316+ var ctx = canvas .getContext (' 2d' , {pixelFormat: ' A8' });
317+ ```
318+
319+ By default, canvases are created in the ` RGBA32 ` format, which corresponds to
320+ the native HTML Canvas behavior. Each pixel is 32 bits. The JavaScript APIs
321+ that involve pixel data (` getImageData ` , ` putImageData ` ) store the colors in
322+ the order {red, green, blue, alpha} without alpha pre-multiplication. (The C++
323+ API stores the colors in the order {alpha, red, green, blue} in native-[ endian] ( https://en.wikipedia.org/wiki/Endianness )
324+ ordering, with alpha pre-multiplication.)
325+
326+ These additional pixel formats have experimental support:
327+
328+ * ` RGB24 ` Like ` RGBA32 ` , but the 8 alpha bits are always opaque. This format is
329+ always used if the ` alpha ` context attribute is set to false (i.e.
330+ ` canvas.getContext('2d', {alpha: false}) ` ). This format can be faster than
331+ ` RGBA32 ` because transparency does not need to be calculated.
332+ * ` A8 ` Each pixel is 8 bits. This format can either be used for creating
333+ grayscale images (treating each byte as an alpha value), or for creating
334+ indexed PNGs (treating each byte as a palette index).
335+ * ` RGB16_565 ` Each pixel is 16 bits, with red in the upper 5 bits, green in the
336+ middle 6 bits, and blue in the lower 5 bits, in native platform endianness.
337+ Some hardware devices and frame buffers use this format. Note that PNG does
338+ not support this format; when creating a PNG, the image will be converted to
339+ 24-bit RGB. This format is thus suboptimal for generating PNGs.
340+ * ` A1 ` Each pixel is 1 bit, and pixels are packed together into 32-bit
341+ quantities. The ordering of the bits matches the endianness of the
342+ platform: on a little-endian machine, the first pixel is the least-
343+ significant bit. This format can be used for creating single-color images.
344+ * Support for this format is incomplete, see note below.*
345+ * ` RGB30 ` Each pixel is 30 bits, with red in the upper 10, green
346+ in the middle 10, and blue in the lower 10. (Requires Cairo 1.12 or later.)
347+ * Support for this format is incomplete, see note below.*
348+
349+ Notes and caveats:
350+
351+ * Using a non-default format can affect the behavior of APIs that involve pixel
352+ data:
353+
354+ * ` context2d.createImageData ` The size of the array returned depends on the
355+ number of bit per pixel for the underlying image data format, per the above
356+ descriptions.
357+ * ` context2d.getImageData ` The format of the array returned depends on the
358+ underlying image mode, per the above descriptions. Be aware of platform
359+ endianness, which can be determined using node.js's [ ` os.endianness() ` ] ( https://nodejs.org/api/os.html#os_os_endianness )
360+ function.
361+ * ` context2d.putImageData ` As above.
362+
363+ * ` A1 ` and ` RGB30 ` do not yet support ` getImageData ` or ` putImageData ` . Have a
364+ use case and/or opinion on working with these formats? Open an issue and let
365+ us know!
366+
367+ * ` A1 ` , ` A8 ` , ` RGB30 ` and ` RGB16_565 ` with shadow blurs may crash or not render
368+ properly.
369+
370+ * The ` ImageData(width, height) ` and ` ImageData(Uint8ClampedArray, width) `
371+ constructors assume 4 bytes per pixel. To create an ` ImageData ` instance with
372+ a different number of bytes per pixel, use
373+ ` new ImageData(new Uint8ClampedArray(size), width, height) ` or
374+ ` new ImageData(new Uint16ClampedArray(size), width, height) ` .
375+
309376## Benchmarks
310377
311378 Although node-canvas is extremely new, and we have not even begun optimization yet it is already quite fast. For benchmarks vs other node canvas implementations view this [ gist] ( https://gist.github.com/664922 ) , or update the submodules and run ` $ make benchmark ` yourself.
0 commit comments