1 /*=========================================================================
4 Module: $RCSfile: gdcmDocument.h,v $
6 Date: $Date: 2011/04/27 13:40:04 $
7 Version: $Revision: 1.155 $
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"
25 #include "gdcmException.h"
26 #include "gdcmDebug.h" // for LEGACY
27 #include "gdcmCommandManager.h"
33 namespace GDCM_NAME_SPACE
38 //-----------------------------------------------------------------------------
40 * \brief Derived by both GDCM_NAME_SPACE::File and GDCM_NAME_SPACE::DicomDir
42 class GDCM_EXPORT Document : public ElementSet
44 gdcmTypeMacro(Document);
47 typedef std::list<DicomElement> ListElements;
50 //Deprecated : use SetFileName() + Load()
51 //#ifndef GDCM_LEGACY_REMOVE
52 // virtual bool Load( std::string const &filename );
59 bool SetShaDict(Dict *dict);
60 bool SetShaDict(DictKey const &dictName);
62 // Informations contained in the GDCM_NAME_SPACE::Document
64 virtual bool IsReadable();
67 FileType GetFileType();
68 std::string GetTransferSyntax();
69 /// Return the Transfer Syntax as a string
70 std::string GetTransferSyntaxName();
73 /// 'Swap code' accessor (see SwapCode )
74 int GetSwapCode() { return SwapCode; }
77 /// Accessor to Filename
78 const std::string &GetFileName() const { return Filename; }
79 /// Accessor to Filename
80 virtual void SetFileName(std::string const &fileName)
81 { if (Filename != fileName)
82 Filename = fileName, IsDocumentModified = true; }
84 std::ifstream *OpenFile();
86 void WriteContent( std::ofstream *fp, FileType type, bool insideMetaElements, bool insideSequence );
89 virtual void LoadEntryBinArea(uint16_t group, uint16_t elem);
90 virtual void LoadEntryBinArea(DataEntry *entry);
92 void SetMaxSizeLoadEntry(long);
93 void AddForceLoadElement(uint16_t group, uint16_t elem);
95 // Ordering of Documents
96 bool operator<(Document &document);
99 * \brief Sets the LoadMode as a boolean string.
100 * LD_NOSEQ, LD_NOSHADOW, LD_NOSHADOWSEQ
101 ... (nothing more, right now)
102 * WARNING : before using NO_SHADOW, be sure *all* your files
103 * contain accurate values in the 0x0000 element (if any)
104 * of *each* Shadow Group. The parser will fail if the size is wrong !
105 * @param mode Load mode to be used
107 void SetLoadMode (int mode) {
108 //if (LoadMode != mode)
109 // LoadMode=mode, IsDocumentModified = true;
111 // fix definitively any misuse of LoadMode !
117 // Constructor and destructor are protected to forbid end user
118 // to instanciate from this class Document (only GDCM_NAME_SPACE::File and
119 // GDCM_NAME_SPACE::DicomDir are meaningfull).
123 virtual void CallStartMethod();
124 virtual void CallProgressMethod();
125 virtual void CallEndMethod();
127 uint16_t ReadInt16() throw ( FormatError );
128 uint32_t ReadInt32() throw ( FormatError );
130 /// \brief skips bytes inside the source file
131 void SkipBytes(uint32_t nBytes) { Fp->seekg((long)nBytes, std::ios::cur);}
132 int ComputeGroup0002Length( );
135 /// Refering underlying filename.
136 std::string Filename;
138 /// \brief Swap code gives an information on the byte order of a
139 /// supposed to be an int32, as it's read on disc
140 /// (depending on the image Transfer Syntax *and* on the processor endianess)
141 /// as opposed as it should in memory to be dealt as an int32.
143 /// - a 'Little Endian' image, read with a little endian processor
144 /// will have a SwapCode= 1234 (the order is OK; nothing to do)
145 /// - a 'Little Endian' image, read with a big endian procesor
146 /// will have a SwapCode= 4321 (the order is wrong; int32 an int16 must be
148 /// note : values 2143, 4321, 3412 remain for the ACR-NEMA time, and
149 /// the well known 'Bad Big Endian' and 'Bad Little Endian' codes
152 ///\brief whether we already parsed group 0002 (Meta Elements)
153 bool Group0002Parsed;
155 ///\brief whether file has a DCM Preamble
158 /// File Pointer, opened during Document parsing.
161 /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
164 /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
165 static const unsigned int HEADER_LENGTH_TO_READ;
166 /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
168 static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
170 /// User supplied list of elements to Anonymize
171 ListElements UserAnonymizeList;
173 /// User supplied list of elements to force Load
174 ListElements UserForceLoadList;
176 /// \brief Bit string integer (each one considered as a boolean)
177 /// Bit 0 : Skip Sequences, if possible
178 /// Bit 1 : Skip Shadow Groups if possible
179 /// Probabely, some more to add
182 /// \brief Whether the GDCM_NAME_SPACE::Document is already parsed/loaded :
183 /// False from the creation of the GDCM_NAME_SPACE::Document untill
184 /// GDCM_NAME_SPACE::Document:Load()
185 bool IsDocumentAlreadyLoaded; // FIXME : probabely useless now
187 /// Whether the GDCM_NAME_SPACE::Document was modified since the last Load()
188 bool IsDocumentModified;
193 bool DoTheLoadingDocumentJob();
195 // System access (meaning endian related !?)
196 void ReadBegBuffer(size_t l) throw ( FormatError );
197 uint16_t SwapShort(uint16_t);
198 uint32_t SwapLong(uint32_t);
199 double SwapDouble(double);
200 /// \brief Unswaps back the bytes of 2-bytes long integer
201 /// so they agree with the processor order.
202 uint16_t UnswapShort(uint16_t a) { return SwapShort(a);}
203 /// \brief Unswaps back the bytes of 4-byte long integer
204 /// so they agree with the processor order.
205 uint32_t UnswapLong(uint32_t a) { return SwapLong(a);}
208 void ParseDES(DocEntrySet *set, long offset, long l_max, bool delim_mode);
209 bool ParseSQ (SeqEntry *seq, long offset, long l_max, bool delim_mode);
211 void LoadDocEntry (DocEntry *e, bool forceLoad = false);
212 void FindDocEntryLength (DocEntry *e) throw ( FormatError );
213 uint32_t FindDocEntryLengthOBOrOW() throw( FormatUnexpected );
214 VRKey FindDocEntryVR();
215 bool CheckDocEntryVR (const VRKey &k);
217 void SkipDocEntry (DocEntry *entry);
218 void SkipToNextDocEntry (DocEntry *entry);
220 void FixDocEntryFoundLength(DocEntry *entry, uint32_t l);
221 bool IsDocEntryAnInteger (DocEntry *entry);
224 void SwitchByteSwapCode();
226 // DocEntry related utilities
227 DocEntry *ReadNextDocEntry();
231 void HandleBrokenEndian (uint16_t &group, uint16_t &elem);
232 void HandleOutOfGroup0002(uint16_t &group, uint16_t &elem);
233 DocEntry *Backtrack(DocEntry *docEntry, DocEntrySet *set);
237 /// value of the ??? for any progress bar
241 /// Public dictionary used to parse this header
243 /// \brief Optional "shadow dictionary" (private elements) used to parse
247 /// \brief Size threshold above which an element value will NOT be loaded
248 /// in memory (to avoid loading the image/volume itself). By default,
249 /// this upper bound is fixed to 1024 bytes (which might look reasonable
250 /// when one considers the definition of the various VR contents).
251 uint32_t MaxSizeLoadEntry;
253 /// \brief to allow any inner method to know current tag Group number
254 uint16_t CurrentGroup;
255 /// \brief to allow any inner method to know current tag Element number
256 uint16_t CurrentElem;
258 // uint32_t GenerateFreeTagKeyInGroup(uint16_t group);
259 // void BuildFlatHashTableRecurse( TagDocEntryHT &builtHT,
260 // DocEntrySet *set );
263 /// \brief buffer to avoid some freads
266 /// \brief to avoid time consuming ftellg
267 size_t CurrentOffsetPosition;
268 /// \brief to indicate if last supposed to be UN DataElement is not
269 /// (according to a private Dicom dictionary)
271 /// \brief whether an unexpected EOF was encountered
273 /// \brief to avoid infinite loop when illegal UN stands for OB
274 size_t OffsetOfPreviousParseDES;
277 } // end namespace gdcm
279 //-----------------------------------------------------------------------------