]> Creatis software - gdcm.git/blobdiff - src/gdcmFile.cxx
gdcm now deals with 16 Bits Run Length Encoded images
[gdcm.git] / src / gdcmFile.cxx
index 7274df46ab47f06e7da21d26b168fca987aa7f2f..af62cd8f545a8856958298eca0e2e1668bb5b47c 100644 (file)
@@ -3,6 +3,7 @@
 #include "gdcmFile.h"
 #include "gdcmUtil.h"
 #include "iddcmjpeg.h" // for the 'LibIDO' Jpeg LossLess
+#include "jpeg/ljpg/jpegless.h"
 
 /////////////////////////////////////////////////////////////////
 /**
@@ -124,7 +125,34 @@ bool gdcmFile::ReadPixelData(void* destination) {
     if ( fseek(fp, GetPixelOffset(), SEEK_SET) == -1 ) {
       CloseFile();
       return false;
-   }     
+   }
+
+// -------------------------  Compacted File (12 Bits Per Pixel)
+
+   /* unpack 12 Bits pixels into 16 Bits pixels */
+   /* 2 pixels 12bit =     [0xABCDEF]           */
+   /* 2 pixels 16bit = [0x0ABD] + [0x0FCE]      */
+
+   if (GetBitsAllocated()==12) {
+      int nbPixels = GetXSize()*GetYSize();
+      unsigned char b0, b1, b2;
+      
+      for(int p=0;p<nbPixels;p+=2) {
+         fread(&b0,1,1,fp);
+         fread(&b1,1,1,fp);
+         fread(&b2,1,1,fp);      
+         *((unsigned short int*)destination)++ = 
+                   ((b0 >> 4) << 8) + ((b0 & 0x0f) << 4) + (b1 & 0x0f);
+                       /* A */          /* B */            /* D */
+         *((unsigned short int*)destination)++ = 
+                   ((b2 & 0x0f) << 8) + ((b1 >> 4) << 4) + (b2 >> 4);
+                       /* F */          /* C */            /* E */
+                 
+       // Troubles expected on Big-Endian processors ?       
+      }
+      return(true);
+   }
+        
 
 // -------------------------  Uncompressed File
     
@@ -143,8 +171,18 @@ bool gdcmFile::ReadPixelData(void* destination) {
          return true;
       }
    } 
+
+   
+// ------------------------- Run Length Encoding
+
+      if (gdcmHeader::IsRLELossLessTransferSyntax()) {
+            int res = (bool)gdcm_read_RLE_file (destination);
+            return res; 
+      }  
+
     
- // ------------------------  Compressed File .
+// ----------------- SingleFrame/Multiframe JPEG Lossless/Lossy/2000 
        
       int nb;
       std::string str_nb=gdcmHeader::GetPubElValByNumber(0x0028,0x0100);
@@ -152,74 +190,21 @@ bool gdcmFile::ReadPixelData(void* destination) {
          nb = 16;
       } else {
          nb = atoi(str_nb.c_str() );
-         if (nb == 12) nb =16;
+         if (nb == 12) nb =16;  // ?? 12 should be ACR-NEMA only ?
       }
       int nBytes= nb/8;
       
-      int taille = GetXSize() *  GetYSize()  * GetSamplesPerPixel(); 
-          
-                
-  // ------------------------------- JPEG LossLess : call to Jpeg Libido
-   
-   if (IsJPEGLossless() && GetZSize() == 1) {
-   
-      int ln; //  Position on begining of Jpeg Pixels
-      fseek(fp,4,SEEK_CUR);  // skipping (fffe,e000) : Basic Offset Table Item
-      fread(&ln,4,1,fp); 
-      if(GetSwapCode()) 
-         ln=SwapLong(ln);    // Item length
-      fseek(fp,ln,SEEK_CUR); // skipping Basic Offset Table ('ln' bytes) 
-      fseek(fp,4,SEEK_CUR);  // skipping (fffe,e000) : First fragment Item Tag
-      fread(&ln,4,1,fp);     // First fragment length (just to know)
-      if(GetSwapCode()) 
-         ln=SwapLong(ln);      
-      ClbJpeg* jpg = _IdDcmJpegRead(fp); // TODO : find a 'full' one.
-                                         // (We use the LibIDO one :-(
-      if(jpg == NULL) {
-         CloseFile();
-         return false;
-      }      
-      int * dataJpg = jpg->DataImg;
-      switch (nBytes) {   
-         case 1:
-         {
-            unsigned short *dest = (unsigned short *)destination;
-            for (int i=0; i<taille; i++) {
-               *((unsigned char *)dest+i) = *(dataJpg +i);   
-            }
-         }
-         break;        
-         
-         case 2:
-         {
-            unsigned short *dest = (unsigned short *)destination;
-            for (int i=0; i<taille; i++) {           
-               *((unsigned short *)dest+i) = *(dataJpg +i);    
-            }
-         }
-         break;       
-     }
-      _IdDcmJpegFree (jpg);
-      return true;
-   } 
-  // ------------------------------- RLE
-
-      if (gdcmHeader::IsRLELossLessTransferSyntax()) {
-            int res = (bool)gdcm_read_RLE_file (destination);
-            return res; 
-      }
-
-  // ------------------------------- Multiframe JPEG 
-    
+      int taille = GetXSize() *  GetYSize()  * GetSamplesPerPixel();    
       long fragmentBegining; // for ftell, fseek
