Mercurial > hg > orthanc-stone
annotate Framework/Radiography/RadiographyScene.cpp @ 439:b70e9be013e4
preparing for 2019
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 24 Dec 2018 13:41:12 +0100 |
parents | c23df8b3433b |
children | a750f11892ec |
rev | line source |
---|---|
408 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
439 | 5 * Copyright (C) 2017-2019 Osimis S.A., Belgium |
408 | 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 "RadiographyScene.h" | |
23 | |
24 #include "../Toolbox/DicomFrameConverter.h" | |
25 | |
26 #include <Core/Images/Image.h> | |
27 #include <Core/Images/ImageProcessing.h> | |
28 #include <Core/Images/PamReader.h> | |
29 #include <Core/Images/PamWriter.h> | |
30 #include <Core/Images/PngWriter.h> | |
31 #include <Core/OrthancException.h> | |
32 #include <Core/Toolbox.h> | |
33 #include <Plugins/Samples/Common/DicomDatasetReader.h> | |
34 #include <Plugins/Samples/Common/FullOrthancDataset.h> | |
35 | |
412 | 36 #include <boost/math/special_functions/round.hpp> |
37 | |
408 | 38 |
39 namespace OrthancStone | |
40 { | |
41 RadiographyScene::LayerAccessor::LayerAccessor(RadiographyScene& scene, | |
42 size_t index) : | |
43 scene_(scene), | |
44 index_(index) | |
45 { | |
46 Layers::iterator layer = scene.layers_.find(index); | |
47 if (layer == scene.layers_.end()) | |
48 { | |
49 layer_ = NULL; | |
50 } | |
51 else | |
52 { | |
53 assert(layer->second != NULL); | |
54 layer_ = layer->second; | |
55 } | |
56 } | |
57 | |
58 | |
59 RadiographyScene::LayerAccessor::LayerAccessor(RadiographyScene& scene, | |
60 double x, | |
61 double y) : | |
62 scene_(scene), | |
63 index_(0) // Dummy initialization | |
64 { | |
65 if (scene.LookupLayer(index_, x, y)) | |
66 { | |
67 Layers::iterator layer = scene.layers_.find(index_); | |
68 | |
69 if (layer == scene.layers_.end()) | |
70 { | |
71 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
72 } | |
73 else | |
74 { | |
75 assert(layer->second != NULL); | |
76 layer_ = layer->second; | |
77 } | |
78 } | |
79 else | |
80 { | |
81 layer_ = NULL; | |
82 } | |
83 } | |
84 | |
85 | |
86 RadiographyScene& RadiographyScene::LayerAccessor::GetScene() const | |
87 { | |
88 if (IsValid()) | |
89 { | |
90 return scene_; | |
91 } | |
92 else | |
93 { | |
94 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
95 } | |
96 } | |
97 | |
98 | |
99 size_t RadiographyScene::LayerAccessor::GetIndex() const | |
100 { | |
101 if (IsValid()) | |
102 { | |
103 return index_; | |
104 } | |
105 else | |
106 { | |
107 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
108 } | |
109 } | |
110 | |
111 | |
410
6decc0ba9da5
rename RadiographyScene::Layer as RadiographyLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
409
diff
changeset
|
112 RadiographyLayer& RadiographyScene::LayerAccessor::GetLayer() const |
408 | 113 { |
114 if (IsValid()) | |
115 { | |
116 return *layer_; | |
117 } | |
118 else | |
119 { | |
120 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
121 } | |
122 } | |
123 | |
124 | |
125 | |
410
6decc0ba9da5
rename RadiographyScene::Layer as RadiographyLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
409
diff
changeset
|
126 class RadiographyScene::AlphaLayer : public RadiographyLayer |
408 | 127 { |
128 private: | |
129 const RadiographyScene& scene_; | |
130 std::auto_ptr<Orthanc::ImageAccessor> alpha_; // Grayscale8 | |
131 bool useWindowing_; | |
132 float foreground_; | |
133 | |
134 public: | |
135 AlphaLayer(const RadiographyScene& scene) : | |
136 scene_(scene), | |
137 useWindowing_(true), | |
138 foreground_(0) | |
139 { | |
140 } | |
141 | |
142 | |
143 void SetForegroundValue(float foreground) | |
144 { | |
145 useWindowing_ = false; | |
146 foreground_ = foreground; | |
147 } | |
148 | |
149 | |
150 void SetAlpha(Orthanc::ImageAccessor* image) | |
151 { | |
152 std::auto_ptr<Orthanc::ImageAccessor> raii(image); | |
153 | |
154 if (image == NULL) | |
155 { | |
156 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
157 } | |
158 | |
159 if (image->GetFormat() != Orthanc::PixelFormat_Grayscale8) | |
160 { | |
161 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
162 } | |
163 | |
164 SetSize(image->GetWidth(), image->GetHeight()); | |
165 alpha_ = raii; | |
166 } | |
167 | |
168 | |
169 void LoadText(const Orthanc::Font& font, | |
170 const std::string& utf8) | |
171 { | |
172 SetAlpha(font.RenderAlpha(utf8)); | |
173 } | |
174 | |
175 | |
176 virtual bool GetDefaultWindowing(float& center, | |
177 float& width) const | |
178 { | |
179 return false; | |
180 } | |
181 | |
182 | |
183 virtual void Render(Orthanc::ImageAccessor& buffer, | |
409 | 184 const AffineTransform2D& viewTransform, |
408 | 185 ImageInterpolation interpolation) const |
186 { | |
187 if (alpha_.get() == NULL) | |
188 { | |
189 return; | |
190 } | |
191 | |
192 if (buffer.GetFormat() != Orthanc::PixelFormat_Float32) | |
193 { | |
194 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
195 } | |
196 | |
197 unsigned int cropX, cropY, cropWidth, cropHeight; | |
198 GetCrop(cropX, cropY, cropWidth, cropHeight); | |
199 | |
409 | 200 const AffineTransform2D t = AffineTransform2D::Combine( |
201 viewTransform, GetTransform(), | |
202 AffineTransform2D::CreateOffset(cropX, cropY)); | |
408 | 203 |
204 Orthanc::ImageAccessor cropped; | |
205 alpha_->GetRegion(cropped, cropX, cropY, cropWidth, cropHeight); | |
206 | |
207 Orthanc::Image tmp(Orthanc::PixelFormat_Grayscale8, buffer.GetWidth(), buffer.GetHeight(), false); | |
409 | 208 |
209 t.Apply(tmp, cropped, interpolation, true /* clear */); | |
408 | 210 |
211 // Blit | |
212 const unsigned int width = buffer.GetWidth(); | |
213 const unsigned int height = buffer.GetHeight(); | |
214 | |
215 float value = foreground_; | |
216 | |
217 if (useWindowing_) | |
218 { | |
219 float center, width; | |
220 if (scene_.GetWindowing(center, width)) | |
221 { | |
222 value = center + width / 2.0f; | |
223 } | |
224 } | |
225 | |
226 for (unsigned int y = 0; y < height; y++) | |
227 { | |
228 float *q = reinterpret_cast<float*>(buffer.GetRow(y)); | |
229 const uint8_t *p = reinterpret_cast<uint8_t*>(tmp.GetRow(y)); | |
230 | |
231 for (unsigned int x = 0; x < width; x++, p++, q++) | |
232 { | |
233 float a = static_cast<float>(*p) / 255.0f; | |
234 | |
235 *q = (a * value + (1.0f - a) * (*q)); | |
236 } | |
237 } | |
238 } | |
239 | |
240 | |
241 virtual bool GetRange(float& minValue, | |
242 float& maxValue) const | |
243 { | |
244 if (useWindowing_) | |
245 { | |
246 return false; | |
247 } | |
248 else | |
249 { | |
250 minValue = 0; | |
251 maxValue = 0; | |
252 | |
253 if (foreground_ < 0) | |
254 { | |
255 minValue = foreground_; | |
256 } | |
257 | |
258 if (foreground_ > 0) | |
259 { | |
260 maxValue = foreground_; | |
261 } | |
262 | |
263 return true; | |
264 } | |
265 } | |
266 }; | |
267 | |
268 | |
269 | |
410
6decc0ba9da5
rename RadiographyScene::Layer as RadiographyLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
409
diff
changeset
|
270 class RadiographyScene::DicomLayer : public RadiographyLayer |
408 | 271 { |
272 private: | |
273 std::auto_ptr<Orthanc::ImageAccessor> source_; // Content of PixelData | |
274 std::auto_ptr<DicomFrameConverter> converter_; | |
275 std::auto_ptr<Orthanc::ImageAccessor> converted_; // Float32 | |
276 | |
277 static OrthancPlugins::DicomTag ConvertTag(const Orthanc::DicomTag& tag) | |
278 { | |
279 return OrthancPlugins::DicomTag(tag.GetGroup(), tag.GetElement()); | |
280 } | |
281 | |
282 | |
283 void ApplyConverter() | |
284 { | |
285 if (source_.get() != NULL && | |
286 converter_.get() != NULL) | |
287 { | |
288 converted_.reset(converter_->ConvertFrame(*source_)); | |
289 } | |
290 } | |
291 | |
292 public: | |
293 void SetDicomTags(const OrthancPlugins::FullOrthancDataset& dataset) | |
294 { | |
295 converter_.reset(new DicomFrameConverter); | |
296 converter_->ReadParameters(dataset); | |
297 ApplyConverter(); | |
298 | |
299 std::string tmp; | |
300 Vector pixelSpacing; | |
301 | |
302 if (dataset.GetStringValue(tmp, ConvertTag(Orthanc::DICOM_TAG_PIXEL_SPACING)) && | |
303 LinearAlgebra::ParseVector(pixelSpacing, tmp) && | |
304 pixelSpacing.size() == 2) | |
305 { | |
306 SetPixelSpacing(pixelSpacing[0], pixelSpacing[1]); | |
307 } | |
308 | |
309 //SetPan(-0.5 * GetPixelSpacingX(), -0.5 * GetPixelSpacingY()); | |
310 | |
311 OrthancPlugins::DicomDatasetReader reader(dataset); | |
312 | |
313 unsigned int width, height; | |
314 if (!reader.GetUnsignedIntegerValue(width, ConvertTag(Orthanc::DICOM_TAG_COLUMNS)) || | |
315 !reader.GetUnsignedIntegerValue(height, ConvertTag(Orthanc::DICOM_TAG_ROWS))) | |
316 { | |
317 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
318 } | |
319 else | |
320 { | |
321 SetSize(width, height); | |
322 } | |
323 } | |
324 | |
325 | |
326 void SetSourceImage(Orthanc::ImageAccessor* image) // Takes ownership | |
327 { | |
328 std::auto_ptr<Orthanc::ImageAccessor> raii(image); | |
329 | |
330 if (image == NULL) | |
331 { | |
332 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
333 } | |
334 | |
335 SetSize(image->GetWidth(), image->GetHeight()); | |
336 | |
337 source_ = raii; | |
338 ApplyConverter(); | |
339 } | |
340 | |
341 | |
342 virtual void Render(Orthanc::ImageAccessor& buffer, | |
409 | 343 const AffineTransform2D& viewTransform, |
408 | 344 ImageInterpolation interpolation) const |
345 { | |
346 if (converted_.get() != NULL) | |
347 { | |
348 if (converted_->GetFormat() != Orthanc::PixelFormat_Float32) | |
349 { | |
350 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
351 } | |
352 | |
353 unsigned int cropX, cropY, cropWidth, cropHeight; | |
354 GetCrop(cropX, cropY, cropWidth, cropHeight); | |
355 | |
409 | 356 AffineTransform2D t = AffineTransform2D::Combine( |
357 viewTransform, GetTransform(), | |
358 AffineTransform2D::CreateOffset(cropX, cropY)); | |
408 | 359 |
360 Orthanc::ImageAccessor cropped; | |
361 converted_->GetRegion(cropped, cropX, cropY, cropWidth, cropHeight); | |
362 | |
409 | 363 t.Apply(buffer, cropped, interpolation, false); |
408 | 364 } |
365 } | |
366 | |
367 | |
368 virtual bool GetDefaultWindowing(float& center, | |
369 float& width) const | |
370 { | |
371 if (converter_.get() != NULL && | |
372 converter_->HasDefaultWindow()) | |
373 { | |
374 center = static_cast<float>(converter_->GetDefaultWindowCenter()); | |
375 width = static_cast<float>(converter_->GetDefaultWindowWidth()); | |
376 return true; | |
377 } | |
378 else | |
379 { | |
380 return false; | |
381 } | |
382 } | |
383 | |
384 | |
385 virtual bool GetRange(float& minValue, | |
386 float& maxValue) const | |
387 { | |
388 if (converted_.get() != NULL) | |
389 { | |
390 if (converted_->GetFormat() != Orthanc::PixelFormat_Float32) | |
391 { | |
392 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
393 } | |
394 | |
395 Orthanc::ImageProcessing::GetMinMaxFloatValue(minValue, maxValue, *converted_); | |
396 return true; | |
397 } | |
398 else | |
399 { | |
400 return false; | |
401 } | |
402 } | |
403 }; | |
404 | |
405 | |
410
6decc0ba9da5
rename RadiographyScene::Layer as RadiographyLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
409
diff
changeset
|
406 RadiographyLayer& RadiographyScene::RegisterLayer(RadiographyLayer* layer) |
408 | 407 { |
408 if (layer == NULL) | |
409 { | |
410 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
411 } | |
412 | |
410
6decc0ba9da5
rename RadiographyScene::Layer as RadiographyLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
409
diff
changeset
|
413 std::auto_ptr<RadiographyLayer> raii(layer); |
408 | 414 |
415 size_t index = countLayers_++; | |
416 raii->SetIndex(index); | |
417 layers_[index] = raii.release(); | |
418 | |
419 EmitMessage(GeometryChangedMessage(*this)); | |
420 EmitMessage(ContentChangedMessage(*this)); | |
421 | |
422 return *layer; | |
423 } | |
424 | |
425 | |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
426 RadiographyScene::RadiographyScene(MessageBroker& broker) : |
408 | 427 IObserver(broker), |
428 IObservable(broker), | |
429 countLayers_(0), | |
430 hasWindowing_(false), | |
431 windowingCenter_(0), // Dummy initialization | |
432 windowingWidth_(0) // Dummy initialization | |
433 { | |
434 } | |
435 | |
436 | |
437 RadiographyScene::~RadiographyScene() | |
438 { | |
439 for (Layers::iterator it = layers_.begin(); it != layers_.end(); it++) | |
440 { | |
441 assert(it->second != NULL); | |
442 delete it->second; | |
443 } | |
444 } | |
445 | |
446 | |
447 bool RadiographyScene::GetWindowing(float& center, | |
448 float& width) const | |
449 { | |
450 if (hasWindowing_) | |
451 { | |
452 center = windowingCenter_; | |
453 width = windowingWidth_; | |
454 return true; | |
455 } | |
456 else | |
457 { | |
458 return false; | |
459 } | |
460 } | |
461 | |
462 | |
463 void RadiographyScene::GetWindowingWithDefault(float& center, | |
464 float& width) const | |
465 { | |
466 if (!GetWindowing(center, width)) | |
467 { | |
468 center = 128; | |
469 width = 256; | |
470 } | |
471 } | |
472 | |
473 | |
474 void RadiographyScene::SetWindowing(float center, | |
475 float width) | |
476 { | |
477 hasWindowing_ = true; | |
478 windowingCenter_ = center; | |
479 windowingWidth_ = width; | |
480 } | |
481 | |
482 | |
410
6decc0ba9da5
rename RadiographyScene::Layer as RadiographyLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
409
diff
changeset
|
483 RadiographyLayer& RadiographyScene::LoadText(const Orthanc::Font& font, |
6decc0ba9da5
rename RadiographyScene::Layer as RadiographyLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
409
diff
changeset
|
484 const std::string& utf8) |
408 | 485 { |
486 std::auto_ptr<AlphaLayer> alpha(new AlphaLayer(*this)); | |
487 alpha->LoadText(font, utf8); | |
488 | |
489 return RegisterLayer(alpha.release()); | |
490 } | |
491 | |
492 | |
410
6decc0ba9da5
rename RadiographyScene::Layer as RadiographyLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
409
diff
changeset
|
493 RadiographyLayer& RadiographyScene::LoadTestBlock(unsigned int width, |
6decc0ba9da5
rename RadiographyScene::Layer as RadiographyLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
409
diff
changeset
|
494 unsigned int height) |
408 | 495 { |
496 std::auto_ptr<Orthanc::Image> block(new Orthanc::Image(Orthanc::PixelFormat_Grayscale8, width, height, false)); | |
497 | |
498 for (unsigned int padding = 0; | |
499 (width > 2 * padding) && (height > 2 * padding); | |
500 padding++) | |
501 { | |
502 uint8_t color; | |
503 if (255 > 10 * padding) | |
504 { | |
505 color = 255 - 10 * padding; | |
506 } | |
507 else | |
508 { | |
509 color = 0; | |
510 } | |
511 | |
512 Orthanc::ImageAccessor region; | |
513 block->GetRegion(region, padding, padding, width - 2 * padding, height - 2 * padding); | |
514 Orthanc::ImageProcessing::Set(region, color); | |
515 } | |
516 | |
517 std::auto_ptr<AlphaLayer> alpha(new AlphaLayer(*this)); | |
518 alpha->SetAlpha(block.release()); | |
519 | |
520 return RegisterLayer(alpha.release()); | |
521 } | |
522 | |
523 | |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
524 RadiographyLayer& RadiographyScene::LoadDicomFrame(OrthancApiClient& orthanc, |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
525 const std::string& instance, |
410
6decc0ba9da5
rename RadiographyScene::Layer as RadiographyLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
409
diff
changeset
|
526 unsigned int frame, |
6decc0ba9da5
rename RadiographyScene::Layer as RadiographyLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
409
diff
changeset
|
527 bool httpCompression) |
408 | 528 { |
410
6decc0ba9da5
rename RadiographyScene::Layer as RadiographyLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
409
diff
changeset
|
529 RadiographyLayer& layer = RegisterLayer(new DicomLayer); |
408 | 530 |
531 { | |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
532 IWebService::HttpHeaders headers; |
408 | 533 std::string uri = "/instances/" + instance + "/tags"; |
534 | |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
535 orthanc.GetBinaryAsync( |
408 | 536 uri, headers, |
537 new Callable<RadiographyScene, OrthancApiClient::BinaryResponseReadyMessage> | |
538 (*this, &RadiographyScene::OnTagsReceived), NULL, | |
539 new Orthanc::SingleValueObject<size_t>(layer.GetIndex())); | |
540 } | |
541 | |
542 { | |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
543 IWebService::HttpHeaders headers; |
408 | 544 headers["Accept"] = "image/x-portable-arbitrarymap"; |
545 | |
546 if (httpCompression) | |
547 { | |
548 headers["Accept-Encoding"] = "gzip"; | |
549 } | |
550 | |
551 std::string uri = ("/instances/" + instance + "/frames/" + | |
552 boost::lexical_cast<std::string>(frame) + "/image-uint16"); | |
553 | |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
554 orthanc.GetBinaryAsync( |
408 | 555 uri, headers, |
556 new Callable<RadiographyScene, OrthancApiClient::BinaryResponseReadyMessage> | |
557 (*this, &RadiographyScene::OnFrameReceived), NULL, | |
558 new Orthanc::SingleValueObject<size_t>(layer.GetIndex())); | |
559 } | |
560 | |
561 return layer; | |
562 } | |
563 | |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
564 |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
565 RadiographyLayer& RadiographyScene::LoadDicomWebFrame(IWebService& web) |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
566 { |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
567 RadiographyLayer& layer = RegisterLayer(new DicomLayer); |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
568 |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
569 |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
570 return layer; |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
571 } |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
572 |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
573 |
408 | 574 |
575 void RadiographyScene::OnTagsReceived(const OrthancApiClient::BinaryResponseReadyMessage& message) | |
576 { | |
577 size_t index = dynamic_cast<const Orthanc::SingleValueObject<size_t>&> | |
578 (message.GetPayload()).GetValue(); | |
579 | |
580 LOG(INFO) << "JSON received: " << message.GetUri().c_str() | |
581 << " (" << message.GetAnswerSize() << " bytes) for layer " << index; | |
582 | |
583 Layers::iterator layer = layers_.find(index); | |
584 if (layer != layers_.end()) | |
585 { | |
586 assert(layer->second != NULL); | |
587 | |
588 OrthancPlugins::FullOrthancDataset dicom(message.GetAnswer(), message.GetAnswerSize()); | |
589 dynamic_cast<DicomLayer*>(layer->second)->SetDicomTags(dicom); | |
590 | |
591 float c, w; | |
592 if (!hasWindowing_ && | |
593 layer->second->GetDefaultWindowing(c, w)) | |
594 { | |
595 hasWindowing_ = true; | |
596 windowingCenter_ = c; | |
597 windowingWidth_ = w; | |
598 } | |
599 | |
600 EmitMessage(GeometryChangedMessage(*this)); | |
601 } | |
602 } | |
603 | |
604 | |
605 void RadiographyScene::OnFrameReceived(const OrthancApiClient::BinaryResponseReadyMessage& message) | |
606 { | |
607 size_t index = dynamic_cast<const Orthanc::SingleValueObject<size_t>&>(message.GetPayload()).GetValue(); | |
608 | |
609 LOG(INFO) << "DICOM frame received: " << message.GetUri().c_str() | |
610 << " (" << message.GetAnswerSize() << " bytes) for layer " << index; | |
611 | |
612 Layers::iterator layer = layers_.find(index); | |
613 if (layer != layers_.end()) | |
614 { | |
615 assert(layer->second != NULL); | |
616 | |
617 std::string content; | |
618 if (message.GetAnswerSize() > 0) | |
619 { | |
620 content.assign(reinterpret_cast<const char*>(message.GetAnswer()), message.GetAnswerSize()); | |
621 } | |
622 | |
623 std::auto_ptr<Orthanc::PamReader> reader(new Orthanc::PamReader); | |
624 reader->ReadFromMemory(content); | |
625 dynamic_cast<DicomLayer*>(layer->second)->SetSourceImage(reader.release()); | |
626 | |
627 EmitMessage(ContentChangedMessage(*this)); | |
628 } | |
629 } | |
630 | |
631 | |
632 Extent2D RadiographyScene::GetSceneExtent() const | |
633 { | |
634 Extent2D extent; | |
635 | |
636 for (Layers::const_iterator it = layers_.begin(); | |
637 it != layers_.end(); ++it) | |
638 { | |
639 assert(it->second != NULL); | |
640 extent.Union(it->second->GetExtent()); | |
641 } | |
642 | |
643 return extent; | |
644 } | |
645 | |
646 | |
647 void RadiographyScene::Render(Orthanc::ImageAccessor& buffer, | |
409 | 648 const AffineTransform2D& viewTransform, |
408 | 649 ImageInterpolation interpolation) const |
650 { | |
651 Orthanc::ImageProcessing::Set(buffer, 0); | |
652 | |
653 // Render layers in the background-to-foreground order | |
654 for (size_t index = 0; index < countLayers_; index++) | |
655 { | |
656 Layers::const_iterator it = layers_.find(index); | |
657 if (it != layers_.end()) | |
658 { | |
659 assert(it->second != NULL); | |
660 it->second->Render(buffer, viewTransform, interpolation); | |
661 } | |
662 } | |
663 } | |
664 | |
665 | |
666 bool RadiographyScene::LookupLayer(size_t& index /* out */, | |
667 double x, | |
668 double y) const | |
669 { | |
670 // Render layers in the foreground-to-background order | |
671 for (size_t i = countLayers_; i > 0; i--) | |
672 { | |
673 index = i - 1; | |
674 Layers::const_iterator it = layers_.find(index); | |
675 if (it != layers_.end()) | |
676 { | |
677 assert(it->second != NULL); | |
678 if (it->second->Contains(x, y)) | |
679 { | |
680 return true; | |
681 } | |
682 } | |
683 } | |
684 | |
685 return false; | |
686 } | |
687 | |
688 | |
689 void RadiographyScene::DrawBorder(CairoContext& context, | |
690 unsigned int layer, | |
691 double zoom) | |
692 { | |
693 Layers::const_iterator found = layers_.find(layer); | |
694 | |
695 if (found != layers_.end()) | |
696 { | |
697 context.SetSourceColor(255, 0, 0); | |
698 found->second->DrawBorders(context, zoom); | |
699 } | |
700 } | |
701 | |
702 | |
703 void RadiographyScene::GetRange(float& minValue, | |
704 float& maxValue) const | |
705 { | |
706 bool first = true; | |
707 | |
708 for (Layers::const_iterator it = layers_.begin(); | |
709 it != layers_.end(); it++) | |
710 { | |
711 assert(it->second != NULL); | |
712 | |
713 float a, b; | |
714 if (it->second->GetRange(a, b)) | |
715 { | |
716 if (first) | |
717 { | |
718 minValue = a; | |
719 maxValue = b; | |
720 first = false; | |
721 } | |
722 else | |
723 { | |
724 minValue = std::min(a, minValue); | |
725 maxValue = std::max(b, maxValue); | |
726 } | |
727 } | |
728 } | |
729 | |
730 if (first) | |
731 { | |
732 minValue = 0; | |
733 maxValue = 0; | |
734 } | |
735 } | |
736 | |
737 | |
738 // Export using PAM is faster than using PNG, but requires Orthanc | |
739 // core >= 1.4.3 | |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
740 void RadiographyScene::ExportDicom(OrthancApiClient& orthanc, |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
741 const Orthanc::DicomMap& dicom, |
408 | 742 double pixelSpacingX, |
743 double pixelSpacingY, | |
744 bool invert, | |
745 ImageInterpolation interpolation, | |
746 bool usePam) | |
747 { | |
748 if (pixelSpacingX <= 0 || | |
749 pixelSpacingY <= 0) | |
750 { | |
751 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
752 } | |
753 | |
754 LOG(INFO) << "Exporting DICOM"; | |
755 | |
756 Extent2D extent = GetSceneExtent(); | |
757 | |
758 int w = std::ceil(extent.GetWidth() / pixelSpacingX); | |
759 int h = std::ceil(extent.GetHeight() / pixelSpacingY); | |
760 | |
761 if (w < 0 || h < 0) | |
762 { | |
763 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
764 } | |
765 | |
766 Orthanc::Image layers(Orthanc::PixelFormat_Float32, | |
767 static_cast<unsigned int>(w), | |
768 static_cast<unsigned int>(h), false); | |
769 | |
409 | 770 AffineTransform2D view = AffineTransform2D::Combine( |
771 AffineTransform2D::CreateScaling(1.0 / pixelSpacingX, 1.0 / pixelSpacingY), | |
772 AffineTransform2D::CreateOffset(-extent.GetX1(), -extent.GetY1())); | |
408 | 773 |
774 Render(layers, view, interpolation); | |
775 | |
776 Orthanc::Image rendered(Orthanc::PixelFormat_Grayscale16, | |
777 layers.GetWidth(), layers.GetHeight(), false); | |
778 Orthanc::ImageProcessing::Convert(rendered, layers); | |
779 | |
780 std::string base64; | |
781 | |
782 { | |
783 std::string content; | |
784 | |
785 if (usePam) | |
786 { | |
787 Orthanc::PamWriter writer; | |
788 writer.WriteToMemory(content, rendered); | |
789 } | |
790 else | |
791 { | |
792 Orthanc::PngWriter writer; | |
793 writer.WriteToMemory(content, rendered); | |
794 } | |
795 | |
796 Orthanc::Toolbox::EncodeBase64(base64, content); | |
797 } | |
798 | |
799 std::set<Orthanc::DicomTag> tags; | |
800 dicom.GetTags(tags); | |
801 | |
802 Json::Value json = Json::objectValue; | |
803 json["Tags"] = Json::objectValue; | |
804 | |
805 for (std::set<Orthanc::DicomTag>::const_iterator | |
806 tag = tags.begin(); tag != tags.end(); ++tag) | |
807 { | |
808 const Orthanc::DicomValue& value = dicom.GetValue(*tag); | |
809 if (!value.IsNull() && | |
810 !value.IsBinary()) | |
811 { | |
812 json["Tags"][tag->Format()] = value.GetContent(); | |
813 } | |
814 } | |
815 | |
816 json["Tags"][Orthanc::DICOM_TAG_PHOTOMETRIC_INTERPRETATION.Format()] = | |
817 (invert ? "MONOCHROME1" : "MONOCHROME2"); | |
818 | |
819 // WARNING: The order of PixelSpacing is Y/X. We use "%0.8f" to | |
820 // avoid floating-point numbers to grow over 16 characters, | |
821 // which would be invalid according to DICOM standard | |
822 // ("dciodvfy" would complain). | |
823 char buf[32]; | |
824 sprintf(buf, "%0.8f\\%0.8f", pixelSpacingY, pixelSpacingX); | |
825 | |
826 json["Tags"][Orthanc::DICOM_TAG_PIXEL_SPACING.Format()] = buf; | |
827 | |
828 float center, width; | |
829 if (GetWindowing(center, width)) | |
830 { | |
831 json["Tags"][Orthanc::DICOM_TAG_WINDOW_CENTER.Format()] = | |
832 boost::lexical_cast<std::string>(boost::math::iround(center)); | |
833 | |
834 json["Tags"][Orthanc::DICOM_TAG_WINDOW_WIDTH.Format()] = | |
835 boost::lexical_cast<std::string>(boost::math::iround(width)); | |
836 } | |
837 | |
838 // This is Data URI scheme: https://en.wikipedia.org/wiki/Data_URI_scheme | |
839 json["Content"] = ("data:" + | |
840 std::string(usePam ? Orthanc::MIME_PAM : Orthanc::MIME_PNG) + | |
841 ";base64," + base64); | |
842 | |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
843 orthanc.PostJsonAsyncExpectJson( |
408 | 844 "/tools/create-dicom", json, |
845 new Callable<RadiographyScene, OrthancApiClient::JsonResponseReadyMessage> | |
846 (*this, &RadiographyScene::OnDicomExported), | |
847 NULL, NULL); | |
848 } | |
849 | |
850 | |
851 void RadiographyScene::OnDicomExported(const OrthancApiClient::JsonResponseReadyMessage& message) | |
852 { | |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
853 LOG(INFO) << "DICOM export was successful: " |
408 | 854 << message.GetJson().toStyledString(); |
855 } | |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
856 |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
857 |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
858 void RadiographyScene::OnDicomWebReceived(const IWebService::HttpRequestSuccessMessage& message) |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
859 { |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
860 LOG(INFO) << "DICOMweb WADO-RS received: " << message.GetAnswerSize() << " bytes"; |
418 | 861 |
862 const IWebService::HttpHeaders& h = message.GetAnswerHttpHeaders(); | |
863 for (IWebService::HttpHeaders::const_iterator | |
864 it = h.begin(); it != h.end(); ++it) | |
865 { | |
866 printf("[%s] = [%s]\n", it->first.c_str(), it->second.c_str()); | |
867 } | |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
868 } |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
412
diff
changeset
|
869 |
408 | 870 } |