Mercurial > hg > orthanc-stone
comparison Framework/Deprecated/Radiography/RadiographyWidget.cpp @ 1398:c5403d52078c
moved Radiography into Deprecated
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Wed, 29 Apr 2020 20:43:09 +0200 |
parents | Framework/Radiography/RadiographyWidget.cpp@7ec8fea061b9 |
children | 30deba7bc8e2 |
comparison
equal
deleted
inserted
replaced
1397:1c2d065ba372 | 1398:c5403d52078c |
---|---|
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 "RadiographyWidget.h" | |
23 | |
24 #include <Core/OrthancException.h> | |
25 #include <Core/Images/Image.h> | |
26 #include <Core/Images/ImageProcessing.h> | |
27 | |
28 #include "RadiographyMaskLayer.h" | |
29 | |
30 namespace OrthancStone | |
31 { | |
32 | |
33 bool RadiographyWidget::IsInvertedInternal() const | |
34 { | |
35 // MONOCHROME1 images must be inverted and the user can invert the | |
36 // image, too -> XOR the two | |
37 return (scene_->GetPreferredPhotomotricDisplayMode() == | |
38 RadiographyPhotometricDisplayMode_Monochrome1) ^ invert_; | |
39 } | |
40 | |
41 void RadiographyWidget::RenderBackground( | |
42 Orthanc::ImageAccessor& image, float minValue, float maxValue) | |
43 { | |
44 // wipe background before rendering | |
45 float backgroundValue = minValue; | |
46 | |
47 switch (scene_->GetPreferredPhotomotricDisplayMode()) | |
48 { | |
49 case RadiographyPhotometricDisplayMode_Monochrome1: | |
50 case RadiographyPhotometricDisplayMode_Default: | |
51 if (IsInvertedInternal()) | |
52 backgroundValue = maxValue; | |
53 else | |
54 backgroundValue = minValue; | |
55 break; | |
56 case RadiographyPhotometricDisplayMode_Monochrome2: | |
57 if (IsInvertedInternal()) | |
58 backgroundValue = minValue; | |
59 else | |
60 backgroundValue = maxValue; | |
61 break; | |
62 default: | |
63 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
64 } | |
65 | |
66 Orthanc::ImageProcessing::Set(image, static_cast<int64_t>(backgroundValue)); | |
67 } | |
68 | |
69 bool RadiographyWidget::RenderInternal(unsigned int width, | |
70 unsigned int height, | |
71 ImageInterpolation interpolation) | |
72 { | |
73 if (floatBuffer_.get() == NULL || | |
74 floatBuffer_->GetWidth() != width || | |
75 floatBuffer_->GetHeight() != height) | |
76 { | |
77 floatBuffer_.reset(new Orthanc::Image( | |
78 Orthanc::PixelFormat_Float32, width, height, false)); | |
79 | |
80 if (floatBuffer_.get() == NULL) | |
81 { | |
82 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotEnoughMemory, "RadiographyWidget::RenderInternal: unable to allocate float buffer"); | |
83 } | |
84 } | |
85 | |
86 if (cairoBuffer_.get() == NULL || | |
87 cairoBuffer_->GetWidth() != width || | |
88 cairoBuffer_->GetHeight() != height) | |
89 { | |
90 cairoBuffer_.reset(new CairoSurface(width, height, false /* no alpha */)); | |
91 | |
92 if (cairoBuffer_.get() == NULL) | |
93 { | |
94 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotEnoughMemory, "RadiographyWidget::RenderInternal: unable to allocate cairo buffer"); | |
95 } | |
96 } | |
97 | |
98 RenderBackground(*floatBuffer_, 0.0, 65535.0); | |
99 | |
100 scene_->Render(*floatBuffer_, GetView().GetMatrix(), interpolation, true); | |
101 | |
102 // Conversion from Float32 to BGRA32 (cairo). Very similar to | |
103 // GrayscaleFrameRenderer => TODO MERGE? | |
104 Orthanc::ImageAccessor target; | |
105 cairoBuffer_->GetWriteableAccessor(target); | |
106 | |
107 bool invert = IsInvertedInternal(); | |
108 | |
109 for (unsigned int y = 0; y < height; y++) | |
110 { | |
111 const float* p = reinterpret_cast<const float*>(floatBuffer_->GetConstRow(y)); | |
112 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | |
113 | |
114 for (unsigned int x = 0; x < width; x++, p++, q += 4) | |
115 { | |
116 uint8_t v = 0; | |
117 if (*p >= 65535.0) | |
118 { | |
119 v = 255; | |
120 } | |
121 else if (*p <= 0.0) | |
122 { | |
123 v = 0; | |
124 } | |
125 else | |
126 { | |
127 v = static_cast<uint8_t>(*p / 256.0); | |
128 } | |
129 | |
130 if (invert) | |
131 { | |
132 v = 255 - v; | |
133 } | |
134 | |
135 q[0] = v; | |
136 q[1] = v; | |
137 q[2] = v; | |
138 q[3] = 255; | |
139 } | |
140 } | |
141 | |
142 return true; | |
143 } | |
144 | |
145 | |
146 bool RadiographyWidget::RenderScene(CairoContext& context, | |
147 const Deprecated::ViewportGeometry& view) | |
148 { | |
149 cairo_t* cr = context.GetObject(); | |
150 | |
151 if (RenderInternal(context.GetWidth(), context.GetHeight(), interpolation_)) | |
152 { | |
153 // https://www.cairographics.org/FAQ/#paint_from_a_surface | |
154 cairo_save(cr); | |
155 cairo_identity_matrix(cr); | |
156 cairo_set_source_surface(cr, cairoBuffer_->GetObject(), 0, 0); | |
157 cairo_paint(cr); | |
158 cairo_restore(cr); | |
159 } | |
160 else | |
161 { | |
162 // https://www.cairographics.org/FAQ/#clear_a_surface | |
163 context.SetSourceColor(0, 0, 0); | |
164 cairo_paint(cr); | |
165 } | |
166 | |
167 if (hasSelection_) | |
168 { | |
169 scene_->DrawBorder( | |
170 context, static_cast<unsigned int>(selectedLayer_), view.GetZoom()); | |
171 } | |
172 | |
173 return true; | |
174 } | |
175 | |
176 | |
177 RadiographyWidget::RadiographyWidget(boost::shared_ptr<RadiographyScene> scene, | |
178 const std::string& name) : | |
179 WorldSceneWidget(name), | |
180 invert_(false), | |
181 interpolation_(ImageInterpolation_Nearest), | |
182 hasSelection_(false), | |
183 selectedLayer_(0) // Dummy initialization | |
184 { | |
185 SetScene(scene); | |
186 } | |
187 | |
188 | |
189 void RadiographyWidget::Select(size_t layer) | |
190 { | |
191 hasSelection_ = true; | |
192 selectedLayer_ = layer; | |
193 | |
194 NotifyContentChanged(); | |
195 BroadcastMessage(SelectionChangedMessage(*this)); | |
196 } | |
197 | |
198 void RadiographyWidget::Unselect() | |
199 { | |
200 hasSelection_ = false; | |
201 | |
202 NotifyContentChanged(); | |
203 BroadcastMessage(SelectionChangedMessage(*this)); | |
204 } | |
205 | |
206 bool RadiographyWidget::LookupSelectedLayer(size_t& layer) const | |
207 { | |
208 if (hasSelection_) | |
209 { | |
210 layer = selectedLayer_; | |
211 return true; | |
212 } | |
213 else | |
214 { | |
215 return false; | |
216 } | |
217 } | |
218 | |
219 | |
220 void RadiographyWidget::OnGeometryChanged(const RadiographyScene::GeometryChangedMessage& message) | |
221 { | |
222 // LOG(INFO) << "Scene geometry has changed"; | |
223 FitContent(); | |
224 } | |
225 | |
226 | |
227 void RadiographyWidget::OnContentChanged(const RadiographyScene::ContentChangedMessage& message) | |
228 { | |
229 // LOG(INFO) << "Scene content has changed"; | |
230 NotifyContentChanged(); | |
231 } | |
232 | |
233 void RadiographyWidget::OnLayerRemoved(const RadiographyScene::LayerRemovedMessage& message) | |
234 { | |
235 size_t removedLayerIndex = message.GetLayerIndex(); | |
236 if (hasSelection_ && selectedLayer_ == removedLayerIndex) | |
237 { | |
238 Unselect(); | |
239 } | |
240 NotifyContentChanged(); | |
241 } | |
242 | |
243 void RadiographyWidget::SetInvert(bool invert) | |
244 { | |
245 if (invert_ != invert) | |
246 { | |
247 invert_ = invert; | |
248 NotifyContentChanged(); | |
249 } | |
250 } | |
251 | |
252 | |
253 void RadiographyWidget::SwitchInvert() | |
254 { | |
255 invert_ = !invert_; | |
256 NotifyContentChanged(); | |
257 } | |
258 | |
259 | |
260 void RadiographyWidget::SetInterpolation(ImageInterpolation interpolation) | |
261 { | |
262 if (interpolation_ != interpolation) | |
263 { | |
264 interpolation_ = interpolation; | |
265 NotifyContentChanged(); | |
266 } | |
267 } | |
268 | |
269 void RadiographyWidget::SetScene(boost::shared_ptr<RadiographyScene> scene) | |
270 { | |
271 scene_ = scene; | |
272 | |
273 Register<RadiographyScene::GeometryChangedMessage>(*scene_, &RadiographyWidget::OnGeometryChanged); | |
274 Register<RadiographyScene::ContentChangedMessage>(*scene_, &RadiographyWidget::OnContentChanged); | |
275 Register<RadiographyScene::LayerRemovedMessage>(*scene_, &RadiographyWidget::OnLayerRemoved); | |
276 | |
277 Unselect(); | |
278 | |
279 NotifyContentChanged(); | |
280 | |
281 // force redraw | |
282 FitContent(); | |
283 } | |
284 } |