]> Creatis software - gdcm.git/blobdiff - src/gdcmFile.cxx
Minor modifications
[gdcm.git] / src / gdcmFile.cxx
index e00f17993920ce27349a53a4374aadcf3d64b114..38282e941cd6878af59f49f9deb65174eb1d6e8a 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmFile.cxx,v $
   Language:  C++
-  Date:      $Date: 2006/06/15 14:25:26 $
-  Version:   $Revision: 1.322 $
+  Date:      $Date: 2006/11/15 15:54:15 $
+  Version:   $Revision: 1.327 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
 #include <stdio.h>  //sscanf
 #include <stdlib.h> // for atoi
 
-namespace gdcm 
+namespace gdcm
+
 {
 
 //-----------------------------------------------------------------------------
@@ -141,6 +142,7 @@ File::~File()
  * @return false if file cannot be open or no swap info was found,
  *         or no tag was found.
  */
+
 bool File::Load( ) 
 {
    if ( ! this->Document::Load( ) )
@@ -175,6 +177,7 @@ bool File::DoTheLoadingJob( )
    }
    else
    {
+
       GrPixel = (uint16_t) atoi( imgLocation.c_str() );
    }   
 
@@ -570,19 +573,21 @@ and
       // - check if  SOPClassUID contains 2 parts (e.g. "4\3")
       // - guess how to deduce the spacing (FOV ?, ??)
       
-      entry = GetDataEntry(0x0028,0x0034); 
-      nbValue = entry->GetValueCount();
-      if( nbValue !=2 ) {
-         gdcmWarningMacro("PixelAspectRatio (0x0028,0x0034) "
-         << "has a wrong number of values :" << nbValue);
+      entry = GetDataEntry(0x0028,0x0034);
+      if ( entry )
+      {
+         nbValue = entry->GetValueCount();
+         if( nbValue !=2 ) {
+            gdcmWarningMacro("PixelAspectRatio (0x0028,0x0034) "
+            << "has a wrong number of values :" << nbValue);
+         }
+         xspacing = 1.0; // We get Pixel Aspect Ratio, not Spacing ...
+         ok = true;
       }
-      xspacing = 1.0; // We get Pixel Aspect Ratio, not Spacing ...
-      ok = true;
-   }
   
-   if (ok)
-      return xspacing;
-
+      if (ok)
+         return xspacing;
+   }
 /*      
    if (Util::DicomStringEqual( SOPClassUID,"1.2.840.10008.5.1.4.1.1.1") ) 
    // Computed Radiography Image Storage   
@@ -677,21 +682,32 @@ float File::GetYSpacing()
       // - check if  SOPClassUID contains 2 parts (e.g. "4\3")
       // - no way to deduce the spacing/
       
-      entry = GetDataEntry(0x0028,0x0034); 
-      nbValue = entry->GetValueCount();
-      if( nbValue !=2 ) {
-         gdcmWarningMacro("PixelAspectRatio (0x0028,0x0034) "
-         << "has a wrong number of values :" << nbValue);
+      entry = GetDataEntry(0x0028,0x0034);
+      if ( entry )
+      {       
+         nbValue = entry->GetValueCount();
+         if( nbValue ==2 ) {
+            yspacing = (float)entry->GetValue(0)/(float)entry->GetValue(1);
+            //std::cout << "ys " << yspacing << std::endl;
+            ok = true;
       }
-      yspacing = (float)entry->GetValue(0)/(float)entry->GetValue(1);
-      std::cout << "ys " << yspacing << std::endl;
-      ok = true;                  
+      else
+      {
+         gdcmWarningMacro("PixelAspectRatio (0x0028,0x0034) "
+               << "has a wrong number of values :" << nbValue);
+         if (nbValue == 0 ) {
+            ok = false;
+         }
+         else if (nbValue == 1 ) {
+            yspacing = 1.0; // We get Pixel Aspect Ratio, not Spacing ...
+            ok = true;
+         } 
+      }                  
    }
   
-   if (ok)
-      return yspacing;
-      
-   
+      if (ok)
+         return yspacing;      
+   }   
 
    // go on with old method ...
    // ---------------------
@@ -759,8 +775,7 @@ float File::GetZSpacing()
    //   we suppose slices joint together
    DataEntry *entry = GetDataEntry(0x0018,0x0088);
    if( entry )
-   {
-      zspacing = (float)entry->GetValue(0);
+   {      zspacing = (float)entry->GetValue(0);
 
       if ( zspacing == 0.0 )
          zspacing = 1.0;
@@ -984,6 +999,122 @@ bool File::GetImageOrientationPatient( float iop[6] )
    return false;
 }
 
+/**
+  * \brief gets the cosine of image X axis, against patient X axis
+  *        (Sorry, but Python needs it :-( )
+  * @return cosine of image X axis, against patient X axis 
+  */
+float File::GetXCosineOnX()
+{  
+   float iop[6];
+   GetImageOrientationPatient( iop );
+   return(iop[0]);
+}
+   
+/**
+  * \brief gets the cosine of image X axis, against patient Y axis
+  *        (Sorry, but Python needs it :-( )
+  * @return cosine of image X axis, against patient Y axis 
+  */
+float File::GetXCosineOnY()
+{  
+   float iop[6];
+   GetImageOrientationPatient( iop );
+   return(iop[1]);
+}   
+
+/**
+  * \brief gets the cosine of image X axis, against patient Z axis
+  *        (Sorry, but Python needs it :-( )
+  * @return cosine of image X axis, against patient Z axis 
+  */
+float File::GetXCosineOnZ()
+{  
+   float iop[6];
+   GetImageOrientationPatient( iop );
+   return(iop[2]);
+}   
+
+/**
+  * \brief gets the cosine of image Y axis, against patient X axis
+  *        (Sorry, but Python needs it :-( )
+  * @return cosine of image Y axis, against patient X axis 
+  */
+float File::GetYCosineOnX()
+{  
+   float iop[6];
+   GetImageOrientationPatient( iop );
+   return(iop[3]);
+}
+   
+/**
+  * \brief gets the cosine of image Y axis, against patient Y axis
+  *        (Sorry, but Python needs it :-( )
+  * @return cosine of image Y axis, against patient Y axis 
+  */
+float File::GetYCosineOnY()
+{  
+   float iop[6];
+   GetImageOrientationPatient( iop );
+   return(iop[4]);
+}   
+
+/**
+  * \brief gets the cosine of image Y axis, against patient Z axis
+  *        (Sorry, but Python needs it :-( )
+  * @return cosine of image Y axis, against patient Z axis 
+  */
+float File::GetYCosineOnZ()
+{  
+   float iop[6];
+   GetImageOrientationPatient( iop );
+   return(iop[5]);
+}    
+/**
+  * \brief gets the info from 0020,0032 : Image Position Patient
+  *                   or from 0020 0030 : Image Position (RET)
+  *
+  * @param ipp adress of the (3)float array to receive values.
+  *        (defaulted as 0.,0.,0. if nothing -or inconsistent stuff-
+  *        is found.
+  * @return true when one of the tag -with consistent values- is found
+  *         false when nothing or inconsistent stuff - is found
+  */
+bool File::GetImagePositionPatient( float ipp[3] )
+{
+   std::string strImPosiPat;
+   //ipp is supposed to be float[3]
+   ipp[0] = ipp[1] = ipp[2] = 0.;
+
+   // 0020 0032 DS REL Image Position (Patient)
+   strImPosiPat = GetEntryString(0x0020,0x0032);
+   if ( strImPosiPat != GDCM_UNFOUND )
+   {
+      if ( sscanf( strImPosiPat.c_str(), "%f \\ %f \\%f ", 
+          &ipp[0], &ipp[1], &ipp[2]) != 3 )
+      {
+         gdcmWarningMacro( "Wrong Image Position Patient (0020,0032)."
+                        << " Less than 3 values were found." );
+         return false;
+      }
+      return true;
+   }
+   //For ACR-NEMA
+   // 0020 0030 DS REL Image Position (RET)
+   else if ( (strImPosiPat = GetEntryString(0x0020,0x0030)) != GDCM_UNFOUND )
+   {
+      if ( sscanf( strImPosiPat.c_str(), "%f \\ %f \\%f ", 
+          &ipp[0], &ipp[1], &ipp[2]) != 3 )
+      {
+         gdcmWarningMacro( "wrong Image Position Patient (0020,0030). "
+                        << "Less than 3 values were found." );
+         return false;
+      }
+      return true;
+   }
+   return false;
+}
+
 /**
  * \brief   Retrieve the number of Bits Stored (actually used)
  *          (as opposed to number of Bits Allocated)
@@ -1124,21 +1255,29 @@ std::string File::GetPixelType()
       bitsAlloc = "16"; // default and arbitrary value, not to polute the output
    }
 
-   if ( bitsAlloc == "64" )
+   else if ( bitsAlloc == "64" )
    {
       return "FD";
    }
+     // useless since we have to bypass a bug ( >8 && < 16)
    else if ( bitsAlloc == "12" )
    {
       // It will be unpacked
       bitsAlloc = "16";
    }
+   
    else if ( bitsAlloc == "24" )
    {
       // (in order no to be messed up by old RGB images)
       bitsAlloc = "8";
    }
-
+   
+   int i= atoi(bitsAlloc.c_str());  // fix a bug in some headers
+   if ( i > 8 &&  i < 16 )
+   {
+      bitsAlloc = "16";
+   }
+   
    std::string sign;
    if( IsSignedPixelData() )
    {