]> Creatis software - gdcm.git/commitdiff
Solve (hope so!) the JPEG troubles when BitsStored == 8 && BitsAllocated==16
authorjpr <jpr>
Tue, 4 Sep 2007 13:14:33 +0000 (13:14 +0000)
committerjpr <jpr>
Tue, 4 Sep 2007 13:14:33 +0000 (13:14 +0000)
src/gdcmFileHelper.cxx
src/gdcmFileHelper.h
src/gdcmPixelReadConvert.cxx
src/gdcmPixelWriteConvert.cxx

index 87c6e895b241c2ca9adc06b227d7d13e03752969..e823ccd7dc70288ff5e0ac9e13429fe17c51dff9 100644 (file)
@@ -4,8 +4,8 @@
   Module:    $RCSfile: gdcmFileHelper.cxx,v $
   Language:  C++
 
-  Date:      $Date: 2007/08/31 14:11:00 $
-  Version:   $Revision: 1.126 $
+  Date:      $Date: 2007/09/04 13:14:33 $
+  Version:   $Revision: 1.127 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -1977,6 +1977,8 @@ void FileHelper::Initialize()
    PixelReadConverter  = new PixelReadConvert;
    PixelWriteConverter = new PixelWriteConvert;
    Archive = new DocEntryArchive( FileInternal );
