2 //-----------------------------------------------------------------------------
6 #include "gdcmCommon.h"
9 #include "gdcmException.h"
10 #include "gdcmDictSet.h"
11 #include "gdcmElValue.h"
12 #include "gdcmElValSet.h"
15 //-----------------------------------------------------------------------------
16 typedef std::string VRKey;
17 typedef std::string VRAtr;
18 typedef std::map<VRKey, VRAtr> VRHT; // Value Representation Hash Table
20 //-----------------------------------------------------------------------------
22 * The purpose of an instance of gdcmHeader is to act as a container of
23 * all the DICOM elements and their corresponding values (and
24 * additionaly the corresponding DICOM dictionary entry) of the header
27 * The typical usage of instances of class gdcmHeader is to classify a set of
28 * dicom files according to header information e.g. to create a file hierarchy
29 * reflecting the Patient/Study/Serie informations, or extracting a given
30 * SerieId. Accesing the content (image[s] or volume[s]) is beyond the
31 * functionality of this class and belongs to gdmcFile.
32 * \note The various entries of the explicit value representation (VR) shall
33 * be managed within a dictionary which is shared by all gdcmHeader
35 * \note The gdcmHeader::Set*Tag* family members cannot be defined as
36 * protected due to Swig limitations for as Has_a dependency between
37 * gdcmFile and gdcmHeader.
39 class GDCM_EXPORT gdcmHeader {
41 gdcmHeader(bool exception_on_error = false);
42 gdcmHeader(const char *filename,
43 bool exception_on_error = false,
44 bool enable_sequences = false);
46 virtual ~gdcmHeader();
48 // Standard values and informations contained in the header
49 inline std::string GetFileName(void) {return filename;}
51 bool IsReadable(void);
52 bool IsImplicitVRLittleEndianTransferSyntax(void);
53 bool IsExplicitVRLittleEndianTransferSyntax(void);
54 bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
55 bool IsExplicitVRBigEndianTransferSyntax(void);
56 bool IsJPEGBaseLineProcess1TransferSyntax(void);
57 bool IsJPEGExtendedProcess2_4TransferSyntax(void);
58 bool IsJPEGExtendedProcess3_5TransferSyntax(void);
59 bool IsJPEGSpectralSelectionProcess6_8TransferSyntax(void);
60 bool IsRLELossLessTransferSyntax(void);
61 bool IsJPEGLossless(void);
62 bool IsJPEG2000(void);
64 FileType GetFileType(void);
66 // Some heuristic based accessors, end user intended
67 // (to be move to gdcmHeaderHelper?)
71 int GetBitsStored(void);
72 int GetBitsAllocated(void);
73 int GetSamplesPerPixel(void);
74 int GetPlanarConfiguration(void);
76 int GetPixelSize(void);
77 std::string GetPixelType(void);
78 size_t GetPixelOffset(void);
79 size_t GetPixelAreaLength(void);
82 int GetLUTNbits(void);
83 unsigned char * GetLUTRGBA(void);
85 std::string GetTransfertSyntaxName(void);
87 // When some proprietary shadow groups are disclosed, we can set up
88 // an additional specific dictionary to access extra information.
90 // OK : we still have *ONE* ElValSet,
91 // with both Public and Shadow Elements
92 // parsed against THE Public Dictionary and A (single) Shadow Dictionary
94 // TODO Swig int SetShaDict(std::string filename);
95 // TODO Swig int SetPubDict(std::string filename);
97 // Public element value
98 std::string GetPubElValByName (std::string tagName);
99 std::string GetPubElValRepByName (std::string tagName);
100 std::string GetPubElValByNumber (guint16 group, guint16 element);
101 std::string GetPubElValRepByNumber(guint16 group, guint16 element);
103 bool SetPubElValByName (std::string content, std::string tagName);
104 bool SetPubElValByNumber(std::string content, guint16 group, guint16 element);
105 bool SetPubElValLengthByNumber(guint32 lgr, guint16 group, guint16 element);
107 inline ListTag & GetPubListElem(void) { return PubElValSet.GetListElem();};
108 inline TagElValueHT & GetPubElVal(void) { return PubElValSet.GetTagHt(); };
110 void PrintPubElVal(std::ostream & os = std::cout);
111 void PrintPubDict (std::ostream & os = std::cout);
114 std::string GetElValByName (std::string tagName);
115 std::string GetElValRepByName (std::string tagName);
116 std::string GetElValByNumber (guint16 group, guint16 element);
117 std::string GetElValRepByNumber(guint16 group, guint16 element);
119 bool SetElValueByName(std::string content,std::string tagName);
120 // bool SetElValueByNumber(std::string content,guint16 group, guint16 element);
122 // inline ListTag & GetListElem(void) { return PubElValSet.GetListElem();};
123 // inline TagElValueHT & GetElVal(void) { return PubElValSet.GetTagHt(); };
125 // Read (used in gdcmFile)
126 FILE *OpenFile(bool exception_on_error = false) throw(gdcmFileError);
127 bool CloseFile(void);
128 virtual void ParseHeader(bool exception_on_error = false) throw(gdcmFormatError);
130 // Write (used in gdcmFile)
131 bool Write(FILE *, FileType);
132 void SetImageDataSize(size_t ExpectedSize);
135 inline int GetSwapCode(void) { return sw; }
136 guint16 SwapShort(guint16); // needed by gdcmFile
137 guint32 SwapLong(guint32); // for JPEG Files
140 gdcmElValue * GetElValueByNumber(guint16 group, guint16 element);
141 bool CheckIfExistByNumber(guint16 Group, guint16 Elem );
143 int write(std::ostream&);
144 int anonymize(std::ostream&); // FIXME : anonymize should be a friend ?
146 size_t GetPubElValOffsetByNumber (guint16 Group, guint16 Elem);
147 void * GetPubElValVoidAreaByNumber(guint16 Group, guint16 Elem);
148 void * LoadElementVoidArea (guint16 Group, guint16 Element);
150 bool ReplaceOrCreateByNumber(std::string Value, guint16 Group, guint16 Elem);
151 bool ReplaceOrCreateByNumber( char * Value, guint16 Group, guint16 Elem);
152 bool ReplaceIfExistByNumber ( char * Value, guint16 Group, guint16 Elem);
156 FileType filetype; // ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
160 void LoadElements (void);
161 void LoadElementValue (gdcmElValue *);
162 void LoadElementValueSafe(gdcmElValue *);
163 void FindLength (gdcmElValue *);
164 void FindVR (gdcmElValue *);
166 guint32 FindLengthOB(void);
168 void SkipElementValue (gdcmElValue *);
169 void FixFoundLength (gdcmElValue *, guint32);
170 bool IsAnInteger (gdcmElValue *);
172 guint16 ReadInt16(void);
173 guint32 ReadInt32(void);
174 void SkipBytes(guint32);
176 void Initialise(void);
177 void CheckSwap(void);
178 void SwitchSwapToBigEndian(void);
179 void SetMaxSizeLoadElementValue(long);
182 gdcmDictEntry *GetDictEntryByName (std::string Name);
183 gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
185 // ElValue related utilities
186 gdcmElValue *ReadNextElement (void);
187 gdcmElValue *NewElValueByNumber(guint16 group, guint16 element);
188 gdcmElValue *NewElValueByName (std::string Name);
189 gdcmElValue* GetElementByName (std::string Name);
192 gdcmElValue *NewManualElValToPubDict(std::string NewTagName,
196 // Pointer to the Value Representation Hash Table which contains all
197 // the VR of the DICOM version3 public dictionary.
198 gdcmVR *dicom_vr; // Not a class member for thread-safety reasons
200 // Pointer to the Transfert Syntax Hash Table which contains all
201 // the TS of the DICOM version3 public dictionary.
202 gdcmTS *dicom_ts; // Not a class member for thread-safety reasons
204 // Pointer to global dictionary container
205 gdcmDictSet *Dicts; // Not a class member for thread-safety reasons
207 // Public dictionary used to parse this header
208 gdcmDict *RefPubDict;
210 // Optional "shadow dictionary" (private elements) used to parse this
212 gdcmDict *RefShaDict;
214 /// ELement VALueS parsed with the PUBlic dictionary.
215 gdcmElValSet PubElValSet;
217 // Refering underlying filename.
218 std::string filename;
222 // FIXME sw should be an enum e.g.
228 // Swap code e.g. little, big, bad-big, bad-little endian). Warning:
229 // this code is not fixed during header parsing.
232 // Size treshold above which an element value will NOT be loaded in
233 // memory (to avoid loading the image/volume itself). By default,
234 // this upper bound is fixed to 1024 bytes (which might look reasonable
235 // when one considers the definition of the various VR contents).
236 guint32 MaxSizeLoadElementValue;
238 static const unsigned int HEADER_LENGTH_TO_READ;
239 static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
242 //-----------------------------------------------------------------------------