comparison OrthancFramework/Sources/Images/PixelTraits.h @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents Core/Images/PixelTraits.h@3c4269229566
children bf7b9edf6b81
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
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 General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #pragma once
35
36 #include "../Enumerations.h"
37
38 #include <limits>
39
40 namespace Orthanc
41 {
42 template <PixelFormat format,
43 typename _PixelType>
44 struct IntegerPixelTraits
45 {
46 typedef _PixelType PixelType;
47
48 ORTHANC_FORCE_INLINE
49 static PixelFormat GetPixelFormat()
50 {
51 return format;
52 }
53
54 ORTHANC_FORCE_INLINE
55 static PixelType IntegerToPixel(int64_t value)
56 {
57 if (value < static_cast<int64_t>(std::numeric_limits<PixelType>::min()))
58 {
59 return std::numeric_limits<PixelType>::min();
60 }
61 else if (value > static_cast<int64_t>(std::numeric_limits<PixelType>::max()))
62 {
63 return std::numeric_limits<PixelType>::max();
64 }
65 else
66 {
67 return static_cast<PixelType>(value);
68 }
69 }
70
71 ORTHANC_FORCE_INLINE
72 static void SetZero(PixelType& target)
73 {
74 target = 0;
75 }
76
77 ORTHANC_FORCE_INLINE
78 static void SetMinValue(PixelType& target)
79 {
80 target = std::numeric_limits<PixelType>::min();
81 }
82
83 ORTHANC_FORCE_INLINE
84 static void SetMaxValue(PixelType& target)
85 {
86 target = std::numeric_limits<PixelType>::max();
87 }
88
89 ORTHANC_FORCE_INLINE
90 static void Copy(PixelType& target,
91 const PixelType& source)
92 {
93 target = source;
94 }
95
96 ORTHANC_FORCE_INLINE
97 static float PixelToFloat(const PixelType& source)
98 {
99 return static_cast<float>(source);
100 }
101
102 ORTHANC_FORCE_INLINE
103 static void FloatToPixel(PixelType& target,
104 float value)
105 {
106 value += 0.5f;
107 if (value < static_cast<float>(std::numeric_limits<PixelType>::min()))
108 {
109 target = std::numeric_limits<PixelType>::min();
110 }
111 else if (value > static_cast<float>(std::numeric_limits<PixelType>::max()))
112 {
113 target = std::numeric_limits<PixelType>::max();
114 }
115 else
116 {
117 target = static_cast<PixelType>(value);
118 }
119 }
120
121 ORTHANC_FORCE_INLINE
122 static bool IsEqual(const PixelType& a,
123 const PixelType& b)
124 {
125 return a == b;
126 }
127 };
128
129
130 template <PixelFormat Format>
131 struct PixelTraits;
132
133
134 template <>
135 struct PixelTraits<PixelFormat_Grayscale8> :
136 public IntegerPixelTraits<PixelFormat_Grayscale8, uint8_t>
137 {
138 };
139
140
141 template <>
142 struct PixelTraits<PixelFormat_Grayscale16> :
143 public IntegerPixelTraits<PixelFormat_Grayscale16, uint16_t>
144 {
145 };
146
147
148 template <>
149 struct PixelTraits<PixelFormat_SignedGrayscale16> :
150 public IntegerPixelTraits<PixelFormat_SignedGrayscale16, int16_t>
151 {
152 };
153
154
155 template <>
156 struct PixelTraits<PixelFormat_Grayscale32> :
157 public IntegerPixelTraits<PixelFormat_Grayscale32, uint32_t>
158 {
159 };
160
161
162 template <>
163 struct PixelTraits<PixelFormat_Grayscale64> :
164 public IntegerPixelTraits<PixelFormat_Grayscale64, uint64_t>
165 {
166 };
167
168
169 template <>
170 struct PixelTraits<PixelFormat_RGB24>
171 {
172 struct PixelType
173 {
174 uint8_t red_;
175 uint8_t green_;
176 uint8_t blue_;
177 };
178
179 ORTHANC_FORCE_INLINE
180 static PixelFormat GetPixelFormat()
181 {
182 return PixelFormat_RGB24;
183 }
184
185 ORTHANC_FORCE_INLINE
186 static void SetZero(PixelType& target)
187 {
188 target.red_ = 0;
189 target.green_ = 0;
190 target.blue_ = 0;
191 }
192
193 ORTHANC_FORCE_INLINE
194 static void Copy(PixelType& target,
195 const PixelType& source)
196 {
197 target.red_ = source.red_;
198 target.green_ = source.green_;
199 target.blue_ = source.blue_;
200 }
201
202 ORTHANC_FORCE_INLINE
203 static bool IsEqual(const PixelType& a,
204 const PixelType& b)
205 {
206 return (a.red_ == b.red_ &&
207 a.green_ == b.green_ &&
208 a.blue_ == b.blue_);
209 }
210
211 ORTHANC_FORCE_INLINE
212 static void FloatToPixel(PixelType& target,
213 float value)
214 {
215 uint8_t v;
216 PixelTraits<PixelFormat_Grayscale8>::FloatToPixel(v, value);
217
218 target.red_ = v;
219 target.green_ = v;
220 target.blue_ = v;
221 }
222 };
223
224
225 template <>
226 struct PixelTraits<PixelFormat_BGRA32>
227 {
228 struct PixelType
229 {
230 uint8_t blue_;
231 uint8_t green_;
232 uint8_t red_;
233 uint8_t alpha_;
234 };
235
236 ORTHANC_FORCE_INLINE
237 static PixelFormat GetPixelFormat()
238 {
239 return PixelFormat_BGRA32;
240 }
241
242 ORTHANC_FORCE_INLINE
243 static void SetZero(PixelType& target)
244 {
245 target.blue_ = 0;
246 target.green_ = 0;
247 target.red_ = 0;
248 target.alpha_ = 0;
249 }
250
251 ORTHANC_FORCE_INLINE
252 static void Copy(PixelType& target,
253 const PixelType& source)
254 {
255 target.blue_ = source.blue_;
256 target.green_ = source.green_;
257 target.red_ = source.red_;
258 target.alpha_ = source.alpha_;
259 }
260
261 ORTHANC_FORCE_INLINE
262 static bool IsEqual(const PixelType& a,
263 const PixelType& b)
264 {
265 return (a.blue_ == b.blue_ &&
266 a.green_ == b.green_ &&
267 a.red_ == b.red_ &&
268 a.alpha_ == b.alpha_);
269 }
270
271 ORTHANC_FORCE_INLINE
272 static void FloatToPixel(PixelType& target,
273 float value)
274 {
275 uint8_t v;
276 PixelTraits<PixelFormat_Grayscale8>::FloatToPixel(v, value);
277
278 target.blue_ = v;
279 target.green_ = v;
280 target.red_ = v;
281 target.alpha_ = 255;
282 }
283 };
284
285
286 template <>
287 struct PixelTraits<PixelFormat_RGBA32>
288 {
289 struct PixelType
290 {
291 uint8_t red_;
292 uint8_t green_;
293 uint8_t blue_;
294 uint8_t alpha_;
295 };
296
297 ORTHANC_FORCE_INLINE
298 static PixelFormat GetPixelFormat()
299 {
300 return PixelFormat_RGBA32;
301 }
302
303 ORTHANC_FORCE_INLINE
304 static void SetZero(PixelType& target)
305 {
306 target.red_ = 0;
307 target.green_ = 0;
308 target.blue_ = 0;
309 target.alpha_ = 0;
310 }
311
312 ORTHANC_FORCE_INLINE
313 static void Copy(PixelType& target,
314 const PixelType& source)
315 {
316 target.red_ = source.red_;
317 target.green_ = source.green_;
318 target.blue_ = source.blue_;
319 target.alpha_ = source.alpha_;
320 }
321
322 ORTHANC_FORCE_INLINE
323 static bool IsEqual(const PixelType& a,
324 const PixelType& b)
325 {
326 return (a.red_ == b.red_ &&
327 a.green_ == b.green_ &&
328 a.blue_ == b.blue_ &&
329 a.alpha_ == b.alpha_);
330 }
331
332 ORTHANC_FORCE_INLINE
333 static void FloatToPixel(PixelType& target,
334 float value)
335 {
336 uint8_t v;
337 PixelTraits<PixelFormat_Grayscale8>::FloatToPixel(v, value);
338
339 target.red_ = v;
340 target.green_ = v;
341 target.blue_ = v;
342 target.alpha_ = 255;
343 }
344 };
345
346
347 template <>
348 struct PixelTraits<PixelFormat_Float32>
349 {
350 typedef float PixelType;
351
352 ORTHANC_FORCE_INLINE
353 static PixelFormat GetPixelFormat()
354 {
355 return PixelFormat_Float32;
356 }
357
358 ORTHANC_FORCE_INLINE
359 static void SetZero(PixelType& target)
360 {
361 target = 0.0f;
362 }
363
364 ORTHANC_FORCE_INLINE
365 static void Copy(PixelType& target,
366 const PixelType& source)
367 {
368 target = source;
369 }
370
371 ORTHANC_FORCE_INLINE
372 static bool IsEqual(const PixelType& a,
373 const PixelType& b)
374 {
375 float tmp = (a - b);
376
377 if (tmp < 0)
378 {
379 tmp = -tmp;
380 }
381
382 return tmp <= std::numeric_limits<float>::epsilon();
383 }
384
385 ORTHANC_FORCE_INLINE
386 static void SetMinValue(PixelType& target)
387 {
388 // std::numeric_limits<float>::lowest is not supported on
389 // all compilers (for instance, Visual Studio 9.0 2008)
390 target = -std::numeric_limits<float>::max();
391 }
392
393 ORTHANC_FORCE_INLINE
394 static void SetMaxValue(PixelType& target)
395 {
396 target = std::numeric_limits<float>::max();
397 }
398
399 ORTHANC_FORCE_INLINE
400 static void FloatToPixel(PixelType& target,
401 float value)
402 {
403 target = value;
404 }
405
406 ORTHANC_FORCE_INLINE
407 static float PixelToFloat(const PixelType& source)
408 {
409 return source;
410 }
411 };
412 }