]> Creatis software - gdcm.git/blob - src/gdcmFileHelper.h
99633894cbcfbd1e782e5f31f1fbeeef405d0d17
[gdcm.git] / src / gdcmFileHelper.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmFileHelper.h,v $
5   Language:  C++
6   Date:      $Date: 2005/02/21 17:45:41 $
7   Version:   $Revision: 1.15 $
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 GDCMFILEHELPER_H
20 #define GDCMFILEHELPER_H
21
22 #include <iostream>
23 #include "gdcmBase.h"
24
25 namespace gdcm 
26 {
27 class File;
28 class ValEntry;
29 class BinEntry;
30 class SeqEntry;
31 class PixelReadConvert;
32 class PixelWriteConvert;
33 class DocEntryArchive;
34 //-----------------------------------------------------------------------------
35 /**
36  * \brief In addition to Dicom header exploration, this class is designed
37  * for accessing the image/volume content. One can also use it to
38  * write Dicom/ACR-NEMA/RAW files.
39  */
40 class GDCM_EXPORT FileHelper : public Base
41 {
42 public:
43    enum FileMode
44    {
45       WMODE_RAW,
46       WMODE_RGB
47    };
48      
49 public:
50    FileHelper( );
51    FileHelper( File *header );
52    FileHelper( std::string const &filename );
53  
54    virtual ~FileHelper();
55
56    void Print(std::ostream &os = std::cout, std::string const &indent = ""); 
57
58    /// Accessor to \ref File
59    File *GetFile() { return FileInternal; }
60
61    // File methods
62    bool SetValEntry(std::string const &content,
63                     uint16_t group, uint16_t elem);
64    bool SetBinEntry(uint8_t *content, int lgth,
65                     uint16_t group, uint16_t elem);
66
67    ValEntry *InsertValEntry(std::string const &content,
68                             uint16_t group, uint16_t elem);
69    BinEntry *InsertBinEntry(uint8_t *binArea, int lgth,
70                             uint16_t group, uint16_t elem);
71    SeqEntry *InsertSeqEntry(uint16_t group, uint16_t elem);
72
73    // File helpers
74    size_t GetImageDataSize();
75    size_t GetImageDataRawSize();
76
77    uint8_t *GetImageData();
78    uint8_t *GetImageDataRaw();
79    size_t GetImageDataIntoVector(void *destination, size_t maxSize);
80
81    void SetImageData(uint8_t *data, size_t expectedSize);
82
83    // User data
84    void SetUserData(uint8_t *data, size_t expectedSize);
85    uint8_t *GetUserData();
86    size_t GetUserDataSize();
87    // RBG data (from file)
88    uint8_t *GetRGBData();
89    size_t GetRGBDataSize();
90    // RAW data (from file)
91    uint8_t *GetRawData();
92    size_t GetRawDataSize();
93
94    // LUT
95    uint8_t* GetLutRGBA();
96
97    // Write mode
98
99    /// \brief Tells the writer we want to keep 'Grey pixels + Palettes color'
100    ///        when possible (as opposed to convert 'Palettes color' to RGB)
101   void SetWriteModeToRaw()            { SetWriteMode(WMODE_RAW);  };
102    /// \brief Tells the writer we want to write RGB image when possible
103    ///        (as opposed to 'Grey pixels + Palettes color')
104    void SetWriteModeToRGB()           { SetWriteMode(WMODE_RGB);  };
105    /// \brief Sets the Write Mode ( )
106    void SetWriteMode(FileMode mode)   { WriteMode = mode;         };
107    /// \brief Gets the Write Mode ( )
108    FileMode GetWriteMode()            { return WriteMode;         };
109
110    // Write format
111
112    /// \brief Tells the writer we want to write as Implicit VR
113    void SetWriteTypeToDcmImplVR()     { SetWriteType(ImplicitVR); };
114    /// \brief Tells the writer we want to write as Explicit VR
115    void SetWriteTypeToDcmExplVR()     { SetWriteType(ExplicitVR); };
116    /// \brief Tells the writer we want to write as ACR-NEMA
117    void SetWriteTypeToAcr()           { SetWriteType(ACR);        };
118    /// \brief Tells the writer we want to write as LibIDO
119    void SetWriteTypeToAcrLibido()     { SetWriteType(ACR_LIBIDO); };
120    /// \brief Tells the writer which format we want to write
121    /// (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
122    void SetWriteType(FileType format) { WriteType = format;       };
123    /// \brief Gets the format we talled the write we wanted to write
124    ///   (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
125    FileType GetWriteType()            { return WriteType;         };
126
127    // Write pixels of ONE image on hard drive
128    // No test is made on processor "endianness"
129    // The user must call his reader correctly
130    bool WriteRawData  (std::string const &fileName);
131    bool WriteDcmImplVR(std::string const &fileName);
132    bool WriteDcmExplVR(std::string const &fileName);
133    bool WriteAcr      (std::string const &fileName);
134    bool Write         (std::string const &fileName);
135
136 protected:
137    bool CheckWriteIntegrity();
138
139    void SetWriteToRaw();
140    void SetWriteToRGB();
141    void RestoreWrite();
142
143    void SetWriteFileTypeToACR();
144    void SetWriteFileTypeToExplicitVR();
145    void SetWriteFileTypeToImplicitVR();
146    void RestoreWriteFileType();
147
148    void SetWriteToLibido();
149    void SetWriteToNoLibido();
150    void RestoreWriteOfLibido();
151
152    ValEntry *CopyValEntry(uint16_t group, uint16_t elem);
153    BinEntry *CopyBinEntry(uint16_t group, uint16_t elem, 
154                           const std::string &vr);
155    void CheckMandatoryElements();
156
157 private:
158    void Initialize();
159
160    uint8_t *GetRaw();
161
162 // members variables:
163    /// gdcm::File to use to load the file
164    File *FileInternal;
165
166    /// \brief Whether the underlying \ref gdcm::File was loaded by
167    ///  the constructor or passed to the constructor. 
168    ///  When false the destructor is in charge of deletion.
169    bool SelfHeader;
170    
171    /// Wether already parsed or not
172    bool Parsed;
173
174    // Utility pixel converter
175    /// \brief Pointer to the PixelReadConverter
176    PixelReadConvert *PixelReadConverter;
177    /// \brief Pointer to the PixelWriteConverter
178    PixelWriteConvert *PixelWriteConverter;
179
180    // Utility header archive
181    /// \brief Pointer to the DocEntryArchive (used while writting process)
182    DocEntryArchive *Archive;
183
184    // Write variables
185    /// \brief (WMODE_RAW, WMODE_RGB)
186    FileMode WriteMode;
187    /// \brief (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
188    FileType WriteType;
189 };
190 } // end namespace gdcm
191
192 //-----------------------------------------------------------------------------
193 #endif