X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=src%2FgdcmFile.cxx;h=1c0db8127a5b61e298c83688d6385a9a2338cda7;hb=77cbc8232f9308aafb17bde18ef9549be212b741;hp=858dec827fc82bb2f15deedf5c097d97a6c88f95;hpb=8b6c2d1b6d9ff41d0b11bced79cf49f3757e8b5c;p=gdcm.git diff --git a/src/gdcmFile.cxx b/src/gdcmFile.cxx index 858dec82..1c0db812 100644 --- a/src/gdcmFile.cxx +++ b/src/gdcmFile.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmFile.cxx,v $ Language: C++ - Date: $Date: 2005/09/21 16:53:59 $ - Version: $Revision: 1.270 $ + Date: $Date: 2005/10/10 22:25:05 $ + Version: $Revision: 1.274 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -118,6 +118,7 @@ File::File(): JPEGInfo = new JPEGFragmentsInfo; GrPixel = 0x7fe0; // to avoid further troubles NumPixel = 0x0010; + BasicOffsetTableItemValue = 0; } @@ -130,6 +131,7 @@ File::~File () delete RLEInfo; if ( JPEGInfo ) delete JPEGInfo; + delete[] BasicOffsetTableItemValue; } //----------------------------------------------------------------------------- @@ -758,7 +760,7 @@ float File::GetZOrigin() * (needed to organize DICOM files based on their x,y,z position) * @param iop adress of the (6)float array to receive values * @return true when one of the tag is found - * false when nothong is found + * false when nothing is found */ bool File::GetImageOrientationPatient( float iop[6] ) { @@ -892,6 +894,7 @@ int File::GetPixelSize() // (in order no to be messed up by old ACR-NEMA RGB images) // if (File::GetEntryValue(0x0028,0x0100) == "24") // return 3; + assert( !(GetEntryValue(0x0028,0x0100) == "24") ); std::string pixelType = GetPixelType(); if ( pixelType == "8U" || pixelType == "8S" ) @@ -1554,7 +1557,7 @@ void File::ComputeRLEInfo() // - the first item in the sequence of items before the encoded pixel // data stream shall be basic offset table item. The basic offset table // item value, however, is not required to be present" - ReadAndSkipEncapsulatedBasicOffsetTable(); + ReadEncapsulatedBasicOffsetTable(); // Encapsulated RLE Compressed Images (see PS 3.5-2003, Annex G) // Loop on the individual frame[s] and store the information @@ -1564,8 +1567,24 @@ void File::ComputeRLEInfo() // - when more than one frame are present, then we are in // the case of a multi-frame image. long frameLength; + int i=0; + uint32_t sum = 0; while ( (frameLength = ReadTagLength(0xfffe, 0xe000)) != 0 ) { + // Since we have read the basic offset table, let's check the value were correct + // or else produce a warning: + if ( BasicOffsetTableItemValue ) + { + // If a BasicOffsetTableItemValue was read + uint32_t individualLength = BasicOffsetTableItemValue[i]; + assert( individualLength == sum ); // REMOVE that if this is a problem + if( individualLength != sum ) + { + gdcmWarningMacro( "BasicOffsetTableItemValue differs from the fragment lenght" ); + } + sum += frameLength + 8; + i++; + } // Parse the RLE Header and store the corresponding RLE Segment // Offset Table information on fragments of this current Frame. // Note that the fragment pixels themselves are not loaded @@ -1638,22 +1657,46 @@ void File::ComputeJPEGFragmentInfo() return; } - ReadAndSkipEncapsulatedBasicOffsetTable(); + ReadEncapsulatedBasicOffsetTable(); // Loop on the fragments[s] and store the parsed information in a // JPEGInfo. long fragmentLength; + int i=0; + uint32_t sum = 0; while ( (fragmentLength = ReadTagLength(0xfffe, 0xe000)) != 0 ) { - long fragmentOffset = Fp->tellg(); + // Since we have read the basic offset table, let's check the value were correct + // or else produce a warning: + // A.4 Transfer syntaxes for encapsulation of encoded pixel data: + // When the Item Value is present, the Basic Offset Table Item Value shall contain + // concatenated 32-bit unsigned integer values that are byte offsets to the first + // byte of the Item Tag of the first fragment for each frame in the Sequence of + // Items. These offsets are measured from the first byte of the first Item Tag + // following the Basic Offset Table item (See Table A.4-2). + + if ( BasicOffsetTableItemValue ) + { + // If a BasicOffsetTableItemValue was read + uint32_t individualLength = BasicOffsetTableItemValue[i]; + //assert( individualLength == sum ); // Seems like 00191113.dcm is off by one ?? + if( individualLength != sum ) + { + gdcmWarningMacro( "BasicOffsetTableItemValue differs from the fragment lenght:" << + individualLength << " != " << sum ); + } + sum += fragmentLength + 8; + i++; + } - // Store the collected info - JPEGFragment *newFragment = new JPEGFragment; - newFragment->SetOffset(fragmentOffset); - newFragment->SetLength(fragmentLength); - JPEGInfo->AddFragment(newFragment); + long fragmentOffset = Fp->tellg(); + // Store the collected info + JPEGFragment *newFragment = new JPEGFragment; + newFragment->SetOffset(fragmentOffset); + newFragment->SetLength(fragmentLength); + JPEGInfo->AddFragment(newFragment); - SkipBytes(fragmentLength); + SkipBytes(fragmentLength); } // Make sure that we encounter a 'Sequence Delimiter Item' @@ -1749,7 +1792,7 @@ uint32_t File::ReadTagLength(uint16_t testGroup, uint16_t testElem) * \brief When parsing the Pixel Data of an encapsulated file, read * the basic offset table (when present, and BTW dump it). */ -void File::ReadAndSkipEncapsulatedBasicOffsetTable() +void File::ReadEncapsulatedBasicOffsetTable() { //// Read the Basic Offset Table Item Tag length... uint32_t itemLength = ReadTagLength(0xfffe, 0xe000); @@ -1762,20 +1805,26 @@ void File::ReadAndSkipEncapsulatedBasicOffsetTable() // lengths, but we won't bother with such fuses for the time being. if ( itemLength != 0 ) { - char *basicOffsetTableItemValue = new char[itemLength + 1]; - Fp->read(basicOffsetTableItemValue, itemLength); + char *charBasicOffsetTableItemValue = new char[itemLength]; + Fp->read(charBasicOffsetTableItemValue, itemLength); + unsigned int nbEntries = itemLength/4; + assert( nbEntries*4 == itemLength); // Make sure this is a multiple + BasicOffsetTableItemValue = new uint32_t[nbEntries]; -#ifdef GDCM_DEBUG - for (unsigned int i=0; i < itemLength; i += 4 ) + for (unsigned int i=0; i < nbEntries; i++ ) { - uint32_t individualLength = str2num( &basicOffsetTableItemValue[i], - uint32_t); - gdcmWarningMacro( "Read one length: " << - std::hex << individualLength ); + BasicOffsetTableItemValue[i] = *((uint32_t*)(&charBasicOffsetTableItemValue[4*i])); +#if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION) + uint32_t val = BasicOffsetTableItemValue[i]; + BasicOffsetTableItemValue[i] + = ( (val<<24) | ((val<<8) & 0x00ff0000) | + ((val>>8) & 0x0000ff00) | (val>>24) ); +#endif + gdcmWarningMacro( "Read one length for: " << + std::hex << BasicOffsetTableItemValue[i] ); } -#endif //GDCM_DEBUG - delete[] basicOffsetTableItemValue; + delete[] charBasicOffsetTableItemValue; } }