]> Creatis software - gdcm.git/blob - Example/Anonymize.cxx
New fashion Patient Name, to avoid dciodvfy warning
[gdcm.git] / Example / Anonymize.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: Anonymize.cxx,v $
5   Language:  C++
6   Date:      $Date: 2006/02/02 11:26:02 $
7   Version:   $Revision: 1.9 $
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 AnonymizeNoLoad 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 = gdcm::File::New(  );
80    f->SetLoadMode( gdcm::LD_ALL );
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        f->Delete();
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 = gdcm::FileHelper::New(f);
99
100    // unit8_t DOESN'T mean it's mandatory for the image to be a 8 bits one !
101    // Feel free to cast if you know it's not. 
102
103    uint8_t *imageData = fh->GetImageData();
104
105    if ( imageData == 0 )
106    {
107        std::cerr << "Sorry, Pixels of" << fileName <<"  are not "
108                  << " gdcm-readable."       << std::endl
109                  << "Use exAnonymizeNoLoad" << std::endl;
110        f->Delete();
111        fh->Delete();
112        return 0;
113    } 
114
115    // ============================================================
116    //  Choose the fields to anonymize.
117    // ============================================================
118    // Institution name 
119    f->AddAnonymizeElement(0x0008, 0x0080, "Xanadoo"); 
120    // Patient's name 
121    f->AddAnonymizeElement(0x0010, 0x0010, "Fantomas^X");   
122    // Patient's ID
123    f->AddAnonymizeElement( 0x0010, 0x0020,"1515" );   
124    // Study Instance UID
125    f->AddAnonymizeElement(0x0020, 0x000d, "9.99.999.9999" );
126    // Telephone
127    f->AddAnonymizeElement(0x0010, 0x2154, "3615" );
128
129    // Aware user will add here more fields to anonymize here
130
131    // The gdcm::File is modified in memory
132
133    f->AnonymizeFile();
134
135    // ============================================================
136    //   Write a new file
137    // ============================================================
138    
139    // Since we just Anonymized the file, we know no modification 
140    // was performed on the pixels.
141    // We don't want this image appears as a 'Secondary Captured image'
142    fh->SetKeepMediaStorageSOPClassUID(true);
143    
144    fh->WriteDcmExplVR(outputFileName);
145    std::cout <<"End Anonymize" << std::cout;
146
147    // ============================================================
148    //   Remove the Anonymize list
149    // ============================================================  
150    f->ClearAnonymizeList();
151     
152    f->Delete();
153    fh->Delete();
154    return 0;
155 }
156