Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with Huffman tables in jpg_0XC3.cpp #244

Closed
cgohlke opened this issue Oct 30, 2018 · 1 comment
Closed

Issue with Huffman tables in jpg_0XC3.cpp #244

cgohlke opened this issue Oct 30, 2018 · 1 comment

Comments

@cgohlke
Copy link

cgohlke commented Oct 30, 2018

Thank you for providing jpg_0XC3.cpp under the BSD license!

I am using a fork of your code in the Python imagecodecs package.

While testing some files, specifically f-18.jpg from the TwelveMonkeys testsuite, I noticed an issue with how missing Huffman tables are substituted.

The following patch fixes that issue for me:

diff --git a/console/jpg_0XC3.cpp b/console/jpg_0XC3.cpp
index f53123f..00b338c 100644
--- a/console/jpg_0XC3.cpp
+++ b/console/jpg_0XC3.cpp
@@ -141,6 +141,7 @@ unsigned char *  decode_JPEG_SOF_0XC3 (const char *fn, int skipBytes, bool verbo
     uint16_t SOFydim = 0;
     uint16_t SOFxdim = 0;
     // long SOSarrayPos; //SOFarrayPos
+    int lFrameCount = 1;
     int lnHufTables = 0;
     const int kmaxFrames = 4;
     struct HufTables l[kmaxFrames+1];
@@ -180,7 +181,6 @@ unsigned char *  decode_JPEG_SOF_0XC3 (const char *fn, int skipBytes, bool verbo
             }
         } else if (btMarkerType == 0xC4) {//if SOF marker else if define-Huffman-tables marker (DHT)
             if (verbose) printMessage(" [Huffman Length %d]\n", lSegmentLength);
-            int lFrameCount = 1;
             do {
                 uint8_t DHTnLi = readByte(lRawRA, &lRawPos, lRawSz ); //we read but ignore DHTtcth.
                 #pragma unused(DHTnLi) //we need to increment the input file position, but we do not care what the value is
@@ -325,8 +325,8 @@ unsigned char *  decode_JPEG_SOF_0XC3 (const char *fn, int skipBytes, bool verbo
     } //For each frame, e.g. once each for Red/Green/Blue
     //NEXT: some RGB images use only a single Huffman table for all 3 colour planes. In this case, replicate the correct values
     if (lnHufTables < SOFnf) { //use single Hufman table for each frame
-        for (int lFrameCount = 2; lFrameCount <= SOFnf; lFrameCount++) {
-            l[lFrameCount] = l[1];
+        for (int lFrameCount = lnHufTables+1; lFrameCount <= SOFnf; lFrameCount++) {
+            l[lFrameCount] = l[lnHufTables];
         } //for each frame
     } // if lnHufTables < SOFnf
     //NEXT: uncompress data: different loops for different predictors

Does this look OK?

@neurolabusc
Copy link
Collaborator

Yes, that works. By the way, the source in dcm2niix is also provided in a minimal C program to convert JPEG lossless to TIF. I will fix it with the same patch. It has many of the same images as the site you link (from LeadTools), but does not have the troublesome F-18 image. Thanks.

yarikoptic added a commit to neurodebian/dcm2niix that referenced this issue Dec 3, 2018
* tag 'v1.0.20181114': (70 commits)
  New stable release: update documentation (v1.0.20181114)
  Avoid using GDCM's internal OpenJPEG library.
  Discriminate trace from raw GE DWI scans (rordenlab#245)
  Restore Philips enhanced (rordenlab@3c31d18)
  Clean up readme, interpolation errors less verbose, add date-of-birth to BIDS (requires '-ba n')
  Not all GE DWI with b>0 bvec=0 are trace (rordenlab#245)
  calculate Zmm for enhanced DICOM w/o 0018,0088 (rordenlab#241)
  Kludge for Siemens MoCo slice timing interfered with CMRR fix (CMRR-C2P/MB#29)
  Bruker enhanced 4D data (rordenlab#241)
  XA10A tag substitutes (rordenlab#240)
  Huffman tables repeated for RGB planes for jpg_0XC3 (rordenlab#244)
  change MoCo naming and derived detection (rordenlab#243)
  Update dcm_qa_nih submodule.
  Update dcm_qa_nih submodule.
  Update dcm_qa submodule.
  experimental dcm4che enhanced support (rordenlab#241)
  Vida partial Fourier update (rordenlab#240)
  XA10A Vida dwell time (rordenlab#240)
  More XA10A Vida header info (rordenlab#240)
  UIH bvecs (rordenlab#225 (comment))
  ...
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

No branches or pull requests

2 participants