Decoding LZW streams, interlaced passes, and color tables
Using a local GIF frame extractor to split animations into still images inside browser memory keeps product demos, confidential screen recordings, and custom game sprites off remote conversion servers. Running a pure TypeScript decoder inside image.worker.ts outputs exact full-canvas frames with zero external uploads.
Parsing GIF87a and GIF89a headers inside image.worker.ts
Upon dropping an image or clicking Load sample GIF, the worker verifies the six-byte GIF87a or GIF89a signature and reads little-endian logical width and height. Next, it walks through extension blocks (0x21) for NETSCAPE2.0 or ANIMEXTS1.0 loop counters alongside Graphic Control Extensions (0xF9) before reading each 0x2C image block.
Per the W3C GIF89a Specification, compressed pixel indices arrive inside sub-blocks up to 255 bytes using LZW codes (4,096-entry dictionary). When an image flags interlacing, our decoder reconstructs rows across four passes. Users sorting stickers alongside still WebP exports can also decode WebP graphics into lossless PNGs offline.
Handling Global Color Tables, local palettes, and limits
After reading the Logical Screen Descriptor, the parser loads any 2 to 256 color Global Color Table while checking for frame-specific Local Color Tables. Using each frame’s own local palette prevents hue shifts in multi-palette animations converted from video clips.
To keep browser tabs responsive against malformed inputs, extractGifFrames caps logical canvas dimensions at 4,096 × 4,096 (16,777,216 pixels) and 600 frames maximum. Clicking Load sample GIF generates a deterministic 48 × 48 px four-frame sample-spinner.gif using an 8-color palette and 120 ms delays.
Why do raw sub-frames break without disposal compositing?
Many animated GIFs optimize file size by storing only tiny changed rectangles (fx, fy, fw, fh) and masking unchanged pixels as transparent. Reading those sub-blocks in isolation without tracking GIF89a disposal states across a persistent logical canvas produces partial fragments instead of complete video frames.
Restoring canvas state across disposal methods 0 through 3
As summarized in the Library of Congress GIF89a Format Description, GIF encoders rely on incremental frame updates rather than storing full bitmaps every tick. After compositing each frame, the decoder evaluates bits 2–4 of the Graphic Control Extension packed byte to prepare the canvas for the next frame.
When disposal equals 2, the worker clears the just-drawn rectangle back to either the background palette entry or full transparent alpha if transparency appeared earlier. When disposal equals 3, it copies back a prevBackup canvas snapshot saved immediately before decoding that frame. Designers converting extracted sprites into retro tilesets can feed individual PNG frames straight into our Pixel Art Converter.
Preserving alpha transparency from Graphic Control Extensions
Before drawing frame 1, the logical canvas initializes with alpha 0 so transparent pixels inside the opening Graphic Control Extension remain transparent. During pixel painting, any colorIdx matching the frame’s transparentIndex skips overwriting the underlying Uint8ClampedArray pixel.
Each completed canvas snapshot then encodes directly to 32-bit RGBA PNG (colorType = 6) via encodePngRgba without touching an HTML <canvas> element. Bypassing <canvas> color-management prevents browser-specific gamma adjustments or premultiplied-alpha edge halos around thin outlines.
GIF89a disposal methods, delays, and selection controls
Inspecting how Graphic Control Extension fields interact with our Uint8ClampedArray canvas makes debugging flickering sprites or uneven playback timing straightforward. Exported layers match the lossless 8-bit RGBA pixel model defined in the W3C PNG Third Edition Specification.
| GIF89a field or UI control | Byte / default value | Canvas or UI behavior | Common animation usage |
|---|---|---|---|
Disposal method 0 / 1 |
Leave graphic in place | Keeps pixels visible for next sub-frame | Delta overlays and moving sprites over static scenes |
Disposal method 2 |
Restore background | Clears frame bounds to background or transparent | Spinning icons and transparent stickers without trails |
Disposal method 3 |
Restore previous | Reverts canvas to pre-frame prevBackup snapshot |
Blinking eyes or temporary badges over rich art |
| Graphic Control delay | delayCs * 10 ms (100 ms fallback) |
Sets per-card delay badge and @timestamp ms |
Inspecting variable cadence or frame holds |
| Every 2nd frame | Even-index filter | Checks frames 1, 3, 5... (idx % 2 === 0) |
Halving 50 fps captures to lightweight keyframes |
When a single GIF already packs a multi-panel collage or grid layout that needs slicing into separate tiles afterward, hand the saved PNG off to sprite grid and carousel slicing. Summary pills above the grid show exact selected counts (X of N selected) alongside total duration and loop count.
Tip: Click Load sample GIF and inspect frame timestamps (
@0 ms,@120 ms,@240 ms,@360 ms) to see how disposal2clears each moving colored quadrant cleanly between120 msticks.
Exporting single PNG frames and store-mode ZIP archives
Once your frame cards finish rendering, a checkerboard background makes transparent pixels immediately visible beside exact PNG byte sizes. Downloading outputs straight from browser RAM avoids server queues, watermarks, or file expiration links.
Picking keyframes with Every 2nd frame and Save PNG
Clicking Save PNG on any frame card exports that single full-canvas image immediately as <stem>-frame-0001.png using four-digit zero-padding. For multi-frame exports, Select all, Every 2nd frame, and Clear selection toggle checkboxes in bulk before clicking Download selected ZIP.
Inside zipSelectedGifFrames, filenames zero-pad dynamically (-frame-001.png or wider for 1,000+ frames) to preserve chronological sorting across macOS Finder, Windows Explorer, and Linux shells. Following the PKWARE ZIP File Format Specification, PNG archives bundle under Store compression (level: 0) because PNG streams are already Deflate-compressed, keeping archive creation snappy.
Assembling sprite sheets and pixel-art assets offline
Game developers and UI motion engineers frequently unpack animated GIF sprites into crisp PNG frames to inspect individual timing holds or edit single pixels cleanly. Removing redundant in-between frames with Every 2nd frame also shrinks sprite sheet footprints before bundling assets for CSS animations.
After selecting and downloading your keyframe PNGs, you can join extracted keyframes into vertical sprite strips offline without touching desktop editors. Every allocated blob URL revokes automatically when loading a new GIF or closing the tab.