]> Creatis software - gdcm.git/blob - src/gdcmParser.h
892ceb5e8d8af6c059aba19b6a6079c5f8ec2a18
[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 *filename, 
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) { printLevel = level; };
52    virtual void Print        (std::ostream &os = std::cout) {PrintEntry(os);};
53    virtual void PrintEntry   (std::ostream &os = std::cout);
54    virtual void PrintPubDict (std::ostream &os = std::cout);
55    virtual void PrintShaDict (std::ostream &os = std::cout);
56
57 // Standard values
58    inline std::string GetFileName(void) {return filename;}
59
60 // Dictionnaries
61    gdcmDict *GetPubDict(void);
62    gdcmDict *GetShaDict(void);
63    bool SetShaDict(gdcmDict *dict);
64    bool SetShaDict(DictKey dictName);
65
66 // Informations contained in the parser
67    virtual bool IsReadable(void);
68    bool IsImplicitVRLittleEndianTransferSyntax(void);
69    bool IsExplicitVRLittleEndianTransferSyntax(void);
70    bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
71    bool IsExplicitVRBigEndianTransferSyntax(void);
72    FileType GetFileType(void);
73
74 // Entries
75    /**
76     * \ingroup gdcmHeader
77     * \brief   returns a ref to the Dicom Header H table (multimap)
78     * return the Dicom Header H table
79     */
80    inline TagHeaderEntryHT &GetEntry(void) { return tagHT; };
81
82    /**
83     * \ingroup gdcmHeader
84     * \brief   returns a ref to the Dicom Header chained list
85     * return the Dicom Header chained list
86     */
87    inline ListTag &GetListEntry(void) { return listEntries; };
88
89 // Read (used in gdcmFile)
90    FILE *OpenFile(bool exception_on_error = false) throw(gdcmFileError);
91    bool CloseFile(void);
92
93 // Write (used in gdcmFile)
94    virtual bool Write(FILE *, FileType);
95
96    bool ReplaceOrCreateByNumber(std::string Value, guint16 Group, guint16 Elem);
97    bool ReplaceOrCreateByNumber(     char  *Value, guint16 Group, guint16 Elem);
98    bool ReplaceIfExistByNumber (     char  *Value, guint16 Group, guint16 Elem);
99
100 // System access
101    inline int GetSwapCode(void) { return sw; }
102    guint16 GetGrPixel(void)  {return GrPixel;}
103    guint16 GetNumPixel(void) {return NumPixel;}
104    
105    guint16 SwapShort(guint16);   // needed by gdcmFile
106    guint32 SwapLong(guint32);    // needed by gdcmFile
107    guint16 UnswapShort(guint16); // needed by gdcmFile
108    guint32 UnswapLong(guint32);  // needed by gdcmFile
109
110 protected:
111 // Entry
112    int CheckIfEntryExistByNumber(guint16 Group, guint16 Elem ); // int !
113    virtual std::string GetEntryByName    (std::string tagName);
114    virtual std::string GetEntryVRByName  (std::string tagName);
115    virtual std::string GetEntryByNumber  (guint16 group, guint16 element);
116    virtual std::string GetEntryVRByNumber(guint16 group, guint16 element);
117    virtual int     GetEntryLengthByNumber(guint16 group, guint16 element);
118
119    virtual bool SetEntryByName  (std::string content, std::string tagName);
120    virtual bool SetEntryByNumber(std::string content, guint16 group, guint16 element);
121    virtual bool SetEntryLengthByNumber(guint32 length, guint16 group, guint16 element);
122
123    virtual size_t GetEntryOffsetByNumber  (guint16 Group, guint16 Elem);
124    virtual void  *GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem);   
125    virtual void  *LoadEntryVoidArea       (guint16 Group, guint16 Element);
126    virtual bool   SetEntryVoidAreaByNumber(void *a, guint16 Group, guint16 Elem);
127
128    virtual void UpdateShaEntries(void);
129
130 // Header entry
131    gdcmHeaderEntry *GetHeaderEntryByNumber  (guint16 group, guint16 element); 
132    gdcmHeaderEntry *GetHeaderEntryByName    (std::string Name);
133    IterHT           GetHeaderEntrySameNumber(guint16 group, guint16 element); 
134 // IterHT           GetHeaderEntrySameName  (std::string Name); 
135
136    void LoadHeaderEntrySafe(gdcmHeaderEntry *);
137
138    void UpdateGroupLength(bool SkipSequence = false, FileType type = ImplicitVR);
139    void WriteEntries(FILE *_fp,FileType type);
140    void WriteEntriesDeprecated(FILE *_fp,FileType type); // JPR
141
142    void AddHeaderEntry       (gdcmHeaderEntry *);
143
144 // Variables
145    FILE *fp;
146    FileType filetype; // ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
147
148    static const unsigned int HEADER_LENGTH_TO_READ; 
149    static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
150    static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
151
152 protected:
153    TagHeaderEntryHT tagHT; // H Table (multimap), to provide fast access
154    ListTag listEntries;    // chained list, to keep the 'spacial' ordering
155     
156    int enableSequences;
157    int printLevel;
158    
159    // For some ACR-NEMA images, it's *not* 7fe0, 0010 ...    
160    guint16 GrPixel;
161    guint16 NumPixel;
162    // some files may contain icons; GrPixel,NumPixel appears several times
163    // Let's remember how many times!
164    int countGrPixel;
165       
166 private:
167    // Read
168    bool ParseHeader(bool exception_on_error = false) throw(gdcmFormatError);
169
170    void LoadHeaderEntries    (void);
171    void LoadHeaderEntry      (gdcmHeaderEntry *);
172    void FindHeaderEntryLength(gdcmHeaderEntry *);
173    void FindHeaderEntryVR    (gdcmHeaderEntry *);
174    bool CheckHeaderEntryVR   (gdcmHeaderEntry *, VRKey);
175
176    std::string GetHeaderEntryValue  (gdcmHeaderEntry *);
177    std::string GetHeaderEntryUnvalue(gdcmHeaderEntry *);
178
179    void SkipHeaderEntry          (gdcmHeaderEntry *);
180    void FixHeaderEntryFoundLength(gdcmHeaderEntry *, guint32);
181    bool IsHeaderEntryAnInteger   (gdcmHeaderEntry *);
182
183    guint32 FindHeaderEntryLengthOB(void);
184
185    guint16 ReadInt16(void);
186    guint32 ReadInt32(void);
187    void    SkipBytes(guint32);
188
189    void Initialise(void);
190    bool CheckSwap(void);
191    void SwitchSwapToBigEndian(void);
192    void SetMaxSizeLoadEntry(long);
193    void SetMaxSizePrintEntry(long);
194
195    // DictEntry  related utilities
196    gdcmDictEntry *GetDictEntryByName  (std::string Name);
197    gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
198    gdcmDictEntry *NewVirtualDictEntry(guint16 group, 
199                                       guint16 element,
200                                       std::string vr     = "unkn",
201                                       std::string fourth = "unkn",
202                                       std::string name   = "unkn");
203    gdcmDictEntry *NewVirtualDictEntry(gdcmHeaderEntry *);
204    
205    // HeaderEntry related utilities
206    gdcmHeaderEntry *ReadNextHeaderEntry   (void);
207    gdcmHeaderEntry *NewHeaderEntryByNumber(guint16 group, 
208                                            guint16 element);
209    gdcmHeaderEntry *NewHeaderEntryByName  (std::string Name);
210
211
212    // Deprecated (Not used) --> commented out
213    //gdcmHeaderEntry *NewManualHeaderEntryToPubDict(std::string NewTagName,
214    //                                               std::string VR);
215    guint32 GenerateFreeTagKeyInGroup(guint16 group);
216
217    // Refering underlying filename.
218    std::string filename; 
219
220    // Public dictionary used to parse this header
221    gdcmDict *RefPubDict;
222    // Optional "shadow dictionary" (private elements) used to parse this header
223    gdcmDict *RefShaDict;
224
225    // = 1 if a gdcmHeaderEntry was added post parsing 
226    int wasUpdated;
227    
228    // =1 if user wants to skip shadow groups while parsing (to save space)
229    int ignoreShadow;
230
231    // Swap code e.g. little, big, bad-big, bad-little endian). Warning:
232    // this code is not fixed during header parsing.
233    int sw;
234
235    // Size treshold above which an element value will NOT be loaded in 
236    // memory (to avoid loading the image/volume itself). By default,
237    // this upper bound is fixed to 1024 bytes (which might look reasonable
238    // when one considers the definition of the various VR contents).
239    guint32 MaxSizeLoadEntry;
240    // Size treshold above which an element value will NOT be *printed* in 
241    // order no to polute the screen output. By default,
242    // this upper bound is fixed to 64 bytes.   
243    guint32 MaxSizePrintEntry;
244    
245 };
246
247 //-----------------------------------------------------------------------------
248 #endif