]> Creatis software - gdcm.git/blobdiff - src/gdcmHeader.cxx
* Memory link hunt (by using valgrind --leak-check=yes PrintHeader).
[gdcm.git] / src / gdcmHeader.cxx
index 4f2d2affb872a154c12ec4bd148249eb939ec6dd..66b97ce2b8ac093cc7de8d98392bc7793040cebd 100644 (file)
 #include "gdcmUtil.h"
 #include "gdcmHeader.h"
 
-#include "iddcmjpeg.h"
-
 // Refer to gdcmHeader::CheckSwap()
 #define HEADER_LENGTH_TO_READ       256
 // Refer to gdcmHeader::SetMaxSizeLoadElementValue()
 #define _MaxSizeLoadElementValue_   1024
 
-VRHT * gdcmHeader::dicom_vr = (VRHT*)0;
+gdcmVR * gdcmHeader::dicom_vr = (gdcmVR*)0;
 
 void gdcmHeader::Initialise(void) {
    if (!gdcmHeader::dicom_vr)
-      InitVRDict();
+      gdcmHeader::dicom_vr = gdcmGlobal::GetVR();
    Dicts = new gdcmDictSet();
    RefPubDict = Dicts->GetDefaultPubDict();
    RefShaDict = (gdcmDict*)0;
 }
 
