-
Notifications
You must be signed in to change notification settings - Fork 4
Format Encoding Functions
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.
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(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(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(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.
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(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(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(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(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(opt: EncodeOptions): Uint8Array
Encode an image to the PVR format.
Parameters:
-
image: the
MemoryImage
to encode.
Returns the encoded bytes of type Uint8Array
.
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(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(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
.