1 /*=========================================================================
4 Module: $RCSfile: gdcmDocument.h,v $
6 Date: $Date: 2005/01/06 20:03:27 $
7 Version: $Revision: 1.75 $
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 enum TransferSyntaxType {
40 ImplicitVRLittleEndian = 0,
41 ImplicitVRLittleEndianDLXGE,
42 ExplicitVRLittleEndian,
43 DeflatedExplicitVRLittleEndian,
46 JPEGExtendedProcess2_4,
47 JPEGExtendedProcess3_5,
48 JPEGSpectralSelectionProcess6_8,
49 JPEGFullProgressionProcess10_12,
50 JPEGLosslessProcess14,
51 JPEGLosslessProcess14_1,
58 //-----------------------------------------------------------------------------
60 * \brief Derived by both Header and DicomDir
62 class GDCM_EXPORT Document : public ElementSet
65 // Informations contained in the parser
66 virtual bool IsReadable();
67 FileType GetFileType();
69 TransferSyntaxType GetTransferSyntax();
71 bool IsJPEGLossless();
77 RLEFramesInfo *GetRLEInfo() { return RLEInfo; }
78 JPEGFragmentsInfo *GetJPEGInfo() { return JPEGInfo; }
81 virtual void PrintPubDict (std::ostream &os = std::cout);
82 virtual void PrintShaDict (std::ostream &os = std::cout);
86 bool SetShaDict(Dict* dict);
87 bool SetShaDict(DictKey const &dictName);
90 /// 'Swap code' accessor (see \ref SwapCode )
91 int GetSwapCode() { return SwapCode; }
92 // System access (meaning endian related !?)
93 uint16_t SwapShort(uint16_t); // needed by File
94 uint32_t SwapLong(uint32_t); // needed by File
95 uint16_t UnswapShort(uint16_t); // needed by File
96 uint32_t UnswapLong(uint32_t); // needed by File
98 // Ordering of Documents
99 bool operator<(Document &document);
103 /// Accessor to \ref Filename
104 const std::string &GetFileName() const { return Filename; }
105 /// Accessor to \ref Filename
106 void SetFileName(std::string const &fileName) { Filename = fileName; }
108 std::ifstream *OpenFile();
110 void WriteContent( std::ofstream *fp, FileType type );
113 virtual bool SetEntryByName (std::string const &content,
114 TagName const &tagName );
115 virtual bool SetEntryByNumber(std::string const &content,
116 uint16_t group, uint16_t element);
117 virtual bool SetEntryByNumber(uint8_t *content, int lgth,
118 uint16_t group, uint16_t element);
119 virtual bool SetEntry(std::string const &content,ValEntry *entry);
120 virtual bool SetEntry(uint8_t *content, int lgth,BinEntry *entry);
122 virtual void *GetEntryBinAreaByNumber(uint16_t group, uint16_t elem);
124 virtual std::string GetEntryByName (TagName const &tagName);
125 virtual std::string GetEntryVRByName (TagName const &tagName);
126 virtual std::string GetEntryByNumber (uint16_t group, uint16_t elem);
127 virtual std::string GetEntryVRByNumber(uint16_t group, uint16_t elem);
128 virtual int GetEntryLengthByNumber(uint16_t group, uint16_t elem);
130 DocEntry *GetDocEntryByNumber(uint16_t group, uint16_t element);
131 DocEntry *GetDocEntryByName (TagName const &tagName);
132 ValEntry *GetValEntryByNumber(uint16_t group, uint16_t element);
133 BinEntry *GetBinEntryByNumber(uint16_t group, uint16_t element);
135 ValEntry *ReplaceOrCreateByNumber(std::string const &value,
136 uint16_t group, uint16_t elem,
137 TagName const &vr = GDCM_UNKNOWN);
138 BinEntry *ReplaceOrCreateByNumber(uint8_t* binArea, int lgth,
139 uint16_t group, uint16_t elem,
140 TagName const &vr = GDCM_UNKNOWN);
141 SeqEntry *ReplaceOrCreateByNumber(uint16_t group, uint16_t elem);
143 bool ReplaceIfExistByNumber ( std::string const &value,
144 uint16_t group, uint16_t elem );
146 virtual void LoadEntryBinArea(uint16_t group, uint16_t elem);
147 virtual void LoadEntryBinArea(BinEntry *entry);
149 void LoadDocEntrySafe(DocEntry *entry);
150 TagDocEntryHT *BuildFlatHashTable();
153 static std::string GetTransferSyntaxValue(TransferSyntaxType type);
157 // Constructor and destructor are protected to forbid end user
158 // to instanciate from this class Document (only Header and
159 // DicomDir are meaningfull).
161 Document( std::string const &filename );
164 void ReadAndSkipEncapsulatedBasicOffsetTable();
165 void ComputeRLEInfo();
166 void ComputeJPEGFragmentInfo();
168 bool CheckIfEntryExistByNumber(uint16_t group, uint16_t elem );
170 int ComputeGroup0002Length( FileType filetype );
173 /// Refering underlying filename.
174 std::string Filename;
176 /// \brief SWap code (e.g. Big Endian, Little Endian, Bad Big Endian,
177 /// Bad Little Endian) according to the processor Endianity and
178 /// what is written on disc.
181 /// File Pointer, opened during Header parsing.
184 /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
187 /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
188 static const unsigned int HEADER_LENGTH_TO_READ;
190 /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
192 static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
194 /// \brief Elements whose value is longer than MAX_SIZE_PRINT_ELEMENT_VALUE
196 /// \todo Currently not used since collides with #define in
197 /// class DocEntry . See also
198 /// method ref Document::SetMaxSizePrintEntry()
199 static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
201 /// Store the RLE frames info obtained during parsing of pixels.
202 RLEFramesInfo *RLEInfo;
204 /// Store the JPEG fragments info obtained during parsing of pixels.
205 JPEGFragmentsInfo *JPEGInfo;
210 void ParseDES(DocEntrySet *set,long offset, long l_max, bool delim_mode);
211 void ParseSQ (SeqEntry *seq, long offset, long l_max, bool delim_mode);
213 void LoadDocEntry (DocEntry *e);
214 void FindDocEntryLength (DocEntry *e) throw ( FormatError );
215 std::string FindDocEntryVR();
216 bool CheckDocEntryVR (VRKey k);
218 std::string GetDocEntryValue (DocEntry *e);
219 std::string GetDocEntryUnvalue(DocEntry *e);
222 void SkipDocEntry (DocEntry *e);
223 void SkipToNextDocEntry (long offset,long readLgth);
225 void FixDocEntryFoundLength(DocEntry *e, uint32_t l);
226 bool IsDocEntryAnInteger (DocEntry *e);
228 uint32_t FindDocEntryLengthOB() throw( FormatUnexpected );
230 uint16_t ReadInt16() throw ( FormatError );
231 uint32_t ReadInt32() throw ( FormatError );
232 void SkipBytes(uint32_t);
233 bool ReadTag(uint16_t, uint16_t);
234 uint32_t ReadTagLength(uint16_t, uint16_t);
238 void SwitchSwapToBigEndian();
239 void SetMaxSizeLoadEntry(long);
240 void SetMaxSizePrintEntry(long);
242 // DocEntry related utilities
243 DocEntry *ReadNextDocEntry();
245 uint32_t GenerateFreeTagKeyInGroup(uint16_t group);
246 void BuildFlatHashTableRecurse( TagDocEntryHT &builtHT,
249 void HandleBrokenEndian(uint16_t group, uint16_t elem);
252 /// Public dictionary used to parse this header
255 /// \brief Optional "shadow dictionary" (private elements) used to parse
259 /// \brief Size threshold above which an element value will NOT be loaded
260 /// in memory (to avoid loading the image/volume itself). By default,
261 /// this upper bound is fixed to 1024 bytes (which might look reasonable
262 /// when one considers the definition of the various VR contents).
263 uint32_t MaxSizeLoadEntry;
265 /// \brief Size threshold above which an element value will NOT be *printed*
266 /// in order no to polute the screen output. By default, this upper bound
267 /// is fixed to 64 bytes.
268 uint32_t MaxSizePrintEntry;
273 } // end namespace gdcm
275 //-----------------------------------------------------------------------------