JFLib

img_gif#

GIF -- crate root lib/img_gif/lib.rs

img_gif -- GIF decoder and encoder, and the GIF parts of the core-format glue
                                                 [src/img/gif.s, src/img/imgcore.s]

  decode(data, sf) -> 0 / ERR_*                  gif_decode: FMT_PARGB
  decode_fl(data, sf, flags) -> 0 / ERR_*        gif_decode_fl: JDF_STRAIGHT -> FMT_ARGB
  encode(sf) -> Some(file) / None                gif_encode
  encode_q(sf, qcfg) -> Some(file) / None        gif_encode_q (quantize_ex cfg)
  meta(data, meta, flags) -> 0 / ERR_*           gif_meta (imgcore.s)
  probe(data, meta) -> recognised                gif_probe
  decode_ex(data, sf, dec, meta) -> 0 / ERR_*    gif_decode_ex
  encode_opt(sf, opt, out) -> 0 / ERR_*          gif_encode_opt
  CODEC                                          gif_codec

Decoder: GIF87a / GIF89a, global and local colour tables, LZW minimum code sizes 1..8,
clear codes anywhere (early clear, deferred clear = codes after the table filled up without
a clear, full 4096-entry tables), interlace, the Graphic Control Extension transparency
index.  Only the first image is decoded (animations show their first frame); the metadata
scanner counts the frames.  The image is placed at its offset on a transparent canvas of the
logical screen size, grown to the frame extent (left + width, top + height) where the screen
is smaller; a 0-sized frame takes the screen size.  Application / comment / plain-text and
unknown extensions are skipped (a GCE counts for the image only when it is the last one
before it).
Colours: index -> opaque palette colour; index past the colour table -> opaque black; no
colour table at all -> grey ramp (index i -> i,i,i); the GCE transparent index -> 0 (also in
FMT_ARGB output: GIF has only binary transparency, so both formats hold the same bytes).
Code size rule (giflib / Wuffs): after every code other than a clear code the width grows by
one bit when next_code >= 2^width (max 12 bits).
Errors: ERR_TRUNC (data ends before the image descriptor is complete), ERR_HDR (no GIF87a /
GIF89a signature), ERR_CHUNK (trailer or an unknown block before the first image), ERR_DIMS
(canvas w or h > 32768, w * h > 2^28, or a 0-sized frame on a 0-sized screen),
ERR_UNSUPPORTED (LZW minimum code size outside 1..8).  Once the image descriptor has been
read the result is always 0: a truncated local colour table, missing, truncated or corrupt
LZW data end the image and the pixels not decoded stay transparent.

Encoder: GIF89a, one image.  img_core::quantize builds the palette: exact when the image has
at most 256 colours (transparent counts as one), else Wu quantization to 256 colours (255 +
transparent) with serpentine Floyd-Steinberg dithering (encode: level 8, encode_q: the cfg's
level 0..8).  Alpha < threshold (encode: 128) becomes transparent (GCE written only when such
pixels exist).  Input FMT_PARGB / FMT_ARGB / FMT_XRGB / FMT_A8.  Global colour table of the
next power of two >= the colour count (min 2), LZW minimum code size max(2, table bits),
variable code width up to 12 bits, a clear code whenever the 4096-entry table is full.

Metadata (gif_meta): logical screen, colour table depth, frame count (walks all blocks),
first frame delay, NETSCAPE loop count, transparency, interlace of the first frame, an ICC
profile from an "ICCRGBG1" "012" application extension (IMGD_ICC).

img_gif API#

  • static CODEC: Codec gif_codec

  • fn decode(data: &[u8], sf: &mut Surface) -> i32 gif_decode: the first image as a new FMT_PARGB surface (sf is replaced; untouched on error)

  • fn decode_fl(data: &[u8], sf: &mut Surface, flags: u32) -> i32 gif_decode_fl: gif_decode; flags JDF_STRAIGHT: FMT_ARGB surface (same bytes)

  • fn encode(sf: &Surface) -> Option<Vec<u8>> gif_encode: GIF89a file (dither level 8, threshold 128) or None (empty or larger than 65535 x 65535, unknown format)

  • fn encode_q(sf: &Surface, qcfg: u32) -> Option<Vec<u8>> gif_encode_q: gif_encode with a quantize_ex cfg (dither level | threshold << IQ_THRESH_SHIFT; IQF_* flags are ignored)

  • fn meta(d: &[u8], m: &mut ImgMeta, flags: u32) -> i32 gif_meta: logical screen and a walk over all blocks -> 0 / ERR_TRUNC / ERR_HDR

  • fn probe(data: &[u8], m: &mut ImgMeta) -> bool gif_probe: header + metadata scan, no pixels

  • fn decode_ex(data: &[u8], sf: &mut Surface, dec: &ImgDec, m: &mut ImgMeta) -> i32 gif_decode_ex: the first frame through decode_fl (core_decode); meta.w / h become the canvas size decode_fl produced

  • fn encode_opt(sf: &Surface, opt: &ImgOpt, out: &mut Vec<u8>) -> i32 gif_encode_opt: encode_q with the dither level ImgOpt::dither and the threshold ImgOpt::athresh -> 0 / ERR_NOMEM (also for a surface encode_q refuses) / ERR_CANCEL

Source: docs/API.md in the JFLib repository.