Skip to content

Format Decoding Functions

Yegor Pelykh edited this page May 10, 2024 · 7 revisions

Format Decoding Functions

What is Decoding

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.

Generic Decoding Functions

⚠️ 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 as decodeJpg, if you know what the format is.

decodeImage

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 is false.

Returns the decoded MemoryImage or undefined if the data cannot be decoded.

decodeImageByMimeType

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 is false.

Returns the decoded MemoryImage or undefined if the data cannot be decoded.

decodeNamedImage

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 is false.

Returns the decoded MemoryImage or undefined if the data cannot be decoded.

findFormatForData

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

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

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

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

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.

Format-Specific Decoding Functions

decodeBmp

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

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

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 is false.

Returns the decoded MemoryImage or undefined if the data cannot be decoded.

decodeJpg

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

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

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

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

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

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

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

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.