1 /*=========================================================================
4 Module: $RCSfile: gdcmDocument.h,v $
6 Date: $Date: 2005/01/21 11:40:55 $
7 Version: $Revision: 1.96 $
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 gdcm::File and gdcm::DicomDir
43 class GDCM_EXPORT Document : public ElementSet
46 // Informations contained in the gdcm::Document
47 virtual bool IsReadable();
48 FileType GetFileType();
50 std::string GetTransferSyntax();
52 RLEFramesInfo *GetRLEInfo() { return RLEInfo; }
53 JPEGFragmentsInfo *GetJPEGInfo() { return JPEGInfo; }
56 virtual void PrintPubDict (std::ostream &os = std::cout);
57 virtual void PrintShaDict (std::ostream &os = std::cout);
61 bool SetShaDict(Dict* dict);
62 bool SetShaDict(DictKey const &dictName);
65 /// 'Swap code' accessor (see \ref SwapCode )
66 int GetSwapCode() { return SwapCode; }
67 // System access (meaning endian related !?)
68 uint16_t SwapShort(uint16_t); // needed by Document
69 uint32_t SwapLong(uint32_t); // needed by Document
70 uint16_t UnswapShort(uint16_t); // needed by Document
71 uint32_t UnswapLong(uint32_t); // needed by Document
73 // Ordering of Documents
74 bool operator<(Document &document);
78 /// Accessor to \ref Filename
79 const std::string &GetFileName() const { return Filename; }
80 /// Accessor to \ref Filename
81 void SetFileName(std::string const &fileName) { Filename = fileName; }
83 std::ifstream *OpenFile();
85 void WriteContent( std::ofstream *fp, FileType type );
89 // Oops ! Python is gonna cry :
90 // 4 methods with same name and different parameters ...
93 virtual bool SetEntry(std::string const &content,
94 uint16_t group, uint16_t elem);
95 virtual bool SetEntry(uint8_t *content, int lgth,
96 uint16_t group, uint16_t elem);
97 virtual bool SetEntry(std::string const &content, ValEntry *entry);
98 virtual bool SetEntry(uint8_t *content, int lgth, BinEntry *entry);
100 virtual void *GetEntryBinArea(uint16_t group, uint16_t elem);
102 virtual std::string GetEntry (uint16_t group, uint16_t elem);
103 virtual std::string GetEntryVR(uint16_t group, uint16_t elem);
104 virtual int GetEntryLength(uint16_t group, uint16_t elem);
106 ValEntry *ReplaceOrCreate(std::string const &value,
107 uint16_t group, uint16_t elem,
108 TagName const &vr = GDCM_UNKNOWN);
109 BinEntry *ReplaceOrCreate(uint8_t* binArea, int lgth,
110 uint16_t group, uint16_t elem,
111 TagName const &vr = GDCM_UNKNOWN);
112 SeqEntry *ReplaceOrCreate(uint16_t group, uint16_t elem);
114 bool ReplaceIfExist(std::string const &value,
115 uint16_t group, uint16_t elem );
117 bool CheckIfEntryExist(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();
132 // Constructor and destructor are protected to forbid end user
133 // to instanciate from this class Document (only gdcm::File and
134 // gdcm::DicomDir are meaningfull).
136 Document( std::string const &filename );
139 void ReadAndSkipEncapsulatedBasicOffsetTable();
140 void ComputeRLEInfo();
141 void ComputeJPEGFragmentInfo();
144 int ComputeGroup0002Length( FileType filetype );
147 /// Refering underlying filename.
148 std::string Filename;
150 /// \brief Swap code gives an information on the byte order of a
151 /// supposed to be an int32, as it's read on disc
152 /// (depending on the image Transfer Syntax *and* on the processor endianess)
153 /// as opposed as it should in memory to be dealt as an int32.
155 /// - a 'Little Endian' image, read with a little endian processor
156 /// will have a SwapCode= 1234 (the order is OK; nothing to do)
157 /// - a 'Little Endian' image, read with a big endian procesor
158 /// will have a SwapCode= 4321 (the order is wrong; int32 an int16 must be
160 /// note : values 2143, 4321, 3412 remain for the ACR-NEMA time, and
161 /// the well known 'Bad Big Endian' and 'Bad Little Endian' codes
164 ///\brief whether we already parsed group 0002 (Meta Elements)
165 bool Group0002Parsed;
167 ///\brief whether file has a DCM Preamble
170 /// File Pointer, opened during Document parsing.
173 /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
176 /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
177 static const unsigned int HEADER_LENGTH_TO_READ;
179 /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
181 static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
183 /// \brief Elements whose value is longer than MAX_SIZE_PRINT_ELEMENT_VALUE
185 static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
187 /// Store the RLE frames info obtained during parsing of pixels.
188 RLEFramesInfo *RLEInfo;
190 /// Store the JPEG fragments info obtained during parsing of pixels.
191 JPEGFragmentsInfo *JPEGInfo;
196 void ParseDES(DocEntrySet *set,long offset, long l_max, bool delim_mode);
197 void ParseSQ (SeqEntry *seq, long offset, long l_max, bool delim_mode);
199 void LoadDocEntry (DocEntry *e);
200 void FindDocEntryLength (DocEntry *e) throw ( FormatError );
201 uint32_t FindDocEntryLengthOBOrOW() throw( FormatUnexpected );
202 std::string FindDocEntryVR();
203 bool CheckDocEntryVR (VRKey k);
205 std::string GetDocEntryValue (DocEntry *entry);
206 std::string GetDocEntryUnvalue(DocEntry *entry);
209 void SkipDocEntry (DocEntry *entry);
210 void SkipToNextDocEntry (DocEntry *entry);
212 void FixDocEntryFoundLength(DocEntry *entry,uint32_t l);
213 bool IsDocEntryAnInteger (DocEntry *entry);
215 uint16_t ReadInt16() throw ( FormatError );
216 uint32_t ReadInt32() throw ( FormatError );
217 void SkipBytes(uint32_t);
218 bool ReadTag(uint16_t, uint16_t);
219 uint32_t ReadTagLength(uint16_t, uint16_t);
223 void SwitchByteSwapCode();
224 void SetMaxSizeLoadEntry(long);
225 void SetMaxSizePrintEntry(long);
227 // DocEntry related utilities
228 DocEntry *ReadNextDocEntry();
230 uint32_t GenerateFreeTagKeyInGroup(uint16_t group);
231 /* void BuildFlatHashTableRecurse( TagDocEntryHT &builtHT,
232 DocEntrySet* set );*/
234 void HandleBrokenEndian(uint16_t &group, uint16_t &elem);
235 void HandleOutOfGroup0002(uint16_t &group, uint16_t &elem);
238 /// Public dictionary used to parse this header
241 /// \brief Optional "shadow dictionary" (private elements) used to parse
245 /// \brief Size threshold above which an element value will NOT be loaded
246 /// in memory (to avoid loading the image/volume itself). By default,
247 /// this upper bound is fixed to 1024 bytes (which might look reasonable
248 /// when one considers the definition of the various VR contents).
249 uint32_t MaxSizeLoadEntry;
251 /// \brief Size threshold above which an element value will NOT be *printed*
252 /// in order no to polute the screen output. By default, this upper bound
253 /// is fixed to 64 bytes.
254 uint32_t MaxSizePrintEntry;
257 friend class FileHelper;
259 } // end namespace gdcm
261 //-----------------------------------------------------------------------------