]> Creatis software - gdcm.git/commitdiff
gdcm now deals with 16 Bits Run Length Encoded images
authorjpr <jpr>
Thu, 23 Oct 2003 12:08:32 +0000 (12:08 +0000)
committerjpr <jpr>
Thu, 23 Oct 2003 12:08:32 +0000 (12:08 +0000)
src/gdcmFile.cxx
src/gdcmHeader.cxx
src/gdcmRLE.cxx

index 4490655cc9e146d786a814dc74de914450e381be..af62cd8f545a8856958298eca0e2e1668bb5b47c 100644 (file)
@@ -126,7 +126,7 @@ bool gdcmFile::ReadPixelData(void* destination) {
       CloseFile();
       return false;
    }
-        
+
 // -------------------------  Compacted File (12 Bits Per Pixel)
 
    /* unpack 12 Bits pixels into 16 Bits pixels */
@@ -152,7 +152,7 @@ bool gdcmFile::ReadPixelData(void* destination) {
       }
       return(true);
    }
-
+        
 
 // -------------------------  Uncompressed File
     
@@ -171,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);
@@ -184,22 +194,11 @@ bool gdcmFile::ReadPixelData(void* destination) {
       }
       int nBytes= nb/8;
       
-      int taille = GetXSize() *  GetYSize()  * GetSamplesPerPixel(); 
-          
-
-  // ---------------- RLE
-
-      if (gdcmHeader::IsRLELossLessTransferSyntax()) {
-            int res = (bool)gdcm_read_RLE_file (destination);
-            return res; 
-      }
-
-  // ----------------- SingleFrame/Multiframe JPEG 
-    
+      int taille = GetXSize() *  GetYSize()  * GetSamplesPerPixel();    
       long fragmentBegining; // for ftell, fseek
       
-      bool b = gdcmHeader::IsJPEG2000();
+      bool jpg2000 =     IsJPEG2000();
+      bool jpgLossless = IsJPEGLossless();
        
       bool res = true;
       guint16 ItemTagGr,ItemTagEl;
@@ -243,39 +242,30 @@ bool gdcmFile::ReadPixelData(void* destination) {
       
          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 xmedcom JPEG
+
+         } // ------------------------------------- endif (JPEG2000)
+          
+         else if (jpgLossless) { // JPEG LossLess : call to xmedcom JPEG
                      
-           JPEGLosslessDecodeImage (fp, 
+           JPEGLosslessDecodeImage (fp,                         // Reading Fragment pixels
                                     (unsigned short *)destination,
                                     GetPixelSize()*8* GetSamplesPerPixel(),
-                                     ln);
-                                                                          
+                                     ln);                                                         
            res=1; // in order not to break the loop
      
-         } // ------------------------------------- endif (IsJPEGLossless())
+         } // ------------------------------------- endif (JPEGLossless)
                   
-         else { //              JPEG Lossy : call to xmedcon JPEG
-               //               (just to see if it works) --> it does NOT !
-        /*
-           JPEGLosslessDecodeImage (fp, 
-                                    (unsigned short *)destination,
-                                    GetPixelSize()*8* GetSamplesPerPixel(),
-                                     ln);       
-        
-        */
-           //               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;
                   
@@ -404,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;
 
@@ -455,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;
@@ -501,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;
        }
      
