]> Creatis software - gdcm.git/blob - src/gdcmHeader.h
Adding :
[gdcm.git] / src / gdcmHeader.h
1 // $Header: /cvs/public/gdcm/src/Attic/gdcmHeader.h,v 1.29 2003/06/26 13:07:01 jpr Exp $
2
3 #ifndef GDCMHEADER_H
4 #define GDCMHEADER_H
5
6 #include <map>
7 #include "gdcmCommon.h"
8 #include "gdcmUtil.h"
9 #include "gdcmException.h"
10 #include "gdcmDictSet.h"
11 #include "gdcmElValue.h"
12 #include "gdcmElValSet.h"
13
14 typedef std::string VRKey;
15 typedef std::string VRAtr;
16 typedef std::map<VRKey, VRAtr> VRHT;    // Value Representation Hash Table
17
18 /// The purpose of an instance of gdcmHeader is to act as a container of
19 /// all the DICOM elements and their corresponding values (and
20 /// additionaly the corresponding DICOM dictionary entry) of the header
21 /// of a DICOM file.
22 ///
23 /// The typical usage of instances of class gdcmHeader is to classify a set of
24 /// dicom files according to header information e.g. to create a file hierarchy
25 /// reflecting the Patient/Study/Serie informations, or extracting a given
26 /// SerieId. Accesing the content (image[s] or volume[s]) is beyond the
27 /// functionality of this class and belongs to gdmcFile.
28 /// \note  The various entries of the explicit value representation (VR) shall
29 ///        be managed within a dictionary which is shared by all gdcmHeader
30 ///        instances.
31 /// \note  The gdcmHeader::Set*Tag* family members cannot be defined as
32 ///        protected due to Swig limitations for as Has_a dependency between
33 ///        gdcmFile and gdcmHeader.
34
35 class GDCM_EXPORT gdcmHeader {
36    void SkipBytes(guint32);
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    // FIXME sw should be an enum e.g.
60    //enum EndianType {
61       //LittleEndian, 
62       //BadLittleEndian,
63       //BigEndian, 
64       //BadBigEndian};
65    /// Swap code e.g. little, big, bad-big, bad-little endian). Warning:
66    /// this code is not fixed during header parsing.
67    int sw;
68
69    /// Size treshold above which an element value will NOT be loaded in 
70    /// memory (to avoid loading the image/volume itself). By default,
71    /// this upper bound is fixed to 1024 bytes (which might look reasonable
72    /// when one considers the definition of the various VR contents).
73    guint32 MaxSizeLoadElementValue;
74
75    guint16 ReadInt16(void);
76    guint32 ReadInt32(void);
77    guint32 FindLengthOB(void);
78    void Initialise(void);
79    void CheckSwap(void);
80    void SwitchSwapToBigEndian(void);
81    // CLEAN ME: NewManualElValToPubDict is NOT called any more.
82    gdcmElValue*  NewManualElValToPubDict(std::string NewTagName,
83                                          std::string VR);
84    void SetMaxSizeLoadElementValue(long);
85
86    gdcmDictEntry * GetDictEntryByNumber(guint16, guint16);
87    gdcmDictEntry * GetDictEntryByName(std::string Name);
88
89    // ElValue related utilities
90    gdcmElValue * ReadNextElement(void);
91    gdcmElValue * NewElValueByNumber(guint16 group, guint16 element);
92    gdcmElValue * NewElValueByName(std::string Name);
93
94    void FindLength(gdcmElValue *);
95    void FindVR(gdcmElValue *);
96    void LoadElementValue(gdcmElValue *);
97    void LoadElementValueSafe(gdcmElValue *);
98    void SkipElementValue(gdcmElValue *);
99    void FixFoundLength(gdcmElValue*, guint32);
100    bool IsAnInteger(gdcmElValue *);
101    void LoadElements(void);
102    
103 protected:
104    FILE * fp;
105    FileType filetype;
106    
107    gdcmElValue * GetElValueByNumber(guint16 group, guint16 element);
108    int CheckIfExistByNumber(guint16 Group, guint16 Elem );
109
110    guint16 SwapShort(guint16); // needed by gdcmFile
111    guint32 SwapLong(guint32);  // for JPEG Files :-(
112    bool OpenFile(bool exception_on_error = false)
113      throw(gdcmFileError);
114    bool CloseFile(void);
115    int write(std::ostream&);   
116    int anonymize(std::ostream&);  // FIXME : anonymize should be a friend ?
117 public:
118    bool IsReadable(void);
119    bool IsImplicitVRLittleEndianTransferSyntax(void);
120    bool IsExplicitVRLittleEndianTransferSyntax(void);
121    bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
122    bool IsExplicitVRBigEndianTransferSyntax(void);
123    bool IsJPEGBaseLineProcess1TransferSyntax(void);
124    bool IsJPEGExtendedProcess2_4TransferSyntax(void); 
125    bool IsJPEGExtendedProcess3_5TransferSyntax(void);
126    bool IsJPEGSpectralSelectionProcess6_8TransferSyntax(void); 
127    bool IsJPEGLossless(void); 
128    bool IsDicomV3(void); 
129       
130    virtual void ParseHeader(bool exception_on_error = false)
131      throw(gdcmFormatError);
132    gdcmHeader(const char *filename, bool exception_on_error = false);
133    gdcmHeader( bool exception_on_error = false);
134    virtual ~gdcmHeader();
135    
136    size_t GetPixelOffset(void);
137    int    GetSwapCode(void) { return sw; }
138
139    // TODO Swig int SetPubDict(std::string filename);
140    // When some proprietary shadow groups are disclosed, we can set up
141    // an additional specific dictionary to access extra information.
142    // TODO Swig int SetShaDict(std::string filename);
143
144    std::string GetPubElValByName     (std::string TagName);
145    std::string GetPubElValRepByName  (std::string TagName);
146    std::string GetPubElValByNumber   (guint16 group, guint16 element);
147    std::string GetPubElValRepByNumber(guint16 group, guint16 element);
148
149    TagElValueHT & GetPubElVal(void) { return PubElValSet.GetTagHt(); };
150    void   PrintPubElVal(std::ostream & os = std::cout);
151    void   PrintPubDict (std::ostream & os = std::cout);
152      
153    // TODO Swig std::string* GetShaTagNames(); 
154    std::string GetShaElValByName     (std::string TagName);
155    std::string GetShaElValRepByName  (std::string TagName);
156    std::string GetShaElValByNumber   (guint16 group, guint16 element);
157    std::string GetShaElValRepByNumber(guint16 group, guint16 element);
158
159    std::string GetElValByName     (std::string TagName);
160    std::string GetElValRepByName  (std::string TagName);
161    std::string GetElValByNumber   (guint16 group, guint16 element);
162    std::string GetElValRepByNumber(guint16 group, guint16 element);
163
164    int SetPubElValByName  (std::string content, std::string TagName);
165    int SetShaElValByName  (std::string content, std::string ShadowTagName);
166    int SetPubElValByNumber(std::string content, guint16 group, guint16 element);
167
168    int SetShaElValByNumber(std::string content, guint16 group, guint16 element);
169    
170    int SetPubElValLengthByNumber(guint32 lgr, guint16 group, guint16 element);                                   
171
172    int ReplaceOrCreateByNumber(std::string Value, guint16 Group, guint16 Elem); 
173    int ReplaceOrCreateByNumber(char * Value,      guint16 Group, guint16 Elem);                                
174                                
175    int GetXSize(void);  
176    int GetYSize(void);
177    int GetZSize(void);
178    int GetBitsStored(void);
179    int GetSamplesPerPixel(void);
180    int GetPixelSize(void);       
181    std::string GetPixelType(void);  
182    
183    float GetXSpacing(void);
184    float GetYSpacing(void);  
185    float GetZSpacing(void);  
186   
187    float GetXImagePosition(void);
188    float GetYImagePosition(void);
189    float GetZImagePosition(void);
190    
191    int Write(FILE *, FileType);
192 };
193
194 #endif