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)

258 lines (257 loc) 9.24 kB
/** @format */ import { InputBuffer } from '../../common/input-buffer.js'; /** * Interface for initializing options for TiffFaxDecoder. */ export interface TiffFaxDecoderInitOptions { /** Fill order of the bits. */ fillOrder: number; /** Width of the image. */ width: number; /** Height of the image. */ height: number; } /** * Class for decoding TIFF Fax images. */ export declare class TiffFaxDecoder { /** * Table for bit manipulation. */ private static readonly _table1; /** * Table for bit manipulation. */ private static readonly _table2; /** * Table to be used when **fillOrder** = 2, for flipping bytes. */ private static readonly _flipTable; /** * The main 10 bit white runs lookup table. */ private static readonly _white; /** * Additional make up codes for both white and black runs */ private static readonly _additionalMakeup; /** * Initial black values. * * The array is divided into two segments: * - 0 - 7: Initial values for the first segment. * - 8 - 15: Initial values for the second segment. */ private static readonly _initBlack; /** * Two-bit black values. */ private static readonly _twoBitBlack; /** * Main black run table, using the last 9 bits of possible 13 bit code. */ private static readonly _black; /** * Array of 2D codes. */ private static readonly _twoDCodes; /** * The width of the image to decode. * @private */ private _width; /** * Gets the width of the image to decode. */ get width(): number; /** * The height of the image to decode. * @private */ private _height; /** * Gets the height of the image to decode. */ get height(): number; /** * The fill order of the image. * @private */ private _fillOrder; /** * Gets the fill order of the image. * @returns {number} The fill order of the object. */ get fillOrder(): number; /** * Size of the element that is being changed. * @private */ private _changingElemSize; /** * Array to store changing elements for the previous scanline */ private _prevChangingElements?; /** * Array to store changing elements for the current scanline */ private _currChangingElements?; /** * Input buffer containing Uint8Array data */ private _data; /** * Pointer to the current bit in the input buffer */ private _bitPointer; /** * Pointer to the current byte in the input buffer */ private _bytePointer; /** * The last element that was changed. * @private */ private _lastChangingElement; /** * The compression level. * @private */ private _compression; /** * Mode for uncompressed data. * @private */ private _uncompressedMode; /** * Bits used for filling. * @private */ private _fillBits; /** * One-dimensional mode. * @private */ private _oneD; /** * Constructs a new TiffFaxDecoder instance with the given options. * * @param {TiffFaxDecoderInitOptions} opt - The initialization options for the decoder. * @param {number} opt.fillOrder - The fill order for the decoder. * @param {number} opt.width - The width of the image to decode. * @param {number} opt.height - The height of the image to decode. */ constructor(opt: TiffFaxDecoderInitOptions); /** * Retrieves the next specified number of bits from the data stream. * * @param {number} bitsToGet - The number of bits to retrieve. * @returns {number} The next bitsToGet bits from the data stream. * @throws {LibError} If the fill order is not recognized. */ private nextNBits; /** * Retrieves the next value with fewer than 8 bits from the data. * @param {number} bitsToGet - The number of bits to retrieve. * @returns {number} The next value with the specified number of bits. * @throws {LibError} If the fill order is not 1 or 2. */ private nextLesserThan8Bits; /** * Move pointer backwards by given amount of bits. * * @param {number} bitsToMoveBack - The number of bits to move the bit pointer back. */ private updatePointer; /** * Move to the next byte boundary. * * @returns {boolean} Always returns true. */ private advancePointer; /** * Sets a specified number of bits to black (1) in the given buffer. * * @param {InputBuffer<Uint8Array>} buffer - The buffer containing the bits to be set. * @param {number} lineOffset - The line offset in the buffer where the setting starts. * @param {number} bitOffset - The bit offset within the line where the setting starts. * @param {number} numBits - The number of bits to set to black. */ private setToBlack; /** * Decodes the next scanline from the input buffer. * * @param {InputBuffer<Uint8Array>} buffer - The input buffer containing the scanline data. * @param {number} lineOffset - The offset in the buffer where the scanline starts. * @param {number} bitOffset - The bit offset within the scanline. * @throws {LibError} - Throws an error if an invalid code is encountered. */ private decodeNextScanline; /** * Reads the End Of Line (EOL) marker from the input stream. * * This method handles both one-dimensional and two-dimensional encoding modes. * It checks for the presence of the EOL marker and ensures that the input stream * is correctly aligned to byte boundaries as required by the EOL marker format. * * @returns {number} - Returns 1 if in one-dimensional encoding mode, otherwise returns * the next bit indicating 1D/2D encoding of the next line. * @throws {LibError} - Throws an error if the EOL marker is not found or if the input * stream is not correctly aligned. */ private readEOL; /** * Finds the next changing element in the sequence and updates the provided return array. * * @param {number | undefined} a0 - The reference value to compare against, can be undefined. * @param {boolean} isWhite - A boolean indicating whether to search for white elements. * @param {Array<number | undefined>} ret - An array to store the results, where the first element is the next changing element * greater than a0, and the second element is the subsequent changing element. */ private getNextChangingElement; /** * Decodes a white code word from the input stream. * * @returns {number} The run length of the decoded white code word. * @throws {LibError} If an error or end-of-line (EOL) condition is encountered. */ private decodeWhiteCodeWord; /** * Decodes the black code word from the input stream. * * @returns {number} The run length of the decoded black code word. * @throws {LibError} If an End Of Line (EOL) code is encountered. */ private decodeBlackCodeWord; /** * Decodes a 1D compressed image data into an output buffer. * * @param {InputBuffer<Uint8Array>} out - The output buffer where the decoded data will be stored. * @param {InputBuffer<Uint8Array>} compData - The compressed image data to be decoded. * @param {number} startX - The starting X coordinate for decoding. * @param {number} height - The number of scanlines to decode. */ decode1D(out: InputBuffer<Uint8Array>, compData: InputBuffer<Uint8Array>, startX: number, height: number): void; /** * Decodes a 2D encoded image. * * @param {InputBuffer<Uint8Array>} out - The output buffer to store the decoded image. * @param {InputBuffer<Uint8Array>} compData - The compressed data buffer. * @param {number} startX - The starting X position for decoding. * @param {number} height - The height of the image to decode. * @param {number} tiffT4Options - The TIFF T4 options for decoding. * @throws {LibError} If the data does not start with an EOL code or if an invalid code is encountered. */ decode2D(out: InputBuffer<Uint8Array>, compData: InputBuffer<Uint8Array>, startX: number, height: number, tiffT4Options: number): void; /** * Decodes a TIFF T6 compressed image. * * @param {InputBuffer<Uint8Array>} out - The output buffer where the decoded image will be stored. * @param {InputBuffer<Uint8Array>} compData - The input buffer containing the compressed image data. * @param {number} startX - The starting X position for decoding. * @param {number} height - The height of the image to decode. * @param {number} tiffT6Options - The options for TIFF T6 decoding. * @throws {LibError} If an invalid code is encountered during decoding. */ decodeT6(out: InputBuffer<Uint8Array>, compData: InputBuffer<Uint8Array>, startX: number, height: number, tiffT6Options: number): void; }