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)

451 lines (450 loc) 12.5 kB
/** @format */ import { InputBuffer } from '../../common/input-buffer.js'; import { MemoryImage } from '../../image/image.js'; import { WebPInfo } from './webp-info.js'; import { WebPInfoInternal } from './webp-info-internal.js'; /** * WebP lossy format decoder (VP8) */ export declare class VP8 { static readonly filterExtraRows: number[]; static readonly vp8Signature = 2752925; static readonly mbFeatureTreeProbs = 3; static readonly numMbSegments = 4; static readonly numRefLfDeltas = 4; static readonly numModeLfDeltas = 4; static readonly maxNumPartitions = 8; static readonly bDcPred = 0; static readonly bTmPred = 1; static readonly bVePred = 2; static readonly bHePred = 3; static readonly bRdPred = 4; static readonly bVrPred = 5; static readonly bLdPred = 6; static readonly bVlPred = 7; static readonly bHdPred = 8; static readonly bHuPred = 9; static readonly numBModes: number; static readonly dcPred = 0; static readonly vPred = 2; static readonly hPred = 3; static readonly tmPred = 1; static readonly bPred: number; static readonly bDcPredNoTop = 4; static readonly bDcPredNoLeft = 5; static readonly bDcPredNoTopLeft = 6; static readonly numBDcModes = 7; static readonly numTypes = 4; static readonly numBands = 8; static readonly numCtx = 3; static readonly numProbas = 11; static readonly bps = 32; static readonly yuvSize: number; static readonly ySize: number; static readonly yOffset: number; static readonly uOffset: number; static readonly vOffset: number; static readonly yuvFix = 16; static readonly yuvHalf: number; static readonly yuvMask: number; static readonly yuvRangeMin = -227; static readonly yuvRangeMax: number; static readonly yuvFix2 = 14; static readonly yuvHalf2: number; static readonly yuvMask2: number; static readonly xorYuvMask2: number; static readonly kYScale = 19077; static readonly kVToR = 26149; static readonly kUToG = 6419; static readonly kVToG = 13320; static readonly kUToB = 33050; static readonly kRCst: number; static readonly kGCst: number; static readonly kBCst: number; static readonly kYModesIntra4: number[]; static readonly kBModesProba: Array<Array<Array<number>>>; static readonly coeffsProba0: Array<Array<Array<Array<number>>>>; static readonly coeffsUpdateProba: Array<Array<Array<Array<number>>>>; static readonly dcTable: number[]; static readonly acTable: number[]; static readonly kScan: number[]; static readonly kBands: number[]; static readonly kCat3: number[]; static readonly kCat4: number[]; static readonly kCat5: number[]; static readonly kCat6: number[]; static readonly kCat3456: Array<Array<number>>; static readonly kZigzag: number[]; static readonly kFilterExtraRows: number[]; /** * Returns public WebP info. */ get webp(): WebPInfo; /** VP8 Frame Header */ private readonly _frameHeader; /** VP8 Picture Header */ private readonly _picHeader; /** VP8 Filter Header */ private readonly _filterHeader; /** VP8 Segment Header */ private readonly _segmentHeader; /** Internal WebP info */ private readonly _webp; /** Input buffer */ private _input; /** Bit reader for main data */ private _br; /** DSP/filter instance */ private _dsp; /** Output image */ private _output?; /** Crop left */ private _cropLeft; /** Crop right */ private _cropRight; /** Crop top */ private _cropTop; /** Crop bottom */ private _cropBottom; /** Macroblock width */ private _mbWidth; /** Macroblock height */ private _mbHeight; /** Top-left macroblock X for filtering */ private _tlMbX; /** Top-left macroblock Y for filtering */ private _tlMbY; /** Bottom-right macroblock X for decoding */ private _brMbX; /** Bottom-right macroblock Y for decoding */ private _brMbY; /** Number of partitions */ private _numPartitions; /** Per-partition boolean decoders */ private readonly _partitions; /** Probabilities */ private _proba?; /** Use skip probability */ private _useSkipProba; /** Skip probability */ private _skipP; /** Dequantization matrices per segment */ private readonly _dqm; /** Current segment index */ private _segment; /** Top intra modes (4 * _mbWidth) */ private _intraT?; /** Left intra modes */ private readonly _intraL; /** Top Y/U/V samples */ private _yuvT; /** Macroblock info (mb_w_ + 1) */ private _mbInfo; /** Macroblock data */ private _mbData; /** Filter info per macroblock */ private _fInfo; /** Precomputed filter strengths */ private _fStrengths; /** Filter type: 0=off, 1=simple, 2=complex */ private _filterType; /** Main YUV block buffer */ private _yuvBlock; /** Macroblock row cache for Y */ private _cacheY; /** Macroblock row cache for U */ private _cacheU; /** Macroblock row cache for V */ private _cacheV; /** Stride for Y cache */ private _cacheYStride; /** Stride for UV cache */ private _cacheUVStride; /** Temporary Y buffer */ private _tmpY; /** Temporary U buffer */ private _tmpU; /** Temporary V buffer */ private _tmpV; /** Current Y buffer */ private _y; /** Current U buffer */ private _u; /** Current V buffer */ private _v; /** Current alpha buffer */ private _a?; /** Current macroblock X */ private _mbX; /** Current macroblock Y */ private _mbY; /** Dithering enabled */ private readonly _dither; /** Alpha-plane decoder */ private _alpha?; /** Compressed alpha data */ private _alphaData?; /** Alpha plane buffer */ private _alphaPlane; /** * Create VP8 decoder instance. * @param input Input buffer * @param webp WebP info */ constructor(input: InputBuffer<Uint8Array>, webp: WebPInfoInternal); /** * Decode the input stream and return the decoded image. * @returns Decoded image or undefined if failed. */ decode(): MemoryImage | undefined; /** * Decode VP8 header from input stream. * @returns True if header is valid. */ decodeHeader(): boolean; /** * Compute vertical position of macroblock. * @param mbY Macroblock Y * @returns Vertical pixel position */ macroBlockVPos(mbY: number): number; /** * Parse all headers and initialize decoding state. * @returns True if headers are valid. */ private getHeaders; /** * Parse segment header. * @param hdr Segment header * @param proba Probability table * @returns True if parsed */ private parseSegmentHeader; /** * Parse filter header. * @returns True if parsed */ private parseFilterHeader; /** * Parse partitions from input buffer. * @param input Input buffer * @returns True if parsed */ private parsePartitions; /** * Parse quantization parameters and update dequant matrices. */ private parseQuant; /** * Parse probability values for VP8 codec. */ private parseProba; /** * Initialize frame buffers and state for decoding. * @returns True if successful. */ private initFrame; /** * Precompute filter strengths for each macroblock segment. */ private precomputeFilterStrengths; /** * Parse frame by iterating macroblock rows and columns. * @returns True if frame parsed */ private parseFrame; /** * Process a row: reconstruct, filter, and emit. * @returns True if successful */ private processRow; /** * Reconstruct a row of macroblocks. */ private reconstructRow; /** * Apply filter to macroblock at (mbX, mbY). * @param mbX Macroblock X * @param mbY Macroblock Y */ private doFilter; /** * Filter a row of macroblocks. */ private filterRow; /** * Dither the current row (stub). */ private ditherRow; /** * Finalize processing of the current row. * @param useFilter Whether to apply filter * @returns True if successful */ private finishRow; /** * Write decoded RGB and alpha to output. * @param mbY Y position * @param mbW Width * @param mbH Height * @returns True if written */ private put; /** * Emit alpha values for RGB. * @param mbY Macroblock Y * @param mbW Macroblock width * @param mbH Macroblock height */ private emitAlphaRGB; /** * Emit fancy RGB values. * @param mbY Macroblock Y * @param mbW Macroblock width * @param mbH Macroblock height * @returns Number of lines output */ private emitFancyRGB; /** * Decompress alpha rows. * @param row Start row * @param numRows Number of rows * @returns Buffer with decompressed alpha rows or undefined */ private decompressAlphaRows; /** * Decode a macroblock. * @param tokenBr Bit reader for tokens * @returns True if successful */ private decodeMB; /** * Parse residuals for a macroblock. * @param mb Macroblock info * @param tokenBr Bit reader for tokens * @returns True if parsed */ private parseResiduals; /** * Parse intra prediction mode for macroblock. */ private parseIntraMode; /** * Transform input buffer based on bit pattern. * @param bits Bit pattern * @param src Source buffer * @param dst Destination buffer */ private doTransform; /** * Perform UV transform on input buffers. * @param bits Bit pattern * @param src Source buffer * @param dst Destination buffer */ private doUVTransform; /** * Walsh-Hadamard Transform (WHT) on input buffer. * @param src Source buffer * @param out Output buffer */ private transformWHT; /** * Compute non-zero coefficients code. * @param nzCoeffs Current code * @param nz Non-zero count * @param dcNz Non-zero DC * @returns Updated code */ private nzCodeBits; /** * Get large value from bit reader. * @param br Bit reader * @param p Probability array * @returns Value */ private getLargeValue; /** * Get position of last non-zero coefficient plus one. * @param br Bit reader * @param prob Probability bands * @param ctx Context * @param dq Dequant values * @param n Initial coefficient index * @param out Output buffer * @returns Position */ private getCoeffs; /** * Clips value v to range [0, M]. * @param v Value * @param M Max * @returns Clipped value */ private static clip; /** * Check prediction mode based on macroblock position. * @param mbX Macroblock X * @param mbY Macroblock Y * @param mode Optional mode * @returns Mode or undefined */ private static checkMode; /** * Clip value to 8-bit range. * @param v Value * @returns Clipped value */ private clip8; /** * Convert YUV to R component. * @param y Y * @param v V * @returns R */ private yuvToR; /** * Convert YUV to G component. * @param y Y * @param u U * @param v V * @returns G */ private yuvToG; /** * Convert YUV to B component. * @param y Y * @param u U * @returns B */ private yuvToB; /** * Convert YUV to RGB. * @param y Y * @param u U * @param v V * @param rgb Output buffer */ private yuvToRgb; /** * Convert YUV to RGBA. * @param y Y * @param u U * @param v V * @param rgba Output buffer */ private yuvToRgba; /** * Upsample YUV components to RGBA. * @param topY Top Y buffer * @param bottomY Bottom Y buffer * @param topU Top U buffer * @param topV Top V buffer * @param curU Current U buffer * @param curV Current V buffer * @param topDst Top output buffer * @param bottomDst Bottom output buffer * @param len Length */ private upSample; }