1 /*=========================================================================
4 Module: $RCSfile: gdcmDocument.h,v $
6 Date: $Date: 2004/12/07 13:39:33 $
7 Version: $Revision: 1.68 $
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
24 #include "gdcmElementSet.h"
37 class JPEGFragmentsInfo;
39 enum TransferSyntaxType {
40 ImplicitVRLittleEndian = 0,
41 ImplicitVRLittleEndianDLXGE,
42 ExplicitVRLittleEndian,
43 DeflatedExplicitVRLittleEndian,
46 JPEGExtendedProcess2_4,
47 JPEGExtendedProcess3_5,
48 JPEGSpectralSelectionProcess6_8,
49 JPEGFullProgressionProcess10_12,
50 JPEGLosslessProcess14,
51 JPEGLosslessProcess14_1,
58 //-----------------------------------------------------------------------------
60 * \brief Derived by both Header and DicomDir
62 class GDCM_EXPORT Document : public ElementSet
66 /// Public dictionary used to parse this header
69 /// \brief Optional "shadow dictionary" (private elements) used to parse
73 /// \brief Size threshold above which an element value will NOT be loaded
74 /// in memory (to avoid loading the image/volume itself). By default,
75 /// this upper bound is fixed to 1024 bytes (which might look reasonable
76 /// when one considers the definition of the various VR contents).
77 uint32_t MaxSizeLoadEntry;
79 /// \brief Size threshold above which an element value will NOT be *printed*
80 /// in order no to polute the screen output. By default, this upper bound
81 /// is fixed to 64 bytes.
82 uint32_t MaxSizePrintEntry;
85 /// Refering underlying filename.
88 /// \brief SWap code (e.g. Big Endian, Little Endian, Bad Big Endian,
89 /// Bad Little Endian) according to the processor Endianity and
90 /// what is written on disc.
93 /// File Pointer, opened during Header parsing.
96 /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
99 /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
100 static const unsigned int HEADER_LENGTH_TO_READ;
102 /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
104 static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
106 /// \brief Elements whose value is longer than MAX_SIZE_PRINT_ELEMENT_VALUE
108 /// \todo Currently not used since collides with #define in
109 /// class DocEntry . See also
110 /// method ref Document::SetMaxSizePrintEntry()
111 static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
113 /// Store the RLE frames info obtained during parsing of pixels.
114 RLEFramesInfo* RLEInfo;
116 /// Store the JPEG fragments info obtained during parsing of pixels.
117 JPEGFragmentsInfo* JPEGInfo;
119 /// \brief Amount of printed details for each Header Entry (Dicom Element):
120 /// 0 : stands for the least detail level.
124 // the 2 following will be merged
125 virtual void PrintPubDict (std::ostream &os = std::cout);
126 virtual void PrintShaDict (std::ostream &os = std::cout);
131 bool SetShaDict(Dict* dict);
132 bool SetShaDict(DictKey const & dictName);
134 // Informations contained in the parser
135 virtual bool IsReadable();
136 TransferSyntaxType GetTransferSyntax();
137 bool IsJPEGLossless();
140 bool IsEncapsulate();
143 FileType GetFileType();
145 std::ifstream * OpenFile();
148 void WriteContent( std::ofstream* fp, FileType type );
150 ValEntry* ReplaceOrCreateByNumber(std::string const & value,
151 uint16_t group, uint16_t elem,
152 TagName const & vr = "unkn");
154 BinEntry* ReplaceOrCreateByNumber(uint8_t* binArea, int lgth,
155 uint16_t group, uint16_t elem,
156 TagName const & vr = "unkn");
158 SeqEntry* ReplaceOrCreateByNumber(uint16_t group, uint16_t elem);
160 bool ReplaceIfExistByNumber ( std::string const & value,
161 uint16_t group, uint16_t elem );
163 virtual void LoadEntryBinArea(uint16_t group, uint16_t elem);
164 virtual void LoadEntryBinArea(BinEntry* entry);
166 // System access (meaning endian related !?)
167 uint16_t SwapShort(uint16_t); // needed by File
168 uint32_t SwapLong(uint32_t); // needed by File
169 uint16_t UnswapShort(uint16_t); // needed by File
170 uint32_t UnswapLong(uint32_t); // needed by File
172 static std::string GetTransferSyntaxValue(TransferSyntaxType type);
175 // Constructor and destructor are protected to forbid end user
176 // to instanciate from this class Document (only Header and
177 // DicomDir are meaningfull).
179 Document( std::string const & filename );
182 void ReadAndSkipEncapsulatedBasicOffsetTable();
183 void ComputeRLEInfo();
184 void ComputeJPEGFragmentInfo();
186 bool CheckIfEntryExistByNumber(uint16_t group, uint16_t elem );
188 virtual std::string GetEntryByName (TagName const & tagName);
189 virtual std::string GetEntryVRByName (TagName const & tagName);
190 virtual std::string GetEntryByNumber (uint16_t group, uint16_t elem);
191 virtual std::string GetEntryVRByNumber(uint16_t group, uint16_t elem);
192 virtual int GetEntryLengthByNumber(uint16_t group, uint16_t elem);
194 virtual bool SetEntryByName (std::string const & content,
195 TagName const & tagName );
196 virtual bool SetEntryByNumber(std::string const & content,
197 uint16_t group, uint16_t element);
198 virtual bool SetEntryByNumber(uint8_t* content, int lgth,
199 uint16_t group, uint16_t element);
200 virtual void* GetEntryBinAreaByNumber(uint16_t group, uint16_t elem);
202 // Verify the usefull of this method... otherwise remove it
203 // It's body is commented in the .xx
204 //virtual bool SetEntryBinAreaByNumber(uint8_t* a, uint16_t group,
207 virtual void UpdateShaEntries();
210 DocEntry* GetDocEntryByNumber(uint16_t group, uint16_t element);
211 DocEntry* GetDocEntryByName (TagName const & tagName);
213 ValEntry* GetValEntryByNumber(uint16_t group, uint16_t element);
214 //BinEntry* GetBinEntryByNumber(uint16_t group, uint16_t element);
215 RLEFramesInfo* GetRLEInfo() { return RLEInfo; }
216 JPEGFragmentsInfo* GetJPEGInfo() { return JPEGInfo; }
218 void LoadDocEntrySafe(DocEntry* entry);
219 TagDocEntryHT* BuildFlatHashTable();
223 void ParseDES(DocEntrySet *set,long offset, long l_max, bool delim_mode);
224 void ParseSQ (SeqEntry *seq, long offset, long l_max, bool delim_mode);
226 void LoadDocEntry (DocEntry *);
227 void FindDocEntryLength(DocEntry *) throw ( FormatError );
228 void FindDocEntryVR (DocEntry *);
229 bool CheckDocEntryVR (DocEntry *, VRKey);
231 std::string GetDocEntryValue (DocEntry *);
232 std::string GetDocEntryUnvalue(DocEntry *);
234 void SkipDocEntry (DocEntry *);
235 void SkipToNextDocEntry (DocEntry *);
237 void FixDocEntryFoundLength(DocEntry *, uint32_t);
238 bool IsDocEntryAnInteger (DocEntry *);
240 uint32_t FindDocEntryLengthOB() throw( FormatUnexpected );
242 uint16_t ReadInt16() throw ( FormatError );
243 uint32_t ReadInt32() throw ( FormatError );
244 void SkipBytes(uint32_t);
245 bool ReadTag(uint16_t, uint16_t);
246 uint32_t ReadTagLength(uint16_t, uint16_t);
250 void SwitchSwapToBigEndian();
251 void SetMaxSizeLoadEntry(long);
252 void SetMaxSizePrintEntry(long);
254 // DocEntry related utilities
255 DocEntry* ReadNextDocEntry();
257 uint32_t GenerateFreeTagKeyInGroup(uint16_t group);
258 void BuildFlatHashTableRecurse( TagDocEntryHT& builtHT,
261 void HandleBrokenEndian(uint16_t group, uint16_t elem);
264 /// Accessor to \ref PrintLevel
265 void SetPrintLevel(int level) { PrintLevel = level; }
267 /// Accessor to \ref Filename
268 const std::string &GetFileName() const { return Filename; }
270 /// Accessor to \ref Filename
271 void SetFileName(std::string const & fileName) { Filename = fileName; }
273 /// 'Swap code' accessor (see \ref SwapCode )
274 int GetSwapCode() { return SwapCode; }
276 bool operator<(Document &document);
279 } // end namespace gdcm
281 //-----------------------------------------------------------------------------