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