]> Creatis software - gdcm.git/blobdiff - src/gdcmHeader.cxx
ENH : OpenFile closes the file if it *really* doesn't look like
[gdcm.git] / src / gdcmHeader.cxx
index a3e31d31131425a27f34d2315656f5e5539d2142..5657dd2c6c1344020c7286e3e40f28700f3de780 100644 (file)
@@ -1,4 +1,4 @@
-// $Header: /cvs/public/gdcm/src/Attic/gdcmHeader.cxx,v 1.95 2003/10/06 13:37:25 jpr Exp $
+// $Header: /cvs/public/gdcm/src/Attic/gdcmHeader.cxx,v 1.99 2003/10/10 16:54:25 jpr Exp $
 
 #include "gdcmHeader.h"
 
@@ -44,6 +44,7 @@ gdcmHeader::gdcmHeader(const char *InFilename, bool exception_on_error) {
    Initialise();
    if ( !OpenFile(exception_on_error))
       return;
+
    ParseHeader();
    LoadElements();
    CloseFile();
@@ -72,9 +73,25 @@ gdcmHeader::gdcmHeader(bool exception_on_error) {
     if(!fp)
       throw gdcmFileError("gdcmHeader::gdcmHeader(const char *, bool)");
   }
-  if ( fp )
-     return true;
-  dbg.Verbose(0, "gdcmHeader::gdcmHeader cannot open file", filename.c_str());
+//  char *testEntete = new char[204];
+  guint16 zero;
+  fread(&zero,  (size_t)2, (size_t)1, fp);
+  if ( fp ) {
+    //ACR
+    if( zero == 0x0008 || zero == 0x0800 )
+       return true;
+    //DICOM
+    fseek(fp, 126L, SEEK_CUR);
+    char dicm[4];
+    fread(dicm,  (size_t)4, (size_t)1, fp);
+    if( strncmp(dicm, "DICM", 4) == 0 )
+       return true;
+    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;
 }
 
@@ -614,8 +631,12 @@ void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) {
       FoundLength = 0;
       // Sorry for the patch!  
       // XMedCom did the trick to read some nasty GE images ...
-    if (FoundLength == 13) 
-      FoundLength =10;
+    if (FoundLength == 13)
+      // The following 'if' will be removed when there is no more
+      // images on Creatis HD with a 13 length for Manufacturer...
+      if ( (ElVal->GetGroup() != 0x0008) || (ElVal->GetElement() ) )
+      // end of remove area
+         FoundLength =10;
 
    ElVal->SetLength(FoundLength);
 }
@@ -1101,9 +1122,12 @@ int gdcmHeader::ReplaceOrCreateByNumber(std::string Value,
        // on l'ajoute au ElValSet
        // on affecte une valeur a cette ElValue a l'interieur du ElValSet
        // --> devrait pouvoir etre fait + simplement ???
-       
-   gdcmElValue* nvElValue=NewElValueByNumber(Group, Elem);
-   PubElValSet.Add(nvElValue); 
+   if (CheckIfExistByNumber(Group, Elem) == 0) {
+      gdcmElValue* a =NewElValueByNumber(Group, Elem);
+      if (a == NULL) 
+         return 0;
+      PubElValSet.Add(a);
+   }   
    PubElValSet.SetElValueByNumber(Value, Group, Elem);
    return(1);
 }   
@@ -1752,6 +1776,33 @@ void gdcmHeader::PrintPubDict(std::ostream & os) {
   * @return
   */ 
 int gdcmHeader::Write(FILE * fp, FileType type) {
+
+
+   // TODO : move the following lines (and a lot of others)
+   // to a future function CheckAndCorrectHeader
+
+   if (type == ImplicitVR) {
+      std::string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
+      ReplaceOrCreateByNumber(implicitVRTransfertSyntax,0x0002, 0x0010);
+      
+      //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
+      //      values with a VR of UI shall be padded with a single trailing null
+      //      Dans le cas suivant on doit pader manuellement avec un 0
+      
+      PubElValSet.SetElValueLengthByNumber(18, 0x0002, 0x0010);
+   } 
+
+   if (type == ExplicitVR) {
+      std::string explicitVRTransfertSyntax = "1.2.840.10008.1.2.1";
+      ReplaceOrCreateByNumber(explicitVRTransfertSyntax,0x0002, 0x0010);
+      
+      //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
+      //      values with a VR of UI shall be padded with a single trailing null
+      //      Dans le cas suivant on doit pader manuellement avec un 0
+      
+      PubElValSet.SetElValueLengthByNumber(20, 0x0002, 0x0010);
+   }
+
    return PubElValSet.Write(fp, type);
 }