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