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)

223 lines (222 loc) 8.28 kB
/** @format */ import { Encoder, EncoderEncodeOptions } from './encoder.js'; /** * JPEG Chroma (sub)sampling format. */ export declare enum JpegChroma { /** 4:4:4 chroma subsampling */ yuv444 = 0, /** 4:2:0 chroma subsampling */ yuv420 = 1 } /** * Interface representing the options for the JpegEncoder.encode method. */ export interface JpegEncoderEncodeOptions extends EncoderEncodeOptions { /** * Chroma subsampling format used in JPEG encoding. */ chroma?: JpegChroma; } /** * Encode an image to the JPEG format. */ export declare class JpegEncoder implements Encoder { /** Zigzag order for quantization table */ private static readonly _zigzag; /** Standard DC luminance number of codes */ private static readonly _stdDcLuminanceNrCodes; /** Standard DC luminance values */ private static readonly _stdDcLuminanceValues; /** Standard AC luminance number of codes */ private static readonly _stdAcLuminanceNrCodes; /** Standard AC luminance values */ private static readonly _stdAcLuminanceValues; /** Standard DC chrominance number of codes */ private static readonly _stdDcChrominanceNrCodes; /** Standard DC chrominance values */ private static readonly _stdDcChrominanceValues; /** Standard AC chrominance number of codes */ private static readonly _stdAcChrominanceNrCodes; /** Standard AC chrominance values */ private static readonly _stdAcChrominanceValues; private static readonly _backgroundColor; /** Quantization table for Y component */ private readonly _tableY; /** Quantization table for UV components */ private readonly _tableUv; /** FDCT quantization table for Y component */ private readonly _fdTableY; /** FDCT quantization table for UV components */ private readonly _fdTableUv; /** Bit code array */ private readonly _bitCode; /** Category array */ private readonly _category; /** Output FDCT quantization array */ private readonly _outputfDCTQuant; /** DU array */ private readonly _du; /** RGB to YUV conversion table */ private readonly _tableRgbYuv; /** Y DC Huffman table */ private _ydcHuffman; /** UV DC Huffman table */ private _uvdcHuffman; /** Y AC Huffman table */ private _yacHuffman; /** UV AC Huffman table */ private _uvacHuffman; /** Current quality setting */ private _currentQuality?; /** Byte new value for bit writing */ private _byteNew; /** Byte position for bit writing */ private _bytePos; /** Flag indicating if animation is supported */ private _supportsAnimation; /** * Gets the flag indicating if animation is supported. * @returns {boolean} True if animation is supported, otherwise false. */ get supportsAnimation(): boolean; /** * Constructs a new JpegEncoder instance. * @param {number} [quality=100] - The quality of the JPEG encoding. */ constructor(quality?: number); /** * Computes the Huffman table. * @param {number[]} nrcodes - Number of codes. * @param {number[]} stdTable - Standard table. * @returns {Array<Array<number> | undefined>} The computed Huffman table. */ private static computeHuffmanTable; /** * Downsamples from four input lists, storing average values into **duOut**. * @param {Float32Array} duOut - Output array. * @param {Float32Array} duIn1 - Input array 1. * @param {Float32Array} duIn2 - Input array 2. * @param {Float32Array} duIn3 - Input array 3. * @param {Float32Array} duIn4 - Input array 4. */ private static downsampleDU; /** * Writes a JPEG marker to the output buffer. * @param {OutputBuffer} fp - The output buffer. * @param {number} marker - The marker to write. */ private static writeMarker; /** * Writes the APP0 marker to the output buffer. * @param {OutputBuffer} out - The output buffer. */ private static writeAPP0; /** * Writes the APP1 (EXIF) marker to the output buffer. * @param {OutputBuffer} out - The output buffer. * @param {ExifData} exif - The EXIF data. */ private static writeExif; /** * Writes the APP2 (ICC Profile) marker to the output buffer. * @param {OutputBuffer} out - The output buffer. * @param {IccProfile} profile - The ICC profile data. */ private static writeICCProfile; /** * Writes the Start of Frame (SOF0) marker to the output buffer. * @param {OutputBuffer} out - The output buffer to write to. * @param {number} width - The width of the image. * @param {number} height - The height of the image. * @param {JpegChroma} chroma - The chroma subsampling format. */ private static writeSOF0; /** * Writes the Start of Scan (SOS) marker to the output buffer. * @param {OutputBuffer} out - The output buffer to write to. */ private static writeSOS; /** * Writes the Define Huffman Table (DHT) marker to the output buffer. * @param {OutputBuffer} out - The output buffer to write to. */ private static writeDHT; /** * Calculates the YUV values for a given block of the image. * @param {MemoryImage} image - The image to process. * @param {number} x - The x-coordinate of the block. * @param {number} y - The y-coordinate of the block. * @param {number} width - The width of the image. * @param {number} height - The height of the image. * @param {Float32Array} ydu - The Y component of the block. * @param {Float32Array} udu - The U component of the block. * @param {Float32Array} vdu - The V component of the block. * @param {Color} backgroundColor - The background color used for alpha blending. */ private calculateYUV; /** * Initializes the Huffman tables. */ private initHuffmanTable; /** * Initializes the category and bit code tables. */ private initCategoryNumber; /** * Initializes the RGB to YUV conversion table. */ private initRgbYuvTable; /** * Sets the quality of the JPEG encoder. * @param {number} quality - The quality value (1-100). */ private setQuality; /** * Initializes the quantization tables. * @param {number} sf - The scaling factor. */ private initQuantTables; /** * Performs the Discrete Cosine Transform (DCT) and quantization on the data. * @param {Float32Array} data - The data to transform. * @param {Float32Array} fdtbl - The quantization table. * @returns {Array<number | undefined>} The quantized DCT coefficients. */ private fDCTQuant; /** * Writes the Define Quantization Table (DQT) marker to the output buffer. * @param {OutputBuffer} out - The output buffer to write to. */ private writeDQT; /** * Writes bits to the output buffer. * @param {OutputBuffer} out - The output buffer to write to. * @param {number[]} bits - The bits to write. */ private writeBits; /** * Resets the bit buffer. */ private resetBits; /** * Processes a data unit (DU) and writes the encoded data to the output buffer. * @param {OutputBuffer} out - The output buffer to write to. * @param {Float32Array} cdu - The current data unit. * @param {Float32Array} fdtbl - The quantization table. * @param {number} dc - The DC coefficient. * @param {Array<number[] | undefined>} htac - The AC Huffman table. * @param {Array<number[] | undefined>} [htdc] - The DC Huffman table (optional). * @returns {number} The new DC coefficient. */ private processDU; /** * Encodes the image using the JPEG format. * @param {JpegEncoderEncodeOptions} opt - The options for encoding. * @param {MemoryImage} opt.image - The image to encode. * @param {boolean} [opt.skipExif] - Whether to skip embedding EXIF metadata (optional). * @param {JpegChroma} [opt.chroma] - The chroma subsampling format (optional). * @returns {Uint8Array} The encoded JPEG image as a Uint8Array. */ encode(opt: JpegEncoderEncodeOptions): Uint8Array; }