]> Creatis software - gdcm.git/blob - src/gdcmHeader.h
f46a12c64961f49b7575ebffdc394e4acac864e6
[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 "gdcmException.h"
9 #include "gdcmDictSet.h"
10 #include "gdcmElValue.h"
11 #include "gdcmElValSet.h"
12
13 typedef string VRKey;
14 typedef string VRAtr;
15 typedef map<VRKey, VRAtr> VRHT;    // Value Representation Hash Table
16
17 /// \brief The purpous of an instance of gdcmHeader is to act as a container of
18 ///        all the DICOM elements and their corresponding values (and
19 ///        additionaly the corresponding DICOM dictionary entry) of the header
20 ///        of a DICOM file.
21 ///
22 /// The typical usage of instances of class gdcmHeader is to classify a set of
23 /// dicom files according to header information e.g. to create a file hierarchy
24 /// reflecting the Patient/Study/Serie informations, or extracting a given
25 /// SerieId. Accesing the content (image[s] or volume[s]) is beyond the
26 /// functionality of this class and belongs to gdmcFile.
27 /// \note  The various entries of the explicit value representation (VR) shall
28 ///        be managed within a dictionary which is shared by all gdcmHeader
29 ///        instances.
30 /// \note  The gdcmHeader::Set*Tag* family members cannot be defined as
31 ///        protected due to Swig limitations for as Has_a dependency between
32 ///        gdcmFile and gdcmHeader.
33
34 class GDCM_EXPORT gdcmHeader {
35    void SkipBytes(guint32);
36 private:
37    /// Pointer to the Value Representation Hash Table which contains all
38    /// the VR of the DICOM version3 public dictionary. 
39    static VRHT *dicom_vr;
40  
41    /// Global dictionary container
42    gdcmDictSet* Dicts;
43    /// Public dictionary used to parse this header
44    gdcmDict* RefPubDict;
45    /// Optional "shadow dictionary" (private elements) used to parse this header
46    gdcmDict* RefShaDict;
47
48    /// ELement VALueS parsed with the PUBlic dictionary.
49    ElValSet PubElVals;
50    /// ELement VALueS parsed with the SHAdow dictionary.
51    ElValSet ShaElVals;
52    /// Refering underlying filename.
53    string filename; 
54    FILE * fp;
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 InitVRDict(void);
80    void SwitchSwapToBigEndian(void);
81    void AddAndDefaultElements(void);
82    void SetMaxSizeLoadElementValue(long);
83
84    gdcmDictEntry * GetDictEntryByKey(guint16, guint16);
85    gdcmDictEntry * GetDictEntryByName(string name);
86
87    // ElValue related utilities
88    ElValue * ReadNextElement(void);
89    ElValue * NewElValueByKey(guint16 group, guint16 element);
90    ElValue * NewElValueByName(string name);
91    void FindLength(ElValue *);
92    void FindVR(ElValue *);
93    void LoadElementValue(ElValue *);
94    void LoadElementValueSafe(ElValue *);
95    void SkipElementValue(ElValue *);
96    void FixFoundLength(ElValue*, guint32);
97    bool IsAnInteger(ElValue *);
98    
99    bool IsImplicitVRLittleEndianTransferSyntax(void);
100    bool IsExplicitVRLittleEndianTransferSyntax(void);
101    bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
102    bool IsExplicitVRBigEndianTransferSyntax(void);
103    bool IsJPEGBaseLineProcess1TransferSyntax(void);
104    bool IsJPEGExtendedProcess2_4TransferSyntax(void); 
105    bool IsJPEGExtendedProcess3_5TransferSyntax(void);
106    bool IsJPEGSpectralSelectionProcess6_8TransferSyntax(void); 
107       
108 protected:
109    // FIXME: is this enum still necessary ??
110    enum FileType {
111       Unknown = 0,
112       TrueDicom,
113       ExplicitVR,
114       ImplicitVR,
115       ACR,
116       ACR_LIBIDO};  // CLEANME
117    FileType filetype;
118    int write(ostream&);   
119    int anonymize(ostream&);  // FIXME : anonymize should be a friend ?
120 public:
121    void LoadElements(void);
122    virtual void ParseHeader(bool exception_on_error = false)
123      throw(gdcmFormatError);
124    gdcmHeader(const char *filename, bool exception_on_error = false)
125      throw(gdcmFileError);
126    virtual ~gdcmHeader();
127    
128    size_t GetPixelOffset(void);
129    void   GetPixels(size_t, void *);
130    int    GetSwapCode(void) { return sw; }
131
132    // TODO Swig int SetPubDict(string filename);
133    // When some proprietary shadow groups are disclosed, we can set up
134    // an additional specific dictionary to access extra information.
135    // TODO Swig int SetShaDict(string filename);
136
137    string GetPubElValByName(string TagName);
138    string GetPubElValByNumber(guint16 group, guint16 element);
139    string GetPubElValRepByName(string TagName);
140    string GetPubElValRepByNumber(guint16 group, guint16 element);
141
142    TagElValueHT & GetPubElVal(void) { return PubElVals.GetTagHt(); };
143    void   PrintPubElVal(ostream & os = cout);
144    void   PrintPubDict(ostream & os = cout);
145      
146    // TODO Swig string* GetShaTagNames(); 
147    string GetShaElValByName(string TagName);
148    string GetShaElValByNumber(guint16 group, guint16 element);
149    string GetShaElValRepByName(string TagName);
150    string GetShaElValRepByNumber(guint16 group, guint16 element);
151
152    string GetElValByName(string TagName);
153    string GetElValByNumber(guint16 group, guint16 element);
154    string GetElValRepByName(string TagName);
155    string GetElValRepByNumber(guint16 group, guint16 element);
156
157    int SetPubElValByName(string content, string TagName);
158    int SetPubElValByNumber(string content, guint16 group, guint16 element);
159    int SetShaElValByName(string content, string ShadowTagName);
160    int SetShaElValByNumber(string content, guint16 group, guint16 element);
161
162    ElValSet GetPubElVals() { return(PubElVals); }
163 };
164
165 #endif