-gdcmHeader::gdcmHeader(const char *InFilename, bool exception_on_error) 
-  throw(gdcmFileError) {
+gdcmHeader::gdcmHeader(const char *InFilename, bool exception_on_error) {
   SetMaxSizeLoadElementValue(_MaxSizeLoadElementValue_);
   filename = InFilename;
   Initialise();
-  fp=fopen(InFilename,"rb");
+  OpenFile(exception_on_error);
+  ParseHeader();
+  LoadElements();
+  CloseFile();
+}
+
+bool gdcmHeader::OpenFile(bool exception_on_error)
+  throw(gdcmFileError) {
+  fp=fopen(filename.c_str(),"rb");
   if(exception_on_error) {
     if(!fp)
       throw gdcmFileError("gdcmHeader::gdcmHeader(const char *, bool)");
   }
   else
-    dbg.Error(!fp, "gdcmHeader::gdcmHeader cannot open file", InFilename);
-  ParseHeader();
-  LoadElements();
+    dbg.Error(!fp, "gdcmHeader::gdcmHeader cannot open file", filename.c_str());
+  if ( fp )
+     return true;
+  return false;
 }
 
+bool gdcmHeader::CloseFile(void) {
+  int closed = fclose(fp);
+  fp = (FILE *)0;
+  if (! closed)
+     return false;
+  return true;
+}
 
 gdcmHeader::~gdcmHeader (void) {
+   delete Dicts;
    //FIXME obviously there is much to be done here !
-   fclose(fp);
    return;
 }
 
-void gdcmHeader::InitVRDict (void) {
-   if (dicom_vr) {
-      dbg.Verbose(0, "gdcmHeader::InitVRDict:", "VR dictionary allready set");
-      return;
-   }
-   VRHT *vr = new VRHT;
-   (*vr)["AE"] = "Application Entity";       // At most 16 bytes
-   (*vr)["AS"] = "Age String";               // Exactly 4 bytes
-   (*vr)["AT"] = "Attribute Tag";            // 2 16-bit unsigned short integers
-   (*vr)["CS"] = "Code String";              // At most 16 bytes
-   (*vr)["DA"] = "Date";                     // Exactly 8 bytes
-   (*vr)["DS"] = "Decimal String";           // At most 16 bytes
-   (*vr)["DT"] = "Date Time";                // At most 26 bytes
-   (*vr)["FL"] = "Floating Point Single";    // 32-bit IEEE 754:1985 float
-   (*vr)["FD"] = "Floating Point Double";    // 64-bit IEEE 754:1985 double
-   (*vr)["IS"] = "Integer String";           // At most 12 bytes
-   (*vr)["LO"] = "Long String";              // At most 64 chars
-   (*vr)["LT"] = "Long Text";                // At most 10240 chars
-   (*vr)["OB"] = "Other Byte String";        // String of bytes (vr independant)
-   (*vr)["OW"] = "Other Word String";        // String of 16-bit words (vr dep)
-   (*vr)["PN"] = "Person Name";              // At most 64 chars
-   (*vr)["SH"] = "Short String";             // At most 16 chars
-   (*vr)["SL"] = "Signed Long";              // Exactly 4 bytes
-   (*vr)["SQ"] = "Sequence of Items";        // Not Applicable
-   (*vr)["SS"] = "Signed Short";             // Exactly 2 bytes
-   (*vr)["ST"] = "Short Text";               // At most 1024 chars
-   (*vr)["TM"] = "Time";                     // At most 16 bytes
-   (*vr)["UI"] = "Unique Identifier";        // At most 64 bytes
-   (*vr)["UL"] = "Unsigned Long ";           // Exactly 4 bytes
-   (*vr)["UN"] = "Unknown";                  // Any length of bytes
-   (*vr)["US"] = "Unsigned Short ";          // Exactly 2 bytes
-   (*vr)["UT"] = "Unlimited Text";           // At most 2^32 -1 chars
-   dicom_vr = vr; 
-}
+// Fourth semantics:
+// CMD      Command        
+// META     Meta Information 
+// DIR      Directory
+// ID
+// PAT      Patient
+// ACQ      Acquisition
+// REL      Related
+// IMG      Image
+// SDY      Study
+// VIS      Visit 
+// WAV      Waveform
+// PRC
+// DEV      Device
+// NMI      Nuclear Medicine
+// MED
+// BFS      Basic Film Session
+// BFB      Basic Film Box
+// BIB      Basic Image Box
+// BAB
+// IOB
+// PJ
+// PRINTER
+// RT       Radio Therapy
+// DVH   
+// SSET
+// RES      Results
+// CRV      Curve
+// OLY      Overlays
+// PXL      Pixels
+//
 
 /**
  * \ingroup gdcmHeader
@@ -241,19 +251,6 @@ void gdcmHeader::SwitchSwapToBigEndian(void) {
       sw = 3412;
 }
 
-void gdcmHeader::GetPixels(size_t lgrTotale, void* _Pixels) {
-   size_t pixelsOffset; 
-   pixelsOffset = GetPixelOffset();
-   fseek(fp, pixelsOffset, SEEK_SET);
-   if (IsJPEGLossless()) {
-       _Pixels=_IdDcmJpegRead(fp);  
-   } else {
-       fread(_Pixels, 1, lgrTotale, fp);
-   }
-}
-
-
-
 /**
  * \ingroup   gdcmHeader
  * \brief     Find the value representation of the current tag.
@@ -293,7 +290,7 @@ void gdcmHeader::FindVR( gdcmElValue *ElVal) {
    // CLEANME searching the dicom_vr at each occurence is expensive.
    // PostPone this test in an optional integrity check at the end
    // of parsing or only in debug mode.
-   if ( RealExplicit && !dicom_vr->count(vr) )
+   if ( RealExplicit && !dicom_vr->Count(vr) )
       RealExplicit= false;
 
    if ( RealExplicit ) {
@@ -617,7 +614,8 @@ void gdcmHeader::FindLength(gdcmElValue * ElVal) {
          // and the dictionary entry depending on them.
          guint16 CorrectGroup   = SwapShort(ElVal->GetGroup());
          guint16 CorrectElem    = SwapShort(ElVal->GetElement());
-         gdcmDictEntry * NewTag = GetDictEntryByKey(CorrectGroup, CorrectElem);
+         gdcmDictEntry * NewTag = GetDictEntryByNumber(CorrectGroup,
+                                                       CorrectElem);
          if (!NewTag) {
             // This correct tag is not in the dictionary. Create a new one.
             NewTag = new gdcmDictEntry(CorrectGroup, CorrectElem);
@@ -803,7 +801,7 @@ void gdcmHeader::LoadElementValue(gdcmElValue * ElVal) {
                return; 
        }
    
-   // FIXME The exact size should be length if we move to strings or whatever
+   // We need an additional byte for storing \0 that is not on disk
    char* NewValue = (char*)malloc(length+1);
    if( !NewValue) {
       dbg.Verbose(1, "LoadElementValue: Failed to allocate NewValue");
@@ -819,6 +817,7 @@ void gdcmHeader::LoadElementValue(gdcmElValue * ElVal) {
       return;
    }
    ElVal->SetValue(NewValue);
+   free(NewValue);
 }
 
 /**
@@ -864,16 +863,6 @@ guint32 gdcmHeader::ReadInt32(void) {
    return g;
 }
 
-
-
-
-//
-// TODO : JPR Pour des raisons d'homogeneité de noms, 
-// remplacer les quelques GetxxxByKey 
-// par des GetxxxByNumber, lorsqu'on passe gr, el en param.
-// il peut etre interessant de rajouter des GetxxxByKey, auxquels on passe *vraiment* une TagKey
-//
-
 /**
  * \ingroup gdcmHeader
  * \brief   Build a new Element Value from all the low level arguments. 
@@ -882,15 +871,15 @@ guint32 gdcmHeader::ReadInt32(void) {
  * @param   Group group   of the underlying DictEntry
  * @param   Elem  element of the underlying DictEntry
  */
-gdcmElValue* gdcmHeader::NewElValueByKey(guint16 Group, guint16 Elem) {
+gdcmElValue* gdcmHeader::NewElValueByNumber(guint16 Group, guint16 Elem) {
    // Find out if the tag we encountered is in the dictionaries:
-   gdcmDictEntry * NewTag = GetDictEntryByKey(Group, Elem);
+   gdcmDictEntry * NewTag = GetDictEntryByNumber(Group, Elem);
    if (!NewTag)
       NewTag = new gdcmDictEntry(Group, Elem);
 
    gdcmElValue* NewElVal = new gdcmElValue(NewTag);
    if (!NewElVal) {
-      dbg.Verbose(1, "gdcmHeader::NewElValueByKey",
+      dbg.Verbose(1, "gdcmHeader::NewElValueByNumber",
                   "failed to allocate gdcmElValue");
       return (gdcmElValue*)0;
    }
@@ -904,7 +893,7 @@ gdcmElValue* gdcmHeader::NewElValueByKey(guint16 Group, guint16 Elem) {
  */
 int gdcmHeader::ReplaceOrCreateByNumber(string Value, guint16 Group, guint16 Elem ) {
 
-       gdcmElValue* nvElValue=NewElValueByKey(Group, Elem);
+       gdcmElValue* nvElValue=NewElValueByNumber(Group, Elem);
        PubElValSet.Add(nvElValue);     
        PubElValSet.SetElValueByNumber(Value, Group, Elem);
        return(1);
@@ -950,7 +939,7 @@ gdcmElValue * gdcmHeader::ReadNextElement(void) {
       // has to be considered as finished.
       return (gdcmElValue *)0;
    
-   NewElVal = NewElValueByKey(g, n);
+   NewElVal = NewElValueByNumber(g, n);
    FindVR(NewElVal);
    FindLength(NewElVal);
    if (errno == 1)
@@ -1018,7 +1007,8 @@ size_t gdcmHeader::GetPixelOffset(void) {
       numPixel = 0x1010;
    else
       numPixel = 0x0010;
-   gdcmElValue* PixelElement = PubElValSet.GetElementByNumber(grPixel, numPixel);
+   gdcmElValue* PixelElement = PubElValSet.GetElementByNumber(grPixel,
+                                                              numPixel);
    if (PixelElement)
       return PixelElement->GetOffset();
    else
@@ -1035,19 +1025,20 @@ size_t gdcmHeader::GetPixelOffset(void) {
  * @param   element element of the searched DictEntry
  * @return  Corresponding DictEntry when it exists, NULL otherwise.
  */
-gdcmDictEntry * gdcmHeader::GetDictEntryByKey(guint16 group, guint16 element) {
+gdcmDictEntry * gdcmHeader::GetDictEntryByNumber(guint16 group,
+                                                 guint16 element) {
    gdcmDictEntry * found = (gdcmDictEntry*)0;
    if (!RefPubDict && !RefShaDict) {
       dbg.Verbose(0, "gdcmHeader::GetDictEntry",
                      "we SHOULD have a default dictionary");
    }
    if (RefPubDict) {
-      found = RefPubDict->GetTagByKey(group, element);
+      found = RefPubDict->GetTagByNumber(group, element);
       if (found)
          return found;
    }
    if (RefShaDict) {
-      found = RefShaDict->GetTagByKey(group, element);
+      found = RefShaDict->GetTagByNumber(group, element);
       if (found)
          return found;
    }