7 #include "vtkGdcmIO/ImageSeriesWriter.hpp" 9 #include <fwCore/base.hpp> 11 #include <fwDataIO/writer/registry/macros.hpp> 13 #include <fwJobs/IJob.hpp> 14 #include <fwJobs/Observer.hpp> 16 #include <fwMedData/Equipment.hpp> 17 #include <fwMedData/ImageSeries.hpp> 18 #include <fwMedData/Patient.hpp> 19 #include <fwMedData/Study.hpp> 21 #include <fwTools/dateAndTime.hpp> 22 #include <fwTools/Dispatcher.hpp> 23 #include <fwTools/DynamicTypeKeyTypeMapping.hpp> 24 #include <fwTools/IntrinsicTypes.hpp> 26 #include <fwVtkIO/helper/vtkLambdaCommand.hpp> 27 #include <fwVtkIO/vtk.hpp> 29 #include <boost/filesystem.hpp> 31 #include <gdcmDicts.h> 32 #include <gdcmFilenameGenerator.h> 33 #include <gdcmGlobal.h> 35 #include <vtkGDCMImageWriter.h> 36 #include <vtkImageData.h> 37 #include <vtkMedicalImageProperties.h> 38 #include <vtkSmartPointer.h> 39 #include <vtkStringArray.h> 50 void setValue(vtkMedicalImageProperties* medprop,
51 const std::uint16_t group,
52 const std::uint16_t element,
53 const std::string& value)
55 const gdcm::Global& g = gdcm::Global::GetInstance();
56 const gdcm::Dicts& ds = g.GetDicts();
58 const gdcm::Tag tag(group, element);
59 const gdcm::DictEntry& dicEntry = ds.GetDictEntry( tag );
60 medprop->AddUserDefinedValue( dicEntry.GetName(), value.c_str() );
66 ::
fwData::location::enableFolder< ::
fwDataIO::writer::IObjectWriter >(this),
67 m_job(::
fwJobs::Observer::New(
"VTK Image writer")),
68 m_compressionTypes(CompressionTypes::RAW)
77 ::fwData::Image::sptr dataImage = imgSeries->getImage();
78 ::fwMedData::Patient::sptr patient = imgSeries->getPatient();
79 ::fwMedData::Study::sptr study = imgSeries->getStudy();
80 ::fwMedData::Equipment::sptr equipment = imgSeries->getEquipment();
83 vtkSmartPointer< vtkImageData > vtkImage = vtkSmartPointer< vtkImageData >::New();
84 ::fwVtkIO::toVTKImage( dataImage, vtkImage );
86 ::boost::filesystem::path outputDirectory = this->
getFolder();
87 FW_RAISE_IF(
"'" << outputDirectory <<
"' is not a directory.",
88 !::boost::filesystem::is_directory( outputDirectory ) );
91 const std::string gdcmfile = ( outputDirectory /
"image_" ).
string() +
"%01d.dcm";
92 ::gdcm::FilenameGenerator fg;
93 fg.SetPattern( gdcmfile.c_str() );
94 int nfiles = vtkImage->GetDimensions()[2];
95 fg.SetNumberOfFilenames( nfiles );
97 bool isGenerated = fg.Generate();
98 FW_RAISE_IF(
"Filename generation failed.", !isGenerated);
100 auto filenames = vtkSmartPointer<vtkStringArray>::New();
101 for(
unsigned int i = 0; i < fg.GetNumberOfFilenames(); ++i)
103 filenames->InsertNextValue( fg.GetFilename(i) );
107 auto medprop = vtkSmartPointer<vtkMedicalImageProperties>::New();
111 medprop->SetPatientName(patient->getName().c_str());
115 medprop->SetPatientSex(patient->getSex().c_str());
119 std::string modality = imgSeries->getModality();
124 medprop->SetModality(modality.c_str());
128 medprop->SetStudyDescription(study->getDescription().c_str());
129 medprop->SetSeriesDescription(imgSeries->getDescription().c_str());
133 medprop->SetPatientID(patient->getPatientId().c_str());
137 medprop->SetPatientBirthDate(patient->getBirthdate().c_str());
141 medprop->SetInstitutionName(equipment->getInstitutionName().c_str());
147 medprop->SetPatientAge(study->getPatientAge().c_str());
151 medprop->SetStudyDate(study->getDate().c_str());
156 setValue(medprop, 0x0008, 0x0030, study->getTime());
159 setValue(medprop, 0x0008, 0x0021, imgSeries->getDate());
162 setValue(medprop, 0x0008, 0x0031, imgSeries->getTime());
164 ::fwMedData::DicomValuesType performingPhysicians = imgSeries->getPerformingPhysiciansName();
165 std::stringstream physicians;
166 if (!performingPhysicians.empty())
168 ::fwMedData::DicomValuesType::const_iterator itr = performingPhysicians.begin();
170 physicians << *itr++;
171 for (; itr < performingPhysicians.end(); ++itr)
173 physicians <<
"\\" << *itr;
177 setValue(medprop, 0x0008, 0x1050, physicians.str());
179 setValue(medprop, 0x0008, 0x0090, study->getReferringPhysicianName());
184 medprop->AddWindowLevelPreset( dataImage->getWindowWidth(), dataImage->getWindowCenter() );
189 std::vector<double> spacing = dataImage->getSpacing();
190 value = ::fwTools::getString< double >(spacing[0]);
192 value += ::fwTools::getString< double >(spacing[1]);
193 setValue(medprop, 0x0028, 0x0030, value);
197 origin = ::fwTools::getString< ::fwData::Image::OriginType::value_type >(orginVec[0]);
199 origin = ::fwTools::getString< ::fwData::Image::OriginType::value_type >(orginVec[1]);
201 if (dataImage->getNumberOfDimensions() > 2)
205 value = ::fwTools::getString< double >(spacing[2]);
206 medprop->SetSliceThickness(value.c_str());
210 value = ::fwTools::getString< double >(spacing[2]);
211 setValue(medprop, 0x0018, 0x0088, value);
214 origin = ::fwTools::getString< ::fwData::Image::OriginType::value_type >(orginVec[2]);
217 setValue(medprop, 0x0020, 0x0032, origin);
221 vtkSmartPointer< vtkGDCMImageWriter > writer = vtkSmartPointer< vtkGDCMImageWriter >::New();
224 writer->SetStudyUID(study->getInstanceUID().c_str());
225 writer->SetSeriesUID(imgSeries->getInstanceUID().c_str());
226 writer->SetInputData( vtkImage );
227 writer->FileLowerLeftOn();
228 writer->SetFileDimensionality( 2 );
229 writer->SetMedicalImageProperties( medprop );
230 writer->SetFileNames( filenames );
231 writer->SetCompressionType(static_cast<int>(m_compressionTypes));
234 vtkSmartPointer<vtkLambdaCommand> progressCallback;
235 progressCallback = vtkSmartPointer<vtkLambdaCommand>::New();
236 progressCallback->SetCallback([
this](vtkObject* caller,
long unsigned int,
void* )
238 auto filter =
static_cast<vtkGDCMImageWriter*
>(caller);
239 m_job->doneWork( filter->GetProgress()*100 );
241 writer->AddObserver(vtkCommand::ProgressEvent, progressCallback);
243 m_job->addSimpleCancelHook( [&]()
245 writer->AbortExecuteOn();
ILocation::PathType getFolder()
Get folder filesystem path.
VTKGDCMIO_API ImageSeriesWriter(::fwDataIO::writer::IObjectWriter::Key key)
Constructor. Does nothing.
This namespace fwDataIO contains reader and writer for several framework's data.
virtual std::shared_ptr< const DataType > getConcreteObject() const
m_object getter.
VTKGDCMIO_API std::shared_ptr< ::fwJobs::IJob > getJob() const override
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
vtkmGdcm reader/writer lib
Write an ImageSeries in DICOM format.
std::shared_ptr< ::fwJobs::IJob > sptr
Cancel request callback type.
Contains the representation of the data objects used in the framework.
VTKGDCMIO_API std::string extension() override
Return an empty string.
VTKGDCMIO_API void write() override
Write the image series in DICOM format.
This namespace fwJobs provides jobs management.
std::vector< double > OriginType
Image origin type.