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