@@ -544,9 +531,7 @@ size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t MaxSize) {
             // and check again 
             // if it works, we shall have to check the 3 Palettes
             // to see which byte is ==0 (first one, or second one)
-                   
-            //unsigned char * x = newDest;
-       
+                          
             for (int i=0;i<l; i++) {
                j=newDest[i]*mult +1;
                *a++ = lutR[j]; 
@@ -555,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 (?!)
@@ -569,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; 
 }
 
index db1a38edfbd14a8dcee2f0559f718b58917ff59e..7cb2cad0fad356999bfcf623eec6399ea0256169 100644 (file)
@@ -1,4 +1,4 @@
-// $Header: /cvs/public/gdcm/src/Attic/gdcmHeader.cxx,v 1.102 2003/10/21 12:18:23 jpr Exp $
+// $Header: /cvs/public/gdcm/src/Attic/gdcmHeader.cxx,v 1.103 2003/10/23 12:08:32 jpr Exp $
 
 #include "gdcmHeader.h"
 
@@ -122,7 +122,7 @@ gdcmHeader::~gdcmHeader (void) {
 
 // Fourth semantics:
 //
-// ---> Warning : This fourth fiels is NOT part 
+// ---> Warning : This fourth field is NOT part 
 //                of the 'official' Dicom Dictionnary
 //                and should NOT be used.
 //                (Not defined for all the groups
@@ -672,14 +672,14 @@ void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) {
          return 0;
       TotalLength += 4;  // We even have to decount the group and element 
      
-      if ( g != 0xfffe           && g!=0xb00c ) /*for bogus header */ {
+      if ( g != 0xfffe && g!=0xb00c ) /*for bogus header */ {
          char msg[100]; // for sprintf. Sorry
          sprintf(msg,"wrong group (%04x) for an item sequence (%04x,%04x)\n",g, g,n);
          dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",msg); 
          errno = 1;
          return 0;
       }
-      if ( n == 0xe0dd       || ( g==0xb00c && n==0x0eb6 ) ) /* for bogus header  */ 
+      if ( n == 0xe0dd || ( g==0xb00c && n==0x0eb6 ) ) /* for bogus header  */ 
          FoundSequenceDelimiter = true;
       else if ( n != 0xe000 ){
          char msg[100];  // for sprintf. Sorry
@@ -1767,7 +1767,7 @@ void gdcmHeader::PrintPubElVal(std::ostream & os) {
 
 /**
   * \ingroup gdcmHeader
-  * \brief
+  * \brief 
   * @return
   */  
 void gdcmHeader::PrintPubDict(std::ostream & os) {
@@ -1777,7 +1777,7 @@ void gdcmHeader::PrintPubDict(std::ostream & os) {
 /**
   * \ingroup gdcmHeader
   * \brief
-  * @return
+  * @return integer, acts as a Boolean
   */ 
 int gdcmHeader::Write(FILE * fp, FileType type) {
 
@@ -1841,7 +1841,6 @@ void * gdcmHeader::LoadElementVoidArea(guint16 Group, guint16 Elem) {
        free(a);
        return NULL;
    }
-   // TODO : finish the function !!!
    return a;  
 }
 
index 8930df2118ff953b836bc65b6aee05b3144160ef..bb993d4828e74053fb20b2daf926987fa4ab7aca 100644 (file)
@@ -11,9 +11,6 @@ static int _gdcm_read_RLE_fragment (char ** image_buffer,
                                     FILE* fp);
 // static because nothing but gdcm_read_RLE_file may call it
 
-#define DEBUG 0
-// Will be removed
-
 // ----------------------------------------------------------------------------
 /**
  * \ingroup   gdcmFile
@@ -35,7 +32,6 @@ int
 gdcmFile::gdcm_read_RLE_file (void * image_buffer) {
    long fragmentBegining; // for ftell, fseek
    char * im = (char *)image_buffer;
-   if (DEBUG)std::cout << "RLE image" << std::endl;
 
    long RleSegmentLength[15],fragmentLength,uncompressedSegmentSize;;
    long ftellRes, ln;
@@ -43,7 +39,6 @@ gdcmFile::gdcm_read_RLE_file (void * image_buffer) {
    guint32 RleSegmentOffsetTable[15];
    guint16 ItemTagGr,ItemTagEl;
    uncompressedSegmentSize=GetXSize()*GetYSize();
-   if (DEBUG)printf("uncompressedSegmentSize %d\n",uncompressedSegmentSize);
    ftellRes=ftell(fp);
    // Basic Offset Table with Item Value
       // Item Tag
@@ -53,15 +48,11 @@ gdcmFile::gdcm_read_RLE_file (void * image_buffer) {
       ItemTagGr=SwapShort(ItemTagGr); 
       ItemTagEl=SwapShort(ItemTagEl);      
    }
-   if (DEBUG)printf ("at %x : ItemTag (should be fffe,e000): %04x,%04x\n",
-          ftellRes,ItemTagGr,ItemTagEl );
       // Item Length
    ftellRes=ftell(fp);
    fread(&ln,4,1,fp); 
    if(GetSwapCode()) 
       ln=SwapLong(ln);    // Basic Offset Table Item Lentgh
-   if (DEBUG)printf("at %x : Basic Offset Table Item Lentgh (??) %d x(%08x)\n",
-         ftellRes,ln,ln);
    if (ln != 0) {
       // What is it used for ??
       char * BasicOffsetTableItemValue= (char *)malloc(ln+1);
@@ -69,7 +60,6 @@ gdcmFile::gdcm_read_RLE_file (void * image_buffer) {
       guint32 a;
       for (int i=0;i<ln;i+=4){
          a=str2num(&BasicOffsetTableItemValue[i],guint32);
-        if (DEBUG)printf("      x(%08x)  %d\n",a,a);
       }        
    }
 
@@ -80,8 +70,6 @@ gdcmFile::gdcm_read_RLE_file (void * image_buffer) {
       ItemTagGr=SwapShort(ItemTagGr); 
       ItemTagEl=SwapShort(ItemTagEl);      
    }  
-   if (DEBUG)printf ("at %x : ItemTag (should be fffe,e000 or e0dd): %04x,%04x\n",
-         ftellRes,ItemTagGr,ItemTagEl );
 
    // while 'Sequence Delimiter Item' (fffe,e0dd) not found
    while (  ( ItemTagGr == 0xfffe) && (ItemTagEl != 0xe0dd) ) { 
@@ -90,31 +78,24 @@ gdcmFile::gdcm_read_RLE_file (void * image_buffer) {
       fread(&fragmentLength,4,1,fp); 
       if(GetSwapCode()) 
          fragmentLength=SwapLong(fragmentLength);    // length
-      if (DEBUG)printf("      at %x : fragment length %d x(%08x)\n",
-             ftellRes, fragmentLength,fragmentLength);
 
           //------------------ scanning (not reading) fragment pixels
  
       fread(&nbRleSegments,4,1,fp);  // Reading : Number of RLE Segments        
       if(GetSwapCode()) 
          nbRleSegments=SwapLong(nbRleSegments);
-         if (DEBUG)printf("         Nb of RLE Segments : %d\n",nbRleSegments);
  
       for(int k=1; k<=15; k++) { // Reading RLE Segments Offset Table
          ftellRes=ftell(fp);
          fread(&RleSegmentOffsetTable[k],4,1,fp);
          if(GetSwapCode())
             RleSegmentOffsetTable[k]=SwapLong(RleSegmentOffsetTable[k]);
-         if (DEBUG)printf("        at : %x Offset Segment %d : %d (%x)\n",
-                 ftellRes,k,RleSegmentOffsetTable[k],RleSegmentOffsetTable[k]);
       }
 
       if (nbRleSegments>1) { 
          for(int k=1; k<=nbRleSegments-1; k++) { // reading RLE Segments
             RleSegmentLength[k]=RleSegmentOffsetTable[k+1]-RleSegmentOffsetTable[k];
             ftellRes=ftell(fp);
-            if (DEBUG)printf ("       (in) Segment %d : Length = %d  x(%x) Start at %x\n",
-                                 k,RleSegmentLength[k],RleSegmentLength[k], ftellRes);
             fragmentBegining=ftell(fp);   
             _gdcm_read_RLE_fragment (&im, RleSegmentLength[k],uncompressedSegmentSize,fp);
             fseek(fp,fragmentBegining,SEEK_SET);  
@@ -123,10 +104,6 @@ gdcmFile::gdcm_read_RLE_file (void * image_buffer) {
       }
       RleSegmentLength[nbRleSegments] = fragmentLength - RleSegmentOffsetTable[nbRleSegments];
       ftellRes=ftell(fp);
-      if (DEBUG)printf ("        (out)Segment %d : Length = %d  x(%x) Start at %x\n",
-                       nbRleSegments,
-                       RleSegmentLength[nbRleSegments],RleSegmentLength[nbRleSegments],
-                       ftellRes);
       fragmentBegining=ftell(fp);
       _gdcm_read_RLE_fragment (&im, RleSegmentLength[nbRleSegments],uncompressedSegmentSize, fp);
       fseek(fp,fragmentBegining,SEEK_SET);  
@@ -141,9 +118,31 @@ gdcmFile::gdcm_read_RLE_file (void * image_buffer) {
          ItemTagGr=SwapShort(ItemTagGr); 
          ItemTagEl=SwapShort(ItemTagEl);      
       }
-      if (DEBUG)printf ("at %x : ItemTag (should be fffe,e000 or e0dd): %04x,%04x\n",
-            ftellRes,ItemTagGr,ItemTagEl );
    } 
+   
+   if (GetBitsAllocated()==16) { // try to deal with RLE 16 Bits
+   
+      im = (char *)image_buffer;
+         //  need to make 16 Bits Pixels from Low Byte and Hight Byte 'Planes'
+
+      int l = GetXSize()*GetYSize();
+      int nbFrames = GetZSize();
+
+      char * newDest = (char*) malloc(l*nbFrames*2);
+      char *x  = newDest;
+      char * a = (char *)image_buffer;
+      char * b = a + l;
+
+      for (int i=0;i<nbFrames;i++) {
+         for (int j=0;j<l; j++) {
+            *(x++) = *(a++);
+            *(x++) = *(b++);
+         }
+      }
+      memmove(image_buffer,newDest,lgrTotale);
+      free(newDest);   
+   }
+      
    return (1);
 }