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