comparison Framework/Orthanc/Core/Images/ImageProcessing.cpp @ 1:2dbe613f6c93

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