]> Creatis software - gdcm.git/blobdiff - src/gdcmFile.cxx
* src/gdcmFile.cxx : bug fix under Window after JPR commit
[gdcm.git] / src / gdcmFile.cxx
index 2b09d36d2aadcc76671dde736a5fadac633d794f..94f86cb7e88c62d58d9f0eff77e2c7e45b7014b9 100644 (file)
@@ -2,6 +2,12 @@
 
 #include "gdcmFile.h"
 #include "gdcmUtil.h"
+
+
+// TODO : remove DEBUG
+#define DEBUG 0
+
+
 #include "iddcmjpeg.h"
 using namespace std;
 
@@ -38,7 +44,7 @@ gdcmFile::gdcmFile(const char * filename)
 /**
  * \ingroup   gdcmFile
  * \brief     calcule la longueur (in bytes) A ALLOUER pour recevoir les
- *        pixels de l'image
+ *             pixels de l'image
  *             ou DES images dans le cas d'un multiframe
  *             ATTENTION : il ne s'agit PAS de la longueur du groupe des Pixels        
  *             (dans le cas d'images compressees, elle n'a pas de sens).
@@ -56,9 +62,10 @@ void gdcmFile::SetPixelDataSizeFromHeader(void) {
       nb = atoi(str_nb.c_str() );
       if (nb == 12) nb =16;
    }
-   lgrTotale =  GetXSize() *  GetYSize() *  GetZSize() * (nb/8);
+   lgrTotale =  GetXSize() *  GetYSize() *  GetZSize() * (nb/8)* GetSamplesPerPixel();;
 }
 
+/////////////////////////////////////////////////////////////////
 /**
  * \ingroup   gdcmFile
  * \brief     Returns the size (in bytes) of required memory to hold
@@ -69,17 +76,21 @@ size_t gdcmFile::GetImageDataSize(void) {
    return (lgrTotale);
 }
 
+
+/////////////////////////////////////////////////////////////////
 /**
  * \ingroup gdcmFile
  * \brief   Read pixel data from disk (optionaly decompressing) into the
  *          caller specified memory location.
- * @param   destination Where the pixel data should be stored.
+ * @param   destination where the pixel data should be stored.
  *
  */
 bool gdcmFile::ReadPixelData(void* destination) {
    if ( !OpenFile())
       return false;
       
+      printf("GetPixelOffset() %d\n",GetPixelOffset() );
+      
     if ( fseek(fp, GetPixelOffset(), SEEK_SET) == -1 ) {
       CloseFile();
       return false;
@@ -99,39 +110,94 @@ bool gdcmFile::ReadPixelData(void* destination) {
          CloseFile();
          return true;
       }
-   }
-         
-   if (IsJPEGLossless()) {
+   } 
+     
+  // ------------------------------- Position on begining of Jpeg Pixels
+  
       int ln;
       fseek(fp,4,SEEK_CUR);
       fread(&ln,4,1,fp); 
       if(GetSwapCode()) 
          ln=SwapLong(ln);
-      //if (DEBUG) 
+      if (DEBUG) 
          printf ("ln %d\n",ln);
       fseek(fp,ln,SEEK_CUR);
       fseek(fp,4,SEEK_CUR);
       fread(&ln,4,1,fp); 
       if(GetSwapCode()) 
          ln=SwapLong(ln);
-      //if (DEBUG) 
-         printf ("ln image comprimée %d\n",ln);
-
+      if (DEBUG) 
+         printf ("ln image comprimée %d\n",ln);        
+          
+  // ------------------------------- JPEG LossLess : call to Jpeg Libido
+   
+   if (IsJPEGLossless()) {
       ClbJpeg* jpg = _IdDcmJpegRead(fp);
       if(jpg == NULL) {
          CloseFile();
          return false;
-      }     
-      memcpy(destination,jpg->DataImg,lgrTotale);
+      } 
+
+      int nb;
+      string str_nb=gdcmHeader::GetPubElValByNumber(0x0028,0x0100);
+      if (str_nb == "gdcm::Unfound" ) {
+         nb = 16;
+      } else {
+         nb = atoi(str_nb.c_str() );
+         if (nb == 12) nb =16;
+      }
+      int nBytes= nb/8;
+      int * dataJpg = jpg->DataImg;
+      int taille = GetXSize() *  GetYSize() *  GetZSize() * GetSamplesPerPixel();
+          
+      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;
+            
+         case 4:
+         {
+            unsigned int *dest=(unsigned int *)destination;
+            for (int i=0;i<taille; i++) {
+               *((unsigned int *)dest+i) = *(dataJpg +i);    
+            }
+          }
+         break;          
+     }
+      
       _IdDcmJpegFree (jpg);
-      CloseFile();
       return true;
