1 /*=========================================================================
4 Module: $RCSfile: gdcmDocument.h,v $
6 Date: $Date: 2005/04/26 16:18:23 $
7 Version: $Revision: 1.109 $
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 0x0000001
32 #define NO_SHADOW 0x00000002
41 //-----------------------------------------------------------------------------
43 * \brief Derived by both gdcm::File and gdcm::DicomDir
45 class GDCM_EXPORT Document : public ElementSet
49 typedef std::list<Element> ListElements;
52 virtual void Load( std::string const &filename );
57 bool SetShaDict(Dict *dict);
58 bool SetShaDict(DictKey const &dictName);
60 // Informations contained in the gdcm::Document
61 virtual bool IsReadable();
64 FileType GetFileType();
65 std::string GetTransferSyntax();
66 /// Return the Transfer Syntax as a string
67 std::string GetTransferSyntaxName();
70 /// 'Swap code' accessor (see \ref SwapCode )
71 int GetSwapCode() { return SwapCode; }
72 // System access (meaning endian related !?)
73 uint16_t SwapShort(uint16_t);
74 uint32_t SwapLong(uint32_t);
75 /// \brief Unswaps back the bytes of 2-bytes long integer
76 /// so they agree with the processor order.
77 uint16_t UnswapShort(uint16_t a) { return SwapShort(a);}
78 /// \brief Unswaps back the bytes of 4-byte long integer
79 /// so they agree with the processor order.
80 uint32_t UnswapLong(uint32_t a) { return SwapLong(a);}
83 /// Accessor to \ref Filename
84 const std::string &GetFileName() const { return Filename; }
85 /// Accessor to \ref Filename
86 void SetFileName(std::string const &fileName) { Filename = fileName; }
88 std::ifstream *OpenFile();
90 void WriteContent( std::ofstream *fp, FileType type );
93 virtual void LoadEntryBinArea(uint16_t group, uint16_t elem);
94 virtual void LoadEntryBinArea(BinEntry *entry);
96 void LoadDocEntrySafe(DocEntry *entry);
98 // Ordering of Documents
99 bool operator<(Document &document);
101 /// \brief Sets the LoadMode as a boolean string
102 /// NO_SEQ, NO_SHADOW, ... (nothing more, right now)
103 void SetLoadMode (int mode) { LoadMode = mode; }
107 // Constructor and destructor are protected to forbid end user
108 // to instanciate from this class Document (only gdcm::File and
109 // gdcm::DicomDir are meaningfull).
111 Document( std::string const &filename );
114 uint16_t ReadInt16() throw ( FormatError );
115 uint32_t ReadInt32() throw ( FormatError );
116 void SkipBytes(uint32_t);
117 int ComputeGroup0002Length( FileType filetype );
120 /// Refering underlying filename.
121 std::string Filename;
123 /// \brief Swap code gives an information on the byte order of a
124 /// supposed to be an int32, as it's read on disc
125 /// (depending on the image Transfer Syntax *and* on the processor endianess)
126 /// as opposed as it should in memory to be dealt as an int32.
128 /// - a 'Little Endian' image, read with a little endian processor
129 /// will have a SwapCode= 1234 (the order is OK; nothing to do)
130 /// - a 'Little Endian' image, read with a big endian procesor
131 /// will have a SwapCode= 4321 (the order is wrong; int32 an int16 must be
133 /// note : values 2143, 4321, 3412 remain for the ACR-NEMA time, and
134 /// the well known 'Bad Big Endian' and 'Bad Little Endian' codes
137 ///\brief whether we already parsed group 0002 (Meta Elements)
138 bool Group0002Parsed;
140 ///\brief whether file has a DCM Preamble
143 /// File Pointer, opened during Document parsing.
146 /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
149 /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
150 static const unsigned int HEADER_LENGTH_TO_READ;
151 /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
153 static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
155 /// List of elements to Anonymize
156 ListElements AnonymizeList;
158 /// \brief Bit string integer (each one considered as a boolean)
159 /// Bit 0 : Skip Sequences, if possible
160 /// Bit 1 : Skip Shadow Groups if possible
164 /// Whether the gdcm::Document is already parsed/loaded
165 /// - False from the creation of the gdcm::Document untill
166 /// gdcm::Document:Load()
167 bool IsDocumentAlreadyLoaded;
174 void ParseDES(DocEntrySet *set, long offset, long l_max, bool delim_mode);
175 void ParseSQ (SeqEntry *seq, long offset, long l_max, bool delim_mode);
177 void LoadDocEntry (DocEntry *e);
178 void FindDocEntryLength (DocEntry *e) throw ( FormatError );
179 uint32_t FindDocEntryLengthOBOrOW() throw( FormatUnexpected );
180 std::string FindDocEntryVR();
181 bool CheckDocEntryVR (VRKey k);
183 std::string GetDocEntryValue (DocEntry *entry);
184 std::string GetDocEntryUnvalue(DocEntry *entry);
186 void SkipDocEntry (DocEntry *entry);
187 void SkipToNextDocEntry (DocEntry *entry);
189 void FixDocEntryFoundLength(DocEntry *entry, uint32_t l);
190 bool IsDocEntryAnInteger (DocEntry *entry);
193 void SwitchByteSwapCode();
194 void SetMaxSizeLoadEntry(long);
196 // DocEntry related utilities
197 DocEntry *ReadNextDocEntry();
199 void HandleBrokenEndian (uint16_t &group, uint16_t &elem);
200 void HandleOutOfGroup0002(uint16_t &group, uint16_t &elem);
203 /// Public dictionary used to parse this header
205 /// \brief Optional "shadow dictionary" (private elements) used to parse
209 /// \brief Size threshold above which an element value will NOT be loaded
210 /// in memory (to avoid loading the image/volume itself). By default,
211 /// this upper bound is fixed to 1024 bytes (which might look reasonable
212 /// when one considers the definition of the various VR contents).
213 uint32_t MaxSizeLoadEntry;
215 // uint32_t GenerateFreeTagKeyInGroup(uint16_t group);
216 // void BuildFlatHashTableRecurse( TagDocEntryHT &builtHT,
217 // DocEntrySet *set );
221 } // end namespace gdcm
223 //-----------------------------------------------------------------------------