Skip to content

add progressive JPEG encoding - #86

Open
matmen wants to merge 4 commits into
masterfrom
progressive-jpeg
Open

matmen wants to merge 4 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 2 to 4% smaller for photos. The pixels are the same as in the baseline file of the same quality.

Refs #85. Trellis quantization and tuned quantization tables aren'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) and uses mozjpeg's scan script (8 scans for color, 4 for grayscale), each scan with its own Huffman tables. Both are credited in the file header, THIRD_PARTY_NOTICES.md and the README.

On 14 photos the files are 2.1% (quality 60), 2.6% (80) and 3.2% (92) smaller. Encoding takes up to 1.3 times as long (a 2400×1600 photo at quality 80: 80 to 97 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 16 of them as hashes, aimed at the corners of the coding.
  • Progressive and baseline files decode to the same pixels, also with transparency.
  • Node, Bun and Deno; lint, typecheck and the suite pass.
  • Ultra HDR images (gain map kept) 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% 97.8% (-0.1)
src/codecs/jpeg.ts (167 lines untested) 93.1% (+1.0) 90.9% (+0.8) 89.5% (-2.2)
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 93.1% (+1.0) 90.9% (+0.8) 89.5% (-2.2)

@matmen
matmen marked this pull request as ready for review November 28, 2026 22:37

This branch has not been deployed

No deployments
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