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