]> Creatis software - gdcm.git/blobdiff - src/gdcmHeader.cxx
Zilch.
[gdcm.git] / src / gdcmHeader.cxx
index 04c501ac428fe735bb535efa7ed7a16803210d98..9d7f6a115672e8ae28a2315c33709727dadc2f8c 100644 (file)
@@ -1,32 +1,31 @@
 // gdcmHeader.cxx
 //-----------------------------------------------------------------------------
-#include "gdcmHeader.h"
-
 #include <stdio.h>
 #include <cerrno>
 #include <cctype>    // for isalpha
+#include <vector>
 
+#include "gdcmHeader.h"
+#include "gdcmGlobal.h"
 #include "gdcmUtil.h"
+#include "gdcmDebug.h"
 #include "gdcmTS.h"
 
-
 //-----------------------------------------------------------------------------
 // Constructor / Destructor
 /**
- * \ingroup gdcmHeader
  * \brief  Constructor 
- * @param   InFilename
- * @param   exception_on_error
- * @param   enable_sequences = true to allow the header 
- *          to be parsed *inside* the SeQuences, 
- *          when they have an actual length 
- * @param   ignore_shadow = true if user wants to skip shadow groups 
- *           during parsing, to save memory space        
+ * @param  InFilename name of the file whose header we want to analyze
+ * @param  exception_on_error whether we want to throw an exception or not
+ * @param  enable_sequences = true to allow the header 
+ *         to be parsed *inside* the SeQuences, when they have an actual length 
+ * @param  ignore_shadow = true if user wants to skip shadow groups 
+ *         during parsing, to save memory space
  */
 gdcmHeader::gdcmHeader(const char *InFilename, 
                        bool exception_on_error,
                        bool enable_sequences, 
-                      bool ignore_shadow):
+                       bool ignore_shadow):
    gdcmParser(InFilename,exception_on_error,enable_sequences,ignore_shadow)
 { 
    
@@ -55,15 +54,14 @@ gdcmHeader::gdcmHeader(const char *InFilename,
          NumPixel = 0x1010;
       else
          NumPixel = 0x0010;
-        
+
       TagKey key = gdcmDictEntry::TranslateToKey(GrPixel, NumPixel);
       countGrPixel = GetEntry().count(key);
 }
 
 /**
- * \ingroup gdcmHeader
  * \brief Constructor  
- * @param   exception_on_error
+ * @param exception_on_error whether we want to throw an exception or not
  */
 gdcmHeader::gdcmHeader(bool exception_on_error) :
    gdcmParser(exception_on_error)
@@ -576,7 +574,7 @@ unsigned char * gdcmHeader::GetLUTRGBA(void) {
 
 //  if Photometric Interpretation # PALETTE COLOR, no LUT to be done
    if (GetEntryByNumber(0x0028,0x0004) != "PALETTE COLOR ") {
-       return NULL;
+      return NULL;
    }  
    int lengthR, debR, nbitsR;
    int lengthG, debG, nbitsG;
@@ -626,7 +624,7 @@ unsigned char * gdcmHeader::GetLUTRGBA(void) {
                          GetEntryVoidAreaByNumber(0x0028,0x1203); 
    
    if (!lutR || !lutG || !lutB ) {
-       return NULL;
+      return NULL;
    } 
    // forge the 4 * 8 Bits Red/Green/Blue/Alpha LUT 
    
@@ -635,7 +633,7 @@ unsigned char * gdcmHeader::GetLUTRGBA(void) {
       return NULL;
    }
    memset(LUTRGBA, 0, 1024);
-       // Bits Allocated
+   // Bits Allocated
    int nb;
    std::string str_nb = GetEntryByNumber(0x0028,0x0100);
    if (str_nb == GDCM_UNFOUND ) {
@@ -716,17 +714,17 @@ std::string gdcmHeader::GetTransfertSyntaxName(void) {
 void gdcmHeader::SetImageDataSize(size_t ImageDataSize) {
    std::string content1;
    char car[20];
-       
+
    // Assumes HeaderEntry (GrPixel, NumPixel) is unique ...   
-   // TODO deal with multiplicity (see gdcmData/icone.dcm)     
+   // TODO deal with multiplicity (see gdcmData/icone.dcm)
    sprintf(car,"%d",ImageDataSize);
  
    gdcmHeaderEntry *a = GetHeaderEntryByNumber(GrPixel, NumPixel);
    a->SetLength(ImageDataSize);
-               
+
    ImageDataSize+=8;
    sprintf(car,"%d",ImageDataSize);
-   content1=car;       
+   content1=car;
    SetEntryByNumber(content1, GrPixel, NumPixel);
 }
 
@@ -747,30 +745,30 @@ void gdcmHeader::SetImageDataSize(size_t ImageDataSize) {
    s1=this->GetEntryByNumber(0x0010,0x0010);
    s2=header.GetEntryByNumber(0x0010,0x0010);
    if(s1 < s2)
-          return(true);
+      return(true);
    else if(s1 > s2)
-          return(false);
+      return(false);
    else
    {
       // Patient ID
       s1=this->GetEntryByNumber(0x0010,0x0020);
       s2=header.GetEntryByNumber(0x0010,0x0020);
       if (s1 < s2)
-             return(true);
+         return(true);
       else if (s1 > s2)
          return(1);
       else
       {
-             // Study Instance UID
+         // Study Instance UID
          s1=this->GetEntryByNumber(0x0020,0x000d);
          s2=header.GetEntryByNumber(0x0020,0x000d);
          if (s1 < s2)
-                return(true);
+            return(true);
          else if(s1 > s2)
-                return(false);
+            return(false);
          else
          {
-                // Serie Instance UID          
+            // Serie Instance UID
             s1=this->GetEntryByNumber(0x0020,0x000e);
             s2=header.GetEntryByNumber(0x0020,0x000e);
             if (s1 < s2)
@@ -783,6 +781,36 @@ void gdcmHeader::SetImageDataSize(size_t ImageDataSize) {
    return(false);
 }
 
+bool gdcmHeader::WriteEntry(gdcmHeaderEntry *tag, FILE *_fp,FileType type)
+{
+   guint32 length = tag->GetLength();
+                                                                                
+   // The value of a tag MUST (see the DICOM norm) be an odd number of
+   // bytes. When this is not the case, pad with an additional byte:
+   if(length%2==1)
+   {
+      tag->SetValue(tag->GetValue()+"\0");
+      tag->SetLength(tag->GetReadLength()+1);
+   }
+                                                                                
+   WriteEntryTagVRLength(tag, _fp, type);
+                                                                                
+   // Pixels are never loaded in the element !
+   // we stop writting when Pixel are processed
+   // FIX : we loose trailing elements (RAB, right now)
+   guint16 el     = tag->GetElement();
+   guint16 group  = tag->GetGroup();
+   int compte =0;
+   if ((group == GrPixel) && (el == NumPixel) ) {
+      compte++;
+      if (compte == countGrPixel) {// we passed *all* the GrPixel,NumPixel
+         return false;
+      }
+   }
+   WriteEntryValue(tag, _fp, type);
+   return true;
+}
+
 //-----------------------------------------------------------------------------
 // Protected
 
@@ -790,7 +818,6 @@ void gdcmHeader::SetImageDataSize(size_t ImageDataSize) {
  * \ingroup   gdcmHeader
  * \brief anonymize a Header (removes Patient's personal info)
  *        (read the code to see which ones ...)
- * @param 
  */
 bool gdcmHeader::anonymizeHeader() {
 
@@ -806,7 +833,7 @@ bool gdcmHeader::anonymizeHeader() {
      if (StudyInstanceUID !=GDCM_UNFOUND)
         ReplaceOrCreateByNumber(StudyInstanceUID, 0x0010, 0x0010);
      else
-        ReplaceOrCreateByNumber("anonymised", 0x0010, 0x0010);            
+        ReplaceOrCreateByNumber(std::string("anonymised"), 0x0010, 0x0010);
   }
   
   // Just for fun :-(
@@ -861,7 +888,7 @@ bool gdcmHeader::anonymizeHeader() {
 //300a 022c DA RT Air Kerma Rate Reference Date
 //300e 0004 DA RT Review Date
  return true;  
- }
+}
 //-----------------------------------------------------------------------------
 // Private