Skip to content

Support non-full-depth uint16 PNM input (10/12/14-bit)#202

Open
chemag wants to merge 1 commit intogoogle:mainfrom
chemag:main
Open

Support non-full-depth uint16 PNM input (10/12/14-bit)#202
chemag wants to merge 1 commit intogoogle:mainfrom
chemag:main

Conversation

@chemag
Copy link
Copy Markdown

@chemag chemag commented Apr 7, 2026

The encoder rejected uint16 input unless bits_per_sample was exactly 16, causing failures for valid PGM files with maxval other than 65535 (e.g. 10-bit maxval=1023, 12-bit maxval=4095, 14-bit maxval=16383).

Fix by converting non-full-depth uint16 data to float with proper normalization (value / (2^N - 1)) before feeding to jpegli, which internally works in float anyway.

Tested with all 8-16 bit depth PNM files in testdata/:

$ for f in $(find testdata -type f ( -name ".pgm" -o -name ".ppm" -o -name ".pnm" -o -name ".pfm" -o -name ".pam" ) | grep -vP '.depth[1-7].' | sort); do ./build/tools/cjpegli "$f" "/tmp/$(basename ${f%.}).jpg" -q 90; done Read 320x320 image, 1228816 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 20762 bytes (1.622 bpp).
320 x 320, 33.281 MP/s, 1 threads.
Read 2268x1512 image, 3429233 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 479651 bytes (1.119 bpp).
2268 x 1512, 69.906 MP/s, 1 threads.
Read 2268x1512 image, 10287665 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 588499 bytes (1.373 bpp).
2268 x 1512, 38.805 MP/s, 1 threads.
Read 510x532 image, 1085356 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 42514 bytes (1.254 bpp).
510 x 532, 55.436 MP/s, 1 threads.
Read 510x532 image, 1085356 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 42539 bytes (1.254 bpp).
510 x 532, 57.930 MP/s, 1 threads.
Read 510x532 image, 1085356 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 42498 bytes (1.253 bpp).
510 x 532, 58.650 MP/s, 1 threads.
Read 510x532 image, 1085356 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 42507 bytes (1.253 bpp).
510 x 532, 57.904 MP/s, 1 threads.
Read 510x532 image, 1085357 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 42504 bytes (1.253 bpp).
510 x 532, 57.796 MP/s, 1 threads.
Read 510x532 image, 1085357 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 42485 bytes (1.253 bpp).
510 x 532, 57.631 MP/s, 1 threads.
Read 510x532 image, 1085357 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 42492 bytes (1.253 bpp).
510 x 532, 49.802 MP/s, 1 threads.
Read 510x532 image, 542715 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 42492 bytes (1.253 bpp).
510 x 532, 50.964 MP/s, 1 threads.
Read 510x532 image, 1085355 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 42551 bytes (1.255 bpp).
510 x 532, 56.318 MP/s, 1 threads.
Read 510x532 image, 542656 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 42514 bytes (1.254 bpp).
510 x 532, 55.254 MP/s, 1 threads.
Read 510x532 image, 542656 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 42539 bytes (1.254 bpp).
510 x 532, 57.512 MP/s, 1 threads.
Read 510x532 image, 542656 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 42498 bytes (1.253 bpp).
510 x 532, 55.191 MP/s, 1 threads.
Read 510x532 image, 542656 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 42507 bytes (1.253 bpp).
510 x 532, 57.439 MP/s, 1 threads.
Read 510x532 image, 542657 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 42504 bytes (1.253 bpp).
510 x 532, 57.402 MP/s, 1 threads.
Read 510x532 image, 542657 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 42485 bytes (1.253 bpp).
510 x 532, 57.802 MP/s, 1 threads.
Read 510x532 image, 542657 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 42492 bytes (1.253 bpp).
510 x 532, 63.444 MP/s, 1 threads.
Read 510x532 image, 271335 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 42492 bytes (1.253 bpp).
510 x 532, 65.117 MP/s, 1 threads.
Read 510x532 image, 542655 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 42551 bytes (1.255 bpp).
510 x 532, 56.888 MP/s, 1 threads.
Read 510x532 image, 2170630 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 55546 bytes (1.638 bpp).
510 x 532, 32.474 MP/s, 1 threads.
Read 510x532 image, 2170630 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 55536 bytes (1.638 bpp).
510 x 532, 31.938 MP/s, 1 threads.
Read 510x532 image, 2170630 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 55508 bytes (1.637 bpp).
510 x 532, 32.667 MP/s, 1 threads.
Read 510x532 image, 2170630 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 55515 bytes (1.637 bpp).
510 x 532, 32.037 MP/s, 1 threads.
Read 510x532 image, 2170631 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 55507 bytes (1.637 bpp).
510 x 532, 32.238 MP/s, 1 threads.
Read 510x532 image, 2170631 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 55527 bytes (1.637 bpp).
510 x 532, 31.883 MP/s, 1 threads.
Read 510x532 image, 2170631 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 55540 bytes (1.638 bpp).
510 x 532, 31.580 MP/s, 1 threads.
Read 510x532 image, 1085349 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 55540 bytes (1.638 bpp).
510 x 532, 31.792 MP/s, 1 threads.
Read 510x532 image, 2170629 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 55458 bytes (1.635 bpp).
510 x 532, 31.598 MP/s, 1 threads.
Read 510x532 image, 1627936 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 55546 bytes (1.638 bpp).
510 x 532, 31.344 MP/s, 1 threads.
Read 510x532 image, 1627936 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 55536 bytes (1.638 bpp).
510 x 532, 32.518 MP/s, 1 threads.
Read 510x532 image, 1627936 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 55508 bytes (1.637 bpp).
510 x 532, 32.627 MP/s, 1 threads.
Read 510x532 image, 1627936 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 55515 bytes (1.637 bpp).
510 x 532, 32.560 MP/s, 1 threads.
Read 510x532 image, 1627937 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 55507 bytes (1.637 bpp).
510 x 532, 32.853 MP/s, 1 threads.
Read 510x532 image, 1627937 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 55527 bytes (1.637 bpp).
510 x 532, 32.485 MP/s, 1 threads.
Read 510x532 image, 1627937 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 55540 bytes (1.638 bpp).
510 x 532, 35.257 MP/s, 1 threads.
Read 510x532 image, 813975 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 55540 bytes (1.638 bpp).
510 x 532, 35.527 MP/s, 1 threads.
Read 510x532 image, 1627935 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 55458 bytes (1.635 bpp).
510 x 532, 33.397 MP/s, 1 threads.
Read 320x320 image, 1228816 bytes.
Encoding [YUV d1.000 AQ p2 OPT]
Compressed to 5167 bytes (0.404 bpp).
320 x 320, 46.631 MP/s, 1 threads.

