1 /*=========================================================================
4 Module: $RCSfile: AnonymizeDicomDir.cxx,v $
6 Date: $Date: 2005/07/12 14:56:48 $
7 Version: $Revision: 1.6 $
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"
35 * \brief AnonymizeDicomDir
38 void AnoNoLoad(gdcm::SQItem *s, std::fstream *fp,
39 uint16_t group, uint16_t elem,
42 void AnoNoLoad(gdcm::SQItem *s, std::fstream *fp,
43 uint16_t group, uint16_t elem,
53 d = s->GetDocEntry( group, elem);
58 if ( ! dynamic_cast<gdcm::ValEntry *>(d) )
61 offset = d->GetOffset();
62 lgth = d->GetLength();
65 spaces = new std::string( lgth-valLgth, ' ');
69 fp->seekp( offset, std::ios::beg );
70 fp->write( v.c_str(), lgth );
74 int main(int argc, char *argv[])
78 " \n AnonymizeDicomDir :\n",
79 " Anonymize a gdcm-readable DICOMDIR ",
80 " even when some 'Objects' are not yet taken into account",
81 " Warning : the DICOMDIR is overwritten",
82 " usage: AnonymizeDicomDir filein=dicomDirName [debug] ",
83 " debug : user wants to run the program in 'debug mode' ",
86 // ----- Initialize Arguments Manager ------
87 gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
89 if (am->ArgMgrDefined("usage"))
91 am->ArgMgrUsage(usage); // Display 'usage'
96 char *fileName = am->ArgMgrWantString("filein",usage);
98 delete am; // --- we don't need Argument Manager any longer ---
101 // Read the input DICOMDIR
103 f = new gdcm::File( );
105 f->SetFileName( fileName );
106 bool res = f->Load();
109 std::cerr << "Sorry, " << fileName <<" not a gdcm-readable "
110 << "file" <<std::endl;
112 std::cout << " ... is readable " << std::endl;
114 // Look for Directory record sequence
115 gdcm::DocEntry *e = f->GetDocEntry(0x0004, 0x1220);
118 std::cout << "No Directory Record Sequence (0004,1220) found" <<std::endl;;
123 gdcm::SeqEntry *s = dynamic_cast<gdcm::SeqEntry *>(e);
126 std::cout << "Element (0004,1220) is not a Sequence ?!?" <<std::endl;
131 // Open the file LTTG (aka ALAP)
132 std::fstream *fp = new std::fstream(fileName,
133 std::ios::in | std::ios::out | std::ios::binary);
137 int patientNumber = 0;
138 std::ostringstream oss;
140 gdcm::SQItem *tmpSI=s->GetFirstSQItem(); // For all the SQItems
143 d = tmpSI->GetDocEntry(0x0004, 0x1430); // Directory Record Type
144 if ( gdcm::ValEntry* valEntry = dynamic_cast<gdcm::ValEntry *>(d) )
146 v = valEntry->GetValue();
150 std::cout << "(0004,1430) not a ValEntry ?!?" << std::endl;
154 if( v != "PATIENT " ) // Work only on PATIENT
156 tmpSI=s->GetNextSQItem();
160 oss << patientNumber;
162 // Overwrite the sensitive Entries
165 AnoNoLoad(tmpSI, fp, 0x0010, 0x0010, oss.str());
167 AnoNoLoad(tmpSI, fp, 0x0010, 0x0020, oss.str());
168 // Patient's Birth Date
169 AnoNoLoad(tmpSI, fp, 0x0010, 0x0030, oss.str());
171 AnoNoLoad(tmpSI, fp, 0x0010, 0x2154, oss.str());
173 // Aware use will add here more Entries if he wants to rubb them out
177 tmpSI=s->GetNextSQItem();
180 // Close the file ASAP