-      bool b = gdcmHeader::IsJPEG2000();
+      
+      bool jpg2000 =     IsJPEG2000();
+      bool jpgLossless = IsJPEGLossless();
        
-      bool res;
+      bool res = true;
       guint16 ItemTagGr,ItemTagEl;
-      int ln;  //  Position on begining of Jpeg Pixels
+      int ln;  
+      
+         //  Position on begining of Jpeg Pixels
       
       fread(&ItemTagGr,2,1,fp);  // Reading (fffe) : Item Tag Gr
       fread(&ItemTagEl,2,1,fp);  // Reading (e000) : Item Tag El
@@ -246,62 +231,41 @@ bool gdcmFile::ReadPixelData(void* destination) {
       }
               
       // parsing fragments until Sequence Delim. Tag found
-      //unsigned short *dest = (unsigned short *)destination;
-         
-      while (  ( ItemTagGr == 0xfffe) && (ItemTagEl != 0xe0dd) ) {      
+                              
+      while (  ( ItemTagGr == 0xfffe) && (ItemTagEl != 0xe0dd) ) { 
+      
+                        // --- for each Fragment
+     
          fread(&ln,4,1,fp); 
          if(GetSwapCode()) 
             ln=SwapLong(ln);    // Fragment Item length
       
-         // FIXME : multi fragments 
          fragmentBegining=ftell(fp);   
  
-         if (b)
+         if (jpg2000) {          // JPEG 2000 :    call to ???
+        
             res = (bool)gdcm_read_JPEG2000_file (destination);  // Not Yet written 
-            
-         else if (IsJPEGLossless()) { // JPEG LossLess : call to LibIDO Jpeg
-                 
-                  // -------------  for each Fragment
-                  
-                  // Warning : Works only if there is one fragment per frame
-                  //           (Or a single fragment for the multiframe file)
-            ClbJpeg* jpg = _IdDcmJpegRead(fp); // TODO : find a 'full' one.
-                                               // (We use the LibIDO one :-(
-            if(jpg == NULL) {
-               CloseFile();
-               return false;
-            }      
-            int * dataJpg = jpg->DataImg;
-            unsigned short *dest = (unsigned short *)destination;
-            switch (nBytes) {   
-               case 1:
-               {
-                  for (int i=0; i<taille; i++) {
-                     *((unsigned char *)dest+i) = *(dataJpg +i);   
-                  }
-               break;
-               }        
-         
-               case 2:
-               {
-                  for (int i=0; i<taille; i++) {        
-                     *((unsigned short *)dest+i) = *(dataJpg +i);    
-                  }
-               break;
-               }       
-           } 
-           _IdDcmJpegFree (jpg);
+
+         } // ------------------------------------- endif (JPEG2000)
           
-          res=1; // in order not to break the loop
+         else if (jpgLossless) { // JPEG LossLess : call to xmedcom JPEG
+                     
+           JPEGLosslessDecodeImage (fp,                         // Reading Fragment pixels
+                                    (unsigned short *)destination,
+                                    GetPixelSize()*8* GetSamplesPerPixel(),
+                                     ln);                                                         
+           res=1; // in order not to break the loop
      
-         } // ------------------------------------- endif (IsJPEGLossless())
+         } // ------------------------------------- endif (JPEGLossless)
                   
-         else  //               JPEG Lossy : call to IJG 6b
+         else {                   // JPEG Lossy : call to IJG 6b
+        
             if  (GetBitsStored() == 8) {
                res = (bool)gdcm_read_JPEG_file (destination);  // Reading Fragment pixels         
             } else {
                res = (bool)gdcm_read_JPEG_file12 (destination);// Reading Fragment pixels  
-            }       
+            } 
+        }  // ------------------------------------- endif (JPEGLossy)    
             
          if (!res) break;
                   
@@ -320,7 +284,7 @@ bool gdcmFile::ReadPixelData(void* destination) {
          } 
       
       }     // endWhile parsing fragments until Sequence Delim. Tag found    
-               
+    
       return res;
 }   
 
