]> Creatis software - gdcm.git/blob - Example/exReadWriteFile.cxx
COMP: Finish patching gdcm for support for bcb6
[gdcm.git] / Example / exReadWriteFile.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exReadWriteFile.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/07/06 15:49:31 $
7   Version:   $Revision: 1.5 $
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( filename );
64
65
66    // Ask content to be printed
67    std::cout << std::endl
68              << "--- Standard Print -------------------------------------------"
69              << std::endl;
70    f1->SetPrintLevel(2);   // to have a nice output
71    //f1->SetPrintLevel(1); 
72    f1->Print();            // user may comment out if too much verbose
73
74
75   // User asks for field by field Printing
76   
77    std::cout << std::endl
78              << "--- Display only human readable values -----------------------"
79              << std::endl;
80
81    gdcm::ValEntry *valEntry;
82    std::string value;
83    std::string vr;   // value representation
84    std::string vm;   // value multiplicity
85    std::string name; // held in the Dicom Dictionary
86
87
88    gdcm::DocEntry *d = f1->GetFirstEntry();
89    while( d )
90    {
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) )
97      {      
98          // If user just 'wants to see'
99          //d->Print();
100          //std::cout << std::endl;
101
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-
106          if (!valEntry)
107             continue;
108
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();
114
115          std::cout //<< std::hex << group << "," << elem 
116           << valEntry->GetKey()
117                << "     VR :[" << vr    << "] VM : [" << vm 
118              << "] name : [" << name  << "]"
119              << " value : [" << value << "]" 
120  
121               << std::endl;
122       }
123       d = f1->GetNextEntry();
124    }
125
126    std::cout << std::endl
127              << "--- Use pre-defined acessors ---------------------------------"
128              << std::endl;
129  
130    // ------ some pre-defined acessors may supply usefull informations ----
131
132    // about Image
133    int linesNumber   = f1->GetYSize();
134    int rawsNumber    = f1->GetXSize();
135    int framesNumber  = f1->GetYSize();// defaulted to 1 if not found
136
137    std::cout << "lines : "   << linesNumber  << " columns : " << rawsNumber
138              << " frames : " << framesNumber << std::endl;
139  
140    // about Pixels
141    int pixelSize         = f1->GetPixelSize(); 
142    std::string pixelType = f1->GetPixelType();
143    bool isSigned         = f1->IsSignedPixelData();
144   
145    std::cout << "pixelSize : "   << pixelSize  << " pixelType : " << pixelType
146              << " signed : "     << isSigned   << std::endl;
147  
148    // about pixels, too.
149    // Better you forget these ones
150   
151    std::cout << "GetBitsStored()"      << f1->GetBitsStored()      << std::endl;
152    std::cout << "GetBitsAllocated()"   << f1->GetBitsAllocated()   << std::endl;
153    std::cout << "GetHighBitPosition()" << f1->GetHighBitPosition() << std::endl;
154
155    std::cout << "GetSamplesPerPixel()"     
156           << f1->GetSamplesPerPixel()     << std::endl;
157    std::cout << "GetPlanarConfiguration()" 
158           << f1->GetPlanarConfiguration() << std::endl; 
159  
160    // about 'image geography'
161  
162    float xs = f1->GetXSpacing();
163    float ys = f1->GetYSpacing();
164    float zs = f1->GetZSpacing();  // defaulted to 1.0 if not found
165
166    float xo = f1->GetXOrigin();
167    float yo = f1->GetYOrigin();
168    float zo = f1->GetZOrigin();
169
170    std::cout << "GetXSpacing()"     << xs      << std::endl;
171    std::cout << "GetYSpacing()"     << ys      << std::endl;
172    std::cout << "GetXSpacing()"     << zs      << std::endl;
173
174    std::cout << "GetXOrigin()"      << xo      << std::endl;
175    std::cout << "GetYOrigin()"      << yo      << std::endl;
176    std::cout << "GetZOrigin()"      << zo      << std::endl;
177
178    // about its way to store colors (if user is aware)
179
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;
186
187    std::cout << "GetNumberOfScalarComponents()"    
188           << f1->GetNumberOfScalarComponents()<< std::endl;
189    std::cout << "GetNumberOfScalarComponentsRaw()" 
190           << f1->GetNumberOfScalarComponentsRaw()<< std::endl;
191   
192
193    std::cout << std::endl
194              << "--- Get values on request ------------------------------------"
195              << std::endl;
196    // ------ User is aware, and wants to get fields with no accesor --------
197
198    std::cout << "Manufacturer :["     << f1->GetEntryValue(0x0008,0x0070)
199              << "]" << std::endl; 
200    std::cout << "Institution :["      << f1->GetEntryValue(0x0008,0x0080)
201              << "]" << std::endl;
202    std::cout << "Patient's name :["   << f1->GetEntryValue(0x0010,0x0010)
203              << "]" << std::endl;
204    std::cout << "Physician's name :[" << f1->GetEntryValue(0x0008,0x0090)
205              << "]" << std::endl; 
206    std::cout << "Study Date :["       << f1->GetEntryValue(0x0008,0x0020)
207              << "]" << std::endl; 
208    std::cout << "Study inst UID :["   << f1->GetEntryValue(0x0020,0x000d)
209              << "]" << std::endl;
210    std::cout << "Serie inst UID :["   << f1->GetEntryValue(0x0020,0x000e)
211              << "]" << std::endl;
212    std::cout << "Frame ref UID :["   << f1->GetEntryValue(0x0020,0x0052)
213              << "]" << std::endl;
214  
215    // User wants to get info about the 'real world' vs image
216
217
218    // ------ User wants to load the pixels---------------------------------
219    
220    // Hope now he knows enought about the image ;-)
221
222    // First, create a gdcm::FileHelper
223    gdcm::FileHelper *fh1 = new gdcm::FileHelper(f1);
224
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();
229
230    // Probabely, a straigh user won't load both ...
231
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();
236
237    // TODO : Newbee user would appreciate any comment !
238  
239    std::cout << "GetImageDataSize()"    
240           << fh1->GetImageDataSize()    << std::endl;
241    std::cout << "GetImageDataRawSize()" 
242           << fh1->GetImageDataRawSize() << std::endl;
243    // User Data
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;
250  
251
252    std::cout << std::endl
253              << "--- write a new image(1) -------------------------------------"
254              << std::endl;
255  
256    // ------ User wants write a new image without shadow groups -------------
257
258    gdcm::FileHelper *copy = new gdcm::FileHelper( output );
259  
260    d = f1->GetFirstEntry();
261    while(d)
262    {
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 ) )
267       { 
268
269          if ( gdcm::BinEntry *b = dynamic_cast<gdcm::BinEntry*>(d) )
270          {              
271             copy->GetFile()->InsertBinEntry( b->GetBinArea(),b->GetLength(),
272                                              b->GetGroup(),b->GetElement(),
273                                              b->GetVR() );
274          }
275          else if ( gdcm::ValEntry *v = dynamic_cast<gdcm::ValEntry*>(d) )
276          {   
277              copy->GetFile()->InsertValEntry( v->GetValue(),
278                                               v->GetGroup(),v->GetElement(),
279                                               v->GetVR() ); 
280          }
281          else
282          {
283           // We skip gdcm::SeqEntries
284          }
285       }
286       d = f1->GetNextEntry();
287    }
288
289    std::cout << std::endl
290              << "--- write a new image(2) -------------------------------------"
291              << std::endl;
292  
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 );
298
299    // User wants to see if there is any difference before and after writting
300
301    std::cout << "GetImageDataSize()"    
302           << fh1->GetImageDataSize()    << std::endl;
303    std::cout << "GetImageDataRawSize()" 
304           << fh1->GetImageDataRawSize() << std::endl;
305    // User Data
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 );
317
318
319    std::cout << std::endl
320              << "------------------------------------------------------------"
321              << std::endl;
322    // User is in a fancy mood and wants to forge a bomb image
323    // just to see how other Dicom viewers act
324
325
326    // TODO : finish it 
327
328
329    std::cout << std::endl
330              << "------------------------------------------------------------"
331              << std::endl;
332    delete f1;
333    delete fh1;
334    delete copy;
335
336    exit (0);
337 }
338