This repository demonstrates a decoding discrepancy affecting certain lossless JPEG images extracted from DICOM files.
The included file dcm.jpg is a single-slice example extracted from the problematic DICOM dataset described in rordenlab/dcm2niix#977.
In that dataset, slices that contain no intensity variability (completely empty slices) are misdecoded — each pixel should have an intensity of 24, yet dcm2niix and several independent JPEG lossless decoders both report a value of 28675.
This image is encoded in the legacy ITU-T81 lossless JPEG format (specification PDF).
Interestingly, independent JavaScript implementations by Mango replicate the same behavior observed in dcm2niix’s C decoder.
Both Daikon 1.1’s lossless.js and the newer JPEGLosslessDecoderJS produce identical incorrect results.
node decode.js dcm.jpg
First three voxel values: 28675, 28675, 28675npm install jpeg-lossless-decoder-js
node decode_jpeglossless.js dcm.jpg
Found at require(...).Decoder
Decoded bytes: 524288
First 3 raw bytes: 3, 112, 3
First 3 samples as Int16 LE: 28675, 28675, 28675Both decoders interpret the first pixel as 28675 rather than 24.
ImageMagick correctly reports a pixel intensity of 24 for the same file (assuming proper point transform handling):
magick dcm.jpg -depth 16 -format "%[fx:u.p{0,0}*65535]\n" info:
24(Note: ImageMagick may internally use a similar JPEG decoder to the one that created the original compression, so its correctness could depend on that implementation. However, the decoded result here matches the expected 16-bit pixel value of 24.)
| Tool / Library | Reported Value | Expected |
|---|---|---|
| dcm2niix (C) | 28675 | 24 |
Daikon lossless.js |
28675 | 24 |
| JPEGLosslessDecoderJS | 28675 | 24 |
| ImageMagick | 24 | Correct |
The error likely arises from signed/unsigned interpretation and missing point transform handling in these JPEG lossless decoders.
You can experience the issue with the source images by using the mangoviewer and opening the sample DICOM dataset. Note that all the empty slices superior to the brai age extremely bright instead of dark.
- ITU-T81 (JPEG Standard): https://www.w3.org/Graphics/JPEG/itu-t81.pdf
- dcm2niix issue: rordenlab/dcm2niix#978
- Daikon 1.1 release: rii-mango/Daikon
- JPEGLosslessDecoderJS: rii-mango/JPEGLosslessDecoderJS
- ImageMagick: https://imagemagick.org/
