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)
239 lines (238 loc) • 7.68 kB
TypeScript
/** @format */
import { CompressionLevel } from '../common/typings.js';
import { Encoder, EncoderEncodeOptions } from './encoder.js';
import { PngFilterType } from './png/png-filter-type.js';
import { MemoryImage } from '../image/image.js';
import { PngPhysicalPixelDimensions } from './png/png-physical-pixel-dimensions.js';
/**
* Options for initializing the PNG encoder.
*/
export interface PngEncoderInitOptions {
/**
* The filter type to use.
*/
filter?: PngFilterType;
/**
* The compression level to use.
*/
level?: CompressionLevel;
/**
* The physical pixel dimensions of the image.
* This provides information about the intended display size of the image in physical units.
*/
pixelDimensions?: PngPhysicalPixelDimensions;
}
/**
* Encode an image to the PNG format.
*/
export declare class PngEncoder implements Encoder {
/**
* Global quantizer for the encoder.
*/
private _globalQuantizer;
/**
* Filter type for the encoder.
*/
private _filter;
/**
* Compression level for the encoder.
*/
private _level;
/**
* Number of times to repeat the animation.
*/
private _repeat;
/**
* Number of frames in the animation.
*/
private _frames;
/**
* Sequence number for the animation frames.
*/
private _sequenceNumber;
/**
* Is the image animated?
*/
private _isAnimated;
/**
* Output buffer for the encoded PNG.
*/
private _output;
private _supportsAnimation;
/**
* Checks if this encoder supports animation.
*
* @returns {boolean} True if the encoder supports animation, otherwise false.
*/
get supportsAnimation(): boolean;
/**
* Physical pixel dimensions of the PNG.
*/
private _pixelDimensions;
/**
* Gets the physical pixel dimensions of the PNG.
*/
get pixelDimensions(): PngPhysicalPixelDimensions | undefined;
/**
* Constructor for PngEncoder.
* @param {PngEncoderInitOptions} [opt] - Initialization options for the encoder.
* @param {PngFilterType} [opt.filter] - The filter type to use for encoding. Defaults to PngFilterType.paeth.
* @param {number} [opt.level] - The compression level to use for encoding. Defaults to 6.
*/
constructor(opt?: PngEncoderInitOptions);
/**
* Return the CRC of the bytes.
* @param {string} type - The type of the chunk.
* @param {Uint8Array} bytes - The bytes of the chunk.
* @returns {number} The CRC checksum.
*/
private static crc;
/**
* Write a chunk to the output buffer.
* @param {OutputBuffer} out - The output buffer.
* @param {string} type - The type of the chunk.
* @param {Uint8Array} chunk - The bytes of the chunk.
*/
private static writeChunk;
/**
* Write bytes to the output buffer.
* @param {number} bpc - Bytes per channel.
* @param {Uint8Array} row - The row of bytes.
* @param {number} ri - Row index.
* @param {Uint8Array} out - The output buffer.
* @param {number} oi - Output index.
* @returns {number} The new output index.
*/
private static write;
/**
* Apply the sub filter to the row.
* @param {Uint8Array} row - The row of bytes.
* @param {number} bpc - Bytes per channel.
* @param {number} bpp - Bytes per pixel.
* @param {Uint8Array} out - The output buffer.
* @param {number} oi - Output index.
* @returns {number} The new output index.
*/
private static filterSub;
/**
* Apply the up filter to the row.
* @param {Uint8Array} row - The row of bytes.
* @param {number} bpc - Bytes per channel.
* @param {Uint8Array} out - The output buffer.
* @param {number} oi - Output index.
* @param {Uint8Array} [prevRow] - The previous row of bytes.
* @returns {number} The new output index.
*/
private static filterUp;
/**
* Apply the average filter to the row.
* @param {Uint8Array} row - The row of bytes.
* @param {number} bpc - Bytes per channel.
* @param {number} bpp - Bytes per pixel.
* @param {Uint8Array} out - The output buffer.
* @param {number} oi - Output index.
* @param {Uint8Array} [prevRow] - The previous row of bytes.
* @returns {number} The new output index.
*/
private static filterAverage;
/**
* Paeth predictor function.
* @param {number} a - Left pixel.
* @param {number} b - Above pixel.
* @param {number} c - Upper-left pixel.
* @returns {number} The predicted value.
*/
private static paethPredictor;
/**
* Apply the Paeth filter to the row.
* @param {Uint8Array} row - The row of bytes.
* @param {number} bpc - Bytes per channel.
* @param {number} bpp - Bytes per pixel.
* @param {Uint8Array} out - The output buffer.
* @param {number} oi - Output index.
* @param {Uint8Array} [prevRow] - The previous row of bytes.
* @returns {number} The new output index.
*/
private static filterPaeth;
/**
* Apply the none filter to the row.
* @param {Uint8Array} rowBytes - The row of bytes.
* @param {number} bpc - Bytes per channel.
* @param {Uint8Array} out - The output buffer.
* @param {number} oi - Output index.
* @returns {number} The new output index.
*/
private static filterNone;
/**
* Get the number of channels in the image.
* @param {MemoryImage} image - The image.
* @returns {number} The number of channels.
*/
private static numChannels;
/**
* Write the PNG header.
* @param {MemoryImage} image - The image.
*/
private writeHeader;
/**
* Write the ICC profile chunk.
* @param {IccProfile} iccp - The ICC profile.
*/
private writeICCPChunk;
/**
* Write the animation control chunk.
*/
private writeAnimationControlChunk;
/**
* Write the frame control chunk.
* @param {MemoryImage} image - The image.
*/
private writeFrameControlChunk;
/**
* Write the palette chunk.
* @param {Palette} palette - The palette.
*/
private writePalette;
/**
* Write the text chunk.
* @param {string} keyword - The keyword.
* @param {string} text - The text.
*/
private writeTextChunk;
/**
* Apply the filter to the image.
* @param {MemoryImage} image - The image.
* @param {Uint8Array} out - The output buffer.
*/
private filter;
/**
* Adds a frame to the PNG encoder.
*
* @param {MemoryImage} image - The image to add as a frame.
*/
addFrame(image: MemoryImage): void;
/**
* Start encoding a PNG.
*
* Call this method once before calling **addFrame**.
*
* @param {number} frameCount - The number of frames to encode.
*/
start(frameCount: number): void;
/**
* Finish encoding a PNG, and return the resulting bytes.
*
* Call this method to finalize the encoding, after all **addFrame** calls.
*
* @returns {Uint8Array | undefined} The encoded PNG bytes, or undefined if encoding was not started.
*/
finish(): Uint8Array | undefined;
/**
* Encode **image** to the PNG format.
* @param {EncoderEncodeOptions} opt - The encoding options.
* @param {MemoryImage} opt.image - The image to encode.
* @param {boolean} [opt.singleFrame] - Optional flag to encode a single frame.
* @returns {Uint8Array} The encoded PNG bytes.
*/
encode(opt: EncoderEncodeOptions): Uint8Array;
}