Skip to content

Format Encoding Functions

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

Format Encoding Functions

What is Encoding

Encoding is the process by which the object of type MemoryImage is converted into the byte content of an image file.

The following functions provide a high level interface for encoding images. You can also use the format specific Encoder classes to access format-specific data.

Generic Encoding Functions

encodeImageByMimeType

encodeImageByMimeType(opt: EncodeImageByMimeTypeOptions): Uint8Array | undefined

Encode the MemoryImage to the format determined by the MIME type. If a format wasn't able to be identified, undefined will be returned.

Parameters:

  • mimeType: the MIME type, as string. For example, image/png.
  • image: the MemoryImage to encode.

Returns the encoded bytes of type Uint8Array or undefined if no matching encoder is found.

encodeNamedImage

encodeNamedImage(opt: EncodeNamedImageOptions): Uint8Array | undefined

Identify the format of the image and encode it with the appropriate encoder.

Parameters:

  • name: The name of the image, used to derive the format to encode with from the extension.
  • image: the MemoryImage to encode.

Returns the encoded bytes of type Uint8Array or undefined if no matching encoder is found.

findEncoderForMimeType

findEncoderForMimeType(mimeType: string): Encoder | undefined

Return the Encoder that can encode image with the given MIME type.

Parameters:

  • mimeType: the MIME type, as string. For example, image/png.

Returns the Encoder that can encode the given image or undefined if no matching encoder is found.

findEncoderForNamedImage

findEncoderForNamedImage(name: string): Encoder | undefined

Return the Encoder that can encode image with the given name, by looking at the file extension.

Parameters:

  • name: The name of the image to search for the encoder.

Returns the Encoder that can encode the given image or undefined if no matching encoder is found.

Format-Specific Encoding Functions

encodeJpg

encodeJpg(opt: EncodeJpgOptions): Uint8Array

Encode an image with the JPEG format.

Parameters:

  • image: the MemoryImage to encode.
  • quality (optional): the JPEG quality, in the range [0, 100] where 100 is highest quality. Default is 100.
  • chroma (optional): JPEG Chroma (sub)sampling format. Default is JpegChroma.yuv444.

Returns the encoded bytes of type Uint8Array.

encodePng

encodePng(opt: EncodePngOptions): Uint8Array

Encode an image with the PNG format.

Parameters:

  • image: the MemoryImage to encode.
  • singleFrame (optional): specifies whether only one frame should be encoded. Default is false.
  • level (optional): the compression level, in the range [0, 9] where 9 is the most compressed. Default is 6.
  • filter (optional): specifies the encoding filter algorithm. Default is PngFilterType.paeth.

Returns the encoded bytes of type Uint8Array.

encodeTga

encodeTga(opt: EncodeOptions): Uint8Array

Encode an image with the TGA (Targa) format.

Parameters:

  • image: the MemoryImage to encode.

Returns the encoded bytes of type Uint8Array.

encodeGif

encodeGif(opt: EncodeGifOptions): Uint8Array

Encode an image with the GIF format.

Parameters:

  • image: the MemoryImage to encode.
  • singleFrame (optional): specifies whether only one frame should be encoded. Default is false.
  • repeat (optional): the loop count. Default is 0.
  • samplingFactor (optional): the sampling factor for image quantization, it is responsible for reducing the amount of unique colors in your images to 256. The default is 10.
  • dither (optional): the dithering kernel type to reduce banding patterns when reducing the number of colors. Default is DitherKernel.floydSteinberg.
  • ditherSerpentine (optional): specifies whether the "serpentine scanning" mode should be enabled (when the horizontal direction of scan alternates between lines). Default is false.

Returns the encoded bytes of type Uint8Array.

encodeTiff

encodeTiff(opt: EncodeAnimatedOptions): Uint8Array

Encode an image with the TIFF format.

Parameters:

  • image: the MemoryImage to encode.
  • singleFrame (optional): specifies whether only one frame should be encoded. Default is false.

Returns the encoded bytes of type Uint8Array.

encodePvr

encodePvr(opt: EncodeOptions): Uint8Array

Encode an image to the PVR format.

Parameters:

  • image: the MemoryImage to encode.

Returns the encoded bytes of type Uint8Array.

encodeBmp

encodeBmp(opt: EncodeOptions): Uint8Array

Encode an image with the BMP format.

Parameters:

  • image: the MemoryImage to encode.

Returns the encoded bytes of type Uint8Array.

encodeIco

encodeIco(opt: EncodeAnimatedOptions): Uint8Array

Encode an image with the ICO format.

Parameters:

  • image: the MemoryImage to encode.
  • singleFrame (optional): specifies whether only one frame should be encoded. Default is false.

Returns the encoded bytes of type Uint8Array.

encodeIcoImages

encodeIcoImages(opt: EncodeIcoImagesOptions): Uint8Array

Encode an array of images into the single entity of the ICO format.

Parameters:

  • images: the array of MemoryImage instances to encode.

Returns the encoded bytes of type Uint8Array.