]> Creatis software - gdcm.git/blob - src/gdcmFileHelper.h
* Minor coding-style clean up
[gdcm.git] / src / gdcmFileHelper.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmFileHelper.h,v $
5   Language:  C++
6   Date:      $Date: 2005/10/18 08:35:50 $
7   Version:   $Revision: 1.23 $
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 "gdcmBase.h"
24 //#include <iostream>
25
26
27
28 namespace gdcm 
29 {
30 class File;
31 class DataEntry;
32 class SeqEntry;
33 class PixelReadConvert;
34 class PixelWriteConvert;
35 class DocEntryArchive;
36
37 typedef void (*VOID_FUNCTION_PUINT8_PFILE_POINTER)(uint8_t *, File *);
38
39 //-----------------------------------------------------------------------------
40 /**
41  * \brief In addition to Dicom header exploration, this class is designed
42  * for accessing the image/volume content. One can also use it to
43  * write Dicom/ACR-NEMA/RAW files.
44  */
45 class GDCM_EXPORT FileHelper : public Base
46 {
47 public:
48    enum FileMode
49    {
50       WMODE_RAW,
51       WMODE_RGB
52    };
53      
54 public:
55    FileHelper( );
56    FileHelper( File *header );
57    GDCM_LEGACY(FileHelper( std::string const &filename ));
58    
59    virtual ~FileHelper();
60
61    void Print(std::ostream &os = std::cout, std::string const &indent = ""); 
62
63    /// Accessor to \ref File
64    File *GetFile() { return FileInternal; }
65    
66
67    void SetLoadMode(int loadMode);
68    void SetFileName(std::string const &fileName);
69    bool Load();
70    /// to allow user to modify pixel order (e.g. Mirror, TopDown,...)
71    void SetUserFunction( VOID_FUNCTION_PUINT8_PFILE_POINTER userFunc ) 
72                         { UserFunction = userFunc; }   
73    // File methods
74    bool SetEntryString(std::string const &content,
75                            uint16_t group, uint16_t elem);
76    bool SetEntryBinArea(uint8_t *content, int lgth,
77                             uint16_t group, uint16_t elem);
78
79    DataEntry *InsertEntryString(std::string const &content,
80                                        uint16_t group, uint16_t elem);
81    DataEntry *InsertEntryBinArea(uint8_t *binArea, int lgth,
82                                         uint16_t group, uint16_t elem);
83    SeqEntry *InsertSeqEntry(uint16_t group, uint16_t elem);
84
85    // File helpers
86    size_t GetImageDataSize();
87    size_t GetImageDataRawSize();
88
89    uint8_t *GetImageData();
90    uint8_t *GetImageDataRaw();
91
92    GDCM_LEGACY(size_t GetImageDataIntoVector(void *destination,size_t maxSize));
93
94    void SetImageData(uint8_t *data, size_t expectedSize);
95
96    // User data
97    void SetUserData(uint8_t *data, size_t expectedSize);
98    uint8_t *GetUserData();
99    size_t GetUserDataSize();
100    // RBG data (from file)
101    uint8_t *GetRGBData();
102    size_t GetRGBDataSize();
103    // RAW data (from file)
104    uint8_t *GetRawData();
105    size_t GetRawDataSize();
106
107    // LUT
108    uint8_t* GetLutRGBA();
109    int GetLutItemNumber();
110    int GetLutItemSize();
111
112    // Write mode
113
114    /// \brief Tells the writer we want to keep 'Grey pixels + Palettes color'
115    ///        when possible (as opposed to convert 'Palettes color' to RGB)
116    void SetWriteModeToRaw()           { SetWriteMode(WMODE_RAW);  }
117    /// \brief Tells the writer we want to write RGB image when possible
118    ///        (as opposed to 'Grey pixels + Palettes color')
119    void SetWriteModeToRGB()           { SetWriteMode(WMODE_RGB);  }
120    /// \brief Sets the Write Mode ( )
121    void SetWriteMode(FileMode mode)   { WriteMode = mode;         }
122    /// \brief Gets the Write Mode ( )
123    FileMode GetWriteMode()            { return WriteMode;         }
124
125    // Write format
126
127    /// \brief Tells the writer we want to write as Implicit VR
128    void SetWriteTypeToDcmImplVR()     { SetWriteType(ImplicitVR); }
129    /// \brief Tells the writer we want to write as Explicit VR
130    void SetWriteTypeToDcmExplVR()     { SetWriteType(ExplicitVR); }
131    /// \brief Tells the writer we want to write as ACR-NEMA
132    void SetWriteTypeToAcr()           { SetWriteType(ACR);        }
133    /// \brief Tells the writer we want to write as LibIDO
134    void SetWriteTypeToAcrLibido()     { SetWriteType(ACR_LIBIDO); }
135    /// \brief Tells the writer which format we want to write
136    /// (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
137    void SetWriteType(FileType format) { WriteType = format;       }
138    /// \brief Gets the format we talled the write we wanted to write
139    ///   (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
140    FileType GetWriteType()            { return WriteType;         }
141
142    // Write pixels of ONE image on hard drive
143    // No test is made on processor "endianness"
144    // The user must call his reader correctly
145    bool WriteRawData  (std::string const &fileName);
146    bool WriteDcmImplVR(std::string const &fileName);
147    bool WriteDcmExplVR(std::string const &fileName);
148    bool WriteAcr      (std::string const &fileName);
149    bool Write         (std::string const &fileName);
150
151 protected:
152    bool CheckWriteIntegrity();
153
154    void SetWriteToRaw();
155    void SetWriteToRGB();
156    void RestoreWrite();
157
158    void SetWriteFileTypeToACR();
159    void SetWriteFileTypeToExplicitVR();
160    void SetWriteFileTypeToImplicitVR();
161    void RestoreWriteFileType();
162
163    void SetWriteToLibido();
164    void SetWriteToNoLibido();
165    void RestoreWriteOfLibido();
166
167    DataEntry *CopyDataEntry(uint16_t group, uint16_t elem, 
168                                const TagName &vr = GDCM_UNKNOWN);
169    void CheckMandatoryElements();
170    void CheckMandatoryEntry(uint16_t group,uint16_t elem,std::string value);
171    void SetMandatoryEntry(uint16_t group,uint16_t elem,std::string value);
172    void CopyMandatoryEntry(uint16_t group,uint16_t elem,std::string value);
173    void RestoreWriteMandatory();
174
175 private:
176    void Initialize();
177
178    uint8_t *GetRaw();
179
180 // members variables:
181    /// gdcm::File to use to load the file
182    File *FileInternal;
183
184    /// \brief Whether the underlying \ref gdcm::File was loaded by
185    ///  the constructor or passed to the constructor. 
186    ///  When false the destructor is in charge of deletion.
187    bool SelfHeader;
188    
189    /// Whether already parsed or not
190    bool Parsed;
191
192    // Utility pixel converter
193    /// \brief Pointer to the PixelReadConverter
194    PixelReadConvert *PixelReadConverter;
195    /// \brief Pointer to the PixelWriteConverter
196    PixelWriteConvert *PixelWriteConverter;
197
198    // Utility header archive
199    /// \brief Pointer to the DocEntryArchive (used while writting process)
200    DocEntryArchive *Archive;
201
202    // Write variables
203    /// \brief (WMODE_RAW, WMODE_RGB)
204    FileMode WriteMode;
205    /// \brief (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
206    FileType WriteType;
207    /// Pointer to a user supplied function to allow modification of pixel order
208    /// (i.e. : Mirror, TopDown, 90°Rotation, ...)
209    /// use as : void userSuppliedFunction(uint8_t *im, gdcm::File *f)
210    /// NB : the "uint8_t *" type of first param is just for prototyping.
211    /// User will Cast it according what he founds with f->GetPixelType()
212    /// See vtkgdcmSerieViewer for an example
213    VOID_FUNCTION_PUINT8_PFILE_POINTER UserFunction;
214 };
215 } // end namespace gdcm
216
217 //-----------------------------------------------------------------------------
218 #endif