]> Creatis software - gdcm.git/blobdiff - src/gdcmHeader.cxx
* src/gdcmDict.cxx, gdcmElValSet.cxx : bug fix under windows for prints.
[gdcm.git] / src / gdcmHeader.cxx
index 9cdc5beb8606f7a56c9e7a1e413d26064378a8ca..ebf321f2274d3bd122dba02e97794884db2340a5 100644 (file)
@@ -1,4 +1,4 @@
-// $Header: /cvs/public/gdcm/src/Attic/gdcmHeader.cxx,v 1.112 2003/11/10 09:21:40 jpr Exp $
+// $Header: /cvs/public/gdcm/src/Attic/gdcmHeader.cxx,v 1.122 2004/01/13 11:13:08 regrain Exp $
 
 #include "gdcmHeader.h"
 
 #include <netinet/in.h>
 #endif
 #include <cctype>    // for isalpha
-#include <sstream>
+
+#ifdef GDCM_NO_ANSI_STRING_STREAM
+#  include <strstream>
+#  define  ostringstream ostrstream
+# else
+#  include <sstream>
+#endif
+
 #include "gdcmUtil.h"
 #include "gdcmTS.h"
 
 // Refer to gdcmHeader::CheckSwap()
 #define HEADER_LENGTH_TO_READ       256
 // Refer to gdcmHeader::SetMaxSizeLoadElementValue()
-//#define _MaxSizeLoadElementValue_   1024
 #define _MaxSizeLoadElementValue_   4096
 /**
  * \ingroup gdcmHeader
@@ -38,7 +44,14 @@ void gdcmHeader::Initialise(void) {
  * @param   InFilename
  * @param   exception_on_error
  */
