gif-tools
Version:
A robust, zero-dependency TypeScript library for creating GIF files with support for both static and animated GIFs. Built with modern TypeScript features and designed to work in both Node.js and browser environments.
154 lines (153 loc) • 3.92 kB
TypeScript
/**
* GIF Reader Implementation
*
* Parses existing GIF files and extracts frames, metadata, and color information.
*/
import { ImageData, RGBColor, DisposalMethod } from './types.js';
export interface GifFrame {
/** Frame image data */
imageData: ImageData;
/** Frame delay in milliseconds */
delay: number;
/** Frame disposal method */
disposal: DisposalMethod;
/** Frame position */
left: number;
top: number;
/** Transparent color index (-1 if none) */
transparentIndex: number;
}
export interface GifMetadata {
/** Extensions found in the GIF */
extensions: string[];
/** Comments from comment extensions */
comments: string[];
/** Whether any frames use interlacing */
hasInterlacedFrames: boolean;
/** Whether local color tables are used */
hasLocalColorTables: boolean;
/** Transparent frames detected */
hasTransparency: boolean;
/** XMP metadata if present */
xmpData?: string;
/** Technical details discovered during parsing */
technicalDetails: {
totalDataSize: number;
compressionRatio?: number;
averageFrameSize: number;
};
}
export interface GifInfo {
/** GIF dimensions */
width: number;
height: number;
/** Number of frames */
frameCount: number;
/** Loop count (0 = infinite) */
loops: number;
/** Global color table */
globalColorTable?: Uint8Array;
/** Background color index */
backgroundColorIndex: number;
/** Pixel aspect ratio */
pixelAspectRatio: number;
/** Color resolution */
colorResolution: number;
/** Sort flag */
sortFlag: boolean;
/** Total duration in milliseconds */
duration: number;
/** File size in bytes */
size: number;
/** GIF version (87a or 89a) */
version: string;
/** Metadata and extension information */
metadata: GifMetadata;
}
/**
* Reads and parses GIF files
*/
export declare class GifReader {
private data;
private position;
private info;
private frames;
private metadata;
constructor(data: Uint8Array);
/**
* Validates GIF header
*/
private validateHeader;
/**
* Reads a byte from the data
*/
private readByte;
/**
* Reads a 16-bit little-endian value
*/
private read16;
/**
* Reads multiple bytes
*/
private readBytes;
/**
* Skips data sub-blocks
*/
private skipDataSubBlocks;
/**
* Reads data sub-blocks and returns combined data
*/
private readDataSubBlocks;
/**
* Creates ImageData from index data and color table
*/
private createFrameImageData;
/**
* Converts interlaced index to actual pixel position
*/
private deinterlaceIndex;
/**
* Converts interlaced Y coordinate to actual Y coordinate
*/
private deinterlaceY;
/**
* Clears a frame area to background color
*/
private clearFrameArea;
/**
* Composites a frame onto the canvas
*/
private compositeFrame;
/**
* Parses the GIF and extracts all information
*/
parse(): GifInfo;
/**
* Gets basic information about the GIF
*/
getInfo(): GifInfo;
/**
* Extracts all frames from the GIF with proper canvas composition
*/
getFrames(): GifFrame[];
/**
* Checks if the GIF is animated
*/
isAnimated(): boolean;
/**
* Gets the dominant colors from the global color table
*/
getDominantColors(count?: number): RGBColor[];
/**
* Gets all colors from the global color table
*/
getAllColors(): RGBColor[];
}
/**
* Convenience function to read GIF info from buffer
*/
export declare function readGifInfo(data: Uint8Array): GifInfo;
/**
* Convenience function to check if data is a valid GIF
*/
export declare function isValidGif(data: Uint8Array): boolean;