images.cr

Pure Crystal image decoding. JPEG port of zune-jpeg with fast scalar IDCT, fancy upsampling and scaled decode.

images.cr

Pure Crystal image decoding. Zero C dependencies — the whole pipeline compiles with the Crystal compiler alone, which keeps Android/iOS cross-compilation trivial.

JPEG

A faithful scalar port of zune-jpeg (Rust; MIT / Apache-2.0 / Zlib), one of the fastest pure-Rust JPEG decoders (itself derived from stb_image's integer IDCT and libjpeg-turbo's fancy upsampling). The Rust reference lives in reference/zune-jpeg (0.4.13); color conversion uses the precise Bt.601 14-bit constants introduced in zune-jpeg 0.4.21.

Pipeline: marker parser → Huffman bitstream (9-bit lookahead + fast-AC tables, marker-aware refills) → dequantize/dezigzag → integer IDCT → triangle-filter upsampling (4:2:0/4:2:2) → color conversion.

Supported: baseline + progressive, 8-bit, grayscale / YCbCr / RGB / Adobe CMYK / YCCK, 4:4:4 / 4:2:2 / 4:2:0 (and odd ratios), restart markers, MJPEG (missing-DHT injection), best-effort decoding of corrupt files.

Conformance

  • Byte-exact vs zune-jpeg 0.4.13 on the whole synthetic corpus (baseline/progressive/grayscale/noise/subsampling variants/odd sizes).
  • CMYK: this port matches libjpeg-turbo (PIL) exactly; zune 0.4.21's CMYK→RGBA path writes 3-byte chunks into 4-channel output and is wrong there.
  • One real-world 3 Mpx progressive photo differs from the Rust references by ±1 on ~0.5% of pixels — the two zune versions themselves disagree on that file by ±3 across the whole frame, so this is IDCT rounding noise, not a decode error.
  • Truncated images decode best-effort (never raise) or return nil.

tools/ holds the conformance harness: gen_corpus.py (PIL-generated corpus), ref_decode.c (stb_image reference), compare.py, and a Rust zune-jpeg dumper. crystal spec runs the golden-value specs.

API

require "images"

# Full decode to straight-alpha RGBA8:
frame = Images.decode(File.read("photo.jpg").to_slice)
frame.width   # Int32
frame.height  # Int32
frame.rgba    # Bytes, width * height * 4

# Scaled decode (output FITS the box — thumbnails):
thumb = Images.decode(bytes, max_w, max_h)

# Scaled decode bounded below (output COVERS the box — display
# textures that must not upscale):
frame = Images.decode_at_least(bytes, box_w, box_h)

# Header-only probe:
size = Images.probe(bytes)   # => Images::Size(width, height, format)

Images.decode returns nil for undecodable input — never raises for corrupt image data.

Performance

Release-mode scalar decode on Apple Silicon (M-series, 2026); crystal build --release vs cc -O3 for stb_image. Higher is better. Corpus numbers are best-of-3 (tools/bench_corpus.cr vs tools/stb_bench.c, warm runs); before this optimization pass images.cr was 1.25–1.8× slower than stb_image on the same files.

Reference photo (1510×2048, ~3.1 MP, progressive 4:2:0):

decoder throughput
zune-jpeg 0.4.13 (Rust + NEON) 417 MP/s
images.cr (Crystal, scalar) ~310 MP/s
stb_image (C, scalar) ~190 MP/s

Full corpus (MP/s, ours ÷ stb in the last column):

file size images.cr stb_image ratio
real_full.jpg (progressive 4:2:0) 1510×2048 316 189 1.67×
real_progressive.jpg 1510×2048 628 479 1.31×
big_420.jpg (baseline 4:2:0) 257×189 441 367 1.20×
big_progressive.jpg 257×189 233 225 1.04×
restart_420.jpg 97×61 361 295 1.22×
baseline_420.jpg 97×61 298 299 1.00×
baseline_422.jpg 97×61 280 270 1.04×
baseline_444.jpg 97×61 237 229 1.04×
cmyk.jpg 97×61 248 216 1.15×
grayscale.jpg 97×61 442 570 0.78×
odd_gray_33x31.jpg 33×31 240 292 0.82×
progressive_420.jpg 97×61 186 186 1.00×
progressive_444.jpg 97×61 130 128 1.02×
noise_420.jpg 160×120 167 139 1.20×
noise_422.jpg 160×120 140 111 1.26×
noise_444.jpg 160×120 96 73 1.32×
noise_progressive.jpg 160×120 86 68 1.27×
odd_15x17.jpg 15×17 61 56 1.09×
odd_7x5.jpg 7×5 15 9 1.67×

At parity or faster on 17 of 20 files (up to 1.7× on real photos); the worst case is tiny grayscale images at ~1.3× slower, well within the usual 1.5× ceiling for managed-vs-C scalar code. Crystal has no stable SIMD API, so the NEON gap is structural.

Always build with --release (crystal build --release, crystal spec --release). Without it the optimizer is off and the decoder runs 10–30× slower — this is the most common reason for a "10× slower than C" measurement.

Implementation notes for the hot paths (all byte-exact vs the zune-jpeg port):

  • row kernels (color convert, upsampling, IDCT output) run on raw pointers with no bounds checks and no per-row allocations;
  • one pooled scratch arena backs all component planes per decode, and the progressive coefficient store is a single pooled allocation;
  • derived Huffman tables live in flat buffers (2–3 allocations per table) and are cached process-wide keyed by the raw DHT payload — standard Annex-K tables are built once per process and shared;
  • the decoder tracks whether a block has non-zero AC coefficients during entropy decode, so flat blocks skip the full IDCT.

Scaled decode (thumbnails)

Images.decode(bytes, max_w, max_h) may return a 1/2, 1/4 or 1/8 frame: s×s samples are box-averaged in component space (before color conversion), so output memory and color-conversion cost drop by s². Entropy decoding and the IDCT still run at full resolution — the output matches a box-average of the full decode (mean error ≈ 0.04). True scaled-IDCT kernels (libjpeg-style, skipping high frequencies) are future work.

Status

  • JPEG decode (baseline, progressive, CMYK/YCCK, restart markers)
  • Conformance harness + specs
  • Scaled decode (1/2, 1/4, 1/8) for thumbnails
  • PNG (zune-png port) and BMP/GIF/QOI codecs
  • WebP/HEIC: sniff-only (pure ports of VP8/AV1 are out of scope; optional bindings may come later)

License

MIT. The JPEG decoder is a port of zune-jpeg (MIT / Apache-2.0 / Zlib); see reference/zune-jpeg for the original sources and licenses.

Repository

images.cr

Owner
Statistic
  • 0
  • 0
  • 0
  • 1
  • 0
  • about 1 hour ago
  • December 2, 2026
License

Links
Synced at

Wed, 02 Dec 2026 21:58:31 GMT

Languages