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)

215 lines (214 loc) 7.88 kB
/** @format */ import { Decoder, DecoderDecodeOptions } from './decoder.js'; import { GifInfo } from './gif/gif-info.js'; import { MemoryImage } from '../image/image.js'; import { ImageFormat } from './image-format.js'; /** * A decoder for the GIF image format. This supports both single frame and * animated GIF files, and transparency. */ export declare class GifDecoder implements Decoder { /** Size of the GIF stamp */ private static readonly _stampSize; /** GIF87a stamp */ private static readonly _gif87Stamp; /** GIF89a stamp */ private static readonly _gif89Stamp; /** Image descriptor record type */ private static readonly _imageDescRecordType; /** Extension record type */ private static readonly _extensionRecordType; /** Terminate record type */ private static readonly _terminateRecordType; /** Graphic control extension */ private static readonly _graphicControlExt; /** Application extension */ private static readonly _applicationExt; /** Maximum LZ code */ private static readonly _lzMaxCode; /** LZ bits */ private static readonly _lzBits; /** Impossible code, to signal empty. */ private static readonly _noSuchCode; /** Code masks */ private static readonly _codeMasks; /** Interlaced offset */ private static readonly _interlacedOffset; /** Interlaced jump */ private static readonly _interlacedJump; /** Input buffer */ private _input?; /** GIF information */ private _info?; /** Repeat count */ private _repeat; /** Buffer */ private _buffer?; /** Stack */ private _stack; /** Suffix */ private _suffix; /** Prefix */ private _prefix?; /** Bits per pixel */ private _bitsPerPixel; /** Pixel count */ private _pixelCount?; /** Current shift DWORD */ private _currentShiftDWord; /** Current shift state */ private _currentShiftState; /** Stack pointer */ private _stackPtr; /** Current code */ private _currentCode?; /** Last code */ private _lastCode; /** Max code + 1 */ private _maxCode1; /** Running bits */ private _runningBits; /** Running code */ private _runningCode; /** EOF code */ private _eofCode; /** Clear code */ private _clearCode; /** Transparent flag */ private _transparentFlag; /** Disposal method */ private _disposalMethod; /** Transparent color index */ private _transparent; /** Frame duration */ private _duration; /** * Get the image format. */ get format(): ImageFormat; /** * How many frames are available to decode? * * You should have prepared the decoder by either passing the file bytes * to the constructor, or calling getInfo. */ get numFrames(): number; /** * Constructor for GifDecoder. * @param {Uint8Array} [bytes] - Optional bytes to start decoding. */ constructor(bytes?: Uint8Array); /** * Routine to trace the Prefixes linked list until we get a prefix which is * not code, but a pixel value (less than ClearCode). Returns that pixel value. * If image is defective, we might loop here forever, so we limit the loops to * the maximum possible if image O.k. - lzMaxCode times. * @param {Uint32Array} prefix - The prefix array. * @param {number} code - The code to trace. * @param {number} clearCode - The clear code. * @returns {number} The prefix character. */ private static getPrefixChar; /** * Update the image with the given line. * @param {MemoryImage} image - The memory image. * @param {number} y - The y-coordinate. * @param {GifColorMap} [colorMap] - The color map. * @param {Uint8Array} line - The line of pixels. */ private static updateImage; /** * Get information about the GIF. * @returns {boolean} True if successful, false otherwise. */ private getInfo; /** * Skip the current image. * @returns {GifImageDesc | undefined} The skipped image descriptor or undefined. */ private skipImage; /** * Continue to get the image code in compressed form. This routine should be * called until NULL block is returned. * The block should NOT be freed by the user (not dynamically allocated). * @returns {boolean} True if successful, false otherwise. */ private skipRemainder; /** * Read the application extension. * @param {InputBuffer<Uint8Array>} input - The input buffer. */ private readApplicationExt; /** * Read the graphics control extension. * @param {InputBuffer<Uint8Array>} input - The input buffer. */ private readGraphicsControlExt; /** * Get a line of pixels. * @param {Uint8Array} line - The line of pixels. * @returns {boolean} True if successful, false otherwise. */ private getLine; /** * The LZ decompression routine: * This version decompress the given gif file into Line of length LineLen. * This routine can be called few times (one per scan line, for example), in * order the complete the whole image. * @param {Uint8Array} line - The line of pixels. * @returns {boolean} True if successful, false otherwise. */ private decompressLine; /** * The LZ decompression input routine: * This routine is responsible for the decompression of the bit stream from * 8 bits (bytes) packets, into the real codes. * @returns {number | undefined} The decompressed code or undefined. */ private decompressInput; /** * This routines read one gif data block at a time and buffers it internally * so that the decompression routine could access it. * The routine returns the next byte from its internal buffer (or read next * block in if buffer empty) and returns undefined on failure. * @returns {number | undefined} The next byte from the internal buffer or undefined on failure. */ private bufferedInput; /** * Initializes the decoding process by setting up necessary buffers and arrays. */ private initDecode; /** * Decodes a given Gif image description into a MemoryImage. * @param {GifImageDesc} gifImage - The Gif image description to decode. * @returns {MemoryImage | undefined} The decoded MemoryImage or undefined on failure. */ private decodeImage; /** * Is the given file a valid Gif image? * @param {Uint8Array} bytes - The bytes of the file to check. * @returns {boolean} True if the file is a valid Gif image, false otherwise. */ isValidFile(bytes: Uint8Array): boolean; /** * Validate the file is a Gif image and get information about it. * If the file is not a valid Gif image, undefined is returned. * @param {Uint8Array} bytes - The bytes of the file to decode. * @returns {GifInfo | undefined} The Gif information or undefined if the file is not valid. */ startDecode(bytes: Uint8Array): GifInfo | undefined; /** * Decodes the Gif image based on the provided options. * @param {DecoderDecodeOptions} opt - The decoding options. * @param {Uint8Array} opt.bytes - The bytes of the Gif image. * @param {number} [opt.frameIndex] - The index of the frame to decode (optional). * @returns {MemoryImage | undefined} The decoded MemoryImage or undefined on failure. */ decode(opt: DecoderDecodeOptions): MemoryImage | undefined; /** * Decodes a specific frame of the Gif image. * @param {number} frameIndex - The index of the frame to decode. * @returns {MemoryImage | undefined} The decoded MemoryImage or undefined on failure. */ decodeFrame(frameIndex: number): MemoryImage | undefined; }