]> Creatis software - gdcm.git/blob - Example/Anonymize.cxx
Unify user interface
[gdcm.git] / Example / Anonymize.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: Anonymize.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/07/21 04:55:50 $
7   Version:   $Revision: 1.3 $
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 "gdcmArgMgr.h"
24
25 #include <iostream>
26
27 int main(int argc, char *argv[])
28 {
29    START_USAGE(usage)
30    " \n Anonymize :\n                                                         ",
31    " Anonymize a full gdcm-readable Dicom image                               ",
32    "          Warning : probably segfaults if pixels are not gdcm readable.   ",
33    "                    Use exAnonymizeNoLoad instead.                        ",
34    " usage: Anonymize filein=inputFileName fileout=anonymizedFileName[debug]  ",
35    "        debug    : user wants to run the program in 'debug mode'          ",
36    FINISH_USAGE
37
38    // ----- Initialize Arguments Manager ------   
39    gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
40   
41    if (argc == 1 || am->ArgMgrDefined("usage")) 
42    {
43       am->ArgMgrUsage(usage); // Display 'usage'
44       delete am;
45       return 0;
46    }
47    char *fileName = am->ArgMgrWantString("filein",usage);
48    if ( fileName == NULL )
49    {
50       delete am;
51       return 0;
52    }
53
54    char *outputFileName = am->ArgMgrWantString("fileout",usage);
55    if ( outputFileName == NULL )
56    {
57       delete am;
58       return 0;
59    }
60    if (am->ArgMgrDefined("debug"))
61       gdcm::Debug::DebugOn();
62  
63    // if unused Param we give up
64    if ( am->ArgMgrPrintUnusedLabels() )
65    { 
66       am->ArgMgrUsage(usage);
67       delete am;
68       return 0;
69    }
70
71    delete am;  // we don't need Argument Manager any longer
72
73    // ============================================================
74    //   Read the input file.
75    // ============================================================
76
77    gdcm::File *f;
78
79    f = new gdcm::File(  );
80    f->SetLoadMode( 0x00000000 );
81    f->SetFileName( fileName );
82    int res = f->Load();
83
84    if ( !res ) 
85    {
86        std::cerr << "Sorry, " << fileName <<"  not a gdcm-readable "
87                  << "DICOM / ACR File" <<std::endl;
88        delete f;  
89        return 0;
90    }
91    std::cout << " ... is readable " << std::endl;
92
93    // ============================================================
94    //   Load the pixels in memory.
95    // ============================================================
96
97    // We need a gdcm::FileHelper, since we want to load the pixels        
98    gdcm::FileHelper *fh = new gdcm::FileHelper(f);
99
100    // (unit8_t DOESN'T mean it's mandatory for the image to be a 8 bits one) 
101
102    uint8_t *imageData = fh->GetImageData();
103
104    if ( imageData == 0 )
105    {
106        std::cerr << "Sorry, Pixels of" << fileName <<"  are not "
107            << " gdcm-readable."       << std::endl
108                  << "Use exAnonymizeNoLoad" << std::endl;
109        delete f;  
110        delete fh;    
111        return 0;
112    } 
113
114    // ============================================================
115    //  Choose the fields to anonymize.
116    // ============================================================
117    // Institution name 
118    f->AddAnonymizeElement(0x0008, 0x0080, "Xanadoo"); 
119    // Patient's name 
120    f->AddAnonymizeElement(0x0010, 0x0010, "Fantomas");   
121    // Patient's ID
122    f->AddAnonymizeElement( 0x0010, 0x0020,"1515" );   
123    // Study Instance UID
124    f->AddAnonymizeElement(0x0020, 0x000d, "9.99.999.9999" );
125    // Telephone
126    f->AddAnonymizeElement(0x0010, 0x2154, "3615" );
127
128    // Aware user will add more fields to anonymize here
129
130    // The gdcm::File is modified in memory
131
132    f->AnonymizeFile();
133
134    // ============================================================
135    //   Write a new file
136    // ============================================================
137
138    fh->WriteDcmExplVR(outputFileName);
139    std::cout <<"End Anonymize" << std::cout;
140
141    // ============================================================
142    //   Remove the Anonymize list
143    // ============================================================  
144    f->ClearAnonymizeList();
145     
146    delete f;
147    delete fh; 
148    return 0;
149 }
150