]> Creatis software - gdcm.git/blob - src/gdcmHeader.h
doxygenation improvement
[gdcm.git] / src / gdcmHeader.h
1 // gdcmHeader.h
2 //-----------------------------------------------------------------------------
3 #ifndef GDCMHEADER_H
4 #define GDCMHEADER_H
5
6 #include "gdcmCommon.h"
7 #include "gdcmVR.h"
8 #include "gdcmTS.h"
9 #include "gdcmException.h"
10 #include "gdcmDictSet.h"
11 #include "gdcmElValue.h"
12 #include "gdcmElValSet.h"
13 #include <map>
14
15 //-----------------------------------------------------------------------------
16 typedef std::string VRKey;
17 typedef std::string VRAtr;
18 typedef std::map<VRKey, VRAtr> VRHT;    // Value Representation Hash Table
19
20 //-----------------------------------------------------------------------------
21 /*
22  * The purpose of an instance of gdcmHeader is to act as a container of
23  * all the DICOM elements and their corresponding values (and
24  * additionaly the corresponding DICOM dictionary entry) of the header
25  * of a DICOM file.
26  *
27  * The typical usage of instances of class gdcmHeader is to classify a set of
28  * dicom files according to header information e.g. to create a file hierarchy
29  * reflecting the Patient/Study/Serie informations, or extracting a given
30  * SerieId. Accesing the content (image[s] or volume[s]) is beyond the
31  * functionality of this class and belongs to gdmcFile.
32  * \note  The various entries of the explicit value representation (VR) shall
33  *        be managed within a dictionary which is shared by all gdcmHeader
34  *        instances.
35  * \note  The gdcmHeader::Set*Tag* family members cannot be defined as
36  *        protected due to Swig limitations for as Has_a dependency between
37  *        gdcmFile and gdcmHeader.
38  */
39 class GDCM_EXPORT gdcmHeader {
40 public:
41    gdcmHeader(bool exception_on_error = false);
42    gdcmHeader(const char *filename, 
43               bool  exception_on_error = false, 
44               bool  enable_sequences   = false);
45               
46    virtual ~gdcmHeader();
47
48 // Standard values and informations contained in the header
49    inline std::string GetFileName(void) {return filename;}
50
51    bool IsReadable(void);
52    bool IsImplicitVRLittleEndianTransferSyntax(void);
53    bool IsExplicitVRLittleEndianTransferSyntax(void);
54    bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
55    bool IsExplicitVRBigEndianTransferSyntax(void);
56    bool IsJPEGBaseLineProcess1TransferSyntax(void);
57    bool IsJPEGExtendedProcess2_4TransferSyntax(void); 
58    bool IsJPEGExtendedProcess3_5TransferSyntax(void);
59    bool IsJPEGSpectralSelectionProcess6_8TransferSyntax(void); 
60    bool IsRLELossLessTransferSyntax(void); 
61    bool IsJPEGLossless(void); 
62    bool IsJPEG2000(void); 
63    bool IsDicomV3(void); 
64    FileType GetFileType(void);
65
66    // Some heuristic based accessors, end user intended 
67    // (to be move to gdcmHeaderHelper?) 
68    int GetXSize(void);  
69    int GetYSize(void);
70    int GetZSize(void);
71    int GetBitsStored(void);
72    int GetBitsAllocated(void);
73    int GetSamplesPerPixel(void);   
74    int GetPlanarConfiguration(void);
75
76    int GetPixelSize(void);   
77    std::string GetPixelType(void);  
78    size_t GetPixelOffset(void);
79    size_t GetPixelAreaLength(void);
80
81    bool   HasLUT(void);
82    int    GetLUTNbits(void);
83    unsigned char * GetLUTRGBA(void);
84
85    std::string GetTransfertSyntaxName(void);
86
87    // When some proprietary shadow groups are disclosed, we can set up
88    // an additional specific dictionary to access extra information.
89    
90    // OK : we still have *ONE* ElValSet, 
91    // with both Public and Shadow Elements
92    // parsed against THE Public Dictionary and A (single) Shadow Dictionary
93    
94    // TODO Swig int SetShaDict(std::string filename);
95    // TODO Swig int SetPubDict(std::string filename);
96
97 // Public element value
98    std::string GetPubElValByName     (std::string tagName);
99    std::string GetPubElValRepByName  (std::string tagName);
100    std::string GetPubElValByNumber   (guint16 group, guint16 element);
101    std::string GetPubElValRepByNumber(guint16 group, guint16 element);
102    
103    bool SetPubElValByName  (std::string content, std::string tagName); 
104    bool SetPubElValByNumber(std::string content, guint16 group, guint16 element);  
105    bool SetPubElValLengthByNumber(guint32 lgr, guint16 group, guint16 element); 
106
107    inline ListTag      & GetPubListElem(void) { return PubElValSet.GetListElem();};
108    inline TagElValueHT & GetPubElVal(void) { return PubElValSet.GetTagHt();   };
109
110    void PrintPubElVal(std::ostream & os = std::cout);
111    void PrintPubDict (std::ostream & os = std::cout);
112
113 // Element value
114    std::string GetElValByName     (std::string tagName);
115    std::string GetElValRepByName  (std::string tagName);
116    std::string GetElValByNumber   (guint16 group, guint16 element);
117    std::string GetElValRepByNumber(guint16 group, guint16 element);
118
119    bool SetElValueByName(std::string content,std::string tagName); 
120 //   bool SetElValueByNumber(std::string content,guint16 group, guint16 element);
121
122 //   inline ListTag      & GetListElem(void) { return PubElValSet.GetListElem();};
123 //   inline TagElValueHT & GetElVal(void) { return PubElValSet.GetTagHt();   };
124
125 // Read (used in gdcmFile)
126    FILE *OpenFile(bool exception_on_error = false) throw(gdcmFileError);
127    bool CloseFile(void);
128    virtual void ParseHeader(bool exception_on_error = false) throw(gdcmFormatError);
129
130 // Write (used in gdcmFile)
131    bool Write(FILE *, FileType);
132    void SetImageDataSize(size_t ExpectedSize);
133
134 // System access
135    inline int GetSwapCode(void) { return sw; }
136    guint16 SwapShort(guint16); // needed by gdcmFile
137    guint32 SwapLong(guint32);  // for JPEG Files
138    
139    // was protected
140    bool ReplaceOrCreateByNumber(std::string Value, guint16 Group, guint16 Elem); 
141    bool ReplaceOrCreateByNumber(     char * Value, guint16 Group, guint16 Elem);                                
142    bool ReplaceIfExistByNumber (     char * Value, guint16 Group, guint16 Elem);
143
144 protected:
145    bool CheckIfExistByNumber(guint16 Group, guint16 Elem );
146    gdcmElValue * GetElValueByNumber(guint16 group, guint16 element); 
147
148    int write(std::ostream&);   
149    int anonymize(std::ostream&);  // FIXME : anonymize should be a friend ?
150
151    size_t GetPubElValOffsetByNumber  (guint16 Group, guint16 Elem);
152    void * GetPubElValVoidAreaByNumber(guint16 Group, guint16 Elem);   
153    void * LoadElementVoidArea        (guint16 Group, guint16 Element);
154
155
156 // Variables
157    FILE * fp;
158    FileType filetype; // ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
159    
160 private:
161    // Read
162    void LoadElements        (void);
163    void LoadElementValue    (gdcmElValue *);
164    void LoadElementValueSafe(gdcmElValue *);
165    void FindLength          (gdcmElValue *);
166    void FindVR              (gdcmElValue *);
167
168    guint32 FindLengthOB(void);
169
170    void SkipElementValue    (gdcmElValue *);
171    void FixFoundLength      (gdcmElValue *, guint32);
172    bool IsAnInteger         (gdcmElValue *);
173
174    guint16 ReadInt16(void);
175    guint32 ReadInt32(void);
176    void    SkipBytes(guint32);
177
178    void Initialise(void);
179    void CheckSwap(void);
180    void SwitchSwapToBigEndian(void);
181    void SetMaxSizeLoadElementValue(long);
182
183    // Dict
184    gdcmDictEntry *GetDictEntryByName  (std::string Name);
185    gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
186
187    // ElValue related utilities
188    gdcmElValue *ReadNextElement   (void);
189    gdcmElValue *NewElValueByNumber(guint16 group, guint16 element);
190    gdcmElValue *NewElValueByName  (std::string Name);
191    gdcmElValue* GetElementByName  (std::string Name);
192
193    // Deprecated
194    gdcmElValue *NewManualElValToPubDict(std::string NewTagName,
195                                         std::string VR);
196
197 // Variables
198    // Pointer to the Value Representation Hash Table which contains all
199    // the VR of the DICOM version3 public dictionary. 
200    gdcmVR *dicom_vr;     // Not a class member for thread-safety reasons
201    
202    // Pointer to the Transfert Syntax Hash Table which contains all
203    // the TS of the DICOM version3 public dictionary. 
204    gdcmTS *dicom_ts;     // Not a class member for thread-safety reasons 
205      
206    // Pointer to global dictionary container
207    gdcmDictSet *Dicts;   // Not a class member for thread-safety reasons
208    
209    // Public dictionary used to parse this header
210    gdcmDict *RefPubDict;
211    
212    // Optional "shadow dictionary" (private elements) used to parse this
213    // header
214    gdcmDict *RefShaDict;
215
216    /// ELement VALueS parsed with the PUBlic dictionary.
217    gdcmElValSet PubElValSet;
218    
219    // Refering underlying filename.
220    std::string filename; 
221   
222    int enableSequences;
223
224    // FIXME sw should be an enum e.g.
225    //enum EndianType {
226       //LittleEndian, 
227       //BadLittleEndian,
228       //BigEndian, 
229       //BadBigEndian};
230    // Swap code e.g. little, big, bad-big, bad-little endian). Warning:
231    // this code is not fixed during header parsing.
232    int sw;
233
234    // Size treshold above which an element value will NOT be loaded in 
235    // memory (to avoid loading the image/volume itself). By default,
236    // this upper bound is fixed to 1024 bytes (which might look reasonable
237    // when one considers the definition of the various VR contents).
238    guint32 MaxSizeLoadElementValue;
239
240    static const unsigned int HEADER_LENGTH_TO_READ; 
241    static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
242 };
243
244 //-----------------------------------------------------------------------------
245 #endif