-gdcmHeader::gdcmHeader(const char *InFilename, bool exception_on_error) {
+gdcmHeader::gdcmHeader(const char *InFilename, 
+                       bool exception_on_error,
+                       bool enable_sequences ) {
+   if (enable_sequences)
+      enableSequences = 1;
+   else
+      enableSequences = 0;
+   
    SetMaxSizeLoadElementValue(_MaxSizeLoadElementValue_);
    filename = InFilename;
    Initialise();
@@ -65,7 +78,7 @@ gdcmHeader::gdcmHeader(bool exception_on_error) {
  * @param   exception_on_error
  * @return  
  */
- bool gdcmHeader::OpenFile(bool exception_on_error)
+FILE *gdcmHeader::OpenFile(bool exception_on_error)
   throw(gdcmFileError) {
   fp=fopen(filename.c_str(),"rb");
   if(exception_on_error) {
@@ -79,20 +92,22 @@ gdcmHeader::gdcmHeader(bool exception_on_error) {
 
     //ACR -- or DICOM with no Preamble
     if( zero == 0x0008 || zero == 0x0800 || zero == 0x0002 || zero == 0x0200)
-       return true;
+       return(fp);
+
     //DICOM
     fseek(fp, 126L, SEEK_CUR);
     char dicm[4];
     fread(dicm,  (size_t)4, (size_t)1, fp);
     if( memcmp(dicm, "DICM", 4) == 0 )
-       return true;
+       return(fp);
+
     fclose(fp);
     dbg.Verbose(0, "gdcmHeader::gdcmHeader not DICOM/ACR", filename.c_str());
   }
   else {
     dbg.Verbose(0, "gdcmHeader::gdcmHeader cannot open file", filename.c_str());
   }
-  return false;
+  return(NULL);
 }
 
 /**
@@ -117,6 +132,8 @@ gdcmHeader::~gdcmHeader (void) {
   Dicts    =   (gdcmDictSet*)0;
   RefPubDict = (gdcmDict*)0;
   RefShaDict = (gdcmDict*)0;
+
+
   return;
 }
 
@@ -627,17 +644,23 @@ bool gdcmHeader::IsDicomV3(void) {
  *          applying this heuristic.
  */
 void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) {
-   if ( FoundLength == 0xffffffff)
+
+   ElVal->SetReadLength(FoundLength); // will be updated only if a bug is found
+                    
+   if ( FoundLength == 0xffffffff) {  
       FoundLength = 0;
+   }
       
       // Sorry for the patch!  
       // XMedCom did the trick to read some nasty GE images ...
    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
@@ -645,12 +668,14 @@ void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) {
    else if ( (ElVal->GetGroup() == 0x0009) 
        &&
        ( (ElVal->GetElement() == 0x1113) || (ElVal->GetElement() == 0x1114) ) ){
-        FoundLength =4; 
+        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        
+    // to try to 'go inside' SeQuences (with length), and not to skip them        
     else if ( ElVal->GetVR() == "SQ") { 
+       if (enableSequences)    // only if the user does want to !
          FoundLength =0;        
     } 
     
@@ -661,7 +686,7 @@ void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) {
          FoundLength =0;            
     }
                  
-   ElVal->SetLength(FoundLength);
+   ElVal->SetUsableLength(FoundLength);
 }
 
 /**
@@ -724,7 +749,7 @@ void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) {
    std::string  vr = ElVal->GetVR();
    guint16 length16;
    if( (element == 0x0010) && (group == 0x7fe0) ) {
-      dbg.SetDebug(0);
+      dbg.SetDebug(-1);
       dbg.Verbose(2, "gdcmHeader::FindLength: ",
                      "we reached 7fe0 0010");
    }   
@@ -744,7 +769,6 @@ void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) {
             return;
          }
          FixFoundLength(ElVal, length32); 
-
          return;
       }
 
@@ -768,7 +792,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
@@ -821,6 +845,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;
 }
 
 /**
@@ -997,6 +1022,9 @@ void gdcmHeader::LoadElementValue(gdcmElValue * ElVal) {
             }
          }
       }                                        
+#ifdef GDCM_NO_ANSI_STRING_STREAM
+      s << std::ends; // to avoid oddities on Solaris
+#endif //GDCM_NO_ANSI_STRING_STREAM
       ElVal->SetValue(s.str());
       return;  
    }
@@ -1244,6 +1272,7 @@ gdcmElValue * gdcmHeader::ReadNextElement(void) {
    NewElVal = NewElValueByNumber(g, n);
    FindVR(NewElVal);
    FindLength(NewElVal);
+       
    if (errno == 1) {
       // Call it quits
       return (gdcmElValue *)0;
@@ -1704,6 +1733,16 @@ void gdcmHeader::ParseHeader(bool exception_on_error) throw(gdcmFormatError) {
    }
 }
 
+/**
+ * \ingroup gdcmHeader
+ * \brief  
+ * @return 
+ */
+FileType gdcmHeader::GetFileType(void)
+{
+   return(filetype);
+}
+
 /**
  * \ingroup gdcmHeader
  * \brief  This predicate, based on hopefully reasonable heuristics,
@@ -1768,14 +1807,14 @@ void gdcmHeader::LoadElements(void) {
    rewind(fp);
    
    // We don't use any longer the HashTable, since a lot a stuff is missing
-   // we SeQuences were encountered 
+   // 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();  
+     for (ListTag::iterator i = GetListElem().begin();  
           i != GetListElem().end();
           ++i){
         LoadElementValue(*i);   
@@ -1886,16 +1925,12 @@ void * gdcmHeader::LoadElementVoidArea(guint16 Group, guint16 Elem) {
    int l=Element->GetLength();
    void * a = malloc(l);
    if(!a) {
-       std::cout << "Big Broblem (LoadElementVoidArea, malloc) " 
-                 << std::hex << Group << " " << Elem << std::endl;
        return NULL;
    }  
    /* int res = */ PubElValSet.SetVoidAreaByNumber(a, Group, Elem);
    // TODO check the result 
    size_t l2 = fread(a, 1, l ,fp);
    if(l != l2) {
-       std::cout << "Big Broblem (LoadElementVoidArea, fread) " 
-                 << std::hex << Group << " " << Elem << std::endl;
        free(a);
        return NULL;
    }
@@ -2117,8 +2152,6 @@ 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);
@@ -2220,7 +2253,7 @@ int gdcmHeader::GetLUTNbits(void) {
   * @return Lookup Table RGBA
   */ 
   
-void * gdcmHeader::GetLUTRGBA(void) {
+unsigned char * gdcmHeader::GetLUTRGBA(void) {
 // Not so easy : see 
 // http://www.barre.nom.fr/medical/dicom2/limitations.html#Color%20Lookup%20Tables
 // and  OT-PAL-8-face.dcm
@@ -2230,9 +2263,7 @@ void * gdcmHeader::GetLUTRGBA(void) {
    if (gdcmHeader::GetPubElValByNumber(0x0028,0x0004) != "PALETTE COLOR ") {
        return NULL;
    }  
-   
-   void * LutR,*LutG,*LutB;
-   int l;
+
    int lengthR, debR, nbitsR;
    int lengthG, debG, nbitsG;
    int lengthB, debB, nbitsB;
@@ -2345,3 +2376,32 @@ void * gdcmHeader::GetLUTRGBA(void) {
   return(LUTRGBA);   
 } 
  
+/////////////////////////////////////////////////////////////////
+/**
+ * \ingroup   gdcmFile
+ * \brief Sets the Pixel Area size in the Header
+ *        --> not-for-rats function
+ * 
+ * \warning WARNING doit-etre etre publique ? 
+ * TODO : y aurait il un inconvenient à fusionner ces 2 fonctions
+ *
+ * @param ImageDataSize new Pixel Area Size
+ *        warning : nothing else is checked
+ */
+
+void gdcmHeader::SetImageDataSize(size_t ImageDataSize) {
+   std::string content1;
+   char car[20];       
+   // Assumes ElValue (0x7fe0, 0x0010) exists ...      
+   sprintf(car,"%d",ImageDataSize);
+   gdcmElValue *a = GetElValueByNumber(0x7fe0, 0x0010);
+   a->SetLength(ImageDataSize);
+               
+   ImageDataSize+=8;
+   sprintf(car,"%d",ImageDataSize);
+   content1=car;       
+   SetPubElValByNumber(content1, 0x7fe0, 0x0000);
+}
+
+