1 /*=========================================================================
4 Module: $RCSfile: gdcmDocument.h,v $
6 Date: $Date: 2005/01/11 23:06:35 $
7 Version: $Revision: 1.83 $
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 //-----------------------------------------------------------------------------
41 * \brief Derived by both Header and DicomDir
43 class GDCM_EXPORT Document : public ElementSet
46 // Informations contained in the parser
47 virtual bool IsReadable();
48 FileType GetFileType();
50 std::string GetTransferSyntax();
54 RLEFramesInfo *GetRLEInfo() { return RLEInfo; }
55 JPEGFragmentsInfo *GetJPEGInfo() { return JPEGInfo; }
58 virtual void PrintPubDict (std::ostream &os = std::cout);
59 virtual void PrintShaDict (std::ostream &os = std::cout);
63 bool SetShaDict(Dict* dict);
64 bool SetShaDict(DictKey const &dictName);
67 /// 'Swap code' accessor (see \ref SwapCode )
68 int GetSwapCode() { return SwapCode; }
69 // System access (meaning endian related !?)
70 uint16_t SwapShort(uint16_t); // needed by File
71 uint32_t SwapLong(uint32_t); // needed by File
72 uint16_t UnswapShort(uint16_t); // needed by File
73 uint32_t UnswapLong(uint32_t); // needed by File
75 // Ordering of Documents
76 bool operator<(Document &document);
80 /// Accessor to \ref Filename
81 const std::string &GetFileName() const { return Filename; }
82 /// Accessor to \ref Filename
83 void SetFileName(std::string const &fileName) { Filename = fileName; }
85 std::ifstream *OpenFile();
87 void WriteContent( std::ofstream *fp, FileType type );
91 virtual bool SetEntry(std::string const &content,
92 uint16_t group, uint16_t element);
93 virtual bool SetEntry(uint8_t *content, int lgth,
94 uint16_t group, uint16_t element);
95 virtual bool SetEntry(std::string const &content, ValEntry *entry);
96 virtual bool SetEntry(uint8_t *content, int lgth, BinEntry *entry);
98 virtual void *GetEntryBinArea(uint16_t group, uint16_t elem);
100 virtual std::string GetEntry (uint16_t group, uint16_t elem);
101 virtual std::string GetEntryVR(uint16_t group, uint16_t elem);
102 virtual int GetEntryLength(uint16_t group, uint16_t elem);
104 DocEntry *GetDocEntry(uint16_t group, uint16_t element);
105 ValEntry *GetValEntry(uint16_t group, uint16_t element);
106 BinEntry *GetBinEntry(uint16_t group, uint16_t element);
108 ValEntry *ReplaceOrCreate(std::string const &value,
109 uint16_t group, uint16_t elem,
110 TagName const &vr = GDCM_UNKNOWN);
111 BinEntry *ReplaceOrCreate(uint8_t* binArea, int lgth,
112 uint16_t group, uint16_t elem,
113 TagName const &vr = GDCM_UNKNOWN);
114 SeqEntry *ReplaceOrCreate(uint16_t group, uint16_t elem);
116 bool ReplaceIfExist(std::string const &value,
117 uint16_t group, uint16_t elem );
119 virtual void LoadEntryBinArea(uint16_t group, uint16_t elem);
120 virtual void LoadEntryBinArea(BinEntry *entry);
122 void LoadDocEntrySafe(DocEntry *entry);
123 TagDocEntryHT *BuildFlatHashTable();
125 /// Return the Transfer Syntax as a string
126 std::string GetTransferSyntaxName();
130 // Constructor and destructor are protected to forbid end user
131 // to instanciate from this class Document (only Header and
132 // DicomDir are meaningfull).
134 Document( std::string const &filename );
137 void ReadAndSkipEncapsulatedBasicOffsetTable();
138 void ComputeRLEInfo();
139 void ComputeJPEGFragmentInfo();
141 bool CheckIfEntryExist(uint16_t group, uint16_t elem );
143 int ComputeGroup0002Length( FileType filetype );
146 /// Refering underlying filename.
147 std::string Filename;
149 /// \brief SWap code (e.g. Big Endian, Little Endian, Bad Big Endian,
150 /// Bad Little Endian) according to the processor Endianity and
151 /// what is written on disc.
154 ///\brief whether we already parsed group 0002
155 bool Group0002Parsed;
157 ///\brief whether file has a DCM Preamble
160 /// File Pointer, opened during Header parsing.
163 /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
166 /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
167 static const unsigned int HEADER_LENGTH_TO_READ;
169 /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
171 static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
173 /// \brief Elements whose value is longer than MAX_SIZE_PRINT_ELEMENT_VALUE
175 /// \todo Currently not used since collides with #define in
176 /// class DocEntry . See also
177 /// method ref Document::SetMaxSizePrintEntry()
178 static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
180 /// Store the RLE frames info obtained during parsing of pixels.
181 RLEFramesInfo *RLEInfo;
183 /// Store the JPEG fragments info obtained during parsing of pixels.
184 JPEGFragmentsInfo *JPEGInfo;
189 void ParseDES(DocEntrySet *set,long offset, long l_max, bool delim_mode);
190 void ParseSQ (SeqEntry *seq, long offset, long l_max, bool delim_mode);
192 void LoadDocEntry (DocEntry *e);
193 void FindDocEntryLength (DocEntry *e) throw ( FormatError );
194 uint32_t FindDocEntryLengthOBOrOW() throw( FormatUnexpected );
195 std::string FindDocEntryVR();
196 bool CheckDocEntryVR (VRKey k);
198 std::string GetDocEntryValue (DocEntry *entry);
199 std::string GetDocEntryUnvalue(DocEntry *entry);
202 void SkipDocEntry (DocEntry *entry);
203 void SkipToNextDocEntry (DocEntry *entry);
205 void FixDocEntryFoundLength(DocEntry *entry,uint32_t l);
206 bool IsDocEntryAnInteger (DocEntry *entry);
208 uint16_t ReadInt16() throw ( FormatError );
209 uint32_t ReadInt32() throw ( FormatError );
210 void SkipBytes(uint32_t);
211 bool ReadTag(uint16_t, uint16_t);
212 uint32_t ReadTagLength(uint16_t, uint16_t);
216 void SwitchByteSwapCode();
217 void SetMaxSizeLoadEntry(long);
218 void SetMaxSizePrintEntry(long);
220 // DocEntry related utilities
221 DocEntry *ReadNextDocEntry();
223 uint32_t GenerateFreeTagKeyInGroup(uint16_t group);
224 void BuildFlatHashTableRecurse( TagDocEntryHT &builtHT,
227 void HandleBrokenEndian(uint16_t &group, uint16_t &elem);
228 void HandleOutOfGroup0002(uint16_t group);
231 /// Public dictionary used to parse this header
234 /// \brief Optional "shadow dictionary" (private elements) used to parse
238 /// \brief Size threshold above which an element value will NOT be loaded
239 /// in memory (to avoid loading the image/volume itself). By default,
240 /// this upper bound is fixed to 1024 bytes (which might look reasonable
241 /// when one considers the definition of the various VR contents).
242 uint32_t MaxSizeLoadEntry;
244 /// \brief Size threshold above which an element value will NOT be *printed*
245 /// in order no to polute the screen output. By default, this upper bound
246 /// is fixed to 64 bytes.
247 uint32_t MaxSizePrintEntry;
252 } // end namespace gdcm
254 //-----------------------------------------------------------------------------