1 /*=========================================================================
4 Module: $RCSfile: gdcmDocument.h,v $
6 Date: $Date: 2004/10/22 03:05:41 $
7 Version: $Revision: 1.53 $
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 //-----------------------------------------------------------------------------
46 * \brief Derived by both Header and DicomDir
48 class GDCM_EXPORT Document : public ElementSet
52 /// Public dictionary used to parse this header
55 /// \brief Optional "shadow dictionary" (private elements) used to parse
59 /// \brief Size threshold above which an element value will NOT be loaded
60 /// in memory (to avoid loading the image/volume itself). By default,
61 /// this upper bound is fixed to 1024 bytes (which might look reasonable
62 /// when one considers the definition of the various VR contents).
63 uint32_t MaxSizeLoadEntry;
65 /// \brief Size threshold above which an element value will NOT be *printed*
66 /// in order no to polute the screen output. By default, this upper bound
67 /// is fixed to 64 bytes.
68 uint32_t MaxSizePrintEntry;
71 /// Refering underlying filename.
74 /// \brief SWap code (e.g. Big Endian, Little Endian, Bad Big Endian,
75 /// Bad Little Endian) according to the processor Endianity and
76 /// what is written on disc.
79 /// File Pointer, opened during Header parsing.
82 /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
85 /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
86 static const unsigned int HEADER_LENGTH_TO_READ;
88 /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
90 static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
92 /// \brief Elements whose value is longer than MAX_SIZE_PRINT_ELEMENT_VALUE
94 /// \todo Currently not used since collides with #define in
95 /// class DocEntry . See also
96 /// method ref Document::SetMaxSizePrintEntry()
97 static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
99 /// Store the RLE frames info obtained during parsing of pixels.
100 RLEFramesInfo* RLEInfo;
102 /// Store the JPEG fragments info obtained during parsing of pixels.
103 JPEGFragmentsInfo* JPEGInfo;
105 /// \brief Amount of printed details for each Header Entry (Dicom Element):
106 /// 0 : stands for the least detail level.
110 // the 2 following will be merged
111 virtual void PrintPubDict (std::ostream &os = std::cout);
112 virtual void PrintShaDict (std::ostream &os = std::cout);
117 bool SetShaDict(Dict* dict);
118 bool SetShaDict(DictKey const & dictName);
120 // Informations contained in the parser
121 virtual bool IsReadable();
122 bool IsGivenTransferSyntax(std::string const & syntaxToCheck);
123 bool IsImplicitVRLittleEndianTransferSyntax();
124 bool IsExplicitVRLittleEndianTransferSyntax();
125 bool IsDeflatedExplicitVRLittleEndianTransferSyntax();
126 bool IsExplicitVRBigEndianTransferSyntax();
127 bool IsRLELossLessTransferSyntax();
128 bool IsJPEGBaseLineProcess1TransferSyntax();
129 bool IsJPEGExtendedProcess2_4TransferSyntax();
130 bool IsJPEGExtendedProcess3_5TransferSyntax();
131 bool IsJPEGSpectralSelectionProcess6_8TransferSyntax();
132 bool IsJPEGLossless();
134 bool IsJPEGTransferSyntax();
135 bool IsEncapsulateTransferSyntax();
138 FileType GetFileType();
140 std::ifstream * OpenFile();
143 void Write( std::ofstream* fp, FileType type );
145 ValEntry* ReplaceOrCreateByNumber(std::string const & value,
146 uint16_t group, uint16_t elem,
147 std::string const & VR ="unkn");
149 BinEntry* ReplaceOrCreateByNumber(uint8_t* binArea, int lgth,
150 uint16_t group, uint16_t elem,
151 std::string const & VR="unkn");
153 SeqEntry* ReplaceOrCreateByNumber(uint16_t group, uint16_t elem);
155 bool ReplaceIfExistByNumber ( std::string const & value,
159 virtual void* LoadEntryBinArea(uint16_t group, uint16_t elem);
160 virtual void* LoadEntryBinArea(BinEntry* entry);
162 // System access (meaning endian related !?)
163 uint16_t SwapShort(uint16_t); // needed by File
164 uint32_t SwapLong(uint32_t); // needed by File
165 uint16_t UnswapShort(uint16_t); // needed by File
166 uint32_t UnswapLong(uint32_t); // needed by File
169 // Constructor and destructor are protected to forbid end user
170 // to instanciate from this class Document (only Header and
171 // DicomDir are meaningfull).
173 Document( std::string const & filename );
176 void ReadAndSkipEncapsulatedBasicOffsetTable();
177 void ComputeRLEInfo();
178 void ComputeJPEGFragmentInfo();
180 bool CheckIfEntryExistByNumber(uint16_t group, uint16_t elem );
182 virtual std::string GetEntryByName (TagName const & tagName);
183 virtual std::string GetEntryVRByName (TagName const & tagName);
184 virtual std::string GetEntryByNumber (uint16_t group, uint16_t elem);
185 virtual std::string GetEntryVRByNumber(uint16_t group, uint16_t elem);
186 virtual int GetEntryLengthByNumber(uint16_t group, uint16_t elem);
188 virtual bool SetEntryByName (std::string const & content,
189 std::string const & tagName);
190 virtual bool SetEntryByNumber(std::string const & content,
191 uint16_t group, uint16_t element);
192 virtual bool SetEntryByNumber(uint8_t* content, int lgth,
193 uint16_t group, uint16_t element);
194 virtual bool SetEntryLengthByNumber(uint32_t length,
195 uint16_t group, uint16_t element);
197 virtual size_t GetEntryOffsetByNumber (uint16_t group, uint16_t elem);
198 virtual void* GetEntryBinAreaByNumber(uint16_t group, uint16_t elem);
199 virtual bool SetEntryBinAreaByNumber(uint8_t* a, uint16_t group,
202 virtual void UpdateShaEntries();
205 DocEntry* GetDocEntryByNumber(uint16_t group, uint16_t element);
206 DocEntry* GetDocEntryByName (std::string const & tagName);
208 ValEntry* GetValEntryByNumber(uint16_t group, uint16_t element);
209 //BinEntry* GetBinEntryByNumber(uint16_t group, uint16_t element);
210 RLEFramesInfo* GetRLEInfo() { return RLEInfo; }
211 JPEGFragmentsInfo* GetJPEGInfo() { return JPEGInfo; }
213 void LoadDocEntrySafe(DocEntry* entry);
214 TagDocEntryHT* BuildFlatHashTable();
218 void ParseDES(DocEntrySet *set,long offset, long l_max, bool delim_mode);
219 void ParseSQ (SeqEntry *seq, long offset, long l_max, bool delim_mode);
221 void LoadDocEntry (DocEntry *);
222 void FindDocEntryLength(DocEntry *) throw ( FormatError );
223 void FindDocEntryVR (DocEntry *);
224 bool CheckDocEntryVR (DocEntry *, VRKey);
226 std::string GetDocEntryValue (DocEntry *);
227 std::string GetDocEntryUnvalue(DocEntry *);
229 void SkipDocEntry (DocEntry *);
230 void SkipToNextDocEntry (DocEntry *);
232 void FixDocEntryFoundLength(DocEntry *, uint32_t);
233 bool IsDocEntryAnInteger (DocEntry *);
235 uint32_t FindDocEntryLengthOB() throw( FormatUnexpected );
237 uint16_t ReadInt16() throw ( FormatError );
238 uint32_t ReadInt32() throw ( FormatError );
239 void SkipBytes(uint32_t);
240 bool ReadTag(uint16_t, uint16_t);
241 uint32_t ReadTagLength(uint16_t, uint16_t);
245 void SwitchSwapToBigEndian();
246 void SetMaxSizeLoadEntry(long);
247 void SetMaxSizePrintEntry(long);
249 // DocEntry related utilities
250 DocEntry* ReadNextDocEntry();
252 uint32_t GenerateFreeTagKeyInGroup(uint16_t group);
253 void BuildFlatHashTableRecurse( TagDocEntryHT& builtHT,
259 /// Accessor to \ref PrintLevel
260 void SetPrintLevel(int level) { PrintLevel = level; }
262 /// Accessor to \ref Filename
263 const std::string &GetFileName() { return Filename; }
265 /// Accessor to \ref Filename
266 void SetFileName(std::string const & fileName) { Filename = fileName; }
268 /// 'Swap code' accessor (see \ref SwapCode )
269 int GetSwapCode() { return SwapCode; }
272 std::ifstream * GetFP() { return Fp; }
274 bool operator<(Document &document);
277 } // end namespace gdcm
279 //-----------------------------------------------------------------------------