1 /*=========================================================================
4 Module: $RCSfile: exReadWriteFile.cxx,v $
6 Date: $Date: 2005/07/06 15:49:31 $
7 Version: $Revision: 1.5 $
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 #include <stdlib.h> // for exit
28 int main(int argc, char *argv[])
32 std::cout << " --- WARNING --- WARNING --- WARNING --- WARNING ---" <<std::endl;
33 std::cout << " " <<std::endl;
34 std::cout << " This source program is NOT intendend to be run as is"<<std::endl;
35 std::cout << " " <<std::endl;
36 std::cout << " It just shows a set of possible uses." <<std::endl;
37 std::cout << "User MUST read it, " <<std::endl;
38 std::cout << " comment out the useless parts " <<std::endl;
39 std::cout << " invoke it with an ad hoc image(*) " <<std::endl;
40 std::cout << " check the resulting image " <<std::endl;
41 std::cout << " " <<std::endl;
42 std::cout << " (*) For samples, user can refer to gdcmData" <<std::endl;
43 std::cout << " and read README.txt file " <<std::endl;
44 std::cout << " " <<std::endl;
45 std::cout << "This source program will be splitted into smaller elementary"
46 << " programs" <<std::endl;
47 std::cout << " " <<std::endl;
48 std::cout << " --- WARNING --- WARNING --- WARNING --- WARNING ---" <<std::endl;
52 std::cerr << "Usage :" << std::endl <<
53 argv[0] << " input_dicom output_dicom" << std::endl;
57 std::string filename = argv[1];
58 std::string output = argv[2];
60 // First, let's create a gdcm::File
61 // that will contain all the Dicom fields but the Pixels Element
63 gdcm::File *f1= new gdcm::File( filename );
66 // Ask content to be printed
67 std::cout << std::endl
68 << "--- Standard Print -------------------------------------------"
70 f1->SetPrintLevel(2); // to have a nice output
71 //f1->SetPrintLevel(1);
72 f1->Print(); // user may comment out if too much verbose
75 // User asks for field by field Printing
77 std::cout << std::endl
78 << "--- Display only human readable values -----------------------"
81 gdcm::ValEntry *valEntry;
83 std::string vr; // value representation
84 std::string vm; // value multiplicity
85 std::string name; // held in the Dicom Dictionary
88 gdcm::DocEntry *d = f1->GetFirstEntry();
91 // We skip SeqEntries, since user cannot do much with them
92 if ( !(dynamic_cast<gdcm::SeqEntry*>(d))
93 // We skip Shadow Groups, since nobody knows what they mean
94 && !( d->GetGroup()%2 )
95 // we skip BinEntries, since their content is not human-readable
96 && !dynamic_cast<gdcm::BinEntry*>(d) )
98 // If user just 'wants to see'
100 //std::cout << std::endl;
102 // If user wants to get info about the entry
103 // (he is sure, here that DocEntry is a ValEntry)
104 valEntry = dynamic_cast<gdcm::ValEntry*>(d);
105 // Let's be carefull -maybe he commented out some previous line-
109 value = valEntry->GetValue();
110 vr = valEntry->GetVR();
111 // user wants really to know everything about entry!
112 vm = valEntry->GetVM();
113 name = valEntry->GetName();
115 std::cout //<< std::hex << group << "," << elem
116 << valEntry->GetKey()
117 << " VR :[" << vr << "] VM : [" << vm
118 << "] name : [" << name << "]"
119 << " value : [" << value << "]"
123 d = f1->GetNextEntry();
126 std::cout << std::endl
127 << "--- Use pre-defined acessors ---------------------------------"
130 // ------ some pre-defined acessors may supply usefull informations ----
133 int linesNumber = f1->GetYSize();
134 int rawsNumber = f1->GetXSize();
135 int framesNumber = f1->GetYSize();// defaulted to 1 if not found
137 std::cout << "lines : " << linesNumber << " columns : " << rawsNumber
138 << " frames : " << framesNumber << std::endl;
141 int pixelSize = f1->GetPixelSize();
142 std::string pixelType = f1->GetPixelType();
143 bool isSigned = f1->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()" << f1->GetBitsStored() << std::endl;
152 std::cout << "GetBitsAllocated()" << f1->GetBitsAllocated() << std::endl;
153 std::cout << "GetHighBitPosition()" << f1->GetHighBitPosition() << std::endl;
155 std::cout << "GetSamplesPerPixel()"
156 << f1->GetSamplesPerPixel() << std::endl;
157 std::cout << "GetPlanarConfiguration()"
158 << f1->GetPlanarConfiguration() << std::endl;
160 // about 'image geography'
162 float xs = f1->GetXSpacing();
163 float ys = f1->GetYSpacing();
164 float zs = f1->GetZSpacing(); // defaulted to 1.0 if not found
166 float xo = f1->GetXOrigin();
167 float yo = f1->GetYOrigin();
168 float zo = f1->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()" << f1->IsMonochrome() << std::endl;
182 std::cout << "IsYBRFull()" << f1->IsYBRFull() << std::endl;
183 std::cout << "IsPaletteColor()" << f1->IsPaletteColor() << std::endl;
184 // checks if LUT are found
185 std::cout << "HasLUT()" << f1->HasLUT() << std::endl;
187 std::cout << "GetNumberOfScalarComponents()"
188 << f1->GetNumberOfScalarComponents()<< std::endl;
189 std::cout << "GetNumberOfScalarComponentsRaw()"
190 << f1->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 :[" << f1->GetEntryValue(0x0008,0x0070)
200 std::cout << "Institution :[" << f1->GetEntryValue(0x0008,0x0080)
202 std::cout << "Patient's name :[" << f1->GetEntryValue(0x0010,0x0010)
204 std::cout << "Physician's name :[" << f1->GetEntryValue(0x0008,0x0090)
206 std::cout << "Study Date :[" << f1->GetEntryValue(0x0008,0x0020)
208 std::cout << "Study inst UID :[" << f1->GetEntryValue(0x0020,0x000d)
210 std::cout << "Serie inst UID :[" << f1->GetEntryValue(0x0020,0x000e)
212 std::cout << "Frame ref UID :[" << f1->GetEntryValue(0x0020,0x0052)
215 // User wants to get info about the 'real world' vs image
218 // ------ User wants to load the pixels---------------------------------
220 // Hope now he knows enought about the image ;-)
222 // First, create a gdcm::FileHelper
223 gdcm::FileHelper *fh1 = new gdcm::FileHelper(f1);
225 // Load the pixels, transforms LUT (if any) into RGB Pixels
226 uint8_t *imageData = fh1->GetImageData();
227 // Get the image data size
228 size_t dataSize = fh1->GetImageDataSize();
230 // Probabely, a straigh user won't load both ...
232 // Load the pixels, DO NOT transform LUT (if any) into RGB Pixels
233 uint8_t *imageDataRaw = fh1->GetImageDataRaw();
234 // Get the image data size
235 size_t dataRawSize = fh1->GetImageDataRawSize();
237 // TODO : Newbee user would appreciate any comment !
239 std::cout << "GetImageDataSize()"
240 << fh1->GetImageDataSize() << std::endl;
241 std::cout << "GetImageDataRawSize()"
242 << fh1->GetImageDataRawSize() << std::endl;
244 std::cout << "GetRGBDataSize()"
245 << fh1->GetRGBDataSize() << std::endl;
246 std::cout << "GetRawDataSize()"
247 << fh1->GetRawDataSize() << std::endl;
248 std::cout << "GetUserDataSize()"
249 << fh1->GetUserDataSize() << std::endl;
252 std::cout << std::endl
253 << "--- write a new image(1) -------------------------------------"
256 // ------ User wants write a new image without shadow groups -------------
258 gdcm::FileHelper *copy = new gdcm::FileHelper( output );
260 d = f1->GetFirstEntry();
263 // We skip SeqEntries, since user cannot do much with them
264 if ( !(dynamic_cast<gdcm::SeqEntry*>(d))
265 // We skip Shadow Groups, since nobody knows what they mean
266 && !( d->GetGroup()%2 ) )
269 if ( gdcm::BinEntry *b = dynamic_cast<gdcm::BinEntry*>(d) )
271 copy->GetFile()->InsertBinEntry( b->GetBinArea(),b->GetLength(),
272 b->GetGroup(),b->GetElement(),
275 else if ( gdcm::ValEntry *v = dynamic_cast<gdcm::ValEntry*>(d) )
277 copy->GetFile()->InsertValEntry( v->GetValue(),
278 v->GetGroup(),v->GetElement(),
283 // We skip gdcm::SeqEntries
286 d = f1->GetNextEntry();
289 std::cout << std::endl
290 << "--- write a new image(2) -------------------------------------"
293 // User knows the image is a 'color' one -RGB, YBR, Palette Color-
294 // and wants to write it as RGB
295 copy->SetImageData(imageData, dataSize);
296 copy->SetWriteModeToRGB();
297 copy->WriteDcmExplVR( output );
299 // User wants to see if there is any difference before and after writting
301 std::cout << "GetImageDataSize()"
302 << fh1->GetImageDataSize() << std::endl;
303 std::cout << "GetImageDataRawSize()"
304 << fh1->GetImageDataRawSize() << std::endl;
306 std::cout << "GetRGBDataSize()"
307 << fh1->GetRGBDataSize() << std::endl;
308 std::cout << "GetRawDataSize()"
309 << fh1->GetRawDataSize() << std::endl;
310 std::cout << "GetUserDataSize()"
311 << fh1->GetUserDataSize() << std::endl;
312 // User wants to keep the Palette Color -if any-
313 // and write the image as it was
314 copy->SetImageData(imageDataRaw, dataRawSize);
315 copy->SetWriteModeToRaw();
316 copy->WriteDcmExplVR( output );
319 std::cout << std::endl
320 << "------------------------------------------------------------"
322 // User is in a fancy mood and wants to forge a bomb image
323 // just to see how other Dicom viewers act
329 std::cout << std::endl
330 << "------------------------------------------------------------"