]> Creatis software - gdcm.git/blob - src/gdcmFileHelper.h
BUG: Yet another shoot yourself in the foot
[gdcm.git] / src / gdcmFileHelper.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmFileHelper.h,v $
5   Language:  C++
6   Date:      $Date: 2005/11/22 20:24:46 $
7   Version:   $Revision: 1.32 $
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    virtual ~FileHelper();
61
62    void Print(std::ostream &os = std::cout, std::string const &indent = ""); 
63
64    /// Accessor to \ref File
65    File *GetFile() { return FileInternal; }
66    
67
68    void SetLoadMode(int loadMode);
69    void SetFileName(std::string const &fileName);
70    bool Load();
71    /// to allow user to modify pixel order (e.g. Mirror, UpsideDown,...)
72    void SetUserFunction( VOID_FUNCTION_PUINT8_PFILE_POINTER userFunc ) 
73                         { UserFunction = userFunc; }   
74    // File methods
75    bool SetEntryString(std::string const &content,
76                            uint16_t group, uint16_t elem);
77    bool SetEntryBinArea(uint8_t *content, int lgth,
78                             uint16_t group, uint16_t elem);
79
80    DataEntry *InsertEntryString(std::string const &content,
81                                        uint16_t group, uint16_t elem);
82    DataEntry *InsertEntryBinArea(uint8_t *binArea, int lgth,
83                                         uint16_t group, uint16_t elem);
84    SeqEntry *InsertSeqEntry(uint16_t group, uint16_t elem);
85
86    // File helpers
87    size_t GetImageDataSize();
88    size_t GetImageDataRawSize();
89
90    uint8_t *GetImageData();
91    uint8_t *GetImageDataRaw();
92
93    GDCM_LEGACY(size_t GetImageDataIntoVector(void *destination,size_t maxSize));
94
95    void SetImageData(uint8_t *data, size_t expectedSize);
96
97    // User data
98    void SetUserData(uint8_t *data, size_t expectedSize);
99    uint8_t *GetUserData();
100    size_t GetUserDataSize();
101    // RBG data (from file)
102    uint8_t *GetRGBData();
103    size_t GetRGBDataSize();
104    // RAW data (from file)
105    uint8_t *GetRawData();
106    size_t GetRawDataSize();
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 which format we want to write
139    /// (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
140    void SetWriteType(FileType format) { WriteType = format;       }
141    /// \brief Gets the format we talled the write we wanted to write
142    /// (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
143    FileType GetWriteType()            { return WriteType;         }
144
145    // Write pixels of ONE image on hard drive
146    // No test is made on processor "endianness"
147    // The user must call his reader correctly
148    bool WriteRawData  (std::string const &fileName);
149    bool WriteDcmImplVR(std::string const &fileName);
150    bool WriteDcmExplVR(std::string const &fileName);
151    bool WriteAcr      (std::string const &fileName);
152    bool Write         (std::string const &fileName);
153    /// \brief if user knows he didn't modify the pixels (e.g. he just anonymized 
154    ///        the file), he is allowed to ask to keep the original
155    ///        'Media Storage SOP Class UID' and 'Image Type'   
156    void SetKeepMediaStorageSOPClassUID (bool v) 
157                               { KeepMediaStorageSOPClassUID = v; }
158    // no GetKeepMediaStorageSOPClassUID() method, on purpose!
159    
160 protected:
161    FileHelper( );
162    FileHelper( File *header );
163    ~FileHelper();
164
165    bool CheckWriteIntegrity();
166
167    void SetWriteToRaw();
168    void SetWriteToRGB();
169    void RestoreWrite();
170
171    void SetWriteFileTypeToACR();
172    void SetWriteFileTypeToJPEG();
173    void SetWriteFileTypeToExplicitVR();
174    void SetWriteFileTypeToImplicitVR();
175    void RestoreWriteFileType();
176
177    void SetWriteToLibido();
178    void SetWriteToNoLibido();
179    void RestoreWriteOfLibido();
180
181    DataEntry *CopyDataEntry(uint16_t group, uint16_t elem, 
182                                const TagName &vr = GDCM_VRUNKNOWN);
183    void CheckMandatoryElements();
184    void CheckMandatoryEntry(uint16_t group, uint16_t elem, std::string value);
185    void SetMandatoryEntry(uint16_t group, uint16_t elem, std::string value);
186    void CopyMandatoryEntry(uint16_t group, uint16_t elem, std::string value);
187    void RestoreWriteMandatory();
188
189 private:
190    void Initialize();
191
192    uint8_t *GetRaw();
193
194 // members variables:
195    /// gdcm::File to use to load the file
196    File *FileInternal;
197
198    /// Whether already parsed or not
199    bool Parsed;
200
201    // Utility pixel converter
202    /// \brief Pointer to the PixelReadConverter
203    PixelReadConvert *PixelReadConverter;
204    /// \brief Pointer to the PixelWriteConverter
205    PixelWriteConvert *PixelWriteConverter;
206
207    // Utility header archive
208    /// \brief Pointer to the DocEntryArchive (used while writting process)
209    DocEntryArchive *Archive;
210
211    // Write variables
212    /// \brief (WMODE_RAW, WMODE_RGB)
213    FileMode WriteMode;
214    
215    /// \brief (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
216    FileType WriteType;
217    
218    /// \brief Pointer to a user supplied function to allow modification 
219    /// of pixel order (e.g. : Mirror, UpsideDown, 90°Rotation, ...)
220    /// use as : void userSuppliedFunction(uint8_t *im, gdcm::File *f)
221    /// NB : the "uint8_t *" type of first param is just for prototyping.
222    /// User will Cast it according what he founds with f->GetPixelType()
223    /// See vtkgdcmSerieViewer for an example
224    VOID_FUNCTION_PUINT8_PFILE_POINTER UserFunction;
225    
226    /// \brief if user knows he didn't modify the pixels (e.g. he just 
227    /// anonymized the file), he is allowed to ask to keep the original
228    /// 'Media Storage SOP Class UID' and 'Image Type'  
229    bool KeepMediaStorageSOPClassUID;
230 };
231 } // end namespace gdcm
232
233 //-----------------------------------------------------------------------------
234 #endif