1 /*=========================================================================
4 Module: $RCSfile: gdcmDocument.h,v $
6 Date: $Date: 2004/11/17 19:49:13 $
7 Version: $Revision: 1.61 $
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 //-----------------------------------------------------------------------------
65 * \brief Derived by both Header and DicomDir
67 class GDCM_EXPORT Document : public ElementSet
71 /// Public dictionary used to parse this header
74 /// \brief Optional "shadow dictionary" (private elements) used to parse
78 /// \brief Size threshold above which an element value will NOT be loaded
79 /// in memory (to avoid loading the image/volume itself). By default,
80 /// this upper bound is fixed to 1024 bytes (which might look reasonable
81 /// when one considers the definition of the various VR contents).
82 uint32_t MaxSizeLoadEntry;
84 /// \brief Size threshold above which an element value will NOT be *printed*
85 /// in order no to polute the screen output. By default, this upper bound
86 /// is fixed to 64 bytes.
87 uint32_t MaxSizePrintEntry;
90 /// Refering underlying filename.
93 /// \brief SWap code (e.g. Big Endian, Little Endian, Bad Big Endian,
94 /// Bad Little Endian) according to the processor Endianity and
95 /// what is written on disc.
98 /// File Pointer, opened during Header parsing.
101 /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
104 /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
105 static const unsigned int HEADER_LENGTH_TO_READ;
107 /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
109 static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
111 /// \brief Elements whose value is longer than MAX_SIZE_PRINT_ELEMENT_VALUE
113 /// \todo Currently not used since collides with #define in
114 /// class DocEntry . See also
115 /// method ref Document::SetMaxSizePrintEntry()
116 static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
118 /// Store the RLE frames info obtained during parsing of pixels.
119 RLEFramesInfo* RLEInfo;
121 /// Store the JPEG fragments info obtained during parsing of pixels.
122 JPEGFragmentsInfo* JPEGInfo;
124 /// \brief Amount of printed details for each Header Entry (Dicom Element):
125 /// 0 : stands for the least detail level.
129 // the 2 following will be merged
130 virtual void PrintPubDict (std::ostream &os = std::cout);
131 virtual void PrintShaDict (std::ostream &os = std::cout);
136 bool SetShaDict(Dict* dict);
137 bool SetShaDict(DictKey const & dictName);
139 // Informations contained in the parser
140 virtual bool IsReadable();
141 TransferSyntaxType GetTransferSyntax();
142 bool IsJPEGLossless();
145 bool IsEncapsulate();
148 FileType GetFileType();
150 std::ifstream * OpenFile();
153 void Write( std::ofstream* fp, FileType type );
155 ValEntry* ReplaceOrCreateByNumber(std::string const & value,
156 uint16_t group, uint16_t elem,
157 TagName const & vr = "unkn");
159 BinEntry* ReplaceOrCreateByNumber(uint8_t* binArea, int lgth,
160 uint16_t group, uint16_t elem,
161 TagName const & vr = "unkn");
163 SeqEntry* ReplaceOrCreateByNumber(uint16_t group, uint16_t elem);
165 bool ReplaceIfExistByNumber ( std::string const & value,
166 uint16_t group, uint16_t elem );
168 virtual void LoadEntryBinArea(uint16_t group, uint16_t elem);
169 virtual void LoadEntryBinArea(BinEntry* entry);
171 // System access (meaning endian related !?)
172 uint16_t SwapShort(uint16_t); // needed by File
173 uint32_t SwapLong(uint32_t); // needed by File
174 uint16_t UnswapShort(uint16_t); // needed by File
175 uint32_t UnswapLong(uint32_t); // needed by File
178 // Constructor and destructor are protected to forbid end user
179 // to instanciate from this class Document (only Header and
180 // DicomDir are meaningfull).
182 Document( std::string const & filename );
185 void ReadAndSkipEncapsulatedBasicOffsetTable();
186 void ComputeRLEInfo();
187 void ComputeJPEGFragmentInfo();
189 bool CheckIfEntryExistByNumber(uint16_t group, uint16_t elem );
191 virtual std::string GetEntryByName (TagName const & tagName);
192 virtual std::string GetEntryVRByName (TagName const & tagName);
193 virtual std::string GetEntryByNumber (uint16_t group, uint16_t elem);
194 virtual std::string GetEntryVRByNumber(uint16_t group, uint16_t elem);
195 virtual int GetEntryLengthByNumber(uint16_t group, uint16_t elem);
197 virtual bool SetEntryByName ( std::string const & content,
198 TagName const & tagName );
199 virtual bool SetEntryByNumber(std::string const & content,
200 uint16_t group, uint16_t element);
201 virtual bool SetEntryByNumber(uint8_t* content, int lgth,
202 uint16_t group, uint16_t element);
203 virtual bool SetEntryLengthByNumber(uint32_t length,
204 uint16_t group, uint16_t element);
206 virtual size_t GetEntryOffsetByNumber(uint16_t group, uint16_t elem);
207 virtual void* GetEntryBinAreaByNumber(uint16_t group, uint16_t elem);
208 virtual bool SetEntryBinAreaByNumber(uint8_t* a, uint16_t group,
211 virtual void UpdateShaEntries();
214 DocEntry* GetDocEntryByNumber(uint16_t group, uint16_t element);
215 DocEntry* GetDocEntryByName (TagName const & tagName);
217 ValEntry* GetValEntryByNumber(uint16_t group, uint16_t element);
218 //BinEntry* GetBinEntryByNumber(uint16_t group, uint16_t element);
219 RLEFramesInfo* GetRLEInfo() { return RLEInfo; }
220 JPEGFragmentsInfo* GetJPEGInfo() { return JPEGInfo; }
222 void LoadDocEntrySafe(DocEntry* entry);
223 TagDocEntryHT* BuildFlatHashTable();
227 void ParseDES(DocEntrySet *set,long offset, long l_max, bool delim_mode);
228 void ParseSQ (SeqEntry *seq, long offset, long l_max, bool delim_mode);
230 void LoadDocEntry (DocEntry *);
231 void FindDocEntryLength(DocEntry *) throw ( FormatError );
232 void FindDocEntryVR (DocEntry *);
233 bool CheckDocEntryVR (DocEntry *, VRKey);
235 std::string GetDocEntryValue (DocEntry *);
236 std::string GetDocEntryUnvalue(DocEntry *);
238 void SkipDocEntry (DocEntry *);
239 void SkipToNextDocEntry (DocEntry *);
241 void FixDocEntryFoundLength(DocEntry *, uint32_t);
242 bool IsDocEntryAnInteger (DocEntry *);
244 uint32_t FindDocEntryLengthOB() throw( FormatUnexpected );
246 uint16_t ReadInt16() throw ( FormatError );
247 uint32_t ReadInt32() throw ( FormatError );
248 void SkipBytes(uint32_t);
249 bool ReadTag(uint16_t, uint16_t);
250 uint32_t ReadTagLength(uint16_t, uint16_t);
254 void SwitchSwapToBigEndian();
255 void SetMaxSizeLoadEntry(long);
256 void SetMaxSizePrintEntry(long);
258 // DocEntry related utilities
259 DocEntry* ReadNextDocEntry();
261 uint32_t GenerateFreeTagKeyInGroup(uint16_t group);
262 void BuildFlatHashTableRecurse( TagDocEntryHT& builtHT,
265 void HandleBrokenEndian(uint16_t group, uint16_t elem);
268 /// Accessor to \ref PrintLevel
269 void SetPrintLevel(int level) { PrintLevel = level; }
271 /// Accessor to \ref Filename
272 const std::string &GetFileName() const { return Filename; }
274 /// Accessor to \ref Filename
275 void SetFileName(std::string const & fileName) { Filename = fileName; }
277 /// 'Swap code' accessor (see \ref SwapCode )
278 int GetSwapCode() { return SwapCode; }
281 std::ifstream * GetFP() { return Fp; }
283 bool operator<(Document &document);
286 } // end namespace gdcm
288 //-----------------------------------------------------------------------------