annotate Framework/Radiography/RadiographyAlphaLayer.cpp @ 981:c20dbaab360c

Ability to cope with empty "Referenced SOP Instance UID" (dicom path (3006,0039)[i] / (0x3006, 0x0040)[0] / (0x3006, 0x0016)[0] / (0x0008, 0x1155)) + better logs + code formating
author Benjamin Golinvaux <bgo@osimis.io>
date Fri, 06 Sep 2019 09:38:18 +0200
parents 42dadae61fa9
children a5f2a6b04a31
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
430
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
1 /**
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
2 * Stone of Orthanc
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
6 *
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
10 * the License, or (at your option) any later version.
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
11 *
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
15 * Affero General Public License for more details.
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
16 *
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
19 **/
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
20
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
21
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
22 #include "RadiographyAlphaLayer.h"
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
23
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
24 #include "RadiographyScene.h"
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
25
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
26 #include <Core/Images/Image.h>
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
27 #include <Core/OrthancException.h>
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
28
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
29 namespace OrthancStone
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
30 {
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
31
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
32 void RadiographyAlphaLayer::SetAlpha(Orthanc::ImageAccessor* image)
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
33 {
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
34 std::auto_ptr<Orthanc::ImageAccessor> raii(image);
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
35
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
36 if (image == NULL)
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
37 {
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
38 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
39 }
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
40
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
41 if (image->GetFormat() != Orthanc::PixelFormat_Grayscale8)
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
42 {
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
43 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat);
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
44 }
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
45
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
46 SetSize(image->GetWidth(), image->GetHeight());
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
47 alpha_ = raii;
503
77e0eb83ff63 layers are now Observable and emitting LayerEdited messages
amazy
parents: 432
diff changeset
48
623
42dadae61fa9 renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 503
diff changeset
49 BroadcastMessage(RadiographyLayer::LayerEditedMessage(*this));
430
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
50 }
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
51
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
52 void RadiographyAlphaLayer::Render(Orthanc::ImageAccessor& buffer,
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
53 const AffineTransform2D& viewTransform,
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
54 ImageInterpolation interpolation) const
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
55 {
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
56 if (alpha_.get() == NULL)
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
57 {
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
58 return;
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
59 }
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
60
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
61 if (buffer.GetFormat() != Orthanc::PixelFormat_Float32)
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
62 {
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
63 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat);
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
64 }
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
65
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
66 unsigned int cropX, cropY, cropWidth, cropHeight;
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
67 GetCrop(cropX, cropY, cropWidth, cropHeight);
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
68
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
69 const AffineTransform2D t = AffineTransform2D::Combine(
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
70 viewTransform, GetTransform(),
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
71 AffineTransform2D::CreateOffset(cropX, cropY));
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
72
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
73 Orthanc::ImageAccessor cropped;
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
74 alpha_->GetRegion(cropped, cropX, cropY, cropWidth, cropHeight);
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
75
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
76 Orthanc::Image tmp(Orthanc::PixelFormat_Grayscale8, buffer.GetWidth(), buffer.GetHeight(), false);
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
77
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
78 t.Apply(tmp, cropped, interpolation, true /* clear */);
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
79
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
80 // Blit
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
81 const unsigned int width = buffer.GetWidth();
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
82 const unsigned int height = buffer.GetHeight();
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
83
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
84 float value = foreground_;
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
85
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
86 if (useWindowing_)
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
87 {
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
88 float center, width;
503
77e0eb83ff63 layers are now Observable and emitting LayerEdited messages
amazy
parents: 432
diff changeset
89 if (GetScene().GetWindowing(center, width))
430
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
90 {
432
4eb96c6b4e96 improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents: 430
diff changeset
91 value = center + width / 2.0f; // set it to the maximum pixel value of the image
430
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
92 }
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
93 }
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
94
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
95 for (unsigned int y = 0; y < height; y++)
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
96 {
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
97 float *q = reinterpret_cast<float*>(buffer.GetRow(y));
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
98 const uint8_t *p = reinterpret_cast<uint8_t*>(tmp.GetRow(y));
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
99
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
100 for (unsigned int x = 0; x < width; x++, p++, q++)
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
101 {
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
102 float a = static_cast<float>(*p) / 255.0f;
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
103
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
104 *q = (a * value + (1.0f - a) * (*q));
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
105 }
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
106 }
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
107 }
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
108
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
109 bool RadiographyAlphaLayer::GetRange(float& minValue,
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
110 float& maxValue) const
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
111 {
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
112 if (useWindowing_)
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
113 {
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
114 return false;
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
115 }
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
116 else
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
117 {
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
118 minValue = 0;
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
119 maxValue = 0;
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
120
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
121 if (foreground_ < 0)
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
122 {
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
123 minValue = foreground_;
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
124 }
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
125
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
126 if (foreground_ > 0)
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
127 {
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
128 maxValue = foreground_;
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
129 }
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
130
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
131 return true;
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
132 }
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
133 }
b85f635f1eb5 added serialization for RadiographyScene
am@osimis.io
parents:
diff changeset
134 }