]> Creatis software - gdcm.git/blob - Example/Anonymize.cxx
Fix mistypings
[gdcm.git] / Example / Anonymize.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: Anonymize.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/06/15 13:18:50 $
7   Version:   $Revision: 1.13 $
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
28 /// \todo : AnonymizeDirectory
29 ///         That should split the images : Patient/Study/Serie
30 ///         and keeps coherent the StudyInstanceUID, SeriesInstanceUID
31 ///         (Now, a new one is generated fore each image :-( )
32 int main(int argc, char *argv[])
33 {
34    START_USAGE(usage)
35    " \n Anonymize :\n                                                         ",
36    " Anonymize a full gdcm-readable Dicom image                               ",
37    "          Warning : probably segfaults if pixels are not gdcm readable.   ",
38    "                    Use AnonymizeNoLoad instead.                          ",
39    " usage: Anonymize filein=inputFileName fileout=anonymizedFileName[debug]  ",
40    "        debug    : user wants to run the program in 'debug mode'          ",
41    FINISH_USAGE
42
43    // ----- Initialize Arguments Manager ------   
44    GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
45   
46    if (argc == 1 || am->ArgMgrDefined("usage")) 
47    {
48       am->ArgMgrUsage(usage); // Display 'usage'
49       delete am;
50       return 0;
51    }
52    char *fileName = am->ArgMgrWantString("filein",usage);
53    if ( fileName == NULL )
54    {
55       delete am;
56       return 0;
57    }
58
59    char *outputFileName = am->ArgMgrWantString("fileout",usage);
60    if ( outputFileName == NULL )
61    {
62       delete am;
63       return 0;
64    }
65    if (am->ArgMgrDefined("debug"))
66       GDCM_NAME_SPACE::Debug::DebugOn();
67  
68    // if unused Param we give up
69    if ( am->ArgMgrPrintUnusedLabels() )
70    { 
71       am->ArgMgrUsage(usage);
72       delete am;
73       return 0;
74    }
75
76    delete am;  // we don't need Argument Manager any longer
77
78    // ============================================================
79    //   Read the input file.
80    // ============================================================
81
82    GDCM_NAME_SPACE::File *f;
83
84    f = GDCM_NAME_SPACE::File::New(  );
85    f->SetLoadMode( GDCM_NAME_SPACE::LD_ALL );
86    f->SetFileName( fileName );
87    int res = f->Load();
88
89    if ( !res ) 
90    {
91        std::cerr << "Sorry, " << fileName <<"  not a gdcm-readable "
92                  << "DICOM / ACR File" <<std::endl;
93        f->Delete();
94        return 0;
95    }
96    std::cout << " ... is readable " << std::endl;
97
98    // ============================================================
99    //   Load the pixels in memory.
100    // ============================================================
101
102    // We need a gdcm::FileHelper, since we want to load the pixels        
103    GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
104
105    // unit8_t DOESN'T mean it's mandatory for the image to be a 8 bits one !
106    // Feel free to cast if you know it's not. 
107
108    uint8_t *imageData = fh->GetImageData();
109
110    if ( imageData == 0 )
111    {
112        std::cerr << "Sorry, Pixels of" << fileName <<"  are not "
113                  << " gdcm-readable."       << std::endl
114                  << "Use exAnonymizeNoLoad" << std::endl;
115        f->Delete();
116        fh->Delete();
117        return 0;
118    } 
119
120    // ============================================================
121    //  Choose the fields to anonymize.
122    // ============================================================
123    // Institution name 
124    f->AddAnonymizeElement(0x0008, 0x0080, "Xanadoo"); 
125    // Patient's name 
126    f->AddAnonymizeElement(0x0010, 0x0010, "Fantomas^X");   
127    // Patient's ID
128    f->AddAnonymizeElement( 0x0010, 0x0020,"1515" );   
129    // Study Instance UID
130    f->AddAnonymizeElement(0x0020, 0x000d, "9.99.999.9999" );
131    // Telephone
132    f->AddAnonymizeElement(0x0010, 0x2154, "3615" );
133
134    // Aware user will add here more fields to anonymize here
135
136    // The gdcm::File is modified in memory
137
138    f->AnonymizeFile();
139
140    // ============================================================
141    //   Write a new file
142    // ============================================================
143    
144    // Since we just Anonymized the file, we know that no modification 
145    // was performed on the pixels.
146    // The written image will not appear as a 'Secondary Captured image'
147    // nor as a DERIVED one  
148
149    fh->SetContentType(GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE);
150    
151    fh->WriteDcmExplVR(outputFileName);
152    std::cout <<"End Anonymize" << std::cout;
153
154    // ============================================================
155    //   Remove the Anonymize list
156    // ============================================================  
157    f->ClearAnonymizeList();
158     
159    f->Delete();
160    fh->Delete();
161    return 0;
162 }
163