comparison OrthancStone/Sources/Scene2D/LookupTableTextureSceneLayer.cpp @ 1512:244ad1e4e76a

reorganization of folders
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 Jul 2020 16:21:02 +0200
parents Framework/Scene2D/LookupTableTextureSceneLayer.cpp@30deba7bc8e2
children 4fb8fdf03314
comparison
equal deleted inserted replaced
1511:9dfeee74c1e6 1512:244ad1e4e76a
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 "LookupTableTextureSceneLayer.h"
23
24 #include <Images/Image.h>
25 #include <Images/ImageProcessing.h>
26 #include <OrthancException.h>
27
28 namespace OrthancStone
29 {
30 LookupTableTextureSceneLayer::LookupTableTextureSceneLayer(const Orthanc::ImageAccessor& texture) :
31 applyLog_(false)
32 {
33 {
34 std::unique_ptr<Orthanc::ImageAccessor> t(
35 new Orthanc::Image(Orthanc::PixelFormat_Float32,
36 texture.GetWidth(),
37 texture.GetHeight(),
38 false));
39
40 Orthanc::ImageProcessing::Convert(*t, texture);
41 SetTexture(t.release());
42 }
43
44 SetLookupTableGrayscale(); // simple ramp between 0 and 255
45 SetRange(0, 1);
46 }
47
48
49 void LookupTableTextureSceneLayer::SetLookupTableGrayscale()
50 {
51 std::vector<uint8_t> rgb(3 * 256);
52
53 for (size_t i = 0; i < 256; i++)
54 {
55 rgb[3 * i] = static_cast<uint8_t>(i);
56 rgb[3 * i + 1] = static_cast<uint8_t>(i);
57 rgb[3 * i + 2] = static_cast<uint8_t>(i);
58 }
59
60 SetLookupTableRgb(rgb);
61 }
62
63
64 void LookupTableTextureSceneLayer::SetLookupTableRgb(const std::vector<uint8_t>& lut)
65 {
66 if (lut.size() != 3 * 256)
67 {
68 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
69 }
70
71 lut_.resize(4 * 256);
72
73 for (size_t i = 0; i < 256; i++)
74 {
75 // Premultiplied alpha
76
77 if (i == 0)
78 {
79 // Make zero transparent
80 lut_[4 * i] = 0; // R
81 lut_[4 * i + 1] = 0; // G
82 lut_[4 * i + 2] = 0; // B
83 lut_[4 * i + 3] = 0; // A
84 }
85 else
86 {
87 float a = static_cast<float>(i) / 255.0f;
88
89 float r = static_cast<float>(lut[3 * i]) * a;
90 float g = static_cast<float>(lut[3 * i + 1]) * a;
91 float b = static_cast<float>(lut[3 * i + 2]) * a;
92
93 lut_[4 * i] = static_cast<uint8_t>(std::floor(r));
94 lut_[4 * i + 1] = static_cast<uint8_t>(std::floor(g));
95 lut_[4 * i + 2] = static_cast<uint8_t>(std::floor(b));
96 lut_[4 * i + 3] = static_cast<uint8_t>(std::floor(a * 255.0f));
97 }
98 }
99
100 IncrementRevision();
101 }
102
103
104 void LookupTableTextureSceneLayer::SetLookupTable(const std::vector<uint8_t>& lut)
105 {
106 if (lut.size() == 4 * 256)
107 {
108 lut_ = lut;
109 IncrementRevision();
110 }
111 else if (lut.size() == 3 * 256)
112 {
113 SetLookupTableRgb(lut);
114 }
115 else
116 {
117 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
118 }
119 }
120
121 void LookupTableTextureSceneLayer::SetRange(float minValue,
122 float maxValue)
123 {
124 if (minValue > maxValue)
125 {
126 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
127 }
128 else
129 {
130 minValue_ = minValue;
131 maxValue_ = maxValue;
132 IncrementRevision();
133 }
134 }
135
136 void LookupTableTextureSceneLayer::SetApplyLog(bool apply)
137 {
138 applyLog_ = apply;
139 IncrementRevision();
140 }
141
142 void LookupTableTextureSceneLayer::FitRange()
143 {
144 Orthanc::ImageProcessing::GetMinMaxFloatValue(minValue_, maxValue_, GetTexture());
145 assert(minValue_ <= maxValue_);
146 // TODO: debug to be removed
147 if (fabs(maxValue_ - minValue_) < 0.0001) {
148 LOG(INFO) << "LookupTableTextureSceneLayer::FitRange(): minValue_ = " << minValue_ << " maxValue_ = " << maxValue_;
149 }
150 IncrementRevision();
151 }
152
153
154 ISceneLayer* LookupTableTextureSceneLayer::Clone() const
155 {
156 std::unique_ptr<LookupTableTextureSceneLayer> cloned
157 (new LookupTableTextureSceneLayer(GetTexture()));
158
159
160 // TODO: why is windowing_ not copied??????
161 cloned->CopyParameters(*this);
162 cloned->minValue_ = minValue_;
163 cloned->maxValue_ = maxValue_;
164 cloned->lut_ = lut_;
165
166 return cloned.release();
167 }
168
169
170 // Templatized function to speed up computations, by avoiding
171 // testing conditions on each pixel
172 template <bool IsApplyLog,
173 Orthanc::PixelFormat TargetFormat>
174 static void RenderInternal(Orthanc::ImageAccessor& target,
175 const Orthanc::ImageAccessor& source,
176 float minValue,
177 float slope,
178 const std::vector<uint8_t>& lut)
179 {
180 static const float LOG_NORMALIZATION = 255.0f / log(1.0f + 255.0f);
181
182 const unsigned int width = source.GetWidth();
183 const unsigned int height = source.GetHeight();
184
185 for (unsigned int y = 0; y < height; y++)
186 {
187 const float* p = reinterpret_cast<const float*>(source.GetConstRow(y));
188 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
189
190 for (unsigned int x = 0; x < width; x++)
191 {
192 float v = (*p - minValue) * slope;
193 if (v <= 0)
194 {
195 v = 0;
196 }
197 else if (v >= 255)
198 {
199 v = 255;
200 }
201
202 if (IsApplyLog)
203 {
204 // https://theailearner.com/2019/01/01/log-transformation/
205 v = LOG_NORMALIZATION * log(1.0f + static_cast<float>(v));
206 }
207
208 assert(v >= 0.0f && v <= 255.0f);
209
210 uint8_t vv = static_cast<uint8_t>(v);
211
212 switch (TargetFormat)
213 {
214 case Orthanc::PixelFormat_BGRA32:
215 // For Cairo surfaces
216 q[0] = lut[4 * vv + 2]; // B
217 q[1] = lut[4 * vv + 1]; // G
218 q[2] = lut[4 * vv + 0]; // R
219 q[3] = lut[4 * vv + 3]; // A
220 break;
221
222 case Orthanc::PixelFormat_RGBA32:
223 // For OpenGL
224 q[0] = lut[4 * vv + 0]; // R
225 q[1] = lut[4 * vv + 1]; // G
226 q[2] = lut[4 * vv + 2]; // B
227 q[3] = lut[4 * vv + 3]; // A
228 break;
229
230 default:
231 assert(0);
232 }
233
234 p++;
235 q += 4;
236 }
237 }
238 }
239
240
241 void LookupTableTextureSceneLayer::Render(Orthanc::ImageAccessor& target) const
242 {
243 assert(sizeof(float) == 4);
244
245 if (!HasTexture())
246 {
247 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
248 }
249
250 const Orthanc::ImageAccessor& source = GetTexture();
251
252 if (source.GetFormat() != Orthanc::PixelFormat_Float32 ||
253 (target.GetFormat() != Orthanc::PixelFormat_RGBA32 &&
254 target.GetFormat() != Orthanc::PixelFormat_BGRA32))
255 {
256 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat);
257 }
258
259 if (source.GetWidth() != target.GetWidth() ||
260 source.GetHeight() != target.GetHeight())
261 {
262 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageSize);
263 }
264
265 const float minValue = GetMinValue();
266 float slope;
267
268 if (GetMinValue() >= GetMaxValue())
269 {
270 slope = 0;
271 }
272 else
273 {
274 slope = 256.0f / (GetMaxValue() - GetMinValue());
275 }
276
277 const std::vector<uint8_t>& lut = GetLookupTable();
278 if (lut.size() != 4 * 256)
279 {
280 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
281 }
282
283 switch (target.GetFormat())
284 {
285 case Orthanc::PixelFormat_RGBA32:
286 if (applyLog_)
287 {
288 RenderInternal<true, Orthanc::PixelFormat_RGBA32>(target, source, minValue, slope, lut);
289 }
290 else
291 {
292 RenderInternal<false, Orthanc::PixelFormat_RGBA32>(target, source, minValue, slope, lut);
293 }
294 break;
295
296 case Orthanc::PixelFormat_BGRA32:
297 if (applyLog_)
298 {
299 RenderInternal<true, Orthanc::PixelFormat_BGRA32>(target, source, minValue, slope, lut);
300 }
301 else
302 {
303 RenderInternal<false, Orthanc::PixelFormat_BGRA32>(target, source, minValue, slope, lut);
304 }
305 break;
306
307 default:
308 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
309 }
310 }
311 }