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)

355 lines (354 loc) 13.1 kB
/** @format */ import { InputBuffer } from '../../common/input-buffer.js'; import { MemoryImage } from '../../image/image.js'; import { VP8LTransform } from './vp8l-transform.js'; import { WebPInfo } from './webp-info.js'; /** * Class representing the VP8L decoder. */ export declare class VP8L { /** * The last pixel processed. */ private _lastPixel; /** * The last row processed. */ private _lastRow; /** * The size of the color cache. */ private _colorCacheSize; /** * The color cache. */ private _colorCache?; /** * The Huffman mask. */ private _huffmanMask; /** * The number of bits for Huffman subsampling. */ private _huffmanSubsampleBits; /** * The size of the Huffman image in the x dimension. */ private _huffmanXsize; /** * The Huffman image. */ private _huffmanImage?; /** * The number of Huffman tree groups. */ private _numHtreeGroups; /** * The Huffman tree groups. */ private _htreeGroups; /** * The image transforms. */ protected _transforms: VP8LTransform[]; /** * The number of transforms seen. */ private _transformsSeen; /** * The pixel data. */ protected _pixels?: Uint32Array; /** * The pixel data in 8-bit format. */ private _pixels8; /** * The ARGB cache. */ private _argbCache?; /** * The opaque data. */ protected _opaque?: Uint8Array; /** * The width of the input/output image. */ protected _ioWidth?: number; /** * The height of the input/output image. */ protected _ioHeight?: number; /** * The input buffer. */ private _input; /** * The bit reader. */ private _br; /** * The decoded image. */ private _image?; /** * The WebP information. */ private _webp; /** * Gets the WebP information. */ get webp(): WebPInfo; /** * Constructs a new VP8L decoder. * @param {InputBuffer<Uint8Array>} input - The input buffer. * @param {WebPInfo} webp - The WebP information. */ constructor(input: InputBuffer<Uint8Array>, webp: WebPInfo); /** * Reads a transform from the bit reader. * @param {number[]} transformSize - The size of the transform. * @returns {boolean} True if the transform was read successfully, false otherwise. * @throws {LibError} If an invalid WebP transform type is encountered. */ private readTransform; /** * Extracts paletted alpha rows. * @param {number} row - The row to extract. */ private extractPalettedAlphaRows; /** * Special method for paletted alpha data. * @param {number} numRows - The number of rows. * @param {InputBuffer<Uint8Array>} rows - The input buffer containing the rows. */ private applyInverseTransformsAlpha; /** * Processes (transforms, scales & color-converts) the rows decoded after the * last call. * @param {number} row - The row to process. */ private processRows; /** * Applies inverse transforms to the pixel data. * @param {number} numRows - The number of rows. * @param {number} rows - The starting row. */ private applyInverseTransforms; /** * Reads Huffman codes from the bit reader. * @param {number} xSize - The size in the x dimension. * @param {number} ySize - The size in the y dimension. * @param {number} colorCacheBits - The number of bits for the color cache. * @param {boolean} allowRecursion - Whether recursion is allowed. * @returns {boolean} True if the Huffman codes were read successfully, false otherwise. */ private readHuffmanCodes; /** * Reads a Huffman code from the bit reader. * @param {number} alphabetSize - The size of the alphabet. * @param {HuffmanTree} tree - The Huffman tree to populate. * @returns {boolean} True if the Huffman code was read successfully, false otherwise. */ private readHuffmanCode; /** * Reads Huffman code lengths from the bit reader. * @param {Int32Array} codeLengthCodeLengths - The lengths of the code length codes. * @param {number} numSymbols - The number of symbols. * @param {Int32Array} codeLengths - The array to populate with code lengths. * @returns {boolean} True if the code lengths were read successfully, false otherwise. */ private readHuffmanCodeLengths; /** * Gets the copy distance for a given distance symbol. * @param {number} distanceSymbol - The distance symbol. * @returns {number} The copy distance. */ private getCopyDistance; /** * Gets the copy length for a given length symbol. * @param {number} lengthSymbol - The length symbol. * @returns {number} The copy length. */ private getCopyLength; /** * Converts a plane code to a distance. * @param {number} xsize - The size in the x dimension. * @param {number} planeCode - The plane code. * @returns {number} The distance. */ private planeCodeToDistance; /** * Expands the color map for a given number of colors and applies a transformation. * @param {number} numColors - The number of colors to expand. * @param {VP8LTransform} transform - The transformation to apply, which includes the data and bits for the transformation. * @param {Uint32Array} transform.data - The data for the transformation. * @param {number} transform.bits - The bits for the transformation. * @returns {boolean} A boolean indicating whether the operation was successful. */ private expandColorMap; /** * Retrieves the meta index from the given image data. * * @param {Uint32Array | undefined} image - The image data array. * @param {number} xsize - The width of the image. * @param {number} bits - The number of bits to shift. * @param {number} x - The x-coordinate. * @param {number} y - The y-coordinate. * @returns {number} - The meta index value. */ private getMetaIndex; /** * Retrieves the HuffmanTreeGroup for the given position (x, y). * * @param {number} x - The x-coordinate of the position. * @param {number} y - The y-coordinate of the position. * @returns {HuffmanTreeGroup} The HuffmanTreeGroup corresponding to the given position. */ private getHtreeGroupForPos; /** * Allocates internal buffers for 32-bit pixel storage. * * This method calculates the total number of pixels required for the image, * including scratch buffers for top-prediction rows and temporary BGRA storage. * It then allocates a Uint32Array to hold these pixels and sets up the necessary * internal references. * * @returns {boolean} Always returns true indicating successful allocation. */ protected allocateInternalBuffers32b(): boolean; /** * Allocates internal buffers for 8-bit pixel data. * * This method initializes the internal buffers required for storing * pixel data in an 8-bit format. It calculates the total number of * pixels based on the width and height of the WebP image, pads the * byte buffer to a multiple of 4, and then creates the necessary * Uint8Array and Uint32Array buffers. * * @returns {boolean} Always returns true to indicate successful allocation. */ protected allocateInternalBuffers8b(): boolean; /** * Decodes an image stream and returns the decoded image data as a Uint32Array. * If `isLevel0` is true, the function sets up the necessary parameters and returns undefined. * * @param {number} xsize - The width of the image. * @param {number} ysize - The height of the image. * @param {boolean} isLevel0 - Indicates if the current level is 0. * @returns {Uint32Array | undefined} - The decoded image data or undefined if `isLevel0` is true. * @throws {LibError} - Throws an error if an invalid transform, color cache, or Huffman codes are encountered. */ protected decodeImageStream(xsize: number, ysize: number, isLevel0: boolean): Uint32Array | undefined; /** * Decodes image data from a given Uint32Array. * * @param {Uint32Array} data - The array to store the decoded image data. * @param {number} width - The width of the image. * @param {number} height - The height of the image. * @param {number} lastRow - The last row to decode. * @param {(_: number) => void} [processFunc] - Optional function to process each row. * @returns {boolean} - Returns true if decoding is successful, otherwise false. */ protected decodeImageData(data: Uint32Array, width: number, height: number, lastRow: number, processFunc?: (_: number) => void): boolean; /** * Determines if the current instance is optimizable for 8-bit processing. * * This method checks if the color cache size is zero and if the Huffman trees * for red, blue, and alpha channels in all Huffman tree groups contain only one symbol. * If these conditions are met, the instance is considered optimizable for 8-bit processing. * * @returns {boolean} - Returns true if the instance is optimizable for 8-bit processing, otherwise false. */ protected is8bOptimizable(): boolean; /** * Extracts alpha rows from the given row number. * * @param row - The row number to extract alpha rows from. */ protected extractAlphaRows(row: number): void; /** * Decodes alpha data for an image. * * @param {number} width - The width of the image. * @param {number} height - The height of the image. * @param {number} lastRow - The last row to decode. * @returns {boolean} A boolean indicating if the decoding was successful. */ protected decodeAlphaData(width: number, height: number, lastRow: number): boolean; /** * Decodes the header of a WebP image. * * This method reads the signature, format, dimensions, alpha presence, and version * from the bit reader and validates them against expected values. * * @returns {boolean} True if the header is successfully decoded and valid, false otherwise. */ decodeHeader(): boolean; /** * Decodes the image data and returns a MemoryImage object. * * @returns {MemoryImage | undefined} The decoded MemoryImage object or undefined if decoding fails. */ decode(): MemoryImage | undefined; /** * Calculates the sub-sample size based on the given size and sampling bits. * * @param {number} size - The original size to be sub-sampled. * @param {number} samplingBits - The number of bits used for sampling. * @returns {number} The calculated sub-sample size. */ protected static subSampleSize(size: number, samplingBits: number): number; /** Green color channel index */ static readonly green = 0; /** Red color channel index */ static readonly red = 1; /** Blue color channel index */ static readonly blue = 2; /** Alpha channel index */ static readonly alpha = 3; /** Distance index */ static readonly dist = 4; /** Number of ARGB cache rows */ static readonly numArgbCacheRows = 16; /** Number of code length codes */ static readonly numCodeLengthCodes = 19; /** Order of code length codes */ static readonly codeLengthCodeOrder: number[]; /** Number of plane codes */ static readonly codeToPlaneCodes = 120; /** Mapping of codes to planes */ static readonly codeToPlane: number[]; /** Code length literals */ static readonly codeLengthLiterals = 16; /** Code length repeat code */ static readonly codeLengthRepeatCode = 16; /** Extra bits for code length */ static readonly codeLengthExtraBits: number[]; /** Repeat offsets for code length */ static readonly codeLengthRepeatOffsets: number[]; /** ARGB value for black color */ static readonly argbBlack = 4278190080; /** Maximum cache bits */ static readonly maxCacheBits = 11; /** Number of Huffman codes per meta code */ static readonly huffmanCodesPerMetaCode = 5; /** Default code length */ static readonly defaultCodeLength = 8; /** Maximum allowed code length */ static readonly maxAllowedCodeLength = 15; /** Number of literal codes */ static readonly numLiteralCodes = 256; /** Number of length codes */ static readonly numLengthCodes = 24; /** Number of distance codes */ static readonly numDistanceCodes = 40; /** Number of code length codes */ static readonly codeLengthCodes = 19; /** Alphabet size for different contexts */ static readonly alphabetSize: number[]; /** Magic byte for VP8L */ static readonly vp8lMagicByte = 47; /** Version for VP8L */ static readonly vp8lVersion = 0; }