]> Creatis software - gdcm.git/blob - src/gdcmHeader.h
* Memory link hunt (by using valgrind --leak-check=yes PrintHeader).
[gdcm.git] / src / gdcmHeader.h
1 // gdcmHeader.h
2
3 #ifndef GDCMHEADER_H
4 #define GDCMHEADER_H
5
6 #include <map>
7 #include "gdcmCommon.h"
8 #include "gdcmUtil.h"
9 #include "gdcmException.h"
10 #include "gdcmDictSet.h"
11 #include "gdcmElValue.h"
12 #include "gdcmElValSet.h"
13
14 typedef string VRKey;
15 typedef string VRAtr;
16 typedef map<VRKey, VRAtr> VRHT;    // Value Representation Hash Table
17
18 /// The purpose of an instance of gdcmHeader is to act as a container of
19 /// all the DICOM elements and their corresponding values (and
20 /// additionaly the corresponding DICOM dictionary entry) of the header
21 /// of a DICOM file.
22 ///
23 /// The typical usage of instances of class gdcmHeader is to classify a set of
24 /// dicom files according to header information e.g. to create a file hierarchy
25 /// reflecting the Patient/Study/Serie informations, or extracting a given
26 /// SerieId. Accesing the content (image[s] or volume[s]) is beyond the
27 /// functionality of this class and belongs to gdmcFile.
28 /// \note  The various entries of the explicit value representation (VR) shall
29 ///        be managed within a dictionary which is shared by all gdcmHeader
30 ///        instances.
31 /// \note  The gdcmHeader::Set*Tag* family members cannot be defined as
32 ///        protected due to Swig limitations for as Has_a dependency between
33 ///        gdcmFile and gdcmHeader.
34
35 class GDCM_EXPORT gdcmHeader {
36    void SkipBytes(guint32);
37 private:
38    /// Pointer to the Value Representation Hash Table which contains all
39    /// the VR of the DICOM version3 public dictionary. 
40    static gdcmVR *dicom_vr;
41  
42    /// Global dictionary container
43    gdcmDictSet* Dicts;
44    /// Public dictionary used to parse this header
45    gdcmDict* RefPubDict;
46    /// Optional "shadow dictionary" (private elements) used to parse this
47    /// header
48    gdcmDict* RefShaDict;
49
50    /// ELement VALueS parsed with the PUBlic dictionary.
51    gdcmElValSet PubElValSet;
52    /// ELement VALueS parsed with the SHAdow dictionary.
53    gdcmElValSet ShaElValSet;
54    /// Refering underlying filename.
55    string filename; 
56    
57    // FIXME sw should be an enum e.g.
58    //enum EndianType {
59       //LittleEndian, 
60       //BadLittleEndian,
61       //BigEndian, 
62       //BadBigEndian};
63    /// Swap code e.g. little, big, bad-big, bad-little endian). Warning:
64    /// this code is not fixed during header parsing.
65    int sw;
66
67    /// Size treshold above which an element value will NOT be loaded in 
68    /// memory (to avoid loading the image/volume itself). By default,
69    /// this upper bound is fixed to 1024 bytes (which might look reasonable
70    /// when one considers the definition of the various VR contents).
71    guint32 MaxSizeLoadElementValue;
72
73    guint16 ReadInt16(void);
74    guint32 ReadInt32(void);
75    guint16 SwapShort(guint16);
76    guint32 SwapLong(guint32);
77    guint32 FindLengthOB(void);
78    void Initialise(void);
79    void CheckSwap(void);
80    void SwitchSwapToBigEndian(void);
81    // CLEAN ME: NewManualElValToPubDict is NOT called any more.
82    gdcmElValue*  NewManualElValToPubDict(string NewTagName, string VR);
83    void SetMaxSizeLoadElementValue(long);
84
85    gdcmDictEntry * GetDictEntryByNumber(guint16, guint16);
86    gdcmDictEntry * GetDictEntryByName(string name);
87
88    // ElValue related utilities
89    gdcmElValue * ReadNextElement(void);
90    gdcmElValue * NewElValueByNumber(guint16 group, guint16 element);
91    gdcmElValue * NewElValueByName(string name);
92    void FindLength(gdcmElValue *);
93    void FindVR(gdcmElValue *);
94    void LoadElementValue(gdcmElValue *);
95    void LoadElementValueSafe(gdcmElValue *);
96    void SkipElementValue(gdcmElValue *);
97    void FixFoundLength(gdcmElValue*, guint32);
98    bool IsAnInteger(gdcmElValue *);
99    void LoadElements(void);
100    
101 protected:
102    FILE * fp;
103    FileType filetype;
104    bool OpenFile(bool exception_on_error = false)
105      throw(gdcmFileError);
106    bool CloseFile(void);
107    int write(ostream&);   
108    int anonymize(ostream&);  // FIXME : anonymize should be a friend ?
109 public:
110    bool IsReadable(void);
111    bool IsImplicitVRLittleEndianTransferSyntax(void);
112    bool IsExplicitVRLittleEndianTransferSyntax(void);
113    bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
114    bool IsExplicitVRBigEndianTransferSyntax(void);
115    bool IsJPEGBaseLineProcess1TransferSyntax(void);
116    bool IsJPEGExtendedProcess2_4TransferSyntax(void); 
117    bool IsJPEGExtendedProcess3_5TransferSyntax(void);
118    bool IsJPEGSpectralSelectionProcess6_8TransferSyntax(void); 
119    bool IsJPEGLossless(void); 
120    bool IsDicomV3(void); 
121       
122    virtual void ParseHeader(bool exception_on_error = false)
123      throw(gdcmFormatError);
124    gdcmHeader(const char *filename, bool exception_on_error = false);
125    virtual ~gdcmHeader();
126    
127    size_t GetPixelOffset(void);
128    int    GetSwapCode(void) { return sw; }
129
130    // TODO Swig int SetPubDict(string filename);
131    // When some proprietary shadow groups are disclosed, we can set up
132    // an additional specific dictionary to access extra information.
133    // TODO Swig int SetShaDict(string filename);
134
135    string GetPubElValByName(string TagName);
136    string GetPubElValByNumber(guint16 group, guint16 element);
137    string GetPubElValRepByName(string TagName);
138    string GetPubElValRepByNumber(guint16 group, guint16 element);
139
140    TagElValueHT & GetPubElVal(void) { return PubElValSet.GetTagHt(); };
141    void   PrintPubElVal(ostream & os = cout);
142    void   PrintPubDict (ostream & os = cout);
143      
144    // TODO Swig string* GetShaTagNames(); 
145    string GetShaElValByName(string TagName);
146    string GetShaElValByNumber(guint16 group, guint16 element);
147    string GetShaElValRepByName(string TagName);
148    string GetShaElValRepByNumber(guint16 group, guint16 element);
149
150    string GetElValByName(string TagName);
151    string GetElValByNumber(guint16 group, guint16 element);
152    string GetElValRepByName(string TagName);
153    string GetElValRepByNumber(guint16 group, guint16 element);
154
155    int SetPubElValByName(string content, string TagName);
156    int SetPubElValByNumber(string content, guint16 group, guint16 element);
157    int SetShaElValByName(string content, string ShadowTagName);
158    int SetShaElValByNumber(string content, guint16 group, guint16 element);
159    
160    int SetPubElValLengthByNumber(guint32 lgr, guint16 group, guint16 element);                                   
161    int ReplaceOrCreateByNumber(string Value, guint16 Group, guint16 Elem);                                
162    int GetXSize(void);  
163    int GetYSize(void);
164    int GetZSize(void);       
165    string GetPixelType(void);  
166    int Write(FILE *, FileType);
167    
168 };
169
170 #endif