UNPKG

image-in-browser

Version:

Package for encoding / decoding images, transforming images, applying filters, drawing primitives on images on the client side (no need for server Node.js)

254 lines (253 loc) 7.09 kB
/** @format */ import { Encoder, EncoderEncodeOptions } from './encoder.js'; import { MemoryImage } from '../image/image.js'; import { DitherKernel } from '../filter/dither-kernel.js'; /** * Interface for initializing GifEncoder options. */ export interface GifEncoderInitOptions { /** The delay between frames in milliseconds. */ delay?: number; /** The number of times the GIF should loop. 0 means infinite looping. */ repeat?: number; /** The sampling factor for color quantization. */ samplingFactor?: number; /** The dithering kernel to use. */ dither?: DitherKernel; /** Whether to use serpentine dithering. */ ditherSerpentine?: boolean; } /** * Class for encoding GIF images. */ export declare class GifEncoder implements Encoder { /** * GIF89a identifier. */ private static readonly _gif89Id; /** * Image Descriptor Record Type. */ private static readonly _imageDescRecordType; /** * Extension Record Type. */ private static readonly _extensionRecordType; /** * Terminate Record Type. */ private static readonly _terminateRecordType; /** * Application Extension. */ private static readonly _applicationExt; /** * Graphic Control Extension. */ private static readonly _graphicControlExt; /** * End of File. */ private static readonly _eof; /** * Number of bits. */ private static readonly _bits; /** * Hash table size. */ private static readonly _hSize; /** * Masks for bit operations. */ private static readonly _masks; /** * Delay between frames. */ private _delay; /** * Repeat count for the GIF. */ private _repeat; /** * Number of colors in the palette. */ private _numColors; /** * Type of quantizer to use. */ private _quantizerType; /** * Sampling factor for the quantizer. */ private _samplingFactor; /** * Last image added to the encoder. */ private _lastImage?; /** * Duration of the last image. */ private _lastImageDuration?; /** * Last color map used. */ private _lastColorMap?; /** * Width of the GIF. */ private _width; /** * Height of the GIF. */ private _height; /** * Number of encoded frames. */ private _encodedFrames; /** * Current accumulator for LZW encoding. */ private _curAccum; /** * Current bits for LZW encoding. */ private _curBits; /** * Number of bits for LZW encoding. */ private _nBits; /** * Initial number of bits for LZW encoding. */ private _initBits; /** * End of file code for LZW encoding. */ private _eofCode; /** * Maximum code for LZW encoding. */ private _maxCode; /** * Clear code for LZW encoding. */ private _clearCode; /** * Free entry for LZW encoding. */ private _freeEnt; /** * Flag indicating if the clear code was used. */ private _clearFlag; /** * Block of bytes for LZW encoding. */ private _block; /** * Size of the block for LZW encoding. */ private _blockSize; /** * Output buffer for the encoded GIF. */ private _outputBuffer?; /** * Dither kernel to use. */ private _dither; /** * Flag indicating if serpentine dithering is used. */ private _ditherSerpentine; /** * Does this encoder support animation? */ private readonly _supportsAnimation; /** * Getter for supportsAnimation. */ get supportsAnimation(): boolean; /** * Constructor for GifEncoder. * @param {GifEncoderInitOptions} [opt] - Initialization options. * @param {number} [opt.delay] - The delay between frames in milliseconds. Default is 80. * @param {number} [opt.repeat] - The number of times the animation should repeat. Default is 0 (infinite). * @param {number} [opt.samplingFactor] - The factor by which to downsample the image. Default is 10. * @param {string} [opt.dither] - The dithering algorithm to use. Default is Floyd-Steinberg. * @param {boolean} [opt.ditherSerpentine] - Whether to use serpentine dithering. Default is false. */ constructor(opt?: GifEncoderInitOptions); /** * Adds an image to the GIF. * @param {MemoryImage} image - The image to add. * @param {number} width - The width of the image. * @param {number} height - The height of the image. * @throws {LibError} Throws an error if the image does not have a palette. */ private addImage; /** * Encodes the image using LZW compression. * @param {MemoryImage} image - The image to encode. */ private encodeLZW; /** * Outputs the given code. * @param {number | undefined} code - The code to output. */ private output; /** * Writes the current block to the output buffer. */ private writeBlock; /** * Adds a byte to the current block. * @param {number} c - The byte to add. */ private addToBlock; /** * Writes the application extension block. */ private writeApplicationExt; /** * Writes the graphics control extension block. * @param {MemoryImage} image - The image for which the extension is written. */ private writeGraphicsCtrlExt; /** * Writes the GIF header and Logical Screen Descriptor. * @param {number} width - The width of the GIF. * @param {number} height - The height of the GIF. */ private writeHeader; /** * Encode the images that were added with **addFrame**. * After this has been called (returning the finished GIF), * calling **addFrame** for a new animation or image is safe again. * * **addFrame** will not encode the first image passed and after that * always encode the previous image. Hence, the last image needs to be * encoded here. * * @returns {Uint8Array | undefined} The encoded GIF as a byte array, or undefined if there was an error. */ private finish; /** * This adds the frame passed to **image**. * After the last frame has been added, **finish** is required to be called. * Optional frame **duration** is in 1/100 sec. * * @param {MemoryImage} image - The image to add as a frame. * @param {number} [duration] - Optional duration of the frame in 1/100 sec. */ addFrame(image: MemoryImage, duration?: number): void; /** * Encode a single frame image. * @param {EncoderEncodeOptions} opt - The options for encoding. * @param {MemoryImage} opt.image - The image to encode. * @param {boolean} [opt.singleFrame] - Optional flag to encode a single frame. * @returns {Uint8Array} The encoded image as a Uint8Array. */ encode(opt: EncoderEncodeOptions): Uint8Array; }