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