]> Creatis software - gdcm.git/commitdiff
ENH: Minor patch that rework code to avoid duplicate code.
authormalaterre <malaterre>
Mon, 17 Jan 2005 03:05:55 +0000 (03:05 +0000)
committermalaterre <malaterre>
Mon, 17 Jan 2005 03:05:55 +0000 (03:05 +0000)
src/gdcmJPEGFragment.cxx
src/gdcmJPEGFragment.h
src/gdcmJPEGFragmentsInfo.cxx
src/gdcmJPEGFragmentsInfo.h
src/gdcmPixelReadConvert.cxx

index 03cc0bc03f5e7979f62d50a60b90196d3fa5f203..188f82266e7f7b2187468947d888e65a690aa5c5 100644 (file)
@@ -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
index 8434bdf2f8097b520656c1aced0cad2bfada8a12..02b161382b5b92a0414b8cef7bb32441860465ff 100644 (file)
@@ -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
 
index ae87b8de6ff7d117a41f89ec3313430573b8190b..3a666cb2f104392befbad6ae0d8a6249b67b26da 100644 (file)
@@ -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 <fstream>
 
 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
 
index 9f6a6443d0b4da8ffac4456d6c7f65d6c958396d..399d8130da256b90d28babbd8d4b3262aa716383 100644 (file)
@@ -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;
index b147cf3be6dbc30ac16ffa820d8afa5cd2761b26..8048fbab973366e52aa6191c59c46c40dc4c158d 100644 (file)
@@ -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 ) )