Skip to content

add progressive JPEG encoding - #86

Merged
matmen merged 5 commits into
masterfrom
progressive-jpeg
Nov 29, 2026
Merged

matmen merged 5 commits into
masterfrom
progressive-jpeg

Conversation

@matmen

@matmen matmen commented Nov 28, 2026 •

Copy link
Copy Markdown
Collaborator

Adds encodeJPEG({ progressive: true }): browsers show the whole picture blurry first and sharpen it while the file loads, and the file is about 3% smaller for photos. The pixels are the same as in the baseline file of the same quality.

Refs #85. Trellis quantization isn't part of this PR.

await image.encodeJPEG({ quality: 80, progressive: true });

false by default, so existing files stay byte for byte the same. The encoder translates libjpeg's progressive encoder (jcphuff.c). The scans (8 for color, 4 for grayscale) are a fixed choice from the ones mozjpeg's scan search tries, each with its own Huffman tables. Both are credited in the file header, THIRD_PARTY_NOTICES.md and the README. An embedded gain map stays a baseline JPEG.

On 18 images (photos, a graphic, a screenshot) the files are 2.7% (quality 60), 3.0% (80) and 3.5% (92) smaller. mozjpeg's own script (9 scans) was 0.2% larger at 60, the same at 80 and 0.6% smaller at 92, but 10 to 15% slower to write. Encoding takes 1.1 to 1.3 times as long as baseline (a 2400×1600 photo at quality 80: 75 to 95 ms).

Checked:

  • cjpeg -scans (libjpeg-turbo 2.1.5) with the same script gives identical files for 288 cases. tests/jpeg-progressive-encode.js keeps 18 of them as hashes, aimed at the corners of the coding, and checks that a scan too long to replay is written the same.
  • Progressive and baseline files decode to the same pixels, also with transparency and with a gain map.
  • Node, Bun and Deno; lint, typecheck and the suite pass.
  • Ultra HDR images look the same in Chrome and the Android gallery.

@matmen matmen linked an issue Nov 28, 2026 that may be closed by this pull request
@github-actions

github-actions Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Test coverage

Compared with master where the branch left it (24a7a85).

Lines Branches Functions
total 97.7% 90.8% 98.0%
src/codecs/jpeg.ts (145 lines untested) 94.0% (+1.9) 91.7% (+1.5) 94.7% (+3.0)
src/codecs/av1/decode.ts (141 lines untested) 91.7% 84.0% 97.9%
src/render/layout.ts (105 lines untested) 93.6% 87.7% 95.7%
src/render/svg.ts (89 lines untested) 97.9% 89.9% 97.3%
src/render/shape.ts (73 lines untested) 92.8% 88.0% 98.1%
src/codecs/jxl/headers.ts (62 lines untested) 87.0% 71.0% 86.4%
src/codecs/av1/obu.ts (43 lines untested) 93.0% 73.9% 92.8%
src/codecs/jxl/modular.ts (43 lines untested) 94.5% 82.7% 100.0%
src/codecs/jxl/fast-lossless.ts (37 lines untested) 94.9% 97.8% 93.0%
src/codecs/av1/frame.ts (35 lines untested) 73.1% 33.3% 100.0%

Changed

Lines Branches Functions
src/codecs/jpeg.ts 94.0% (+1.9) 91.7% (+1.5) 94.7% (+3.0)

@matmen
matmen marked this pull request as ready for review November 28, 2026 22:37
@matmen
matmen merged commit 07793ce into master Nov 29, 2026
17 checks passed
@matmen
matmen deleted the progressive-jpeg branch November 29, 2026 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mozjpeg support?

1 participant