]> Creatis software - gdcm.git/blobdiff - src/gdcmRLE.cxx
gdcm now deals with 16 Bits Run Length Encoded images
[gdcm.git] / src / gdcmRLE.cxx
index f119cdbcb112c0e4f195d162a92fa86f2b172912..bb993d4828e74053fb20b2daf926987fa4ab7aca 100644 (file)
@@ -1,12 +1,3 @@
-//This is needed when compiling in debug mode
-#ifdef _MSC_VER
-// 'identifier' : class 'type' needs to have dll-interface to be used by
-// clients of class 'type2'
-#pragma warning ( disable : 4251 )
-// 'identifier' : identifier was truncated to 'number' characters in the
-// debug information
-#pragma warning ( disable : 4786 )
-#endif //_MSC_VER
 
 #include <stdio.h>
 #include "gdcmFile.h"
@@ -20,12 +11,13 @@ static int _gdcm_read_RLE_fragment (char ** image_buffer,
                                     FILE* fp);
 // static because nothing but gdcm_read_RLE_file may call it
 
-// ------------------------------------------------------------------------------------
+// ----------------------------------------------------------------------------
 /**
  * \ingroup   gdcmFile
  * \brief     Reads a 'Run Length Encoded' Dicom encapsulated file
- * @param     destination Address (in caller's memory space) at which the
- *            pixel data should be copied
+ * @param     image_buffer destination Address (in caller's memory space) 
+ *            at which the pixel data should be copied
+ *   
  * @return    int acts as a Boolean 
  */
 
@@ -33,10 +25,13 @@ static int _gdcm_read_RLE_fragment (char ** image_buffer,
 // Forget the printf as they will be removed
 // as soon as the last Heuristics are checked
 
+// pb with RLE 16 Bits : 
+
+
 int
 gdcmFile::gdcm_read_RLE_file (void * image_buffer) {
+   long fragmentBegining; // for ftell, fseek
    char * im = (char *)image_buffer;
-   cout << "RLE image" << endl;
 
    long RleSegmentLength[15],fragmentLength,uncompressedSegmentSize;;
    long ftellRes, ln;
@@ -44,7 +39,6 @@ gdcmFile::gdcm_read_RLE_file (void * image_buffer) {
    guint32 RleSegmentOffsetTable[15];
    guint16 ItemTagGr,ItemTagEl;
    uncompressedSegmentSize=GetXSize()*GetYSize();
-   printf("uncompressedSegmentSize %d\n",uncompressedSegmentSize);
    ftellRes=ftell(fp);
    // Basic Offset Table with Item Value
       // Item Tag
@@ -54,15 +48,11 @@ gdcmFile::gdcm_read_RLE_file (void * image_buffer) {
       ItemTagGr=SwapShort(ItemTagGr); 
       ItemTagEl=SwapShort(ItemTagEl);      
    }
-   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
-   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);
@@ -70,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);
-         printf("      x(%08x)  %d\n",a,a);
       }        
    }
 
@@ -81,8 +70,6 @@ gdcmFile::gdcm_read_RLE_file (void * image_buffer) {
       ItemTagGr=SwapShort(ItemTagGr); 
       ItemTagEl=SwapShort(ItemTagEl);      
    }  
-   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) ) { 
@@ -91,47 +78,38 @@ gdcmFile::gdcm_read_RLE_file (void * image_buffer) {
       fread(&fragmentLength,4,1,fp); 
       if(GetSwapCode()) 
          fragmentLength=SwapLong(fragmentLength);    // length
-      printf("      at %x : fragment length %d x(%08x)\n",
-             ftellRes, fragmentLength,fragmentLength);
 
-       // scanning fragment pixels
+          //------------------ scanning (not reading) fragment pixels
  
       fread(&nbRleSegments,4,1,fp);  // Reading : Number of RLE Segments        
       if(GetSwapCode()) 
          nbRleSegments=SwapLong(nbRleSegments);
-         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]);
-         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);
-                printf ("        Segment %d : Length = %d Start at %x\n",
-                                 k,RleSegmentLength[k], ftellRes);    
-             _gdcm_read_RLE_fragment (&im, RleSegmentLength[k],uncompressedSegmentSize,fp);
-              //fseek(fp,RleSegmentLength[k],SEEK_CUR);    
-     
-          }
-       }
-       RleSegmentLength[nbRleSegments] = fragmentLength - RleSegmentOffsetTable[nbRleSegments]; // + 4;
-            // 4 : bytes for number of RLE Segments (WHY ???);
-                                           // TODO : Check the value
-          ftellRes=ftell(fp);
-          printf ("        Segment %d : Length = %d Start at %x\n",
-                           nbRleSegments,RleSegmentLength[nbRleSegments],ftellRes);
-       _gdcm_read_RLE_fragment (&im, RleSegmentLength[nbRleSegments],uncompressedSegmentSize, fp);
-        //fseek(fp,RleSegmentLength[nbRleSegments],SEEK_CUR);    
-  
-       
-      // end of scanning fragment pixels     
+      if (nbRleSegments>1) { 
+         for(int k=1; k<=nbRleSegments-1; k++) { // reading RLE Segments
+            RleSegmentLength[k]=RleSegmentOffsetTable[k+1]-RleSegmentOffsetTable[k];
+            ftellRes=ftell(fp);
+            fragmentBegining=ftell(fp);   
+            _gdcm_read_RLE_fragment (&im, RleSegmentLength[k],uncompressedSegmentSize,fp);
+            fseek(fp,fragmentBegining,SEEK_SET);  
+            fseek(fp,RleSegmentLength[k],SEEK_CUR);        
+         }
+      }
+      RleSegmentLength[nbRleSegments] = fragmentLength - RleSegmentOffsetTable[nbRleSegments];
+      ftellRes=ftell(fp);
+      fragmentBegining=ftell(fp);
+      _gdcm_read_RLE_fragment (&im, RleSegmentLength[nbRleSegments],uncompressedSegmentSize, fp);
+      fseek(fp,fragmentBegining,SEEK_SET);  
+      fseek(fp,RleSegmentLength[nbRleSegments],SEEK_CUR);    
+      
+      // end of scanning fragment pixels       
    
       ftellRes=ftell(fp);
       fread(&ItemTagGr,2,1,fp);  // Reading (fffe) : Item Tag Gr
@@ -140,9 +118,31 @@ gdcmFile::gdcm_read_RLE_file (void * image_buffer) {
          ItemTagGr=SwapShort(ItemTagGr); 
          ItemTagEl=SwapShort(ItemTagEl);      
       }
-      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);
 }
 
@@ -166,14 +166,12 @@ _gdcm_read_RLE_fragment (char ** areaToRead,
    long numberOfOutputBytes=0;
    char n, car;
    ftellRes =ftell(fp);
-   printf ("Fragment begin : %x lengthToDecode %d\n",ftellRes,lengthToDecode);
 
    while(numberOfOutputBytes<uncompressedSegmentSize) {
 
       ftellRes =ftell(fp);
       fread(&n,sizeof(char),1,fp);
       count=n;
-     //printf ("   Piece begin : %x count : %d x(%x)\n",ftellRes, count, count);
       if (count >= 0 && count <= 127) {
          fread(*areaToRead,(count+1)*sizeof(char),1,fp);
          *areaToRead+=count+1;