1 /*=========================================================================
4 Module: $RCSfile: exReadWriteFile.cxx,v $
6 Date: $Date: 2005/02/03 15:44:20 $
7 Version: $Revision: 1.1 $
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.
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.
17 =========================================================================*/
20 #include "gdcmFileHelper.h"
21 #include "gdcmDocument.h"
22 #include "gdcmValEntry.h"
23 #include "gdcmBinEntry.h"
24 #include "gdcmSeqEntry.h"
26 // ----- WARNING ----- WARNING ----- WARNING ----- WARNING ----- WARNING ---
28 // This program is NOT intendend to be run as is.
30 // It just shows a set of possible uses.
32 // comment out the useless parts
33 // invoke it with an ad hoc image(*)
34 // check the resulting image
36 // (*) For samples, user can refer to gdcmData
37 // and read README.txt file
39 // ----- WARNING ----- WARNING ----- WARNING ----- WARNING ----- WARNING ---
42 int main(int argc, char *argv[])
46 std::cerr << "Usage :" << std::endl <<
47 argv[0] << " input_dicom output_dicom" << std::endl;
51 std::string filename = argv[1];
52 std::string output = argv[2];
54 // First, let's create a gdcm::File
55 // that will contain all the Dicom Field but the Pixels Element
57 gdcm::File *e1= new gdcm::File( filename );
60 // Ask content to be printed
61 std::cout << std::endl
62 << "--- Standard Print -------------------------------------------"
64 e1->SetPrintLevel(2); // to have a nice output
65 //e1->SetPrintLevel(1);
66 e1->Print(); // user may comment out if too much verbose
69 // Field by field Printing
71 std::cout << std::endl
72 << "--- Display only human readable values -----------------------"
75 gdcm::ValEntry *valEntry;
80 std::string vr; // value representation
81 std::string vm; // value multiplicity
82 std::string name; // held in the Dicom Dictionary
85 gdcm::DocEntry *d = e1->GetFirstEntry();
88 // We skip SeqEntries, since user cannot do much with them
89 if ( !(dynamic_cast<gdcm::SeqEntry*>(d))
90 // We skip Shadow Groups, since nobody knows what they mean
91 && !( d->GetGroup()%2 )
92 // we skip BinEntries, since their content is not human-readable
93 && !dynamic_cast<gdcm::BinEntry*>(d) )
95 // If user just 'wants to see'
97 //std::cout << std::endl;
99 // If user wants to get info about the entry
100 // (he is sure, here that DocEntry is a ValEntry)
101 valEntry = dynamic_cast<gdcm::ValEntry*>(d);
102 // Let's be carefull -maybe he commented out some previous line-
106 value = valEntry->GetValue();
107 group = valEntry->GetGroup();
108 elem = valEntry->GetElement();
109 vr = valEntry->GetVR();
110 // user wants really to know everything about entry!
111 vm = valEntry->GetVM();
112 name = valEntry->GetName();
113 offset = valEntry->GetOffset();
115 std::cout //<< std::hex << group << "," << elem
116 << valEntry->GetKey()
117 << " VR :[" << vr << "] VM : [" << vm
118 << "] name : [" << name << "]"
119 << " value : [" << value << "]"
123 d = e1->GetNextEntry();
126 std::cout << std::endl
127 << "--- Use pre-defined acessors ---------------------------------"
130 // ------ some pre-defined acessors may supply usefull informations ----
133 int linesNumber = e1->GetYSize();
134 int rawsNumber = e1->GetXSize();
135 int framesNumber = e1->GetYSize();// defaulted to 1 if not found
137 std::cout << "lines : " << linesNumber << " columns : " << rawsNumber
138 << " frames : " << framesNumber << std::endl;
141 int pixelSize = e1->GetPixelSize();
142 std::string pixelType = e1->GetPixelType();
143 bool isSigned = e1->IsSignedPixelData();
145 std::cout << "pixelSize : " << pixelSize << " pixelType : " << pixelType
146 << " signed : " << isSigned << std::endl;
148 // about pixels, too.
149 // Better you forget these ones
151 std::cout << "GetBitsStored()" << e1->GetBitsStored() << std::endl;
152 std::cout << "GetBitsAllocated()" << e1->GetBitsAllocated() << std::endl;
153 std::cout << "GetHighBitPosition()" << e1->GetHighBitPosition() << std::endl;
155 std::cout << "GetSamplesPerPixel()"
156 << e1->GetSamplesPerPixel() << std::endl;
157 std::cout << "GetPlanarConfiguration()"
158 << e1->GetPlanarConfiguration() << std::endl;
160 // about 'image geography'
162 float xs = e1->GetXSpacing();
163 float ys = e1->GetYSpacing();
164 float zs = e1->GetZSpacing(); // defaulted to 1.0 if not found
166 float xo = e1->GetXOrigin();
167 float yo = e1->GetYOrigin();
168 float zo = e1->GetZOrigin();
170 std::cout << "GetXSpacing()" << xs << std::endl;
171 std::cout << "GetYSpacing()" << ys << std::endl;
172 std::cout << "GetXSpacing()" << zs << std::endl;
174 std::cout << "GetXOrigin()" << xo << std::endl;
175 std::cout << "GetYOrigin()" << yo << std::endl;
176 std::cout << "GetZOrigin()" << zo << std::endl;
178 // about its way to store colors (if user is aware)
180 // checks Photometric Interpretation
181 std::cout << "IsMonochrome()" << e1->IsMonochrome() << std::endl;
182 std::cout << "IsYBRFull()" << e1->IsYBRFull() << std::endl;
183 std::cout << "IsPaletteColor()" << e1->IsPaletteColor() << std::endl;
184 // checks if LUT are found
185 std::cout << "HasLUT()" << e1->HasLUT() << std::endl;
187 std::cout << "GetNumberOfScalarComponents()"
188 << e1->GetNumberOfScalarComponents()<< std::endl;
189 std::cout << "GetNumberOfScalarComponentsRaw()"
190 << e1->GetNumberOfScalarComponentsRaw()<< std::endl;
193 std::cout << std::endl
194 << "--- Get values on request ------------------------------------"
196 // ------ User is aware, and wants to get fields with no accesor --------
198 std::cout << "Manufacturer :[" << e1->GetEntryValue(0x0008,0x0070)
200 std::cout << "Institution :[" << e1->GetEntryValue(0x0008,0x0080)
202 std::cout << "Patient's name :[" << e1->GetEntryValue(0x0010,0x0010)
204 std::cout << "Physician's name :[" << e1->GetEntryValue(0x0008,0x0090)
206 std::cout << "Study Date :[" << e1->GetEntryValue(0x0008,0x0020)
208 std::cout << "Study inst UID :[" << e1->GetEntryValue(0x0020,0x000d)
210 std::cout << "Serie inst UID :[" << e1->GetEntryValue(0x0020,0x000e)
212 std::cout << "Frame ref UID :[" << e1->GetEntryValue(0x0020,0x0052)
215 // ------ User wants to load the pixels---------------------------------
217 // Hope now he knows enought about the image ;-)
219 // First, create a gdcm::FileHelper
220 gdcm::FileHelper *fh1 = new gdcm::FileHelper(e1);
222 // Load the pixels, transforms LUT (if any) into RGB Pixels
223 uint8_t *imageData = fh1->GetImageData();
224 // Get the image data size
225 size_t dataSize = fh1->GetImageDataSize();
227 // Probabely, a straigh user won't load both ...
229 // Load the pixels, DO NOT transform LUT (if any) into RGB Pixels
230 uint8_t *imageDataRaw = fh1->GetImageDataRaw();
231 // Get the image data size
232 size_t dataRawSize = fh1->GetImageDataRawSize();
234 // TODO : Newbee user would appreciate any comment !
236 std::cout << "GetImageDataSize()"
237 << fh1->GetImageDataSize() << std::endl;
238 std::cout << "GetImageDataRawSize()"
239 << fh1->GetImageDataRawSize() << std::endl;
241 std::cout << "GetRGBDataSize()"
242 << fh1->GetRGBDataSize() << std::endl;
243 std::cout << "GetRawDataSize()"
244 << fh1->GetRawDataSize() << std::endl;
245 std::cout << "GetUserDataSize()"
246 << fh1->GetUserDataSize() << std::endl;
249 std::cout << std::endl
250 << "--- write a new image(1) -------------------------------------"
253 // ------ User wants write a new image without shadow groups -------------
255 gdcm::FileHelper *copy = new gdcm::FileHelper( output );
257 d = e1->GetFirstEntry();
260 // We skip SeqEntries, since user cannot do much with them
261 if ( !(dynamic_cast<gdcm::SeqEntry*>(d))
262 // We skip Shadow Groups, since nobody knows what they mean
263 && !( d->GetGroup()%2 ) )
266 if ( gdcm::BinEntry *b = dynamic_cast<gdcm::BinEntry*>(d) )
268 copy->GetFile()->InsertBinEntry( b->GetBinArea(),b->GetLength(),
269 b->GetGroup(),b->GetElement(),
272 else if ( gdcm::ValEntry *v = dynamic_cast<gdcm::ValEntry*>(d) )
274 copy->GetFile()->InsertValEntry( v->GetValue(),
275 v->GetGroup(),v->GetElement(),
280 // We skip gdcm::SeqEntries
283 d = e1->GetNextEntry();
286 std::cout << std::endl
287 << "--- write a new image(2) -------------------------------------"
290 // User knows the image is a 'color' one -RGB, YBR, Palette Color-
291 // and wants to write it as RGB
292 copy->SetImageData(imageData, dataSize);
293 copy->SetWriteModeToRGB();
294 copy->WriteDcmExplVR( output );
296 // User wants to see if there is any difference before and after writting
298 std::cout << "GetImageDataSize()"
299 << fh1->GetImageDataSize() << std::endl;
300 std::cout << "GetImageDataRawSize()"
301 << fh1->GetImageDataRawSize() << std::endl;
303 std::cout << "GetRGBDataSize()"
304 << fh1->GetRGBDataSize() << std::endl;
305 std::cout << "GetRawDataSize()"
306 << fh1->GetRawDataSize() << std::endl;
307 std::cout << "GetUserDataSize()"
308 << fh1->GetUserDataSize() << std::endl;
309 // User wants to keep the Palette Color -if any-
310 // and write the image as it was
311 copy->SetImageData(imageDataRaw, dataRawSize);
312 copy->SetWriteModeToRGB();
313 copy->WriteDcmExplVR( output );
316 std::cout << std::endl
317 << "------------------------------------------------------------"
319 // User is in a fancy mood and wants to forge a bonm image
320 // just to see how other Dicom viewers act
326 std::cout << std::endl
327 << "------------------------------------------------------------"