]> Creatis software - gdcm.git/blob - Example/exAnonymize.cxx
Remove useless exAnonymizeNoLoad (use AnonymizeNoload)
[gdcm.git] / Example / exAnonymize.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exAnonymize.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/07/07 17:31:53 $
7   Version:   $Revision: 1.4 $
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 #include "gdcmFile.h"
19 #include "gdcmFileHelper.h"
20 #include "gdcmCommon.h"
21 #include "gdcmDebug.h"
22
23 #include <iostream>
24
25 int main(int argc, char *argv[])
26 {   
27    gdcm::Debug::DebugOn();
28    std::cout << "------------------------------------------------" << std::endl;
29    std::cout << "Anonymize a full gdcm-readable  Dicom image"      << std::endl;
30    std::cout << " Warning : probably segfaults if pixels are not "
31           << " gdcm readable. Use exAnonymizeNoLoad"            << std::endl;
32
33    if( argc < 3 )
34     {
35     std::cerr << "Usage " << argv[0] << " Source image.dcm  " 
36               << " Output image.dcm " << std::endl;
37     return 1;
38     }
39
40    std::string fileName       = argv[1];
41    std::string outputFileName = argv[2];
42
43 // ============================================================
44 //   Read the input image.
45 // ============================================================
46
47    std::cout << argv[1] << std::endl;
48
49    gdcm::File *f = new gdcm::File();
50    f->SetLoadMode( 0x00000000);
51    f->SetFileName( fileName );
52    bool res = f->Load();        
53
54    if (!res) {
55        std::cerr << "Sorry, " << fileName <<"  not a gdcm-readable "
56            << "DICOM / ACR File"
57                  <<std::endl;
58        return 0;
59    }
60    std::cout << " ... is readable " << std::endl;
61
62
63 // ============================================================
64 //   Load the pixels in memory.
65 // ============================================================
66
67    // We need a gdcm::FileHelper, since we want to load the pixels        
68    gdcm::FileHelper *fh = new gdcm::FileHelper(f);
69
70    // (unit8_t DOESN'T mean it's mandatory for the image to be a 8 bits one) 
71
72    uint8_t *imageData = fh->GetImageData();
73
74    if ( imageData == 0 )
75    {
76        std::cerr << "Sorry, Pixels of" << fileName <<"  are not "
77            << " gdcm-readable."       << std::endl
78                  << "Use exAnonymizeNoLoad" << std::endl;
79   
80        return 0;
81    } 
82
83 // ============================================================
84 //  Choose the fields to anonymize.
85 // ============================================================
86    // Institution name 
87    f->AddAnonymizeElement(0x0008, 0x0080, "Xanadoo"); 
88    // Patient's name 
89    f->AddAnonymizeElement(0x0010, 0x0010, "Fantomas");   
90    // Patient's ID
91    f->AddAnonymizeElement( 0x0010, 0x0020,"1515" );   
92    // Study Instance UID
93    f->AddAnonymizeElement(0x0020, 0x000d, "9.99.999.9999" );
94    // Telephone
95    f->AddAnonymizeElement(0x0010, 0x2154, "3615" );
96   // Aware use will add new fields here
97
98 // The gdcm::File is modified in memory
99
100    f->AnonymizeFile();
101
102 // ============================================================
103 //   Write a new file
104 // ============================================================
105
106    fh->WriteDcmExplVR(outputFileName);
107    std::cout <<"End Anonymize" << std::cout;
108
109 // ============================================================
110 //   Remove the Anonymize list
111 // ============================================================  
112    f->ClearAnonymizeList();
113     
114    delete f;
115    delete fh; 
116    return 0;
117 }
118