1 /*=========================================================================
4 Module: $RCSfile: gdcmDocument.h,v $
6 Date: $Date: 2005/02/11 16:36:52 $
7 Version: $Revision: 1.106 $
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"
38 //-----------------------------------------------------------------------------
40 * \brief Derived by both gdcm::File and gdcm::DicomDir
42 class GDCM_EXPORT Document : public ElementSet
46 typedef std::list<Element> ListElements;
51 bool SetShaDict(Dict *dict);
52 bool SetShaDict(DictKey const &dictName);
54 // Informations contained in the gdcm::Document
55 virtual bool IsReadable();
58 FileType GetFileType();
59 std::string GetTransferSyntax();
60 /// Return the Transfer Syntax as a string
61 std::string GetTransferSyntaxName();
64 /// 'Swap code' accessor (see \ref SwapCode )
65 int GetSwapCode() { return SwapCode; }
66 // System access (meaning endian related !?)
67 uint16_t SwapShort(uint16_t);
68 uint32_t SwapLong(uint32_t);
69 /// \brief Unswaps back the bytes of 2-bytes long integer
70 /// so they agree with the processor order.
71 uint16_t UnswapShort(uint16_t a) { return SwapShort(a);}
72 /// \brief Unswaps back the bytes of 4-byte long integer
73 /// so they agree with the processor order.
74 uint32_t UnswapLong(uint32_t a) { return SwapLong(a);}
77 /// Accessor to \ref Filename
78 const std::string &GetFileName() const { return Filename; }
79 /// Accessor to \ref Filename
80 void SetFileName(std::string const &fileName) { Filename = fileName; }
82 std::ifstream *OpenFile();
84 void WriteContent( std::ofstream *fp, FileType type );
87 virtual void LoadEntryBinArea(uint16_t group, uint16_t elem);
88 virtual void LoadEntryBinArea(BinEntry *entry);
90 void LoadDocEntrySafe(DocEntry *entry);
92 // Ordering of Documents
93 bool operator<(Document &document);
97 // Constructor and destructor are protected to forbid end user
98 // to instanciate from this class Document (only gdcm::File and
99 // gdcm::DicomDir are meaningfull).
101 Document( std::string const &filename );
104 uint16_t ReadInt16() throw ( FormatError );
105 uint32_t ReadInt32() throw ( FormatError );
106 void SkipBytes(uint32_t);
107 int ComputeGroup0002Length( FileType filetype );
110 /// Refering underlying filename.
111 std::string Filename;
113 /// \brief Swap code gives an information on the byte order of a
114 /// supposed to be an int32, as it's read on disc
115 /// (depending on the image Transfer Syntax *and* on the processor endianess)
116 /// as opposed as it should in memory to be dealt as an int32.
118 /// - a 'Little Endian' image, read with a little endian processor
119 /// will have a SwapCode= 1234 (the order is OK; nothing to do)
120 /// - a 'Little Endian' image, read with a big endian procesor
121 /// will have a SwapCode= 4321 (the order is wrong; int32 an int16 must be
123 /// note : values 2143, 4321, 3412 remain for the ACR-NEMA time, and
124 /// the well known 'Bad Big Endian' and 'Bad Little Endian' codes
127 ///\brief whether we already parsed group 0002 (Meta Elements)
128 bool Group0002Parsed;
130 ///\brief whether file has a DCM Preamble
133 /// File Pointer, opened during Document parsing.
136 /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
139 /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
140 static const unsigned int HEADER_LENGTH_TO_READ;
141 /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
143 static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
144 /// \brief Elements whose value is longer than MAX_SIZE_PRINT_ELEMENT_VALUE
146 static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
148 /// List of element to Anonymize
149 ListElements AnonymizeList;
156 void ParseDES(DocEntrySet *set,long offset, long l_max, bool delim_mode);
157 void ParseSQ (SeqEntry *seq, long offset, long l_max, bool delim_mode);
159 void LoadDocEntry (DocEntry *e);
160 void FindDocEntryLength (DocEntry *e) throw ( FormatError );
161 uint32_t FindDocEntryLengthOBOrOW() throw( FormatUnexpected );
162 std::string FindDocEntryVR();
163 bool CheckDocEntryVR (VRKey k);
165 std::string GetDocEntryValue (DocEntry *entry);
166 std::string GetDocEntryUnvalue(DocEntry *entry);
168 void SkipDocEntry (DocEntry *entry);
169 void SkipToNextDocEntry (DocEntry *entry);
171 void FixDocEntryFoundLength(DocEntry *entry,uint32_t l);
172 bool IsDocEntryAnInteger (DocEntry *entry);
175 void SwitchByteSwapCode();
176 void SetMaxSizeLoadEntry(long);
177 void SetMaxSizePrintEntry(long);
179 // DocEntry related utilities
180 DocEntry *ReadNextDocEntry();
182 void HandleBrokenEndian (uint16_t &group, uint16_t &elem);
183 void HandleOutOfGroup0002(uint16_t &group, uint16_t &elem);
186 /// Public dictionary used to parse this header
188 /// \brief Optional "shadow dictionary" (private elements) used to parse
192 /// \brief Size threshold above which an element value will NOT be loaded
193 /// in memory (to avoid loading the image/volume itself). By default,
194 /// this upper bound is fixed to 1024 bytes (which might look reasonable
195 /// when one considers the definition of the various VR contents).
196 uint32_t MaxSizeLoadEntry;
198 /// \brief Size threshold above which an element value will NOT be *printed*
199 /// in order no to polute the screen output. By default, this upper bound
200 /// is fixed to 64 bytes.
201 uint32_t MaxSizePrintEntry;
204 // uint32_t GenerateFreeTagKeyInGroup(uint16_t group);
205 // void BuildFlatHashTableRecurse( TagDocEntryHT &builtHT,
206 // DocEntrySet *set );
210 } // end namespace gdcm
212 //-----------------------------------------------------------------------------