-   }
-   
-    printf ("Sorry, TransfertSyntax not yet taken into account ...\n");
-    CloseFile();
-    return false;
-
+   }  
+  // ------------------------------- JPEG Lossy : call to IJG 6b
+  
+  // TODO : faire qq chose d'intelligent pour limiter 
+  // la duplication du code JPEG <bits=8 / bits=12>
+  // TODO : eplucher icelui pour traiter *egalement* bits=16
+  
+      int nBS;        
+      if  ((nBS = GetBitsStored()) != 12) {
+         printf("Sorry, Bits Stored = %d not yet taken into account\n",nBS);
+         return false;
+      }
+      
+      bool res = (bool)gdcm_read_JPEG_file (destination);
+      return res;
 }   
 
 /**
@@ -232,6 +298,43 @@ size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t MaxSize) {
          return (size_t)0; 
       }
    }
+   
+ // ---
+   string str_PlanarConfiguration = GetPubElValByNumber(0x0028,0x0006);
+   int PlanarConfiguration;
+   if (str_PlanarConfiguration == "gdcm::Unfound" ) {
+      PlanarConfiguration = 0;
+   } else {
+      PlanarConfiguration = atoi(str_PlanarConfiguration.c_str() );
+   }   
+ // ---
+   // TODO : replace by                         
+   // if (GetPlanarConfiguration() == 1) {
+   // after unfreeze
+   
+   if (PlanarConfiguration == 1) { // need to make RGB Pixels
+      int l = lgrTotale/3 ;
+
+      char * a = (char *)destination;
+      char * b = a + l;
+      char * c = b + l;
+      char * newDest = (char*) malloc(lgrTotale);
+      // TODO :
+      // any trick not to have to allocate temporary buffer is welcome ...
+      char *x = newDest;
+      for (int j=0;j<l; j++) {
+         *(x++) = *(a++);
+         *(x++) = *(b++);
+         *(x++) = *(c++);  
+      }
+      a = (char *)destination;
+      x = newDest;
+      for (int i=0;i<lgrTotale; i++) {
+         *(a++) = *(x++);
+      }
+      free(newDest);
+   }
    return lgrTotale; 
 }
 
@@ -245,69 +348,67 @@ guint32 s32;
 guint16 fort,faible;
 int i;
 
-if(nb == 16)
-    
-       switch(swap) {
-               case 0:
-               case 12:
-               case 1234:
-                       break;
+if(nb == 16)  
+   switch(swap) {
+      case 0:
+      case 12:
+      case 1234:
+         break;
                
-               case 21:
-               case 3412:
-               case 2143:
-               case 4321:
-
-                       for(i=0;i<lgr;i++)
-                               ((unsigned short int*)im)[i]= ((((unsigned short int*)im)[i])>>8)
-                                               | ((((unsigned short int*)im)[i])<<8);
-                       break;
+      case 21:
+      case 3412:
+      case 2143:
+      case 4321:
+
+         for(i=0;i<lgr;i++)
+            ((unsigned short int*)im)[i]= ((((unsigned short int*)im)[i])>>8)
+                                        | ((((unsigned short int*)im)[i])<<8);
+         break;
                        
-               default:
-                       printf("valeur de SWAP (16 bits) non autorisee : %d\n", swap);
-       
+      default:
+         printf("valeur de SWAP (16 bits) non autorisee : %d\n", swap);
+   } 
  
 if( nb == 32 )
-
-       switch (swap) {
-               case 0:
-               case 1234:
-                        break;
-
-               case 4321:
-                        for(i=0;i<lgr;i++) {
-                               faible=  ((unsigned long int*)im)[i]&0x0000ffff;    /* 4321 */
-                                       fort  =((unsigned long int*)im)[i]>>16;
-                               fort=  (fort>>8)   | (fort<<8);
-                               faible=(faible>>8) | (faible<<8);
-                               s32=faible;
-                               ((unsigned long int*)im)[i]=(s32<<16)|fort;
-                               }
-                       break;
-
-               case 2143:
-                       for(i=0;i<lgr;i++) {
-                                       faible=  ((unsigned long int*)im)[i]&0x0000ffff;    /* 2143 */
-                                       fort=((unsigned long int*)im)[i]>>16;
-                                       fort=  (fort>>8)   | (fort<<8);
-                                       faible=(faible>>8) | (faible<<8);
-                                       s32=fort; 
-                                       ((unsigned long int*)im)[i]=(s32<<16)|faible;
-                       }
-                       break;
+   switch (swap) {
+      case 0:
+      case 1234:
+         break;
+
+      case 4321:
+         for(i=0;i<lgr;i++) {
+            faible=  ((unsigned long int*)im)[i]&0x0000ffff;    /* 4321 */
+            fort  =((unsigned long int*)im)[i]>>16;
+            fort=  (fort>>8)   | (fort<<8);
+            faible=(faible>>8) | (faible<<8);
+            s32=faible;
+            ((unsigned long int*)im)[i]=(s32<<16)|fort;
+         }
+         break;
+
+      case 2143:
+         for(i=0;i<lgr;i++) {
+            faible=  ((unsigned long int*)im)[i]&0x0000ffff;    /* 2143 */
+            fort=((unsigned long int*)im)[i]>>16;
+            fort=  (fort>>8)   | (fort<<8);
+            faible=(faible>>8) | (faible<<8);
+            s32=fort; 
+            ((unsigned long int*)im)[i]=(s32<<16)|faible;
+         }
+         break;
   
-               case 3412:
-                       for(i=0;i<lgr;i++) {
-                               faible=  ((unsigned long int*)im)[i]&0x0000ffff;    /* 3412 */
-                                       fort=((unsigned long int*)im)[i]>>16;                  
-                                       s32=faible; 
-                                       ((unsigned long int*)im)[i]=(s32<<16)|fort;
-                       }                 
-                       break; 
+      case 3412:
+         for(i=0;i<lgr;i++) {
+            faible=  ((unsigned long int*)im)[i]&0x0000ffff;    /* 3412 */
+            fort=((unsigned long int*)im)[i]>>16;                  
+            s32=faible; 
+            ((unsigned long int*)im)[i]=(s32<<16)|fort;
+         }                 
+         break; 
                                
-               default:
-                       printf("valeur de SWAP (32 bits) non autorisee : %d\n", swap);
-       
+      default:
+         printf("valeur de SWAP (32 bits) non autorisee : %d\n", swap);
+   } 
 return;
 }
 
