815
|
1 /**
|
|
2 * Stone of Orthanc
|
|
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
|
|
4 * Department, University Hospital of Liege, Belgium
|
|
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
|
|
6 *
|
|
7 * This program is free software: you can redistribute it and/or
|
|
8 * modify it under the terms of the GNU Affero General Public License
|
|
9 * as published by the Free Software Foundation, either version 3 of
|
|
10 * the License, or (at your option) any later version.
|
|
11 *
|
|
12 * This program is distributed in the hope that it will be useful, but
|
|
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
15 * Affero General Public License for more details.
|
|
16 *
|
|
17 * You should have received a copy of the GNU Affero General Public License
|
|
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
19 **/
|
|
20
|
|
21
|
|
22 #include "OrthancMultiframeVolumeLoader.h"
|
|
23
|
816
|
24 #include <Core/Endianness.h>
|
815
|
25 #include <Core/Toolbox.h>
|
|
26
|
|
27 namespace OrthancStone
|
|
28 {
|
|
29 class OrthancMultiframeVolumeLoader::LoadRTDoseGeometry : public LoaderStateMachine::State
|
|
30 {
|
|
31 private:
|
|
32 std::auto_ptr<Orthanc::DicomMap> dicom_;
|
|
33
|
|
34 public:
|
|
35 LoadRTDoseGeometry(OrthancMultiframeVolumeLoader& that,
|
|
36 Orthanc::DicomMap* dicom) :
|
|
37 State(that),
|
|
38 dicom_(dicom)
|
|
39 {
|
|
40 if (dicom == NULL)
|
|
41 {
|
|
42 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
|
|
43 }
|
|
44
|
|
45 }
|
|
46
|
|
47 virtual void Handle(const OrthancRestApiCommand::SuccessMessage& message)
|
|
48 {
|
|
49 // Complete the DICOM tags with just-received "Grid Frame Offset Vector"
|
|
50 std::string s = Orthanc::Toolbox::StripSpaces(message.GetAnswer());
|
|
51 dicom_->SetValue(Orthanc::DICOM_TAG_GRID_FRAME_OFFSET_VECTOR, s, false);
|
|
52
|
|
53 GetLoader<OrthancMultiframeVolumeLoader>().SetGeometry(*dicom_);
|
|
54 }
|
|
55 };
|
|
56
|
|
57
|
|
58 static std::string GetSopClassUid(const Orthanc::DicomMap& dicom)
|
|
59 {
|
|
60 std::string s;
|
|
61 if (!dicom.CopyToString(s, Orthanc::DICOM_TAG_SOP_CLASS_UID, false))
|
|
62 {
|
|
63 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat,
|
|
64 "DICOM file without SOP class UID");
|
|
65 }
|
|
66 else
|
|
67 {
|
|
68 return s;
|
|
69 }
|
|
70 }
|
|
71
|
|
72
|
|
73 class OrthancMultiframeVolumeLoader::LoadGeometry : public State
|
|
74 {
|
|
75 public:
|
|
76 LoadGeometry(OrthancMultiframeVolumeLoader& that) :
|
|
77 State(that)
|
|
78 {
|
|
79 }
|
|
80
|
|
81 virtual void Handle(const OrthancRestApiCommand::SuccessMessage& message)
|
|
82 {
|
|
83 OrthancMultiframeVolumeLoader& loader = GetLoader<OrthancMultiframeVolumeLoader>();
|
|
84
|
|
85 Json::Value body;
|
|
86 message.ParseJsonBody(body);
|
|
87
|
|
88 if (body.type() != Json::objectValue)
|
|
89 {
|
|
90 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);
|
|
91 }
|
|
92
|
|
93 std::auto_ptr<Orthanc::DicomMap> dicom(new Orthanc::DicomMap);
|
|
94 dicom->FromDicomAsJson(body);
|
|
95
|
|
96 if (StringToSopClassUid(GetSopClassUid(*dicom)) == SopClassUid_RTDose)
|
|
97 {
|
|
98 // Download the "Grid Frame Offset Vector" DICOM tag, that is
|
|
99 // mandatory for RT-DOSE, but is too long to be returned by default
|
|
100
|
|
101 std::auto_ptr<OrthancRestApiCommand> command(new OrthancRestApiCommand);
|
|
102 command->SetUri("/instances/" + loader.GetInstanceId() + "/content/" +
|
|
103 Orthanc::DICOM_TAG_GRID_FRAME_OFFSET_VECTOR.Format());
|
|
104 command->SetPayload(new LoadRTDoseGeometry(loader, dicom.release()));
|
|
105
|
|
106 Schedule(command.release());
|
|
107 }
|
|
108 else
|
|
109 {
|
|
110 loader.SetGeometry(*dicom);
|
|
111 }
|
|
112 }
|
|
113 };
|
|
114
|
|
115
|
|
116
|
|
117 class OrthancMultiframeVolumeLoader::LoadTransferSyntax : public State
|
|
118 {
|
|
119 public:
|
|
120 LoadTransferSyntax(OrthancMultiframeVolumeLoader& that) :
|
|
121 State(that)
|
|
122 {
|
|
123 }
|
|
124
|
|
125 virtual void Handle(const OrthancRestApiCommand::SuccessMessage& message)
|
|
126 {
|
|
127 GetLoader<OrthancMultiframeVolumeLoader>().SetTransferSyntax(message.GetAnswer());
|
|
128 }
|
|
129 };
|
|
130
|
|
131
|
|
132 class OrthancMultiframeVolumeLoader::LoadUncompressedPixelData : public State
|
|
133 {
|
|
134 public:
|
|
135 LoadUncompressedPixelData(OrthancMultiframeVolumeLoader& that) :
|
|
136 State(that)
|
|
137 {
|
|
138 }
|
|
139
|
|
140 virtual void Handle(const OrthancRestApiCommand::SuccessMessage& message)
|
|
141 {
|
|
142 GetLoader<OrthancMultiframeVolumeLoader>().SetUncompressedPixelData(message.GetAnswer());
|
|
143 }
|
|
144 };
|
|
145
|
|
146
|
|
147 const std::string& OrthancMultiframeVolumeLoader::GetInstanceId() const
|
|
148 {
|
|
149 if (IsActive())
|
|
150 {
|
|
151 return instanceId_;
|
|
152 }
|
|
153 else
|
|
154 {
|
|
155 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
|
|
156 }
|
|
157 }
|
|
158
|
|
159
|
|
160 void OrthancMultiframeVolumeLoader::ScheduleFrameDownloads()
|
|
161 {
|
|
162 if (transferSyntaxUid_.empty() ||
|
|
163 !volume_->HasGeometry())
|
|
164 {
|
|
165 return;
|
|
166 }
|
|
167 /*
|
|
168 1.2.840.10008.1.2 Implicit VR Endian: Default Transfer Syntax for DICOM
|
|
169 1.2.840.10008.1.2.1 Explicit VR Little Endian
|
|
170 1.2.840.10008.1.2.2 Explicit VR Big Endian
|
|
171
|
|
172 See https://www.dicomlibrary.com/dicom/transfer-syntax/
|
|
173 */
|
|
174 if (transferSyntaxUid_ == "1.2.840.10008.1.2" ||
|
|
175 transferSyntaxUid_ == "1.2.840.10008.1.2.1" ||
|
|
176 transferSyntaxUid_ == "1.2.840.10008.1.2.2")
|
|
177 {
|
|
178 std::auto_ptr<OrthancRestApiCommand> command(new OrthancRestApiCommand);
|
|
179 command->SetHttpHeader("Accept-Encoding", "gzip");
|
|
180 command->SetUri("/instances/" + instanceId_ + "/content/" +
|
|
181 Orthanc::DICOM_TAG_PIXEL_DATA.Format() + "/0");
|
|
182 command->SetPayload(new LoadUncompressedPixelData(*this));
|
|
183 Schedule(command.release());
|
|
184 }
|
|
185 else
|
|
186 {
|
|
187 throw Orthanc::OrthancException(
|
|
188 Orthanc::ErrorCode_NotImplemented,
|
|
189 "No support for multiframe instances with transfer syntax: " + transferSyntaxUid_);
|
|
190 }
|
|
191 }
|
|
192
|
|
193
|
|
194 void OrthancMultiframeVolumeLoader::SetTransferSyntax(const std::string& transferSyntax)
|
|
195 {
|
|
196 transferSyntaxUid_ = Orthanc::Toolbox::StripSpaces(transferSyntax);
|
|
197 ScheduleFrameDownloads();
|
|
198 }
|
|
199
|
|
200
|
|
201 void OrthancMultiframeVolumeLoader::SetGeometry(const Orthanc::DicomMap& dicom)
|
|
202 {
|
|
203 DicomInstanceParameters parameters(dicom);
|
|
204 volume_->SetDicomParameters(parameters);
|
|
205
|
|
206 Orthanc::PixelFormat format;
|
|
207 if (!parameters.GetImageInformation().ExtractPixelFormat(format, true))
|
|
208 {
|
|
209 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
|
|
210 }
|
|
211
|
|
212 double spacingZ;
|
|
213 switch (parameters.GetSopClassUid())
|
|
214 {
|
|
215 case SopClassUid_RTDose:
|
|
216 spacingZ = parameters.GetThickness();
|
|
217 break;
|
|
218
|
|
219 default:
|
|
220 throw Orthanc::OrthancException(
|
|
221 Orthanc::ErrorCode_NotImplemented,
|
|
222 "No support for multiframe instances with SOP class UID: " + GetSopClassUid(dicom));
|
|
223 }
|
|
224
|
|
225 const unsigned int width = parameters.GetImageInformation().GetWidth();
|
|
226 const unsigned int height = parameters.GetImageInformation().GetHeight();
|
|
227 const unsigned int depth = parameters.GetImageInformation().GetNumberOfFrames();
|
|
228
|
|
229 {
|
|
230 VolumeImageGeometry geometry;
|
|
231 geometry.SetSize(width, height, depth);
|
|
232 geometry.SetAxialGeometry(parameters.GetGeometry());
|
|
233 geometry.SetVoxelDimensions(parameters.GetPixelSpacingX(),
|
|
234 parameters.GetPixelSpacingY(), spacingZ);
|
|
235 volume_->Initialize(geometry, format);
|
|
236 }
|
|
237
|
|
238 volume_->GetPixelData().Clear();
|
|
239
|
|
240 ScheduleFrameDownloads();
|
|
241
|
|
242 BroadcastMessage(DicomVolumeImage::GeometryReadyMessage(*volume_));
|
|
243 }
|
|
244
|
|
245
|
|
246 ORTHANC_FORCE_INLINE
|
|
247 static void CopyPixel(uint32_t& target,
|
|
248 const void* source)
|
|
249 {
|
|
250 // TODO - check alignement?
|
|
251 target = le32toh(*reinterpret_cast<const uint32_t*>(source));
|
|
252 }
|
|
253
|
|
254
|
|
255 template <typename T>
|
|
256 void OrthancMultiframeVolumeLoader::CopyPixelData(const std::string& pixelData)
|
|
257 {
|
|
258 ImageBuffer3D& target = volume_->GetPixelData();
|
|
259
|
|
260 const unsigned int bpp = target.GetBytesPerPixel();
|
|
261 const unsigned int width = target.GetWidth();
|
|
262 const unsigned int height = target.GetHeight();
|
|
263 const unsigned int depth = target.GetDepth();
|
|
264
|
|
265 if (pixelData.size() != bpp * width * height * depth)
|
|
266 {
|
|
267 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat,
|
|
268 "The pixel data has not the proper size");
|
|
269 }
|
|
270
|
|
271 if (pixelData.empty())
|
|
272 {
|
|
273 return;
|
|
274 }
|
|
275
|
|
276 const uint8_t* source = reinterpret_cast<const uint8_t*>(pixelData.c_str());
|
|
277
|
|
278 for (unsigned int z = 0; z < depth; z++)
|
|
279 {
|
|
280 ImageBuffer3D::SliceWriter writer(target, VolumeProjection_Axial, z);
|
|
281
|
|
282 assert (writer.GetAccessor().GetWidth() == width &&
|
|
283 writer.GetAccessor().GetHeight() == height);
|
|
284
|
|
285 for (unsigned int y = 0; y < height; y++)
|
|
286 {
|
|
287 assert(sizeof(T) == Orthanc::GetBytesPerPixel(target.GetFormat()));
|
|
288
|
|
289 T* target = reinterpret_cast<T*>(writer.GetAccessor().GetRow(y));
|
|
290
|
|
291 for (unsigned int x = 0; x < width; x++)
|
|
292 {
|
|
293 CopyPixel(*target, source);
|
|
294
|
|
295 target ++;
|
|
296 source += bpp;
|
|
297 }
|
|
298 }
|
|
299 }
|
|
300 }
|
|
301
|
|
302
|
|
303 void OrthancMultiframeVolumeLoader::SetUncompressedPixelData(const std::string& pixelData)
|
|
304 {
|
|
305 switch (volume_->GetPixelData().GetFormat())
|
|
306 {
|
|
307 case Orthanc::PixelFormat_Grayscale32:
|
|
308 CopyPixelData<uint32_t>(pixelData);
|
|
309 break;
|
|
310
|
|
311 default:
|
|
312 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
|
|
313 }
|
|
314
|
|
315 volume_->IncrementRevision();
|
|
316
|
|
317 BroadcastMessage(DicomVolumeImage::ContentUpdatedMessage(*volume_));
|
|
318 }
|
|
319
|
|
320
|
|
321 OrthancMultiframeVolumeLoader::OrthancMultiframeVolumeLoader(const boost::shared_ptr<DicomVolumeImage>& volume,
|
|
322 IOracle& oracle,
|
|
323 IObservable& oracleObservable) :
|
|
324 LoaderStateMachine(oracle, oracleObservable),
|
|
325 IObservable(oracleObservable.GetBroker()),
|
|
326 volume_(volume)
|
|
327 {
|
|
328 if (volume.get() == NULL)
|
|
329 {
|
|
330 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
|
|
331 }
|
|
332 }
|
|
333
|
|
334
|
|
335 void OrthancMultiframeVolumeLoader::LoadInstance(const std::string& instanceId)
|
|
336 {
|
|
337 Start();
|
|
338
|
|
339 instanceId_ = instanceId;
|
|
340
|
|
341 {
|
|
342 std::auto_ptr<OrthancRestApiCommand> command(new OrthancRestApiCommand);
|
|
343 command->SetHttpHeader("Accept-Encoding", "gzip");
|
|
344 command->SetUri("/instances/" + instanceId + "/tags");
|
|
345 command->SetPayload(new LoadGeometry(*this));
|
|
346 Schedule(command.release());
|
|
347 }
|
|
348
|
|
349 {
|
|
350 std::auto_ptr<OrthancRestApiCommand> command(new OrthancRestApiCommand);
|
|
351 command->SetUri("/instances/" + instanceId + "/metadata/TransferSyntax");
|
|
352 command->SetPayload(new LoadTransferSyntax(*this));
|
|
353 Schedule(command.release());
|
|
354 }
|
|
355 }
|
|
356 }
|