]> Creatis software - gdcm.git/blob - src/gdcmParser.h
8767a1d4b99e13fe0880b971bbbd796508af533f
[gdcm.git] / src / gdcmParser.h
1 // gdcmParser.h
2 //-----------------------------------------------------------------------------
3 #ifndef GDCMPARSER_H
4 #define GDCMPARSER_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
13 #include <map>
14 #include <list>       // for linking together *all* the Dicom Elements
15
16 //-----------------------------------------------------------------------------
17 typedef std::string VRKey;
18 typedef std::string VRAtr;
19 typedef std::map<VRKey, VRAtr> VRHT;    // Value Representation Hash Table
20
21 typedef std::multimap<TagKey, gdcmHeaderEntry *> TagHeaderEntryHT;
22 typedef std::pair<TagKey, gdcmHeaderEntry *> PairHT;
23 typedef std::pair<TagHeaderEntryHT::iterator,TagHeaderEntryHT::iterator> IterHT; 
24
25 typedef std::list<gdcmHeaderEntry *> ListTag; // for linking together the Elements
26
27 typedef std::string GroupKey;
28 typedef std::map<GroupKey, int> GroupHT;
29
30 //-----------------------------------------------------------------------------
31 /*
32  * \defgroup gdcmParser
33  * \brief used by both gdcmHeader and gdcmDicomDir
34  */
35 class GDCM_EXPORT gdcmParser
36 {
37 public:
38    gdcmParser(bool exception_on_error  = false);
39    gdcmParser(const char *inFilename, 
40               bool  exception_on_error = false, 
41               bool  enable_sequences   = false,
42               bool  ignore_shadow      = false);
43    virtual ~gdcmParser(void);
44
45 // Print
46    /**
47     * \ingroup gdcmParser
48     * \brief   Sets the print level for the Dicom Header 
49     * \note    0 for Light Print; 1 for 'medium' Print, 2 for Heavy
50     */
51    void SetPrintLevel(int level) 
52       { printLevel = level; };
53    /**
54     * \ingroup gdcmParser
55     * \brief   canonical Printer 
56     * \sa    SetPrintLevel
57     */   
58    virtual void Print        (std::ostream &os = std::cout) 
59       {PrintEntry(os);};
60    virtual void PrintEntry   (std::ostream &os = std::cout);
61    virtual void PrintPubDict (std::ostream &os = std::cout);
62    virtual void PrintShaDict (std::ostream &os = std::cout);
63
64 // Standard values
65    /**
66     * \ingroup gdcmParser
67     * \brief   Gets the external File Name 
68     */
69    inline std::string GetFileName(void) 
70       {return filename;}
71
72 // Dictionnaries
73    gdcmDict *GetPubDict(void);
74    gdcmDict *GetShaDict(void);
75    bool SetShaDict(gdcmDict *dict);
76    bool SetShaDict(DictKey dictName);
77
78 // Informations contained in the parser
79    virtual bool IsReadable(void);
80    bool IsImplicitVRLittleEndianTransferSyntax(void);
81    bool IsExplicitVRLittleEndianTransferSyntax(void);
82    bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
83    bool IsExplicitVRBigEndianTransferSyntax(void);
84    FileType GetFileType(void);
85
86 // Entries
87    /**
88     * \ingroup gdcmHeader
89     * \brief   returns a ref to the Dicom Header H table (multimap)
90     * return the Dicom Header H table
91     */
92    inline TagHeaderEntryHT &GetEntry(void) { return tagHT; };
93
94    /**
95     * \ingroup gdcmHeader
96     * \brief   returns a ref to the Dicom Header chained list
97     * return the Dicom Header chained list
98     */
99    inline ListTag &GetListEntry(void) { return listEntries; };
100
101 // Read (used in gdcmFile, gdcmDicomDir)
102    FILE *OpenFile(bool exception_on_error = false) throw(gdcmFileError);
103    bool CloseFile(void);
104
105 // Write (used in gdcmFile, gdcmDicomDir)
106    virtual bool Write(FILE *, FileType);
107
108    bool ReplaceOrCreateByNumber(std::string Value, guint16 Group, guint16 Elem);
109    bool ReplaceOrCreateByNumber(     char  *Value, guint16 Group, guint16 Elem);
110    bool ReplaceIfExistByNumber (     char  *Value, guint16 Group, guint16 Elem);
111
112 // System access
113    /**
114     * \ingroup gdcmHeader
115     * \brief   returns the 'swap code' 
116     *          (Big Endian, Little Endian, 
117     *          Bad Big Endian, Bad Little Endian)
118     *          according to the processor Endianity and what's written on disc
119     * return 
120     */
121    inline int GetSwapCode(void) { return sw; }
122    
123    guint16 SwapShort(guint16);   // needed by gdcmFile
124    guint32 SwapLong(guint32);    // needed by gdcmFile
125    guint16 UnswapShort(guint16); // needed by gdcmFile
126    guint32 UnswapLong(guint32);  // needed by gdcmFile
127
128 protected:
129 // Entry
130    int CheckIfEntryExistByNumber(guint16 Group, guint16 Elem ); // int !
131    virtual std::string GetEntryByName    (std::string tagName);
132    virtual std::string GetEntryVRByName  (std::string tagName);
133    virtual std::string GetEntryByNumber  (guint16 group, guint16 element);
134    virtual std::string GetEntryVRByNumber(guint16 group, guint16 element);
135    virtual int     GetEntryLengthByNumber(guint16 group, guint16 element);
136
137    virtual bool SetEntryByName  (std::string content, std::string tagName);
138    virtual bool SetEntryByNumber(std::string content,  guint16 group, guint16 element);
139    virtual bool SetEntryLengthByNumber(guint32 length, guint16 group, guint16 element);
140
141    virtual size_t GetEntryOffsetByNumber  (guint16 Group, guint16 Elem);
142    virtual void  *GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem);   
143    virtual void  *LoadEntryVoidArea       (guint16 Group, guint16 Element);
144    virtual bool   SetEntryVoidAreaByNumber(void *a, guint16 Group, guint16 Elem);
145
146    virtual void UpdateShaEntries(void);
147
148 // Header entry
149    gdcmHeaderEntry *GetHeaderEntryByNumber  (guint16 group, guint16 element); 
150    gdcmHeaderEntry *GetHeaderEntryByName    (std::string Name);
151    IterHT           GetHeaderEntrySameNumber(guint16 group, guint16 element); 
152 // IterHT           GetHeaderEntrySameName  (std::string Name); 
153
154    void LoadHeaderEntrySafe(gdcmHeaderEntry *);
155
156    void UpdateGroupLength(bool SkipSequence = false, FileType type = ImplicitVR);
157    void WriteEntry(gdcmHeaderEntry *tag,FILE *_fp,FileType type);
158    void WriteEntries(FILE *_fp,FileType type);
159    void WriteEntriesDeprecated(FILE *_fp,FileType type); // JPR
160
161    void AddHeaderEntry       (gdcmHeaderEntry *);
162
163 // Variables
164    /**
165    * \brief File Pointer, open during Header parsing
166    */
167    FILE *fp;
168    /**
169    * \brief ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
170    */
171    FileType filetype;  
172
173    static const unsigned int HEADER_LENGTH_TO_READ; 
174    static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
175    static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
176
177 protected:
178    /**
179    * \brief H Table (multimap), to provide fast access
180    */
181    TagHeaderEntryHT tagHT; 
182    /**
183    * \brief chained list, to keep the 'spacial' ordering
184    */
185    ListTag listEntries; 
186    /**
187    * \brief will be set 1 if user asks to 'go inside' the 'sequences' (VR = "SQ")
188    */    
189    int enableSequences;
190    /**
191    * \brief amount of printed details for each Header Entry (Dicom Element)
192    *  0 : the least 
193    */    
194    int printLevel;
195    
196    /** 
197    * \brief For some ACR-NEMA images, it's *not* 7fe0 ... 
198    */   
199    guint16 GrPixel;
200    
201    /** 
202    * \brief For some ACR-NEMA images, it's *not* 0010 ... 
203    */    
204    guint16 NumPixel;
205    /**
206    * \brief some files may contain icons; GrPixel,NumPixel appears several times
207    * Let's remember how many times!
208    */
209    int countGrPixel;
210    /**
211    * \brief = true when the 'pixel Element' is reached during writting process
212    */   
213    bool itsTimeToWritePixels;
214       
215 private:
216    // Read
217    bool ParseHeader(bool exception_on_error = false) throw(gdcmFormatError);
218
219    void LoadHeaderEntries    (void);
220    void LoadHeaderEntry      (gdcmHeaderEntry *);
221    void FindHeaderEntryLength(gdcmHeaderEntry *);
222    void FindHeaderEntryVR    (gdcmHeaderEntry *);
223    bool CheckHeaderEntryVR   (gdcmHeaderEntry *, VRKey);
224
225    std::string GetHeaderEntryValue  (gdcmHeaderEntry *);
226    std::string GetHeaderEntryUnvalue(gdcmHeaderEntry *);
227
228    void SkipHeaderEntry          (gdcmHeaderEntry *);
229    void FixHeaderEntryFoundLength(gdcmHeaderEntry *, guint32);
230    bool IsHeaderEntryAnInteger   (gdcmHeaderEntry *);
231
232    guint32 FindHeaderEntryLengthOB(void);
233
234    guint16 ReadInt16(void);
235    guint32 ReadInt32(void);
236    void    SkipBytes(guint32);
237
238    void Initialise(void);
239    bool CheckSwap(void);
240    void SwitchSwapToBigEndian(void);
241    void SetMaxSizeLoadEntry(long);
242    void SetMaxSizePrintEntry(long);
243
244    // DictEntry  related utilities
245    gdcmDictEntry *GetDictEntryByName  (std::string Name);
246    gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
247    gdcmDictEntry *NewVirtualDictEntry(guint16 group, 
248                                       guint16 element,
249                                       std::string vr     = "unkn",
250                                       std::string fourth = "unkn",
251                                       std::string name   = "unkn");
252    //gdcmDictEntry *NewVirtualDictEntry(gdcmHeaderEntry *); // never defined
253    
254    // HeaderEntry related utilities
255    
256    gdcmHeaderEntry *ReadNextHeaderEntry   (void);
257    gdcmHeaderEntry *NewHeaderEntryByNumber(guint16 group, 
258                                            guint16 element);
259    gdcmHeaderEntry *NewHeaderEntryByName  (std::string Name);
260
261
262    // Deprecated (Not used) --> commented out
263    //gdcmHeaderEntry *NewManualHeaderEntryToPubDict(std::string NewTagName,
264    //                                                  std::string VR);
265    
266    guint32 GenerateFreeTagKeyInGroup(guint16 group);
267
268    /**
269    * \brief Refering underlying filename.
270    */
271    std::string filename; 
272
273    /**
274    * \brief Public dictionary used to parse this header
275    */
276    gdcmDict *RefPubDict;
277    
278    /**
279    * \brief Optional "shadow dictionary" (private elements) used to parse this header
280    */
281    gdcmDict *RefShaDict;
282
283    /**
284    * \brief = 1 if a gdcmHeaderEntry was added post parsing 
285    */   
286    int wasUpdated;
287    
288    /**
289    * \brief =1 if user wants to skip shadow groups while parsing (to save space)
290    */
291    int ignoreShadow;
292    
293    /**
294    * \brief Swap code e.g. little, big, bad-big, bad-little endian). 
295    * \warning : this code is not fixed during header parsing.      
296    */
297    int sw;
298    /**
299    * \brief Size treshold above which an element value will NOT be loaded in 
300    *       memory (to avoid loading the image/volume itself). By default,
301    *       this upper bound is fixed to 1024 bytes (which might look reasonable
302    * when one considers the definition of the various VR contents).
303    */
304    guint32 MaxSizeLoadEntry;
305    
306    /**
307    * \brief Size treshold above which an element value will NOT be *printed* in
308    *        order no to polute the screen output. 
309    *        By default, this upper bound is fixed to 64 bytes.
310    */   
311    guint32 MaxSizePrintEntry;
312    
313 };
314
315 //-----------------------------------------------------------------------------
316 #endif