Mercurial > hg > orthanc-stone
annotate Resources/Orthanc/Core/Images/ImageProcessing.cpp @ 54:01aa453d4d5b wasm
IWidget::HasRenderMouseOver
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 27 Apr 2017 17:49:29 +0200 |
parents | 81e2651dca17 |
children |
rev | line source |
---|---|
1 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
40
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
5 * Copyright (C) 2017 Osimis, Belgium |
1 | 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 #include "../PrecompiledHeaders.h" | |
35 #include "ImageProcessing.h" | |
36 | |
37 #include "../OrthancException.h" | |
38 | |
39 #include <boost/math/special_functions/round.hpp> | |
40 | |
41 #include <cassert> | |
42 #include <string.h> | |
43 #include <limits> | |
44 #include <stdint.h> | |
45 | |
46 namespace Orthanc | |
47 { | |
48 template <typename TargetType, typename SourceType> | |
49 static void ConvertInternal(ImageAccessor& target, | |
50 const ImageAccessor& source) | |
51 { | |
52 const TargetType minValue = std::numeric_limits<TargetType>::min(); | |
53 const TargetType maxValue = std::numeric_limits<TargetType>::max(); | |
54 | |
55 for (unsigned int y = 0; y < source.GetHeight(); y++) | |
56 { | |
57 TargetType* t = reinterpret_cast<TargetType*>(target.GetRow(y)); | |
58 const SourceType* s = reinterpret_cast<const SourceType*>(source.GetConstRow(y)); | |
59 | |
60 for (unsigned int x = 0; x < source.GetWidth(); x++, t++, s++) | |
61 { | |
62 if (static_cast<int32_t>(*s) < static_cast<int32_t>(minValue)) | |
63 { | |
64 *t = minValue; | |
65 } | |
66 else if (static_cast<int32_t>(*s) > static_cast<int32_t>(maxValue)) | |
67 { | |
68 *t = maxValue; | |
69 } | |
70 else | |
71 { | |
72 *t = static_cast<TargetType>(*s); | |
73 } | |
74 } | |
75 } | |
76 } | |
77 | |
78 | |
79 template <typename SourceType> | |
80 static void ConvertGrayscaleToFloat(ImageAccessor& target, | |
81 const ImageAccessor& source) | |
82 { | |
83 assert(sizeof(float) == 4); | |
84 | |
85 for (unsigned int y = 0; y < source.GetHeight(); y++) | |
86 { | |
87 float* t = reinterpret_cast<float*>(target.GetRow(y)); | |
88 const SourceType* s = reinterpret_cast<const SourceType*>(source.GetConstRow(y)); | |
89 | |
90 for (unsigned int x = 0; x < source.GetWidth(); x++, t++, s++) | |
91 { | |
92 *t = static_cast<float>(*s); | |
93 } | |
94 } | |
95 } | |
96 | |
97 | |
98 template <typename TargetType> | |
99 static void ConvertColorToGrayscale(ImageAccessor& target, | |
100 const ImageAccessor& source) | |
101 { | |
102 assert(source.GetFormat() == PixelFormat_RGB24); | |
103 | |
104 const TargetType minValue = std::numeric_limits<TargetType>::min(); | |
105 const TargetType maxValue = std::numeric_limits<TargetType>::max(); | |
106 | |
107 for (unsigned int y = 0; y < source.GetHeight(); y++) | |
108 { | |
109 TargetType* t = reinterpret_cast<TargetType*>(target.GetRow(y)); | |
110 const uint8_t* s = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); | |
111 | |
112 for (unsigned int x = 0; x < source.GetWidth(); x++, t++, s += 3) | |
113 { | |
114 // Y = 0.2126 R + 0.7152 G + 0.0722 B | |
115 int32_t v = (2126 * static_cast<int32_t>(s[0]) + | |
116 7152 * static_cast<int32_t>(s[1]) + | |
117 0722 * static_cast<int32_t>(s[2])) / 1000; | |
118 | |
119 if (static_cast<int32_t>(v) < static_cast<int32_t>(minValue)) | |
120 { | |
121 *t = minValue; | |
122 } | |
123 else if (static_cast<int32_t>(v) > static_cast<int32_t>(maxValue)) | |
124 { | |
125 *t = maxValue; | |
126 } | |
127 else | |
128 { | |
129 *t = static_cast<TargetType>(v); | |
130 } | |
131 } | |
132 } | |
133 } | |
134 | |
135 | |
136 template <typename PixelType> | |
137 static void SetInternal(ImageAccessor& image, | |
138 int64_t constant) | |
139 { | |
140 for (unsigned int y = 0; y < image.GetHeight(); y++) | |
141 { | |
142 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y)); | |
143 | |
144 for (unsigned int x = 0; x < image.GetWidth(); x++, p++) | |
145 { | |
146 *p = static_cast<PixelType>(constant); | |
147 } | |
148 } | |
149 } | |
150 | |
151 | |
152 template <typename PixelType> | |
153 static void GetMinMaxValueInternal(PixelType& minValue, | |
154 PixelType& maxValue, | |
155 const ImageAccessor& source) | |
156 { | |
157 // Deal with the special case of empty image | |
158 if (source.GetWidth() == 0 || | |
159 source.GetHeight() == 0) | |
160 { | |
161 minValue = 0; | |
162 maxValue = 0; | |
163 return; | |
164 } | |
165 | |
166 minValue = std::numeric_limits<PixelType>::max(); | |
167 maxValue = std::numeric_limits<PixelType>::min(); | |
168 | |
169 for (unsigned int y = 0; y < source.GetHeight(); y++) | |
170 { | |
171 const PixelType* p = reinterpret_cast<const PixelType*>(source.GetConstRow(y)); | |
172 | |
173 for (unsigned int x = 0; x < source.GetWidth(); x++, p++) | |
174 { | |
175 if (*p < minValue) | |
176 { | |
177 minValue = *p; | |
178 } | |
179 | |
180 if (*p > maxValue) | |
181 { | |
182 maxValue = *p; | |
183 } | |
184 } | |
185 } | |
186 } | |
187 | |
188 | |
189 | |
190 template <typename PixelType> | |
191 static void AddConstantInternal(ImageAccessor& image, | |
192 int64_t constant) | |
193 { | |
194 if (constant == 0) | |
195 { | |
196 return; | |
197 } | |
198 | |
199 const int64_t minValue = std::numeric_limits<PixelType>::min(); | |
200 const int64_t maxValue = std::numeric_limits<PixelType>::max(); | |
201 | |
202 for (unsigned int y = 0; y < image.GetHeight(); y++) | |
203 { | |
204 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y)); | |
205 | |
206 for (unsigned int x = 0; x < image.GetWidth(); x++, p++) | |
207 { | |
208 int64_t v = static_cast<int64_t>(*p) + constant; | |
209 | |
210 if (v > maxValue) | |
211 { | |
212 *p = std::numeric_limits<PixelType>::max(); | |
213 } | |
214 else if (v < minValue) | |
215 { | |
216 *p = std::numeric_limits<PixelType>::min(); | |
217 } | |
218 else | |
219 { | |
220 *p = static_cast<PixelType>(v); | |
221 } | |
222 } | |
223 } | |
224 } | |
225 | |
226 | |
227 | |
228 template <typename PixelType> | |
229 void MultiplyConstantInternal(ImageAccessor& image, | |
230 float factor) | |
231 { | |
232 if (std::abs(factor - 1.0f) <= std::numeric_limits<float>::epsilon()) | |
233 { | |
234 return; | |
235 } | |
236 | |
237 const int64_t minValue = std::numeric_limits<PixelType>::min(); | |
238 const int64_t maxValue = std::numeric_limits<PixelType>::max(); | |
239 | |
240 for (unsigned int y = 0; y < image.GetHeight(); y++) | |
241 { | |
242 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y)); | |
243 | |
244 for (unsigned int x = 0; x < image.GetWidth(); x++, p++) | |
245 { | |
246 int64_t v = boost::math::llround(static_cast<float>(*p) * factor); | |
247 | |
248 if (v > maxValue) | |
249 { | |
250 *p = std::numeric_limits<PixelType>::max(); | |
251 } | |
252 else if (v < minValue) | |
253 { | |
254 *p = std::numeric_limits<PixelType>::min(); | |
255 } | |
256 else | |
257 { | |
258 *p = static_cast<PixelType>(v); | |
259 } | |
260 } | |
261 } | |
262 } | |
263 | |
264 | |
265 template <typename PixelType> | |
266 void ShiftScaleInternal(ImageAccessor& image, | |
267 float offset, | |
268 float scaling) | |
269 { | |
270 const float minValue = static_cast<float>(std::numeric_limits<PixelType>::min()); | |
271 const float maxValue = static_cast<float>(std::numeric_limits<PixelType>::max()); | |
272 | |
273 for (unsigned int y = 0; y < image.GetHeight(); y++) | |
274 { | |
275 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y)); | |
276 | |
277 for (unsigned int x = 0; x < image.GetWidth(); x++, p++) | |
278 { | |
279 float v = (static_cast<float>(*p) + offset) * scaling; | |
280 | |
281 if (v > maxValue) | |
282 { | |
283 *p = std::numeric_limits<PixelType>::max(); | |
284 } | |
285 else if (v < minValue) | |
286 { | |
287 *p = std::numeric_limits<PixelType>::min(); | |
288 } | |
289 else | |
290 { | |
291 *p = static_cast<PixelType>(boost::math::iround(v)); | |
292 } | |
293 } | |
294 } | |
295 } | |
296 | |
297 | |
298 void ImageProcessing::Copy(ImageAccessor& target, | |
299 const ImageAccessor& source) | |
300 { | |
301 if (target.GetWidth() != source.GetWidth() || | |
302 target.GetHeight() != source.GetHeight()) | |
303 { | |
304 throw OrthancException(ErrorCode_IncompatibleImageSize); | |
305 } | |
306 | |
307 if (target.GetFormat() != source.GetFormat()) | |
308 { | |
309 throw OrthancException(ErrorCode_IncompatibleImageFormat); | |
310 } | |
311 | |
312 unsigned int lineSize = GetBytesPerPixel(source.GetFormat()) * source.GetWidth(); | |
313 | |
314 assert(source.GetPitch() >= lineSize && target.GetPitch() >= lineSize); | |
315 | |
316 for (unsigned int y = 0; y < source.GetHeight(); y++) | |
317 { | |
318 memcpy(target.GetRow(y), source.GetConstRow(y), lineSize); | |
319 } | |
320 } | |
321 | |
322 | |
323 void ImageProcessing::Convert(ImageAccessor& target, | |
324 const ImageAccessor& source) | |
325 { | |
326 if (target.GetWidth() != source.GetWidth() || | |
327 target.GetHeight() != source.GetHeight()) | |
328 { | |
329 throw OrthancException(ErrorCode_IncompatibleImageSize); | |
330 } | |
331 | |
332 if (source.GetFormat() == target.GetFormat()) | |
333 { | |
334 Copy(target, source); | |
335 return; | |
336 } | |
337 | |
338 if (target.GetFormat() == PixelFormat_Grayscale16 && | |
339 source.GetFormat() == PixelFormat_Grayscale8) | |
340 { | |
341 ConvertInternal<uint16_t, uint8_t>(target, source); | |
342 return; | |
343 } | |
344 | |
345 if (target.GetFormat() == PixelFormat_SignedGrayscale16 && | |
346 source.GetFormat() == PixelFormat_Grayscale8) | |
347 { | |
348 ConvertInternal<int16_t, uint8_t>(target, source); | |
349 return; | |
350 } | |
351 | |
352 if (target.GetFormat() == PixelFormat_Grayscale8 && | |
353 source.GetFormat() == PixelFormat_Grayscale16) | |
354 { | |
355 ConvertInternal<uint8_t, uint16_t>(target, source); | |
356 return; | |
357 } | |
358 | |
359 if (target.GetFormat() == PixelFormat_SignedGrayscale16 && | |
360 source.GetFormat() == PixelFormat_Grayscale16) | |
361 { | |
362 ConvertInternal<int16_t, uint16_t>(target, source); | |
363 return; | |
364 } | |
365 | |
366 if (target.GetFormat() == PixelFormat_Grayscale8 && | |
367 source.GetFormat() == PixelFormat_SignedGrayscale16) | |
368 { | |
369 ConvertInternal<uint8_t, int16_t>(target, source); | |
370 return; | |
371 } | |
372 | |
373 if (target.GetFormat() == PixelFormat_Grayscale16 && | |
374 source.GetFormat() == PixelFormat_SignedGrayscale16) | |
375 { | |
376 ConvertInternal<uint16_t, int16_t>(target, source); | |
377 return; | |
378 } | |
379 | |
380 if (target.GetFormat() == PixelFormat_Grayscale8 && | |
381 source.GetFormat() == PixelFormat_RGB24) | |
382 { | |
383 ConvertColorToGrayscale<uint8_t>(target, source); | |
384 return; | |
385 } | |
386 | |
387 if (target.GetFormat() == PixelFormat_Grayscale16 && | |
388 source.GetFormat() == PixelFormat_RGB24) | |
389 { | |
390 ConvertColorToGrayscale<uint16_t>(target, source); | |
391 return; | |
392 } | |
393 | |
394 if (target.GetFormat() == PixelFormat_SignedGrayscale16 && | |
395 source.GetFormat() == PixelFormat_RGB24) | |
396 { | |
397 ConvertColorToGrayscale<int16_t>(target, source); | |
398 return; | |
399 } | |
400 | |
401 if (target.GetFormat() == PixelFormat_Float32 && | |
402 source.GetFormat() == PixelFormat_Grayscale8) | |
403 { | |
404 ConvertGrayscaleToFloat<uint8_t>(target, source); | |
405 return; | |
406 } | |
407 | |
408 if (target.GetFormat() == PixelFormat_Float32 && | |
409 source.GetFormat() == PixelFormat_Grayscale16) | |
410 { | |
411 ConvertGrayscaleToFloat<uint16_t>(target, source); | |
412 return; | |
413 } | |
414 | |
415 if (target.GetFormat() == PixelFormat_Float32 && | |
416 source.GetFormat() == PixelFormat_SignedGrayscale16) | |
417 { | |
418 ConvertGrayscaleToFloat<int16_t>(target, source); | |
419 return; | |
420 } | |
421 | |
422 if (target.GetFormat() == PixelFormat_Grayscale8 && | |
423 source.GetFormat() == PixelFormat_RGBA32) | |
424 { | |
425 for (unsigned int y = 0; y < source.GetHeight(); y++) | |
426 { | |
427 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); | |
428 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | |
429 for (unsigned int x = 0; x < source.GetWidth(); x++, q++) | |
430 { | |
431 *q = static_cast<uint8_t>((2126 * static_cast<uint32_t>(p[0]) + | |
432 7152 * static_cast<uint32_t>(p[1]) + | |
433 0722 * static_cast<uint32_t>(p[2])) / 10000); | |
434 p += 4; | |
435 } | |
436 } | |
437 | |
438 return; | |
439 } | |
440 | |
441 if (target.GetFormat() == PixelFormat_RGB24 && | |
442 source.GetFormat() == PixelFormat_RGBA32) | |
443 { | |
444 for (unsigned int y = 0; y < source.GetHeight(); y++) | |
445 { | |
446 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); | |
447 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | |
448 for (unsigned int x = 0; x < source.GetWidth(); x++) | |
449 { | |
450 q[0] = p[0]; | |
451 q[1] = p[1]; | |
452 q[2] = p[2]; | |
453 p += 4; | |
454 q += 3; | |
455 } | |
456 } | |
457 | |
458 return; | |
459 } | |
460 | |
42 | 461 if (target.GetFormat() == PixelFormat_RGB24 && |
462 source.GetFormat() == PixelFormat_BGRA32) | |
463 { | |
464 for (unsigned int y = 0; y < source.GetHeight(); y++) | |
465 { | |
466 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); | |
467 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | |
468 for (unsigned int x = 0; x < source.GetWidth(); x++) | |
469 { | |
470 q[0] = p[2]; | |
471 q[1] = p[1]; | |
472 q[2] = p[0]; | |
473 p += 4; | |
474 q += 3; | |
475 } | |
476 } | |
477 | |
478 return; | |
479 } | |
480 | |
1 | 481 if (target.GetFormat() == PixelFormat_RGBA32 && |
482 source.GetFormat() == PixelFormat_RGB24) | |
483 { | |
484 for (unsigned int y = 0; y < source.GetHeight(); y++) | |
485 { | |
486 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); | |
487 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | |
488 for (unsigned int x = 0; x < source.GetWidth(); x++) | |
489 { | |
490 q[0] = p[0]; | |
491 q[1] = p[1]; | |
492 q[2] = p[2]; | |
493 q[3] = 255; // Set the alpha channel to full opacity | |
494 p += 3; | |
495 q += 4; | |
496 } | |
497 } | |
498 | |
499 return; | |
500 } | |
501 | |
502 if (target.GetFormat() == PixelFormat_RGB24 && | |
503 source.GetFormat() == PixelFormat_Grayscale8) | |
504 { | |
505 for (unsigned int y = 0; y < source.GetHeight(); y++) | |
506 { | |
507 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); | |
508 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | |
509 for (unsigned int x = 0; x < source.GetWidth(); x++) | |
510 { | |
511 q[0] = *p; | |
512 q[1] = *p; | |
513 q[2] = *p; | |
514 p += 1; | |
515 q += 3; | |
516 } | |
517 } | |
518 | |
519 return; | |
520 } | |
521 | |
522 if (target.GetFormat() == PixelFormat_RGBA32 && | |
523 source.GetFormat() == PixelFormat_Grayscale8) | |
524 { | |
525 for (unsigned int y = 0; y < source.GetHeight(); y++) | |
526 { | |
527 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); | |
528 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | |
529 for (unsigned int x = 0; x < source.GetWidth(); x++) | |
530 { | |
531 q[0] = *p; | |
532 q[1] = *p; | |
533 q[2] = *p; | |
534 q[3] = 255; | |
535 p += 1; | |
536 q += 4; | |
537 } | |
538 } | |
539 | |
540 return; | |
541 } | |
542 | |
543 if (target.GetFormat() == PixelFormat_BGRA32 && | |
544 source.GetFormat() == PixelFormat_RGB24) | |
545 { | |
546 for (unsigned int y = 0; y < source.GetHeight(); y++) | |
547 { | |
548 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); | |
549 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | |
550 for (unsigned int x = 0; x < source.GetWidth(); x++) | |
551 { | |
552 q[0] = p[2]; | |
553 q[1] = p[1]; | |
554 q[2] = p[0]; | |
555 q[3] = 255; | |
556 p += 3; | |
557 q += 4; | |
558 } | |
559 } | |
560 | |
561 return; | |
562 } | |
563 | |
564 throw OrthancException(ErrorCode_NotImplemented); | |
565 } | |
566 | |
567 | |
568 | |
569 void ImageProcessing::Set(ImageAccessor& image, | |
570 int64_t value) | |
571 { | |
572 switch (image.GetFormat()) | |
573 { | |
574 case PixelFormat_Grayscale8: | |
575 SetInternal<uint8_t>(image, value); | |
576 return; | |
577 | |
578 case PixelFormat_Grayscale16: | |
579 SetInternal<uint16_t>(image, value); | |
580 return; | |
581 | |
582 case PixelFormat_SignedGrayscale16: | |
583 SetInternal<int16_t>(image, value); | |
584 return; | |
585 | |
586 case PixelFormat_Float32: | |
587 assert(sizeof(float) == 4); | |
588 SetInternal<float>(image, value); | |
589 return; | |
590 | |
591 default: | |
592 throw OrthancException(ErrorCode_NotImplemented); | |
593 } | |
594 } | |
595 | |
596 | |
597 void ImageProcessing::Set(ImageAccessor& image, | |
598 uint8_t red, | |
599 uint8_t green, | |
600 uint8_t blue, | |
601 uint8_t alpha) | |
602 { | |
603 uint8_t p[4]; | |
604 unsigned int size; | |
605 | |
606 switch (image.GetFormat()) | |
607 { | |
608 case PixelFormat_RGBA32: | |
609 p[0] = red; | |
610 p[1] = green; | |
611 p[2] = blue; | |
612 p[3] = alpha; | |
613 size = 4; | |
614 break; | |
615 | |
616 case PixelFormat_BGRA32: | |
617 p[0] = blue; | |
618 p[1] = green; | |
619 p[2] = red; | |
620 p[3] = alpha; | |
621 size = 4; | |
622 break; | |
623 | |
624 case PixelFormat_RGB24: | |
625 p[0] = red; | |
626 p[1] = green; | |
627 p[2] = blue; | |
628 size = 3; | |
629 break; | |
630 | |
631 default: | |
632 throw OrthancException(ErrorCode_NotImplemented); | |
633 } | |
634 | |
635 for (unsigned int y = 0; y < image.GetHeight(); y++) | |
636 { | |
637 uint8_t* q = reinterpret_cast<uint8_t*>(image.GetRow(y)); | |
638 | |
639 for (unsigned int x = 0; x < image.GetWidth(); x++) | |
640 { | |
641 for (unsigned int i = 0; i < size; i++) | |
642 { | |
643 q[i] = p[i]; | |
644 } | |
645 | |
646 q += size; | |
647 } | |
648 } | |
649 } | |
650 | |
651 | |
652 void ImageProcessing::ShiftRight(ImageAccessor& image, | |
653 unsigned int shift) | |
654 { | |
655 if (image.GetWidth() == 0 || | |
656 image.GetHeight() == 0 || | |
657 shift == 0) | |
658 { | |
659 // Nothing to do | |
660 return; | |
661 } | |
662 | |
663 throw OrthancException(ErrorCode_NotImplemented); | |
664 } | |
665 | |
666 | |
667 void ImageProcessing::GetMinMaxValue(int64_t& minValue, | |
668 int64_t& maxValue, | |
669 const ImageAccessor& image) | |
670 { | |
671 switch (image.GetFormat()) | |
672 { | |
673 case PixelFormat_Grayscale8: | |
674 { | |
675 uint8_t a, b; | |
676 GetMinMaxValueInternal<uint8_t>(a, b, image); | |
677 minValue = a; | |
678 maxValue = b; | |
679 break; | |
680 } | |
681 | |
682 case PixelFormat_Grayscale16: | |
683 { | |
684 uint16_t a, b; | |
685 GetMinMaxValueInternal<uint16_t>(a, b, image); | |
686 minValue = a; | |
687 maxValue = b; | |
688 break; | |
689 } | |
690 | |
691 case PixelFormat_SignedGrayscale16: | |
692 { | |
693 int16_t a, b; | |
694 GetMinMaxValueInternal<int16_t>(a, b, image); | |
695 minValue = a; | |
696 maxValue = b; | |
697 break; | |
698 } | |
699 | |
700 default: | |
701 throw OrthancException(ErrorCode_NotImplemented); | |
702 } | |
703 } | |
704 | |
705 | |
706 | |
707 void ImageProcessing::AddConstant(ImageAccessor& image, | |
708 int64_t value) | |
709 { | |
710 switch (image.GetFormat()) | |
711 { | |
712 case PixelFormat_Grayscale8: | |
713 AddConstantInternal<uint8_t>(image, value); | |
714 return; | |
715 | |
716 case PixelFormat_Grayscale16: | |
717 AddConstantInternal<uint16_t>(image, value); | |
718 return; | |
719 | |
720 case PixelFormat_SignedGrayscale16: | |
721 AddConstantInternal<int16_t>(image, value); | |
722 return; | |
723 | |
724 default: | |
725 throw OrthancException(ErrorCode_NotImplemented); | |
726 } | |
727 } | |
728 | |
729 | |
730 void ImageProcessing::MultiplyConstant(ImageAccessor& image, | |
731 float factor) | |
732 { | |
733 switch (image.GetFormat()) | |
734 { | |
735 case PixelFormat_Grayscale8: | |
736 MultiplyConstantInternal<uint8_t>(image, factor); | |
737 return; | |
738 | |
739 case PixelFormat_Grayscale16: | |
740 MultiplyConstantInternal<uint16_t>(image, factor); | |
741 return; | |
742 | |
743 case PixelFormat_SignedGrayscale16: | |
744 MultiplyConstantInternal<int16_t>(image, factor); | |
745 return; | |
746 | |
747 default: | |
748 throw OrthancException(ErrorCode_NotImplemented); | |
749 } | |
750 } | |
751 | |
752 | |
753 void ImageProcessing::ShiftScale(ImageAccessor& image, | |
754 float offset, | |
755 float scaling) | |
756 { | |
757 switch (image.GetFormat()) | |
758 { | |
759 case PixelFormat_Grayscale8: | |
760 ShiftScaleInternal<uint8_t>(image, offset, scaling); | |
761 return; | |
762 | |
763 case PixelFormat_Grayscale16: | |
764 ShiftScaleInternal<uint16_t>(image, offset, scaling); | |
765 return; | |
766 | |
767 case PixelFormat_SignedGrayscale16: | |
768 ShiftScaleInternal<int16_t>(image, offset, scaling); | |
769 return; | |
770 | |
771 default: | |
772 throw OrthancException(ErrorCode_NotImplemented); | |
773 } | |
774 } | |
775 } |