]> Creatis software - gdcm.git/blob - src/gdcmHeader.h
remove H Table NameHT
[gdcm.git] / src / gdcmHeader.h
1 // $Header: /cvs/public/gdcm/src/Attic/gdcmHeader.h,v 1.47 2004/01/13 11:32:30 jpr 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    
43    /// Pointer to the Transfert Syntax Hash Table which contains all
44    /// the TS of the DICOM version3 public dictionary. 
45    gdcmTS *dicom_ts;     // Not a class member for thread-safety reasons 
46      
47    /// Pointer to global dictionary container
48    gdcmDictSet *Dicts;   // Not a class member for thread-safety reasons
49    
50    /// Public dictionary used to parse this header
51    gdcmDict *RefPubDict;
52    
53    /// Optional "shadow dictionary" (private elements) used to parse this
54    /// header
55    gdcmDict *RefShaDict;
56
57    /// ELement VALueS parsed with the PUBlic dictionary.
58    gdcmElValSet PubElValSet;
59    
60    /// Refering underlying filename.
61    std::string filename; 
62   
63    int enableSequences;
64
65    // FIXME sw should be an enum e.g.
66    //enum EndianType {
67       //LittleEndian, 
68       //BadLittleEndian,
69       //BigEndian, 
70       //BadBigEndian};
71    /// Swap code e.g. little, big, bad-big, bad-little endian). Warning:
72    /// this code is not fixed during header parsing.
73    int sw;
74
75    /// Size treshold above which an element value will NOT be loaded in 
76    /// memory (to avoid loading the image/volume itself). By default,
77    /// this upper bound is fixed to 1024 bytes (which might look reasonable
78    /// when one considers the definition of the various VR contents).
79    guint32 MaxSizeLoadElementValue;
80
81    guint16 ReadInt16(void);
82    guint32 ReadInt32(void);
83    guint32 FindLengthOB(void);
84    void Initialise(void);
85    void CheckSwap(void);
86    void SwitchSwapToBigEndian(void);
87    // CLEAN ME: NewManualElValToPubDict is NOT called any more.
88    gdcmElValue *NewManualElValToPubDict(std::string NewTagName,
89                                          std::string VR);
90    void SetMaxSizeLoadElementValue(long);
91
92    gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
93    gdcmDictEntry *GetDictEntryByName  (std::string Name);
94
95    // ElValue related utilities
96    gdcmElValue *ReadNextElement(void);
97    gdcmElValue *NewElValueByNumber(guint16 group, guint16 element);
98    gdcmElValue *NewElValueByName  (std::string Name);
99    
100    gdcmElValue* GetElementByName  (std::string Name);
101    // moved from gdcmElValSet
102
103    void FindLength          (gdcmElValue *);
104    void FindVR              (gdcmElValue *);
105    void LoadElementValue    (gdcmElValue *);
106    void LoadElementValueSafe(gdcmElValue *);
107    void SkipElementValue    (gdcmElValue *);
108    void FixFoundLength      (gdcmElValue *, guint32);
109    bool IsAnInteger         (gdcmElValue *);
110    void LoadElements(void);
111    void SkipBytes(guint32);
112
113 protected:
114    FILE * fp;
115    FileType filetype; // ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
116    
117    gdcmElValue * GetElValueByNumber(guint16 group, guint16 element);
118    bool CheckIfExistByNumber(guint16 Group, guint16 Elem );
119
120    int write(std::ostream&);   
121    int anonymize(std::ostream&);  // FIXME : anonymize should be a friend ?
122
123 public:
124    FILE *OpenFile(bool exception_on_error = false)
125      throw(gdcmFileError);
126    bool CloseFile(void);
127    FileType GetFileType(void);
128
129    bool IsReadable(void);
130    bool IsImplicitVRLittleEndianTransferSyntax(void);
131    bool IsExplicitVRLittleEndianTransferSyntax(void);
132    bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
133    bool IsExplicitVRBigEndianTransferSyntax(void);
134    bool IsJPEGBaseLineProcess1TransferSyntax(void);
135    bool IsJPEGExtendedProcess2_4TransferSyntax(void); 
136    bool IsJPEGExtendedProcess3_5TransferSyntax(void);
137    bool IsJPEGSpectralSelectionProcess6_8TransferSyntax(void); 
138    bool IsRLELossLessTransferSyntax(void); 
139    bool IsJPEGLossless(void); 
140    bool IsJPEG2000(void); 
141    bool IsDicomV3(void); 
142       
143    virtual void ParseHeader(bool exception_on_error = false)
144      throw(gdcmFormatError);
145      
146    gdcmHeader(bool exception_on_error = false);
147    gdcmHeader(const char *filename, 
148               bool  exception_on_error = false, 
149               bool  enable_sequences   = false);
150               
151    virtual ~gdcmHeader();
152
153    inline std::string GetFileName(void) {return filename;}
154    
155    size_t GetPixelOffset(void);
156    size_t GetPixelAreaLength(void);
157
158    inline int    GetSwapCode(void) { return sw; }
159   
160    // When some proprietary shadow groups are disclosed, we can set up
161    // an additional specific dictionary to access extra information.
162    
163    // OK : we still have *ONE* ElValSet, 
164    // with both Public and Shadow Elements
165    // parsed against THE Public Dictionary and A (single) Shadow Dictionary
166    
167    // TODO Swig int SetShaDict(std::string filename);
168    // TODO Swig int SetPubDict(std::string filename);
169
170    std::string GetPubElValByName     (std::string tagName);
171    std::string GetPubElValRepByName  (std::string tagName);
172    std::string GetPubElValByNumber   (guint16 group, guint16 element);
173    std::string GetPubElValRepByNumber(guint16 group, guint16 element);
174    
175    size_t GetPubElValOffsetByNumber  (guint16 Group, guint16 Elem);
176    void * GetPubElValVoidAreaByNumber(guint16 Group, guint16 Elem);   
177    void * LoadElementVoidArea        (guint16 Group, guint16 Element);
178    
179    inline ListTag      & GetListElem(void) { return PubElValSet.GetListElem();};
180    inline TagElValueHT & GetPubElVal(void) { return PubElValSet.GetTagHt();   };
181    
182    void   PrintPubElVal(std::ostream & os = std::cout);
183    void   PrintPubDict (std::ostream & os = std::cout);
184
185    bool SetPubElValByName  (std::string content, std::string tagName); 
186    bool SetPubElValByNumber(std::string content, guint16 group, guint16 element);  
187    bool SetPubElValLengthByNumber(guint32 lgr, guint16 group, guint16 element); 
188       
189    std::string GetElValByName     (std::string tagName);
190    std::string GetElValRepByName  (std::string tagName);
191    std::string GetElValByNumber   (guint16 group, guint16 element);
192    std::string GetElValRepByNumber(guint16 group, guint16 element);                                  
193
194    bool SetElValueByName(std::string content,std::string tagName); 
195    // moved from ElValSet
196                                     
197    bool ReplaceOrCreateByNumber(std::string Value, guint16 Group, guint16 Elem); 
198    bool ReplaceOrCreateByNumber(     char * Value, guint16 Group, guint16 Elem);                                
199    bool ReplaceIfExistByNumber (     char * Value, guint16 Group, guint16 Elem);
200                                   
201    bool Write(FILE *, FileType);
202    
203  // Some heuristic based accessors, end user intended 
204  // (to be move to gdcmHeaderHelper?) 
205   
206    int GetXSize(void);  
207    int GetYSize(void);
208    int GetZSize(void);
209    int GetBitsStored(void);
210    int GetBitsAllocated(void);
211    int GetSamplesPerPixel(void);   
212    int GetPlanarConfiguration(void);
213    int GetPixelSize(void);   
214        
215    std::string GetPixelType(void);  
216    
217    std::string GetTransferSyntaxName(void);
218    bool   HasLUT(void);
219    int    GetLUTNbits(void);
220    unsigned char * GetLUTRGBA(void);
221   
222   // voir gdcmFile::SetImageData ?!?         
223    void SetImageDataSize (size_t ExpectedSize);
224
225 // System access
226    guint16 SwapShort(guint16); // needed by gdcmFile
227    guint32 SwapLong(guint32);  // for JPEG Files
228 };
229
230 #endif