UNPKG

@syncfusion/ej2-pdf

Version:

Feature-rich JavaScript PDF library with built-in support for loading and manipulating PDF document.

454 lines (453 loc) 14.7 kB
import { _ImageDecoder } from './image-decoder'; import { _PdfStream } from './../../base-stream'; import { _PdfDictionary } from './../../pdf-primitives'; /** * PNG image decoder that parses PNG chunks, inflates compressed data, * applies PNG scanline filters None/Sub/Up/Average/Paeth, reconstructs * pixels (including alpha/mask when present), and exposes a PDF image stream. * * @private */ export declare class _PngDecoder extends _ImageDecoder { /** * Indicates whether the PNG uses an RGB color model. * * @private */ _isRedGreenBlue: boolean; /** * Indicates whether grayscale shades are used. * * @private */ _shades: boolean; /** * Internal flag controlling IDAT/deflate decode sequence. * * @private */ _ideateDecode: boolean; /** * Number of color components present in the PNG. * * @private */ _colors: number; /** * Bits per pixel after expansion according to color type and depth. * * @private */ _bitsPerPixel: number; /** * Total concatenated length of all IDAT chunks. * * @private */ _idatLength: number; /** * Number of input bands processed per scanline. * * @private */ _inputBands: number; /** * Indicates whether the image data has been decoded. * * @private */ _isDecode: boolean; /** * Length of the currently parsed PNG chunk. * * @private */ _currentChunkLength: number; /** * Parsed IHDR and related header information. * * @private */ _header: _PngHeader; /** * Accumulated compressed bytes extracted from IDAT chunks. * * @private */ _encodedStream: Uint8Array; /** * Logical length of encoded stream (since we pre-allocate). * * @private */ _encodedStreamLength: number; /** * Extracted mask channel data if available. * * @private */ _maskData: Uint8Array; /** * Color space parameters resolved for this PNG image. * * @private */ _colorSpace: any[]; /** * Raw alpha channel bytes extracted from the PNG. * * @private */ _alpha: Uint8Array; /** * Working buffer containing filtered/unfiltered scanline data. * * @private */ _dataStream: Uint8Array; /** * Output buffer holding fully decoded pixel values. * * @private */ _decodedImageData: Uint8Array; /** * Current read offset into the data stream buffer. * * @private */ _dataStreamOffset: number; /** * Initializes a new instance of the `_PngDecoder` class. * * @private * @param {Uint8Array} stream byte array. */ constructor(stream: Uint8Array); /** * Iterates through the PNG stream, reading chunk headers and dispatching * handlers (IHDR, IDAT, PLTE, tRNS, etc.) until end of image or stream end. * * @private * @returns {void} */ _initialize(): void; /** * Reads the next chunk length and type, mapping it to an internal chunk enum. * * @private * @param {_PngChunkTypes} type Placeholder for the detected chunk type (ignored input). * @returns {{type: _PngChunkTypes, hasValidChunk: boolean}} The detected type and whether a valid chunk is available. */ _hasValidChunkType(type: _PngChunkTypes): { type: _PngChunkTypes; hasValidChunk: boolean; }; /** * Skips over the current chunk payload and CRC. * * @private * @returns {void} */ _ignoreChunk(): void; /** * Parses the IHDR chunk and initializes decoder fields such as dimensions, * bit depth, color type, interlace, and derived attributes. * * @private * @returns {void} */ _readHeader(): void; /** * Computes input bands, IDAT storage length hints, and effective bytes-per-pixel * based on color type and bit depth. * * @private * @returns {void} */ _setBitsPerPixel(): void; /** * Appends the current IDAT chunk payload to the internal compressed buffer * and advances the stream position past CRC. * * @private * @returns {void} */ _readImageData(): void; /** * Reads the PLTE palette for indexed color images and prepares the * Indexed color space structure in PDF terms. * * @private * @returns {void} */ _readPhotoPlate(): void; /** * Reads the tRNS transparency table and caches the * alpha bytes while detecting partial transparency. * * @private * @returns {void} */ _readTransparency(): void; /** * Resolves the effective PDF color space for the image. Returns either * `DeviceGray`/`DeviceRGB` or a calibrated `CalRGB` array when sRGB is present. * * @private * @returns {any} A color space name object or an array describing a calibrated RGB space. */ _getPngColorSpace(): any; /** * Determines whether raw decode is required interlaced, 16-bit, or with alpha/shades. * If so, inflates IDAT data, allocates buffers, and reconstructs pixels; otherwise * reuses the encoded stream directly. * * @private * @returns {void} */ _decodeImageData(): void; /** * Inflates the zlib-compressed IDAT payload omitting zlib header and Adler-32 trailer * into a raw byte array. * * @private * @param {Uint8Array} data The zlib-wrapped IDAT bytes (concatenated). * @returns {Uint8Array} The inflated byte array. */ _getDeflatedData(data: Uint8Array): Uint8Array; /** * Dispatches pixel reconstruction by interlace method: a single pass for * non-interlaced images or seven Adam7 passes for interlaced images. * * @private * @returns {void} */ _readDecodeData(): void; /** * Reconstructs scanlines for a pass/region by reading filter type, applying the * corresponding PNG filter, and writing pixels (and alpha/mask) to output buffers. * * @private * @param {number} xOffset Starting x offset in destination image for this pass. * @param {number} yOffset Starting y offset in destination image for this pass. * @param {number} xStep X stride (per Adam7 pass or 1 for non-interlaced). * @param {number} yStep Y stride (per Adam7 pass or 1 for non-interlaced). * @param {number} width Width of this pass/region in pixels. * @param {number} height Height of this pass/region in pixels. * @returns {void} */ _decodeData(xOffset: number, yOffset: number, xStep: number, yStep: number, width: number, height: number): void; /** * Reads `count` bytes from a numeric array stream into `data` using the common * `_read` routine, returning the updated stream offset. * * @private * @param {Uint8Array} stream The source byte array. * @param {number} streamOffset The current read offset in the stream. * @param {Uint8Array} data The destination buffer to fill. * @param {number} count The number of bytes to read. * @returns {number} The new stream offset after reading. * @throws {Error} If insufficient data is available. */ _readStream(stream: Uint8Array, streamOffset: number, data: Uint8Array, count: number): number; /** * Applies the PNG Sub filter (type 1) in-place to the current scanline. * * @private * @param {Uint8Array} data The scanline bytes to modify. * @param {number} count Number of bytes in the scanline row. * @param {number} bitsPerPixel Bytes-per-pixel (not bit-depth) for subtraction reference. * @returns {void} */ _decompressSub(data: Uint8Array, count: number, bitsPerPixel: number): void; /** * Applies the PNG Up filter (type 2) in-place using the prior scanline. * * @private * @param {Uint8Array} data The current scanline bytes. * @param {Uint8Array} pData The prior scanline bytes. * @param {number} count Number of bytes per row. * @returns {void} */ _decompressUp(data: Uint8Array, pData: Uint8Array, count: number): void; /** * Applies the PNG Average filter (type 3) in-place using left and prior values. * * @private * @param {Uint8Array} data The current scanline bytes. * @param {Uint8Array} pData The prior scanline bytes. * @param {number} count Number of bytes per row. * @param {number} bitsPerPixel Bytes-per-pixel (not bit-depth) for left reference. * @returns {void} */ _decompressAverage(data: Uint8Array, pData: Uint8Array, count: number, bitsPerPixel: number): void; /** * Applies the PNG Paeth filter (type 4) in-place using left, up, and up-left predictors. * * @private * @param {Uint8Array} data The current scanline bytes. * @param {Uint8Array} pData The prior scanline bytes. * @param {number} count Number of bytes per row. * @param {number} bitsPerPixel Bytes-per-pixel (not bit-depth) for left reference. * @returns {void} */ _decompressPaeth(data: Uint8Array, pData: Uint8Array, count: number, bitsPerPixel: number): void; /** * Computes the Paeth predictor from left (`a`), up (`b`), and up-left (`c`) neighbors. * * @private * @param {number} a The left pixel sample. * @param {number} b The above pixel sample. * @param {number} c The upper-left pixel sample. * @returns {number} The chosen predictor value. */ _paethPredictor(a: number, b: number, c: number): number; /** * Converts a filtered scanline row to pixel values and writes them into the * decoded image buffer, also writing an 8-bit mask row when alpha/shades are present. * * @private * @param {Uint8Array} data The unfiltered scanline bytes for this pass. * @param {number} x Destination x start (accounting for pass offset). * @param {number} step Destination x increment (per Adam7 pass or 1). * @param {number} y Destination y coordinate. * @param {number} width The number of pixels to process from this row. * @returns {void} */ _processPixels(data: Uint8Array, x: number, step: number, y: number, width: number): void; /** * Expands a scanline's filtered bytes into sample values based on bit depth: * 8-bit (1:1), 16-bit (merge pairs), or packed sub-byte samples. * * @private * @param {Uint8Array} data The scanline bytes. * @returns {Uint8Array | Uint16Array} The expanded per-sample values for the row. */ _getPixel(data: Uint8Array): Uint8Array | Uint16Array; /** * Writes one pixel or scalar sample to the destination buffer, handling * 8/16-bit storage or sub-byte packing as required. * * @private * @param {Uint8Array} imageData The destination buffer (image or mask). * @param {Uint8Array | Uint16Array} data The source per-sample array. * @param {number} offset Offset into source sample array. * @param {number} size Number of samples to write (1=gray/alpha, 3=RGB). * @param {number} x Destination x coordinate. * @param {number} y Destination y coordinate. * @param {number} bitDepth Source bit depth (8/16 or packed). * @param {number} bpr Bytes-per-row in the destination buffer. * @returns {void} */ _setPixel(imageData: Uint8Array, data: Uint8Array | Uint16Array, offset: number, size: number, x: number, y: number, bitDepth: number, bpr: number): void; /** * Builds the PDF image stream for the PNG image, wiring the * core dictionary entries, optional `FlateDecode`, and optional `DecodeParms`, * and attaches the mask if present. * * @private * @returns {_PdfStream} The image stream suitable for embedding. */ _getImageDictionary(): _PdfStream; /** * Creates and assigns the soft-mask image stream from the alpha (or shades) * buffer when available. * * @private * @returns {void} */ _setMask(): void; /** * Creates the `DecodeParms` dictionary (Columns/Colors/Predictor/BitsPerComponent) * for use with `FlateDecode` streams. * * @private * @returns {_PdfDictionary} The decode parameters dictionary. */ _getDecodeParams(): _PdfDictionary; /** * Maps a 4 character PNG chunk label to the internal enum value. * * @private * @param {string} chunk The chunk name. * @returns {_PngChunkTypes} The mapped chunk type or `null` if unknown. */ _getChunkType(chunk: string): _PngChunkTypes; /** * Maps a numeric filter byte to the corresponding PNG filter type enum. * * @private * @param {number} type The PNG filter byte (0..4). * @returns {_PngFilterTypes} The filter enum value. */ _getFilterType(type: number): _PngFilterTypes; /** * Releases decoder-held buffers and references to allow GC. * * @private * @returns {void} */ dispose(): void; } /** * Holds parsed values from the PNG `IHDR` chunk, including size, color type, * compression, filter method, and interlace mode; used to guide decoding. * * @private */ declare class _PngHeader { constructor(); _width: number; _height: number; _colorType: number; _compression: number; _bitDepth: number; _filter: _PngFilterTypes; _interlace: number; } /** * Enumerates PNG chunk types recognized by the decoder, including core chunks * and optional metadata/ancillary chunks. * * @private */ declare enum _PngChunkTypes { iHDR = 0, pLTE = 1, iDAT = 2, iEND = 3, bKGD = 4, cHRM = 5, gAMA = 6, hIST = 7, pHYs = 8, sBIT = 9, tEXt = 10, tIME = 11, tRNS = 12, zTXt = 13, sRGB = 14, iCCP = 15, iTXt = 16, unknown = 17 } /** * Enumerates the PNG scanline filter types applied to each row before compression. * These filters improve Deflate efficiency by transforming pixel values: * - `none` : No filtering applied. * - `sub` : Uses the left pixel as predictor. * - `up` : Uses the pixel above as predictor. * - `average` : Averages the left and above pixels. * - `paeth` : Applies the Paeth predictor using left, above, and upper-left. * * @private */ declare enum _PngFilterTypes { none = 0, sub = 1, up = 2, average = 3, paeth = 4 } export {};