]> Creatis software - gdcm.git/commitdiff
A few nasty patches to allow the reading of a lot of nasty images
authorjpr <jpr>
Mon, 6 Oct 2003 13:37:25 +0000 (13:37 +0000)
committerjpr <jpr>
Mon, 6 Oct 2003 13:37:25 +0000 (13:37 +0000)
( e.g : well known GE images with a wrong elem length,
      : Philips MRI Images with a wrong Huffman table)
--> still to be done : allow the reading of bugged LEONARDO Siemens images

src/gdcmHeader.cxx
src/jpeg/libijg12/jchuff12.c
src/jpeg/libijg12/jcparam12.c
src/jpeg/libijg12/jdhuff12.c
src/jpeg/libijg12/jdmarker12.c

index e59c6657a68281dcef0c0eb96498fcf075f3a36b..a3e31d31131425a27f34d2315656f5e5539d2142 100644 (file)
@@ -1,4 +1,4 @@
-// $Header: /cvs/public/gdcm/src/Attic/gdcmHeader.cxx,v 1.94 2003/10/03 16:22:24 jpr Exp $
+// $Header: /cvs/public/gdcm/src/Attic/gdcmHeader.cxx,v 1.95 2003/10/06 13:37:25 jpr Exp $
 
 #include "gdcmHeader.h"
 
@@ -612,6 +612,11 @@ bool gdcmHeader::IsDicomV3(void) {
 void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) {
    if ( FoundLength == 0xffffffff)
       FoundLength = 0;
+      // Sorry for the patch!  
+      // XMedCom did the trick to read some nasty GE images ...
+    if (FoundLength == 13) 
+      FoundLength =10;
+
    ElVal->SetLength(FoundLength);
 }
 
