/*=========================================================================
-
+
Program: gdcm
Module: $RCSfile: AnonymizeReWriteMultiPatient.cxx,v $
Language: C++
- Date: $Date: 2007/11/21 11:52:22 $
- Version: $Revision: 1.1 $
+ Date: $Date: 2008/03/18 13:37:54 $
+ Version: $Revision: 1.4 $
Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
l'Image). All rights reserved. See Doc/License.txt or
" Warning : the image is OVERWRITTEN ",
" to preserve image integrity, use a copy. ",
" usage: AnonymizeReWriteMultiPatient dirin=inputDirectoryName dicomdir ",
+ " dirin : directory holding images to anonymize ",
+ " rootname : root for the 'anonymized' name ",
" listOfElementsToRubOut : group-elem,g2-e2,... (in hexa, no space) ",
" of extra Elements to rub out ",
" dicomdir : user wants to generate a DICOMDIR ",
bool verbose = ( 0 != am->ArgMgrDefined("verbose") );
bool dicomdir = ( 0 != am->ArgMgrDefined("dicomdir") );
+ const char *rootname = am->ArgMgrGetString("rootname","Patient");
+
if (am->ArgMgrDefined("debug"))
- GDCM_NAME_SPACE::Debug::DebugOn();
+ GDCM_NAME_SPACE::Debug::DebugOn();
int loadMode = GDCM_NAME_SPACE::LD_ALL;
std::string patID;
GDCM_NAME_SPACE::File *f;
+
+
+ //GDCM_NAME_SPACE::Debug::DebugOn();
+
int sequentialPatientNumber = 0;
char char_sequentialPatientNumber[10]; // 999999999 patients in a directory should be enough?
- pa = dcmdir->GetFirstPatient();
+ pa = dcmdir->GetFirstPatient();
while ( pa )
{ // on degouline les PATIENT du DICOMDIR
//codedName = "g^" + GDCM_NAME_SPACE::Util::ConvertToMD5(patName);
- codedName = "g^Patient" + char_sequentialPatientNumber;
+ codedName = "g^" + std::string(rootname) + std::string(char_sequentialPatientNumber);
patID = pa->GetEntryString(0x0010, 0x0020);
codedID = GDCM_NAME_SPACE::Util::ConvertToMD5(patID);
while ( im )
{ // on degouline les Images de cette serie
fullFileName = dirName;
- fullFileName += GDCM_NAME_SPACE::GDCM_FILESEPARATOR;
+ const char lastChar = dirName.c_str()[strlen(dirName.c_str())-1];
+ if ( lastChar != '/' && lastChar != '\\')
+ fullFileName += GDCM_NAME_SPACE::GDCM_FILESEPARATOR;
fullFileName += im->GetEntryString(0x0004, 0x1500);
+
+ // -- remove trailing space, if any
+ int pos=fullFileName.length()-1;
+ if (fullFileName[pos] == ' ')
+ {
+ fullFileName.erase(pos);
+ }
+ // --
+
if (verbose)
- std::cout << "FileName " << fullFileName << std::endl;
+ std::cout << "FileName [" << fullFileName << "]" << std::endl;
f = GDCM_NAME_SPACE::File::New( );
- f->SetMaxSizeLoadEntry(0x7fff); // we want to load entries of any length !
- f->SetLoadMode(loadMode);
- f->SetFileName( fullFileName );
+ f->SetMaxSizeLoadEntry(0x7fff); // we want to load entries of any length !
+ f->SetLoadMode(loadMode);
+ f->SetFileName( fullFileName );
if ( !f->Load() )
- std::cout << "Load failed for [" << fullFileName << "]" << std::endl;
+ {
+ std::cout << "Load failed for [" << fullFileName << "]" << std::endl;
+ //f->Delete();
+ //continue; // or return 0; ?
+ }
else
+ {
if (verbose)
std::cout << "Load successed for [" << fullFileName << "]" << std::endl;
-
+ }
//
// Choose the fields to anonymize.
//
// Load the pixels in memory.
// ============================================================
- // We need a gdcm::FileHelper, since we want to load the pixels
- GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
-
- // unit8_t DOESN'T mean it's mandatory for the image to be a 8 bits one !
- // Feel free to cast if you know it's not.
+ // We need a gdcm::FileHelper, since we want to load the pixels
+ GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
- uint8_t *imageData = fh->GetImageData();
+ // unit8_t DOESN'T mean it's mandatory for the image to be a 8 bits one !
+ // Feel free to cast if you know it's not.
- if ( imageData == 0 )
- {
- std::cerr << "Sorry, Pixels of" << fileName <<" are not "
- << " gdcm-readable." << std::endl
- << "Use exAnonymizeNoLoad" << std::endl;
- f->Delete();
- fh->Delete();
- return 0;
- }
-
-
- f->Anonymize();
+ uint8_t *imageData = fh->GetImageData();
+ if ( imageData == 0 )
+ {
+ std::cerr << "Sorry, Pixels of [" << fullFileName <<"] are not "
+ << " gdcm-readable." << std::endl
+ << "Use exAnonymizeNoLoad" << std::endl;
+ f->Delete();
+ fh->Delete();
+ return 0;
+ }
+
+ // ============================================================
+ f->AnonymizeFile();
f->ClearAnonymizeList();
+
+ fh->SetContentType(GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE);
+ fh->Write(fullFileName); // WARNING : overwrites the file!
+
+ im = se->GetNextImage();
f->Delete();
-
- im = se->GetNextImage();
+ fh->Delete();
}
se = st->GetNextSerie();
}
st = pa->GetNextStudy();
}
- pa = dcmdir->GetNextPatient();
-
- f->Delete();
- fh->Delete();
+ pa = dcmdir->GetNextPatient();
}
dcmdir->Delete();