Mercurial > hg > orthanc-stone
annotate Framework/Radiography/RadiographyWidget.cpp @ 871:4bc8d9609447
Added support for measuring tool edition in TrackerSampleApp
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Tue, 25 Jun 2019 18:17:33 +0200 |
parents | be9c1530d40a |
children | b537002f83a9 d33ae2b0db9d |
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 | |
184 RadiographyWidget::RadiographyWidget(MessageBroker& broker, | |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
185 boost::shared_ptr<RadiographyScene> scene, |
413 | 186 const std::string& name) : |
187 WorldSceneWidget(name), | |
188 IObserver(broker), | |
189 invert_(false), | |
190 interpolation_(ImageInterpolation_Nearest), | |
191 hasSelection_(false), | |
192 selectedLayer_(0) // Dummy initialization | |
193 { | |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
194 SetScene(scene); |
413 | 195 } |
196 | |
197 | |
198 void RadiographyWidget::Select(size_t layer) | |
199 { | |
200 hasSelection_ = true; | |
201 selectedLayer_ = layer; | |
202 } | |
203 | |
492 | 204 bool RadiographyWidget::SelectMaskLayer(size_t index) |
205 { | |
206 std::vector<size_t> layerIndexes; | |
207 size_t count = 0; | |
208 scene_->GetLayersIndexes(layerIndexes); | |
209 | |
210 for (size_t i = 0; i < layerIndexes.size(); ++i) | |
211 { | |
212 const RadiographyMaskLayer* maskLayer = dynamic_cast<const RadiographyMaskLayer*>(&(scene_->GetLayer(layerIndexes[i]))); | |
213 if (maskLayer != NULL) | |
214 { | |
215 if (count == index) | |
216 { | |
217 Select(layerIndexes[i]); | |
218 return true; | |
219 } | |
220 count++; | |
221 } | |
222 } | |
223 | |
224 return false; | |
225 } | |
413 | 226 |
227 bool RadiographyWidget::LookupSelectedLayer(size_t& layer) | |
228 { | |
229 if (hasSelection_) | |
230 { | |
231 layer = selectedLayer_; | |
232 return true; | |
233 } | |
234 else | |
235 { | |
236 return false; | |
237 } | |
238 } | |
239 | |
240 | |
241 void RadiographyWidget::OnGeometryChanged(const RadiographyScene::GeometryChangedMessage& message) | |
242 { | |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
243 LOG(INFO) << "Scene geometry has changed"; |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
244 |
413 | 245 FitContent(); |
246 } | |
247 | |
248 | |
249 void RadiographyWidget::OnContentChanged(const RadiographyScene::ContentChangedMessage& message) | |
250 { | |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
251 LOG(INFO) << "Scene content has changed"; |
413 | 252 NotifyContentChanged(); |
253 } | |
254 | |
255 | |
256 void RadiographyWidget::SetInvert(bool invert) | |
257 { | |
258 if (invert_ != invert) | |
259 { | |
260 invert_ = invert; | |
261 NotifyContentChanged(); | |
262 } | |
263 } | |
264 | |
265 | |
266 void RadiographyWidget::SwitchInvert() | |
267 { | |
268 invert_ = !invert_; | |
269 NotifyContentChanged(); | |
270 } | |
271 | |
272 | |
273 void RadiographyWidget::SetInterpolation(ImageInterpolation interpolation) | |
274 { | |
275 if (interpolation_ != interpolation) | |
276 { | |
277 interpolation_ = interpolation; | |
278 NotifyContentChanged(); | |
279 } | |
280 } | |
428
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 void RadiographyWidget::SetScene(boost::shared_ptr<RadiographyScene> scene) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
283 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
284 if (scene_ != NULL) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
285 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
286 scene_->Unregister(this); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
287 } |
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 scene_ = scene; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
290 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
291 scene_->RegisterObserverCallback( |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
292 new Callable<RadiographyWidget, RadiographyScene::GeometryChangedMessage> |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
293 (*this, &RadiographyWidget::OnGeometryChanged)); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
294 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
295 scene_->RegisterObserverCallback( |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
296 new Callable<RadiographyWidget, RadiographyScene::ContentChangedMessage> |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
297 (*this, &RadiographyWidget::OnContentChanged)); |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
298 |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
299 NotifyContentChanged(); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
300 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
301 // force redraw |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
302 FitContent(); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
303 } |
413 | 304 } |