]> Creatis software - gdcm.git/blobdiff - src/gdcmHeaderHelper.cxx
update Changelog
[gdcm.git] / src / gdcmHeaderHelper.cxx
index cb4b3559e582b36b460afb458a66ac30cd3690ba..25d2d80c55d1e0dde855a0ebae8ff24f7e3fc5d2 100644 (file)
@@ -1,4 +1,4 @@
-// $Header: /cvs/public/gdcm/src/Attic/gdcmHeaderHelper.cxx,v 1.10 2003/10/03 14:26:11 jpr Exp $
+// $Header: /cvs/public/gdcm/src/Attic/gdcmHeaderHelper.cxx,v 1.15 2003/11/03 10:49:31 jpr Exp $
 
 #include "gdcmHeaderHelper.h"
 
@@ -79,6 +79,12 @@ gdcmHeaderHelper::gdcmHeaderHelper(const char *InFilename,
  *
  */
 int gdcmHeaderHelper::GetPixelSize() {
+
+     // 0028 0100 US IMG Bits Allocated
+     // (in order no to be messed up by old RGB images)
+   if (gdcmHeader::GetPubElValByNumber(0x0028,0x0100) == "24")
+      return 3;
+        
    std::string PixelType = GetPixelType();
    if (PixelType == "8U"  || PixelType == "8S")
       return 1;
@@ -93,22 +99,65 @@ int gdcmHeaderHelper::GetPixelSize() {
 //----------------------------------------------------------------------------
 /**
   * \ingroup gdcmHeaderHelper
-  * \brief gets the info from 0028,0004 : Photometric Interp
-  * \           else 1.
-  * @return 1 if Gray level, 3 if Color
+  * \brief This function is intended to user who doesn't whan 
+  * \ to have to manage a LUT and expects to get an RBG Pixel image
+  * \ (or a monochrome one ...) 
+  * \warning to be used with GetImagePixels()
+  * @return 1 if Gray level, 3 if Color (RGB, YBR or PALETTE COLOR)
   */
 int gdcmHeaderHelper::GetNumberOfScalarComponents() {
-      std::string PhotometricInterpretation = 
-                  gdcmHeader::GetPubElValByNumber(0x0028,0x0004);
 
-// The compiler will optimze, if it feels like !
+   if (GetSamplesPerPixel() ==3)
+      return 3;
+      
+     // 0028 0100 US IMG Bits Allocated
+     // (in order no to be messed up by old RGB images)
+   if (gdcmHeader::GetPubElValByNumber(0x0028,0x0100) == "24")
+      return 3;
+       
+   std::string PhotometricInterpretation = 
+                  gdcmHeader::GetPubElValByNumber(0x0028,0x0004);
 
-      if (PhotometricInterpretation == GDCM_UNFOUND) return 1;
-      if (PhotometricInterpretation == "MONOCHROME1") return 1;
-      if (PhotometricInterpretation == "MONOCHROME2") return 1;
+   if ( ( PhotometricInterpretation == "PALETTE COLOR ") ) {
+      if (HasLUT())   // PALETTE COLOR is NOT enough
+         return 3;
+      else
+         return 1;      
+   }   
+                 
+      //beware of trailing space at end of string                                              
+   if (PhotometricInterpretation.find(GDCM_UNFOUND) < 
+                           PhotometricInterpretation.length() || 
+       PhotometricInterpretation.find("MONOCHROME1") < 
+                           PhotometricInterpretation.length() || 
+       PhotometricInterpretation.find("MONOCHROME2") < 
+                           PhotometricInterpretation.length() ) 
+       return 1;
+    else
+    // we assume that *all* kinds of YBR are dealt with
+      return 3;
+}
 
+//----------------------------------------------------------------------------
+/**
+  * \ingroup gdcmHeaderHelper
+  * \brief This function is intended to user that DOESN'T want 
+  * \to get RGB pixels image when it's stored as a PALETTE COLOR image
+  * \ - the (vtk) user is supposed to know how deal with LUTs - 
+  * \warning to be used with GetImagePixelsRaw()
+  * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
+  */
+int gdcmHeaderHelper::GetNumberOfScalarComponentsRaw() {
+      
+     // 0028 0100 US IMG Bits Allocated
+     // (in order no to be messed up by old RGB images)
+   if (gdcmHeader::GetPubElValByNumber(0x0028,0x0100) == "24")
       return 3;
+
+    // we assume that *all* kinds of YBR are dealt with
+      return GetSamplesPerPixel();
 }
+
 //----------------------------------------------------------------------------
 /**
  * \ingroup gdcmHeaderHelper
@@ -121,6 +170,7 @@ int gdcmHeaderHelper::GetNumberOfScalarComponents() {
  *          - 32U unsigned 32 bit,
  *          - 32S   signed 32 bit,
  * \warning 12 bit images appear as 16 bit.
+ * \        24 bit images appear as 8 bit
  * @return  
  */
 std::string gdcmHeaderHelper::GetPixelType() {
@@ -130,9 +180,11 @@ std::string gdcmHeaderHelper::GetPixelType() {
       dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
       BitsAlloc = std::string("16");
    }
-   if (BitsAlloc == "12")
+   if (BitsAlloc == "12")           // It will be unpacked
       BitsAlloc = std::string("16");
-
+   else if (BitsAlloc == "24")      // (in order no to be messed up
+      BitsAlloc = std::string("8"); // by old RGB images)
+      
    std::string Signed;
    Signed = GetElValByName("Pixel Representation");
    if (Signed == GDCM_UNFOUND) {
@@ -156,15 +208,19 @@ std::string gdcmHeaderHelper::GetPixelType() {
 float gdcmHeaderHelper::GetXSpacing() {
     float xspacing, yspacing;
     std::string StrSpacing = GetPubElValByNumber(0x0028,0x0030);
-
-    if (StrSpacing == GDCM_UNFOUND) {
-       dbg.Verbose(0, "gdcmHeader::GetXSpacing: unfound Pixel Spacing (0028,0030)");
-       return 1.;
-     }
-   if( sscanf( StrSpacing.c_str(), "%f\\%f", &xspacing, &yspacing) != 2)
-     return 0.;
-   //else
-   return xspacing;
+    
+   if (StrSpacing == GDCM_UNFOUND) {
+      dbg.Verbose(0, "gdcmHeader::GetXSpacing: unfound Pixel Spacing (0028,0030)");
+      return 1.;
+    }
+  if( sscanf( StrSpacing.c_str(), "%f\\%f", &yspacing, &xspacing) != 2)
+    return 0.;
+  if (xspacing == 0.) {
+    dbg.Verbose(0, "gdcmHeader::GetYSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
+    // seems to be a bug in the header ...
+    sscanf( StrSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
+  }
+  return xspacing;
 }
 //----------------------------------------------------------------------------
 /**
@@ -181,12 +237,12 @@ float gdcmHeaderHelper::GetYSpacing() {
       dbg.Verbose(0, "gdcmHeader::GetYSpacing: unfound Pixel Spacing (0028,0030)");
       return 1.;
     }
-  if( sscanf( StrSpacing.c_str(), "%f\\%f", &xspacing, &yspacing) != 2)
+  if( sscanf( StrSpacing.c_str(), "%f\\%f", &yspacing, &xspacing) != 2)
     return 0.;
-  if (yspacing == 0.) {
+  if (xspacing == 0.) {
     dbg.Verbose(0, "gdcmHeader::GetYSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
     // seems to be a bug in the header ...
-    sscanf( StrSpacing.c_str(), "%f\\0\\%f", &xspacing, &yspacing);
+    sscanf( StrSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
   }
   return yspacing;
 }