]> Creatis software - gdcm.git/blob - Example/exReadWriteFile.cxx
ENH: gdcm now almost compile with GDCM_LEGACY_REMOVE on
[gdcm.git] / Example / exReadWriteFile.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exReadWriteFile.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/07/19 15:19:25 $
7   Version:   $Revision: 1.6 $
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 #include "gdcmFile.h"
20 #include "gdcmFileHelper.h"
21 #include "gdcmDocument.h"
22 #include "gdcmValEntry.h"
23 #include "gdcmBinEntry.h"
24 #include "gdcmSeqEntry.h"
25
26 #include <stdlib.h> // for exit
27  
28 int main(int argc, char *argv[])
29 {
30
31
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;
49
50    if (argc < 3)
51    {
52       std::cerr << "Usage :" << std::endl << 
53       argv[0] << " input_dicom output_dicom" << std::endl;
54       return 1;
55    }
56
57    std::string filename = argv[1];
58    std::string output   = argv[2];
59
60    // First, let's create a gdcm::File
61    // that will contain all the Dicom fields but the Pixels Element
62
63    gdcm::File *f1= new gdcm::File( );
64    f1->SetFileName( filename );
65    f1->Load();
66
67
68    // Ask content to be printed
69    std::cout << std::endl
70              << "--- Standard Print -------------------------------------------"
71              << std::endl;
72    f1->SetPrintLevel(2);   // to have a nice output
73    //f1->SetPrintLevel(1); 
74    f1->Print();            // user may comment out if too much verbose
75
76
77   // User asks for field by field Printing
78   
79    std::cout << std::endl
80              << "--- Display only human readable values -----------------------"
81              << std::endl;
82
83    gdcm::ValEntry *valEntry;
84    std::string value;
85    std::string vr;   // value representation
86    std::string vm;   // value multiplicity
87    std::string name; // held in the Dicom Dictionary
88
89
90    gdcm::DocEntry *d = f1->GetFirstEntry();
91    while( d )
92    {
93       // We skip SeqEntries, since user cannot do much with them
94       if ( !(dynamic_cast<gdcm::SeqEntry*>(d))
95       // We skip Shadow Groups, since nobody knows what they mean
96            && !( d->GetGroup()%2 )
97      // we skip BinEntries, since their content is not human-readable
98            && !dynamic_cast<gdcm::BinEntry*>(d) )
99      {      
100          // If user just 'wants to see'
101          //d->Print();
102          //std::cout << std::endl;
103
104          // If user wants to get info about the entry
105          // (he is sure, here that DocEntry is a ValEntry)
106          valEntry = dynamic_cast<gdcm::ValEntry*>(d);
107          // Let's be carefull -maybe he commented out some previous line-
108          if (!valEntry)
109             continue;
110
111          value  = valEntry->GetValue();
112          vr     = valEntry->GetVR();
113          // user wants really to know everything about entry!
114          vm     = valEntry->GetVM();
115          name   = valEntry->GetName();
116
117          std::cout //<< std::hex << group << "," << elem 
118           << valEntry->GetKey()
119                << "     VR :[" << vr    << "] VM : [" << vm 
120              << "] name : [" << name  << "]"
121              << " value : [" << value << "]" 
122  
123               << std::endl;
124       }
125       d = f1->GetNextEntry();
126    }
127
128    std::cout << std::endl
129              << "--- Use pre-defined acessors ---------------------------------"
130              << std::endl;
131  
132    // ------ some pre-defined acessors may supply usefull informations ----
133
134    // about Image
135    int linesNumber   = f1->GetYSize();
136    int rawsNumber    = f1->GetXSize();
137    int framesNumber  = f1->GetYSize();// defaulted to 1 if not found
138
139    std::cout << "lines : "   << linesNumber  << " columns : " << rawsNumber
140              << " frames : " << framesNumber << std::endl;
141  
142    // about Pixels
143    int pixelSize         = f1->GetPixelSize(); 
144    std::string pixelType = f1->GetPixelType();
145    bool isSigned         = f1->IsSignedPixelData();
146   
147    std::cout << "pixelSize : "   << pixelSize  << " pixelType : " << pixelType
148              << " signed : "     << isSigned   << std::endl;
149  
150    // about pixels, too.
151    // Better you forget these ones
152   
153    std::cout << "GetBitsStored()"      << f1->GetBitsStored()      << std::endl;
154    std::cout << "GetBitsAllocated()"   << f1->GetBitsAllocated()   << std::endl;
155    std::cout << "GetHighBitPosition()" << f1->GetHighBitPosition() << std::endl;
156
157    std::cout << "GetSamplesPerPixel()"     
158           << f1->GetSamplesPerPixel()     << std::endl;
159    std::cout << "GetPlanarConfiguration()" 
160           << f1->GetPlanarConfiguration() << std::endl; 
161  
162    // about 'image geography'
163  
164    float xs = f1->GetXSpacing();
165    float ys = f1->GetYSpacing();
166    float zs = f1->GetZSpacing();  // defaulted to 1.0 if not found
167
168    float xo = f1->GetXOrigin();
169    float yo = f1->GetYOrigin();
170    float zo = f1->GetZOrigin();
171
172    std::cout << "GetXSpacing()"     << xs      << std::endl;
173    std::cout << "GetYSpacing()"     << ys      << std::endl;
174    std::cout << "GetXSpacing()"     << zs      << std::endl;
175
176    std::cout << "GetXOrigin()"      << xo      << std::endl;
177    std::cout << "GetYOrigin()"      << yo      << std::endl;
178    std::cout << "GetZOrigin()"      << zo      << std::endl;
179
180    // about its way to store colors (if user is aware)
181
182    // checks Photometric Interpretation
183    std::cout << "IsMonochrome()"   << f1->IsMonochrome()     << std::endl;
184    std::cout << "IsYBRFull()"      << f1->IsYBRFull()        << std::endl;
185    std::cout << "IsPaletteColor()" << f1->IsPaletteColor()   << std::endl;
186    // checks if LUT are found
187    std::cout << "HasLUT()"         << f1->HasLUT()           << std::endl;
188
189    std::cout << "GetNumberOfScalarComponents()"    
190           << f1->GetNumberOfScalarComponents()<< std::endl;
191    std::cout << "GetNumberOfScalarComponentsRaw()" 
192           << f1->GetNumberOfScalarComponentsRaw()<< std::endl;
193   
194
195    std::cout << std::endl
196              << "--- Get values on request ------------------------------------"
197              << std::endl;
198    // ------ User is aware, and wants to get fields with no accesor --------
199
200    std::cout << "Manufacturer :["     << f1->GetEntryValue(0x0008,0x0070)
201              << "]" << std::endl; 
202    std::cout << "Institution :["      << f1->GetEntryValue(0x0008,0x0080)
203              << "]" << std::endl;
204    std::cout << "Patient's name :["   << f1->GetEntryValue(0x0010,0x0010)
205              << "]" << std::endl;
206    std::cout << "Physician's name :[" << f1->GetEntryValue(0x0008,0x0090)
207              << "]" << std::endl; 
208    std::cout << "Study Date :["       << f1->GetEntryValue(0x0008,0x0020)
209              << "]" << std::endl; 
210    std::cout << "Study inst UID :["   << f1->GetEntryValue(0x0020,0x000d)
211              << "]" << std::endl;
212    std::cout << "Serie inst UID :["   << f1->GetEntryValue(0x0020,0x000e)
213              << "]" << std::endl;
214    std::cout << "Frame ref UID :["   << f1->GetEntryValue(0x0020,0x0052)
215              << "]" << std::endl;
216  
217    // User wants to get info about the 'real world' vs image
218
219
220    // ------ User wants to load the pixels---------------------------------
221    
222    // Hope now he knows enought about the image ;-)
223
224    // First, create a gdcm::FileHelper
225    gdcm::FileHelper *fh1 = new gdcm::FileHelper(f1);
226
227    // Load the pixels, transforms LUT (if any) into RGB Pixels 
228    uint8_t *imageData = fh1->GetImageData();
229    // Get the image data size
230    size_t dataSize    = fh1->GetImageDataSize();
231
232    // Probabely, a straigh user won't load both ...
233
234    // Load the pixels, DO NOT transform LUT (if any) into RGB Pixels 
235    uint8_t *imageDataRaw = fh1->GetImageDataRaw();
236    // Get the image data size
237    size_t dataRawSize    = fh1->GetImageDataRawSize();
238
239    // TODO : Newbee user would appreciate any comment !
240  
241    std::cout << "GetImageDataSize()"    
242           << fh1->GetImageDataSize()    << std::endl;
243    std::cout << "GetImageDataRawSize()" 
244           << fh1->GetImageDataRawSize() << std::endl;
245    // User Data
246    std::cout << "GetRGBDataSize()"      
247           << fh1->GetRGBDataSize()      << std::endl;
248    std::cout << "GetRawDataSize()"      
249           << fh1->GetRawDataSize()      << std::endl;
250    std::cout << "GetUserDataSize()"     
251           << fh1->GetUserDataSize()     << std::endl;
252  
253
254    std::cout << std::endl
255              << "--- write a new image(1) -------------------------------------"
256              << std::endl;
257  
258    // ------ User wants write a new image without shadow groups -------------
259
260    gdcm::FileHelper *copy = new gdcm::FileHelper( );
261    copy->SetFileName( output );
262    copy->Load();
263  
264    d = f1->GetFirstEntry();
265    while(d)
266    {
267       // We skip SeqEntries, since user cannot do much with them
268       if ( !(dynamic_cast<gdcm::SeqEntry*>(d))
269       // We skip Shadow Groups, since nobody knows what they mean
270            && !( d->GetGroup()%2 ) )
271       { 
272
273          if ( gdcm::BinEntry *b = dynamic_cast<gdcm::BinEntry*>(d) )
274          {              
275             copy->GetFile()->InsertBinEntry( b->GetBinArea(),b->GetLength(),
276                                              b->GetGroup(),b->GetElement(),
277                                              b->GetVR() );
278          }
279          else if ( gdcm::ValEntry *v = dynamic_cast<gdcm::ValEntry*>(d) )
280          {   
281              copy->GetFile()->InsertValEntry( v->GetValue(),
282                                               v->GetGroup(),v->GetElement(),
283                                               v->GetVR() ); 
284          }
285          else
286          {
287           // We skip gdcm::SeqEntries
288          }
289       }
290       d = f1->GetNextEntry();
291    }
292
293    std::cout << std::endl
294              << "--- write a new image(2) -------------------------------------"
295              << std::endl;
296  
297    // User knows the image is a 'color' one -RGB, YBR, Palette Color-
298    // and wants to write it as RGB
299    copy->SetImageData(imageData, dataSize);
300    copy->SetWriteModeToRGB();
301    copy->WriteDcmExplVR( output );
302
303    // User wants to see if there is any difference before and after writting
304
305    std::cout << "GetImageDataSize()"    
306           << fh1->GetImageDataSize()    << std::endl;
307    std::cout << "GetImageDataRawSize()" 
308           << fh1->GetImageDataRawSize() << std::endl;
309    // User Data
310    std::cout << "GetRGBDataSize()"      
311           << fh1->GetRGBDataSize()      << std::endl;
312    std::cout << "GetRawDataSize()"      
313           << fh1->GetRawDataSize()      << std::endl;
314    std::cout << "GetUserDataSize()"     
315           << fh1->GetUserDataSize()     << std::endl;
316    // User wants to keep the Palette Color -if any- 
317    // and write the image as it was
318    copy->SetImageData(imageDataRaw, dataRawSize);
319    copy->SetWriteModeToRaw();
320    copy->WriteDcmExplVR( output );
321
322
323    std::cout << std::endl
324              << "------------------------------------------------------------"
325              << std::endl;
326    // User is in a fancy mood and wants to forge a bomb image
327    // just to see how other Dicom viewers act
328
329
330    // TODO : finish it 
331
332
333    std::cout << std::endl
334              << "------------------------------------------------------------"
335              << std::endl;
336    delete f1;
337    delete fh1;
338    delete copy;
339
340    exit (0);
341 }
342