]> Creatis software - gdcm.git/blob - Example/Anonymize.cxx
Use Argument Manager in 'utilities'
[gdcm.git] / Example / Anonymize.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: Anonymize.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/06/07 11:12:10 $
7   Version:   $Revision: 1.1 $
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 (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 *f1;
78
79    f1 = new gdcm::File( fileName );
80    if (!f1->IsReadable()) 
81    {
82        std::cerr << "Sorry, " << fileName <<"  not a gdcm-readable "
83                  << "DICOM / ACR File" <<std::endl;
84        delete f1;  
85        return 0;
86    }
87    std::cout << " ... is readable " << std::endl;
88
89    // ============================================================
90    //   Load the pixels in memory.
91    // ============================================================
92
93    // We need a gdcm::FileHelper, since we want to load the pixels        
94    gdcm::FileHelper *fh1 = new gdcm::FileHelper(f1);
95
96    // (unit8_t DOESN'T mean it's mandatory for the image to be a 8 bits one) 
97
98    uint8_t *imageData = fh1->GetImageData();
99
100    if ( imageData == 0 )
101    {
102        std::cerr << "Sorry, Pixels of" << fileName <<"  are not "
103            << " gdcm-readable."       << std::endl
104                  << "Use exAnonymizeNoLoad" << std::endl;
105        delete f1;  
106        delete fh1;    
107        return 0;
108    } 
109
110    // ============================================================
111    //  Choose the fields to anonymize.
112    // ============================================================
113    // Institution name 
114    f1->AddAnonymizeElement(0x0008, 0x0080, "Xanadoo"); 
115    // Patient's name 
116    f1->AddAnonymizeElement(0x0010, 0x0010, "Fantomas");   
117    // Patient's ID
118    f1->AddAnonymizeElement( 0x0010, 0x0020,"1515" );   
119    // Study Instance UID
120    f1->AddAnonymizeElement(0x0020, 0x000d, "9.99.999.9999" );
121    // Telephone
122    f1->AddAnonymizeElement(0x0010, 0x2154, "3615" );
123
124    // Aware user will add more fields to anonymize here
125
126    // The gdcm::File is modified in memory
127
128    f1->AnonymizeFile();
129
130    // ============================================================
131    //   Write a new file
132    // ============================================================
133
134    fh1->WriteDcmExplVR(outputFileName);
135    std::cout <<"End Anonymize" << std::cout;
136
137    // ============================================================
138    //   Remove the Anonymize list
139    // ============================================================  
140    f1->ClearAnonymizeList();
141     
142    delete f1;
143    delete fh1; 
144    return 0;
145 }
146