]> Creatis software - gdcm.git/blob - src/gdcmFileHelper.h
ee5e9d776ac572051ea069079970f0943885a398
[gdcm.git] / src / gdcmFileHelper.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmFileHelper.h,v $
5   Language:  C++
6   Date:      $Date: 2005/09/02 07:10:03 $
7   Version:   $Revision: 1.22 $
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 ValEntry;
32 class BinEntry;
33 class SeqEntry;
34 class PixelReadConvert;
35 class PixelWriteConvert;
36 class DocEntryArchive;
37
38 typedef void (*VOID_FUNCTION_PUINT8_PFILE_POINTER)(uint8_t *, File *);
39
40 //-----------------------------------------------------------------------------
41 /**
42  * \brief In addition to Dicom header exploration, this class is designed
43  * for accessing the image/volume content. One can also use it to
44  * write Dicom/ACR-NEMA/RAW files.
45  */
46 class GDCM_EXPORT FileHelper : public Base
47 {
48 public:
49    enum FileMode
50    {
51       WMODE_RAW,
52       WMODE_RGB
53    };
54      
55 public:
56    FileHelper( );
57    FileHelper( File *header );
58    GDCM_LEGACY(FileHelper( std::string const &filename ));
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, TopDown,...)
72    void SetUserFunction( VOID_FUNCTION_PUINT8_PFILE_POINTER userFunc ) 
73                         { UserFunction = userFunc; }   
74    // File methods
75    bool SetValEntry(std::string const &content,
76                     uint16_t group, uint16_t elem);
77    bool SetBinEntry(uint8_t *content, int lgth,
78                     uint16_t group, uint16_t elem);
79
80    ValEntry *InsertValEntry(std::string const &content,
81                             uint16_t group, uint16_t elem);
82    BinEntry *InsertBinEntry(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 which format we want to write
137    /// (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
138    void SetWriteType(FileType format) { WriteType = format;       }
139    /// \brief Gets the format we talled the write we wanted to write
140    ///   (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
141    FileType GetWriteType()            { return WriteType;         }
142
143    // Write pixels of ONE image on hard drive
144    // No test is made on processor "endianness"
145    // The user must call his reader correctly
146    bool WriteRawData  (std::string const &fileName);
147    bool WriteDcmImplVR(std::string const &fileName);
148    bool WriteDcmExplVR(std::string const &fileName);
149    bool WriteAcr      (std::string const &fileName);
150    bool Write         (std::string const &fileName);
151
152 protected:
153    bool CheckWriteIntegrity();
154
155    void SetWriteToRaw();
156    void SetWriteToRGB();
157    void RestoreWrite();
158
159    void SetWriteFileTypeToACR();
160    void SetWriteFileTypeToExplicitVR();
161    void SetWriteFileTypeToImplicitVR();
162    void RestoreWriteFileType();
163
164    void SetWriteToLibido();
165    void SetWriteToNoLibido();
166    void RestoreWriteOfLibido();
167
168    ValEntry *CopyValEntry(uint16_t group, uint16_t elem);
169    BinEntry *CopyBinEntry(uint16_t group, uint16_t elem, 
170                           const std::string &vr);
171    void CheckMandatoryElements();
172    void RestoreWriteMandatory();
173
174 private:
175    void Initialize();
176
177    uint8_t *GetRaw();
178
179 // members variables:
180    /// gdcm::File to use to load the file
181    File *FileInternal;
182
183    /// \brief Whether the underlying \ref gdcm::File was loaded by
184    ///  the constructor or passed to the constructor. 
185    ///  When false the destructor is in charge of deletion.
186    bool SelfHeader;
187    
188    /// Whether already parsed or not
189    bool Parsed;
190
191    // Utility pixel converter
192    /// \brief Pointer to the PixelReadConverter
193    PixelReadConvert *PixelReadConverter;
194    /// \brief Pointer to the PixelWriteConverter
195    PixelWriteConvert *PixelWriteConverter;
196
197    // Utility header archive
198    /// \brief Pointer to the DocEntryArchive (used while writting process)
199    DocEntryArchive *Archive;
200
201    // Write variables
202    /// \brief (WMODE_RAW, WMODE_RGB)
203    FileMode WriteMode;
204    /// \brief (ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO)
205    FileType WriteType;
206    /// Pointer to a user supplied function to allow modification of pixel order
207    /// (i.e. : Mirror, TopDown, 90°Rotation, ...)
208    /// use as : void userSuppliedFunction(uint8_t *im, gdcm::File *f)
209    /// NB : the "uint8_t *" type of first param is just for prototyping.
210    /// User will Cast it according what he founds with f->GetPixelType()
211    /// See vtkgdcmSerieViewer for an example
212    VOID_FUNCTION_PUINT8_PFILE_POINTER UserFunction;
213 };
214 } // end namespace gdcm
215
216 //-----------------------------------------------------------------------------
217 #endif