]> Creatis software - gdcm.git/blob - src/gdcmFileHelper.h
Fix mistypings
[gdcm.git] / src / gdcmFileHelper.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmFileHelper.h,v $
5   Language:  C++
6   Date:      $Date: 2007/09/17 12:16:01 $
7   Version:   $Revision: 1.55 $
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 #include "gdcmVRKey.h"
25 #include "gdcmFile.h"
26
27 namespace GDCM_NAME_SPACE
28 {
29 //class File;
30 class DataEntry;
31 class SeqEntry;
32 class PixelReadConvert;
33 class PixelWriteConvert;
34 class DocEntryArchive;
35
36 typedef void (*VOID_FUNCTION_PUINT8_PFILE_POINTER)(uint8_t *, File *);
37
38 //-----------------------------------------------------------------------------
39 /**
40  * \brief In addition to Dicom header exploration, this class is designed
41  * for accessing the image/volume content. One can also use it to
42  * write Dicom/ACR-NEMA/RAW files.
43  */
44 class GDCM_EXPORT FileHelper : public RefCounter
45 {
46    gdcmTypeMacro(FileHelper);
47
48 public:
49    enum FileMode
50    {
51       WMODE_RAW,
52       WMODE_RGB
53    };
54
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 File
63    File *GetFile() { return FileInternal; }
64    
65    /// \brief Tells gdcm wether we want to keep ACR-NEMA-like overlays or not.  
66    void SetKeepOverlays(bool k) { KeepOverlays =k; }
67    bool GetKeepOverlays( )      { return KeepOverlays; }
68      
69    void SetLoadMode(int loadMode);
70    void SetFileName(std::string const &fileName);
71    bool Load();
72    /// to allow user to modify pixel order (e.g. Mirror, UpsideDown,...)
73    void SetUserFunction( VOID_FUNCTION_PUINT8_PFILE_POINTER userFunc ) 
74                         { UserFunction = userFunc; }   
75    // File methods
76    bool SetEntryString(std::string const &content,
77                            uint16_t group, uint16_t elem);
78    bool SetEntryBinArea(uint8_t *content, int lgth,
79                             uint16_t group, uint16_t elem);
80
81    DataEntry *InsertEntryString(std::string const &content,
82                                 uint16_t group, uint16_t elem, const VRKey &vr);
83    DataEntry *InsertEntryBinArea(uint8_t *binArea, int lgth,
84                                 uint16_t group, uint16_t elem, const VRKey &vr);
85    SeqEntry *InsertSeqEntry(uint16_t group, uint16_t elem);
86
87    // File helpers
88    size_t GetImageDataSize();
89    size_t GetImageDataRawSize();
90
91    uint8_t *GetImageData();
92    uint8_t *GetImageDataRaw();
93
94 // GDCM_LEGACY(size_t GetImageDataIntoVector(void *destination,size_t maxSize))
95
96    void SetImageData(uint8_t *data, size_t expectedSize);
97
98    // User data
99    void SetUserData(uint8_t *data, size_t expectedSize);
100    uint8_t *GetUserData();
101    size_t GetUserDataSize();
102    // RBG data (from file)
103    uint8_t *GetRGBData();
104    size_t GetRGBDataSize();
105    // RAW data (from file)
106    uint8_t *GetRawData();
107    size_t GetRawDataSize();
108
109    void ConvertFixGreyLevels(uint8_t *data, size_t size);
110
111    // LUT
112    uint8_t* GetLutRGBA();
113    int GetLutItemNumber();
114    int GetLutItemSize();
115
116    // Write mode
117
118    /// \brief Tells the writer we want to keep 'Grey pixels + Palettes color'
119    ///        when possible (as opposed to convert 'Palettes color' to RGB)
120    void SetWriteModeToRaw()           { SetWriteMode(WMODE_RAW);  }
121    /// \brief Tells the writer we want to write RGB image when possible
122    ///        (as opposed to 'Grey pixels + Palettes color')
123    void SetWriteModeToRGB()           { SetWriteMode(WMODE_RGB);  }
124    /// \brief Sets the Write Mode ( )
125    void SetWriteMode(FileMode mode)   { WriteMode = mode;         }
126    /// \brief Gets the Write Mode ( )
127    FileMode GetWriteMode()            { return WriteMode;         }
128
129    // Write format
130
131    /// \brief Tells the writer we want to write as Implicit VR
132    void SetWriteTypeToDcmImplVR()     { SetWriteType(ImplicitVR); }
133    /// \brief Tells the writer we want to write as Explicit VR
134    void SetWriteTypeToDcmExplVR()     { SetWriteType(ExplicitVR); }
135    /// \brief Tells the writer we want to write as ACR-NEMA
136    void SetWriteTypeToAcr()           { SetWriteType(ACR);        }
137    /// \brief Tells the writer we want to write as LibIDO
138    void SetWriteTypeToAcrLibido()     { SetWriteType(ACR_LIBIDO); }
139    /// \brief Tells the writer we want to write as JPEG
140    void SetWriteTypeToJPEG()          { SetWriteType(JPEG);       }
141    /// \brief Tells the writer we want to write as JPEG2000
142    void SetWriteTypeToJPEG2000()      { SetWriteType(JPEG2000);   }
143    /// \brief Tells the writer which format we want to write
144    /// (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
145    void SetWriteType(FileType format) { WriteType = format;       }
146    /// \brief Gets the format we talled the write we wanted to write
147    /// (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
148    FileType GetWriteType()            { return WriteType;         }
149    /// \brief 1 : white=0, black=high value
150    void SetPhotometricInterpretationToMonochrome1() {
151                                     PhotometricInterpretation = 1;}
152    /// \brief 2 : black=0, white=high value  (default)
153    void SetPhotometricInterpretationToMonochrome2() {
154                                     PhotometricInterpretation = 2;}
155    /// \brief 1 : white=0, black=high value
156    int GetPhotometricInterpretation() {
157                                 return PhotometricInterpretation; }
158
159    // Write pixels of ONE image on hard drive
160    // No test is made on processor "endianness"
161    // The user must call his reader correctly
162    bool WriteRawData  (std::string const &fileName);
163    bool WriteDcmImplVR(std::string const &fileName);
164    bool WriteDcmExplVR(std::string const &fileName);
165    bool WriteAcr      (std::string const &fileName);
166    bool Write         (std::string const &fileName);
167    
168 /// \brief We have to deal with 4 *very* different cases :
169 /// -1) user created ex nihilo his own image and wants to write it as a Dicom image.
170 ///    USER_OWN_IMAGE
171 /// -2) user modified the pixels of an existing image.
172 ///    FILTERED_IMAGE
173 /// -3) user created a new image, using existing images (eg MIP, MPR, cartography image)
174 ///   CREATED_IMAGE
175 /// -4) user modified/added some tags *without processing* the pixels (anonymization...
176 ///   UNMODIFIED_PIXELS_IMAGE
177
178    void SetContentType (ImageContentType c) { ContentType = c; }
179    // no GetContentType() method, on purpose!
180    
181    void CallStartMethod();
182    void CallProgressMethod();
183    void CallEndMethod();
184    
185 protected:
186    FileHelper( );
187    FileHelper( File *header );
188    ~FileHelper();
189
190    /// \todo move all those 'protected' methods to 'private'
191    ///       since FileHelper is not derived in anything!
192    bool CheckWriteIntegrity();
193
194    void SetWriteToRaw();
195    void SetWriteToRGB();
196    void RestoreWrite();
197
198    void SetWriteFileTypeToACR();
199    void SetWriteFileTypeToJPEG();
200    void SetWriteFileTypeToJPEG2000();
201    void SetWriteFileTypeToExplicitVR();
202    void SetWriteFileTypeToImplicitVR();
203    void RestoreWriteFileType();
204
205    void SetWriteToLibido();
206    void SetWriteToNoLibido();
207    void RestoreWriteOfLibido();
208
209    DataEntry *CopyDataEntry(uint16_t group, uint16_t elem, 
210                             const VRKey &vr = GDCM_VRUNKNOWN);
211    void CheckMandatoryElements();
212    void CheckMandatoryEntry(uint16_t group, uint16_t elem, std::string value,
213                             const VRKey &vr = GDCM_VRUNKNOWN);
214    void SetMandatoryEntry(uint16_t group, uint16_t elem, std::string value,
215                           const VRKey &vr = GDCM_VRUNKNOWN);
216    void CopyMandatoryEntry(uint16_t group, uint16_t elem, std::string value,
217                            const VRKey &vr = GDCM_VRUNKNOWN);
218    void RestoreWriteMandatory();
219
220 private:
221    void Initialize();
222
223    uint8_t *GetRaw();
224
225 // members variables:
226 protected:
227    /// value of the ??? for any progress bar
228    float Progress;
229    mutable bool Abort;
230    
231 private:
232
233    /// gdcm::File to use to load the file
234    File *FileInternal;
235
236    /// Whether already parsed or not
237    bool Parsed;
238
239    // Utility pixel converter
240    
241    /// \brief Pointer to the PixelReadConverter
242    PixelReadConvert *PixelReadConverter;
243    
244    /// \brief Pointer to the PixelWriteConverter
245    PixelWriteConvert *PixelWriteConverter;
246
247    // Utility header archive
248    /// \brief Pointer to the DocEntryArchive (used while writting process)
249    DocEntryArchive *Archive;
250
251    // Write variables
252    /// \brief (WMODE_RAW, WMODE_RGB)
253    FileMode WriteMode;
254
255    /// \brief (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
256    FileType WriteType;
257
258    /// \brief Pointer to a user supplied function to allow modification 
259    /// of pixel order (e.g. : Mirror, UpsideDown, 90°Rotation, ...)
260    /// use as : void userSuppliedFunction(uint8_t *im, gdcm::File *f)
261    /// NB : the "uint8_t *" type of first param is just for prototyping.
262    /// User will Cast it according what he founds with f->GetPixelType()
263    /// See vtkgdcmSerieViewer for an example
264    VOID_FUNCTION_PUINT8_PFILE_POINTER UserFunction;
265    
266    /// \brief only user knows what he did before asking the image to be written
267    /// - he created ex nihilo his own image
268    /// - he just applied a mathematical process on the pixels
269    /// - he created a new image, using existing images (eg MIP, MPR,cartography)
270    /// - he anonymized and image (*no* modif on the pixels)
271    ImageContentType ContentType;
272  
273    /// \brief  1 : white=0, black=high value
274    ///         2 : black=0, white=high value (default)
275    int PhotometricInterpretation;
276  
277    /// \brief wether we want to keep ACR-NEMA-like overlays or not.
278    bool KeepOverlays;   
279
280 };
281 } // end namespace gdcm
282
283 //-----------------------------------------------------------------------------
284 #endif