]> Creatis software - gdcm.git/blob - src/gdcmHeader.h
* More memmory link related corrections and documentation fixes.
[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    gdcmVR *dicom_vr;     // Not a class member for thread-safety reasons
41    /// Pointer to global dictionary container
42    gdcmDictSet* Dicts;   // Not a class member for thread-safety reasons
43    /// Public dictionary used to parse this header
44    gdcmDict* RefPubDict;
45    /// Optional "shadow dictionary" (private elements) used to parse this
46    /// header
47    gdcmDict* RefShaDict;
48
49    /// ELement VALueS parsed with the PUBlic dictionary.
50    gdcmElValSet PubElValSet;
51    /// ELement VALueS parsed with the SHAdow dictionary.
52    gdcmElValSet ShaElValSet;
53    /// Refering underlying filename.
54    string filename; 
55    
56    // FIXME sw should be an enum e.g.
57    //enum EndianType {
58       //LittleEndian, 
59       //BadLittleEndian,
60       //BigEndian, 
61       //BadBigEndian};
62    /// Swap code e.g. little, big, bad-big, bad-little endian). Warning:
63    /// this code is not fixed during header parsing.
64    int sw;
65
66    /// Size treshold above which an element value will NOT be loaded in 
67    /// memory (to avoid loading the image/volume itself). By default,
68    /// this upper bound is fixed to 1024 bytes (which might look reasonable
69    /// when one considers the definition of the various VR contents).
70    guint32 MaxSizeLoadElementValue;
71
72    guint16 ReadInt16(void);
73    guint32 ReadInt32(void);
74    guint16 SwapShort(guint16);
75    guint32 SwapLong(guint32);
76    guint32 FindLengthOB(void);
77    void Initialise(void);
78    void CheckSwap(void);
79    void SwitchSwapToBigEndian(void);
80    // CLEAN ME: NewManualElValToPubDict is NOT called any more.
81    gdcmElValue*  NewManualElValToPubDict(string NewTagName, string VR);
82    void SetMaxSizeLoadElementValue(long);
83
84    gdcmDictEntry * GetDictEntryByNumber(guint16, guint16);
85    gdcmDictEntry * GetDictEntryByName(string name);
86
87    // ElValue related utilities
88    gdcmElValue * ReadNextElement(void);
89    gdcmElValue * NewElValueByNumber(guint16 group, guint16 element);
90    gdcmElValue * NewElValueByName(string name);
91    void FindLength(gdcmElValue *);
92    void FindVR(gdcmElValue *);
93    void LoadElementValue(gdcmElValue *);
94    void LoadElementValueSafe(gdcmElValue *);
95    void SkipElementValue(gdcmElValue *);
96    void FixFoundLength(gdcmElValue*, guint32);
97    bool IsAnInteger(gdcmElValue *);
98    void LoadElements(void);
99    
100 protected:
101    FILE * fp;
102    FileType filetype;
103    bool OpenFile(bool exception_on_error = false)
104      throw(gdcmFileError);
105    bool CloseFile(void);
106    int write(ostream&);   
107    int anonymize(ostream&);  // FIXME : anonymize should be a friend ?
108 public:
109    bool IsReadable(void);
110    bool IsImplicitVRLittleEndianTransferSyntax(void);
111    bool IsExplicitVRLittleEndianTransferSyntax(void);
112    bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
113    bool IsExplicitVRBigEndianTransferSyntax(void);
114    bool IsJPEGBaseLineProcess1TransferSyntax(void);
115    bool IsJPEGExtendedProcess2_4TransferSyntax(void); 
116    bool IsJPEGExtendedProcess3_5TransferSyntax(void);
117    bool IsJPEGSpectralSelectionProcess6_8TransferSyntax(void); 
118    bool IsJPEGLossless(void); 
119    bool IsDicomV3(void); 
120       
121    virtual void ParseHeader(bool exception_on_error = false)
122      throw(gdcmFormatError);
123    gdcmHeader(const char *filename, bool exception_on_error = false);
124    virtual ~gdcmHeader();
125    
126    size_t GetPixelOffset(void);
127    int    GetSwapCode(void) { return sw; }
128
129    // TODO Swig int SetPubDict(string filename);
130    // When some proprietary shadow groups are disclosed, we can set up
131    // an additional specific dictionary to access extra information.
132    // TODO Swig int SetShaDict(string filename);
133
134    string GetPubElValByName(string TagName);
135    string GetPubElValByNumber(guint16 group, guint16 element);
136    string GetPubElValRepByName(string TagName);
137    string GetPubElValRepByNumber(guint16 group, guint16 element);
138
139    TagElValueHT & GetPubElVal(void) { return PubElValSet.GetTagHt(); };
140    void   PrintPubElVal(ostream & os = cout);
141    void   PrintPubDict (ostream & os = cout);
142      
143    // TODO Swig string* GetShaTagNames(); 
144    string GetShaElValByName(string TagName);
145    string GetShaElValByNumber(guint16 group, guint16 element);
146    string GetShaElValRepByName(string TagName);
147    string GetShaElValRepByNumber(guint16 group, guint16 element);
148
149    string GetElValByName(string TagName);
150    string GetElValByNumber(guint16 group, guint16 element);
151    string GetElValRepByName(string TagName);
152    string GetElValRepByNumber(guint16 group, guint16 element);
153
154    int SetPubElValByName(string content, string TagName);
155    int SetPubElValByNumber(string content, guint16 group, guint16 element);
156    int SetShaElValByName(string content, string ShadowTagName);
157    int SetShaElValByNumber(string content, guint16 group, guint16 element);
158    
159    int SetPubElValLengthByNumber(guint32 lgr, guint16 group, guint16 element);                                   
160    int ReplaceOrCreateByNumber(string Value, guint16 Group, guint16 Elem);                                
161    int GetXSize(void);  
162    int GetYSize(void);
163    int GetZSize(void);       
164    string GetPixelType(void);  
165    int Write(FILE *, FileType);
166    
167 };
168
169 #endif