]> Creatis software - gdcm.git/blobdiff - src/gdcmHeader.cxx
*FIX : gdcmHeader::LoadElements is now based
[gdcm.git] / src / gdcmHeader.cxx
index a398ad35926dfdb3d1d2a461c8b36a2394ae0bbc..79ad3c0bd87135df6d27bb7385ecc8feb02059d2 100644 (file)
@@ -1,4 +1,4 @@
-// $Header: /cvs/public/gdcm/src/Attic/gdcmHeader.cxx,v 1.106 2003/10/31 11:29:59 malaterre Exp $
+// $Header: /cvs/public/gdcm/src/Attic/gdcmHeader.cxx,v 1.113 2003/11/10 14:17:12 jpr Exp $
 
 #include "gdcmHeader.h"
 
@@ -508,7 +508,7 @@ bool gdcmHeader::IsJPEGLossless(void) {
    const char * Transfert = Element->GetValue().c_str();
    if ( memcmp(Transfert+strlen(Transfert)-2 ,"70",2)==0) return true;
    if ( memcmp(Transfert+strlen(Transfert)-2 ,"55",2)==0) return true;
-   if (Element->GetValue() == "1.2.840.10008.1.2.4.57") return true;
+   if (Element->GetValue() == "1.2.840.10008.1.2.4.57")   return true;
 
    return false;
 }
