- FILE* fp = Header->OpenFile();
-
- if ( !fp )
- {
- return false;
- }
- if ( fseek(fp, Header->GetPixelOffset(), SEEK_SET) == -1 )
- {
- Header->CloseFile();
- return false;
- }
-
- if ( Header->GetBitsAllocated() == 12 )
- {
- ConvertDecompress12BitsTo16Bits( (uint8_t*)destination,
- Header->GetXSize(),
- Header->GetYSize(),
- fp);
- Header->CloseFile();
- return true;
- }
-
- // ---------------------- Uncompressed File
- if ( !Header->IsDicomV3() ||
- Header->IsImplicitVRLittleEndianTransferSyntax() ||
- Header->IsExplicitVRLittleEndianTransferSyntax() ||
- Header->IsExplicitVRBigEndianTransferSyntax() ||
- Header->IsDeflatedExplicitVRLittleEndianTransferSyntax() )
- {
- size_t ItemRead = fread(destination, Header->GetPixelAreaLength(), 1, fp);
- Header->CloseFile();
- if ( ItemRead != 1 )
- {
- return false;
- }
- else
- {
- return true;
- }
- }
-
- // ---------------------- Run Length Encoding
- if ( Header->IsRLELossLessTransferSyntax() )
- {
- bool res = gdcmPixelConvert::gdcm_read_RLE_file ( destination,
- Header->GetXSize(),
- Header->GetYSize(),
- Header->GetZSize(),
- Header->GetBitsAllocated(),
- &(Header->RLEInfo),
- fp );
- Header->CloseFile();
- return res;
- }
-
- // --------------- SingleFrame/Multiframe JPEG Lossless/Lossy/2000
- int numberBitsAllocated = Header->GetBitsAllocated();
- if ( ( numberBitsAllocated == 0 ) || ( numberBitsAllocated == 12 ) )
- {
- numberBitsAllocated = 16;
- }
-
- int nBytes= numberBitsAllocated/8;
- int taille = Header->GetXSize() * Header->GetYSize()
- * Header->GetSamplesPerPixel();
- long fragmentBegining; // for ftell, fseek
-
- bool jpg2000 = Header->IsJPEG2000();
- bool jpgLossless = Header->IsJPEGLossless();
-
- bool res = true;
- uint16_t ItemTagGr, ItemTagEl;
- int ln;
-
- // Position on begining of Jpeg Pixels
-
- fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
- fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
- if(Header->GetSwapCode())
- {
- ItemTagGr = Header->SwapShort(ItemTagGr);
- ItemTagEl = Header->SwapShort(ItemTagEl);
- }
-
- fread(&ln,4,1,fp);
- if( Header->GetSwapCode() )
- {
- ln = Header->SwapLong( ln ); // Basic Offset Table Item length
- }
-
- if ( ln != 0 )
- {
- // What is it used for ?!?
- uint8_t* BasicOffsetTableItemValue = new uint8_t[ln+1];
- fread(BasicOffsetTableItemValue,ln,1,fp);
- //delete[] BasicOffsetTableItemValue;
- }
-
- // first Fragment initialisation
- fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
- fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
- if( Header->GetSwapCode() )
- {
- ItemTagGr = Header->SwapShort( ItemTagGr );
- ItemTagEl = Header->SwapShort( ItemTagEl );
- }
-
- // parsing fragments until Sequence Delim. Tag found
- while ( ItemTagGr == 0xfffe && ItemTagEl != 0xe0dd )
- {
- // --- for each Fragment
- fread(&ln,4,1,fp);
- if( Header->GetSwapCode() )
- {
- ln = Header->SwapLong(ln); // Fragment Item length
- }
- fragmentBegining = ftell( fp );
-
- if ( jpg2000 )
- {
- // JPEG 2000 : call to ???
- res = gdcm_read_JPEG2000_file (fp,destination); // Not Yet written
- // ------------------------------------- endif (JPEG2000)
- }
- else if (jpgLossless)
- {
- // JPEG LossLess : call to xmedcom Lossless JPEG
- // Reading Fragment pixels
- JPEGLosslessDecodeImage (fp, (uint16_t*)destination,
- Header->GetPixelSize() * 8 * Header->GetSamplesPerPixel(), ln);
- res = 1; // in order not to break the loop
-
- } // ------------------------------------- endif (JPEGLossless)
- else
- {
- // JPEG Lossy : call to IJG 6b
- if ( Header->GetBitsStored() == 8)
- {
- // Reading Fragment pixels
- res = gdcm_read_JPEG_file (fp,destination);
- }
- else
- {
- // Reading Fragment pixels
- res = gdcm_read_JPEG_file12 (fp,destination);
- }
- // ------------------------------------- endif (JPEGLossy)
- }
-
- if ( !res )
- {
- break;
- }
-
- // location in user's memory
- // for next fragment (if any)
- destination = (uint8_t*)destination + taille * nBytes;
-
- fseek(fp,fragmentBegining, SEEK_SET); // To be sure we start
- fseek(fp,ln,SEEK_CUR); // at the begining of next fragment
-
- ItemTagGr = ItemTagEl = 0;
- fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
- fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
- if( Header->GetSwapCode() )
- {
- ItemTagGr = Header->SwapShort( ItemTagGr );
- ItemTagEl = Header->SwapShort( ItemTagEl );
- }
- }
- // endWhile parsing fragments until Sequence Delim. Tag found
-
- Header->CloseFile();
- return res;