]> Creatis software - gdcm.git/blob - src/gdcmFileHelper.h
Try to make gdcm1.2 and gdcm1.3 usable int the same source code.
[gdcm.git] / src / gdcmFileHelper.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmFileHelper.h,v $
5   Language:  C++
6   Date:      $Date: 2007/08/22 16:14:04 $
7   Version:   $Revision: 1.52 $
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 \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, const VRKey &vr);
80    DataEntry *InsertEntryBinArea(uint8_t *binArea, int lgth,
81                                 uint16_t group, uint16_t elem, const VRKey &vr);
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    void ConvertFixGreyLevels(uint8_t *data, size_t size);
107
108    // LUT
109    uint8_t* GetLutRGBA();
110    int GetLutItemNumber();
111    int GetLutItemSize();
112
113    // Write mode
114
115    /// \brief Tells the writer we want to keep 'Grey pixels + Palettes color'
116    ///        when possible (as opposed to convert 'Palettes color' to RGB)
117    void SetWriteModeToRaw()           { SetWriteMode(WMODE_RAW);  }
118    /// \brief Tells the writer we want to write RGB image when possible
119    ///        (as opposed to 'Grey pixels + Palettes color')
120    void SetWriteModeToRGB()           { SetWriteMode(WMODE_RGB);  }
121    /// \brief Sets the Write Mode ( )
122    void SetWriteMode(FileMode mode)   { WriteMode = mode;         }
123    /// \brief Gets the Write Mode ( )
124    FileMode GetWriteMode()            { return WriteMode;         }
125
126    // Write format
127
128    /// \brief Tells the writer we want to write as Implicit VR
129    void SetWriteTypeToDcmImplVR()     { SetWriteType(ImplicitVR); }
130    /// \brief Tells the writer we want to write as Explicit VR
131    void SetWriteTypeToDcmExplVR()     { SetWriteType(ExplicitVR); }
132    /// \brief Tells the writer we want to write as ACR-NEMA
133    void SetWriteTypeToAcr()           { SetWriteType(ACR);        }
134    /// \brief Tells the writer we want to write as LibIDO
135    void SetWriteTypeToAcrLibido()     { SetWriteType(ACR_LIBIDO); }
136    /// \brief Tells the writer we want to write as JPEG
137    void SetWriteTypeToJPEG()          { SetWriteType(JPEG);       }
138    /// \brief Tells the writer we want to write as JPEG2000
139    void SetWriteTypeToJPEG2000()      { SetWriteType(JPEG2000);   }
140    /// \brief Tells the writer which format we want to write
141    /// (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
142    void SetWriteType(FileType format) { WriteType = format;       }
143    /// \brief Gets the format we talled the write we wanted to write
144    /// (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
145    FileType GetWriteType()            { return WriteType;         }
146    /// \brief 1 : white=0, black=high value
147    void SetPhotometricInterpretationToMonochrome1() {
148                                     PhotometricInterpretation = 1;}
149    /// \brief 2 : black=0, white=high value  (default)
150    void SetPhotometricInterpretationToMonochrome2() {
151                                     PhotometricInterpretation = 2;}
152    /// \brief 1 : white=0, black=high value
153    int GetPhotometricInterpretation() {
154                                 return PhotometricInterpretation; }
155
156    // Write pixels of ONE image on hard drive
157    // No test is made on processor "endianness"
158    // The user must call his reader correctly
159    bool WriteRawData  (std::string const &fileName);
160    bool WriteDcmImplVR(std::string const &fileName);
161    bool WriteDcmExplVR(std::string const &fileName);
162    bool WriteAcr      (std::string const &fileName);
163    bool Write         (std::string const &fileName);
164    
165 /// \brief We have to deal with 4 *very* different cases :
166 /// -1) user created ex nihilo his own image and wants to write it as a Dicom image.
167 ///    USER_OWN_IMAGE
168 /// -2) user modified the pixels of an existing image.
169 ///    FILTERED_IMAGE
170 /// -3) user created a new image, using existing images (eg MIP, MPR, cartography image)
171 ///   CREATED_IMAGE
172 /// -4) user modified/added some tags *without processing* the pixels (anonymization...
173 ///   UNMODIFIED_PIXELS_IMAGE
174
175    void SetContentType (ImageContentType c) { ContentType = c; }
176    // no GetContentType() method, on purpose!
177    
178    void CallStartMethod();
179    void CallProgressMethod();
180    void CallEndMethod();
181    
182 protected:
183    FileHelper( );
184    FileHelper( File *header );
185    ~FileHelper();
186
187    /// \todo move all those 'protected' methods to 'private'
188    ///       since FileHelper is not derived in anything!
189    bool CheckWriteIntegrity();
190
191    void SetWriteToRaw();
192    void SetWriteToRGB();
193    void RestoreWrite();
194
195    void SetWriteFileTypeToACR();
196    void SetWriteFileTypeToJPEG();
197    void SetWriteFileTypeToJPEG2000();
198    void SetWriteFileTypeToExplicitVR();
199    void SetWriteFileTypeToImplicitVR();
200    void RestoreWriteFileType();
201
202    void SetWriteToLibido();
203    void SetWriteToNoLibido();
204    void RestoreWriteOfLibido();
205
206    DataEntry *CopyDataEntry(uint16_t group, uint16_t elem, 
207                             const VRKey &vr = GDCM_VRUNKNOWN);
208    void CheckMandatoryElements();
209    void CheckMandatoryEntry(uint16_t group, uint16_t elem, std::string value,
210                             const VRKey &vr = GDCM_VRUNKNOWN);
211    void SetMandatoryEntry(uint16_t group, uint16_t elem, std::string value,
212                           const VRKey &vr = GDCM_VRUNKNOWN);
213    void CopyMandatoryEntry(uint16_t group, uint16_t elem, std::string value,
214                            const VRKey &vr = GDCM_VRUNKNOWN);
215    void RestoreWriteMandatory();
216
217 private:
218    void Initialize();
219
220    uint8_t *GetRaw();
221
222 // members variables:
223 protected:
224    /// value of the ??? for any progress bar
225    float Progress;
226    mutable bool Abort;
227    
228 private:
229
230    /// gdcm::File to use to load the file
231    File *FileInternal;
232
233    /// Whether already parsed or not
234    bool Parsed;
235
236    // Utility pixel converter
237    /// \brief Pointer to the PixelReadConverter
238    PixelReadConvert *PixelReadConverter;
239    /// \brief Pointer to the PixelWriteConverter
240    PixelWriteConvert *PixelWriteConverter;
241
242    // Utility header archive
243    /// \brief Pointer to the DocEntryArchive (used while writting process)
244    DocEntryArchive *Archive;
245
246    // Write variables
247    /// \brief (WMODE_RAW, WMODE_RGB)
248    FileMode WriteMode;
249
250    /// \brief (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
251    FileType WriteType;
252
253    /// \brief Pointer to a user supplied function to allow modification 
254    /// of pixel order (e.g. : Mirror, UpsideDown, 90°Rotation, ...)
255    /// use as : void userSuppliedFunction(uint8_t *im, gdcm::File *f)
256    /// NB : the "uint8_t *" type of first param is just for prototyping.
257    /// User will Cast it according what he founds with f->GetPixelType()
258    /// See vtkgdcmSerieViewer for an example
259    VOID_FUNCTION_PUINT8_PFILE_POINTER UserFunction;
260    
261    /// \brief only user knows what he did before asking the image to be written
262    /// - he created ex nihilo his own image
263    /// - he just applied a mathematical process on the pixels
264    /// - he created a new image, using existing images (eg MIP, MPR,cartography)
265    /// - he anonymized and image (*no* modif on the pixels)
266    ImageContentType ContentType;
267  
268    /// \brief  1 : white=0, black=high value
269    ///         2 : black=0, white=high value (default)
270    int PhotometricInterpretation;
271
272 };
273 } // end namespace gdcm
274
275 //-----------------------------------------------------------------------------
276 #endif