fw4spl
writer/iod/CTMRImageIOD.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/writer/iod/CTMRImageIOD.hpp"
8 
9 #include "fwGdcmIO/helper/FileWriter.hpp"
10 #include "fwGdcmIO/writer/ie/Equipment.hpp"
11 #include "fwGdcmIO/writer/ie/FrameOfReference.hpp"
12 #include "fwGdcmIO/writer/ie/Image.hpp"
13 #include "fwGdcmIO/writer/ie/Patient.hpp"
14 #include "fwGdcmIO/writer/ie/Series.hpp"
15 #include "fwGdcmIO/writer/ie/Study.hpp"
16 
17 #include <fwCore/spyLog.hpp>
18 
19 #include <fwData/Image.hpp>
20 
21 #include <fwMedData/Equipment.hpp>
22 #include <fwMedData/ImageSeries.hpp>
23 #include <fwMedData/Patient.hpp>
24 #include <fwMedData/Study.hpp>
25 
26 #include <boost/make_shared.hpp>
27 
28 #include <gdcmImageWriter.h>
29 
30 namespace fwGdcmIO
31 {
32 namespace writer
33 {
34 namespace iod
35 {
36 
37 //------------------------------------------------------------------------------
38 
40  const ::boost::filesystem::path& destinationPath,
41  const ::fwLog::Logger::sptr& logger,
42  ProgressCallback progress,
43  CancelRequestedCallback cancel) :
44  ::fwGdcmIO::writer::iod::InformationObjectDefinition(instance, destinationPath, logger,
45  progress, cancel)
46 {
47 }
48 
49 //------------------------------------------------------------------------------
50 
52 {
53 }
54 
55 //------------------------------------------------------------------------------
56 
57 void CTMRImageIOD::write(const ::fwMedData::Series::csptr& series)
58 {
59  // Retrieve image series
60  ::fwMedData::ImageSeries::csptr imageSeries = ::fwMedData::ImageSeries::dynamicCast(series);
61  SLM_ASSERT("Image series should not be null.", imageSeries);
62 
63  // Retrieve image
64  ::fwData::Image::sptr image = imageSeries->getImage();
65 
66  // Create writer
67  SPTR(::gdcm::ImageWriter) writer = std::make_shared< ::gdcm::ImageWriter >();
68 
69  // Create Information Entity helpers
70  ::fwGdcmIO::writer::ie::Patient patientIE(writer, m_instance, series->getPatient());
71  ::fwGdcmIO::writer::ie::Study studyIE(writer, m_instance, series->getStudy());
72  ::fwGdcmIO::writer::ie::Series seriesIE(writer, m_instance, series);
73  ::fwGdcmIO::writer::ie::FrameOfReference frameOfReferenceIE(writer, m_instance, series);
74  ::fwGdcmIO::writer::ie::Equipment equipmentIE(writer, m_instance, series->getEquipment());
75  ::fwGdcmIO::writer::ie::Image imageIE(writer, m_instance, imageSeries->getImage());
76 
77  // Write Patient Module - PS 3.3 C.7.1.1
78  patientIE.writePatientModule();
79 
80  // Write General Study Module - PS 3.3 C.7.2.1
81  studyIE.writeGeneralStudyModule();
82 
83  // Write Patient Study Module - PS 3.3 C.7.2.2
84  studyIE.writePatientStudyModule();
85 
86  // Write General Series Module - PS 3.3 C.7.3.1
87  seriesIE.writeGeneralSeriesModule();
88 
89  // Write Frame of Reference Module - PS 3.3 C.7.4.1
90  frameOfReferenceIE.writeFrameOfReferenceModule();
91 
92  // Write General Equipment Module - PS 3.3 C.7.5.1
93  equipmentIE.writeGeneralEquipmentModule();
94 
95  // Write General Image Module - PS 3.3 C.7.6.1
96  imageIE.writeGeneralImageModule();
97 
98  // Write Image Plane Module - PS 3.3 C.7.6.2
99  imageIE.writeImagePlaneModule();
100 
101  // Write Image Pixel Module - PS 3.3 C.7.6.3
102  imageIE.writeImagePixelModule();
103 
104  if(m_instance->getSOPClassUID() == ::gdcm::MediaStorage::GetMSString(::gdcm::MediaStorage::CTImageStorage))
105  {
106  // Write CT Image Module - PS 3.3 C.8.2.1
107  imageIE.writeCTImageModule();
108  }
109  else if(m_instance->getSOPClassUID() == ::gdcm::MediaStorage::GetMSString(::gdcm::MediaStorage::MRImageStorage))
110  {
111  // Write MR Image Module - PS 3.3 C.8.3.1
112  imageIE.writeMRImageModule();
113  }
114 
115  // Write VOI LUT Module - PS 3.3 C.11.2
116  imageIE.writeVOILUTModule();
117 
118  // Write SOP Common Module - PS 3.3 C.12.1
119  imageIE.writeSOPCommonModule();
120 
121  // Copy dataset to avoid writing conflict with GDCM
122  const ::gdcm::DataSet datasetCopy = writer->GetFile().GetDataSet();
123 
124  // Compute number of frames
125  std::size_t nbFrames = (m_instance->getIsMultiFiles()) ? (image->getSize()[2]) : 1;
126 
127  // Write specific tags according to frame number
128  for(unsigned int i = 0; i < nbFrames; ++i)
129  {
130  // Reset dataset
131  writer->GetFile().SetDataSet(datasetCopy);
132 
133  // Write SOP Common Module specific tags - PS 3.3 C.12.1
134  imageIE.writeSOPCommonModuleSpecificTags(i);
135 
136  // Write General Image Module specific tags - PS 3.3 C.7.6.1
137  imageIE.writeGeneralImageModuleSpecificTags(i);
138 
139  // Write Image Plane Module specific tags - PS 3.3 C.7.6.2
140  imageIE.writeImagePlaneModuleSpecificTags(i);
141 
142  // Write Image Pixel Module specific tags - PS 3.3 C.7.6.3
143  imageIE.writeImagePixelModuleSpecificTags(i);
144 
145  // Write file
146  std::stringstream ss;
147  ss << std::setfill('0') << std::setw(5) << i;
148  auto framePath = m_destinationPath;
149  framePath += ss.str();
150  ::fwGdcmIO::helper::FileWriter::write(framePath, writer);
151  }
152 }
153 //------------------------------------------------------------------------------
154 
155 } // namespace iod
156 } // namespace writer
157 } // namespace fwGdcmIO
Frame Of Reference Information Entity class.
#define SPTR(_cls_)
std::shared_ptr< ::fwGdcmIO::container::DicomInstance > m_instance
DICOM Instance.
virtual FWGDCMIO_API void writeFrameOfReferenceModule()
Write Frame of Reference Module tags.
This class defines a DICOM SOP instance. It is useful during the whole writing process. This class allows to share data between module writers.
The namespace fwGdcmIO contains reader, writer and helper for dicom data.
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
FWGDCMIO_API CTMRImageIOD(const std::shared_ptr< ::fwGdcmIO::container::DicomInstance > &instance, const ::boost::filesystem::path &destinationPath, const ::fwLog::Logger::sptr &logger=nullptr, ProgressCallback progress=nullptr, CancelRequestedCallback cancel=nullptr)
Constructor.
static FWGDCMIO_API void write(const ::boost::filesystem::path &filename, const std::shared_ptr< ::gdcm::Writer > &writer)
Write a file from the dataset.
Definition: FileWriter.cpp:20
virtual FWGDCMIO_API void write(const ::fwMedData::Series::csptr &series) override
Write DICOM file.
virtual FWGDCMIO_API ~CTMRImageIOD()
Destructor.
This file defines SpyLog macros. These macros are used to log messages to a file or to the console du...
InformationObjectDefinition base class used to write DICOM modules.