fw4spl
DicomSeriesAnonymizer.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwGdcmIO/helper/DicomSeriesAnonymizer.hpp"
8 
9 #include <fwCore/base.hpp>
10 
11 #include <fwJobs/Aggregator.hpp>
12 #include <fwJobs/IJob.hpp>
13 #include <fwJobs/Job.hpp>
14 #include <fwJobs/Observer.hpp>
15 
16 #include <fwMedData/SeriesDB.hpp>
17 
18 #include <fwTools/System.hpp>
19 
20 #include <boost/foreach.hpp>
21 
22 namespace fwGdcmIO
23 {
24 namespace helper
25 {
26 
28  m_job(::fwJobs::Aggregator::New("Anonymization process"))
29 {
30  m_writer = ::fwGdcmIO::helper::DicomSeriesWriter::New();
31  m_reader = ::fwGdcmIO::reader::SeriesDB::New();
32 }
33 
34 //------------------------------------------------------------------------------
35 
37 {
38 }
39 
40 //------------------------------------------------------------------------------
41 
42 void DicomSeriesAnonymizer::anonymize(const ::fwMedData::DicomSeries::sptr& source)
43 {
44  this->anonymize(source, source);
45 }
46 
47 //------------------------------------------------------------------------------
48 
49 void DicomSeriesAnonymizer::anonymize(const ::fwMedData::DicomSeries::sptr& source,
50  const ::fwMedData::DicomSeries::sptr& destination)
51 {
52  auto writerObserver = m_writer->getJob();
53  auto anonymizerObserver = m_anonymizer.getJob();
54  auto readerObserver = m_reader->getJob();
55 
56  // Set up observer cancel callback
57  m_job->addSimpleCancelHook([&]
58  {
59  writerObserver->cancel();
60  anonymizerObserver->cancel();
61  readerObserver->cancel();
62  });
63 
64  m_job->add(writerObserver);
65  m_job->add(anonymizerObserver, 10);
66  m_job->add(readerObserver);
67 
68  const auto future = m_job->run();
69 
70  // Create destination directory
71  const ::boost::filesystem::path destPath = ::fwTools::System::getTemporaryFolder("AnonymizedSeries");
72  ::boost::filesystem::create_directories( destPath );
73 
74  // Write DicomSeries (Copy files)
75  m_writer->setObject(source);
76  ::fwData::location::Folder::sptr loc = ::fwData::location::Folder::New();
77  loc->setFolder(destPath);
78  m_writer->setLocation(loc);
79  m_writer->write();
80 
81  if(m_job->cancelRequested())
82  {
83  return;
84  }
85 
86  // Anonymize directory
87  m_anonymizer.anonymize(destPath);
88 
89  if(m_job->cancelRequested())
90  {
91  return;
92  }
93 
94  // Read anonymized series
95  ::fwMedData::SeriesDB::sptr seriesDB = ::fwMedData::SeriesDB::New();
96  m_reader->setObject(seriesDB);
97  m_reader->setFolder(destPath);
98  m_reader->readDicomSeries();
99 
100  if(m_job->cancelRequested())
101  {
102  return;
103  }
104 
105  // Wait for the aggregator to finish and catch exceptions
106  future.get();
107 
108  // Update DicomSeries
109  ::fwMedData::DicomSeries::sptr anonymizedSeries =
110  ::fwMedData::DicomSeries::dynamicCast(seriesDB->getContainer().front());
111  destination->deepCopy(anonymizedSeries);
112 }
113 
114 //------------------------------------------------------------------------------
115 
117 {
118  return m_job;
119 }
120 
121 //------------------------------------------------------------------------------
122 
123 } // namespace helper
124 } // namespace fwGdcmIO
FWGDCMIO_API DicomSeriesAnonymizer()
Constructor.
::fwGdcmIO::helper::DicomSeriesWriter::sptr m_writer
Dicom writer.
FWGDCMIO_API std::shared_ptr< ::fwJobs::Aggregator > getJob() const
Get job observer.
The namespace fwGdcmIO contains reader, writer and helper for dicom data.
std::shared_ptr< ::fwJobs::Aggregator > sptr
Aggregator container type.
Definition: Aggregator.hpp:39
std::shared_ptr< ::fwJobs::Aggregator > m_job
Job observer.
virtual FWGDCMIO_API ~DicomSeriesAnonymizer()
Destructor.
::fwGdcmIO::helper::DicomAnonymizer m_anonymizer
Dicom anonymizer.
static FWTOOLS_APIconst::boost::filesystem::path getTemporaryFolder(const std::string &subFolderPrefix="") noexcept
Returns a unique per-process temporary folder. The top level temporary folder will be automatically d...
Definition: System.cpp:148
FWGDCMIO_API std::shared_ptr< ::fwJobs::IJob > getJob() const
Get job observer.
This namespace fwJobs provides jobs management.
FWGDCMIO_API void anonymize(const ::fwMedData::DicomSeries::sptr &source)
Anonymize the DicomSeries (modify the current object)
::fwGdcmIO::reader::SeriesDB::sptr m_reader
Dicom Reader.
FWGDCMIO_API void anonymize(const ::boost::filesystem::path &dirPath)
Anonymize a folder containing Dicom files.