-
-/**
- * \brief Reads from disk the Pixel Data of JPEG Dicom encapsulated
- * file and decompress it. This funciton assumes that each
- * jpeg fragment contains a whole frame (jpeg file).
- * @param fp File Pointer
- * @return Boolean
- */
-bool PixelReadConvert::ReadAndDecompressJPEGFramesFromFile( std::ifstream* fp )
-{
- uint8_t* localRaw = Raw;
- // Loop on the fragment[s]
- for( JPEGFragmentsInfo::JPEGFragmentsList::iterator
- it = JPEGInfo->Fragments.begin();
- it != JPEGInfo->Fragments.end();
- ++it )
- {
- fp->seekg( (*it)->Offset, std::ios::beg);
-
- if ( BitsStored == 8)
- {
- // JPEG Lossy : call to IJG 6b
- if ( ! gdcm_read_JPEG_file8( fp, localRaw ) )
- {
- return false;
- }
- }
- else if ( BitsStored <= 12)
- {
- // Reading Fragment pixels
- if ( ! gdcm_read_JPEG_file12 ( fp, localRaw ) )
- {
- return false;
- }
- }
- else if ( BitsStored <= 16)
- {
- // Reading Fragment pixels
- if ( ! gdcm_read_JPEG_file16 ( fp, localRaw ) )
- {
- return false;
- }
- //assert( IsJPEGLossless );
- }
- else
- {
- // other JPEG lossy not supported
- dbg.Error("PixelReadConvert::ReadAndDecompressJPEGFile: unknown "
- "jpeg lossy compression ");
- return false;
- }
-
- // Advance to next free location in Raw
- // for next fragment decompression (if any)
- int length = XSize * YSize * SamplesPerPixel;
- int numberBytes = BitsAllocated / 8;
-
- localRaw += length * numberBytes;
- }
- return true;
-}
-
-/**
- * \brief Reads from disk the Pixel Data of JPEG Dicom encapsulated
- * file and decompress it. This function assumes that the dicom
- * image is a single frame split into several JPEG fragments.
- * Those fragments will be glued together into a memory buffer
- * before being read.
- * @param fp File Pointer
- * @return Boolean
- */
-bool PixelReadConvert::
-ReadAndDecompressJPEGSingleFrameFragmentsFromFile( std::ifstream* fp )
-{
- // Loop on the fragment[s] to get total length
- size_t totalLength = 0;
- JPEGFragmentsInfo::JPEGFragmentsList::iterator it;
- for( it = JPEGInfo->Fragments.begin();
- it != JPEGInfo->Fragments.end();
- ++it )
- {
- totalLength += (*it)->Length;
- }
-
- // Concatenate the jpeg fragments into a local buffer
- JOCTET *buffer = new JOCTET [totalLength];
- JOCTET *p = buffer;
-
- // Loop on the fragment[s]
- for( it = JPEGInfo->Fragments.begin();
- it != JPEGInfo->Fragments.end();
- ++it )
- {
- fp->seekg( (*it)->Offset, std::ios::beg);
- size_t len = (*it)->Length;
- fp->read((char *)p,len);
- p += len;
- }
-
- size_t howManyRead = 0;
- size_t howManyWritten = 0;
-
- if ( BitsStored == 8)
- {
- if ( ! gdcm_read_JPEG_memory8( buffer, totalLength, Raw,
- &howManyRead, &howManyWritten ) )
- {
- dbg.Error(
- "PixelConvert::ReadAndDecompressJPEGFile: failed to read jpeg8 "
- );
- delete [] buffer;
- return false;
- }
- }
- else if ( BitsStored <= 12)
- {
- if ( ! gdcm_read_JPEG_memory12( buffer, totalLength, Raw,
- &howManyRead, &howManyWritten ) )
- {
- dbg.Error(
- "PixelConvert::ReadAndDecompressJPEGFile: failed to read jpeg12 "
- );
- delete [] buffer;
- return false;
- }
- }
- else if ( BitsStored <= 16)
- {
-
- if ( ! gdcm_read_JPEG_memory16( buffer, totalLength, Raw,
- &howManyRead, &howManyWritten ) )
- {
- dbg.Error(
- "PixelConvert::ReadAndDecompressJPEGFile: failed to read jpeg16 "
- );
- delete [] buffer;
- return false;
- }
- }
- else
- {
- // other JPEG lossy not supported
- dbg.Error("PixelConvert::ReadAndDecompressJPEGFile: unknown "
- "jpeg lossy compression ");
- delete [] buffer;
- return false;
- }
-
- // free local buffer
- delete [] buffer;
-
- return true;
-}
-
-/**
- * \brief Reads from disk the Pixel Data of JPEG Dicom encapsulated
- * file and decompress it. This function handles the generic
- * and complex case where the DICOM contains several frames,
- * and some of the frames are possibly split into several JPEG
- * fragments.
- * @param fp File Pointer
- * @return Boolean
- */
-bool PixelReadConvert::
-ReadAndDecompressJPEGFragmentedFramesFromFile( std::ifstream* fp )
-{
- // Loop on the fragment[s] to get total length
- size_t totalLength = 0;
- JPEGFragmentsInfo::JPEGFragmentsList::iterator it;
- for( it = JPEGInfo->Fragments.begin();
- it != JPEGInfo->Fragments.end();
- ++it )
- {
- totalLength += (*it)->Length;
- }
-
- // Concatenate the jpeg fragments into a local buffer
- JOCTET *buffer = new JOCTET [totalLength];
- JOCTET *p = buffer;
-
- // Loop on the fragment[s]
- for( it = JPEGInfo->Fragments.begin();
- it != JPEGInfo->Fragments.end();
- ++it )
- {
- fp->seekg( (*it)->Offset, std::ios::beg);
- size_t len = (*it)->Length;
- fp->read((char *)p,len);
- p+=len;
- }
-
- size_t howManyRead = 0;
- size_t howManyWritten = 0;
- size_t fragmentLength = 0;
-
- for( it = JPEGInfo->Fragments.begin() ;
- (it != JPEGInfo->Fragments.end()) && (howManyRead < totalLength);
- ++it )
- {
- fragmentLength += (*it)->Length;
-
- if (howManyRead > fragmentLength) continue;
-
- if ( BitsStored == 8)
- {
- if ( ! gdcm_read_JPEG_memory8( buffer+howManyRead, totalLength-howManyRead,
- Raw+howManyWritten,
- &howManyRead, &howManyWritten ) )
- {
- dbg.Error("PixelConvert::ReadAndDecompressJPEGFile: failed to read jpeg8 ");
- delete [] buffer;
- return false;
- }
- }
- else if ( BitsStored <= 12)
- {
-
- if ( ! gdcm_read_JPEG_memory12( buffer+howManyRead, totalLength-howManyRead,
- Raw+howManyWritten,
- &howManyRead, &howManyWritten ) )
- {
- dbg.Error("PixelConvert::ReadAndDecompressJPEGFile: failed to read jpeg12 ");
- delete [] buffer;
- return false;
- }
- }
- else if ( BitsStored <= 16)
- {
-
- if ( ! gdcm_read_JPEG_memory16( buffer+howManyRead, totalLength-howManyRead,
- Raw+howManyWritten,
- &howManyRead, &howManyWritten ) )
- {
- dbg.Error("PixelConvert::ReadAndDecompressJPEGFile: failed to read jpeg16 ");
- delete [] buffer;
- return false;
- }
- }
- else
- {
- // other JPEG lossy not supported
- dbg.Error("PixelConvert::ReadAndDecompressJPEGFile: unknown "
- "jpeg lossy compression ");
- delete [] buffer;
- return false;
- }
-
- if (howManyRead < fragmentLength)
- howManyRead = fragmentLength;
- }
-
- // free local buffer
- delete [] buffer;
-
- return true;
-}
-
-/**
- * \brief Reads from disk the Pixel Data of JPEG Dicom encapsulated
- * file and decompress it.
- * @param fp File Pointer
- * @return Boolean
- */
-bool PixelReadConvert::ReadAndDecompressJPEGFile( std::ifstream* fp )
-{
- if ( IsJPEG2000 )
- {
- fp->seekg( (*JPEGInfo->Fragments.begin())->Offset, std::ios::beg);
- if ( ! gdcm_read_JPEG2000_file( fp,Raw ) )
- return false;
- }
-
- if ( ( ZSize == 1 ) && ( JPEGInfo->Fragments.size() > 1 ) )
- {
- // we have one frame split into several fragments
- // we will pack those fragments into a single buffer and
- // read from it
- return ReadAndDecompressJPEGSingleFrameFragmentsFromFile( fp );
- }
- else if (JPEGInfo->Fragments.size() == (size_t)ZSize)
- {
- // suppose each fragment is a frame
- return ReadAndDecompressJPEGFramesFromFile( fp );
- }
- else
- {
- // The dicom image contains frames containing fragments of images
- // a more complex algorithm :-)
- return ReadAndDecompressJPEGFragmentedFramesFromFile( fp );
- }
-}
-