]> Creatis software - gdcm.git/commitdiff
ENH: First pass at simplifying the JPEG stuff.
authormalaterre <malaterre>
Mon, 17 Jan 2005 01:14:32 +0000 (01:14 +0000)
committermalaterre <malaterre>
Mon, 17 Jan 2005 01:14:32 +0000 (01:14 +0000)
src/gdcmJPEGFragment.cxx
src/gdcmJPEGFragment.h
src/gdcmJPEGFragmentsInfo.cxx
src/gdcmJPEGFragmentsInfo.h
src/gdcmPixelReadConvert.cxx
src/gdcmValEntry.cxx

index 0af4944ddecbbc1fe21fd47b4fdf4facb2065f72..03cc0bc03f5e7979f62d50a60b90196d3fa5f203 100644 (file)
@@ -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
 =========================================================================*/
                                                                                 
 #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
 
index 4047d2b8520b1ad7de8d7648917daaf3e9174d24..8434bdf2f8097b520656c1aced0cad2bfada8a12 100644 (file)
@@ -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;
index 88725b92aa4553fdafc1272bca55ef38ce1ec885..ae87b8de6ff7d117a41f89ec3313430573b8190b 100644 (file)
@@ -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
+
index 55ad8f999167cc517488010b66d15d241bc525f0..9f6a6443d0b4da8ffac4456d6c7f65d6c958396d 100644 (file)
@@ -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
+
index 126552704b313add803ff445b0db9539599174f3..b147cf3be6dbc30ac16ffa820d8afa5cd2761b26 100644 (file)
@@ -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 ) )
index 314368d26816a07781e2a4b5fbdf63b6185b1471..9ef3d2ddeee5f81ba9be59674243d7473e8ee6b0 100644 (file)
@@ -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<<std::hex<<GetGroup()<<"|"<<GetElement()<<std::dec<<" : "<<GetReadLength()<<" / "<<GetLength()<<"\n";
    if (vr == "US" || vr == "SS")