From 4aa1b564dbc367ea47737eaac09b39fa3ec3edf6 Mon Sep 17 00:00:00 2001 From: jpr Date: Mon, 6 Oct 2003 13:37:25 +0000 Subject: [PATCH] A few nasty patches to allow the reading of a lot of nasty images ( 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 | 15 ++++++++++++--- src/jpeg/libijg12/jchuff12.c | 14 +++++++++++--- src/jpeg/libijg12/jcparam12.c | 4 +++- src/jpeg/libijg12/jdhuff12.c | 18 ++++++++++++++---- src/jpeg/libijg12/jdmarker12.c | 5 +++-- 5 files changed, 43 insertions(+), 13 deletions(-) diff --git a/src/gdcmHeader.cxx b/src/gdcmHeader.cxx index e59c6657..a3e31d31 100644 --- a/src/gdcmHeader.cxx +++ b/src/gdcmHeader.cxx @@ -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; } diff --git a/src/jpeg/libijg12/jchuff12.c b/src/jpeg/libijg12/jchuff12.c index 1d745c87..7b879a1e 100644 --- a/src/jpeg/libijg12/jchuff12.c +++ b/src/jpeg/libijg12/jchuff12.c @@ -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]; } diff --git a/src/jpeg/libijg12/jcparam12.c b/src/jpeg/libijg12/jcparam12.c index 15f787d8..ba1952ff 100644 --- a/src/jpeg/libijg12/jcparam12.c +++ b/src/jpeg/libijg12/jcparam12.c @@ -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)); diff --git a/src/jpeg/libijg12/jdhuff12.c b/src/jpeg/libijg12/jdhuff12.c index 0a3b012a..b3734b89 100644 --- a/src/jpeg/libijg12/jdhuff12.c +++ b/src/jpeg/libijg12/jdhuff12.c @@ -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; + } } } } diff --git a/src/jpeg/libijg12/jdmarker12.c b/src/jpeg/libijg12/jdmarker12.c index 86f3a0ec..6c6309f4 100644 --- a/src/jpeg/libijg12/jdmarker12.c +++ b/src/jpeg/libijg12/jdmarker12.c @@ -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); -- 2.45.1