@@ -694,6 +699,7 @@ void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) {
          fseek(fp, 2L, SEEK_CUR);
 
          guint32 length32 = ReadInt32();
+
          if ( (vr == "OB") && (length32 == 0xffffffff) ) {
             ElVal->SetLength(FindLengthOB());
             return;
@@ -758,9 +764,12 @@ void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) {
       // Heuristic: well some files are really ill-formed.
       if ( length16 == 0xffff) {
          length16 = 0;
-         dbg.Verbose(0, "gdcmHeader::FindLength",
-                     "Erroneous element length fixed.");
+         //dbg.Verbose(0, "gdcmHeader::FindLength",
+         //            "Erroneous element length fixed.");
+         // Actually, length= 0xffff means that we deal with
+         // Unknown Sequence Length 
       }
+
       FixFoundLength(ElVal, (guint32)length16);
       return;
    }
index 1d745c8734b6459b2e6f3404baf245f4dcc27a23..7b879a1e09c25ea8c1173359a94f038696d40f27 100644 (file)
@@ -109,6 +109,8 @@ start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
   int ci, dctbl, actbl;
   jpeg_component_info * compptr;
 
+fprintf (stderr,"=======================================================JPEG12\n");
+
   if (gather_statistics) {
 #ifdef ENTROPY_OPT_SUPPORTED
     entropy->pub.encode_mcu = encode_mcu_gather;
@@ -210,8 +212,10 @@ jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
   p = 0;
   for (l = 1; l <= 16; l++) {
     i = (int) htbl->bits[l];
-    if (i < 0 || p + i > 256)  /* protect against table overrun */
+    if (i < 0 || p + i > 256) {        /* protect against table overrun */
+      printf ("JERR_BAD_HUFF_TABLE : protect against table overrun  (i=%d p=%d)\n",i,p);
       ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+    }
     while (i--)
       huffsize[p++] = (char) l;
   }
@@ -232,8 +236,10 @@ jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
     /* code is now 1 more than the last code used for codelength si; but
      * it must still fit in si bits, since no code is allowed to be all ones.
      */
-    if (((INT32) code) >= (((INT32) 1) << si))
+    if (((INT32) code) >= (((INT32) 1) << si)) {
+      printf("JERR_BAD_HUFF_TABLE : (((INT32) code) >= (((INT32) 1) << si)) code %d si%d\v",code, si);
       ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+    }
     code <<= 1;
     si++;
   }
@@ -256,8 +262,10 @@ jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
 
   for (p = 0; p < lastp; p++) {
     i = htbl->huffval[p];
-    if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
+    if (i < 0 || i > maxsymbol || dtbl->ehufsi[i]) {
+      printf("JERR_BAD_HUFF_TABLE (i < 0 || i > maxsymbol || dtbl->ehufsi[i]) i %d maxsymbol %d dtbl->ehufsi[i] %d\n", i, maxsymbol, dtbl->ehufsi[i]);
       ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+    }
     dtbl->ehufco[i] = huffcode[p];
     dtbl->ehufsi[i] = huffsize[p];
   }
index 15f787d8ad96e1e1cd45feba8da0cfcb2c6d97b9..ba1952ff0f01bb3e5a0e896fa3874573012f0be0 100644 (file)
@@ -168,8 +168,10 @@ add_huff_table (j_compress_ptr cinfo,
   nsymbols = 0;
   for (len = 1; len <= 16; len++)
     nsymbols += bits[len];
-  if (nsymbols < 1 || nsymbols > 256)
+  if (nsymbols < 1 || nsymbols > 256) {
+    printf ("JERR_BAD_HUFF_TABLE nsymbols %d\n",nsymbols);
     ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+  }
 
   MEMCOPY((*htblptr)->huffval, val, nsymbols * SIZEOF(UINT8));
 
index 0a3b012a950d758662478b078053fe4605dbb231..b3734b89ab366b2d70ecd0d8aabc39d788464388 100644 (file)
@@ -182,8 +182,10 @@ jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
   p = 0;
   for (l = 1; l <= 16; l++) {
     i = (int) htbl->bits[l];
-    if (i < 0 || p + i > 256)  /* protect against table overrun */
+    if (i < 0 || p + i > 256){ /* protect against table overrun */
+      printf ("JERR_BAD_HUFF_TABLE : protect against table overrun  (i=%d p=%d)\n",i,p);
       ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+    }
     while (i--)
       huffsize[p++] = (char) l;
   }
@@ -204,8 +206,10 @@ jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
     /* code is now 1 more than the last code used for codelength si; but
      * it must still fit in si bits, since no code is allowed to be all ones.
      */
-    if (((INT32) code) >= (((INT32) 1) << si))
+    if (((INT32) code) >= (((INT32) 1) << si)) {
+      printf("JERR_BAD_HUFF_TABLE : (((INT32) code) >= (((INT32) 1) << si)) code %d si%d\v",code, si);
       ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+    }
     code <<= 1;
     si++;
   }
@@ -259,8 +263,14 @@ jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
   if (isDC) {
     for (i = 0; i < numsymbols; i++) {
       int sym = htbl->huffval[i];
-      if (sym < 0 || sym > 15)
-       ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+      //printf ("htbl->huffval[%d]=%d\n",i,htbl->huffval[i]); 
+      if (sym < 0 || sym > 15) {
+        printf("JERR_BAD_HUFF_TABLE sym %d (>15)\n",sym);
+       //ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+        // Sorry for the patch :
+        // Now, we can read Philips MRI Images
+        htbl->huffval[i]=15;
+      }
     }
   }
 }
index 86f3a0ecba4fec47a09b7e8705993971e8bd468c..6c6309f4070c14271b31078ec12528e501554339 100644 (file)
@@ -453,9 +453,10 @@ get_dht (j_decompress_ptr cinfo)
     /* Here we just do minimal validation of the counts to avoid walking
      * off the end of our table space.  jdhuff.c will check more carefully.
      */
-    if (count > 256 || ((INT32) count) > length)
+    if (count > 256 || ((INT32) count) > length) {
+      printf("JERR_BAD_HUFF_TABLE in jdmarker12.c count : %d\n",count);
       ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
-
+    }
     for (i = 0; i < count; i++)
       INPUT_BYTE(cinfo, huffval[i], return FALSE);