Mercurial > hg > orthanc-stone
annotate Framework/Radiography/RadiographyWidget.cpp @ 1100:08cc0d47aa94 broker
exception handling in IObservable::EmitMessage()
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 25 Oct 2019 18:06:30 +0200 |
parents | 05b2e71ed145 |
children | 383aa2a7d426 |
rev | line source |
---|---|
413 | 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 |
413 | 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 | |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
24 #include <Core/OrthancException.h> |
413 | 25 #include <Core/Images/Image.h> |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
26 #include <Core/Images/ImageProcessing.h> |
413 | 27 |
492 | 28 #include "RadiographyMaskLayer.h" |
413 | 29 |
30 namespace OrthancStone | |
31 { | |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
32 |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
33 bool RadiographyWidget::IsInvertedInternal() const |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
34 { |
516
11fa6f00e33c
Dummy changes (warnings, dummy dtor to check proper deletion, line wrapping) +
Benjamin Golinvaux <bgo@osimis.io>
parents:
492
diff
changeset
|
35 // MONOCHROME1 images must be inverted and the user can invert the |
11fa6f00e33c
Dummy changes (warnings, dummy dtor to check proper deletion, line wrapping) +
Benjamin Golinvaux <bgo@osimis.io>
parents:
492
diff
changeset
|
36 // image, too -> XOR the two |
11fa6f00e33c
Dummy changes (warnings, dummy dtor to check proper deletion, line wrapping) +
Benjamin Golinvaux <bgo@osimis.io>
parents:
492
diff
changeset
|
37 return (scene_->GetPreferredPhotomotricDisplayMode() == |
739
be9c1530d40a
deprecating enum SliceImageQuality
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
726
diff
changeset
|
38 RadiographyPhotometricDisplayMode_Monochrome1) ^ invert_; |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
39 } |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
40 |
516
11fa6f00e33c
Dummy changes (warnings, dummy dtor to check proper deletion, line wrapping) +
Benjamin Golinvaux <bgo@osimis.io>
parents:
492
diff
changeset
|
41 void RadiographyWidget::RenderBackground( |
11fa6f00e33c
Dummy changes (warnings, dummy dtor to check proper deletion, line wrapping) +
Benjamin Golinvaux <bgo@osimis.io>
parents:
492
diff
changeset
|
42 Orthanc::ImageAccessor& image, float minValue, float maxValue) |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
43 { |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
44 // wipe background before rendering |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
45 float backgroundValue = minValue; |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
46 |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
47 switch (scene_->GetPreferredPhotomotricDisplayMode()) |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
48 { |
739
be9c1530d40a
deprecating enum SliceImageQuality
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
726
diff
changeset
|
49 case RadiographyPhotometricDisplayMode_Monochrome1: |
be9c1530d40a
deprecating enum SliceImageQuality
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
726
diff
changeset
|
50 case RadiographyPhotometricDisplayMode_Default: |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
51 if (IsInvertedInternal()) |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
52 backgroundValue = maxValue; |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
53 else |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
54 backgroundValue = minValue; |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
55 break; |
739
be9c1530d40a
deprecating enum SliceImageQuality
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
726
diff
changeset
|
56 case RadiographyPhotometricDisplayMode_Monochrome2: |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
57 if (IsInvertedInternal()) |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
58 backgroundValue = minValue; |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
59 else |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
60 backgroundValue = maxValue; |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
61 break; |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
62 default: |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
63 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
64 } |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
65 |
516
11fa6f00e33c
Dummy changes (warnings, dummy dtor to check proper deletion, line wrapping) +
Benjamin Golinvaux <bgo@osimis.io>
parents:
492
diff
changeset
|
66 Orthanc::ImageProcessing::Set(image, static_cast<int64_t>(backgroundValue)); |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
67 } |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
68 |
413 | 69 bool RadiographyWidget::RenderInternal(unsigned int width, |
70 unsigned int height, | |
71 ImageInterpolation interpolation) | |
72 { | |
73 float windowCenter, windowWidth; | |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
74 scene_->GetWindowingWithDefault(windowCenter, windowWidth); |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
75 |
413 | 76 float x0 = windowCenter - windowWidth / 2.0f; |
77 float x1 = windowCenter + windowWidth / 2.0f; | |
78 | |
79 if (windowWidth <= 0.001f) // Avoid division by zero at (*) | |
80 { | |
81 return false; | |
82 } | |
83 else | |
84 { | |
85 if (floatBuffer_.get() == NULL || | |
86 floatBuffer_->GetWidth() != width || | |
87 floatBuffer_->GetHeight() != height) | |
88 { | |
516
11fa6f00e33c
Dummy changes (warnings, dummy dtor to check proper deletion, line wrapping) +
Benjamin Golinvaux <bgo@osimis.io>
parents:
492
diff
changeset
|
89 floatBuffer_.reset(new Orthanc::Image( |
11fa6f00e33c
Dummy changes (warnings, dummy dtor to check proper deletion, line wrapping) +
Benjamin Golinvaux <bgo@osimis.io>
parents:
492
diff
changeset
|
90 Orthanc::PixelFormat_Float32, width, height, false)); |
413 | 91 } |
92 | |
93 if (cairoBuffer_.get() == NULL || | |
94 cairoBuffer_->GetWidth() != width || | |
95 cairoBuffer_->GetHeight() != height) | |
96 { | |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
97 cairoBuffer_.reset(new CairoSurface(width, height, false /* no alpha */)); |
413 | 98 } |
99 | |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
100 RenderBackground(*floatBuffer_, x0, x1); |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
101 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
102 scene_->Render(*floatBuffer_, GetView().GetMatrix(), interpolation); |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
103 |
413 | 104 // Conversion from Float32 to BGRA32 (cairo). Very similar to |
105 // GrayscaleFrameRenderer => TODO MERGE? | |
106 | |
107 Orthanc::ImageAccessor target; | |
108 cairoBuffer_->GetWriteableAccessor(target); | |
109 | |
110 float scaling = 255.0f / (x1 - x0); | |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
111 |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
112 bool invert = IsInvertedInternal(); |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
113 |
413 | 114 for (unsigned int y = 0; y < height; y++) |
115 { | |
116 const float* p = reinterpret_cast<const float*>(floatBuffer_->GetConstRow(y)); | |
117 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | |
118 | |
119 for (unsigned int x = 0; x < width; x++, p++, q += 4) | |
120 { | |
121 uint8_t v = 0; | |
122 if (*p >= x1) | |
123 { | |
124 v = 255; | |
125 } | |
126 else if (*p <= x0) | |
127 { | |
128 v = 0; | |
129 } | |
130 else | |
131 { | |
132 // https://en.wikipedia.org/wiki/Linear_interpolation | |
133 v = static_cast<uint8_t>(scaling * (*p - x0)); // (*) | |
134 } | |
135 | |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
136 if (invert) |
413 | 137 { |
138 v = 255 - v; | |
139 } | |
140 | |
141 q[0] = v; | |
142 q[1] = v; | |
143 q[2] = v; | |
144 q[3] = 255; | |
145 } | |
146 } | |
147 | |
148 return true; | |
149 } | |
150 } | |
151 | |
152 | |
153 bool RadiographyWidget::RenderScene(CairoContext& context, | |
726
4f2416d519b4
moving layers, widgets and loaders to Deprecated namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
561
diff
changeset
|
154 const Deprecated::ViewportGeometry& view) |
413 | 155 { |
156 cairo_t* cr = context.GetObject(); | |
157 | |
158 if (RenderInternal(context.GetWidth(), context.GetHeight(), interpolation_)) | |
159 { | |
160 // https://www.cairographics.org/FAQ/#paint_from_a_surface | |
161 cairo_save(cr); | |
162 cairo_identity_matrix(cr); | |
163 cairo_set_source_surface(cr, cairoBuffer_->GetObject(), 0, 0); | |
164 cairo_paint(cr); | |
165 cairo_restore(cr); | |
166 } | |
167 else | |
168 { | |
169 // https://www.cairographics.org/FAQ/#clear_a_surface | |
170 context.SetSourceColor(0, 0, 0); | |
171 cairo_paint(cr); | |
172 } | |
173 | |
174 if (hasSelection_) | |
175 { | |
516
11fa6f00e33c
Dummy changes (warnings, dummy dtor to check proper deletion, line wrapping) +
Benjamin Golinvaux <bgo@osimis.io>
parents:
492
diff
changeset
|
176 scene_->DrawBorder( |
11fa6f00e33c
Dummy changes (warnings, dummy dtor to check proper deletion, line wrapping) +
Benjamin Golinvaux <bgo@osimis.io>
parents:
492
diff
changeset
|
177 context, static_cast<unsigned int>(selectedLayer_), view.GetZoom()); |
413 | 178 } |
179 | |
180 return true; | |
181 } | |
182 | |
183 | |
1066
b537002f83a9
removing broker from deprecated classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
739
diff
changeset
|
184 RadiographyWidget::RadiographyWidget(boost::shared_ptr<RadiographyScene> scene, |
413 | 185 const std::string& name) : |
186 WorldSceneWidget(name), | |
187 invert_(false), | |
188 interpolation_(ImageInterpolation_Nearest), | |
189 hasSelection_(false), | |
190 selectedLayer_(0) // Dummy initialization | |
191 { | |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
192 SetScene(scene); |
413 | 193 } |
194 | |
195 | |
196 void RadiographyWidget::Select(size_t layer) | |
197 { | |
198 hasSelection_ = true; | |
199 selectedLayer_ = layer; | |
200 } | |
201 | |
492 | 202 bool RadiographyWidget::SelectMaskLayer(size_t index) |
203 { | |
204 std::vector<size_t> layerIndexes; | |
205 size_t count = 0; | |
206 scene_->GetLayersIndexes(layerIndexes); | |
207 | |
208 for (size_t i = 0; i < layerIndexes.size(); ++i) | |
209 { | |
210 const RadiographyMaskLayer* maskLayer = dynamic_cast<const RadiographyMaskLayer*>(&(scene_->GetLayer(layerIndexes[i]))); | |
211 if (maskLayer != NULL) | |
212 { | |
213 if (count == index) | |
214 { | |
215 Select(layerIndexes[i]); | |
216 return true; | |
217 } | |
218 count++; | |
219 } | |
220 } | |
221 | |
222 return false; | |
223 } | |
413 | 224 |
225 bool RadiographyWidget::LookupSelectedLayer(size_t& layer) | |
226 { | |
227 if (hasSelection_) | |
228 { | |
229 layer = selectedLayer_; | |
230 return true; | |
231 } | |
232 else | |
233 { | |
234 return false; | |
235 } | |
236 } | |
237 | |
238 | |
239 void RadiographyWidget::OnGeometryChanged(const RadiographyScene::GeometryChangedMessage& message) | |
240 { | |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
241 LOG(INFO) << "Scene geometry has changed"; |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
242 |
413 | 243 FitContent(); |
244 } | |
245 | |
246 | |
247 void RadiographyWidget::OnContentChanged(const RadiographyScene::ContentChangedMessage& message) | |
248 { | |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
249 LOG(INFO) << "Scene content has changed"; |
413 | 250 NotifyContentChanged(); |
251 } | |
252 | |
253 | |
254 void RadiographyWidget::SetInvert(bool invert) | |
255 { | |
256 if (invert_ != invert) | |
257 { | |
258 invert_ = invert; | |
259 NotifyContentChanged(); | |
260 } | |
261 } | |
262 | |
263 | |
264 void RadiographyWidget::SwitchInvert() | |
265 { | |
266 invert_ = !invert_; | |
267 NotifyContentChanged(); | |
268 } | |
269 | |
270 | |
271 void RadiographyWidget::SetInterpolation(ImageInterpolation interpolation) | |
272 { | |
273 if (interpolation_ != interpolation) | |
274 { | |
275 interpolation_ = interpolation; | |
276 NotifyContentChanged(); | |
277 } | |
278 } | |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
279 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
280 void RadiographyWidget::SetScene(boost::shared_ptr<RadiographyScene> scene) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
281 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
282 scene_ = scene; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
283 |
1066
b537002f83a9
removing broker from deprecated classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
739
diff
changeset
|
284 Register<RadiographyScene::GeometryChangedMessage>(*scene_, &RadiographyWidget::OnGeometryChanged); |
b537002f83a9
removing broker from deprecated classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
739
diff
changeset
|
285 Register<RadiographyScene::ContentChangedMessage>(*scene_, &RadiographyWidget::OnContentChanged); |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
286 |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
287 NotifyContentChanged(); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
288 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
289 // force redraw |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
290 FitContent(); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
291 } |
413 | 292 } |