]> Creatis software - gdcm.git/blob - src/gdcmDocument.h
* Test/TestUtil.cxx : reformat the source code
[gdcm.git] / src / gdcmDocument.h
1 /*=========================================================================
2  
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocument.h,v $
5   Language:  C++
6   Date:      $Date: 2004/12/07 13:39:33 $
7   Version:   $Revision: 1.68 $
8  
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.
12  
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.
16  
17 =========================================================================*/
18
19 #ifndef GDCMDOCUMENT_H
20 #define GDCMDOCUMENT_H
21
22 #include "gdcmVR.h"
23 #include "gdcmDict.h"
24 #include "gdcmElementSet.h"
25
26 #include <map>
27 #include <list>
28 #include <fstream>
29
30 namespace gdcm 
31 {
32 class ValEntry;
33 class BinEntry;
34 class SeqEntry;
35 class Dict;
36 class RLEFramesInfo;
37 class JPEGFragmentsInfo;
38
39 enum TransferSyntaxType {
40   ImplicitVRLittleEndian = 0,
41   ImplicitVRLittleEndianDLXGE,
42   ExplicitVRLittleEndian,
43   DeflatedExplicitVRLittleEndian,
44   ExplicitVRBigEndian,
45   JPEGBaselineProcess1,
46   JPEGExtendedProcess2_4,
47   JPEGExtendedProcess3_5,
48   JPEGSpectralSelectionProcess6_8,
49   JPEGFullProgressionProcess10_12,
50   JPEGLosslessProcess14,
51   JPEGLosslessProcess14_1,
52   JPEG2000Lossless,
53   JPEG2000,
54   RLELossless,
55   UnknownTS
56 };
57
58 //-----------------------------------------------------------------------------
59 /**
60  * \brief Derived by both Header and DicomDir
61  */
62 class GDCM_EXPORT Document : public ElementSet
63 {
64 friend class File;
65 private:
66    /// Public dictionary used to parse this header
67    Dict* RefPubDict;
68    
69    /// \brief Optional "shadow dictionary" (private elements) used to parse
70    /// this header
71    Dict* RefShaDict;
72
73    /// \brief Size threshold above which an element value will NOT be loaded
74    /// in memory (to avoid loading the image/volume itself). By default,
75    /// this upper bound is fixed to 1024 bytes (which might look reasonable
76    /// when one considers the definition of the various VR contents).
77    uint32_t MaxSizeLoadEntry;
78    
79    /// \brief Size threshold above which an element value will NOT be *printed*
80    /// in order no to polute the screen output. By default, this upper bound
81    /// is fixed to 64 bytes.
82    uint32_t MaxSizePrintEntry;   
83
84 protected:
85    /// Refering underlying filename.
86    std::string Filename;
87
88    /// \brief SWap code (e.g. Big Endian, Little Endian, Bad Big Endian,
89    /// Bad Little Endian) according to the processor Endianity and
90    /// what is written on disc.
91    int SwapCode;
92
93    /// File Pointer, opened during Header parsing.
94    std::ifstream* Fp;
95
96    /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
97    FileType Filetype;  
98
99    /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
100    static const unsigned int HEADER_LENGTH_TO_READ; 
101
102    /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
103    /// are NOT loaded.
104    static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
105
106    /// \brief Elements whose value is longer than  MAX_SIZE_PRINT_ELEMENT_VALUE
107    /// are NOT printed.
108    /// \todo Currently not used since collides with #define in
109    ///       class DocEntry . See also
110    ///       method ref Document::SetMaxSizePrintEntry()
111    static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
112
113    /// Store the RLE frames info obtained during parsing of pixels.
114    RLEFramesInfo* RLEInfo;
115
116    /// Store the JPEG fragments info obtained during parsing of pixels.
117    JPEGFragmentsInfo* JPEGInfo;
118
119    /// \brief Amount of printed details for each Header Entry (Dicom Element):
120    /// 0 : stands for the least detail level.
121    int PrintLevel;
122    
123 public:
124 // the 2 following will be merged
125    virtual void PrintPubDict (std::ostream &os = std::cout);
126    virtual void PrintShaDict (std::ostream &os = std::cout);
127
128 // Dictionnaries
129    Dict* GetPubDict();
130    Dict* GetShaDict();
131    bool SetShaDict(Dict* dict);
132    bool SetShaDict(DictKey const & dictName);
133
134 // Informations contained in the parser
135    virtual bool IsReadable();
136    TransferSyntaxType GetTransferSyntax();
137    bool IsJPEGLossless();
138    bool IsJPEG2000();
139    bool IsJPEG();
140    bool IsEncapsulate();
141    bool IsDicomV3();
142
143    FileType GetFileType();
144
145    std::ifstream * OpenFile();
146    bool CloseFile();
147
148    void WriteContent( std::ofstream* fp, FileType type );
149
150    ValEntry* ReplaceOrCreateByNumber(std::string const & value,
151                                      uint16_t group, uint16_t elem,
152                                      TagName const & vr = "unkn");
153    
154    BinEntry* ReplaceOrCreateByNumber(uint8_t* binArea, int lgth,
155                                      uint16_t group, uint16_t elem,
156                                      TagName const & vr = "unkn");
157
158    SeqEntry* ReplaceOrCreateByNumber(uint16_t group, uint16_t elem);
159
160    bool ReplaceIfExistByNumber ( std::string const & value,
161                                  uint16_t group, uint16_t elem );
162    
163    virtual void LoadEntryBinArea(uint16_t group, uint16_t elem);
164    virtual void LoadEntryBinArea(BinEntry* entry);
165       
166    // System access (meaning endian related !?)
167    uint16_t SwapShort(uint16_t);   // needed by File
168    uint32_t SwapLong(uint32_t);    // needed by File
169    uint16_t UnswapShort(uint16_t); // needed by File
170    uint32_t UnswapLong(uint32_t);  // needed by File
171
172    static std::string GetTransferSyntaxValue(TransferSyntaxType type);
173
174 protected:
175    // Constructor and destructor are protected to forbid end user 
176    // to instanciate from this class Document (only Header and
177    // DicomDir are meaningfull).
178    Document();
179    Document( std::string const & filename );
180    virtual ~Document();
181    
182    void ReadAndSkipEncapsulatedBasicOffsetTable();
183    void ComputeRLEInfo();
184    void ComputeJPEGFragmentInfo();
185    // Entry
186    bool CheckIfEntryExistByNumber(uint16_t group, uint16_t elem );
187 public:
188    virtual std::string GetEntryByName    (TagName const & tagName);
189    virtual std::string GetEntryVRByName  (TagName const & tagName);
190    virtual std::string GetEntryByNumber  (uint16_t group, uint16_t elem);
191    virtual std::string GetEntryVRByNumber(uint16_t group, uint16_t elem);
192    virtual int     GetEntryLengthByNumber(uint16_t group, uint16_t elem);
193 //protected:
194    virtual bool SetEntryByName  (std::string const & content, 
195                                  TagName const & tagName );
196    virtual bool SetEntryByNumber(std::string const & content,
197                                  uint16_t group, uint16_t element);
198    virtual bool SetEntryByNumber(uint8_t* content, int lgth,
199                                  uint16_t group, uint16_t element);
200    virtual void* GetEntryBinAreaByNumber(uint16_t group, uint16_t elem);   
201    // FIXME
202    // Verify the usefull of this method... otherwise remove it
203    // It's body is commented in the .xx
204    //virtual bool  SetEntryBinAreaByNumber(uint8_t* a, uint16_t group,
205    //                                                uint16_t elem);
206
207    virtual void UpdateShaEntries();
208
209    // Header entry
210    DocEntry* GetDocEntryByNumber(uint16_t group, uint16_t element); 
211    DocEntry* GetDocEntryByName  (TagName const & tagName);
212
213    ValEntry* GetValEntryByNumber(uint16_t group, uint16_t element); 
214    //BinEntry* GetBinEntryByNumber(uint16_t group, uint16_t element); 
215    RLEFramesInfo* GetRLEInfo() { return RLEInfo; }
216    JPEGFragmentsInfo* GetJPEGInfo() { return JPEGInfo; }
217
218    void LoadDocEntrySafe(DocEntry* entry);
219    TagDocEntryHT* BuildFlatHashTable();
220
221 private:
222    // Read
223    void ParseDES(DocEntrySet *set,long offset, long l_max, bool delim_mode);
224    void ParseSQ (SeqEntry *seq,   long offset, long l_max, bool delim_mode);
225
226    void LoadDocEntry      (DocEntry *);
227    void FindDocEntryLength(DocEntry *) throw ( FormatError );
228    void FindDocEntryVR    (DocEntry *);
229    bool CheckDocEntryVR   (DocEntry *, VRKey);
230
231    std::string GetDocEntryValue  (DocEntry *);
232    std::string GetDocEntryUnvalue(DocEntry *);
233
234    void SkipDocEntry          (DocEntry *);
235    void SkipToNextDocEntry    (DocEntry *);
236
237    void FixDocEntryFoundLength(DocEntry *, uint32_t);
238    bool IsDocEntryAnInteger   (DocEntry *);
239
240    uint32_t FindDocEntryLengthOB() throw( FormatUnexpected );
241
242    uint16_t ReadInt16() throw ( FormatError );
243    uint32_t ReadInt32() throw ( FormatError );
244    void     SkipBytes(uint32_t);
245    bool     ReadTag(uint16_t, uint16_t);
246    uint32_t ReadTagLength(uint16_t, uint16_t);
247
248    void Initialise();
249    bool CheckSwap();
250    void SwitchSwapToBigEndian();
251    void SetMaxSizeLoadEntry(long);
252    void SetMaxSizePrintEntry(long);
253
254    // DocEntry related utilities
255    DocEntry* ReadNextDocEntry();
256
257    uint32_t GenerateFreeTagKeyInGroup(uint16_t group);
258    void BuildFlatHashTableRecurse( TagDocEntryHT& builtHT,
259                                    DocEntrySet* set );
260
261    void HandleBrokenEndian(uint16_t  group, uint16_t  elem);
262 public:
263 // Accessors:
264    /// Accessor to \ref PrintLevel
265    void SetPrintLevel(int level) { PrintLevel = level; }
266
267    /// Accessor to \ref Filename
268    const std::string &GetFileName() const { return Filename; }
269
270    /// Accessor to \ref Filename
271    void SetFileName(std::string const & fileName) { Filename = fileName; }
272
273    /// 'Swap code' accessor (see \ref SwapCode )
274    int GetSwapCode() { return SwapCode; }
275    
276    bool operator<(Document &document);
277
278 };
279 } // end namespace gdcm
280
281 //-----------------------------------------------------------------------------
282 #endif