Format Decoding Functions
- Format Decoding Functions
Decoding is the process by which the byte content of an image file is converted into an object of type MemoryImage in RAM.
The following functions provide a high level interface for decoding images. You can also use the format specific Decoder classes to access format-specific data.
ℹ️ These methods take input of type
TypedArray
, which is short union of types:Int8Array
|Int16Array
|Int32Array
|Uint8Array
|Uint16Array
|Uint32Array
|Float32Array
|Float64Array
.
⚠️ Because these functions have to determine the format of the image before they can decode it, it is preferable to use a format specific decoding function, such asdecodeJpg
, if you know what the format is.
decodeImage(opt: DecodeImageLargestOptions): MemoryImage | undefined
Decode the given image file bytes by first identifying the format of the file and using that decoder to decode the file into a single frame MemoryImage.
Parameters:
-
data: The content of the image file of type
TypedArray
. -
frameIndex (optional): Decode only a specific frame (relevant only for an animated image). This parameter specifies the frame number. Default is
undefined
. -
largest (optional): If
true
, specifies that the largest frame should be decoded if the image is an icon (ICO). Default isfalse
.
Returns the decoded MemoryImage
or undefined
if the data cannot be decoded.
decodeImageByMimeType(opt: DecodeImageByMimeTypeOptions): MemoryImage | undefined
Decodes the given image file bytes, using the MIME type to determine the decoder.
Parameters:
-
data: The content of the image file of type
TypedArray
. -
mimeType: The MIME type. For example,
image/png
. -
frameIndex (optional): Decode only a specific frame (relevant only for an animated image). This parameter specifies the frame number. Default is
undefined
. -
largest (optional): If
true
, specifies that the largest frame should be decoded if the image is an icon (ICO). Default isfalse
.
Returns the decoded MemoryImage
or undefined
if the data cannot be decoded.
decodeNamedImage(opt: DecodeNamedImageOptions): MemoryImage | undefined
Identify the format of the image using the file extension provided by name
, and decode it with the appropriate decoder.
Parameters:
-
data: The content of the image file of type
TypedArray
. -
name: Filename or extension (including dot). For example,
image.png
or.png
. -
frameIndex (optional): Decode only a specific frame (relevant only for an animated image). This parameter specifies the frame number. Default is
undefined
. -
largest (optional): If
true
, specifies that the largest frame should be decoded if the image is an icon (ICO). Default isfalse
.
Returns the decoded MemoryImage
or undefined
if the data cannot be decoded.
findFormatForData(data: TypedArray): ImageFormat
Returns the ImageFormat
for the given file data.
Parameters:
-
data: The content of the image file of type
TypedArray
.
Returns the ImageFormat
corresponding to the provided image data set. If the format is not found, returns ImageFormat.invalid
.
findDecoderForFormat(format: ImageFormat): Decoder | undefined
Returns the Decoder for the given ImageFormat
.
Parameters:
-
format:
ImageFormat
for which a decoder needs to be found.
Returns the Decoder
that can decode the given image format or undefined
if no matching decoder is found.
findDecoderForData(data: TypedArray): Decoder | undefined
Returns the Decoder that can decode the given image data.
Parameters:
-
data: The content of the image file of type
TypedArray
.
Returns the Decoder
that can decode the given image data or undefined
if no matching decoder is found.
findDecoderForMimeType(mimeType: string): Decoder | undefined
Return the Decoder that can decode image of the given MIME type.
Parameters:
-
mimeType: the MIME type, as
string
. For example,image/png
.
Returns the Decoder
that can decode the given image data or undefined
if no matching decoder is found.
findDecoderForNamedImage(name: string): Decoder | undefined
Return the Decoder that can decode image with the given name, by looking at the file extension.
Parameters:
- name: The name of the image to search for the decoder.
Returns the Decoder
that can decode the given image or undefined
if no matching encoder is found.
decodeBmp(opt: DecodeOptions): MemoryImage | undefined
Decode a BMP formatted image.
Parameters:
-
data: The content of the image file of type
TypedArray
.
Returns the decoded MemoryImage
or undefined
if the data cannot be decoded.
decodeGif(opt: DecodeMultiframeOptions): MemoryImage | undefined
Decode a GIF formatted image.
Parameters:
-
data: The content of the image file of type
TypedArray
. -
frameIndex (optional): Decode only a specific frame (relevant only for an animated image). This parameter specifies the frame number. Default is
undefined
.
Returns the decoded MemoryImage
or undefined
if the data cannot be decoded.
decodeIco(opt: DecodeImageLargestOptions): MemoryImage | undefined
Decode an ICO formatted image.
Parameters:
-
data: The content of the image file of type
TypedArray
. -
frameIndex (optional): Decode only a specific frame (relevant only for an animated image). This parameter specifies the frame number. Default is
undefined
. -
largest (optional): If
true
, specifies that the largest frame should be decoded. Default isfalse
.
Returns the decoded MemoryImage
or undefined
if the data cannot be decoded.
decodeJpg(opt: DecodeOptions): MemoryImage | undefined
Decode a JPEG formatted image.
Parameters:
-
data: The contents of the JPEG file of type
TypedArray
.
Returns the decoded MemoryImage
or undefined
if the data cannot be decoded.
decodePng(opt: DecodeMultiframeOptions): MemoryImage | undefined
Decode a PNG formatted image.
Parameters:
-
data: The content of the image file of type
TypedArray
. -
frameIndex (optional): Decode only a specific frame (relevant only for an animated image). This parameter specifies the frame number. Default is
undefined
.
Returns the decoded MemoryImage
or undefined
if the data cannot be decoded.
decodePnm(opt: DecodeOptions): MemoryImage | undefined
Decode a PNM formatted image.
Parameters:
-
data: The contents of the PNM file of type
TypedArray
.
Returns the decoded MemoryImage
or undefined
if the data cannot be decoded.
decodePsd(opt: DecodeOptions): MemoryImage | undefined
Decode a Photoshop PSD formatted image.
Parameters:
-
data: The content of the image file of type
TypedArray
.
Returns the decoded MemoryImage
or undefined
if the data cannot be decoded.
decodePvr(opt: DecodeOptions): MemoryImage | undefined
Decode a PVR formatted image.
Parameters:
-
data: The content of the image file of type
TypedArray
.
Returns the decoded MemoryImage
or undefined
if the data cannot be decoded.
decodeTga(opt: DecodeMultiframeOptions): MemoryImage | undefined
Decode a TGA (Targa) formatted image.
Parameters:
-
data: The content of the image file of type
TypedArray
. -
frameIndex (optional): Decode only a specific frame (relevant only for an animated image). This parameter specifies the frame number. Default is
undefined
.
Returns the decoded MemoryImage
or undefined
if the data cannot be decoded.
decodeTiff(opt: DecodeMultiframeOptions): MemoryImage | undefined
Decode a TIFF formatted image.
Parameters:
-
data: The content of the image file of type
TypedArray
. -
frameIndex (optional): Decode only a specific frame (relevant only for an animated image). This parameter specifies the frame number. Default is
undefined
.
Returns the decoded MemoryImage
or undefined
if the data cannot be decoded.
decodeWebP(opt: DecodeMultiframeOptions): MemoryImage | undefined {
Decode a WebP formatted image.
Parameters:
-
data: The content of the image file of type
TypedArray
. -
frameIndex (optional): Decode only a specific frame (relevant only for an animated image). This parameter specifies the frame number. Default is
undefined
.
Returns the decoded MemoryImage
or undefined
if the data cannot be decoded.