]> Creatis software - gdcm.git/blob - Example/exReadWriteFile.cxx
typo + comments
[gdcm.git] / Example / exReadWriteFile.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exReadWriteFile.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/02/09 14:00:41 $
7   Version:   $Revision: 1.2 $
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 Field 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    uint16_t group;
82    uint16_t elem;
83    int offset;
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          group  = valEntry->GetGroup();
113          elem   = valEntry->GetElement();
114          vr     = valEntry->GetVR();
115          // user wants really to know everything about entry!
116          vm     = valEntry->GetVM();
117          name   = valEntry->GetName();
118          offset = valEntry->GetOffset();
119
120          std::cout //<< std::hex << group << "," << elem 
121           << valEntry->GetKey()
122                << "     VR :[" << vr    << "] VM : [" << vm 
123              << "] name : [" << name  << "]"
124              << " value : [" << value << "]" 
125  
126               << std::endl;
127       }
128       d = f1->GetNextEntry();
129    }
130
131    std::cout << std::endl
132              << "--- Use pre-defined acessors ---------------------------------"
133              << std::endl;
134  
135    // ------ some pre-defined acessors may supply usefull informations ----
136
137    // about Image
138    int linesNumber   = f1->GetYSize();
139    int rawsNumber    = f1->GetXSize();
140    int framesNumber  = f1->GetYSize();// defaulted to 1 if not found
141
142    std::cout << "lines : "   << linesNumber  << " columns : " << rawsNumber
143              << " frames : " << framesNumber << std::endl;
144  
145    // about Pixels
146    int pixelSize         = f1->GetPixelSize(); 
147    std::string pixelType = f1->GetPixelType();
148    bool isSigned         = f1->IsSignedPixelData();
149   
150    std::cout << "pixelSize : "   << pixelSize  << " pixelType : " << pixelType
151              << " signed : "     << isSigned   << std::endl;
152  
153    // about pixels, too.
154    // Better you forget these ones
155   
156    std::cout << "GetBitsStored()"      << f1->GetBitsStored()      << std::endl;
157    std::cout << "GetBitsAllocated()"   << f1->GetBitsAllocated()   << std::endl;
158    std::cout << "GetHighBitPosition()" << f1->GetHighBitPosition() << std::endl;
159
160    std::cout << "GetSamplesPerPixel()"     
161           << f1->GetSamplesPerPixel()     << std::endl;
162    std::cout << "GetPlanarConfiguration()" 
163           << f1->GetPlanarConfiguration() << std::endl; 
164  
165    // about 'image geography'
166  
167    float xs = f1->GetXSpacing();
168    float ys = f1->GetYSpacing();
169    float zs = f1->GetZSpacing();  // defaulted to 1.0 if not found
170
171    float xo = f1->GetXOrigin();
172    float yo = f1->GetYOrigin();
173    float zo = f1->GetZOrigin();
174
175    std::cout << "GetXSpacing()"     << xs      << std::endl;
176    std::cout << "GetYSpacing()"     << ys      << std::endl;
177    std::cout << "GetXSpacing()"     << zs      << std::endl;
178
179    std::cout << "GetXOrigin()"      << xo      << std::endl;
180    std::cout << "GetYOrigin()"      << yo      << std::endl;
181    std::cout << "GetZOrigin()"      << zo      << std::endl;
182
183    // about its way to store colors (if user is aware)
184
185    // checks Photometric Interpretation
186    std::cout << "IsMonochrome()"   << f1->IsMonochrome()     << std::endl;
187    std::cout << "IsYBRFull()"      << f1->IsYBRFull()        << std::endl;
188    std::cout << "IsPaletteColor()" << f1->IsPaletteColor()   << std::endl;
189    // checks if LUT are found
190    std::cout << "HasLUT()"         << f1->HasLUT()           << std::endl;
191
192    std::cout << "GetNumberOfScalarComponents()"    
193           << f1->GetNumberOfScalarComponents()<< std::endl;
194    std::cout << "GetNumberOfScalarComponentsRaw()" 
195           << f1->GetNumberOfScalarComponentsRaw()<< std::endl;
196   
197
198    std::cout << std::endl
199              << "--- Get values on request ------------------------------------"
200              << std::endl;
201    // ------ User is aware, and wants to get fields with no accesor --------
202
203    std::cout << "Manufacturer :["     << f1->GetEntryValue(0x0008,0x0070)
204              << "]" << std::endl; 
205    std::cout << "Institution :["      << f1->GetEntryValue(0x0008,0x0080)
206              << "]" << std::endl;
207    std::cout << "Patient's name :["   << f1->GetEntryValue(0x0010,0x0010)
208              << "]" << std::endl;
209    std::cout << "Physician's name :[" << f1->GetEntryValue(0x0008,0x0090)
210              << "]" << std::endl; 
211    std::cout << "Study Date :["       << f1->GetEntryValue(0x0008,0x0020)
212              << "]" << std::endl; 
213    std::cout << "Study inst UID :["   << f1->GetEntryValue(0x0020,0x000d)
214              << "]" << std::endl;
215    std::cout << "Serie inst UID :["   << f1->GetEntryValue(0x0020,0x000e)
216              << "]" << std::endl;
217    std::cout << "Frame ref UID :["   << f1->GetEntryValue(0x0020,0x0052)
218              << "]" << std::endl;
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( output );
261  
262    d = f1->GetFirstEntry();
263    while(d)
264    {
265       // We skip SeqEntries, since user cannot do much with them
266       if ( !(dynamic_cast<gdcm::SeqEntry*>(d))
267       // We skip Shadow Groups, since nobody knows what they mean
268            && !( d->GetGroup()%2 ) )
269       { 
270
271          if ( gdcm::BinEntry *b = dynamic_cast<gdcm::BinEntry*>(d) )
272          {              
273             copy->GetFile()->InsertBinEntry( b->GetBinArea(),b->GetLength(),
274                                              b->GetGroup(),b->GetElement(),
275                                              b->GetVR() );
276          }
277          else if ( gdcm::ValEntry *v = dynamic_cast<gdcm::ValEntry*>(d) )
278          {   
279              copy->GetFile()->InsertValEntry( v->GetValue(),
280                                               v->GetGroup(),v->GetElement(),
281                                               v->GetVR() ); 
282          }
283          else
284          {
285           // We skip gdcm::SeqEntries
286          }
287       }
288       d = f1->GetNextEntry();
289    }
290
291    std::cout << std::endl
292              << "--- write a new image(2) -------------------------------------"
293              << std::endl;
294  
295    // User knows the image is a 'color' one -RGB, YBR, Palette Color-
296    // and wants to write it as RGB
297    copy->SetImageData(imageData, dataSize);
298    copy->SetWriteModeToRGB();
299    copy->WriteDcmExplVR( output );
300
301    // User wants to see if there is any difference before and after writting
302
303    std::cout << "GetImageDataSize()"    
304           << fh1->GetImageDataSize()    << std::endl;
305    std::cout << "GetImageDataRawSize()" 
306           << fh1->GetImageDataRawSize() << std::endl;
307    // User Data
308    std::cout << "GetRGBDataSize()"      
309           << fh1->GetRGBDataSize()      << std::endl;
310    std::cout << "GetRawDataSize()"      
311           << fh1->GetRawDataSize()      << std::endl;
312    std::cout << "GetUserDataSize()"     
313           << fh1->GetUserDataSize()     << std::endl;
314    // User wants to keep the Palette Color -if any- 
315    // and write the image as it was
316    copy->SetImageData(imageDataRaw, dataRawSize);
317    copy->SetWriteModeToRaw();
318    copy->WriteDcmExplVR( output );
319
320
321    std::cout << std::endl
322              << "------------------------------------------------------------"
323              << std::endl;
324    // User is in a fancy mood and wants to forge a bomb image
325    // just to see how other Dicom viewers act
326
327
328    // TODO : finish it 
329
330
331    std::cout << std::endl
332              << "------------------------------------------------------------"
333              << std::endl;
334    delete f1;
335    delete fh1;
336    delete copy;
337
338    exit (0);
339 }
340