1 /*=========================================================================
4 Module: $RCSfile: AnonymizeDicomDir.cxx,v $
6 Date: $Date: 2005/03/09 19:15:04 $
7 Version: $Revision: 1.1 $
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"
32 void AnoNoLoad(gdcm::SQItem *s, std::fstream *fp,
33 uint16_t group, uint16_t elem,
36 void AnoNoLoad(gdcm::SQItem *s, std::fstream *fp,
37 uint16_t group, uint16_t elem,
47 d = s->GetDocEntry( group, elem);
52 if ( ! dynamic_cast<gdcm::ValEntry *>(d) )
55 offset = d->GetOffset();
56 lgth = d->GetLength();
59 spaces = new std::string( lgth-valLgth, ' ');
63 fp->seekp( offset, std::ios::beg );
64 fp->write( v.c_str(), lgth );
67 int main(int argc, char *argv[])
71 gdcm::Debug::DebugOn();
72 std::cout << "------------------------------------------------" << std::endl;
73 std::cout << "Anonymize a gdcm-readable DICOMDIR " << std::endl;
74 std::cout << "even some Objects are not yet taken into account" << std::endl;
75 std::cout << "Warning : the DICOMDIR is overwritten" << std::endl;
76 std::cout << " : to preserve file integrity "
77 << " think unto using a copy .. " << std::endl;
81 std::cerr << "Usage " << argv[0] << " DICOMDIR to anonymize "
86 std::string fileName = argv[1];
88 // ============================================================
89 // Read the input DICOMDIR
90 // ============================================================
92 std::cout << argv[1] << std::endl;
94 f1 = new gdcm::File( fileName );
95 if (!f1->IsReadable()) {
96 std::cerr << "Sorry, " << fileName <<" not a gdcm-readable "
97 << "file" <<std::endl;
99 std::cout << " ... is readable " << std::endl;
102 // Directory record sequence
103 gdcm::DocEntry *e = f1->GetDocEntry(0x0004, 0x1220);
106 std::cout << "No Directory Record Sequence (0004,1220) found" <<std::endl;;
110 gdcm::SeqEntry *s = dynamic_cast<gdcm::SeqEntry *>(e);
113 std::cout << "Element (0004,1220) is not a Sequence ?!?" <<std::endl;
117 // Open the file LTTG (aka ALAP)
118 std::fstream *fp = new std::fstream(fileName.c_str(),
119 std::ios::in | std::ios::out | std::ios::binary);
123 int patientNumber = 0;
124 std::ostringstream oss;
126 gdcm::SQItem *tmpSI=s->GetFirstSQItem(); // For all the SQItems
129 d = tmpSI->GetDocEntry(0x0004, 0x1430); // Directory Record Type
130 if ( gdcm::ValEntry* valEntry = dynamic_cast<gdcm::ValEntry *>(d) )
132 v = valEntry->GetValue();
136 std::cout << "(0004,1430) not a ValEntry ?!?" << std::endl;
140 if( v != "PATIENT " )
142 continue; // Work only on PATIENT
145 oss << patientNumber;
147 // Overwrite the sensitive Entries
150 AnoNoLoad(tmpSI, fp, 0x0010, 0x0010, oss.str());
152 AnoNoLoad(tmpSI, fp, 0x0010, 0x0020, oss.str());
153 // Patient's Birth Date
154 AnoNoLoad(tmpSI, fp, 0x0010, 0x0030, oss.str());
156 AnoNoLoad(tmpSI, fp, 0x0010, 0x2154, oss.str());
158 // Aware use will add more Entries he wants to rubb out here
162 tmpSI=s->GetNextSQItem();
165 // Close the file ASAP