]> Creatis software - gdcm.git/blob - src/gdcmFileHelper.h
Unnormalized 4th dimension is now dealt with.
[gdcm.git] / src / gdcmFileHelper.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmFileHelper.h,v $
5   Language:  C++
6   Date:      $Date: 2006/03/29 16:09:48 $
7   Version:   $Revision: 1.41 $
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 "gdcmFile.h"
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 /// \brief Constructs a FileHelper with a RefCounter
55    static FileHelper *New() {return new FileHelper();}
56 /// \brief Constructs a FileHelper with a RefCounter from a fileHelper  
57    static FileHelper *New(File *header) {return new FileHelper(header);}
58    
59    void Print(std::ostream &os = std::cout, std::string const &indent = ""); 
60
61    /// Accessor to \ref File
62    File *GetFile() { return FileInternal; }
63    
64
65    void SetLoadMode(int loadMode);
66    void SetFileName(std::string const &fileName);
67    bool Load();
68    /// to allow user to modify pixel order (e.g. Mirror, UpsideDown,...)
69    void SetUserFunction( VOID_FUNCTION_PUINT8_PFILE_POINTER userFunc ) 
70                         { UserFunction = userFunc; }   
71    // File methods
72    bool SetEntryString(std::string const &content,
73                            uint16_t group, uint16_t elem);
74    bool SetEntryBinArea(uint8_t *content, int lgth,
75                             uint16_t group, uint16_t elem);
76
77    DataEntry *InsertEntryString(std::string const &content,
78                                        uint16_t group, uint16_t elem);
79    DataEntry *InsertEntryBinArea(uint8_t *binArea, int lgth,
80                                         uint16_t group, uint16_t elem);
81    SeqEntry *InsertSeqEntry(uint16_t group, uint16_t elem);
82
83    // File helpers
84    size_t GetImageDataSize();
85    size_t GetImageDataRawSize();
86
87    uint8_t *GetImageData();
88    uint8_t *GetImageDataRaw();
89
90    GDCM_LEGACY(size_t GetImageDataIntoVector(void *destination,size_t maxSize));
91
92    void SetImageData(uint8_t *data, size_t expectedSize);
93
94    // User data
95    void SetUserData(uint8_t *data, size_t expectedSize);
96    uint8_t *GetUserData();
97    size_t GetUserDataSize();
98    // RBG data (from file)
99    uint8_t *GetRGBData();
100    size_t GetRGBDataSize();
101    // RAW data (from file)
102    uint8_t *GetRawData();
103    size_t GetRawDataSize();
104
105    // LUT
106    uint8_t* GetLutRGBA();
107    int GetLutItemNumber();
108    int GetLutItemSize();
109
110    // Write mode
111
112    /// \brief Tells the writer we want to keep 'Grey pixels + Palettes color'
113    ///        when possible (as opposed to convert 'Palettes color' to RGB)
114    void SetWriteModeToRaw()           { SetWriteMode(WMODE_RAW);  }
115    /// \brief Tells the writer we want to write RGB image when possible
116    ///        (as opposed to 'Grey pixels + Palettes color')
117    void SetWriteModeToRGB()           { SetWriteMode(WMODE_RGB);  }
118    /// \brief Sets the Write Mode ( )
119    void SetWriteMode(FileMode mode)   { 
120       WriteMode = mode;
121       // Deal with Samples per Pixel    
122       //if (mode == WMODE_RGB) FileInternal->InsertEntryString("3",0x0028,0x0002);
123    }
124    /// \brief Gets the Write Mode ( )
125    FileMode GetWriteMode()            { return WriteMode;         }
126
127    // Write format
128
129    /// \brief Tells the writer we want to write as Implicit VR
130    void SetWriteTypeToDcmImplVR()     { SetWriteType(ImplicitVR); }
131    /// \brief Tells the writer we want to write as Explicit VR
132    void SetWriteTypeToDcmExplVR()     { SetWriteType(ExplicitVR); }
133    /// \brief Tells the writer we want to write as ACR-NEMA
134    void SetWriteTypeToAcr()           { SetWriteType(ACR);        }
135    /// \brief Tells the writer we want to write as LibIDO
136    void SetWriteTypeToAcrLibido()     { SetWriteType(ACR_LIBIDO); }
137    /// \brief Tells the writer we want to write as JPEG   
138    void SetWriteTypeToJPEG()          { SetWriteType(JPEG);       }
139    /// \brief Tells the writer which format we want to write
140    /// (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
141    void SetWriteType(FileType format) { WriteType = format;       }
142    /// \brief Gets the format we talled the write we wanted to write
143    /// (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
144    FileType GetWriteType()            { return WriteType;         }
145
146    // Write pixels of ONE image on hard drive
147    // No test is made on processor "endianness"
148    // The user must call his reader correctly
149    bool WriteRawData  (std::string const &fileName);
150    bool WriteDcmImplVR(std::string const &fileName);
151    bool WriteDcmExplVR(std::string const &fileName);
152    bool WriteAcr      (std::string const &fileName);
153    bool Write         (std::string const &fileName);
154    
155 /// \brief We have to deal with 4 *very* different cases :
156 /// -1) user created ex nihilo his own image and wants to write it as a Dicom image.
157 ///    USER_OWN_IMAGE
158 /// -2) user modified the pixels of an existing image.
159 ///    FILTERED_IMAGE
160 /// -3) user created a new image, using existing images (eg MIP, MPR, cartography image)
161 ///   CREATED_IMAGE
162 /// -4) user modified/added some tags *without processing* the pixels (anonymization...
163 ///   UNMODIFIED_PIXELS_IMAGE
164
165    void SetContentType (ImageContentType c) { ContentType = c; }
166    // no GetContentType() method, on purpose!
167    
168    void CallStartMethod();
169    void CallProgressMethod();
170    void CallEndMethod();
171    
172 protected:
173    FileHelper( );
174    FileHelper( File *header );
175    ~FileHelper();
176
177    bool CheckWriteIntegrity();
178
179    void SetWriteToRaw();
180    void SetWriteToRGB();
181    void RestoreWrite();
182
183    void SetWriteFileTypeToACR();
184    void SetWriteFileTypeToJPEG();
185    void SetWriteFileTypeToExplicitVR();
186    void SetWriteFileTypeToImplicitVR();
187    void RestoreWriteFileType();
188
189    void SetWriteToLibido();
190    void SetWriteToNoLibido();
191    void RestoreWriteOfLibido();
192
193    DataEntry *CopyDataEntry(uint16_t group, uint16_t elem, 
194                                const TagName &vr = GDCM_VRUNKNOWN);
195    void CheckMandatoryElements();
196    void CheckMandatoryEntry(uint16_t group, uint16_t elem, std::string value);
197    void SetMandatoryEntry(uint16_t group, uint16_t elem, std::string value);
198    void CopyMandatoryEntry(uint16_t group, uint16_t elem, std::string value);
199    void RestoreWriteMandatory();
200
201 private:
202    void Initialize();
203
204    uint8_t *GetRaw();
205
206 // members variables:
207 protected:
208    /// value of the ??? for any progress bar
209    float Progress;
210    mutable bool Abort;
211    
212 private:
213
214    /// gdcm::File to use to load the file
215    File *FileInternal;
216
217    /// Whether already parsed or not
218    bool Parsed;
219
220    // Utility pixel converter
221    /// \brief Pointer to the PixelReadConverter
222    PixelReadConvert *PixelReadConverter;
223    /// \brief Pointer to the PixelWriteConverter
224    PixelWriteConvert *PixelWriteConverter;
225
226    // Utility header archive
227    /// \brief Pointer to the DocEntryArchive (used while writting process)
228    DocEntryArchive *Archive;
229
230    // Write variables
231    /// \brief (WMODE_RAW, WMODE_RGB)
232    FileMode WriteMode;
233
234    /// \brief (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
235    FileType WriteType;
236    
237    /// \brief Pointer to a user supplied function to allow modification 
238    /// of pixel order (e.g. : Mirror, UpsideDown, 90°Rotation, ...)
239    /// use as : void userSuppliedFunction(uint8_t *im, gdcm::File *f)
240    /// NB : the "uint8_t *" type of first param is just for prototyping.
241    /// User will Cast it according what he founds with f->GetPixelType()
242    /// See vtkgdcmSerieViewer for an example
243    VOID_FUNCTION_PUINT8_PFILE_POINTER UserFunction;
244    
245    /// \brief only user knows what he did before asking the image to be written
246    /// - he created ex nihilo his own image
247    /// - he just applied a mathematical process on the pixels
248    /// - he created a new image, using existing images (eg MIP, MPR,cartography)
249    /// - he anonymized and image (*no* modif on the pixels)
250    ImageContentType ContentType;
251
252 };
253 } // end namespace gdcm
254
255 //-----------------------------------------------------------------------------
256 #endif