From: malaterre Date: Mon, 17 Jan 2005 03:05:55 +0000 (+0000) Subject: ENH: Minor patch that rework code to avoid duplicate code. X-Git-Tag: Version1.0.bp~303 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=fefb49a4c17b2c2134cf23edf1ecf230c5e309d1;p=gdcm.git ENH: Minor patch that rework code to avoid duplicate code. --- diff --git a/src/gdcmJPEGFragment.cxx b/src/gdcmJPEGFragment.cxx index 03cc0bc0..188f8226 100644 --- a/src/gdcmJPEGFragment.cxx +++ b/src/gdcmJPEGFragment.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmJPEGFragment.cxx,v $ Language: C++ - Date: $Date: 2005/01/17 01:14:32 $ - Version: $Revision: 1.3 $ + Date: $Date: 2005/01/17 03:05:55 $ + Version: $Revision: 1.4 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -74,37 +74,40 @@ void JPEGFragment::Print( std::ostream &os, std::string indent ) */ void JPEGFragment::DecompressJPEGFramesFromFile(std::ifstream *fp, uint8_t *buffer, int nBits) { - if ( nBits == 8) - { - // JPEG Lossy : call to IJG 6b - if ( ! gdcm_read_JPEG_file8( fp, buffer) ) - { - //return false; - } - } - else if ( nBits <= 12) + // First thing need to reset file to proper position: + fp->seekg( Offset, std::ios::beg); + + if ( nBits == 8 ) + { + // JPEG Lossy : call to IJG 6b + if ( ! gdcm_read_JPEG_file8( fp, buffer) ) { - // Reading Fragment pixels - if ( ! gdcm_read_JPEG_file12 ( fp, buffer) ) - { - //return false; - } + //return false; } - else if ( nBits <= 16) + } + else if ( nBits <= 12 ) + { + // Reading Fragment pixels + if ( ! gdcm_read_JPEG_file12 ( fp, buffer) ) { - // Reading Fragment pixels - if ( ! gdcm_read_JPEG_file16 ( fp, buffer) ) - { - //return false; - } - //gdcmAssertMacro( IsJPEGLossless ); + //return false; } - else + } + else if ( nBits <= 16 ) + { + // Reading Fragment pixels + if ( ! gdcm_read_JPEG_file16 ( fp, buffer) ) { - // other JPEG lossy not supported - gdcmErrorMacro( "Unknown jpeg lossy compression "); //return false; } + //gdcmAssertMacro( IsJPEGLossless ); + } + else + { + // other JPEG lossy not supported + gdcmErrorMacro( "Unknown jpeg lossy compression "); + //return false; + } } @@ -156,49 +159,48 @@ void JPEGFragment::DecompressJPEGSingleFrameFragmentsFromFile(JOCTET *buffer, si void JPEGFragment::DecompressJPEGFragmentedFramesFromFile(JOCTET *buffer, uint8_t* raw, int nBits, size_t &howManyRead, size_t &howManyWritten, size_t totalLength) { - if ( nBits == 8) - { - if ( ! gdcm_read_JPEG_memory8( buffer+howManyRead, totalLength-howManyRead, - raw+howManyWritten, - &howManyRead, &howManyWritten ) ) - { - gdcmErrorMacro( "Failed to read jpeg8"); - //delete [] buffer; - //return false; - } - } - else if ( nBits <= 12) - { - - if ( ! gdcm_read_JPEG_memory12( buffer+howManyRead, totalLength-howManyRead, - raw+howManyWritten, - &howManyRead, &howManyWritten ) ) - { - gdcmErrorMacro( "Failed to read jpeg12"); - //delete [] buffer; - //return false; - } - } - else if ( nBits <= 16) - { - - if ( ! gdcm_read_JPEG_memory16( buffer+howManyRead, totalLength-howManyRead, - raw+howManyWritten, - &howManyRead, &howManyWritten ) ) - { - gdcmErrorMacro( "Failed to read jpeg16 "); - //delete [] buffer; - //return false; - } - } - else - { - // other JPEG lossy not supported - gdcmErrorMacro( "Unsupported jpeg lossy compression "); + if ( nBits == 8 ) + { + if ( ! gdcm_read_JPEG_memory8( buffer+howManyRead, totalLength-howManyRead, + raw+howManyWritten, + &howManyRead, &howManyWritten ) ) + { + gdcmErrorMacro( "Failed to read jpeg8"); + //delete [] buffer; + //return false; + } + } + else if ( nBits <= 12 ) + { + + if ( ! gdcm_read_JPEG_memory12( buffer+howManyRead, totalLength-howManyRead, + raw+howManyWritten, + &howManyRead, &howManyWritten ) ) + { + gdcmErrorMacro( "Failed to read jpeg12"); //delete [] buffer; //return false; } - + } + else if ( nBits <= 16 ) + { + + if ( ! gdcm_read_JPEG_memory16( buffer+howManyRead, totalLength-howManyRead, + raw+howManyWritten, + &howManyRead, &howManyWritten ) ) + { + gdcmErrorMacro( "Failed to read jpeg16 "); + //delete [] buffer; + //return false; + } + } + else + { + // other JPEG lossy not supported + gdcmErrorMacro( "Unsupported jpeg lossy compression "); + //delete [] buffer; + //return false; + } } } // end namespace gdcm diff --git a/src/gdcmJPEGFragment.h b/src/gdcmJPEGFragment.h index 8434bdf2..02b16138 100644 --- a/src/gdcmJPEGFragment.h +++ b/src/gdcmJPEGFragment.h @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmJPEGFragment.h,v $ Language: C++ - Date: $Date: 2005/01/17 01:14:33 $ - Version: $Revision: 1.8 $ + Date: $Date: 2005/01/17 03:05:55 $ + Version: $Revision: 1.9 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -55,6 +55,7 @@ private: friend class Document; friend class File; friend class PixelReadConvert; +friend class JPEGFragmentsInfo; }; } // end namespace gdcm diff --git a/src/gdcmJPEGFragmentsInfo.cxx b/src/gdcmJPEGFragmentsInfo.cxx index ae87b8de..3a666cb2 100644 --- a/src/gdcmJPEGFragmentsInfo.cxx +++ b/src/gdcmJPEGFragmentsInfo.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmJPEGFragmentsInfo.cxx,v $ Language: C++ - Date: $Date: 2005/01/17 01:14:33 $ - Version: $Revision: 1.6 $ + Date: $Date: 2005/01/17 03:05:55 $ + Version: $Revision: 1.7 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -17,6 +17,7 @@ =========================================================================*/ #include "gdcmJPEGFragmentsInfo.h" +#include namespace gdcm { @@ -60,5 +61,44 @@ void JPEGFragmentsInfo::Print( std::ostream &os, std::string const & indent ) } } +/** + * \brief Calculate sum of all fragments lenght and return total + * @return Total size of JPEG fragments length + */ +size_t JPEGFragmentsInfo::GetFragmentsLength() +{ + // Loop on the fragment[s] to get total length + size_t totalLength = 0; + JPEGFragmentsList::const_iterator it; + for( it = Fragments.begin(); + it != Fragments.end(); + ++it ) + { + totalLength += (*it)->Length; + } + return totalLength; +} + +/** + * \brief Read the all the JPEG Fragment into the input buffer + */ +void JPEGFragmentsInfo::ReadAllFragments(std::ifstream *fp, JOCTET *buffer ) +{ + JOCTET *p = buffer; + + // Loop on the fragment[s] + JPEGFragmentsList::const_iterator it; + for( it = Fragments.begin(); + it != Fragments.end(); + ++it ) + { + fp->seekg( (*it)->Offset, std::ios::beg); + size_t len = (*it)->Length; + fp->read((char *)p,len); + p += len; + } + +} + } // end namespace gdcm diff --git a/src/gdcmJPEGFragmentsInfo.h b/src/gdcmJPEGFragmentsInfo.h index 9f6a6443..399d8130 100644 --- a/src/gdcmJPEGFragmentsInfo.h +++ b/src/gdcmJPEGFragmentsInfo.h @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmJPEGFragmentsInfo.h,v $ Language: C++ - Date: $Date: 2005/01/17 01:14:33 $ - Version: $Revision: 1.9 $ + Date: $Date: 2005/01/17 03:05:55 $ + Version: $Revision: 1.10 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -44,6 +44,9 @@ class GDCM_EXPORT JPEGFragmentsInfo public: ~JPEGFragmentsInfo(); void Print( std::ostream &os = std::cout, std::string const & indent = "" ); + size_t GetFragmentsLength(); + void ReadAllFragments(std::ifstream *fp, JOCTET *buffer ); + private: typedef std::list< JPEGFragment* > JPEGFragmentsList; JPEGFragmentsList Fragments; diff --git a/src/gdcmPixelReadConvert.cxx b/src/gdcmPixelReadConvert.cxx index b147cf3b..8048fbab 100644 --- a/src/gdcmPixelReadConvert.cxx +++ b/src/gdcmPixelReadConvert.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmPixelReadConvert.cxx,v $ Language: C++ - Date: $Date: 2005/01/17 01:14:33 $ - Version: $Revision: 1.29 $ + Date: $Date: 2005/01/17 03:05:55 $ + Version: $Revision: 1.30 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -367,9 +367,9 @@ void PixelReadConvert::ConvertReorderEndianity() } // Special kludge in order to deal with xmedcon broken images: - if ( ( BitsAllocated == 16 ) - && ( BitsStored < BitsAllocated ) - && ( ! PixelSign ) ) + if ( BitsAllocated == 16 + && BitsStored < BitsAllocated + && !PixelSign ) { int l = (int)( RawSize / ( BitsAllocated / 8 ) ); uint16_t *deb = (uint16_t *)Raw; @@ -394,21 +394,23 @@ void PixelReadConvert::ConvertReorderEndianity() */ bool PixelReadConvert::ReadAndDecompressJPEGFramesFromFile( std::ifstream *fp ) { + // Pointer to the Raw image uint8_t *localRaw = Raw; + + // Precompute the offset localRaw will be shifted with + int length = XSize * YSize * SamplesPerPixel; + int numberBytes = BitsAllocated / 8; + // 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); - (*it)->DecompressJPEGFramesFromFile(fp, localRaw, BitsStored ); // 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; } @@ -428,30 +430,15 @@ 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; - } + size_t totalLength = JPEGInfo->GetFragmentsLength(); // 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; - } + // Fill in the buffer: + JPEGInfo->ReadAllFragments(fp, buffer); + // kludge: // FIXME + JPEGFragmentsInfo::JPEGFragmentsList::const_iterator it = JPEGInfo->Fragments.begin(); (*it)->DecompressJPEGSingleFrameFragmentsFromFile(buffer, totalLength, Raw, BitsStored); // free local buffer @@ -473,34 +460,18 @@ 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; - } + size_t totalLength = JPEGInfo->GetFragmentsLength(); // 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; - } + // Fill in the buffer: + JPEGInfo->ReadAllFragments(fp, buffer); size_t howManyRead = 0; size_t howManyWritten = 0; size_t fragmentLength = 0; + JPEGFragmentsInfo::JPEGFragmentsList::const_iterator it; for( it = JPEGInfo->Fragments.begin() ; (it != JPEGInfo->Fragments.end()) && (howManyRead < totalLength); ++it ) @@ -533,7 +504,7 @@ bool PixelReadConvert::ReadAndDecompressJPEGFile( std::ifstream *fp ) { fp->seekg( (*JPEGInfo->Fragments.begin())->Offset, std::ios::beg); // if ( ! gdcm_read_JPEG2000_file( fp,Raw ) ) -// return false; + return false; } if ( ( ZSize == 1 ) && ( JPEGInfo->Fragments.size() > 1 ) )