1 /*=========================================================================
4 Module: $RCSfile: AnonymizeDicomDir.cxx,v $
6 Date: $Date: 2005/06/07 11:12:10 $
7 Version: $Revision: 1.2 $
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.
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.
17 =========================================================================*/
18 #include "gdcmGlobal.h"
19 #include "gdcmCommon.h"
20 #include "gdcmDebug.h"
23 #include "gdcmSQItem.h"
24 #include "gdcmSeqEntry.h"
25 #include "gdcmValEntry.h"
27 #include "gdcmDocument.h"
30 #include "gdcmArgMgr.h"
34 void AnoNoLoad(gdcm::SQItem *s, std::fstream *fp,
35 uint16_t group, uint16_t elem,
38 void AnoNoLoad(gdcm::SQItem *s, std::fstream *fp,
39 uint16_t group, uint16_t elem,
49 d = s->GetDocEntry( group, elem);
54 if ( ! dynamic_cast<gdcm::ValEntry *>(d) )
57 offset = d->GetOffset();
58 lgth = d->GetLength();
61 spaces = new std::string( lgth-valLgth, ' ');
65 fp->seekp( offset, std::ios::beg );
66 fp->write( v.c_str(), lgth );
69 int main(int argc, char *argv[])
73 " \n AnonymizeDicomDir :\n",
74 " Anonymize a gdcm-readable DICOMDIR ",
75 " even when some 'Objects' are not yet taken into account",
76 " Warning : the DICOMDIR is overwritten",
77 " usage: AnonymizeDicomDir filein=dicomDirName [debug] ",
78 " debug : user wants to run the program in 'debug mode' ",
81 // ----- Initialize Arguments Manager ------
82 gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
84 if (am->ArgMgrDefined("usage"))
86 am->ArgMgrUsage(usage); // Display 'usage'
91 char *fileName = am->ArgMgrWantString("filein",usage);
93 delete am; // we don't need Argument Manager any longer
95 // ============================================================
96 // Read the input DICOMDIR
97 // ============================================================
99 gdcm::File *f1 = new gdcm::File( fileName );
100 if (!f1->IsReadable()) {
101 std::cerr << "Sorry, " << fileName <<" not a gdcm-readable "
102 << "file" <<std::endl;
104 std::cout << " ... is readable " << std::endl;
106 // Directory record sequence
107 gdcm::DocEntry *e = f1->GetDocEntry(0x0004, 0x1220);
110 std::cout << "No Directory Record Sequence (0004,1220) found" <<std::endl;;
116 gdcm::SeqEntry *s = dynamic_cast<gdcm::SeqEntry *>(e);
119 std::cout << "Element (0004,1220) is not a Sequence ?!?" <<std::endl;
125 // Open the file LTTG (aka ALAP)
126 std::fstream *fp = new std::fstream(fileName,
127 std::ios::in | std::ios::out | std::ios::binary);
131 int patientNumber = 0;
132 std::ostringstream oss;
134 gdcm::SQItem *tmpSI=s->GetFirstSQItem(); // For all the SQItems
137 d = tmpSI->GetDocEntry(0x0004, 0x1430); // Directory Record Type
138 if ( gdcm::ValEntry* valEntry = dynamic_cast<gdcm::ValEntry *>(d) )
140 v = valEntry->GetValue();
144 std::cout << "(0004,1430) not a ValEntry ?!?" << std::endl;
148 if( v != "PATIENT " )
150 continue; // Work only on PATIENT
153 oss << patientNumber;
155 // Overwrite the sensitive Entries
158 AnoNoLoad(tmpSI, fp, 0x0010, 0x0010, oss.str());
160 AnoNoLoad(tmpSI, fp, 0x0010, 0x0020, oss.str());
161 // Patient's Birth Date
162 AnoNoLoad(tmpSI, fp, 0x0010, 0x0030, oss.str());
164 AnoNoLoad(tmpSI, fp, 0x0010, 0x2154, oss.str());
166 // Aware use will add more Entries he wants to rubb out here
170 tmpSI=s->GetNextSQItem();
173 // Close the file ASAP