From d5e16925a7d3c8dbd0e71b1989629ca0f619b119 Mon Sep 17 00:00:00 2001 From: malaterre Date: Mon, 17 Jan 2005 01:14:32 +0000 Subject: [PATCH] ENH: First pass at simplifying the JPEG stuff. --- src/gdcmJPEGFragment.cxx | 166 +++++++++++++++++++++++++++++++++- src/gdcmJPEGFragment.h | 8 +- src/gdcmJPEGFragmentsInfo.cxx | 6 +- src/gdcmJPEGFragmentsInfo.h | 18 ++-- src/gdcmPixelReadConvert.cxx | 149 ++---------------------------- src/gdcmValEntry.cxx | 26 +++--- 6 files changed, 201 insertions(+), 172 deletions(-) diff --git a/src/gdcmJPEGFragment.cxx b/src/gdcmJPEGFragment.cxx index 0af4944d..03cc0bc0 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/16 04:50:42 $ - Version: $Revision: 1.2 $ + Date: $Date: 2005/01/17 01:14:32 $ + Version: $Revision: 1.3 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -17,10 +17,34 @@ =========================================================================*/ #include "gdcmJPEGFragment.h" - +#include "gdcmDebug.h" + namespace gdcm { +// For JPEG 2000, body in file gdcmJpeg2000.cxx +bool gdcm_read_JPEG2000_file (std::ifstream* fp, void* image_buffer); + +// For JPEG 8 Bits, body in file gdcmJpeg8.cxx +bool gdcm_read_JPEG_file8 (std::ifstream *fp, void *image_buffer); +bool gdcm_read_JPEG_memory8 (const JOCTET *buffer, const size_t buflen, + void *image_buffer, + size_t *howManyRead, size_t *howManyWritten); +// +// For JPEG 12 Bits, body in file gdcmJpeg12.cxx +bool gdcm_read_JPEG_file12 (std::ifstream *fp, void *image_buffer); +bool gdcm_read_JPEG_memory12 (const JOCTET *buffer, const size_t buflen, + void *image_buffer, + size_t *howManyRead, size_t *howManyWritten); + +// For JPEG 16 Bits, body in file gdcmJpeg16.cxx +// Beware this is misleading there is no 16bits DCT algorithm, only +// jpeg lossless compression exist in 16bits. +bool gdcm_read_JPEG_file16 (std::ifstream *fp, void *image_buffer); +bool gdcm_read_JPEG_memory16 (const JOCTET *buffer, const size_t buflen, + void* image_buffer, + size_t *howManyRead, size_t *howManyWritten); + /** * \brief Default constructor. */ @@ -32,8 +56,8 @@ JPEGFragment::JPEGFragment() /** * \brief Print self. - * @param indent Indentation string to be prepended during printing. * @param os Stream to print to. + * @param indent Indentation string to be prepended during printing. */ void JPEGFragment::Print( std::ostream &os, std::string indent ) { @@ -43,5 +67,139 @@ void JPEGFragment::Print( std::ostream &os, std::string indent ) << std::endl; } +/** + * \brief Decompress 8bits JPEG Fragment + * @param buffer output (data decompress) + * @param nBits 8/12 or 16 bits jpeg + */ +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) + { + // Reading Fragment pixels + if ( ! gdcm_read_JPEG_file12 ( fp, buffer) ) + { + //return false; + } + } + else if ( nBits <= 16) + { + // Reading Fragment pixels + if ( ! gdcm_read_JPEG_file16 ( fp, buffer) ) + { + //return false; + } + //gdcmAssertMacro( IsJPEGLossless ); + } + else + { + // other JPEG lossy not supported + gdcmErrorMacro( "Unknown jpeg lossy compression "); + //return false; + } + +} + +void JPEGFragment::DecompressJPEGSingleFrameFragmentsFromFile(JOCTET *buffer, size_t totalLength, uint8_t* raw, int nBits) +{ + size_t howManyRead = 0; + size_t howManyWritten = 0; + + if ( nBits == 8) + { + if ( ! gdcm_read_JPEG_memory8( buffer, totalLength, raw, + &howManyRead, &howManyWritten ) ) + { + gdcmErrorMacro( "Failed to read jpeg8 "); + delete [] buffer; + //return false; + } + } + else if ( nBits <= 12) + { + if ( ! gdcm_read_JPEG_memory12( buffer, totalLength, raw, + &howManyRead, &howManyWritten ) ) + { + gdcmErrorMacro( "Failed to read jpeg12 "); + delete [] buffer; + //return false; + } + } + else if ( nBits <= 16) + { + + if ( ! gdcm_read_JPEG_memory16( buffer, totalLength, raw, + &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; + } + +} + +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 "); + //delete [] buffer; + //return false; + } + +} + } // end namespace gdcm diff --git a/src/gdcmJPEGFragment.h b/src/gdcmJPEGFragment.h index 4047d2b8..8434bdf2 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/16 04:50:42 $ - Version: $Revision: 1.7 $ + Date: $Date: 2005/01/17 01:14:33 $ + Version: $Revision: 1.8 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -26,6 +26,7 @@ namespace gdcm { +#define JOCTET uint8_t /** * \brief Utility class for summerizing the informations of a JPEG @@ -43,6 +44,9 @@ class GDCM_EXPORT JPEGFragment public: JPEGFragment(); void Print( std::ostream &os = std::cout, std::string indent = "" ); + void DecompressJPEGFramesFromFile(std::ifstream *fp, uint8_t *buffer, int nBits); + void DecompressJPEGSingleFrameFragmentsFromFile(JOCTET *buffer, size_t totalLength, uint8_t* raw, int nBits); + void DecompressJPEGFragmentedFramesFromFile(JOCTET *buffer, uint8_t* raw, int nBits, size_t &howManyRead, size_t &howManyWritten, size_t totalLength); private: long Offset; diff --git a/src/gdcmJPEGFragmentsInfo.cxx b/src/gdcmJPEGFragmentsInfo.cxx index 88725b92..ae87b8de 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/16 04:50:42 $ - Version: $Revision: 1.5 $ + Date: $Date: 2005/01/17 01:14:33 $ + Version: $Revision: 1.6 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -60,5 +60,5 @@ void JPEGFragmentsInfo::Print( std::ostream &os, std::string const & indent ) } } - } // end namespace gdcm + diff --git a/src/gdcmJPEGFragmentsInfo.h b/src/gdcmJPEGFragmentsInfo.h index 55ad8f99..9f6a6443 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/16 04:50:42 $ - Version: $Revision: 1.8 $ + Date: $Date: 2005/01/17 01:14:33 $ + 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 @@ -41,17 +41,19 @@ namespace gdcm */ class GDCM_EXPORT JPEGFragmentsInfo { -friend class Document; -friend class File; -friend class PixelReadConvert; -private: - typedef std::list< JPEGFragment* > JPEGFragmentsList; - JPEGFragmentsList Fragments; public: ~JPEGFragmentsInfo(); void Print( std::ostream &os = std::cout, std::string const & indent = "" ); +private: + typedef std::list< JPEGFragment* > JPEGFragmentsList; + JPEGFragmentsList Fragments; + +friend class Document; +friend class File; +friend class PixelReadConvert; }; } // end namespace gdcm //----------------------------------------------------------------------------- #endif + diff --git a/src/gdcmPixelReadConvert.cxx b/src/gdcmPixelReadConvert.cxx index 12655270..b147cf3b 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/16 04:50:42 $ - Version: $Revision: 1.28 $ + Date: $Date: 2005/01/17 01:14:33 $ + Version: $Revision: 1.29 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -38,30 +38,6 @@ namespace gdcm { #define str2num(str, typeNum) *((typeNum *)(str)) -// For JPEG 2000, body in file gdcmJpeg2000.cxx -bool gdcm_read_JPEG2000_file (std::ifstream* fp, void* image_buffer); - -#define JOCTET uint8_t -// For JPEG 8 Bits, body in file gdcmJpeg8.cxx -bool gdcm_read_JPEG_file8 (std::ifstream *fp, void *image_buffer); -bool gdcm_read_JPEG_memory8 (const JOCTET *buffer, const size_t buflen, - void *image_buffer, - size_t *howManyRead, size_t *howManyWritten); -// -// For JPEG 12 Bits, body in file gdcmJpeg12.cxx -bool gdcm_read_JPEG_file12 (std::ifstream *fp, void *image_buffer); -bool gdcm_read_JPEG_memory12 (const JOCTET *buffer, const size_t buflen, - void *image_buffer, - size_t *howManyRead, size_t *howManyWritten); - -// For JPEG 16 Bits, body in file gdcmJpeg16.cxx -// Beware this is misleading there is no 16bits DCT algorithm, only -// jpeg lossless compression exist in 16bits. -bool gdcm_read_JPEG_file16 (std::ifstream *fp, void *image_buffer); -bool gdcm_read_JPEG_memory16 (const JOCTET *buffer, const size_t buflen, - void* image_buffer, - size_t *howManyRead, size_t *howManyWritten); - //----------------------------------------------------------------------------- // Constructor / Destructor @@ -427,37 +403,7 @@ bool PixelReadConvert::ReadAndDecompressJPEGFramesFromFile( std::ifstream *fp ) { 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; - } - //gdcmAssertMacro( IsJPEGLossless ); - } - else - { - // other JPEG lossy not supported - gdcmErrorMacro( "Unknown jpeg lossy compression "); - return false; - } + (*it)->DecompressJPEGFramesFromFile(fp, localRaw, BitsStored ); // Advance to next free location in Raw // for next fragment decompression (if any) @@ -506,47 +452,7 @@ ReadAndDecompressJPEGSingleFrameFragmentsFromFile( std::ifstream *fp ) p += len; } - size_t howManyRead = 0; - size_t howManyWritten = 0; - - if ( BitsStored == 8) - { - if ( ! gdcm_read_JPEG_memory8( buffer, totalLength, Raw, - &howManyRead, &howManyWritten ) ) - { - gdcmErrorMacro( "Failed to read jpeg8 "); - delete [] buffer; - return false; - } - } - else if ( BitsStored <= 12) - { - if ( ! gdcm_read_JPEG_memory12( buffer, totalLength, Raw, - &howManyRead, &howManyWritten ) ) - { - gdcmErrorMacro( "Failed to read jpeg12 "); - delete [] buffer; - return false; - } - } - else if ( BitsStored <= 16) - { - - if ( ! gdcm_read_JPEG_memory16( buffer, totalLength, Raw, - &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; - } + (*it)->DecompressJPEGSingleFrameFragmentsFromFile(buffer, totalLength, Raw, BitsStored); // free local buffer delete [] buffer; @@ -602,49 +508,8 @@ ReadAndDecompressJPEGFragmentedFramesFromFile( std::ifstream *fp ) fragmentLength += (*it)->Length; if (howManyRead > fragmentLength) continue; - - if ( BitsStored == 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 ( BitsStored <= 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 ( BitsStored <= 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; - } + (*it)->DecompressJPEGFragmentedFramesFromFile(buffer, Raw, BitsStored, howManyRead, howManyWritten, totalLength); if (howManyRead < fragmentLength) howManyRead = fragmentLength; @@ -667,8 +532,8 @@ 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 ( ! gdcm_read_JPEG2000_file( fp,Raw ) ) +// return false; } if ( ( ZSize == 1 ) && ( JPEGInfo->Fragments.size() > 1 ) ) diff --git a/src/gdcmValEntry.cxx b/src/gdcmValEntry.cxx index 314368d2..9ef3d2dd 100644 --- a/src/gdcmValEntry.cxx +++ b/src/gdcmValEntry.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmValEntry.cxx,v $ Language: C++ - Date: $Date: 2005/01/16 04:50:42 $ - Version: $Revision: 1.46 $ + Date: $Date: 2005/01/17 01:14:33 $ + Version: $Revision: 1.47 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -72,10 +72,9 @@ void ValEntry::Print(std::ostream &os, std::string const &) { uint16_t g = GetGroup(); uint16_t e = GetElement(); - std::string vr = GetVR(); + VRKey vr = GetVR(); std::ostringstream s; std::string st; - TSKey v; std::string d2; os << "V "; @@ -89,10 +88,11 @@ void ValEntry::Print(std::ostream &os, std::string const &) TS * ts = Global::GetTS(); - v = GetValue(); // not applicable for SQ ... + TSAtr v = GetValue(); // not applicable for SQ ... d2 = Util::CreateCleanString(v); // replace non printable characters by '.' - if( (GetLength()<=MAX_SIZE_PRINT_ELEMENT_VALUE) || - (PrintLevel>=3) || (d2.find(GDCM_NOTLOADED) < d2.length()) ) + if( GetLength() <= MAX_SIZE_PRINT_ELEMENT_VALUE + || PrintLevel >= 3 + || d2.find(GDCM_NOTLOADED) < d2.length() ) { s << " [" << d2 << "]"; } @@ -106,7 +106,7 @@ void ValEntry::Print(std::ostream &os, std::string const &) if (g == 0x0002) { // Any more to be displayed ? - if ( (e == 0x0010) || (e == 0x0002) ) + if ( e == 0x0010 || e == 0x0002 ) { if ( v.length() != 0 ) // for brain damaged headers { @@ -138,7 +138,7 @@ void ValEntry::Print(std::ostream &os, std::string const &) { if (g == 0x0004) { - if ( (e == 0x1510) || (e == 0x1512) ) + if ( e == 0x1510 || e == 0x1512 ) { if ( v.length() != 0 ) // for brain damaged headers { @@ -153,7 +153,7 @@ void ValEntry::Print(std::ostream &os, std::string const &) } } //if (e == 0x0000) { // elem 0x0000 --> group length - if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") ) + if ( vr == "UL" || vr == "US" || vr == "SL" || vr == "SS" ) { if (v == "4294967295") // to avoid troubles in convertion { @@ -161,7 +161,7 @@ void ValEntry::Print(std::ostream &os, std::string const &) } else { - if ( GetLength() !=0 ) + if ( GetLength() != 0 ) { st = Util::Format(" x(%x)", atoi(v.c_str()));//FIXME } @@ -183,7 +183,7 @@ void ValEntry::SetValue(std::string const &val) int l = val.length(); if ( l != 0) // To avoid to be cheated by 'zero length' integers { - VRKey vr = GetVR(); + const VRKey &vr = GetVR(); if( vr == "US" || vr == "SS" ) { // for multivaluated items @@ -229,7 +229,7 @@ void ValEntry::WriteContent(std::ofstream *fp, FileType filetype) return; //delimitors have NO value } - std::string vr = GetVR(); + const VRKey &vr = GetVR(); unsigned int lgr = GetLength(); //std::cout<