]> Creatis software - gdcm.git/blob - Example/AnonymizeDicomDir.cxx
Begin of kosherization of Example
[gdcm.git] / Example / AnonymizeDicomDir.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: AnonymizeDicomDir.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 "gdcmGlobal.h"
19 #include "gdcmCommon.h"
20 #include "gdcmDebug.h"
21 #include "gdcmUtil.h"
22
23 #include "gdcmSQItem.h"
24 #include "gdcmSeqEntry.h"
25 #include "gdcmValEntry.h"
26
27 #include "gdcmDocument.h"
28 #include "gdcmFile.h"
29
30 #include "gdcmArgMgr.h"
31
32 #include <iostream>
33
34 /**
35  * \brief AnonymizeDicomDir
36  */
37
38 void AnoNoLoad(gdcm::SQItem *s, std::fstream *fp, 
39                uint16_t group, uint16_t elem, 
40                std::string val);
41
42 void AnoNoLoad(gdcm::SQItem *s, std::fstream *fp, 
43                uint16_t group, uint16_t elem, 
44                std::string val)
45 {
46    gdcm::DocEntry *d;
47    uint32_t offset;
48    uint32_t lgth;
49    uint32_t valLgth = 0;
50    std::string *spaces;
51    std::string v;
52
53    d = s->GetDocEntry( group, elem);
54
55    if ( d == NULL)
56       return;
57
58    if ( ! dynamic_cast<gdcm::ValEntry *>(d) )
59       return;
60
61    offset = d->GetOffset();
62    lgth =   d->GetLength();
63    if (valLgth < lgth)
64    {
65       spaces = new std::string( lgth-valLgth, ' ');
66       v = val + *spaces;
67       delete spaces;
68    }
69    fp->seekp( offset, std::ios::beg );
70    fp->write( v.c_str(), lgth );
71 }
72
73 int main(int argc, char *argv[])
74
75
76    START_USAGE(usage)
77    " \n AnonymizeDicomDir :\n",
78    " Anonymize a gdcm-readable DICOMDIR ",
79    "           even when some 'Objects' are not yet taken into account",
80    "           Warning : the DICOMDIR is overwritten",
81    " usage: AnonymizeDicomDir filein=dicomDirName [debug] ",
82    "        debug    : user wants to run the program in 'debug mode' ",
83    FINISH_USAGE
84
85    // ----- Initialize Arguments Manager ------   
86    gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
87   
88    if (am->ArgMgrDefined("usage")) 
89    {
90       am->ArgMgrUsage(usage); // Display 'usage'
91       delete am;
92       return 0;
93    }
94  
95    char *fileName  = am->ArgMgrWantString("filein",usage); 
96
97    delete am;  // we don't need Argument Manager any longer
98
99 // ============================================================
100 //   Read the input DICOMDIR
101 // ============================================================
102
103    gdcm::File *f;
104    f = new gdcm::File( );
105    f->SetLoadMode(0);
106    f->SetFileName( fileName );
107    bool res = f->Load();  
108    if ( !res )
109    {
110        std::cerr << "Sorry, " << fileName <<"  not a gdcm-readable "
111                  << "file" <<std::endl;
112    }
113    std::cout << " ... is readable " << std::endl;
114
115    // Directory record sequence
116    gdcm::DocEntry *e = f->GetDocEntry(0x0004, 0x1220);
117    if ( !e )
118    {
119       std::cout << "No Directory Record Sequence (0004,1220) found" <<std::endl;;
120       delete f;
121       delete e;
122       return 0;         
123    }
124    
125    gdcm::SeqEntry *s = dynamic_cast<gdcm::SeqEntry *>(e);
126    if ( !s )
127    {
128       std::cout << "Element (0004,1220) is not a Sequence ?!?" <<std::endl;
129       delete f;
130       delete e;
131       return 0;
132    }
133
134    // Open the file LTTG (aka ALAP)
135    std::fstream *fp = new std::fstream(fileName, 
136                               std::ios::in | std::ios::out | std::ios::binary);
137    gdcm::DocEntry *d;
138    std::string v;
139
140    int patientNumber = 0;
141    std::ostringstream oss;
142
143    gdcm::SQItem *tmpSI=s->GetFirstSQItem();  // For all the SQItems
144    while(tmpSI)
145    {
146       d = tmpSI->GetDocEntry(0x0004, 0x1430); // Directory Record Type
147       if ( gdcm::ValEntry* valEntry = dynamic_cast<gdcm::ValEntry *>(d) )
148       {
149          v = valEntry->GetValue();
150       }
151       else
152       {
153          std::cout << "(0004,1430) not a ValEntry ?!?" << std::endl;
154          continue;
155       }
156
157       if( v != "PATIENT " )
158       {
159          continue;          // Work only on PATIENT
160       }
161
162       oss << patientNumber;      
163
164       //   Overwrite the sensitive Entries
165
166       // Patient's Name
167       AnoNoLoad(tmpSI, fp, 0x0010, 0x0010, oss.str());
168       // Patient's ID
169       AnoNoLoad(tmpSI, fp, 0x0010, 0x0020, oss.str());
170       // Patient's Birth Date
171       AnoNoLoad(tmpSI, fp, 0x0010, 0x0030, oss.str());
172      // Telephone
173       AnoNoLoad(tmpSI, fp, 0x0010, 0x2154, oss.str()); 
174
175     // Aware use will add more Entries he wants to rubb out here
176
177       oss << "";
178       patientNumber++;
179       tmpSI=s->GetNextSQItem();
180    }
181
182    // Close the file ASAP
183
184    fp->close();
185
186    delete fp;
187    delete e;   
188    delete f;
189    return 0;
190 }
191