Mercurial > hg > orthanc-stone
annotate Framework/Radiography/RadiographyWidget.cpp @ 448:cc47e6eaefb0 bgo-commands-codegen
Fixed dummy change
author | bgo-osimis |
---|---|
date | Wed, 16 Jan 2019 21:08:38 +0100 |
parents | a750f11892ec |
children | 64d90190a08c aaeec7be8fb7 |
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 |
28 | |
29 namespace OrthancStone | |
30 { | |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
31 |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
32 bool RadiographyWidget::IsInvertedInternal() const |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
33 { |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
34 return (scene_->GetPreferredPhotomotricDisplayMode() == PhotometricDisplayMode_Monochrome1) ^ invert_; // MONOCHROME1 images must be inverted and the user can invert the image too -> XOR the two |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
35 } |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
36 |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
37 void RadiographyWidget::RenderBackground(Orthanc::ImageAccessor& image, float minValue, float maxValue) |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
38 { |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
39 // wipe background before rendering |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
40 float backgroundValue = minValue; |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
41 |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
42 switch (scene_->GetPreferredPhotomotricDisplayMode()) |
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 case PhotometricDisplayMode_Monochrome1: |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
45 case PhotometricDisplayMode_Default: |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
46 if (IsInvertedInternal()) |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
47 backgroundValue = maxValue; |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
48 else |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
49 backgroundValue = minValue; |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
50 break; |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
51 case PhotometricDisplayMode_Monochrome2: |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
52 if (IsInvertedInternal()) |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
53 backgroundValue = minValue; |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
54 else |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
55 backgroundValue = maxValue; |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
56 break; |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
57 default: |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
58 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
59 } |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
60 |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
61 Orthanc::ImageProcessing::Set(image, backgroundValue); |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
62 } |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
63 |
413 | 64 bool RadiographyWidget::RenderInternal(unsigned int width, |
65 unsigned int height, | |
66 ImageInterpolation interpolation) | |
67 { | |
68 float windowCenter, windowWidth; | |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
69 scene_->GetWindowingWithDefault(windowCenter, windowWidth); |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
70 |
413 | 71 float x0 = windowCenter - windowWidth / 2.0f; |
72 float x1 = windowCenter + windowWidth / 2.0f; | |
73 | |
74 if (windowWidth <= 0.001f) // Avoid division by zero at (*) | |
75 { | |
76 return false; | |
77 } | |
78 else | |
79 { | |
80 if (floatBuffer_.get() == NULL || | |
81 floatBuffer_->GetWidth() != width || | |
82 floatBuffer_->GetHeight() != height) | |
83 { | |
84 floatBuffer_.reset(new Orthanc::Image(Orthanc::PixelFormat_Float32, width, height, false)); | |
85 } | |
86 | |
87 if (cairoBuffer_.get() == NULL || | |
88 cairoBuffer_->GetWidth() != width || | |
89 cairoBuffer_->GetHeight() != height) | |
90 { | |
91 cairoBuffer_.reset(new CairoSurface(width, height)); | |
92 } | |
93 | |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
94 RenderBackground(*floatBuffer_, x0, x1); |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
95 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
96 scene_->Render(*floatBuffer_, GetView().GetMatrix(), interpolation); |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
97 |
413 | 98 // Conversion from Float32 to BGRA32 (cairo). Very similar to |
99 // GrayscaleFrameRenderer => TODO MERGE? | |
100 | |
101 Orthanc::ImageAccessor target; | |
102 cairoBuffer_->GetWriteableAccessor(target); | |
103 | |
104 float scaling = 255.0f / (x1 - x0); | |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
105 |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
106 bool invert = IsInvertedInternal(); |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
107 |
413 | 108 for (unsigned int y = 0; y < height; y++) |
109 { | |
110 const float* p = reinterpret_cast<const float*>(floatBuffer_->GetConstRow(y)); | |
111 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | |
112 | |
113 for (unsigned int x = 0; x < width; x++, p++, q += 4) | |
114 { | |
115 uint8_t v = 0; | |
116 if (*p >= x1) | |
117 { | |
118 v = 255; | |
119 } | |
120 else if (*p <= x0) | |
121 { | |
122 v = 0; | |
123 } | |
124 else | |
125 { | |
126 // https://en.wikipedia.org/wiki/Linear_interpolation | |
127 v = static_cast<uint8_t>(scaling * (*p - x0)); // (*) | |
128 } | |
129 | |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
130 if (invert) |
413 | 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 | |
147 bool RadiographyWidget::RenderScene(CairoContext& context, | |
148 const ViewportGeometry& view) | |
149 { | |
150 cairo_t* cr = context.GetObject(); | |
151 | |
152 if (RenderInternal(context.GetWidth(), context.GetHeight(), interpolation_)) | |
153 { | |
154 // https://www.cairographics.org/FAQ/#paint_from_a_surface | |
155 cairo_save(cr); | |
156 cairo_identity_matrix(cr); | |
157 cairo_set_source_surface(cr, cairoBuffer_->GetObject(), 0, 0); | |
158 cairo_paint(cr); | |
159 cairo_restore(cr); | |
160 } | |
161 else | |
162 { | |
163 // https://www.cairographics.org/FAQ/#clear_a_surface | |
164 context.SetSourceColor(0, 0, 0); | |
165 cairo_paint(cr); | |
166 } | |
167 | |
168 if (hasSelection_) | |
169 { | |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
170 scene_->DrawBorder(context, selectedLayer_, view.GetZoom()); |
413 | 171 } |
172 | |
173 return true; | |
174 } | |
175 | |
176 | |
177 RadiographyWidget::RadiographyWidget(MessageBroker& broker, | |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
178 boost::shared_ptr<RadiographyScene> scene, |
413 | 179 const std::string& name) : |
180 WorldSceneWidget(name), | |
181 IObserver(broker), | |
182 invert_(false), | |
183 interpolation_(ImageInterpolation_Nearest), | |
184 hasSelection_(false), | |
185 selectedLayer_(0) // Dummy initialization | |
186 { | |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
187 SetScene(scene); |
413 | 188 } |
189 | |
190 | |
191 void RadiographyWidget::Select(size_t layer) | |
192 { | |
193 hasSelection_ = true; | |
194 selectedLayer_ = layer; | |
195 } | |
196 | |
197 | |
198 bool RadiographyWidget::LookupSelectedLayer(size_t& layer) | |
199 { | |
200 if (hasSelection_) | |
201 { | |
202 layer = selectedLayer_; | |
203 return true; | |
204 } | |
205 else | |
206 { | |
207 return false; | |
208 } | |
209 } | |
210 | |
211 | |
212 void RadiographyWidget::OnGeometryChanged(const RadiographyScene::GeometryChangedMessage& message) | |
213 { | |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
214 LOG(INFO) << "Scene geometry has changed"; |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
215 |
413 | 216 FitContent(); |
217 } | |
218 | |
219 | |
220 void RadiographyWidget::OnContentChanged(const RadiographyScene::ContentChangedMessage& message) | |
221 { | |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
222 LOG(INFO) << "Scene content has changed"; |
413 | 223 NotifyContentChanged(); |
224 } | |
225 | |
226 | |
227 void RadiographyWidget::SetInvert(bool invert) | |
228 { | |
229 if (invert_ != invert) | |
230 { | |
231 invert_ = invert; | |
232 NotifyContentChanged(); | |
233 } | |
234 } | |
235 | |
236 | |
237 void RadiographyWidget::SwitchInvert() | |
238 { | |
239 invert_ = !invert_; | |
240 NotifyContentChanged(); | |
241 } | |
242 | |
243 | |
244 void RadiographyWidget::SetInterpolation(ImageInterpolation interpolation) | |
245 { | |
246 if (interpolation_ != interpolation) | |
247 { | |
248 interpolation_ = interpolation; | |
249 NotifyContentChanged(); | |
250 } | |
251 } | |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
252 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
253 void RadiographyWidget::SetScene(boost::shared_ptr<RadiographyScene> scene) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
254 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
255 if (scene_ != NULL) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
256 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
257 scene_->Unregister(this); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
258 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
259 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
260 scene_ = scene; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
261 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
262 scene_->RegisterObserverCallback( |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
263 new Callable<RadiographyWidget, RadiographyScene::GeometryChangedMessage> |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
264 (*this, &RadiographyWidget::OnGeometryChanged)); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
265 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
266 scene_->RegisterObserverCallback( |
432
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
267 new Callable<RadiographyWidget, RadiographyScene::ContentChangedMessage> |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
268 (*this, &RadiographyWidget::OnContentChanged)); |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
269 |
4eb96c6b4e96
improved handling of MONOCHROME1, background and invertion
am@osimis.io
parents:
428
diff
changeset
|
270 NotifyContentChanged(); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
271 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
272 // force redraw |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
273 FitContent(); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
413
diff
changeset
|
274 } |
413 | 275 } |