@@ -627,6 +627,9 @@ bool gdcmHeader::IsDicomV3(void) {
  *          applying this heuristic.
  */
 void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) {
+
+   ElVal->SetReadLength(FoundLength); // will be updated only if a bug is found
+   
    if ( FoundLength == 0xffffffff)
       FoundLength = 0;
       
@@ -635,19 +638,36 @@ void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) {
    else if (FoundLength == 13) {
       // The following 'if' will be removed when there is no more
       // images on Creatis HDs with a 13 length for Manufacturer...
-      if ( (ElVal->GetGroup() != 0x0008) || (ElVal->GetElement() != 0x0070))  {
+      if ( (ElVal->GetGroup() != 0x0008) &&  
+           ( (ElVal->GetElement() != 0x0070) || (ElVal->GetElement() != 0x0080) ) ) {
       // end of remove area
          FoundLength =10;
+        ElVal->SetReadLength(10); // a bug is to be fixed
       }
    } 
      // to fix some garbage 'Leonardo' Siemens images
      // May be commented out to avoid overhead
    else if ( (ElVal->GetGroup() == 0x0009) 
        &&
-       ( (ElVal->GetElement() == 0x1113) || (ElVal->GetElement() == 0x1114) ) )
-        FoundLength =4;         
-
-   ElVal->SetLength(FoundLength);
+       ( (ElVal->GetElement() == 0x1113) || (ElVal->GetElement() == 0x1114) ) ){
+        FoundLength =4;
+       ElVal->SetReadLength(4); // a bug is to be fixed 
+   } 
+     // end of fix
+        
+    // to try to 'go inside' SeQuences (with length), and not to ship them        
+    else if ( ElVal->GetVR() == "SQ") { 
+         FoundLength =0;        
+    } 
+    
+    // a SeQuence Element is beginning
+    // Let's forget it's length
+    // (we want to 'go inside')
+    else if(ElVal->GetGroup() == 0xfffe){
+         FoundLength =0;            
+    }
+                 
+   ElVal->SetUsableLength(FoundLength);
 }
 
 /**
@@ -729,7 +749,7 @@ void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) {
             ElVal->SetLength(FindLengthOB());
             return;
          }
-         FixFoundLength(ElVal, length32);        
+         FixFoundLength(ElVal, length32); 
          return;
       }
 
@@ -753,7 +773,7 @@ void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) {
       //   appear when we find the first group with big endian encoding. This
       //   is easy to detect since the length of a "Group Length" tag (the
       //   ones with zero as element number) has to be of 4 (0x0004). When we
-      //   encouter 1024 (0x0400) chances are the encoding changed and we
+      //   encounter 1024 (0x0400) chances are the encoding changed and we
       //   found a group with big endian encoding.
       // We shall use this second strategy. In order to make sure that we
       // can interpret the presence of an apparently big endian encoded
@@ -806,6 +826,7 @@ void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) {
    // not coexist in a Data Set and Data Sets nested within it".]
    // Length is on 4 bytes.
    FixFoundLength(ElVal, ReadInt32());
+   return;
 }
 
 /**
@@ -866,7 +887,7 @@ guint16 gdcmHeader::SwapShort(guint16 a) {
  * @return 
  */
  void gdcmHeader::SkipElementValue(gdcmElValue * ElVal) {
-   SkipBytes(ElVal->GetLength());
+    SkipBytes(ElVal->GetLength());
 }
 
 /**
@@ -907,14 +928,17 @@ void gdcmHeader::LoadElementValue(gdcmElValue * ElVal) {
    // car commencer par les ignorer risque de conduire a qq chose
    // qui pourrait ne pas etre generalisable
    // Well, I'm expecting your code !!!
+   
+   // the test was commented out to 'go inside' the SeQuences
+   // we don't any longer skip them !
     
-   if( vr == "SQ" )
-      SkipLoad = true;
+  // if( vr == "SQ" )  
+  //    SkipLoad = true;
 
-   // A sequence "contains" a set of Elements.  
+   // A SeQuence "contains" a set of Elements.  
    //          (fffe e000) tells us an Element is beginning
    //          (fffe e00d) tells us an Element just ended
-   //          (fffe e0dd) tells us the current SQuence just ended
+   //          (fffe e0dd) tells us the current SeQuence just ended
   
    if( group == 0xfffe )
       SkipLoad = true;
@@ -939,6 +963,7 @@ void gdcmHeader::LoadElementValue(gdcmElValue * ElVal) {
       s << "gdcm::NotLoaded.";
       s << " Address:" << (long)ElVal->GetOffset();
       s << " Length:"  << ElVal->GetLength();
+      s << " x(" << std::hex << ElVal->GetLength() << ")";
       ElVal->SetValue(s.str());
       return;
    }
@@ -946,9 +971,9 @@ void gdcmHeader::LoadElementValue(gdcmElValue * ElVal) {
    // When an integer is expected, read and convert the following two or
    // four bytes properly i.e. as an integer as opposed to a string.
        
-       // pour les elements de Value Multiplicity > 1
-       // on aura en fait une serie d'entiers  
-       // on devrait pouvoir faire + compact (?)
+       // Actually, elements with Value Multiplicity > 1
+       // contain a set of integers (not a single one)         
+       // Any compacter code suggested (?)
                
    if ( IsAnInteger(ElVal) ) {
       guint32 NewInt;
@@ -1115,7 +1140,6 @@ gdcmElValue* gdcmHeader::NewElValueByNumber(guint16 Group, guint16 Elem) {
  */
 int gdcmHeader::ReplaceOrCreateByNumber(std::string Value, 
                                         guint16 Group, guint16 Elem ) {
-
        // TODO : FIXME JPRx
        // curieux, non ?
        // on (je) cree une Elvalue ne contenant pas de valeur
@@ -1226,6 +1250,7 @@ gdcmElValue * gdcmHeader::ReadNextElement(void) {
    NewElVal = NewElValueByNumber(g, n);
    FindVR(NewElVal);
    FindLength(NewElVal);
+       
    if (errno == 1) {
       // Call it quits
       return (gdcmElValue *)0;
@@ -1427,7 +1452,7 @@ std::string gdcmHeader::GetPubElValRepByNumber(guint16 group, guint16 element) {
  * \ingroup gdcmHeader
  * \brief   Searches within the public dictionary for element value of
  *          a given tag.
- * @param   TagName name of the researched element.
+ * @param   TagName name of the searched element.
  * @return  Corresponding element value when it exists, and the string
  *          GDCM_UNFOUND ("gdcm::Unfound") otherwise.
  */
@@ -1444,7 +1469,7 @@ std::string gdcmHeader::GetPubElValByName(std::string TagName) {
  *          to convert the string typed content to caller's native type 
  *          (think of C++ vs Python). The VR is actually of a higher level
  *          of semantics than just the native C++ type.
- * @param   TagName name of the researched element.
+ * @param   TagName name of the searched element.
  * @return  Corresponding element value representation when it exists,
  *          and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
  */
@@ -1459,8 +1484,8 @@ std::string gdcmHeader::GetPubElValRepByName(std::string TagName) {
  * \ingroup gdcmHeader
  * \brief   Searches within elements parsed with the SHADOW dictionary 
  *          for the element value of a given tag.
- * @param   group Group of the researched tag.
- * @param   element Element of the researched tag.
+ * @param   group Group of the searched tag.
+ * @param   element Element of the searched tag.
  * @return  Corresponding element value representation when it exists,
  *          and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
  */
@@ -1477,8 +1502,8 @@ std::string gdcmHeader::GetShaElValByNumber(guint16 group, guint16 element) {
  *          to convert the string typed content to caller's native type 
  *          (think of C++ vs Python). The VR is actually of a higher level
  *          of semantics than just the native C++ type.
- * @param   group Group of the researched tag.
- * @param   element Element of the researched tag.
+ * @param   group Group of the searched tag.
+ * @param   element Element of the searched tag.
  * @return  Corresponding element value representation when it exists,
  *          and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
  */
@@ -1493,7 +1518,7 @@ std::string gdcmHeader::GetShaElValRepByNumber(guint16 group, guint16 element) {
  * \ingroup gdcmHeader
  * \brief   Searches within the elements parsed with the shadow dictionary
  *          for an element value of given tag.
- * @param   TagName name of the researched element.
+ * @param   TagName name of the searched element.
  * @return  Corresponding element value when it exists, and the string
  *          GDCM_UNFOUND ("gdcm::Unfound") otherwise.
  */
@@ -1510,7 +1535,7 @@ std::string gdcmHeader::GetShaElValByName(std::string TagName) {
  *          to convert the string typed content to caller's native type 
  *          (think of C++ vs Python). The VR is actually of a higher level
  *          of semantics than just the native C++ type.
- * @param   TagName name of the researched element.
+ * @param   TagName name of the searched element.
  * @return  Corresponding element value representation when it exists,
  *          and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
  */
@@ -1526,8 +1551,8 @@ std::string gdcmHeader::GetShaElValRepByName(std::string TagName) {
  * \brief   Searches within elements parsed with the public dictionary 
  *          and then within the elements parsed with the shadow dictionary
  *          for the element value of a given tag.
- * @param   group Group of the researched tag.
- * @param   element Element of the researched tag.
+ * @param   group Group of the searched tag.
+ * @param   element Element of the searched tag.
  * @return  Corresponding element value representation when it exists,
  *          and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
  */
@@ -1548,8 +1573,8 @@ std::string gdcmHeader::GetElValByNumber(guint16 group, guint16 element) {
  *          to convert the string typed content to caller's native type 
  *          (think of C++ vs Python). The VR is actually of a higher level
  *          of semantics than just the native C++ type.
- * @param   group Group of the researched tag.
- * @param   element Element of the researched tag.
+ * @param   group Group of the searched tag.
+ * @param   element Element of the searched tag.
  * @return  Corresponding element value representation when it exists,
  *          and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
  */
@@ -1565,7 +1590,7 @@ std::string gdcmHeader::GetElValRepByNumber(guint16 group, guint16 element) {
  * \brief   Searches within elements parsed with the public dictionary 
  *          and then within the elements parsed with the shadow dictionary
  *          for the element value of a given tag.
- * @param   TagName name of the researched element.
+ * @param   TagName name of the searched element.
  * @return  Corresponding element value when it exists,
  *          and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
  */
@@ -1586,7 +1611,7 @@ std::string gdcmHeader::GetElValByName(std::string TagName) {
  *          to convert the string typed content to caller's native type 
  *          (think of C++ vs Python). The VR is actually of a higher level
  *          of semantics than just the native C++ type.
- * @param   TagName name of the researched element.
+ * @param   TagName name of the searched element.
  * @return  Corresponding element value representation when it exists,
  *          and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
  */
@@ -1680,7 +1705,7 @@ void gdcmHeader::ParseHeader(bool exception_on_error) throw(gdcmFormatError) {
    
    rewind(fp);
    CheckSwap();
-   while ( (newElValue = ReadNextElement()) ) {
+   while ( (newElValue = ReadNextElement()) ) { 
       SkipElementValue(newElValue);
       PubElValSet.Add(newElValue);
    }
@@ -1688,7 +1713,7 @@ void gdcmHeader::ParseHeader(bool exception_on_error) throw(gdcmFormatError) {
 
 /**
  * \ingroup gdcmHeader
- * \brief  This predicate, based on hopefully reasonnable heuristics,
+ * \brief  This predicate, based on hopefully reasonable heuristics,
  *         decides whether or not the current gdcmHeader was properly parsed
  *         and contains the mandatory information for being considered as
  *         a well formed and usable image.
@@ -1747,11 +1772,22 @@ gdcmElValue* gdcmHeader::NewManualElValToPubDict(std::string NewTagName,
  *          public tag based hash table.
  */
 void gdcmHeader::LoadElements(void) {
-   rewind(fp);   
-   TagElValueHT ht = PubElValSet.GetTagHt();
-   for (TagElValueHT::iterator tag = ht.begin(); tag != ht.end(); ++tag) {
-      LoadElementValue(tag->second);
-   }
+   rewind(fp);
+   
+   // We don't use any longer the HashTable, since a lot a stuff is missing
+   // when SeQuences were encountered 
+   //  
+   //TagElValueHT ht = PubElValSet.GetTagHt();
+   //for (TagElValueHT::iterator tag = ht.begin(); tag != ht.end(); ++tag) {
+   //     LoadElementValue(tag->second);
+   //}
+   
+     for (std::list<gdcmElValue*>::iterator i = GetListElem().begin();  
+          i != GetListElem().end();
+          ++i){
+        LoadElementValue(*i);   
+     }    
+    
    rewind(fp);
 
    // Load 'non string' values   
@@ -1811,8 +1847,7 @@ void gdcmHeader::PrintPubDict(std::ostream & os) {
   */ 
 int gdcmHeader::Write(FILE * fp, FileType type) {
 
-
-   // TODO : move the following lines (and a lot of others)
+   // TODO : move the following lines (and a lot of others, to be written)
    // to a future function CheckAndCorrectHeader
 
    if (type == ImplicitVR) {
@@ -2011,7 +2046,7 @@ int gdcmHeader::GetBitsAllocated(void) {
 /**
  * \ingroup gdcmHeader
  * \brief   Retrieve the number of Samples Per Pixel
- *          (1 : gray level, 3 : RGB)
+ *          (1 : gray level, 3 : RGB -1 or 3 Planes-)
  * 
  * @return  The encountered number of Samples Per Pixel, 1 by default.
  */
@@ -2065,6 +2100,7 @@ int gdcmHeader::GetPixelSize(void) {
  *          - 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 gdcmHeader::GetPixelType(void) {
@@ -2074,9 +2110,11 @@ std::string gdcmHeader::GetPixelType(void) {
       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) {
@@ -2086,6 +2124,8 @@ std::string gdcmHeader::GetPixelType(void) {
    if (Signed == "0")
       Signed = std::string("U");
    else
+
+std::cout << "GetPixelType : " << BitsAlloc + Signed << std::endl;
       Signed = std::string("S");
 
    return( BitsAlloc + Signed);
@@ -2115,12 +2155,15 @@ std::string gdcmHeader::GetTransferSyntaxName(void) {
 /**
   * \ingroup gdcmHeader
   * \brief tells us if LUT are used
+  * \warning Right now, Segmented xxx Palette Color Lookup Table Data
+  * \        are NOT considered as LUT, since nobody knows
+  *\         how to deal with them
   * @return int acts as a Boolean 
   */
   
 int gdcmHeader::HasLUT(void) {
 
-   // Just hope checking the presence of the LUT Descriptors is enough 
+   // Check the presence of the LUT Descriptors 
    if (GetPubElValByNumber(0x0028,0x1101) == GDCM_UNFOUND)
       return 0;
    // LutDescriptorGreen 
@@ -2128,7 +2171,15 @@ int gdcmHeader::HasLUT(void) {
       return 0;
    // LutDescriptorBlue 
    if (GetPubElValByNumber(0x0028,0x1103) == GDCM_UNFOUND)
+      return 0;
+   //  It is not enough
+   // we check also 
+   if (GetPubElValByNumber(0x0028,0x1201) == GDCM_UNFOUND)
       return 0;  
+   if (GetPubElValByNumber(0x0028,0x1202) == GDCM_UNFOUND)
+      return 0;
+   if (GetPubElValByNumber(0x0028,0x1203) == GDCM_UNFOUND)
+      return 0;   
    return 1;
 }
 
@@ -2243,7 +2294,7 @@ void * gdcmHeader::GetLUTRGBA(void) {
    } 
  // forge the 4 * 8 Bits Red/Green/Blue/Alpha LUT 
    
-  char *LUTRGBA = (char *)calloc(1024,1); // 256 * 4 (R, G, B, Alpha) 
+  unsigned char *LUTRGBA = (unsigned char *)calloc(1024,1); // 256 * 4 (R, G, B, Alpha) 
   if (!LUTRGBA) {
      return NULL;
   }
@@ -2269,8 +2320,9 @@ void * gdcmHeader::GetLUTRGBA(void) {
             // if it works, we shall have to check the 3 Palettes
             // to see which byte is ==0 (first one, or second one)
            // and fix the code
-           // We give up the checking to avoid some overhead    
-  char *a;      
+           // We give up the checking to avoid some overhead 
+           
+  unsigned char *a;      
   int i;
   a= LUTRGBA+0;
   for(i=0;i<lengthR;i++) {
@@ -2288,13 +2340,14 @@ void * gdcmHeader::GetLUTRGBA(void) {
      a+=4;       
   }  
   a= LUTRGBA+3;
-  for(i=0;i<255;i++) {
+  for(i=0;i<256;i++) {
      *a = 1; // Alpha component
      a+=4; 
   } 
-    
-// WHY does it seg fault ?!?
-//free(LutR); free(LutB); free(LutG); printf ("libere\n");
+      
+//How to free the now useless LUTs?
+//
+//free(LutR); free(LutB); free(LutG);
 
   return(LUTRGBA);   
 }