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