Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

fast-png

image-js132.1kMIT6.3.0TypeScript support: included

PNG image decoder and encoder written entirely in JavaScript

png, image, image-js, encoder, decoder

readme

fast-png

PNG image decoder and encoder written entirely in JavaScript.

Zakodium logo

Maintained by Zakodium

NPM version build status npm download

Installation

$ npm install --save fast-png

Usage

decode(png[, options])

Arguments

  • png - A TypedArray or Buffer that contains the PNG data.
  • options - An object of options

Options

  • checkCrc - If set to true, the CRC will be checked for each chunk and an error will be thrown in case it's wrong (default: false).

encode(image)

Arguments

  • png - An object representing the image. You can pass an ImageData from the Canvas API or an object with the following properties:
    • width - The width of the image
    • height - The height of the image
    • data - An array or TypedArray with the image data
    • depth - A number indicating the color depth (only 8 and 16 are supported now). Default: 8.
    • channels - Number of channels, including alpha (1, 2, 3 and 4 are supported). Default: 4.
    • text - An object with key-value pairs representing tEXt chunks. The keys must have less than 80 characters.
           The keys and values must have only characters in the latin1 charset (maximum code point of 255).
           Default: `undefined`.

hasPngSignature(array)

Returns whether the array starts with the PNG signature (magic bytes).

PNG standard

Spec can be found at: https://www.w3.org/TR/PNG/

License

MIT

changelog

Changelog

6.3.0 (2025-02-01)

Features

  • add hasPngSignature method (#36) (98c771e)
  • add support for tEXt in encoder (ff545ce)

Bug Fixes

  • correctly decode non-ASCII tEXt chunks (76391df)

6.2.0 (2023-07-18)

Features

  • add support for tRNS field on colour types 0 and 2 (ee5f451)

6.1.0 (2021-10-18)

Features

  • add support for decoding ICC embedded profiles (#21) (9b7cc36)

6.0.1 (2021-10-12)

Bug Fixes

  • set TypeScript target to ES2020 (255bc9e)

6.0.0 (2021-10-08)

⚠ BREAKING CHANGES

  • remove support for node 10 and 15
  • use camelcase for acronyms

Code Refactoring

  • use camelcase for acronyms (3f485b3), closes #17

Miscellaneous Chores

  • remove support for node 10 and 15 (ad0bc8b)

5.0.4 (2021-03-26)

Bug Fixes

  • add Uint8ClampedArray to PNGDataArray union type (#13) (ae1cc70)

5.0.3 (2020-12-14)

Bug Fixes

5.0.2 (2020-02-28)

Bug Fixes

  • support tRNS chunk for palette images (6d9e3ec)

5.0.1 (2019-11-12)

Bug Fixes

  • update iobuffer to fix browser build (75d3e63)

5.0.0 (2019-11-12)

chore

BREAKING CHANGES

  • Node.js 6 and 8 are no longer supported.

4.0.1 (2019-04-01)

Bug Fixes

  • decode: set channels property of result (683d61a)

4.0.0 (2019-04-01)

Code Refactoring

  • rename bitDepth to depth (306b32b)
  • replace components with channels and remove alpha (35393fa)

BREAKING CHANGES

    • The components property has been replaced with channels, that includes alpha.
  • The alpha property has been removed. It was unused after the previous change.
  • The bitDepth field was renamed to depth.

3.1.3 (2019-04-01)

Bug Fixes

  • include src in package (e7cf353)
  • revert breaking change in PNGDecoder (df9aa5e)

3.1.2 (2019-03-29)

Bug Fixes

  • undo breaking change: alpha can be 0 or 1 again (e82c80c)

3.1.1 (2019-03-27)

3.1.0 (2017-07-25)

Features

  • allow to specify zlib options - default level to 3 (70ec732)

3.0.0 (2017-07-13)

Bug Fixes

  • remove misleading kind option (9749e1c)
  • rename "tEXt" field to "text" in decoded PNG (0bdc855)

BREAKING CHANGES

  • The "tEXt" field returned by the decoder has been renamed to "text".
  • The "kind" option was misleading and has been replaced by separate "components" and "alpha" options.

2.0.1 (2017-07-12)

Bug Fixes

  • decoder: add missing byteOffset in IDAT decoding (fd1d302)

2.0.0 (2017-07-12)

  • add CRC calculation and fix 16-bit encoding (d11fa54)

Bug Fixes

  • detect endianess of the platform in decoder (4f74ee7)

Features

  • add a checkCrc option to the decoder (d920bc0), closes #7
  • add support for decoding pHYs chunk (800d428)
  • implement PNG encoder (00d1e3c)

BREAKING CHANGES

  • To be consistent with the encoder, the data array returned by the decoder is now a Uint16Array for 16-bit images.

1.1.0 (2016-06-12)

1.0.0 (2015-11-23)

0.0.4 (2015-09-26)

0.0.3 (2015-09-26)

0.0.2 (2015-09-26)

0.0.1 (2015-09-26)