@@ -430,25 +394,28 @@ size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t MaxSize) {
 
    // *Try* to deal with the color
    // ----------------------------
-
+   
+       std::string str_PhotometricInterpretation = 
+                 gdcmHeader::GetPubElValByNumber(0x0028,0x0004);
+                  
+      if ( (str_PhotometricInterpretation == "MONOCHROME1 ") 
+        || (str_PhotometricInterpretation == "MONOCHROME2 ") ) {
+         return lgrTotale; 
+      }
+      
    // Planar configuration = 0 : Pixels are already RGB
    // Planar configuration = 1 : 3 planes : R, G, B
    // Planar configuration = 2 : 1 gray Plane + 3 LUT
 
    // Well ... supposed to be !
-   // See US-PAL-8-10x-echo.dcm: PlanarConfiguration=0,PhotometricInterpretation=PALETTE COLOR
+   // See US-PAL-8-10x-echo.dcm: PlanarConfiguration=0,
+   //                            PhotometricInterpretation=PALETTE COLOR
    // and heuristic has to be found :-( 
 
-      std::string str_PhotometricInterpretation = gdcmHeader::GetPubElValByNumber(0x0028,0x0004);
-   
-      if ( (str_PhotometricInterpretation == "MONOCHROME1 ") 
-        || (str_PhotometricInterpretation == "MONOCHROME2 ") 
-        || (str_PhotometricInterpretation == "RGB")) {
-         return lgrTotale; 
-      }
       int planConf=GetPlanarConfiguration();
 
-      // Whatever Planar Configuration is, "PALETTE COLOR " implies that we deal with the palette. 
+      // Whatever Planar Configuration is, 
+      // "PALETTE COLOR " implies that we deal with the palette. 
       if (str_PhotometricInterpretation == "PALETTE COLOR ")
          planConf=2;
 
@@ -481,9 +448,9 @@ size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t MaxSize) {
             //        integer computation counterpart
             for (int i=0;i<nbFrames;i++) {
                for (int j=0;j<l; j++) {
-                  R= 1.164 *( *a-16) + 1.596 *( *c -128) + 0.5;
-                  G= 1.164 *( *a-16) - 0.813 *( *c -128) - 0.392 *(*b -128) + 0.5;
-                  B= 1.164 *( *a-16) + 2.017 *( *b -128) + 0.5;
+                  R= 1.164 *(*a-16) + 1.596 *(*c -128) + 0.5;
+                  G= 1.164 *(*a-16) - 0.813 *(*c -128) - 0.392 *(*b -128) + 0.5;
+                  B= 1.164 *(*a-16) + 2.017 *(*b -128) + 0.5;
 
                   if (R<0.0)   R=0.0;
                   if (G<0.0)   G=0.0;
@@ -527,13 +494,7 @@ size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t MaxSize) {
             memmove(destination,newDest,lgrTotale);
             free(newDest);
         }
-
-            // now, it's an RGB image
-            // Lets's write it in the Header
-         std::string spp = "3";
-         gdcmHeader::SetPubElValByNumber(spp,0x0028,0x0002);
-         std::string rgb="RGB";
-         gdcmHeader::SetPubElValByNumber(rgb,0x0028,0x0004);
+         
          break;
        }
      
@@ -559,8 +520,6 @@ size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t MaxSize) {
             int l = lgrTotale/3;
             memmove(newDest, destination, l);// move Gray pixels to temp area
 
-            unsigned char * x = newDest;
-
             int j;
                // See PS 3.3-2003 C.11.1.1.2 p 619
                // 
@@ -571,8 +530,8 @@ size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t MaxSize) {
             // if we get a black image, let's just remove the '+1'
             // and check again 
             // if it works, we shall have to check the 3 Palettes
-            // to see which byte is ==0 (first one, on second one)        
-       
+            // to see which byte is ==0 (first one, or second one)
+                          
             for (int i=0;i<l; i++) {
                j=newDest[i]*mult +1;
                *a++ = lutR[j]; 
@@ -581,12 +540,6 @@ size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t MaxSize) {
             }
 
             free(newDest);
-        
-               // now, it's an RGB image      
-           std::string spp = "3";
-           gdcmHeader::SetPubElValByNumber(spp,0x0028,0x0002); 
-           std::string rgb="RGB";
-           gdcmHeader::SetPubElValByNumber(rgb,0x0028,0x0004);
                
          } else { // need to make RGB Pixels (?)
                   // from grey Pixels (?!)
@@ -595,9 +548,23 @@ size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t MaxSize) {
                     // Well . I'll wait till I find such an image 
          }
          break;
-         }
+      }
    } 
-    
+
+            // now, it's an RGB image
+            // Lets's write it in the Header
+
+         // CreateOrReplaceIfExist ?
+        
+   std::string spp = "3";        // Samples Per Pixel
+   gdcmHeader::SetPubElValByNumber(spp,0x0028,0x0002);
+   std::string rgb="RGB ";       // Photometric Interpretation
+   gdcmHeader::SetPubElValByNumber(rgb,0x0028,0x0004);
+   std::string planConfig = "0"; // Planar Configuration
+   gdcmHeader::SetPubElValByNumber(planConfig,0x0028,0x0006);
+        
+        // + Drop Palette Color ? 
+            
    return lgrTotale; 
 }