Description

Pull Request Checklist

  • CLA Signed: Have you signed the Contributor License Agreement (individual or corporate, as appropriate)? Only contributions from signed contributors can be accepted.
  • Authors: Have you considered adding your name to the AUTHORS file?
  • Code Style: Have you ensured your code adheres to the project's coding style guidelines? You can use ./ci.sh lint for automatic code formatting.

Please review the full contributing guidelines for more details.

The encoder rejected uint16 input unless bits_per_sample was exactly 16,
causing failures for valid PGM files with maxval other than 65535 (e.g.
10-bit maxval=1023, 12-bit maxval=4095, 14-bit maxval=16383).

Fix by converting non-full-depth uint16 data to float with proper
normalization (value / (2^N - 1)) before feeding to jpegli, which
internally works in float anyway.

Tested encode/decode roundtrip (cjpegli -q 90 + djpegli) with all 8-16
bit depth PNM files in testdata/. PSNR and per-pixel error (in 8-bit
units) are consistent across all depths:

Grayscale (PGM) - flower_small
Depth   PSNR (dB)      Mean    Median    StdDev       Max
------------------------------------------------------------------------
    8       45.88     0.946     1.000     0.885     9.000
    9       45.79     1.018     0.804     0.822     9.603
   10       45.86     1.007     0.827     0.820     8.654
   11       45.88     0.981     0.921     0.846     8.217
   12       45.89     0.964     0.952     0.864     8.941
   13       45.88     0.956     0.983     0.874     8.979
   14       45.88     0.951     0.992     0.880     8.982
   15       45.88     0.948     0.999     0.883     8.999
   16       45.88     0.946     1.000     0.885     9.000

RGB (PPM) - flower_small
Depth   PSNR (dB)      Mean    Median    StdDev       Max
------------------------------------------------------------------------
    8       41.58     1.542     1.000     1.464    32.000
    9       41.51     1.597     1.231     1.430    32.740
   10       41.56     1.584     1.226     1.426    32.361
   11       41.57     1.567     1.114     1.441    32.047
   12       41.58     1.554     1.055     1.451    32.077
   13       41.58     1.548     1.027     1.457    32.061
   14       41.58     1.545     1.013     1.460    32.022
   15       41.58     1.543     1.004     1.463    32.010
   16       41.58     1.542     1.000     1.464    32.000
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.

1 participant