1 /*=========================================================================
4 Module: $RCSfile: exReadWriteFile.cxx,v $
6 Date: $Date: 2010/09/01 14:41:48 $
7 Version: $Revision: 1.11 $
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 "gdcmDataEntry.h"
23 #include "gdcmSeqEntry.h"
25 #include <stdlib.h> // for exit
27 int main(int argc, char *argv[])
29 std::cout << " --- WARNING --- WARNING --- WARNING --- WARNING ---" <<std::endl;
30 std::cout << " " <<std::endl;
31 std::cout << " This source program is NOT intendend to be run as is"<<std::endl;
32 std::cout << " " <<std::endl;
33 std::cout << " It just shows a set of possible uses." <<std::endl;
34 std::cout << "User MUST read it, " <<std::endl;
35 std::cout << " comment out the useless parts " <<std::endl;
36 std::cout << " invoke it with an ad hoc image(*) " <<std::endl;
37 std::cout << " check the resulting image " <<std::endl;
38 std::cout << " " <<std::endl;
39 std::cout << " (*) For samples, user can refer to gdcmData" <<std::endl;
40 std::cout << " and read README.txt file " <<std::endl;
41 std::cout << " " <<std::endl;
42 std::cout << "This source program will be splitted into smaller elementary"
43 << " programs" <<std::endl;
44 std::cout << " " <<std::endl;
45 std::cout << " --- WARNING --- WARNING --- WARNING --- WARNING ---" <<std::endl;
49 std::cerr << "Usage :" << std::endl <<
50 argv[0] << " input_dicom output_dicom" << std::endl;
54 std::string filename = argv[1];
55 std::string output = argv[2];
57 // First, let's create a GDCM_NAME_SPACE::File
58 // that will contain all the Dicom fields but the Pixels Element
60 GDCM_NAME_SPACE::File *f1= GDCM_NAME_SPACE::File::New( );
61 f1->SetFileName( filename );
65 // Ask content to be printed
66 std::cout << std::endl
67 << "--- Standard Print -------------------------------------------"
69 f1->SetPrintLevel(2); // to have a nice output
70 //f1->SetPrintLevel(1);
71 f1->Print(); // user may comment out if too much verbose
74 // User asks for field by field Printing
76 std::cout << std::endl
77 << "--- Display only human readable values -----------------------"
80 GDCM_NAME_SPACE::DataEntry *dataEntry;
82 GDCM_NAME_SPACE::VRKey vr; // value representation
83 std::string vm; // value multiplicity
84 std::string name; // held in the Dicom Dictionary
87 GDCM_NAME_SPACE::DocEntry *d = f1->GetFirstEntry();
90 // We skip SeqEntries, since user cannot do much with them
91 if ( !(dynamic_cast<GDCM_NAME_SPACE::SeqEntry*>(d))
92 // We skip Shadow Groups, since nobody knows what they mean
93 && !( d->GetGroup()%2 ) )
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 DataEntry)
101 dataEntry = dynamic_cast<GDCM_NAME_SPACE::DataEntry *>(d);
102 // Let's be carefull -maybe he commented out some previous line-
106 value = dataEntry->GetString();
107 vr = dataEntry->GetVR();
108 // user wants really to know everything about entry!
109 vm = dataEntry->GetVM();
110 name = dataEntry->GetName();
112 std::cout //<< std::hex << group << "," << elem
113 << dataEntry->GetKey()
114 << " VR :[" << vr << "] VM : [" << vm
115 << "] name : [" << name << "]"
116 << " value : [" << value << "]"
119 d = f1->GetNextEntry();
122 std::cout << std::endl
123 << "--- Use pre-defined acessors ---------------------------------"
126 // ------ some pre-defined acessors may supply usefull informations ----
129 int linesNumber = f1->GetYSize();
130 int rawsNumber = f1->GetXSize();
131 int framesNumber = f1->GetYSize();// defaulted to 1 if not found
133 std::cout << "lines : " << linesNumber << " columns : " << rawsNumber
134 << " frames : " << framesNumber << std::endl;
137 int pixelSize = f1->GetPixelSize();
138 std::string pixelType = f1->GetPixelType();
139 bool isSigned = f1->IsSignedPixelData();
141 std::cout << "pixelSize : " << pixelSize << " pixelType : " << pixelType
142 << " signed : " << isSigned << std::endl;
144 // about pixels, too.
145 // Better you forget these ones
147 std::cout << "GetBitsStored()" << f1->GetBitsStored() << std::endl;
148 std::cout << "GetBitsAllocated()" << f1->GetBitsAllocated() << std::endl;
149 std::cout << "GetHighBitPosition()" << f1->GetHighBitPosition() << std::endl;
151 std::cout << "GetSamplesPerPixel()"
152 << f1->GetSamplesPerPixel() << std::endl;
153 std::cout << "GetPlanarConfiguration()"
154 << f1->GetPlanarConfiguration() << std::endl;
156 // about 'image geography'
158 float xs = f1->GetXSpacing();
159 float ys = f1->GetYSpacing();
160 float zs = f1->GetZSpacing(); // defaulted to 1.0 if not found
162 float xo = f1->GetXOrigin();
163 float yo = f1->GetYOrigin();
164 float zo = f1->GetZOrigin();
166 std::cout << "GetXSpacing()" << xs << std::endl;
167 std::cout << "GetYSpacing()" << ys << std::endl;
168 std::cout << "GetXSpacing()" << zs << std::endl;
170 std::cout << "GetXOrigin()" << xo << std::endl;
171 std::cout << "GetYOrigin()" << yo << std::endl;
172 std::cout << "GetZOrigin()" << zo << std::endl;
174 // about its way to store colors (if user is aware)
176 // checks Photometric Interpretation
177 std::cout << "IsMonochrome()" << f1->IsMonochrome() << std::endl;
178 std::cout << "IsYBRFull()" << f1->IsYBRFull() << std::endl;
179 std::cout << "IsPaletteColor()" << f1->IsPaletteColor() << std::endl;
180 // checks if LUT are found
181 std::cout << "HasLUT()" << f1->HasLUT() << std::endl;
183 std::cout << "GetNumberOfScalarComponents()"
184 << f1->GetNumberOfScalarComponents()<< std::endl;
185 std::cout << "GetNumberOfScalarComponentsRaw()"
186 << f1->GetNumberOfScalarComponentsRaw()<< std::endl;
189 std::cout << std::endl
190 << "--- Get values on request ------------------------------------"
192 // ------ User is aware, and wants to get fields with no accesor --------
194 std::cout << "Manufacturer :[" << f1->GetEntryString(0x0008,0x0070)
196 std::cout << "Institution :[" << f1->GetEntryString(0x0008,0x0080)
198 std::cout << "Patient's name :[" << f1->GetEntryString(0x0010,0x0010)
200 std::cout << "Physician's name :[" << f1->GetEntryString(0x0008,0x0090)
202 std::cout << "Study Date :[" << f1->GetEntryString(0x0008,0x0020)
204 std::cout << "Study inst UID :[" << f1->GetEntryString(0x0020,0x000d)
206 std::cout << "Serie inst UID :[" << f1->GetEntryString(0x0020,0x000e)
208 std::cout << "Frame ref UID :[" << f1->GetEntryString(0x0020,0x0052)
211 // User wants to get info about the 'real world' vs image
214 // ------ User wants to load the pixels---------------------------------
216 // Hope now he knows enought about the image ;-)
218 // First, create a GDCM_NAME_SPACE::FileHelper
219 GDCM_NAME_SPACE::FileHelper *fh1 = GDCM_NAME_SPACE::FileHelper::New(f1);
221 // Load the pixels, transforms LUT (if any) into RGB Pixels
222 uint8_t *imageData = fh1->GetImageData();
223 // Get the image data size
224 size_t dataSize = fh1->GetImageDataSize();
226 // Probabely, a straigh user won't load both ...
228 // Load the pixels, DO NOT transform LUT (if any) into RGB Pixels
229 uint8_t *imageDataRaw = fh1->GetImageDataRaw();
230 // Get the image data size
231 size_t dataRawSize = fh1->GetImageDataRawSize();
233 /// \TODO : Newbee user would appreciate any comment !
235 std::cout << "GetImageDataSize()"
236 << fh1->GetImageDataSize() << std::endl;
237 std::cout << "GetImageDataRawSize()"
238 << fh1->GetImageDataRawSize() << std::endl;
240 std::cout << "GetRGBDataSize()"
241 << fh1->GetRGBDataSize() << std::endl;
242 std::cout << "GetRawDataSize()"
243 << fh1->GetRawDataSize() << std::endl;
244 std::cout << "GetUserDataSize()"
245 << fh1->GetUserDataSize() << std::endl;
248 std::cout << std::endl
249 << "--- write a new image(1) -------------------------------------"
252 // ------ User wants write a new image without shadow groups -------------
254 GDCM_NAME_SPACE::FileHelper *copy = GDCM_NAME_SPACE::FileHelper::New( );
255 copy->SetFileName( output );
258 d = f1->GetFirstEntry();
261 // We skip SeqEntries, since user cannot do much with them
262 if ( !(dynamic_cast<GDCM_NAME_SPACE::SeqEntry*>(d))
263 // We skip Shadow Groups, since nobody knows what they mean
264 && !( d->GetGroup()%2 ) )
267 if ( GDCM_NAME_SPACE::DataEntry *de = dynamic_cast<GDCM_NAME_SPACE::DataEntry *>(d) )
269 copy->GetFile()->InsertEntryBinArea( de->GetBinArea(),de->GetLength(),
270 de->GetGroup(),de->GetElement(),
275 // We skip GDCM_NAME_SPACE::SeqEntries
278 d = f1->GetNextEntry();
281 std::cout << std::endl
282 << "--- write a new image(2) -------------------------------------"
285 // User knows the image is a 'color' one -RGB, YBR, Palette Color-
286 // and wants to write it as RGB
287 copy->SetImageData(imageData, dataSize);
288 copy->SetWriteModeToRGB();
289 copy->WriteDcmExplVR( output );
291 // User wants to see if there is any difference before and after writting
293 std::cout << "GetImageDataSize()"
294 << fh1->GetImageDataSize() << std::endl;
295 std::cout << "GetImageDataRawSize()"
296 << fh1->GetImageDataRawSize() << std::endl;
298 std::cout << "GetRGBDataSize()"
299 << fh1->GetRGBDataSize() << std::endl;
300 std::cout << "GetRawDataSize()"
301 << fh1->GetRawDataSize() << std::endl;
302 std::cout << "GetUserDataSize()"
303 << fh1->GetUserDataSize() << std::endl;
304 // User wants to keep the Palette Color -if any-
305 // and write the image as it was
306 copy->SetImageData(imageDataRaw, dataRawSize);
307 copy->SetWriteModeToRaw();
308 copy->WriteDcmExplVR( output );
311 std::cout << std::endl
312 << "------------------------------------------------------------"
314 // User is in a fancy mood and wants to forge a bomb image
315 // just to see how other Dicom viewers act
321 std::cout << std::endl
322 << "------------------------------------------------------------"