]> Creatis software - gdcm.git/blob - src/gdcmHeader.h
0f1c8f32e4b0d6a94310cc1bf7e8f36681fd981d
[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 ////////////////////////////////////////////////////////////////////////////
14 // The purpous of an instance of gdcmHeader is to act as a container of
15 // all the elements and their corresponding values (and additionaly the
16 // corresponding DICOM dictionary entry) of the header of a DICOM file.
17 //
18 // The typical usage of instances of class gdcmHeader is to classify a set of
19 // dicom files according to header information e.g. to create a file hierarchy
20 // reflecting the Patient/Study/Serie informations, or extracting a given
21 // SerieId. Accesing the content (image[s] or volume[s]) is beyond the
22 // functionality of this class and belong to gdmcFile (see below).
23 // Notes:
24 // * the various entries of the explicit value representation (VR) shall
25 //   be managed within a dictionary which is shared by all gdcmHeader instances
26 // * the gdcmHeader::Set*Tag* family members cannot be defined as protected
27 //   (Swig limitations for as Has_a dependency between gdcmFile and gdcmHeader)
28
29 typedef string VRKey;
30 typedef string VRAtr;
31 typedef map<VRKey, VRAtr> VRHT;    // Value Representation Hash Table
32
33 class GDCM_EXPORT gdcmHeader {
34         void SkipBytes(guint32);
35 private:
36         static VRHT *dicom_vr;
37         // Dictionaries of data elements:
38
39         gdcmDictSet* Dicts;         // global dictionary container
40         gdcmDict* RefPubDict;       // public dictionary
41         gdcmDict* RefShaDict;       // shadow dictionary (optional)
42         // Parsed element values:
43         ElValSet PubElVals;         // parsed with Public Dictionary
44         ElValSet ShaElVals;         // parsed with Shadow Dictionary
45         string filename;            // refering underlying file
46         FILE * fp;
47         guint16 grPixel;
48         guint16 numPixel;
49         // Ne faudrait-il pas une indication sur la presence ou non
50         // du 'groupe des pixels' dans l'entete?
51         // (voir pb du DICOMDIR)
52         
53         // Swap code (little, big, bad-big, bad-little endian): this code is not fixed
54         // during parsing.FIXME sw should be an enum e.g.
55         //enum EndianType {
56                 //LittleEndian, 
57                 //BadLittleEndian,
58                 //BigEndian, 
59                 //BadBigEndian};
60         int sw;
61
62         // Only the elements whose size is below this bound will be loaded.
63         // By default, this upper bound is limited to 1024 (which looks reasonable
64         // when one considers the definition of the various VR contents).
65         guint32 MaxSizeLoadElementValue;
66
67         guint16 ReadInt16(void);
68         guint32 ReadInt32(void);
69         guint16 SwapShort(guint16);
70         guint32 SwapLong(guint32);
71         guint32 FindLengthOB(void);
72         void Initialise(void);
73         void CheckSwap(void);
74         void InitVRDict(void);
75         void SwitchSwapToBigEndian(void);
76         void AddAndDefaultElements(void);
77         void SetMaxSizeLoadElementValue(long);
78
79         gdcmDictEntry * GetDictEntryByKey(guint16, guint16);
80         gdcmDictEntry * GetDictEntryByName(string name);
81
82         // ElValue related utilities
83         ElValue * ReadNextElement(void);
84         ElValue * NewElValueByKey(guint16 group, guint16 element);
85         ElValue * NewElValueByName(string name);
86         void FindLength(ElValue *);
87         void FindVR(ElValue *);
88         void LoadElementValue(ElValue *);
89         void LoadElementValueSafe(ElValue *);
90         void SkipElementValue(ElValue *);
91         void FixFoundLength(ElValue*, guint32);
92         bool IsAnInteger(ElValue *);
93         
94         bool IsImplicitVRLittleEndianTransferSyntax(void);
95         bool IsExplicitVRLittleEndianTransferSyntax(void);
96         bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
97         bool IsExplicitVRBigEndianTransferSyntax(void);
98         bool IsJPEGBaseLineProcess1TransferSyntax(void);
99         bool IsJPEGExtendedProcess2_4TransferSyntax(void);      
100         bool IsJPEGExtendedProcess3_5TransferSyntax(void);
101         bool IsJPEGSpectralSelectionProcess6_8TransferSyntax(void);     
102                 
103 protected:
104         enum FileType {
105                 Unknown = 0,
106                 TrueDicom,
107                 ExplicitVR,
108                 ImplicitVR,
109                 ACR,
110                 ACR_LIBIDO};  // CLEANME
111         FileType filetype;
112         int write(ostream&);   
113         int anonymize(ostream&);  // FIXME : anonymize should be a friend ?
114 public:
115         void LoadElements(void);
116         virtual void ParseHeader(bool exception_on_error = false)
117           throw(gdcmFormatError);
118         gdcmHeader(const char *filename, bool exception_on_error = false)
119           throw(gdcmFileError);
120         virtual ~gdcmHeader();
121         
122         size_t GetPixelOffset(void);
123         void   GetPixels(size_t, void *);
124         int    GetSwapCode(void) { return sw; }
125
126         // TODO Swig int SetPubDict(string filename);
127         // When some proprietary shadow groups are disclosed, we can set up
128         // an additional specific dictionary to access extra information.
129         // TODO Swig int SetShaDict(string filename);
130
131         // Get the element values themselves:
132         string GetPubElValByName(string TagName);
133         string GetPubElValByNumber(guint16 group, guint16 element);
134
135         // Getting the element value representation (VR) might be needed by caller
136         // to convert the string typed content to caller's native type 
137         // (think of C/C++ vs Python).
138
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         // Same thing with the shadow :
147         // TODO Swig string* GetShaTagNames(); 
148         string GetShaElValByName(string TagName);
149         string GetShaElValByNumber(guint16 group, guint16 element);
150         string GetShaElValRepByName(string TagName);
151         string GetShaElValRepByNumber(guint16 group, guint16 element);
152
153         // Wrappers of the above (public is privileged over shadow) to avoid
154         // bugging the caller with knowing if ElVal is from the public or shadow
155         // dictionary.
156         string GetElValByName(string TagName);
157         string GetElValByNumber(guint16 group, guint16 element);
158         string GetElValRepByName(string TagName);
159         string GetElValRepByNumber(guint16 group, guint16 element);
160
161         int SetPubElValByName(string content, string TagName);
162         int SetPubElValByNumber(string content, guint16 group, guint16 element);
163         int SetShaElValByName(string content, string ShadowTagName);
164         int SetShaElValByNumber(string content, guint16 group, guint16 element);
165
166         ElValSet GetPubElVals() { return(PubElVals); }
167 };
168
169 #endif