@@ -327,8 +428,6 @@ int gdcmFile::SetImageData(void * inData, size_t ExpectedSize) {
    SetImageDataSize(ExpectedSize);
    PixelData = inData;
    lgrTotale = ExpectedSize;
-   
-   
    return(1);
 }
 
@@ -347,20 +446,20 @@ int gdcmFile::SetImageData(void * inData, size_t ExpectedSize) {
 
 void gdcmFile::SetImageDataSize(size_t ImageDataSize) {
 
-       string content1;
-       char car[20];
+   string content1;
+   char car[20];
        
-       // suppose que le ElValue (0x7fe0, 0x0010) existe ...
+   // suppose que le ElValue (0x7fe0, 0x0010) existe ...
        
-       sprintf(car,"%d",ImageDataSize);
+   sprintf(car,"%d",ImageDataSize);
  
-       gdcmElValue*a = GetElValueByNumber(0x7fe0, 0x0010);
-       a->SetLength(ImageDataSize);
+   gdcmElValue*a = GetElValueByNumber(0x7fe0, 0x0010);
+   a->SetLength(ImageDataSize);
                
-       ImageDataSize+=8;
-       sprintf(car,"%d",ImageDataSize);
-       content1=car;   
-       SetPubElValByNumber(content1, 0x7fe0, 0x0000);
+   ImageDataSize+=8;
+   sprintf(car,"%d",ImageDataSize);
+   content1=car;       
+   SetPubElValByNumber(content1, 0x7fe0, 0x0000);
 }
 
 
@@ -379,16 +478,16 @@ void gdcmFile::SetImageDataSize(size_t ImageDataSize) {
 
 int gdcmFile::WriteRawData (string nomFichier) {
 
-       FILE * fp1;
-       fp1 = fopen(nomFichier.c_str(),"wb");
-       if (fp1 == NULL) {
-               printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
-               return (0);
-       
+   FILE * fp1;
+   fp1 = fopen(nomFichier.c_str(),"wb");
+   if (fp1 == NULL) {
+      printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
+      return (0);
+   } 
        
-       fwrite (PixelData,lgrTotale, 1, fp1);
-       fclose (fp1);
-       return(1);
+   fwrite (PixelData,lgrTotale, 1, fp1);
+   fclose (fp1);
+   return(1);
 }
 
 
@@ -410,6 +509,15 @@ int gdcmFile::WriteDcmImplVR (string nomFichier) {
    return WriteBase(nomFichier, ImplicitVR);
 }
 
+/////////////////////////////////////////////////////////////////
+/**
+ * \ingroup   gdcmFile
+ *
+ * @param  nomFichier TODO JPR
+ *
+ * @return TODO JPR
+ */
 int gdcmFile::WriteDcmImplVR (const char* nomFichier) {
    return WriteDcmImplVR (string (nomFichier));
 }
@@ -448,12 +556,21 @@ int gdcmFile::WriteAcr (string nomFichier) {
    return WriteBase(nomFichier, ACR);
 }
 
-int gdcmFile::WriteBase (string nomFichier, FileType type) {
+/////////////////////////////////////////////////////////////////
+/**
+ * \ingroup   gdcmFile
+ *
+ * @param  FileName TODO JPR
+ * @param  type TODO JPR
+ *
+ * @return TODO JPR
+ */
+int gdcmFile::WriteBase (string FileName, FileType type) {
 
    FILE * fp1;
-   fp1 = fopen(nomFichier.c_str(),"wb");
+   fp1 = fopen(FileName.c_str(),"wb");
    if (fp1 == NULL) {
-      printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
+      printf("Echec ouverture (ecriture) Fichier [%s] \n",FileName.c_str());
       return (0);
    }