]> Creatis software - gdcm.git/blob - src/gdcmFileHelper.h
Doxygenation
[gdcm.git] / src / gdcmFileHelper.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmFileHelper.h,v $
5   Language:  C++
6   Date:      $Date: 2005/11/21 09:46:26 $
7   Version:   $Revision: 1.31 $
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
164    bool CheckWriteIntegrity();
165
166    void SetWriteToRaw();
167    void SetWriteToRGB();
168    void RestoreWrite();
169
170    void SetWriteFileTypeToACR();
171    void SetWriteFileTypeToJPEG();
172    void SetWriteFileTypeToExplicitVR();
173    void SetWriteFileTypeToImplicitVR();
174    void RestoreWriteFileType();
175
176    void SetWriteToLibido();
177    void SetWriteToNoLibido();
178    void RestoreWriteOfLibido();
179
180    DataEntry *CopyDataEntry(uint16_t group, uint16_t elem, 
181                                const TagName &vr = GDCM_VRUNKNOWN);
182    void CheckMandatoryElements();
183    void CheckMandatoryEntry(uint16_t group, uint16_t elem, std::string value);
184    void SetMandatoryEntry(uint16_t group, uint16_t elem, std::string value);
185    void CopyMandatoryEntry(uint16_t group, uint16_t elem, std::string value);
186    void RestoreWriteMandatory();
187
188 private:
189    void Initialize();
190
191    uint8_t *GetRaw();
192
193 // members variables:
194    /// gdcm::File to use to load the file
195    File *FileInternal;
196
197    /// Whether already parsed or not
198    bool Parsed;
199
200    // Utility pixel converter
201    /// \brief Pointer to the PixelReadConverter
202    PixelReadConvert *PixelReadConverter;
203    /// \brief Pointer to the PixelWriteConverter
204    PixelWriteConvert *PixelWriteConverter;
205
206    // Utility header archive
207    /// \brief Pointer to the DocEntryArchive (used while writting process)
208    DocEntryArchive *Archive;
209
210    // Write variables
211    /// \brief (WMODE_RAW, WMODE_RGB)
212    FileMode WriteMode;
213    
214    /// \brief (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
215    FileType WriteType;
216    
217    /// \brief Pointer to a user supplied function to allow modification 
218    /// of pixel order (e.g. : Mirror, UpsideDown, 90°Rotation, ...)
219    /// use as : void userSuppliedFunction(uint8_t *im, gdcm::File *f)
220    /// NB : the "uint8_t *" type of first param is just for prototyping.
221    /// User will Cast it according what he founds with f->GetPixelType()
222    /// See vtkgdcmSerieViewer for an example
223    VOID_FUNCTION_PUINT8_PFILE_POINTER UserFunction;
224    
225    /// \brief if user knows he didn't modify the pixels (e.g. he just 
226    /// anonymized the file), he is allowed to ask to keep the original
227    /// 'Media Storage SOP Class UID' and 'Image Type'  
228    bool KeepMediaStorageSOPClassUID;
229 };
230 } // end namespace gdcm
231
232 //-----------------------------------------------------------------------------
233 #endif