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