comparison Framework/Loaders/OrthancSeriesVolumeProgressiveLoader.cpp @ 1337:b1396be5aa27 broker

Moved the fixed loaders back from the dead
author Benjamin Golinvaux <bgo@osimis.io>
date Fri, 03 Apr 2020 16:13:06 +0200
parents
children 556b4bc19118
comparison
equal deleted inserted replaced
1334:04055b6b9e2c 1337:b1396be5aa27
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-2020 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 "OrthancSeriesVolumeProgressiveLoader.h"
23
24 #include "../../StoneException.h"
25 #include "../../Loaders/ILoadersContext.h"
26 #include "../../Loaders/BasicFetchingItemsSorter.h"
27 #include "../../Loaders/BasicFetchingStrategy.h"
28 #include "../../Toolbox/GeometryToolbox.h"
29 #include "../../Volumes/DicomVolumeImageMPRSlicer.h"
30
31 #include <Core/Images/ImageProcessing.h>
32 #include <Core/OrthancException.h>
33
34 namespace OrthancStone
35 {
36 using OrthancStone::ILoadersContext;
37
38 class OrthancSeriesVolumeProgressiveLoader::ExtractedSlice : public OrthancStone::DicomVolumeImageMPRSlicer::Slice
39 {
40 private:
41 const OrthancSeriesVolumeProgressiveLoader& that_;
42
43 public:
44 ExtractedSlice(const OrthancSeriesVolumeProgressiveLoader& that,
45 const OrthancStone::CoordinateSystem3D& plane) :
46 OrthancStone::DicomVolumeImageMPRSlicer::Slice(*that.volume_, plane),
47 that_(that)
48 {
49 if (IsValid())
50 {
51 if (GetProjection() == OrthancStone::VolumeProjection_Axial)
52 {
53 // For coronal and sagittal projections, we take the global
54 // revision of the volume because even if a single slice changes,
55 // this means the projection will yield a different result -->
56 // we must increase the revision as soon as any slice changes
57 SetRevision(that_.seriesGeometry_.GetSliceRevision(GetSliceIndex()));
58 }
59
60 if (that_.strategy_.get() != NULL &&
61 GetProjection() == OrthancStone::VolumeProjection_Axial)
62 {
63 that_.strategy_->SetCurrent(GetSliceIndex());
64 }
65 }
66 }
67 };
68
69 void OrthancSeriesVolumeProgressiveLoader::SeriesGeometry::CheckSlice(
70 size_t index, const OrthancStone::DicomInstanceParameters& reference) const
71 {
72 const OrthancStone::DicomInstanceParameters& slice = *slices_[index];
73
74 if (!OrthancStone::GeometryToolbox::IsParallel(
75 reference.GetGeometry().GetNormal(),
76 slice.GetGeometry().GetNormal()))
77 {
78 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadGeometry,
79 "A slice in the volume image is not parallel to the others");
80 }
81
82 if (reference.GetExpectedPixelFormat() != slice.GetExpectedPixelFormat())
83 {
84 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat,
85 "The pixel format changes across the slices of the volume image");
86 }
87
88 if (reference.GetImageInformation().GetWidth() != slice.GetImageInformation().GetWidth() ||
89 reference.GetImageInformation().GetHeight() != slice.GetImageInformation().GetHeight())
90 {
91 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageSize,
92 "The width/height of slices are not constant in the volume image");
93 }
94
95 if (!OrthancStone::LinearAlgebra::IsNear(reference.GetPixelSpacingX(), slice.GetPixelSpacingX()) ||
96 !OrthancStone::LinearAlgebra::IsNear(reference.GetPixelSpacingY(), slice.GetPixelSpacingY()))
97 {
98 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadGeometry,
99 "The pixel spacing of the slices change across the volume image");
100 }
101 }
102
103
104 void OrthancSeriesVolumeProgressiveLoader::SeriesGeometry::CheckVolume() const
105 {
106 for (size_t i = 0; i < slices_.size(); i++)
107 {
108 assert(slices_[i] != NULL);
109 if (slices_[i]->GetImageInformation().GetNumberOfFrames() != 1)
110 {
111 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadGeometry,
112 "This class does not support multi-frame images");
113 }
114 }
115
116 if (slices_.size() != 0)
117 {
118 const OrthancStone::DicomInstanceParameters& reference = *slices_[0];
119
120 for (size_t i = 1; i < slices_.size(); i++)
121 {
122 CheckSlice(i, reference);
123 }
124 }
125 }
126
127
128 void OrthancSeriesVolumeProgressiveLoader::SeriesGeometry::Clear()
129 {
130 for (size_t i = 0; i < slices_.size(); i++)
131 {
132 assert(slices_[i] != NULL);
133 delete slices_[i];
134 }
135
136 slices_.clear();
137 slicesRevision_.clear();
138 }
139
140
141 void OrthancSeriesVolumeProgressiveLoader::SeriesGeometry::CheckSliceIndex(size_t index) const
142 {
143 if (!HasGeometry())
144 {
145 LOG(ERROR) << "OrthancSeriesVolumeProgressiveLoader::SeriesGeometry::CheckSliceIndex(size_t index): (!HasGeometry())";
146 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
147 }
148 else if (index >= slices_.size())
149 {
150 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
151 }
152 else
153 {
154 assert(slices_.size() == GetImageGeometry().GetDepth() &&
155 slices_.size() == slicesRevision_.size());
156 }
157 }
158
159
160 // WARNING: The payload of "slices" must be of class "DicomInstanceParameters"
161 // (called with the slices created in LoadGeometry)
162 void OrthancSeriesVolumeProgressiveLoader::SeriesGeometry::ComputeGeometry(OrthancStone::SlicesSorter& slices)
163 {
164 Clear();
165
166 if (!slices.Sort())
167 {
168 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange,
169 "Cannot sort the 3D slices of a DICOM series");
170 }
171
172 if (slices.GetSlicesCount() == 0)
173 {
174 geometry_.reset(new OrthancStone::VolumeImageGeometry);
175 }
176 else
177 {
178 slices_.reserve(slices.GetSlicesCount());
179 slicesRevision_.resize(slices.GetSlicesCount(), 0);
180
181 for (size_t i = 0; i < slices.GetSlicesCount(); i++)
182 {
183 const OrthancStone::DicomInstanceParameters& slice =
184 dynamic_cast<const OrthancStone::DicomInstanceParameters&>(slices.GetSlicePayload(i));
185 slices_.push_back(new OrthancStone::DicomInstanceParameters(slice));
186 }
187
188 CheckVolume();
189
190 double spacingZ;
191
192 if (slices.ComputeSpacingBetweenSlices(spacingZ))
193 {
194 LOG(TRACE) << "Computed spacing between slices: " << spacingZ << "mm";
195
196 const OrthancStone::DicomInstanceParameters& parameters = *slices_[0];
197
198 geometry_.reset(new OrthancStone::VolumeImageGeometry);
199 geometry_->SetSizeInVoxels(parameters.GetImageInformation().GetWidth(),
200 parameters.GetImageInformation().GetHeight(),
201 static_cast<unsigned int>(slices.GetSlicesCount()));
202 geometry_->SetAxialGeometry(slices.GetSliceGeometry(0));
203 geometry_->SetVoxelDimensions(parameters.GetPixelSpacingX(),
204 parameters.GetPixelSpacingY(), spacingZ);
205 }
206 else
207 {
208 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadGeometry,
209 "The origins of the slices of a volume image are not regularly spaced");
210 }
211 }
212 }
213
214
215 const OrthancStone::VolumeImageGeometry& OrthancSeriesVolumeProgressiveLoader::SeriesGeometry::GetImageGeometry() const
216 {
217 if (!HasGeometry())
218 {
219 LOG(ERROR) << "OrthancSeriesVolumeProgressiveLoader::SeriesGeometry::GetImageGeometry(): (!HasGeometry())";
220 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
221 }
222 else
223 {
224 assert(slices_.size() == geometry_->GetDepth());
225 return *geometry_;
226 }
227 }
228
229
230 const OrthancStone::DicomInstanceParameters& OrthancSeriesVolumeProgressiveLoader::SeriesGeometry::GetSliceParameters(size_t index) const
231 {
232 CheckSliceIndex(index);
233 return *slices_[index];
234 }
235
236
237 uint64_t OrthancSeriesVolumeProgressiveLoader::SeriesGeometry::GetSliceRevision(size_t index) const
238 {
239 CheckSliceIndex(index);
240 return slicesRevision_[index];
241 }
242
243
244 void OrthancSeriesVolumeProgressiveLoader::SeriesGeometry::IncrementSliceRevision(size_t index)
245 {
246 CheckSliceIndex(index);
247 slicesRevision_[index] ++;
248 }
249
250
251 static unsigned int GetSliceIndexPayload(const OrthancStone::OracleCommandBase& command)
252 {
253 assert(command.HasPayload());
254 return dynamic_cast< const Orthanc::SingleValueObject<unsigned int>& >(command.GetPayload()).GetValue();
255 }
256
257
258 void OrthancSeriesVolumeProgressiveLoader::ScheduleNextSliceDownload()
259 {
260 assert(strategy_.get() != NULL);
261
262 unsigned int sliceIndex = 0, quality = 0;
263
264 if (strategy_->GetNext(sliceIndex, quality))
265 {
266 if (!progressiveQuality_)
267 {
268 ORTHANC_ASSERT(quality == QUALITY_00, "INTERNAL ERROR. quality != QUALITY_00 in "
269 << "OrthancSeriesVolumeProgressiveLoader::ScheduleNextSliceDownload");
270 }
271
272 const OrthancStone::DicomInstanceParameters& slice = seriesGeometry_.GetSliceParameters(sliceIndex);
273
274 const std::string& instance = slice.GetOrthancInstanceIdentifier();
275 if (instance.empty())
276 {
277 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
278 }
279
280 std::unique_ptr<OrthancStone::OracleCommandBase> command;
281
282 if (!progressiveQuality_ || quality == QUALITY_02)
283 {
284 std::unique_ptr<OrthancStone::GetOrthancImageCommand> tmp(new OrthancStone::GetOrthancImageCommand);
285 // TODO: review the following comment.
286 // - Commented out by bgo on 2019-07-19 | reason: Alain has seen cases
287 // where gzipping the uint16 image took 11 sec to produce 5mb.
288 // The unzipped request was much much faster.
289 // - Re-enabled on 2019-07-30. Reason: in Web Assembly, the browser
290 // does not use the Accept-Encoding header and always requests
291 // compression. Furthermore, NOT
292 tmp->SetHttpHeader("Accept-Encoding", "gzip");
293 tmp->SetHttpHeader("Accept", std::string(Orthanc::EnumerationToString(Orthanc::MimeType_Pam)));
294 tmp->SetInstanceUri(instance, slice.GetExpectedPixelFormat());
295 tmp->SetExpectedPixelFormat(slice.GetExpectedPixelFormat());
296 //LOG(INFO)
297 // << "OrthancSeriesVolumeProgressiveLoader.ScheduleNextSliceDownload()"
298 // << " sliceIndex = " << sliceIndex << " slice quality = " << quality
299 // << " URI = " << tmp->GetUri();
300 command.reset(tmp.release());
301 }
302 else // progressive mode is true AND quality is not final (different from QUALITY_02
303 {
304 std::unique_ptr<OrthancStone::GetOrthancWebViewerJpegCommand> tmp(
305 new OrthancStone::GetOrthancWebViewerJpegCommand);
306
307 // TODO: review the following comment. Commented out by bgo on 2019-07-19
308 // (gzip for jpeg seems overkill)
309 //tmp->SetHttpHeader("Accept-Encoding", "gzip");
310 tmp->SetInstance(instance);
311 tmp->SetQuality((quality == 0 ? 50 : 90)); // QUALITY_00 is Jpeg50 while QUALITY_01 is Jpeg90
312 tmp->SetExpectedPixelFormat(slice.GetExpectedPixelFormat());
313 LOG(TRACE)
314 << "OrthancSeriesVolumeProgressiveLoader.ScheduleNextSliceDownload()"
315 << " sliceIndex = " << sliceIndex << " slice quality = " << quality;
316 command.reset(tmp.release());
317 }
318
319 command->AcquirePayload(new Orthanc::SingleValueObject<unsigned int>(sliceIndex));
320
321 {
322 std::unique_ptr<OrthancStone::ILoadersContext::ILock> lock(loadersContext_.Lock());
323 boost::shared_ptr<IObserver> observer(GetSharedObserver());
324 lock->Schedule(observer, 0, command.release()); // TODO: priority!
325 }
326 }
327 else
328 {
329 // loading is finished!
330 volumeImageReadyInHighQuality_ = true;
331 BroadcastMessage(OrthancSeriesVolumeProgressiveLoader::VolumeImageReadyInHighQuality(*this));
332 }
333 }
334
335 /**
336 This is called in response to GET "/series/XXXXXXXXXXXXX/instances-tags"
337 */
338 void OrthancSeriesVolumeProgressiveLoader::LoadGeometry(const OrthancStone::OrthancRestApiCommand::SuccessMessage& message)
339 {
340 Json::Value body;
341 message.ParseJsonBody(body);
342
343 if (body.type() != Json::objectValue)
344 {
345 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);
346 }
347
348 {
349 Json::Value::Members instances = body.getMemberNames();
350
351 OrthancStone::SlicesSorter slices;
352
353 for (size_t i = 0; i < instances.size(); i++)
354 {
355 Orthanc::DicomMap dicom;
356 dicom.FromDicomAsJson(body[instances[i]]);
357
358 std::unique_ptr<OrthancStone::DicomInstanceParameters> instance(new OrthancStone::DicomInstanceParameters(dicom));
359 instance->SetOrthancInstanceIdentifier(instances[i]);
360
361 // the 3D plane corresponding to the slice
362 OrthancStone::CoordinateSystem3D geometry = instance->GetGeometry();
363 slices.AddSlice(geometry, instance.release());
364 }
365
366 seriesGeometry_.ComputeGeometry(slices);
367 }
368
369 size_t slicesCount = seriesGeometry_.GetImageGeometry().GetDepth();
370
371 if (slicesCount == 0)
372 {
373 volume_->Initialize(seriesGeometry_.GetImageGeometry(), Orthanc::PixelFormat_Grayscale8);
374 }
375 else
376 {
377 const OrthancStone::DicomInstanceParameters& parameters = seriesGeometry_.GetSliceParameters(0);
378
379 volume_->Initialize(seriesGeometry_.GetImageGeometry(), parameters.GetExpectedPixelFormat());
380 volume_->SetDicomParameters(parameters);
381 volume_->GetPixelData().Clear();
382
383 // If we are in progressive mode, the Fetching strategy will first request QUALITY_00, then QUALITY_01, then
384 // QUALITY_02... Otherwise, it's only QUALITY_00
385 unsigned int maxQuality = QUALITY_00;
386 if (progressiveQuality_)
387 maxQuality = QUALITY_02;
388
389 strategy_.reset(new OrthancStone::BasicFetchingStrategy(
390 sorter_->CreateSorter(static_cast<unsigned int>(slicesCount)),
391 maxQuality));
392
393 assert(simultaneousDownloads_ != 0);
394 for (unsigned int i = 0; i < simultaneousDownloads_; i++)
395 {
396 ScheduleNextSliceDownload();
397 }
398 }
399
400 slicesQuality_.resize(slicesCount, 0);
401
402 BroadcastMessage(OrthancStone::DicomVolumeImage::GeometryReadyMessage(*volume_));
403 }
404
405
406 void OrthancSeriesVolumeProgressiveLoader::SetSliceContent(unsigned int sliceIndex,
407 const Orthanc::ImageAccessor& image,
408 unsigned int quality)
409 {
410 ORTHANC_ASSERT(sliceIndex < slicesQuality_.size() &&
411 slicesQuality_.size() == volume_->GetPixelData().GetDepth());
412
413 if (!progressiveQuality_)
414 {
415 ORTHANC_ASSERT(quality == QUALITY_00);
416 ORTHANC_ASSERT(slicesQuality_[sliceIndex] == QUALITY_00);
417 }
418
419 if (quality >= slicesQuality_[sliceIndex])
420 {
421 {
422 OrthancStone::ImageBuffer3D::SliceWriter writer(volume_->GetPixelData(),
423 OrthancStone::VolumeProjection_Axial,
424 sliceIndex);
425
426 Orthanc::ImageProcessing::Copy(writer.GetAccessor(), image);
427 }
428
429 volume_->IncrementRevision();
430 seriesGeometry_.IncrementSliceRevision(sliceIndex);
431 slicesQuality_[sliceIndex] = quality;
432
433 BroadcastMessage(OrthancStone::DicomVolumeImage::ContentUpdatedMessage(*volume_));
434 }
435 LOG(TRACE) << "SetSliceContent sliceIndex = " << sliceIndex << " -- will "
436 << " now call ScheduleNextSliceDownload()";
437 ScheduleNextSliceDownload();
438 }
439
440 void OrthancSeriesVolumeProgressiveLoader::LoadBestQualitySliceContent(
441 const OrthancStone::GetOrthancImageCommand::SuccessMessage& message)
442 {
443 unsigned int quality = QUALITY_00;
444 if (progressiveQuality_)
445 quality = QUALITY_02;
446
447 SetSliceContent(GetSliceIndexPayload(message.GetOrigin()),
448 message.GetImage(),
449 quality);
450 }
451
452 void OrthancSeriesVolumeProgressiveLoader::LoadJpegSliceContent(
453 const OrthancStone::GetOrthancWebViewerJpegCommand::SuccessMessage& message)
454 {
455 ORTHANC_ASSERT(progressiveQuality_, "INTERNAL ERROR: OrthancSeriesVolumeProgressiveLoader::LoadJpegSliceContent"
456 << " called while progressiveQuality_ is false!");
457
458 LOG(TRACE) << "OrthancSeriesVolumeProgressiveLoader::LoadJpegSliceContent";
459 unsigned int quality;
460
461 switch (dynamic_cast<const OrthancStone::GetOrthancWebViewerJpegCommand&>(message.GetOrigin()).GetQuality())
462 {
463 case 50:
464 quality = QUALITY_00;
465 break;
466
467 case 90:
468 quality = QUALITY_01;
469 break;
470
471 default:
472 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
473 }
474
475 SetSliceContent(GetSliceIndexPayload(message.GetOrigin()), message.GetImage(), quality);
476 }
477
478 OrthancSeriesVolumeProgressiveLoader::OrthancSeriesVolumeProgressiveLoader(
479 OrthancStone::ILoadersContext& loadersContext,
480 boost::shared_ptr<OrthancStone::DicomVolumeImage> volume,
481 bool progressiveQuality)
482 : loadersContext_(loadersContext)
483 , active_(false)
484 , progressiveQuality_(progressiveQuality)
485 , simultaneousDownloads_(4)
486 , volume_(volume)
487 , sorter_(new OrthancStone::BasicFetchingItemsSorter::Factory)
488 , volumeImageReadyInHighQuality_(false)
489 {
490 }
491
492 boost::shared_ptr<OrthancSeriesVolumeProgressiveLoader>
493 OrthancSeriesVolumeProgressiveLoader::Create(
494 OrthancStone::ILoadersContext& loadersContext,
495 boost::shared_ptr<OrthancStone::DicomVolumeImage> volume,
496 bool progressiveQuality)
497 {
498 std::auto_ptr<OrthancStone::ILoadersContext::ILock> lock(loadersContext.Lock());
499
500 boost::shared_ptr<OrthancSeriesVolumeProgressiveLoader> obj(
501 new OrthancSeriesVolumeProgressiveLoader(
502 loadersContext, volume, progressiveQuality));
503
504 obj->Register<OrthancStone::OrthancRestApiCommand::SuccessMessage>(
505 lock->GetOracleObservable(),
506 &OrthancSeriesVolumeProgressiveLoader::LoadGeometry);
507
508 obj->Register<OrthancStone::GetOrthancImageCommand::SuccessMessage>(
509 lock->GetOracleObservable(),
510 &OrthancSeriesVolumeProgressiveLoader::LoadBestQualitySliceContent);
511
512 obj->Register<OrthancStone::GetOrthancWebViewerJpegCommand::SuccessMessage>(
513 lock->GetOracleObservable(),
514 &OrthancSeriesVolumeProgressiveLoader::LoadJpegSliceContent);
515
516 return obj;
517 }
518
519
520 OrthancSeriesVolumeProgressiveLoader::~OrthancSeriesVolumeProgressiveLoader()
521 {
522 LOG(TRACE) << "OrthancSeriesVolumeProgressiveLoader::~OrthancSeriesVolumeProgressiveLoader()";
523 }
524
525 void OrthancSeriesVolumeProgressiveLoader::SetSimultaneousDownloads(unsigned int count)
526 {
527 if (active_)
528 {
529 LOG(ERROR) << "OrthancSeriesVolumeProgressiveLoader::SetSimultaneousDownloads(): (active_)";
530 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
531 }
532 else if (count == 0)
533 {
534 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
535 }
536 else
537 {
538 simultaneousDownloads_ = count;
539 }
540 }
541
542
543 void OrthancSeriesVolumeProgressiveLoader::LoadSeries(const std::string& seriesId)
544 {
545 if (active_)
546 {
547 LOG(ERROR) << "OrthancSeriesVolumeProgressiveLoader::LoadSeries(const std::string& seriesId): (active_)";
548 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
549 }
550 else
551 {
552 active_ = true;
553
554 std::unique_ptr<OrthancStone::OrthancRestApiCommand> command(new OrthancStone::OrthancRestApiCommand);
555 command->SetUri("/series/" + seriesId + "/instances-tags");
556 {
557 std::unique_ptr<OrthancStone::ILoadersContext::ILock> lock(loadersContext_.Lock());
558 boost::shared_ptr<IObserver> observer(GetSharedObserver());
559 lock->Schedule(observer, 0, command.release()); //TODO: priority!
560 }
561 }
562 }
563
564
565 OrthancStone::IVolumeSlicer::IExtractedSlice*
566 OrthancSeriesVolumeProgressiveLoader::ExtractSlice(const OrthancStone::CoordinateSystem3D& cuttingPlane)
567 {
568 if (volume_->HasGeometry())
569 {
570 return new ExtractedSlice(*this, cuttingPlane);
571 }
572 else
573 {
574 return new IVolumeSlicer::InvalidSlice;
575 }
576 }
577 }