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)
340 lines (339 loc) • 11.5 kB
TypeScript
/** @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';
/** VP8L lossless WebP decoder. */
export declare class VP8L {
/** 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[];
static readonly huffmanTableBits = 8;
static readonly codeToPlaneCodes = 120;
static readonly codeToPlane: number[];
static readonly lengthsTableBits = 7;
static readonly lengthsTableMask: number;
static readonly codeLengthLiterals = 16;
static readonly codeLengthRepeatCode = 16;
static readonly codeLengthExtraBits: number[];
static readonly codeLengthRepeatOffsets: number[];
static readonly argbBlack = 4278190080;
static readonly maxCacheBits = 11;
static readonly huffmanCodesPerMetaCode = 5;
static readonly huffmanPackedBits = 6;
static readonly huffmanPackedTableSize: number;
static readonly minHuffmanBits = 2;
static readonly numHuffmanBits = 3;
static readonly defaultCodeLength = 8;
static readonly maxAllowedCodeLength = 15;
static readonly numLiteralCodes = 256;
static readonly numLengthCodes = 24;
static readonly numDistanceCodes = 40;
static readonly codeLengthCodes = 19;
static readonly packedNonLiteralCode = 0;
static readonly bitsSpecialMarker = 256;
static readonly alphabetSize: number[];
static readonly fixedTableSize: number;
static readonly tableSize: number[];
static readonly literalMap: number[];
static readonly vp8lMagicByte = 47;
static readonly vp8lVersion = 0;
/** Input buffer for bit reading */
private _input;
/** Bit reader for VP8L stream */
private _br;
/** WebP image info */
private _webp;
/** Decoded image */
private _image?;
/** Alpha decoder */
private _alphaDec?;
/** Huffman tables */
private _huffmanTables?;
/** Huffman tree groups */
private _htreeGroups;
/** Huffman image */
private _huffmanImage?;
/** Pixel data (32-bit) */
protected _pixels?: Uint32Array;
/** Pixel data (8-bit) */
private _pixels8;
/** Opaque alpha data */
protected _opaque?: Uint8Array;
/** Image transforms */
protected _transforms: VP8LTransform[];
/** Width of the image */
protected _ioWidth: number;
/** Height of the image */
protected _ioHeight: number;
/** Color cache */
private _colorCache?;
/** Size of color cache */
private _colorCacheSize;
/** Huffman mask */
private _huffmanMask;
/** Huffman subsample bits */
private _huffmanSubsampleBits;
/** Huffman image X size */
private _huffmanXsize;
/** Number of Huffman tree groups */
private _numHtreeGroups;
/** Number of transforms seen */
private _transformsSeen;
/** ARGB cache offset */
private _argbCache;
/** Last processed pixel */
private _lastPixel;
/** Last processed row */
private _lastRow;
/** Returns WebP info */
get webp(): WebPInfo;
/**
* Creates a new VP8L decoder.
* @param input Input buffer for bit reading.
* @param webp WebP image info.
*/
constructor(input: InputBuffer<Uint8Array>, webp: WebPInfo);
/**
* Decodes the header of a WebP image.
* @returns True if valid, false otherwise.
*/
decodeHeader(): boolean;
/**
* Decodes the image and returns a MemoryImage or undefined on failure.
* @returns Decoded image or undefined if failed.
*/
decode(): MemoryImage | undefined;
/**
* Allocates 32-bit internal pixel buffers.
* @param finalWidth Width of the image.
* @returns True if successful.
*/
protected allocateInternalBuffers32b(finalWidth: number): boolean;
/**
* Allocates 8-bit internal pixel buffers.
* @returns True if successful.
*/
protected allocateInternalBuffers8b(): boolean;
/**
* Decodes the image stream and returns decoded data or undefined for level 0.
* @param xsize Width of the image.
* @param ysize Height of the image.
* @param isLevel0 Whether this is level 0.
* @returns Decoded data or undefined.
*/
protected decodeImageStream(xsize: number, ysize: number, isLevel0: boolean): Uint32Array | undefined;
/**
* Decodes image data into the provided buffer.
* @param data Buffer to write decoded data.
* @param width Image width.
* @param height Image height.
* @param lastRow Last row to process.
* @param processFunc Optional row processing callback.
* @returns True if successful.
*/
protected decodeImageData(data: Uint32Array, width: number, height: number, lastRow: number, processFunc?: (_row: number, _waitForBiggestBatch: boolean) => void): boolean;
/**
* Reads a transform from the bit reader.
* @param transformSize Array with [width, height] to update.
* @returns True if successful.
*/
private readTransform;
/**
* Expands the color map for color indexing transform.
* @param numColors Number of colors.
* @param transform Transform to expand.
* @returns True if successful.
*/
private expandColorMap;
/**
* Returns the meta index for Huffman group lookup.
* @param image Huffman image.
* @param xsize Image width.
* @param bits Subsample bits.
* @param x X position.
* @param y Y position.
* @returns Meta index.
*/
private getMetaIndex;
/**
* Returns HuffmanTreeGroup for given pixel position.
* @param x X position.
* @param y Y position.
* @returns Huffman tree group.
*/
private getHtreeGroupForPos;
/**
* Applies inverse transforms to pixel data.
* @param startRow Start row.
* @param numRows Number of rows.
* @param rows Row offset.
*/
private applyInverseTransforms;
/**
* Processes decoded rows and writes to MemoryImage.
* @param row Current row.
* @param waitForBiggestBatch Whether to wait for the biggest batch.
*/
private processRows;
/**
* Reads Huffman codes from the bit reader.
* @param xSize Image width.
* @param ySize Image height.
* @param colorCacheBits Color cache bits.
* @param allowRecursion Allow recursion.
* @returns True if successful.
*/
private readHuffmanCodes;
/**
* Reads Huffman code for a given alphabet size.
* @param alphabetSize Size of the alphabet.
* @param codeLengths Array to store code lengths.
* @param table Huffman tables.
* @returns Table size.
*/
private readHuffmanCode;
/**
* Reads Huffman code lengths from the bit reader.
* @param codeLengthCodeLengths Code length code lengths.
* @param numSymbols Number of symbols.
* @param codeLengths Array to store code lengths.
* @returns True if successful.
*/
private readHuffmanCodeLengths;
/**
* Helper for reading Huffman codes for all groups.
* @param colorCacheBits Color cache bits.
* @param numHtreeGroups Number of Huffman tree groups.
* @param numHtreeGroupsMax Maximum number of Huffman tree groups.
* @param mapping Optional mapping array.
* @returns Array of Huffman tree groups or undefined.
*/
private readHuffmanCodesHelper;
/**
* Builds packed Huffman table for fast decoding.
* @param htreeGroup Huffman tree group.
*/
private buildPackedTable;
/**
* Accumulates Huffman code bits and value.
* @param hcode Huffman code.
* @param shift Bit shift.
* @param huff Huffman code 32.
* @returns Number of bits accumulated.
*/
private accumulateHCode;
/**
* Returns the copy distance for a given distance symbol.
* @param distanceSymbol Distance symbol.
* @returns Copy distance.
*/
private getCopyDistance;
/**
* Returns the copy length for a given length symbol.
* @param lengthSymbol Length symbol.
* @returns Copy length.
*/
private getCopyLength;
/**
* Converts a plane code to a distance.
* @param xsize Image width.
* @param planeCode Plane code.
* @returns Distance.
*/
private planeCodeToDistance;
/**
* Returns true if the instance is optimizable for 8-bit processing.
* @returns True if optimizable.
*/
protected is8bOptimizable(): boolean;
/**
* Extracts paletted alpha rows for the given row.
* @param row Row number.
*/
private extractPalettedAlphaRows;
/**
* Applies inverse transforms for paletted alpha.
* @param numRows Number of rows.
* @param rows Input buffer for rows.
*/
private applyInverseTransformsAlpha;
/**
* Extracts alpha rows from the given row number.
* @param lastRow Last row to extract.
* @param waitForBiggestBatch Whether to wait for the biggest batch.
*/
protected extractAlphaRows(lastRow: number, waitForBiggestBatch: boolean): void;
/**
* Decodes alpha data for an image.
* @param width Image width.
* @param height Image height.
* @param lastRow Last row to decode.
* @returns True if successful.
*/
protected decodeAlphaData(width: number, height: number, lastRow: number): boolean;
/**
* Replicates value in Huffman table.
* @param table Huffman code list.
* @param key Table key.
* @param step Step size.
* @param end End index.
* @param bits Number of bits.
* @param value Value to replicate.
*/
private replicateValue;
/**
* Returns the table width of the next 2nd level table.
* @param count Count array.
* @param len Current length.
* @param rootBits Root bits.
* @returns Table width.
*/
private nextTableBitSize;
/**
* Returns next Huffman key.
* @param key Current key.
* @param len Length.
* @returns Next key.
*/
private getNextKey;
/**
* Builds Huffman table for decoding.
* @param rootTable Root Huffman code list.
* @param rootBits Root bits.
* @param codeLengths Code lengths array.
* @param codeLengthsSize Size of code lengths array.
* @param sorted Optional sorted array.
* @returns Total table size.
*/
private buildHuffmanTable;
/**
* Builds Huffman table for VP8L decoding.
* @param rootTable Huffman tables.
* @param rootBits Root bits.
* @param codeLengths Code lengths array.
* @param codeLengthsSize Size of code lengths array.
* @returns Total table size.
*/
private vp8lBuildHuffmanTable;
/**
* Calculates the sub-sample size for transforms.
* @param size Size.
* @param samplingBits Sampling bits.
* @returns Sub-sample size.
*/
protected static subSampleSize(size: number, samplingBits: number): number;
}