I wrote a jpeg compressor / decompressor a few years ago that can process files without loss or loss of jpeg. It works well, but does not always correctly decode jpeg streams in DICOM files.
I know jpeg well, but I know little about DICOM. Lossless jpeg in DICOM cannot conform to jpeg ISO standard. There must be some kind of modification, either hardcoded or changed by a parameter somewhere in the DICOM file outside the jpeg file stream.
My code fails in most DICOM sample files (compsamples_jpeg.tar) at ftp://medical.nema.org/MEDICAL/Dicom/DataSets/WG04/
Here, when I decrypt the first lossless jpeg (IMAGES \ JPLL \ CT1_JPLL) in this set:
decoded image dicom
The left image is rendered from my code, the DICOM online reader is displayed on the right: www (dot) ofoct (dot) com (slash) viewer (slash) dicom-viewer-online (dot) html
(x) MedCon, an open source DICOM reader, fails in the same pixel as my code, so I'm not the only one who has this problem. xmedcon dot sourceforge dot net
I read that byte of jpeg by byte byte, drew a huffman tree and computed the huffman codes with pencil and paper, and my code does exactly what it should do. Here are the Huffman codes:
- 0 00
- 4 01
- 3 100
- 5 101
- 1100
- 2 1101
- 6 1110
- 7 11110
- 8 111110
- 9 1111110
- 12 11111110
- 11 111111110
- 10 1111111110
- 15 11111111110
Here is the compressed data after the SOS marker:
- ff 00 de 0c 00 (00 after ff is a byte)
- 11111111 11011110 00001100 00000000
- 11111111110 si = 15
- 111100000110000 diff = 30768
In online viewing mode, the first pixel value is -3024. If this is correct, the first diff value should be -3024, but it is not.
After that, my code correctly decodes about 2/5 of the image, but then decodes the wildly inaccurate diff value:
If you look at the image decoded by the online viewer, there is no radical change in pixel intensity in this place, so the value si = 11 cannot be correct.
I'm sure I have a good understanding of jpeg, but jpeg streams in DICOM don't seem to follow the jpeg standard. What extensions / changes are made to jpeg streams when they are embedded in DICOM files?