Members
(constant) ChunkType :ChunkType
An enum for standard PNG chunk type codes (4-byte Uint32 decimal), including critical and ancillary chunks.
Type:
Properties:
Name | Type | Description |
---|---|---|
IHDR |
number | |
PLTE |
number | |
IDAT |
number | |
IEND |
number | |
(...) |
number | see source for full list |
- Source:
(constant) ColorType :ColorType
An enum for standard PNG color types, such as RGB or RGBA.
Type:
Properties:
Name | Type | Description |
---|---|---|
GRAYSCALE |
number | (1) |
RGB |
number | (2) |
INDEXED |
number | (3) |
GRAYSCALE_ALPHA |
number | (4) |
RGBA |
number | (6) |
- Source:
(constant) FilterMethod :FilterMethod
An enum for standard PNG scanline filter methods.
Type:
Properties:
Name | Type | Description |
---|---|---|
None |
number | No filter (0x00) |
Sub |
number | Compute from left (0x01) |
Up |
number | Compute from above scanline (0x02) |
Average |
number | Compute from average of up and left (0x03) |
Paeth |
number | Compute the PNG 'paeth' predictor from up & left (0x04) |
- Source:
(constant) Intent :Intent
An enum for Intent when specifying sRGB chunk.
Type:
Properties:
Name | Type | Description |
---|---|---|
Perceptual |
number | (0x00) |
Relative |
number | relative colorimetric (0x01) |
Saturation |
number | (0x02) |
Absolute |
number | absolute colorimetric (0x03) |
- Source:
Methods
chunkNameToType(name) → {number}
Converts a 4-character ASCII string into a 4-byte (32-bit) integer representing a chunk type.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | the name of the chunk |
Returns:
a 32-bit integer representing this chunk type
- Type
- number
chunkTypeToName(type) → {string}
Converts an arbitrary 4-byte chunk type into a readable ASCII string.
Parameters:
Name | Type | Description |
---|---|---|
type |
number | the chunk type |
Returns:
a name representing this type
- Type
- string
colorTypeToString(colorType) → {string}
Converts a ColorType enum to a human readable string, for example ColorType.RGBA (= 6) becomes "RGBA".
Although these numerical constants are defined in the PNG spec, the exact string for each is not.
Parameters:
Name | Type | Description |
---|---|---|
colorType |
ColorType | the type to convert |
Returns:
a readable string
- Type
- string
crc32(buf, copt) → {number}
Calculate the CRC32 checksum of an array-like buffer.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
buf |
ArrayLike | the array-like buffer to calculate the CRC32 of | ||
c |
number |
<optional> |
-1 | the initial CRC32 value |
Returns:
the CRC32 checksum
- Type
- number
decode_IHDR(data) → {IHDRData}
Decodes the IHDR chunk data, which gives information about the PNG image.
The chunk data does not include the length or chunk type fields, nor the CRC32 checksum.
Parameters:
Name | Type | Description |
---|---|---|
data |
ArrayBufferView | a typed array input, typically Uint8Array |
Returns:
the decoded IHDR data as a plain JS object
- Type
- IHDRData
decode_iCCP(data) → {iCCPData}
Decodes iCCP (color profile) chunk data and returns an object.
Parameters:
Name | Type | Description |
---|---|---|
data |
ArrayBufferView | the chunk data, typically Uint8Array |
Returns:
the iCCP chunk metadata
- Type
- iCCPData
decode_iTXt(data) → {iTXtData}
Decodes iTXt (international text) chunk data and returns an object.
Parameters:
Name | Type | Description |
---|---|---|
data |
ArrayBufferView | the chunk data, typically Uint8Array |
Returns:
the iTXt chunk metadata
- Type
- iTXtData
decode_pHYs(data) → {pHYsData}
Decodes pHYs (physical pixel dimensions) chunk data and returns an object.
Parameters:
Name | Type | Description |
---|---|---|
data |
ArrayBufferView | the chunk data, typically Uint8Array |
Returns:
the pHYs chunk metadata
- Type
- pHYsData
decode_pHYs_PPI(data) → {number}
Decodes the pHYs chunk data and returns the pixels per inch value. This expects the data's x and y to be equal and the unit to be meters.
Note that since the value is rounded during encoding, the resulting PPI might not be exactly the same as the original.
Parameters:
Name | Type | Description |
---|---|---|
data |
ArrayBufferView | the chunk data, typically Uint8Array |
Returns:
the pixels per inch value, or null if the data is invalid
- Type
- number
encode(options, deflate, deflateOptionsopt)
Encodes a PNG buffer from the given image and options, using the specified `deflate` algorithm and optional compression options.
The deflate function should have the signature `(buf, [deflateOptions]) => Uint8Array`.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
options |
EncodeOptions | the encoding options | |
deflate |
function | the sync deflate function to use | |
deflateOptions |
Object |
<optional> |
optional deflate options passed to the deflate() function |
encodeChunk(chunk) → {Uint8Array}
Encodes a single PNG chunk into a Uint8Array buffer, by writing the chunk length, type, data, and CRC value.
Parameters:
Name | Type | Description |
---|---|---|
chunk |
Chunk | the chunk to encode |
Returns:
the encoded chunk buffer
- Type
- Uint8Array
encodeHeader() → {Uint8Array}
Encodes just the raw PNG header into a Uint8Array buffer.
Returns:
the PNG header
- Type
- Uint8Array
encode_IDAT_raw(data, opts) → {Uint8Array}
Encodes an image scanline (or sequence of scanlines) in the form of IDAT chunk data (a Uint8Array).
This will use the specified `filter` parameter, or default to Paeth, which is a good general-purpose filter. You can
set the filter to `FilterMethod.None`, which will be much more efficient but may compress poorly.
Note that if you are using many chunks of IDAT data, such as for parallel encoding, you'll want to set the `firstFilter`
to either `FilterMethod.None` or `FilterMethod.Sub` (the others require the previous scanline to be available).
Parameters:
Name | Type | Description |
---|---|---|
data |
ArrayBufferView | the raw image pixel data |
opts |
IDATParameters | the IDAT parameters |
Returns:
the encoded IDAT data
- Type
- Uint8Array
encode_IHDR(data) → {Uint8Array}
Encodes the IHDR metadata as a Uint8Array buffer. Depth defaults to 8, colorType defaults to ColorType.RGBA, and interlace defaults to 0.
Parameters:
Name | Type | Description |
---|---|---|
data |
IHDRData | the IHDR metadata |
Returns:
the encoded IHDR data
- Type
- Uint8Array
encode_iCCP(opts) → {Uint8Array}
Encodes iCCP (color profile) metadata as a Uint8Array buffer. The data is expected to already be compressed by DEFLATE,
and the `compression` flag is ignored and set to 0 as it is the only valid value defined by the PNG spec.
Parameters:
Name | Type | Description |
---|---|---|
opts |
iCCPData | the iCCP metadata |
Returns:
the encoded iCCP data
- Type
- Uint8Array
encode_iTXt(opts) → {Uint8Array}
Encodes iTXt (international text) metadata as a Uint8Array buffer.
Parameters:
Name | Type | Description |
---|---|---|
opts |
iTXtData | the iTXt metadata |
Returns:
the encoded iTXt data
- Type
- Uint8Array
encode_pHYs(opts) → {Uint8Array}
Encodes pHYs (physical pixel dimensions) metadata as a Uint8Array buffer.
Parameters:
Name | Type | Description |
---|---|---|
opts |
pHYsData | the pHYs metadata |
Returns:
the encoded pHYs data
- Type
- Uint8Array
encode_pHYs_PPI(pixelsPerInch) → {Uint8Array}
Encodes a pHYs chunk with a pixels per inch value, this is mostly a convenience function.
Parameters:
Name | Type | Description |
---|---|---|
pixelsPerInch |
number | the number of pixels per inch, such as 72 (web) or 300 (print) DPI |
Returns:
the encoded pHYs data
- Type
- Uint8Array
encode_sRGB(intent) → {Uint8Array}
Encodes an sRGB chunk with an intent value.
Parameters:
Name | Type | Description |
---|---|---|
intent |
Intent | the rendering intent |
Returns:
the encoded sRGB data
- Type
- Uint8Array
encode_standardChromatics() → {Uint8Array}
Encodes a standard chromatics chunk, useful as a default.
Returns:
the encoded chromatics data
- Type
- Uint8Array
encode_standardGamma() → {Uint8Array}
Encodes a standard sRGB gamma chunk, useful as a default.
Returns:
the encoded sRGB gamma data
- Type
- Uint8Array
flattenBuffers(chunks)
Concatenates a given array of array-like data (array buffers, typed arrays) into a single Uint8Array.
Parameters:
Name | Type | Description |
---|---|---|
chunks |
Array.<ArrayLike> |
Returns:
Uint8Array concatenated data
readChunks(buf, optsopt) → {Array.<Chunk>}
Parses a PNG buffer and returns an array of chunks, each containing a type code and its data.
The individual chunks are not decoded, but left as raw Uint8Array data. If `copy` option is `false`,
the chunk data is a view into the original ArrayBufferView (no copy involved), which is more memory efficient
for large files.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
buf |
ArrayBufferView | |||
opts |
PNGReaderOptions |
<optional> |
{} | optional parameters for reading PNG chunks |
Returns:
an array of chunks
- Type
- Array.<Chunk>
readIHDR(buf, optsopt) → {IHDRData}
Reads a PNG buffer up to the end of the IHDR chunk and returns this metadata, giving its width, height, bit depth, and color type.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
buf |
ArrayBufferView | the PNG buffer to read | ||
opts |
PNGReaderOptions |
<optional> |
{} | optional parameters for reading |
Returns:
- Type
- IHDRData
reader(buf, optsopt) → {Array.<Chunk>}
A low-level interface for stream reading a PNG file. With the speicifed buffer, this function reads
each chunk and calls the `read(type, data)` function, which is expected to do something with the chunk data.
If the `read()` function returns `false`, the stream will stop reading the rest of the PNG file and safely end early,
otherwise it will expect to end on an IEND type chunk to form a valid PNG file.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
buf |
ArrayBufferView | |||
opts |
PNGReaderOptions |
<optional> |
{} | optional parameters for reading PNG chunks |
Returns:
an array of chunks
- Type
- Array.<Chunk>
writeChunks(chunks) → {Uint8Array}
Writes and formats an array of PNG chunks into a complete PNG buffer, including the PNG header.
Parameters:
Name | Type | Description |
---|---|---|
chunks |
Array.<Chunk> | the array of chunks to encode |
Returns:
the encoded PNG buffer
- Type
- Uint8Array
Type Definitions
Chunk
Properties:
Name | Type | Description |
---|---|---|
type |
number | the chunk type code as a 4-byte Uint32 value (ASCII string) |
data |
Uint8Array | the chunk data (not including chunk length, type, or CRC checksum) |
EncodeOptions
Type:
- Object
Properties:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
data |
Uint8Array | the raw pixel data to encode | ||
width |
number | the width of the image | ||
height |
number | the height of the image | ||
colorType |
ColorType |
<optional> |
ColorType.RGBA | the color type of the pixel data |
depth |
number |
<optional> |
8 | the bit depth of the image |
filterMethod |
number |
<optional> |
FilterMethod.Paeth | the filter method to use |
firstFilter |
number |
<optional> |
filter | the first scanline filter method to use |
ancillary |
Array.<Chunk> |
<optional> |
[] | additional chunks to include in the PNG |
IDATParameters
Properties:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
width |
number | the width of the image in pixels | ||
height |
number | the height of the image in pixels | ||
depth |
number |
<optional> |
8 | the bit depth of the image (default 8) |
colorType |
ColorType |
<optional> |
ColorType.RGBA | the color type of the image (default RGBA) |
filter |
FilterMethod |
<optional> |
FilterMethod.Paeth | the filter method to use (default Paeth) |
firstFilter |
FilterMethod |
<optional> |
filter | the filter method to use for the *first* scanline (defaults to provided `filter`) |
IHDRData
Type:
- Object
Properties:
Name | Type | Description |
---|---|---|
width |
number | the width of the image in pixels |
height |
number | the height of the image in pixels |
depth |
number | the bit depth of the image |
colorType |
ColorType | the color type of the image |
compression |
number | the compression method of the image (always 0 in well-formed PNGs) |
filter |
number | the filter specification (always 0 in well-formed PNGs, note this is not the same as FilterMethod) |
interlace |
number | the interlace method of the image (0 for no interlace, 1 for Adam7 interlace) |
PNGReaderOptions
Type:
- Object
Properties:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
checkCRC |
boolean |
<optional> |
false | whether to check and verify CRC values of each chunk (slower but can detect errors and corruption earlier during parsing) |
copy |
boolean |
<optional> |
true | whether to return a sliced copy of each chunk data instead of a shallow subarray view into the input buffer |
iCCPData
Type:
- Object
Properties:
Name | Type | Description |
---|---|---|
name |
string | the name of the color profile |
compression |
number | the compression method used (always 0 for well-formed PNG, implying DEFLATE) |
data |
Uint8Array | the compressed color profile data |
iTXtData
Type:
- Object
Properties:
Name | Type | Description |
---|---|---|
keyword |
string | the keyword for this text chunk |
compressionFlag |
number | the compression flag (0 for no compression, 1 for compression) |
compressionMethod |
number | the compression method (0 for zlib) |
languageTag |
string | the language tag |
translatedKeyword |
string | the translated keyword |
text |
string | the text data |
pHYsData
Type:
- Object
Properties:
Name | Type | Description |
---|---|---|
x |
number | the number of pixels per unit in the x direction |
y |
number | the number of pixels per unit in the y direction |
unit |
number | the unit specifier (0 for unknown, 1 for meter) |