]> Creatis software - gdcm.git/blob - src/gdcmFileHelper.h
FileHelper has CallProgressMethod and others
[gdcm.git] / src / gdcmFileHelper.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmFileHelper.h,v $
5   Language:  C++
6   Date:      $Date: 2005/11/30 13:42:19 $
7   Version:   $Revision: 1.37 $
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 "gdcmDebug.h"
23 #include "gdcmRefCounter.h"
24
25
26 namespace gdcm 
27 {
28 class File;
29 class DataEntry;
30 class SeqEntry;
31 class PixelReadConvert;
32 class PixelWriteConvert;
33 class DocEntryArchive;
34
35 typedef void (*VOID_FUNCTION_PUINT8_PFILE_POINTER)(uint8_t *, File *);
36
37 //-----------------------------------------------------------------------------
38 /**
39  * \brief In addition to Dicom header exploration, this class is designed
40  * for accessing the image/volume content. One can also use it to
41  * write Dicom/ACR-NEMA/RAW files.
42  */
43 class GDCM_EXPORT FileHelper : public RefCounter
44 {
45    gdcmTypeMacro(FileHelper);
46
47 public:
48    enum FileMode
49    {
50       WMODE_RAW,
51       WMODE_RGB
52    };
53      
54 public:
55 /// \brief Constructs a FileHelper with a RefCounter
56    static FileHelper *New() {return new FileHelper();}
57 /// \brief Constructs a FileHelper with a RefCounter from a fileHelper  
58    static FileHelper *New(File *header) {return new FileHelper(header);}
59    
60    void Print(std::ostream &os = std::cout, std::string const &indent = ""); 
61
62    /// Accessor to \ref File
63    File *GetFile() { return FileInternal; }
64    
65
66    void SetLoadMode(int loadMode);
67    void SetFileName(std::string const &fileName);
68    bool Load();
69    /// to allow user to modify pixel order (e.g. Mirror, UpsideDown,...)
70    void SetUserFunction( VOID_FUNCTION_PUINT8_PFILE_POINTER userFunc ) 
71                         { UserFunction = userFunc; }   
72    // File methods
73    bool SetEntryString(std::string const &content,
74                            uint16_t group, uint16_t elem);
75    bool SetEntryBinArea(uint8_t *content, int lgth,
76                             uint16_t group, uint16_t elem);
77
78    DataEntry *InsertEntryString(std::string const &content,
79                                        uint16_t group, uint16_t elem);
80    DataEntry *InsertEntryBinArea(uint8_t *binArea, int lgth,
81                                         uint16_t group, uint16_t elem);
82    SeqEntry *InsertSeqEntry(uint16_t group, uint16_t elem);
83
84    // File helpers
85    size_t GetImageDataSize();
86    size_t GetImageDataRawSize();
87
88    uint8_t *GetImageData();
89    uint8_t *GetImageDataRaw();
90
91    GDCM_LEGACY(size_t GetImageDataIntoVector(void *destination,size_t maxSize));
92
93    void SetImageData(uint8_t *data, size_t expectedSize);
94
95    // User data
96    void SetUserData(uint8_t *data, size_t expectedSize);
97    uint8_t *GetUserData();
98    size_t GetUserDataSize();
99    // RBG data (from file)
100    uint8_t *GetRGBData();
101    size_t GetRGBDataSize();
102    // RAW data (from file)
103    uint8_t *GetRawData();
104    size_t GetRawDataSize();
105
106    // LUT
107    uint8_t* GetLutRGBA();
108    int GetLutItemNumber();
109    int GetLutItemSize();
110
111    // Write mode
112
113    /// \brief Tells the writer we want to keep 'Grey pixels + Palettes color'
114    ///        when possible (as opposed to convert 'Palettes color' to RGB)
115    void SetWriteModeToRaw()           { SetWriteMode(WMODE_RAW);  }
116    /// \brief Tells the writer we want to write RGB image when possible
117    ///        (as opposed to 'Grey pixels + Palettes color')
118    void SetWriteModeToRGB()           { SetWriteMode(WMODE_RGB);  }
119    /// \brief Sets the Write Mode ( )
120    void SetWriteMode(FileMode mode)   { WriteMode = mode;         }
121    /// \brief Gets the Write Mode ( )
122    FileMode GetWriteMode()            { return WriteMode;         }
123
124    // Write format
125
126    /// \brief Tells the writer we want to write as Implicit VR
127    void SetWriteTypeToDcmImplVR()     { SetWriteType(ImplicitVR); }
128    /// \brief Tells the writer we want to write as Explicit VR
129    void SetWriteTypeToDcmExplVR()     { SetWriteType(ExplicitVR); }
130    /// \brief Tells the writer we want to write as ACR-NEMA
131    void SetWriteTypeToAcr()           { SetWriteType(ACR);        }
132    /// \brief Tells the writer we want to write as LibIDO
133    void SetWriteTypeToAcrLibido()     { SetWriteType(ACR_LIBIDO); }
134    /// \brief Tells the writer we want to write as JPEG   
135    void SetWriteTypeToJPEG()          { SetWriteType(JPEG);       }
136    /// \brief Tells the writer which format we want to write
137    /// (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
138    void SetWriteType(FileType format) { WriteType = format;       }
139    /// \brief Gets the format we talled the write we wanted to write
140    /// (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
141    FileType GetWriteType()            { return WriteType;         }
142
143    // Write pixels of ONE image on hard drive
144    // No test is made on processor "endianness"
145    // The user must call his reader correctly
146    bool WriteRawData  (std::string const &fileName);
147    bool WriteDcmImplVR(std::string const &fileName);
148    bool WriteDcmExplVR(std::string const &fileName);
149    bool WriteAcr      (std::string const &fileName);
150    bool Write         (std::string const &fileName);
151    /// \brief if user knows he didn't modify the pixels (e.g. he just anonymized 
152    ///        the file), he is allowed to ask to keep the original
153    ///        'Media Storage SOP Class UID' and 'Image Type'   
154    void SetKeepMediaStorageSOPClassUID (bool v) 
155                               { KeepMediaStorageSOPClassUID = v; }
156    // no GetKeepMediaStorageSOPClassUID() method, on purpose!
157
158    void CallStartMethod();
159    void CallProgressMethod();
160    void CallEndMethod();
161    
162 protected:
163    FileHelper( );
164    FileHelper( File *header );
165    ~FileHelper();
166
167    bool CheckWriteIntegrity();
168
169    void SetWriteToRaw();
170    void SetWriteToRGB();
171    void RestoreWrite();
172
173    void SetWriteFileTypeToACR();
174    void SetWriteFileTypeToJPEG();
175    void SetWriteFileTypeToExplicitVR();
176    void SetWriteFileTypeToImplicitVR();
177    void RestoreWriteFileType();
178
179    void SetWriteToLibido();
180    void SetWriteToNoLibido();
181    void RestoreWriteOfLibido();
182
183    DataEntry *CopyDataEntry(uint16_t group, uint16_t elem, 
184                                const TagName &vr = GDCM_VRUNKNOWN);
185    void CheckMandatoryElements();
186    void CheckMandatoryEntry(uint16_t group, uint16_t elem, std::string value);
187    void SetMandatoryEntry(uint16_t group, uint16_t elem, std::string value);
188    void CopyMandatoryEntry(uint16_t group, uint16_t elem, std::string value);
189    void RestoreWriteMandatory();
190
191 private:
192    void Initialize();
193
194    uint8_t *GetRaw();
195
196 // members variables:
197 protected:
198    /// value of the ??? for any progress bar
199    float Progress;
200    mutable bool Abort;
201    
202 private:
203
204    /// gdcm::File to use to load the file
205    File *FileInternal;
206
207    /// Whether already parsed or not
208    bool Parsed;
209
210    // Utility pixel converter
211    /// \brief Pointer to the PixelReadConverter
212    PixelReadConvert *PixelReadConverter;
213    /// \brief Pointer to the PixelWriteConverter
214    PixelWriteConvert *PixelWriteConverter;
215
216    // Utility header archive
217    /// \brief Pointer to the DocEntryArchive (used while writting process)
218    DocEntryArchive *Archive;
219
220    // Write variables
221    /// \brief (WMODE_RAW, WMODE_RGB)
222    FileMode WriteMode;
223    
224    /// \brief (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
225    FileType WriteType;
226    
227    /// \brief Pointer to a user supplied function to allow modification 
228    /// of pixel order (e.g. : Mirror, UpsideDown, 90°Rotation, ...)
229    /// use as : void userSuppliedFunction(uint8_t *im, gdcm::File *f)
230    /// NB : the "uint8_t *" type of first param is just for prototyping.
231    /// User will Cast it according what he founds with f->GetPixelType()
232    /// See vtkgdcmSerieViewer for an example
233    VOID_FUNCTION_PUINT8_PFILE_POINTER UserFunction;
234    
235    /// \brief if user knows he didn't modify the pixels (e.g. he just 
236    /// anonymized the file), he is allowed to ask to keep the original
237    /// 'Media Storage SOP Class UID' and 'Image Type'  
238    bool KeepMediaStorageSOPClassUID;
239 };
240 } // end namespace gdcm
241
242 //-----------------------------------------------------------------------------
243 #endif