1 /*=========================================================================
4 Module: $RCSfile: gdcmDocument.h,v $
6 Date: $Date: 2005/08/24 12:09:13 $
7 Version: $Revision: 1.119 $
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"
31 #define NO_SEQ 0x00000001 // Don't load odd groups
32 #define NO_SHADOW 0x00000002 // Don't load Sequences
33 #define NO_SHADOWSEQ 0x00000004 // Don't load Sequences if they belong
35 // (*exclusive* from NO_SEQ and NO_SHADOW)
43 //-----------------------------------------------------------------------------
45 * \brief Derived by both gdcm::File and gdcm::DicomDir
47 class GDCM_EXPORT Document : public ElementSet
51 typedef std::list<Element> ListElements;
54 //Deprecated : use SetFileName() + Load()
55 virtual bool Load( std::string const &filename );
61 bool SetShaDict(Dict *dict);
62 bool SetShaDict(DictKey const &dictName);
64 // Informations contained in the gdcm::Document
65 virtual bool IsReadable();
68 FileType GetFileType();
69 std::string GetTransferSyntax();
70 /// Return the Transfer Syntax as a string
71 std::string GetTransferSyntaxName();
74 /// 'Swap code' accessor (see \ref SwapCode )
75 int GetSwapCode() { return SwapCode; }
76 // System access (meaning endian related !?)
77 uint16_t SwapShort(uint16_t);
78 uint32_t SwapLong(uint32_t);
79 /// \brief Unswaps back the bytes of 2-bytes long integer
80 /// so they agree with the processor order.
81 uint16_t UnswapShort(uint16_t a) { return SwapShort(a);}
82 /// \brief Unswaps back the bytes of 4-byte long integer
83 /// so they agree with the processor order.
84 uint32_t UnswapLong(uint32_t a) { return SwapLong(a);}
87 /// Accessor to \ref Filename
88 const std::string &GetFileName() const { return Filename; }
89 /// Accessor to \ref Filename
90 virtual void SetFileName(std::string const &fileName)
91 { if (Filename != fileName)
92 Filename = fileName, IsDocumentModified = true; }
94 std::ifstream *OpenFile();
96 void WriteContent( std::ofstream *fp, FileType type );
99 virtual void LoadEntryBinArea(uint16_t group, uint16_t elem);
100 virtual void LoadEntryBinArea(BinEntry *entry);
102 void LoadDocEntrySafe(DocEntry *entry);
104 // Ordering of Documents
105 bool operator<(Document &document);
108 * \brief Sets the LoadMode as a boolean string.
109 * NO_SEQ, NO_SHADOW, NO_SHADOWSEQ
110 ... (nothing more, right now)
111 * WARNING : before using NO_SHADOW, be sure *all* your files
112 * contain accurate values in the 0x0000 element (if any)
113 * of *each* Shadow Group. The parser will fail if the size is wrong !
114 * @param mode Load mode to be used
116 void SetLoadMode (int mode) { if (LoadMode != mode)
117 LoadMode=mode, IsDocumentModified = true; }
121 // Constructor and destructor are protected to forbid end user
122 // to instanciate from this class Document (only gdcm::File and
123 // gdcm::DicomDir are meaningfull).
125 Document( std::string const &filename );
128 uint16_t ReadInt16() throw ( FormatError );
129 uint32_t ReadInt32() throw ( FormatError );
130 void SkipBytes(uint32_t);
131 int ComputeGroup0002Length( FileType filetype );
134 /// Refering underlying filename.
135 std::string Filename;
137 /// \brief Swap code gives an information on the byte order of a
138 /// supposed to be an int32, as it's read on disc
139 /// (depending on the image Transfer Syntax *and* on the processor endianess)
140 /// as opposed as it should in memory to be dealt as an int32.
142 /// - a 'Little Endian' image, read with a little endian processor
143 /// will have a SwapCode= 1234 (the order is OK; nothing to do)
144 /// - a 'Little Endian' image, read with a big endian procesor
145 /// will have a SwapCode= 4321 (the order is wrong; int32 an int16 must be
147 /// note : values 2143, 4321, 3412 remain for the ACR-NEMA time, and
148 /// the well known 'Bad Big Endian' and 'Bad Little Endian' codes
151 ///\brief whether we already parsed group 0002 (Meta Elements)
152 bool Group0002Parsed;
154 ///\brief whether file has a DCM Preamble
157 /// File Pointer, opened during Document parsing.
160 /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
163 /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
164 static const unsigned int HEADER_LENGTH_TO_READ;
165 /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
167 static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
169 /// List of elements to Anonymize
170 ListElements AnonymizeList;
172 /// \brief Bit string integer (each one considered as a boolean)
173 /// Bit 0 : Skip Sequences, if possible
174 /// Bit 1 : Skip Shadow Groups if possible
175 /// Probabely, some more to add
178 /// \brief Whether the gdcm::Document is already parsed/loaded :
179 /// False from the creation of the gdcm::Document untill
180 /// gdcm::Document:Load()
181 bool IsDocumentAlreadyLoaded; // FIXME : probabely useless now
183 /// Whether the gdcm::Document was modified since the last Load()
184 bool IsDocumentModified;
189 bool DoTheLoadingDocumentJob();
191 void ParseDES(DocEntrySet *set, long offset, long l_max, bool delim_mode);
192 void ParseSQ (SeqEntry *seq, long offset, long l_max, bool delim_mode);
194 void LoadDocEntry (DocEntry *e);
195 void FindDocEntryLength (DocEntry *e) throw ( FormatError );
196 uint32_t FindDocEntryLengthOBOrOW() throw( FormatUnexpected );
197 std::string FindDocEntryVR();
198 bool CheckDocEntryVR (VRKey k);
200 std::string GetDocEntryValue (DocEntry *entry);
201 std::string GetDocEntryUnvalue(DocEntry *entry);
203 void SkipDocEntry (DocEntry *entry);
204 void SkipToNextDocEntry (DocEntry *entry);
206 void FixDocEntryFoundLength(DocEntry *entry, uint32_t l);
207 bool IsDocEntryAnInteger (DocEntry *entry);
210 void SwitchByteSwapCode();
211 void SetMaxSizeLoadEntry(long);
213 // DocEntry related utilities
214 DocEntry *ReadNextDocEntry();
216 void HandleBrokenEndian (uint16_t &group, uint16_t &elem);
217 void HandleOutOfGroup0002(uint16_t &group, uint16_t &elem);
218 DocEntry *Backtrack(DocEntry *docEntry);
221 /// Public dictionary used to parse this header
223 /// \brief Optional "shadow dictionary" (private elements) used to parse
227 /// \brief Size threshold above which an element value will NOT be loaded
228 /// in memory (to avoid loading the image/volume itself). By default,
229 /// this upper bound is fixed to 1024 bytes (which might look reasonable
230 /// when one considers the definition of the various VR contents).
231 uint32_t MaxSizeLoadEntry;
233 // uint32_t GenerateFreeTagKeyInGroup(uint16_t group);
234 // void BuildFlatHashTableRecurse( TagDocEntryHT &builtHT,
235 // DocEntrySet *set );
239 } // end namespace gdcm
241 //-----------------------------------------------------------------------------