1 /*=========================================================================
4 Module: $RCSfile: gdcmDocument.h,v $
6 Date: $Date: 2004/11/25 13:12:02 $
7 Version: $Revision: 1.63 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
19 #ifndef GDCMDOCUMENT_H
20 #define GDCMDOCUMENT_H
22 #include "gdcmCommon.h"
25 #include "gdcmException.h"
26 #include "gdcmDictSet.h"
27 #include "gdcmDocEntry.h"
28 #include "gdcmRLEFramesInfo.h"
29 #include "gdcmJPEGFragmentsInfo.h"
30 #include "gdcmDocEntrySet.h"
31 #include "gdcmElementSet.h"
44 enum TransferSyntaxType {
45 ImplicitVRLittleEndian = 0,
46 ImplicitVRLittleEndianDLXGE,
47 ExplicitVRLittleEndian,
48 DeflatedExplicitVRLittleEndian,
51 JPEGExtendedProcess2_4,
52 JPEGExtendedProcess3_5,
53 JPEGSpectralSelectionProcess6_8,
54 JPEGFullProgressionProcess10_12,
55 JPEGLosslessProcess14,
56 JPEGLosslessProcess14_1,
63 static const char *TransferSyntaxStrings[] = {
64 // Implicit VR Little Endian
66 // Implicit VR Little Endian DLX G.E?
68 // Explicit VR Little Endian
69 "1.2.840.10008.1.2.1",
70 // Deflated Explicit VR Little Endian
71 "1.2.840.10008.1.2.1.99",
72 // Explicit VR Big Endian
73 "1.2.840.10008.1.2.2",
74 // JPEG Baseline (Process 1)
75 "1.2.840.10008.1.2.4.50",
76 // JPEG Extended (Process 2 & 4)
77 "1.2.840.10008.1.2.4.51",
78 // JPEG Extended (Process 3 & 5)
79 "1.2.840.10008.1.2.4.52",
80 // JPEG Spectral Selection, Non-Hierarchical (Process 6 & 8)
81 "1.2.840.10008.1.2.4.53",
82 // JPEG Full Progression, Non-Hierarchical (Process 10 & 12)
83 "1.2.840.10008.1.2.4.55",
84 // JPEG Lossless, Non-Hierarchical (Process 14)
85 "1.2.840.10008.1.2.4.57",
86 // JPEG Lossless, Hierarchical, First-Order Prediction (Process 14, [Selection Value 1])
87 "1.2.840.10008.1.2.4.70",
89 "1.2.840.10008.1.2.4.90",
91 "1.2.840.10008.1.2.4.91",
93 "1.2.840.10008.1.2.5",
95 "Unknown Transfer Syntax"
98 //-----------------------------------------------------------------------------
100 * \brief Derived by both Header and DicomDir
102 class GDCM_EXPORT Document : public ElementSet
106 /// Public dictionary used to parse this header
109 /// \brief Optional "shadow dictionary" (private elements) used to parse
113 /// \brief Size threshold above which an element value will NOT be loaded
114 /// in memory (to avoid loading the image/volume itself). By default,
115 /// this upper bound is fixed to 1024 bytes (which might look reasonable
116 /// when one considers the definition of the various VR contents).
117 uint32_t MaxSizeLoadEntry;
119 /// \brief Size threshold above which an element value will NOT be *printed*
120 /// in order no to polute the screen output. By default, this upper bound
121 /// is fixed to 64 bytes.
122 uint32_t MaxSizePrintEntry;
125 /// Refering underlying filename.
126 std::string Filename;
128 /// \brief SWap code (e.g. Big Endian, Little Endian, Bad Big Endian,
129 /// Bad Little Endian) according to the processor Endianity and
130 /// what is written on disc.
133 /// File Pointer, opened during Header parsing.
136 /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
139 /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
140 static const unsigned int HEADER_LENGTH_TO_READ;
142 /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
144 static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
146 /// \brief Elements whose value is longer than MAX_SIZE_PRINT_ELEMENT_VALUE
148 /// \todo Currently not used since collides with #define in
149 /// class DocEntry . See also
150 /// method ref Document::SetMaxSizePrintEntry()
151 static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
153 /// Store the RLE frames info obtained during parsing of pixels.
154 RLEFramesInfo* RLEInfo;
156 /// Store the JPEG fragments info obtained during parsing of pixels.
157 JPEGFragmentsInfo* JPEGInfo;
159 /// \brief Amount of printed details for each Header Entry (Dicom Element):
160 /// 0 : stands for the least detail level.
164 // the 2 following will be merged
165 virtual void PrintPubDict (std::ostream &os = std::cout);
166 virtual void PrintShaDict (std::ostream &os = std::cout);
171 bool SetShaDict(Dict* dict);
172 bool SetShaDict(DictKey const & dictName);
174 // Informations contained in the parser
175 virtual bool IsReadable();
176 TransferSyntaxType GetTransferSyntax();
177 bool IsJPEGLossless();
180 bool IsEncapsulate();
183 FileType GetFileType();
185 std::ifstream * OpenFile();
188 void Write( std::ofstream* fp, FileType type );
190 ValEntry* ReplaceOrCreateByNumber(std::string const & value,
191 uint16_t group, uint16_t elem,
192 TagName const & vr = "unkn");
194 BinEntry* ReplaceOrCreateByNumber(uint8_t* binArea, int lgth,
195 uint16_t group, uint16_t elem,
196 TagName const & vr = "unkn");
198 SeqEntry* ReplaceOrCreateByNumber(uint16_t group, uint16_t elem);
200 bool ReplaceIfExistByNumber ( std::string const & value,
201 uint16_t group, uint16_t elem );
203 virtual void LoadEntryBinArea(uint16_t group, uint16_t elem);
204 virtual void LoadEntryBinArea(BinEntry* entry);
206 // System access (meaning endian related !?)
207 uint16_t SwapShort(uint16_t); // needed by File
208 uint32_t SwapLong(uint32_t); // needed by File
209 uint16_t UnswapShort(uint16_t); // needed by File
210 uint32_t UnswapLong(uint32_t); // needed by File
213 // Constructor and destructor are protected to forbid end user
214 // to instanciate from this class Document (only Header and
215 // DicomDir are meaningfull).
217 Document( std::string const & filename );
220 void ReadAndSkipEncapsulatedBasicOffsetTable();
221 void ComputeRLEInfo();
222 void ComputeJPEGFragmentInfo();
224 bool CheckIfEntryExistByNumber(uint16_t group, uint16_t elem );
226 virtual std::string GetEntryByName (TagName const & tagName);
227 virtual std::string GetEntryVRByName (TagName const & tagName);
228 virtual std::string GetEntryByNumber (uint16_t group, uint16_t elem);
229 virtual std::string GetEntryVRByNumber(uint16_t group, uint16_t elem);
230 virtual int GetEntryLengthByNumber(uint16_t group, uint16_t elem);
232 virtual bool SetEntryByName ( std::string const & content,
233 TagName const & tagName );
234 virtual bool SetEntryByNumber(std::string const & content,
235 uint16_t group, uint16_t element);
236 virtual bool SetEntryByNumber(uint8_t* content, int lgth,
237 uint16_t group, uint16_t element);
238 virtual bool SetEntryLengthByNumber(uint32_t length,
239 uint16_t group, uint16_t element);
241 virtual size_t GetEntryOffsetByNumber(uint16_t group, uint16_t elem);
242 virtual void* GetEntryBinAreaByNumber(uint16_t group, uint16_t elem);
243 virtual bool SetEntryBinAreaByNumber(uint8_t* a, uint16_t group,
246 virtual void UpdateShaEntries();
249 DocEntry* GetDocEntryByNumber(uint16_t group, uint16_t element);
250 DocEntry* GetDocEntryByName (TagName const & tagName);
252 ValEntry* GetValEntryByNumber(uint16_t group, uint16_t element);
253 //BinEntry* GetBinEntryByNumber(uint16_t group, uint16_t element);
254 RLEFramesInfo* GetRLEInfo() { return RLEInfo; }
255 JPEGFragmentsInfo* GetJPEGInfo() { return JPEGInfo; }
257 void LoadDocEntrySafe(DocEntry* entry);
258 TagDocEntryHT* BuildFlatHashTable();
262 void ParseDES(DocEntrySet *set,long offset, long l_max, bool delim_mode);
263 void ParseSQ (SeqEntry *seq, long offset, long l_max, bool delim_mode);
265 void LoadDocEntry (DocEntry *);
266 void FindDocEntryLength(DocEntry *) throw ( FormatError );
267 void FindDocEntryVR (DocEntry *);
268 bool CheckDocEntryVR (DocEntry *, VRKey);
270 std::string GetDocEntryValue (DocEntry *);
271 std::string GetDocEntryUnvalue(DocEntry *);
273 void SkipDocEntry (DocEntry *);
274 void SkipToNextDocEntry (DocEntry *);
276 void FixDocEntryFoundLength(DocEntry *, uint32_t);
277 bool IsDocEntryAnInteger (DocEntry *);
279 uint32_t FindDocEntryLengthOB() throw( FormatUnexpected );
281 uint16_t ReadInt16() throw ( FormatError );
282 uint32_t ReadInt32() throw ( FormatError );
283 void SkipBytes(uint32_t);
284 bool ReadTag(uint16_t, uint16_t);
285 uint32_t ReadTagLength(uint16_t, uint16_t);
289 void SwitchSwapToBigEndian();
290 void SetMaxSizeLoadEntry(long);
291 void SetMaxSizePrintEntry(long);
293 // DocEntry related utilities
294 DocEntry* ReadNextDocEntry();
296 uint32_t GenerateFreeTagKeyInGroup(uint16_t group);
297 void BuildFlatHashTableRecurse( TagDocEntryHT& builtHT,
300 void HandleBrokenEndian(uint16_t group, uint16_t elem);
303 /// Accessor to \ref PrintLevel
304 void SetPrintLevel(int level) { PrintLevel = level; }
306 /// Accessor to \ref Filename
307 const std::string &GetFileName() const { return Filename; }
309 /// Accessor to \ref Filename
310 void SetFileName(std::string const & fileName) { Filename = fileName; }
312 /// 'Swap code' accessor (see \ref SwapCode )
313 int GetSwapCode() { return SwapCode; }
315 bool operator<(Document &document);
318 } // end namespace gdcm
320 //-----------------------------------------------------------------------------