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)
191 lines (190 loc) • 7.72 kB
TypeScript
/** @format */
import { Encoder, EncoderEncodeOptions } from './encoder.js';
/**
* Encode an image to the WebP format (lossless).
*
* Uses the VP8L lossless bitstream format wrapped in a RIFF/WebP container.
* Applies the subtract-green transform and LZ77 back-references.
*/
export declare class WebPEncoder implements Encoder {
/**
* Lookup table for mapping (x, y) offsets to plane codes for distance encoding.
* Used in the LZ77 back-reference distance calculation.
* @private
*/
private static readonly planeLut;
/**
* Indicates whether the encoder supports animation.
* @private
*/
private _supportsAnimation;
/**
* Gets the value indicating whether the encoder supports animation.
* @returns {boolean} True if the encoder supports animation, otherwise false.
*/
get supportsAnimation(): boolean;
/**
* Encodes the given image data into the VP8L lossless bitstream.
* Applies transforms, predictor selection, and entropy coding.
* @param {MemoryImage} image - The image to encode.
* @param {number} width - The width of the image.
* @param {number} height - The height of the image.
* @returns {Uint8Array} The encoded VP8L bitstream.
* @private
*/
private encodeVP8L;
/**
* Applies the subtract-green transform to the red and blue channels.
* This decorrelates color channels for better compression.
* @param {Uint8Array} r - Red channel data.
* @param {Uint8Array} g - Green channel data.
* @param {Uint8Array} b - Blue channel data.
* @param {number} numPixels - Number of pixels in the image.
* @private
*/
private applySubtractGreenTransform;
/**
* Selects the best predictor mode for each block in the image.
* Evaluates candidate predictors and chooses the one with the lowest cost.
* @param {Uint8Array} r - Red channel data.
* @param {Uint8Array} g - Green channel data.
* @param {Uint8Array} b - Blue channel data.
* @param {number} width - Image width.
* @param {number} height - Image height.
* @param {number} blockW - Number of blocks horizontally.
* @param {number} blockH - Number of blocks vertically.
* @param {number} blockSize - Size of each block.
* @returns {number[]} Array of predictor modes for each block.
* @private
*/
private selectPredictorModes;
/**
* Applies the selected predictor transform to the image channels.
* Modifies the channel data in-place to store residuals.
* @param {Uint8Array} r - Red channel data.
* @param {Uint8Array} g - Green channel data.
* @param {Uint8Array} b - Blue channel data.
* @param {Uint8Array} a - Alpha channel data.
* @param {number} width - Image width.
* @param {number} height - Image height.
* @param {number} blockW - Number of blocks horizontally.
* @param {number} blockSize - Size of each block.
* @param {number[]} modes - Predictor modes for each block.
* @private
*/
private applyPredictorTransform;
/**
* Writes the predictor mode subimage to the bitstream.
* Encodes the predictor modes using Huffman coding.
* @param {WebPBitWriter} bw - The bit writer.
* @param {number} blockW - Number of blocks horizontally.
* @param {number} blockH - Number of blocks vertically.
* @param {number[]} modes - Predictor modes for each block.
* @private
*/
private writePredictorSubImage;
/**
* Maps a match length to its corresponding symbol for entropy coding.
* @param {number} length - The match length.
* @returns {number} The symbol representing the length.
* @private
*/
private lengthSymbol;
/**
* Computes the extra bits and value for a given match length symbol.
* Used for encoding match lengths with additional precision.
* @param {number} length - The match length.
* @returns {{ extraBits: number, extraValue: number }} Extra bits and value.
* @private
*/
private lengthExtra;
/**
* Converts a pixel distance to a plane code for distance entropy coding.
* Uses a lookup table for small distances and a direct mapping for larger ones.
* @param {number} width - Image width.
* @param {number} dist - Pixel distance.
* @returns {number} The plane code for the distance.
* @private
*/
private distToPlaneCode;
/**
* Maps a value to its prefix code for distance or length coding.
* @param {number} v - The value to encode.
* @returns {number} The prefix code.
* @private
*/
private prefixCode;
/**
* Computes the extra bits and value for a given prefix code.
* Used for encoding distances and lengths with additional precision.
* @param {number} v - The value to encode.
* @returns {{ extraBits: number, extraValue: number }} Extra bits and value.
* @private
*/
private prefixExtra;
/**
* Computes the floor of the base-2 logarithm of the given value.
* @param {number} v - The value to compute the log2 floor for.
* @returns {number} The floor of log2(v).
* @private
*/
private log2Floor;
/**
* Builds the Huffman code lengths for the given symbol frequencies.
* Uses a bounded-length Huffman tree construction.
* @param {number[]} freq - Symbol frequencies.
* @param {number} alphabetSize - Number of symbols in the alphabet.
* @param {number} [maxBits] - Maximum allowed code length.
* @returns {number[]} Array of code lengths for each symbol.
* @private
*/
private buildHuffmanCodeLengths;
/**
* Writes the Huffman code tree to the bitstream.
* Handles special cases for small alphabets and uses RLE for code lengths.
* @param {WebPBitWriter} bw - The bit writer.
* @param {number} alphabetSize - Number of symbols in the alphabet.
* @param {number[]} codeLengths - Huffman code lengths for each symbol.
* @private
*/
private writeHuffmanCode;
/**
* Builds a run-length encoded (RLE) sequence of code length symbols.
* Used for compact representation of Huffman code trees.
* @param {number[]} codeLengths - Huffman code lengths for each symbol.
* @param {number} alphabetSize - Number of symbols in the alphabet.
* @returns {WebPClSymbol[]} RLE sequence of code length symbols.
* @private
*/
private buildRleSequence;
/**
* Generates canonical Huffman codes from code lengths.
* @param {Int32Array} codeLengths - Huffman code lengths for each symbol.
* @param {number} numSymbols - Number of symbols in the alphabet.
* @returns {number[]} Canonical Huffman codes for each symbol.
* @private
*/
private canonicalCodes;
/**
* Reverses the order of bits in the given value.
* @param {number} value - The value whose bits to reverse.
* @param {number} numBits - The number of bits to reverse.
* @returns {number} The value with bits reversed.
* @private
*/
private reverseBits;
/**
* Returns a Uint8Array containing the ASCII codes of the given string.
* @param {string} s - The string to convert.
* @returns {Uint8Array} The byte array representing the string.
* @private
*/
private tag;
/**
* Encodes the given image into a WEBP format.
* @param {EncoderEncodeOptions} opt - The options for encoding.
* @param {MemoryImage} opt.image - The image to encode.
* @returns {Uint8Array} The encoded image in WEBP format.
*/
encode(opt: EncoderEncodeOptions): Uint8Array;
}