Mercurial > hg > orthanc-stone
comparison Framework/Radiography/RadiographyWidget.cpp @ 1200:54cbffabdc45 broker
integration mainline->broker
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 29 Nov 2019 11:03:41 +0100 |
parents | 8d2f1b25593c 922d2e61aa5d |
children | 37bc7f115f81 |
comparison
equal
deleted
inserted
replaced
1198:4cc997207d8a | 1200:54cbffabdc45 |
---|---|
68 | 68 |
69 bool RadiographyWidget::RenderInternal(unsigned int width, | 69 bool RadiographyWidget::RenderInternal(unsigned int width, |
70 unsigned int height, | 70 unsigned int height, |
71 ImageInterpolation interpolation) | 71 ImageInterpolation interpolation) |
72 { | 72 { |
73 float windowCenter, windowWidth; | 73 if (floatBuffer_.get() == NULL || |
74 scene_->GetWindowingWithDefault(windowCenter, windowWidth); | 74 floatBuffer_->GetWidth() != width || |
75 | 75 floatBuffer_->GetHeight() != height) |
76 float x0 = windowCenter - windowWidth / 2.0f; | 76 { |
77 float x1 = windowCenter + windowWidth / 2.0f; | 77 floatBuffer_.reset(new Orthanc::Image( |
78 | 78 Orthanc::PixelFormat_Float32, width, height, false)); |
79 if (windowWidth <= 0.001f) // Avoid division by zero at (*) | 79 } |
80 { | 80 |
81 return false; | 81 if (cairoBuffer_.get() == NULL || |
82 } | 82 cairoBuffer_->GetWidth() != width || |
83 else | 83 cairoBuffer_->GetHeight() != height) |
84 { | 84 { |
85 if (floatBuffer_.get() == NULL || | 85 cairoBuffer_.reset(new CairoSurface(width, height, false /* no alpha */)); |
86 floatBuffer_->GetWidth() != width || | 86 } |
87 floatBuffer_->GetHeight() != height) | 87 |
88 RenderBackground(*floatBuffer_, 0.0, 65535.0); | |
89 | |
90 scene_->Render(*floatBuffer_, GetView().GetMatrix(), interpolation, true); | |
91 | |
92 // Conversion from Float32 to BGRA32 (cairo). Very similar to | |
93 // GrayscaleFrameRenderer => TODO MERGE? | |
94 Orthanc::ImageAccessor target; | |
95 cairoBuffer_->GetWriteableAccessor(target); | |
96 | |
97 bool invert = IsInvertedInternal(); | |
98 | |
99 for (unsigned int y = 0; y < height; y++) | |
100 { | |
101 const float* p = reinterpret_cast<const float*>(floatBuffer_->GetConstRow(y)); | |
102 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | |
103 | |
104 for (unsigned int x = 0; x < width; x++, p++, q += 4) | |
88 { | 105 { |
89 floatBuffer_.reset(new Orthanc::Image( | 106 uint8_t v = 0; |
90 Orthanc::PixelFormat_Float32, width, height, false)); | 107 if (*p >= 65535.0) |
108 { | |
109 v = 255; | |
110 } | |
111 else if (*p <= 0.0) | |
112 { | |
113 v = 0; | |
114 } | |
115 else | |
116 { | |
117 v = static_cast<uint8_t>(*p / 256.0); | |
118 } | |
119 | |
120 if (invert) | |
121 { | |
122 v = 255 - v; | |
123 } | |
124 | |
125 q[0] = v; | |
126 q[1] = v; | |
127 q[2] = v; | |
128 q[3] = 255; | |
91 } | 129 } |
92 | 130 } |
93 if (cairoBuffer_.get() == NULL || | 131 |
94 cairoBuffer_->GetWidth() != width || | 132 return true; |
95 cairoBuffer_->GetHeight() != height) | |
96 { | |
97 cairoBuffer_.reset(new CairoSurface(width, height, false /* no alpha */)); | |
98 } | |
99 | |
100 RenderBackground(*floatBuffer_, x0, x1); | |
101 | |
102 scene_->Render(*floatBuffer_, GetView().GetMatrix(), interpolation); | |
103 | |
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); | |
111 | |
112 bool invert = IsInvertedInternal(); | |
113 | |
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 | |
136 if (invert) | |
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 } | 133 } |
151 | 134 |
152 | 135 |
153 bool RadiographyWidget::RenderScene(CairoContext& context, | 136 bool RadiographyWidget::RenderScene(CairoContext& context, |
154 const Deprecated::ViewportGeometry& view) | 137 const Deprecated::ViewportGeometry& view) |
197 { | 180 { |
198 hasSelection_ = true; | 181 hasSelection_ = true; |
199 selectedLayer_ = layer; | 182 selectedLayer_ = layer; |
200 } | 183 } |
201 | 184 |
202 void RadiographyWidget::ClearSelectedLayer() | |
203 { | |
204 hasSelection_ = false; | |
205 } | |
206 | |
207 bool RadiographyWidget::SelectMaskLayer(size_t index) | |
208 { | |
209 std::vector<size_t> layerIndexes; | |
210 size_t count = 0; | |
211 scene_->GetLayersIndexes(layerIndexes); | |
212 | |
213 for (size_t i = 0; i < layerIndexes.size(); ++i) | |
214 { | |
215 const RadiographyMaskLayer* maskLayer = dynamic_cast<const RadiographyMaskLayer*>(&(scene_->GetLayer(layerIndexes[i]))); | |
216 if (maskLayer != NULL) | |
217 { | |
218 if (count == index) | |
219 { | |
220 Select(layerIndexes[i]); | |
221 return true; | |
222 } | |
223 count++; | |
224 } | |
225 } | |
226 | |
227 return false; | |
228 } | |
229 | |
230 bool RadiographyWidget::LookupSelectedLayer(size_t& layer) | 185 bool RadiographyWidget::LookupSelectedLayer(size_t& layer) |
231 { | 186 { |
232 if (hasSelection_) | 187 if (hasSelection_) |
233 { | 188 { |
234 layer = selectedLayer_; | 189 layer = selectedLayer_; |
257 void RadiographyWidget::OnLayerRemoved(const RadiographyScene::LayerRemovedMessage& message) | 212 void RadiographyWidget::OnLayerRemoved(const RadiographyScene::LayerRemovedMessage& message) |
258 { | 213 { |
259 size_t removedLayerIndex = message.GetLayerIndex(); | 214 size_t removedLayerIndex = message.GetLayerIndex(); |
260 if (hasSelection_ && selectedLayer_ == removedLayerIndex) | 215 if (hasSelection_ && selectedLayer_ == removedLayerIndex) |
261 { | 216 { |
262 ClearSelectedLayer(); | 217 Unselect(); |
263 } | 218 } |
219 NotifyContentChanged(); | |
264 } | 220 } |
265 | 221 |
266 void RadiographyWidget::SetInvert(bool invert) | 222 void RadiographyWidget::SetInvert(bool invert) |
267 { | 223 { |
268 if (invert_ != invert) | 224 if (invert_ != invert) |