+   
+   KeepOverlays = false;
 }
 
 /**
index d59ee3842ca21f9f37cacc1609deb0215d419f15..38ab9d2a39abd23b7a67e461530e77b093ca7bc6 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmFileHelper.h,v $
   Language:  C++
-  Date:      $Date: 2007/08/24 10:45:18 $
-  Version:   $Revision: 1.53 $
+  Date:      $Date: 2007/09/04 13:14:33 $
+  Version:   $Revision: 1.54 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -62,7 +62,10 @@ public:
    /// Accessor to \ref File
    File *GetFile() { return FileInternal; }
    
-
+   /// \brief Tells gdcm wether we want to keep ACR-NEMA-like overlays or not.  
+   void SetKeepOverlays(bool k) { KeepOverlays =k; }
+   bool GetKeepOverlays( )      { return KeepOverlays; }
+     
    void SetLoadMode(int loadMode);
    void SetFileName(std::string const &fileName);
    bool Load();
@@ -270,6 +273,9 @@ private:
    /// \brief  1 : white=0, black=high value
    ///         2 : black=0, white=high value (default)
    int PhotometricInterpretation;
+   /// \brief wether we want to keep ACR-NEMA-like overlays or not.
+   bool KeepOverlays;   
 
 };
 } // end namespace gdcm
index a5e035e3cf471c5a8ede0844000441da3a089c10..c8aec3dfaefcb0fb087fc9d7fd84ad327ba25578 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmPixelReadConvert.cxx,v $
   Language:  C++
-  Date:      $Date: 2007/09/03 16:34:58 $
-  Version:   $Revision: 1.118 $
+  Date:      $Date: 2007/09/04 13:14:33 $
+  Version:   $Revision: 1.119 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -595,6 +595,7 @@ bool PixelReadConvert::ReadAndDecompressJPEGFile( std::ifstream *fp )
      int length = XSize * YSize * ZSize * SamplesPerPixel;
      int numberBytes = BitsAllocated / 8;
       
+      // to avoid major troubles when BitsStored == 8 && BitsAllocated==16 !
      int dummy;
      if (BitsStored == 8 && BitsAllocated==16)
         dummy = 16;
@@ -976,7 +977,6 @@ void PixelReadConvert::ConvertReorderEndianity()
      && !PixelSign )
    {
       int l = (int)( RawSize / ( BitsAllocated / 8 ) );
-      uint16_t *deb = (uint16_t *)Raw;
       int l = (int)( RawSize / ( BitsAllocated / 8 ) );
       uint16_t *deb = (uint16_t *)Raw;
       for(int i = 0; i<l; i++)
@@ -1117,6 +1117,17 @@ bool PixelReadConvert::ConvertReArrangeBits() throw ( FormatError )
       }
       else if ( BitsAllocated == 32 )
       {
+               }
+               deb++;
+            }
+         }
+      }
+      else if ( BitsAllocated == 32 )
+      { 
+         // pmask : to mask the 'unused bits' (may contain overlays)
+         uint32_t pmask = 0xffffffff;
+         pmask = pmask >> ( BitsAllocated - BitsStored );
          uint32_t *deb = (uint32_t*)Raw;
 
          if ( !PixelSign )
@@ -1141,8 +1152,8 @@ bool PixelReadConvert::ConvertReArrangeBits() throw ( FormatError )
                *deb = *deb >> (BitsStored - HighBitPosition - 1);
                if ( *deb & smask )
                   *deb = *deb | nmask;
-               else
-                  *deb = *deb & pmask;
+               else                
+                 *deb = *deb & pmask; 
                deb++;
             }
          }
@@ -1535,17 +1546,6 @@ of 12 for the third value and a max of 0xfff).
 
 Since almost every vendor that I have encountered that encodes LUTs
 makes this mistake, perhaps it is time to amend the standard to warn
-implementor's of receivers and/or sanction this bad behavior. We have
-talked about this in the past in WG 6 but so far everyone has been
-reluctant to write into the standard such a comment. Maybe it is time
-to try again, since if one is not aware of this problem, one cannot
-effectively implement display using VOI LUTs, and there is a vast
-installed base to contend with.
-
-I did not check presentation states, in which VOI LUTs could also be
-encountered, for the prevalence of this mistake, nor did I look at the
-encoding of Modality LUT's, which are unusual. Nor did I check digital
-mammography images. I would be interested to hear from anyone who has.
 I did not check presentation states, in which VOI LUTs could also be
 encountered, for the prevalence of this mistake, nor did I look at the
 encoding of Modality LUT's, which are unusual. Nor did I check digital
index a68250d17e9cddd7fe05dc720dd760a4558fde34..f4b2bbf141abcd51e10f42527e78c21c9e8cb45c 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmPixelWriteConvert.cxx,v $
   Language:  C++
-  Date:      $Date: 2007/08/31 10:59:08 $
-  Version:   $Revision: 1.22 $
+  Date:      $Date: 2007/09/04 13:14:33 $
+  Version:   $Revision: 1.23 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -328,16 +328,20 @@ void PixelWriteConvert::SetCompressJPEGUserData(uint8_t *data, size_t size, File
     int xsize = image->GetXSize();
     int ysize = image->GetYSize();
     int zsize = image->GetZSize();
+    
     int samplesPerPixel = image->GetSamplesPerPixel();
+    int bitsAllocated   = image->GetBitsAllocated();
+    int bitsStored      = image->GetBitsStored();
+        
    //std::cout << "X: " << xsize << std::endl;
    //std::cout << "Y: " << ysize << std::endl;
    //std::cout << "Sample: " << samplesPerPixel << std::endl;
    
-    gdcmDebugMacro( "image->GetBitsAllocated() " << image->GetBitsAllocated() << " image->GetBitsStored() " <<image->GetBitsStored() <<
+    gdcmDebugMacro( "bitsAllocated " << bitsAllocated << " bitsStored " <<bitsStored <<
                     std::endl);
    
-    int bitsstored = image->GetBitsStored();
-    unsigned int fragment_size = xsize*ysize*samplesPerPixel * (image->GetBitsAllocated() / 8);
+
+    unsigned int fragment_size = xsize*ysize*samplesPerPixel * (bitsAllocated / 8);
     gdcmDebugMacro("fragment_size " << fragment_size << " zsize " << zsize << " size " << size);
     assert( abs(fragment_size*zsize-size) <=1 );
 
@@ -348,44 +352,50 @@ void PixelWriteConvert::SetCompressJPEGUserData(uint8_t *data, size_t size, File
 #else
    EncodeWithoutBasicOffsetTable(of, 1);
 #endif
+
+      // to avoid major troubles when BitsStored == 8 && BitsAllocated==16 !
+
+   if (bitsStored == 8 && bitsAllocated == 16)
+      bitsStored = 16;
+
    uint8_t *pImageData = data;
    for(int i=0; i<zsize;i++)
      {
      gdcmDebugMacro("Write fragment no " << i );
      WriteDICOMItems(of, JpegFragmentSize);
      size_t beg = of->tellp();
-     if( bitsstored == 8 )
+     if( bitsStored == 8 )
        {
        gdcm_write_JPEG_file8(of, (char*)pImageData,size, 
-         image->GetXSize(), image->GetYSize(), image->GetZSize(), image->GetSamplesPerPixel(),
-         image->GetBitsAllocated(), 100 );
+         xsize, ysize, zsize, samplesPerPixel,
+         bitsAllocated, 100 );
        }
-     else if (bitsstored <= 12)
+     else if (bitsStored <= 12)
        {
-       assert( bitsstored >= 8 );
-       gdcm_write_JPEG_file12(of, (char*)pImageData,size,
-         image->GetXSize(), image->GetYSize(), image->GetZSize(), image->GetSamplesPerPixel(),
-         image->GetBitsAllocated(), 100);
+       assert( bitsStored >= 8 );
+       gdcm_write_JPEG_file12(of, (char*)pImageData, size,
+         xsize, ysize, zsize, samplesPerPixel,
+         bitsAllocated, 100);
        }
-     else if (bitsstored <= 16)
+     else if (bitsStored <= 16)
        {
-       assert( bitsstored >= 12 );
+       assert( bitsStored >= 12 );
        gdcm_write_JPEG_file16(of, (char*)pImageData,size, 
-         image->GetXSize(), image->GetYSize(), image->GetZSize(), image->GetSamplesPerPixel(),
-         image->GetBitsAllocated(), 100);
+         xsize, ysize, zsize, samplesPerPixel,
+         bitsAllocated, 100);
        }
-     else if (bitsstored <= 32)  // if we are lucky (?), it will compress as a 2 bytes stream
+     else if (bitsStored <= 32)  // if we are lucky (?), it will compress as a 2 bytes stream
        {                            // (Actually it doesn't !)
        // Just to allow ctest not to abort on 32bits per pixel image RTDOSE.dcm
-       assert( bitsstored >= 16 );
-       gdcmDebugMacro( "Warning : bitsstored>16 not supported by JPEG !" );
+       assert( bitsStored >= 16 );
+       gdcmDebugMacro( "Warning : bitsStored>16 not supported by JPEG !" );
        gdcm_write_JPEG_file16(of, (char*)pImageData, size, 
-         image->GetXSize(), image->GetYSize(), image->GetZSize(), image->GetSamplesPerPixel(),
-         image->GetBitsAllocated(), 100);
+         xsize, ysize, zsize, samplesPerPixel,
+         bitsAllocated, 100);
        }       
      else
        {
-       std::cerr << "Major pb : bitsstored =" << bitsstored << std::endl;
+       std::cerr << "Major pb : bitsStored =" << bitsStored << std::endl;
        abort();
        }
     size_t end = of->tellp();