Mercurial > hg > orthanc
annotate Core/Images/ImageProcessing.cpp @ 3343:6e42d676401c
Fix issue #137 (C-STORE fails for unknown SOP Class although server is configured to accept any)
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 05 Apr 2019 16:01:42 +0200 |
parents | a15a4b9d8c00 |
children | 9345710bbf12 |
rev | line source |
---|---|
853 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
993
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
3060
4e43e67f8ecf
preparing for 2019
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2918
diff
changeset
|
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium |
853 | 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. | |
3227 | 23 * |
853 | 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 | |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
37 #include "PixelTraits.h" |
2895 | 38 #include "../OrthancException.h" |
853 | 39 |
863 | 40 #include <boost/math/special_functions/round.hpp> |
41 | |
853 | 42 #include <cassert> |
43 #include <string.h> | |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
44 #include <limits> |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
45 #include <stdint.h> |
3258 | 46 #include <algorithm> |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
47 |
853 | 48 namespace Orthanc |
49 { | |
3265
59a184cbb596
ImagePoint::Set + GetDistanceTo
Alain Mazy <alain@mazy.be>
parents:
3259
diff
changeset
|
50 double ImageProcessing::ImagePoint::GetDistanceTo(const ImagePoint& other) const |
59a184cbb596
ImagePoint::Set + GetDistanceTo
Alain Mazy <alain@mazy.be>
parents:
3259
diff
changeset
|
51 { |
59a184cbb596
ImagePoint::Set + GetDistanceTo
Alain Mazy <alain@mazy.be>
parents:
3259
diff
changeset
|
52 double dx = (double)(other.GetX() - GetX()); |
59a184cbb596
ImagePoint::Set + GetDistanceTo
Alain Mazy <alain@mazy.be>
parents:
3259
diff
changeset
|
53 double dy = (double)(other.GetY() - GetY()); |
59a184cbb596
ImagePoint::Set + GetDistanceTo
Alain Mazy <alain@mazy.be>
parents:
3259
diff
changeset
|
54 return sqrt(dx * dx + dy * dy); |
59a184cbb596
ImagePoint::Set + GetDistanceTo
Alain Mazy <alain@mazy.be>
parents:
3259
diff
changeset
|
55 } |
59a184cbb596
ImagePoint::Set + GetDistanceTo
Alain Mazy <alain@mazy.be>
parents:
3259
diff
changeset
|
56 |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
57 template <typename TargetType, typename SourceType> |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
58 static void ConvertInternal(ImageAccessor& target, |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
59 const ImageAccessor& source) |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
60 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
61 const TargetType minValue = std::numeric_limits<TargetType>::min(); |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
62 const TargetType maxValue = std::numeric_limits<TargetType>::max(); |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
63 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
64 const unsigned int width = source.GetWidth(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
65 const unsigned int height = source.GetHeight(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
66 |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
67 for (unsigned int y = 0; y < height; y++) |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
68 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
69 TargetType* t = reinterpret_cast<TargetType*>(target.GetRow(y)); |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
70 const SourceType* s = reinterpret_cast<const SourceType*>(source.GetConstRow(y)); |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
71 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
72 for (unsigned int x = 0; x < width; x++, t++, s++) |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
73 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
74 if (static_cast<int32_t>(*s) < static_cast<int32_t>(minValue)) |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
75 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
76 *t = minValue; |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
77 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
78 else if (static_cast<int32_t>(*s) > static_cast<int32_t>(maxValue)) |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
79 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
80 *t = maxValue; |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
81 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
82 else |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
83 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
84 *t = static_cast<TargetType>(*s); |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
85 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
86 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
87 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
88 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
89 |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
90 |
1993
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
91 template <typename SourceType> |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
92 static void ConvertGrayscaleToFloat(ImageAccessor& target, |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
93 const ImageAccessor& source) |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
94 { |
1994
4d099fee5eca
ImageProcessing::Set for float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1993
diff
changeset
|
95 assert(sizeof(float) == 4); |
4d099fee5eca
ImageProcessing::Set for float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1993
diff
changeset
|
96 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
97 const unsigned int width = source.GetWidth(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
98 const unsigned int height = source.GetHeight(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
99 |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
100 for (unsigned int y = 0; y < height; y++) |
1993
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
101 { |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
102 float* t = reinterpret_cast<float*>(target.GetRow(y)); |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
103 const SourceType* s = reinterpret_cast<const SourceType*>(source.GetConstRow(y)); |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
104 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
105 for (unsigned int x = 0; x < width; x++, t++, s++) |
1993
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
106 { |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
107 *t = static_cast<float>(*s); |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
108 } |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
109 } |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
110 } |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
111 |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
112 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
113 template <PixelFormat TargetFormat> |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
114 static void ConvertFloatToGrayscale(ImageAccessor& target, |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
115 const ImageAccessor& source) |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
116 { |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
117 typedef typename PixelTraits<TargetFormat>::PixelType TargetType; |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
118 |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
119 assert(sizeof(float) == 4); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
120 |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
121 const unsigned int width = source.GetWidth(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
122 const unsigned int height = source.GetHeight(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
123 |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
124 for (unsigned int y = 0; y < height; y++) |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
125 { |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
126 TargetType* q = reinterpret_cast<TargetType*>(target.GetRow(y)); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
127 const float* p = reinterpret_cast<const float*>(source.GetConstRow(y)); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
128 |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
129 for (unsigned int x = 0; x < width; x++, p++, q++) |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
130 { |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
131 PixelTraits<TargetFormat>::FloatToPixel(*q, *p); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
132 } |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
133 } |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
134 } |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
135 |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
136 |
993
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
137 template <typename TargetType> |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
138 static void ConvertColorToGrayscale(ImageAccessor& target, |
1993
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
139 const ImageAccessor& source) |
993
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
140 { |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
141 assert(source.GetFormat() == PixelFormat_RGB24); |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
142 |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
143 const TargetType minValue = std::numeric_limits<TargetType>::min(); |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
144 const TargetType maxValue = std::numeric_limits<TargetType>::max(); |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
145 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
146 const unsigned int width = source.GetWidth(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
147 const unsigned int height = source.GetHeight(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
148 |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
149 for (unsigned int y = 0; y < height; y++) |
993
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
150 { |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
151 TargetType* t = reinterpret_cast<TargetType*>(target.GetRow(y)); |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
152 const uint8_t* s = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
153 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
154 for (unsigned int x = 0; x < width; x++, t++, s += 3) |
993
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
155 { |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
156 // Y = 0.2126 R + 0.7152 G + 0.0722 B |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
157 int32_t v = (2126 * static_cast<int32_t>(s[0]) + |
3227 | 158 7152 * static_cast<int32_t>(s[1]) + |
159 0722 * static_cast<int32_t>(s[2])) / 10000; | |
993
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
160 |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
161 if (static_cast<int32_t>(v) < static_cast<int32_t>(minValue)) |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
162 { |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
163 *t = minValue; |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
164 } |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
165 else if (static_cast<int32_t>(v) > static_cast<int32_t>(maxValue)) |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
166 { |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
167 *t = maxValue; |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
168 } |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
169 else |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
170 { |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
171 *t = static_cast<TargetType>(v); |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
172 } |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
173 } |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
174 } |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
175 } |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
176 |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
177 |
2902
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
178 static void MemsetZeroInternal(ImageAccessor& image) |
3227 | 179 { |
2902
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
180 const unsigned int height = image.GetHeight(); |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
181 const size_t lineSize = image.GetBytesPerPixel() * image.GetWidth(); |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
182 const size_t pitch = image.GetPitch(); |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
183 |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
184 uint8_t *p = reinterpret_cast<uint8_t*>(image.GetBuffer()); |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
185 |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
186 for (unsigned int y = 0; y < height; y++) |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
187 { |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
188 memset(p, 0, lineSize); |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
189 p += pitch; |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
190 } |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
191 } |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
192 |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
193 |
863 | 194 template <typename PixelType> |
195 static void SetInternal(ImageAccessor& image, | |
196 int64_t constant) | |
197 { | |
2902
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
198 if (constant == 0 && |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
199 (image.GetFormat() == PixelFormat_Grayscale8 || |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
200 image.GetFormat() == PixelFormat_Grayscale16 || |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
201 image.GetFormat() == PixelFormat_Grayscale32 || |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
202 image.GetFormat() == PixelFormat_Grayscale64 || |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
203 image.GetFormat() == PixelFormat_SignedGrayscale16)) |
863 | 204 { |
2902
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
205 MemsetZeroInternal(image); |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
206 } |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
207 else |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
208 { |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
209 const unsigned int width = image.GetWidth(); |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
210 const unsigned int height = image.GetHeight(); |
863 | 211 |
2902
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
212 for (unsigned int y = 0; y < height; y++) |
863 | 213 { |
2902
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
214 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y)); |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
215 |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
216 for (unsigned int x = 0; x < width; x++, p++) |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
217 { |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
218 *p = static_cast<PixelType>(constant); |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
219 } |
863 | 220 } |
221 } | |
222 } | |
223 | |
224 | |
225 template <typename PixelType> | |
226 static void GetMinMaxValueInternal(PixelType& minValue, | |
227 PixelType& maxValue, | |
228 const ImageAccessor& source) | |
229 { | |
230 // Deal with the special case of empty image | |
231 if (source.GetWidth() == 0 || | |
232 source.GetHeight() == 0) | |
233 { | |
234 minValue = 0; | |
235 maxValue = 0; | |
236 return; | |
237 } | |
238 | |
239 minValue = std::numeric_limits<PixelType>::max(); | |
240 maxValue = std::numeric_limits<PixelType>::min(); | |
241 | |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
242 const unsigned int height = source.GetHeight(); |
2482
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
243 const unsigned int width = source.GetWidth(); |
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
244 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
245 for (unsigned int y = 0; y < height; y++) |
863 | 246 { |
247 const PixelType* p = reinterpret_cast<const PixelType*>(source.GetConstRow(y)); | |
248 | |
2482
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
249 for (unsigned int x = 0; x < width; x++, p++) |
863 | 250 { |
251 if (*p < minValue) | |
252 { | |
253 minValue = *p; | |
254 } | |
255 | |
256 if (*p > maxValue) | |
257 { | |
258 maxValue = *p; | |
259 } | |
260 } | |
261 } | |
262 } | |
263 | |
264 | |
265 | |
266 template <typename PixelType> | |
267 static void AddConstantInternal(ImageAccessor& image, | |
268 int64_t constant) | |
269 { | |
270 if (constant == 0) | |
271 { | |
272 return; | |
273 } | |
274 | |
275 const int64_t minValue = std::numeric_limits<PixelType>::min(); | |
276 const int64_t maxValue = std::numeric_limits<PixelType>::max(); | |
277 | |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
278 const unsigned int width = image.GetWidth(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
279 const unsigned int height = image.GetHeight(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
280 |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
281 for (unsigned int y = 0; y < height; y++) |
863 | 282 { |
283 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y)); | |
284 | |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
285 for (unsigned int x = 0; x < width; x++, p++) |
863 | 286 { |
287 int64_t v = static_cast<int64_t>(*p) + constant; | |
288 | |
289 if (v > maxValue) | |
290 { | |
876 | 291 *p = std::numeric_limits<PixelType>::max(); |
863 | 292 } |
293 else if (v < minValue) | |
294 { | |
876 | 295 *p = std::numeric_limits<PixelType>::min(); |
863 | 296 } |
297 else | |
298 { | |
299 *p = static_cast<PixelType>(v); | |
300 } | |
301 } | |
302 } | |
303 } | |
304 | |
305 | |
306 | |
2488
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
307 template <typename PixelType, |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
308 bool UseRound> |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
309 static void MultiplyConstantInternal(ImageAccessor& image, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
310 float factor) |
863 | 311 { |
1334 | 312 if (std::abs(factor - 1.0f) <= std::numeric_limits<float>::epsilon()) |
863 | 313 { |
314 return; | |
315 } | |
316 | |
317 const int64_t minValue = std::numeric_limits<PixelType>::min(); | |
318 const int64_t maxValue = std::numeric_limits<PixelType>::max(); | |
319 | |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
320 const unsigned int width = image.GetWidth(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
321 const unsigned int height = image.GetHeight(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
322 |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
323 for (unsigned int y = 0; y < height; y++) |
863 | 324 { |
325 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y)); | |
326 | |
2482
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
327 for (unsigned int x = 0; x < width; x++, p++) |
863 | 328 { |
2488
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
329 int64_t v; |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
330 if (UseRound) |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
331 { |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
332 // The "round" operation is very costly |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
333 v = boost::math::llround(static_cast<float>(*p) * factor); |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
334 } |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
335 else |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
336 { |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
337 v = static_cast<int64_t>(static_cast<float>(*p) * factor); |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
338 } |
863 | 339 |
340 if (v > maxValue) | |
341 { | |
876 | 342 *p = std::numeric_limits<PixelType>::max(); |
863 | 343 } |
344 else if (v < minValue) | |
345 { | |
876 | 346 *p = std::numeric_limits<PixelType>::min(); |
863 | 347 } |
348 else | |
349 { | |
350 *p = static_cast<PixelType>(v); | |
351 } | |
352 } | |
353 } | |
354 } | |
355 | |
356 | |
2488
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
357 template <typename PixelType, |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
358 bool UseRound> |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
359 static void ShiftScaleInternal(ImageAccessor& image, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
360 float offset, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
361 float scaling) |
863 | 362 { |
2482
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
363 const float minFloatValue = static_cast<float>(std::numeric_limits<PixelType>::min()); |
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
364 const float maxFloatValue = static_cast<float>(std::numeric_limits<PixelType>::max()); |
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
365 const PixelType minPixelValue = std::numeric_limits<PixelType>::min(); |
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
366 const PixelType maxPixelValue = std::numeric_limits<PixelType>::max(); |
863 | 367 |
2482
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
368 const unsigned int height = image.GetHeight(); |
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
369 const unsigned int width = image.GetWidth(); |
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
370 |
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
371 for (unsigned int y = 0; y < height; y++) |
863 | 372 { |
373 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y)); | |
374 | |
2482
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
375 for (unsigned int x = 0; x < width; x++, p++) |
863 | 376 { |
377 float v = (static_cast<float>(*p) + offset) * scaling; | |
378 | |
2482
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
379 if (v > maxFloatValue) |
863 | 380 { |
2482
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
381 *p = maxPixelValue; |
863 | 382 } |
2482
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
383 else if (v < minFloatValue) |
863 | 384 { |
2482
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
385 *p = minPixelValue; |
863 | 386 } |
2488
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
387 else if (UseRound) |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
388 { |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
389 // The "round" operation is very costly |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
390 *p = static_cast<PixelType>(boost::math::iround(v)); |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
391 } |
863 | 392 else |
393 { | |
2482
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
394 *p = static_cast<PixelType>(v); |
863 | 395 } |
396 } | |
397 } | |
398 } | |
399 | |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
400 |
853 | 401 void ImageProcessing::Copy(ImageAccessor& target, |
402 const ImageAccessor& source) | |
403 { | |
404 if (target.GetWidth() != source.GetWidth() || | |
405 target.GetHeight() != source.GetHeight()) | |
406 { | |
407 throw OrthancException(ErrorCode_IncompatibleImageSize); | |
408 } | |
409 | |
410 if (target.GetFormat() != source.GetFormat()) | |
411 { | |
412 throw OrthancException(ErrorCode_IncompatibleImageFormat); | |
413 } | |
414 | |
2902
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
415 unsigned int lineSize = source.GetBytesPerPixel() * source.GetWidth(); |
853 | 416 |
417 assert(source.GetPitch() >= lineSize && target.GetPitch() >= lineSize); | |
418 | |
419 for (unsigned int y = 0; y < source.GetHeight(); y++) | |
420 { | |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
421 memcpy(target.GetRow(y), source.GetConstRow(y), lineSize); |
853 | 422 } |
423 } | |
424 | |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
425 |
853 | 426 void ImageProcessing::Convert(ImageAccessor& target, |
427 const ImageAccessor& source) | |
428 { | |
429 if (target.GetWidth() != source.GetWidth() || | |
430 target.GetHeight() != source.GetHeight()) | |
431 { | |
432 throw OrthancException(ErrorCode_IncompatibleImageSize); | |
433 } | |
434 | |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
435 const unsigned int width = source.GetWidth(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
436 const unsigned int height = source.GetHeight(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
437 |
853 | 438 if (source.GetFormat() == target.GetFormat()) |
439 { | |
440 Copy(target, source); | |
441 return; | |
442 } | |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
443 |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
444 if (target.GetFormat() == PixelFormat_Grayscale16 && |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
445 source.GetFormat() == PixelFormat_Grayscale8) |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
446 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
447 ConvertInternal<uint16_t, uint8_t>(target, source); |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
448 return; |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
449 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
450 |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
451 if (target.GetFormat() == PixelFormat_SignedGrayscale16 && |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
452 source.GetFormat() == PixelFormat_Grayscale8) |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
453 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
454 ConvertInternal<int16_t, uint8_t>(target, source); |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
455 return; |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
456 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
457 |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
458 if (target.GetFormat() == PixelFormat_Grayscale8 && |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
459 source.GetFormat() == PixelFormat_Grayscale16) |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
460 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
461 ConvertInternal<uint8_t, uint16_t>(target, source); |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
462 return; |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
463 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
464 |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
465 if (target.GetFormat() == PixelFormat_SignedGrayscale16 && |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
466 source.GetFormat() == PixelFormat_Grayscale16) |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
467 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
468 ConvertInternal<int16_t, uint16_t>(target, source); |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
469 return; |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
470 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
471 |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
472 if (target.GetFormat() == PixelFormat_Grayscale8 && |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
473 source.GetFormat() == PixelFormat_SignedGrayscale16) |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
474 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
475 ConvertInternal<uint8_t, int16_t>(target, source); |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
476 return; |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
477 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
478 |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
479 if (target.GetFormat() == PixelFormat_Grayscale16 && |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
480 source.GetFormat() == PixelFormat_SignedGrayscale16) |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
481 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
482 ConvertInternal<uint16_t, int16_t>(target, source); |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
483 return; |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
484 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
485 |
993
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
486 if (target.GetFormat() == PixelFormat_Grayscale8 && |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
487 source.GetFormat() == PixelFormat_RGB24) |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
488 { |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
489 ConvertColorToGrayscale<uint8_t>(target, source); |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
490 return; |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
491 } |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
492 |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
493 if (target.GetFormat() == PixelFormat_Grayscale16 && |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
494 source.GetFormat() == PixelFormat_RGB24) |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
495 { |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
496 ConvertColorToGrayscale<uint16_t>(target, source); |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
497 return; |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
498 } |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
499 |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
500 if (target.GetFormat() == PixelFormat_SignedGrayscale16 && |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
501 source.GetFormat() == PixelFormat_RGB24) |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
502 { |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
503 ConvertColorToGrayscale<int16_t>(target, source); |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
504 return; |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
505 } |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
506 |
1993
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
507 if (target.GetFormat() == PixelFormat_Float32 && |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
508 source.GetFormat() == PixelFormat_Grayscale8) |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
509 { |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
510 ConvertGrayscaleToFloat<uint8_t>(target, source); |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
511 return; |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
512 } |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
513 |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
514 if (target.GetFormat() == PixelFormat_Float32 && |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
515 source.GetFormat() == PixelFormat_Grayscale16) |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
516 { |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
517 ConvertGrayscaleToFloat<uint16_t>(target, source); |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
518 return; |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
519 } |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
520 |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
521 if (target.GetFormat() == PixelFormat_Float32 && |
2415
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
522 source.GetFormat() == PixelFormat_Grayscale32) |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
523 { |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
524 ConvertGrayscaleToFloat<uint32_t>(target, source); |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
525 return; |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
526 } |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
527 |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
528 if (target.GetFormat() == PixelFormat_Float32 && |
1993
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
529 source.GetFormat() == PixelFormat_SignedGrayscale16) |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
530 { |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
531 ConvertGrayscaleToFloat<int16_t>(target, source); |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
532 return; |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
533 } |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
534 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
535 |
1610
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
536 if (target.GetFormat() == PixelFormat_Grayscale8 && |
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
537 source.GetFormat() == PixelFormat_RGBA32) |
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
538 { |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
539 for (unsigned int y = 0; y < height; y++) |
1610
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
540 { |
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
541 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); |
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
542 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
543 for (unsigned int x = 0; x < width; x++, q++) |
1610
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
544 { |
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
545 *q = static_cast<uint8_t>((2126 * static_cast<uint32_t>(p[0]) + |
3227 | 546 7152 * static_cast<uint32_t>(p[1]) + |
547 0722 * static_cast<uint32_t>(p[2])) / 10000); | |
1610
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
548 p += 4; |
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
549 } |
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
550 } |
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
551 |
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
552 return; |
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
553 } |
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
554 |
2840
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
555 if (target.GetFormat() == PixelFormat_Grayscale8 && |
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
556 source.GetFormat() == PixelFormat_BGRA32) |
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
557 { |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
558 for (unsigned int y = 0; y < height; y++) |
2840
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
559 { |
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
560 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); |
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
561 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
562 for (unsigned int x = 0; x < width; x++, q++) |
2840
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
563 { |
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
564 *q = static_cast<uint8_t>((2126 * static_cast<uint32_t>(p[2]) + |
3227 | 565 7152 * static_cast<uint32_t>(p[1]) + |
566 0722 * static_cast<uint32_t>(p[0])) / 10000); | |
2840
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
567 p += 4; |
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
568 } |
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
569 } |
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
570 |
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
571 return; |
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
572 } |
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
573 |
1608
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
574 if (target.GetFormat() == PixelFormat_RGB24 && |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
575 source.GetFormat() == PixelFormat_RGBA32) |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
576 { |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
577 for (unsigned int y = 0; y < height; y++) |
1608
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
578 { |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
579 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
580 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
581 for (unsigned int x = 0; x < width; x++) |
1608
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
582 { |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
583 q[0] = p[0]; |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
584 q[1] = p[1]; |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
585 q[2] = p[2]; |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
586 p += 4; |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
587 q += 3; |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
588 } |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
589 } |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
590 |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
591 return; |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
592 } |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
593 |
2252
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
594 if (target.GetFormat() == PixelFormat_RGB24 && |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
595 source.GetFormat() == PixelFormat_BGRA32) |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
596 { |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
597 for (unsigned int y = 0; y < height; y++) |
2252
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
598 { |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
599 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
600 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
601 for (unsigned int x = 0; x < width; x++) |
2252
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
602 { |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
603 q[0] = p[2]; |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
604 q[1] = p[1]; |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
605 q[2] = p[0]; |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
606 p += 4; |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
607 q += 3; |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
608 } |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
609 } |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
610 |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
611 return; |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
612 } |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
613 |
1608
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
614 if (target.GetFormat() == PixelFormat_RGBA32 && |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
615 source.GetFormat() == PixelFormat_RGB24) |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
616 { |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
617 for (unsigned int y = 0; y < height; y++) |
1608
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
618 { |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
619 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
620 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
621 for (unsigned int x = 0; x < width; x++) |
1608
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
622 { |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
623 q[0] = p[0]; |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
624 q[1] = p[1]; |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
625 q[2] = p[2]; |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
626 q[3] = 255; // Set the alpha channel to full opacity |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
627 p += 3; |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
628 q += 4; |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
629 } |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
630 } |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
631 |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
632 return; |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
633 } |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
634 |
1992
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
635 if (target.GetFormat() == PixelFormat_RGB24 && |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
636 source.GetFormat() == PixelFormat_Grayscale8) |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
637 { |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
638 for (unsigned int y = 0; y < height; y++) |
1992
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
639 { |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
640 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
641 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
642 for (unsigned int x = 0; x < width; x++) |
1992
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
643 { |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
644 q[0] = *p; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
645 q[1] = *p; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
646 q[2] = *p; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
647 p += 1; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
648 q += 3; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
649 } |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
650 } |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
651 |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
652 return; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
653 } |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
654 |
2650 | 655 if ((target.GetFormat() == PixelFormat_RGBA32 || |
656 target.GetFormat() == PixelFormat_BGRA32) && | |
1992
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
657 source.GetFormat() == PixelFormat_Grayscale8) |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
658 { |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
659 for (unsigned int y = 0; y < height; y++) |
1992
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
660 { |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
661 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
662 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
663 for (unsigned int x = 0; x < width; x++) |
1992
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
664 { |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
665 q[0] = *p; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
666 q[1] = *p; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
667 q[2] = *p; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
668 q[3] = 255; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
669 p += 1; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
670 q += 4; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
671 } |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
672 } |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
673 |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
674 return; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
675 } |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
676 |
2100
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
677 if (target.GetFormat() == PixelFormat_BGRA32 && |
2495
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
678 source.GetFormat() == PixelFormat_Grayscale16) |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
679 { |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
680 for (unsigned int y = 0; y < height; y++) |
2495
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
681 { |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
682 const uint16_t* p = reinterpret_cast<const uint16_t*>(source.GetConstRow(y)); |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
683 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
684 for (unsigned int x = 0; x < width; x++) |
2495
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
685 { |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
686 uint8_t value = (*p < 256 ? *p : 255); |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
687 q[0] = value; |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
688 q[1] = value; |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
689 q[2] = value; |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
690 q[3] = 255; |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
691 p += 1; |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
692 q += 4; |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
693 } |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
694 } |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
695 |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
696 return; |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
697 } |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
698 |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
699 if (target.GetFormat() == PixelFormat_BGRA32 && |
2496
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
700 source.GetFormat() == PixelFormat_SignedGrayscale16) |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
701 { |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
702 for (unsigned int y = 0; y < height; y++) |
2496
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
703 { |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
704 const int16_t* p = reinterpret_cast<const int16_t*>(source.GetConstRow(y)); |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
705 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
706 for (unsigned int x = 0; x < width; x++) |
2496
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
707 { |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
708 uint8_t value; |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
709 if (*p < 0) |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
710 { |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
711 value = 0; |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
712 } |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
713 else if (*p > 255) |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
714 { |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
715 value = 255; |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
716 } |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
717 else |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
718 { |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
719 value = static_cast<uint8_t>(*p); |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
720 } |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
721 |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
722 q[0] = value; |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
723 q[1] = value; |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
724 q[2] = value; |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
725 q[3] = 255; |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
726 p += 1; |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
727 q += 4; |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
728 } |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
729 } |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
730 |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
731 return; |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
732 } |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
733 |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
734 if (target.GetFormat() == PixelFormat_BGRA32 && |
2100
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
735 source.GetFormat() == PixelFormat_RGB24) |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
736 { |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
737 for (unsigned int y = 0; y < height; y++) |
2100
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
738 { |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
739 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
740 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
741 for (unsigned int x = 0; x < width; x++) |
2100
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
742 { |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
743 q[0] = p[2]; |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
744 q[1] = p[1]; |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
745 q[2] = p[0]; |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
746 q[3] = 255; |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
747 p += 3; |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
748 q += 4; |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
749 } |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
750 } |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
751 |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
752 return; |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
753 } |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
754 |
2423
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
755 if (target.GetFormat() == PixelFormat_RGB24 && |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
756 source.GetFormat() == PixelFormat_RGB48) |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
757 { |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
758 for (unsigned int y = 0; y < height; y++) |
2423
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
759 { |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
760 const uint16_t* p = reinterpret_cast<const uint16_t*>(source.GetConstRow(y)); |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
761 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
762 for (unsigned int x = 0; x < width; x++) |
2423
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
763 { |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
764 q[0] = p[0] >> 8; |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
765 q[1] = p[1] >> 8; |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
766 q[2] = p[2] >> 8; |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
767 p += 3; |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
768 q += 3; |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
769 } |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
770 } |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
771 |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
772 return; |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
773 } |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
774 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
775 if (target.GetFormat() == PixelFormat_Grayscale16 && |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
776 source.GetFormat() == PixelFormat_Float32) |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
777 { |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
778 ConvertFloatToGrayscale<PixelFormat_Grayscale16>(target, source); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
779 return; |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
780 } |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
781 |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
782 if (target.GetFormat() == PixelFormat_Grayscale8 && |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
783 source.GetFormat() == PixelFormat_Float32) |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
784 { |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
785 ConvertFloatToGrayscale<PixelFormat_Grayscale8>(target, source); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
786 return; |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
787 } |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
788 |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
789 throw OrthancException(ErrorCode_NotImplemented); |
853 | 790 } |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
791 |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
792 |
863 | 793 |
794 void ImageProcessing::Set(ImageAccessor& image, | |
795 int64_t value) | |
796 { | |
797 switch (image.GetFormat()) | |
798 { | |
3227 | 799 case PixelFormat_Grayscale8: |
800 SetInternal<uint8_t>(image, value); | |
801 return; | |
863 | 802 |
3227 | 803 case PixelFormat_Grayscale16: |
804 SetInternal<uint16_t>(image, value); | |
805 return; | |
863 | 806 |
3227 | 807 case PixelFormat_Grayscale32: |
808 SetInternal<uint32_t>(image, value); | |
809 return; | |
2415
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
810 |
3227 | 811 case PixelFormat_Grayscale64: |
812 SetInternal<uint64_t>(image, value); | |
813 return; | |
2645
89b789366596
Grayscale64 pixel format
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2496
diff
changeset
|
814 |
3227 | 815 case PixelFormat_SignedGrayscale16: |
816 SetInternal<int16_t>(image, value); | |
817 return; | |
863 | 818 |
3227 | 819 case PixelFormat_Float32: |
820 assert(sizeof(float) == 4); | |
821 SetInternal<float>(image, value); | |
822 return; | |
1994
4d099fee5eca
ImageProcessing::Set for float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1993
diff
changeset
|
823 |
3227 | 824 default: |
825 throw OrthancException(ErrorCode_NotImplemented); | |
863 | 826 } |
827 } | |
828 | |
829 | |
2089
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
830 void ImageProcessing::Set(ImageAccessor& image, |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
831 uint8_t red, |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
832 uint8_t green, |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
833 uint8_t blue, |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
834 uint8_t alpha) |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
835 { |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
836 uint8_t p[4]; |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
837 unsigned int size; |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
838 |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
839 switch (image.GetFormat()) |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
840 { |
3227 | 841 case PixelFormat_RGBA32: |
842 p[0] = red; | |
843 p[1] = green; | |
844 p[2] = blue; | |
845 p[3] = alpha; | |
846 size = 4; | |
847 break; | |
2089
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
848 |
3227 | 849 case PixelFormat_BGRA32: |
850 p[0] = blue; | |
851 p[1] = green; | |
852 p[2] = red; | |
853 p[3] = alpha; | |
854 size = 4; | |
855 break; | |
2089
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
856 |
3227 | 857 case PixelFormat_RGB24: |
858 p[0] = red; | |
859 p[1] = green; | |
860 p[2] = blue; | |
861 size = 3; | |
862 break; | |
2089
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
863 |
3227 | 864 default: |
865 throw OrthancException(ErrorCode_NotImplemented); | |
866 } | |
2089
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
867 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
868 const unsigned int width = image.GetWidth(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
869 const unsigned int height = image.GetHeight(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
870 |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
871 for (unsigned int y = 0; y < height; y++) |
2089
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
872 { |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
873 uint8_t* q = reinterpret_cast<uint8_t*>(image.GetRow(y)); |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
874 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
875 for (unsigned int x = 0; x < width; x++) |
2089
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
876 { |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
877 for (unsigned int i = 0; i < size; i++) |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
878 { |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
879 q[i] = p[i]; |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
880 } |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
881 |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
882 q += size; |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
883 } |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
884 } |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
885 } |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
886 |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
887 |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
888 void ImageProcessing::ShiftRight(ImageAccessor& image, |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
889 unsigned int shift) |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
890 { |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
891 if (image.GetWidth() == 0 || |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
892 image.GetHeight() == 0 || |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
893 shift == 0) |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
894 { |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
895 // Nothing to do |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
896 return; |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
897 } |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
898 |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
899 throw OrthancException(ErrorCode_NotImplemented); |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
900 } |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
901 |
863 | 902 |
2415
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
903 void ImageProcessing::GetMinMaxIntegerValue(int64_t& minValue, |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
904 int64_t& maxValue, |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
905 const ImageAccessor& image) |
863 | 906 { |
907 switch (image.GetFormat()) | |
908 { | |
3227 | 909 case PixelFormat_Grayscale8: |
910 { | |
911 uint8_t a, b; | |
912 GetMinMaxValueInternal<uint8_t>(a, b, image); | |
913 minValue = a; | |
914 maxValue = b; | |
915 break; | |
916 } | |
863 | 917 |
3227 | 918 case PixelFormat_Grayscale16: |
919 { | |
920 uint16_t a, b; | |
921 GetMinMaxValueInternal<uint16_t>(a, b, image); | |
922 minValue = a; | |
923 maxValue = b; | |
924 break; | |
925 } | |
863 | 926 |
3227 | 927 case PixelFormat_Grayscale32: |
928 { | |
929 uint32_t a, b; | |
930 GetMinMaxValueInternal<uint32_t>(a, b, image); | |
931 minValue = a; | |
932 maxValue = b; | |
933 break; | |
934 } | |
2415
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
935 |
3227 | 936 case PixelFormat_SignedGrayscale16: |
937 { | |
938 int16_t a, b; | |
939 GetMinMaxValueInternal<int16_t>(a, b, image); | |
940 minValue = a; | |
941 maxValue = b; | |
942 break; | |
943 } | |
863 | 944 |
3227 | 945 default: |
946 throw OrthancException(ErrorCode_NotImplemented); | |
863 | 947 } |
948 } | |
949 | |
950 | |
2415
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
951 void ImageProcessing::GetMinMaxFloatValue(float& minValue, |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
952 float& maxValue, |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
953 const ImageAccessor& image) |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
954 { |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
955 switch (image.GetFormat()) |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
956 { |
3227 | 957 case PixelFormat_Float32: |
958 { | |
959 assert(sizeof(float) == 4); | |
960 float a, b; | |
961 GetMinMaxValueInternal<float>(a, b, image); | |
962 minValue = a; | |
963 maxValue = b; | |
964 break; | |
965 } | |
2415
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
966 |
3227 | 967 default: |
968 throw OrthancException(ErrorCode_NotImplemented); | |
2415
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
969 } |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
970 } |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
971 |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
972 |
863 | 973 |
974 void ImageProcessing::AddConstant(ImageAccessor& image, | |
975 int64_t value) | |
976 { | |
977 switch (image.GetFormat()) | |
978 { | |
3227 | 979 case PixelFormat_Grayscale8: |
980 AddConstantInternal<uint8_t>(image, value); | |
981 return; | |
863 | 982 |
3227 | 983 case PixelFormat_Grayscale16: |
984 AddConstantInternal<uint16_t>(image, value); | |
985 return; | |
863 | 986 |
3227 | 987 case PixelFormat_SignedGrayscale16: |
988 AddConstantInternal<int16_t>(image, value); | |
989 return; | |
863 | 990 |
3227 | 991 default: |
992 throw OrthancException(ErrorCode_NotImplemented); | |
863 | 993 } |
994 } | |
995 | |
996 | |
997 void ImageProcessing::MultiplyConstant(ImageAccessor& image, | |
2488
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
998 float factor, |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
999 bool useRound) |
863 | 1000 { |
1001 switch (image.GetFormat()) | |
1002 { | |
3227 | 1003 case PixelFormat_Grayscale8: |
1004 if (useRound) | |
1005 { | |
1006 MultiplyConstantInternal<uint8_t, true>(image, factor); | |
1007 } | |
1008 else | |
1009 { | |
1010 MultiplyConstantInternal<uint8_t, false>(image, factor); | |
1011 } | |
1012 return; | |
863 | 1013 |
3227 | 1014 case PixelFormat_Grayscale16: |
1015 if (useRound) | |
1016 { | |
1017 MultiplyConstantInternal<uint16_t, true>(image, factor); | |
1018 } | |
1019 else | |
1020 { | |
1021 MultiplyConstantInternal<uint16_t, false>(image, factor); | |
1022 } | |
1023 return; | |
863 | 1024 |
3227 | 1025 case PixelFormat_SignedGrayscale16: |
1026 if (useRound) | |
1027 { | |
1028 MultiplyConstantInternal<int16_t, true>(image, factor); | |
1029 } | |
1030 else | |
1031 { | |
1032 MultiplyConstantInternal<int16_t, false>(image, factor); | |
1033 } | |
1034 return; | |
863 | 1035 |
3227 | 1036 default: |
1037 throw OrthancException(ErrorCode_NotImplemented); | |
863 | 1038 } |
1039 } | |
1040 | |
1041 | |
1042 void ImageProcessing::ShiftScale(ImageAccessor& image, | |
1043 float offset, | |
2488
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
1044 float scaling, |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
1045 bool useRound) |
863 | 1046 { |
1047 switch (image.GetFormat()) | |
1048 { | |
3227 | 1049 case PixelFormat_Grayscale8: |
1050 if (useRound) | |
1051 { | |
1052 ShiftScaleInternal<uint8_t, true>(image, offset, scaling); | |
1053 } | |
1054 else | |
1055 { | |
1056 ShiftScaleInternal<uint8_t, false>(image, offset, scaling); | |
1057 } | |
1058 return; | |
863 | 1059 |
3227 | 1060 case PixelFormat_Grayscale16: |
1061 if (useRound) | |
1062 { | |
1063 ShiftScaleInternal<uint16_t, true>(image, offset, scaling); | |
1064 } | |
1065 else | |
1066 { | |
1067 ShiftScaleInternal<uint16_t, false>(image, offset, scaling); | |
1068 } | |
1069 return; | |
863 | 1070 |
3227 | 1071 case PixelFormat_SignedGrayscale16: |
1072 if (useRound) | |
1073 { | |
1074 ShiftScaleInternal<int16_t, true>(image, offset, scaling); | |
1075 } | |
1076 else | |
1077 { | |
1078 ShiftScaleInternal<int16_t, false>(image, offset, scaling); | |
1079 } | |
1080 return; | |
863 | 1081 |
3227 | 1082 default: |
1083 throw OrthancException(ErrorCode_NotImplemented); | |
863 | 1084 } |
1085 } | |
2281
e002430baa41
Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2252
diff
changeset
|
1086 |
e002430baa41
Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2252
diff
changeset
|
1087 |
3227 | 1088 void ImageProcessing::Invert(ImageAccessor& image, int64_t maxValue) |
1089 { | |
1090 const unsigned int width = image.GetWidth(); | |
1091 const unsigned int height = image.GetHeight(); | |
1092 | |
1093 switch (image.GetFormat()) | |
1094 { | |
1095 case PixelFormat_Grayscale16: | |
1096 { | |
3234
3ca924140de6
merged ImageProcessing::Invert implementations
am@osimis.io
parents:
3227
diff
changeset
|
1097 uint16_t maxValueUint16 = (uint16_t)(std::min(maxValue, static_cast<int64_t>(std::numeric_limits<uint16_t>::max()))); |
3227 | 1098 |
1099 for (unsigned int y = 0; y < height; y++) | |
1100 { | |
1101 uint16_t* p = reinterpret_cast<uint16_t*>(image.GetRow(y)); | |
1102 | |
1103 for (unsigned int x = 0; x < width; x++, p++) | |
1104 { | |
1105 *p = maxValueUint16 - (*p); | |
1106 } | |
1107 } | |
1108 | |
1109 return; | |
1110 } | |
3234
3ca924140de6
merged ImageProcessing::Invert implementations
am@osimis.io
parents:
3227
diff
changeset
|
1111 case PixelFormat_Grayscale8: |
3ca924140de6
merged ImageProcessing::Invert implementations
am@osimis.io
parents:
3227
diff
changeset
|
1112 { |
3ca924140de6
merged ImageProcessing::Invert implementations
am@osimis.io
parents:
3227
diff
changeset
|
1113 uint8_t maxValueUint8 = (uint8_t)(std::min(maxValue, static_cast<int64_t>(std::numeric_limits<uint8_t>::max()))); |
3ca924140de6
merged ImageProcessing::Invert implementations
am@osimis.io
parents:
3227
diff
changeset
|
1114 |
3ca924140de6
merged ImageProcessing::Invert implementations
am@osimis.io
parents:
3227
diff
changeset
|
1115 for (unsigned int y = 0; y < height; y++) |
3ca924140de6
merged ImageProcessing::Invert implementations
am@osimis.io
parents:
3227
diff
changeset
|
1116 { |
3ca924140de6
merged ImageProcessing::Invert implementations
am@osimis.io
parents:
3227
diff
changeset
|
1117 uint8_t* p = reinterpret_cast<uint8_t*>(image.GetRow(y)); |
3ca924140de6
merged ImageProcessing::Invert implementations
am@osimis.io
parents:
3227
diff
changeset
|
1118 |
3ca924140de6
merged ImageProcessing::Invert implementations
am@osimis.io
parents:
3227
diff
changeset
|
1119 for (unsigned int x = 0; x < width; x++, p++) |
3ca924140de6
merged ImageProcessing::Invert implementations
am@osimis.io
parents:
3227
diff
changeset
|
1120 { |
3ca924140de6
merged ImageProcessing::Invert implementations
am@osimis.io
parents:
3227
diff
changeset
|
1121 *p = maxValueUint8 - (*p); |
3ca924140de6
merged ImageProcessing::Invert implementations
am@osimis.io
parents:
3227
diff
changeset
|
1122 } |
3ca924140de6
merged ImageProcessing::Invert implementations
am@osimis.io
parents:
3227
diff
changeset
|
1123 } |
3ca924140de6
merged ImageProcessing::Invert implementations
am@osimis.io
parents:
3227
diff
changeset
|
1124 |
3ca924140de6
merged ImageProcessing::Invert implementations
am@osimis.io
parents:
3227
diff
changeset
|
1125 return; |
3ca924140de6
merged ImageProcessing::Invert implementations
am@osimis.io
parents:
3227
diff
changeset
|
1126 } |
3227 | 1127 |
1128 default: | |
1129 throw OrthancException(ErrorCode_NotImplemented); | |
1130 } | |
1131 | |
1132 } | |
1133 | |
2281
e002430baa41
Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2252
diff
changeset
|
1134 void ImageProcessing::Invert(ImageAccessor& image) |
e002430baa41
Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2252
diff
changeset
|
1135 { |
e002430baa41
Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2252
diff
changeset
|
1136 switch (image.GetFormat()) |
e002430baa41
Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2252
diff
changeset
|
1137 { |
3227 | 1138 case PixelFormat_Grayscale8: |
3234
3ca924140de6
merged ImageProcessing::Invert implementations
am@osimis.io
parents:
3227
diff
changeset
|
1139 return Invert(image, 255); |
3227 | 1140 default: |
3234
3ca924140de6
merged ImageProcessing::Invert implementations
am@osimis.io
parents:
3227
diff
changeset
|
1141 throw OrthancException(ErrorCode_NotImplemented); // you should use the Invert(image, maxValue) overload |
3227 | 1142 } |
2281
e002430baa41
Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2252
diff
changeset
|
1143 } |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1144 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1145 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1146 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1147 namespace |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1148 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1149 template <Orthanc::PixelFormat Format> |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1150 class BresenhamPixelWriter |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1151 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1152 private: |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1153 typedef typename PixelTraits<Format>::PixelType PixelType; |
3227 | 1154 |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1155 Orthanc::ImageAccessor& image_; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1156 PixelType value_; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1157 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1158 void PlotLineLow(int x0, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1159 int y0, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1160 int x1, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1161 int y1) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1162 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1163 int dx = x1 - x0; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1164 int dy = y1 - y0; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1165 int yi = 1; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1166 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1167 if (dy < 0) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1168 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1169 yi = -1; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1170 dy = -dy; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1171 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1172 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1173 int d = 2 * dy - dx; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1174 int y = y0; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1175 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1176 for (int x = x0; x <= x1; x++) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1177 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1178 Write(x, y); |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1179 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1180 if (d > 0) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1181 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1182 y = y + yi; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1183 d = d - 2 * dx; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1184 } |
3227 | 1185 |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1186 d = d + 2*dy; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1187 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1188 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1189 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1190 void PlotLineHigh(int x0, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1191 int y0, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1192 int x1, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1193 int y1) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1194 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1195 int dx = x1 - x0; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1196 int dy = y1 - y0; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1197 int xi = 1; |
3227 | 1198 |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1199 if (dx < 0) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1200 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1201 xi = -1; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1202 dx = -dx; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1203 } |
3227 | 1204 |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1205 int d = 2 * dx - dy; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1206 int x = x0; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1207 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1208 for (int y = y0; y <= y1; y++) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1209 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1210 Write(x, y); |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1211 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1212 if (d > 0) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1213 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1214 x = x + xi; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1215 d = d - 2 * dy; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1216 } |
3227 | 1217 |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1218 d = d + 2 * dx; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1219 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1220 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1221 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1222 public: |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1223 BresenhamPixelWriter(Orthanc::ImageAccessor& image, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1224 int64_t value) : |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1225 image_(image), |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1226 value_(PixelTraits<Format>::IntegerToPixel(value)) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1227 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1228 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1229 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1230 BresenhamPixelWriter(Orthanc::ImageAccessor& image, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1231 const PixelType& value) : |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1232 image_(image), |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1233 value_(value) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1234 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1235 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1236 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1237 void Write(int x, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1238 int y) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1239 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1240 if (x >= 0 && |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1241 y >= 0 && |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1242 static_cast<unsigned int>(x) < image_.GetWidth() && |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1243 static_cast<unsigned int>(y) < image_.GetHeight()) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1244 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1245 PixelType* p = reinterpret_cast<PixelType*>(image_.GetRow(y)); |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1246 p[x] = value_; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1247 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1248 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1249 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1250 void DrawSegment(int x0, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1251 int y0, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1252 int x1, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1253 int y1) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1254 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1255 // This is an implementation of Bresenham's line algorithm |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1256 // https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm#All_cases |
3227 | 1257 |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1258 if (abs(y1 - y0) < abs(x1 - x0)) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1259 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1260 if (x0 > x1) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1261 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1262 PlotLineLow(x1, y1, x0, y0); |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1263 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1264 else |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1265 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1266 PlotLineLow(x0, y0, x1, y1); |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1267 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1268 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1269 else |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1270 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1271 if (y0 > y1) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1272 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1273 PlotLineHigh(x1, y1, x0, y0); |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1274 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1275 else |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1276 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1277 PlotLineHigh(x0, y0, x1, y1); |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1278 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1279 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1280 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1281 }; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1282 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1283 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1284 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1285 void ImageProcessing::DrawLineSegment(ImageAccessor& image, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1286 int x0, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1287 int y0, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1288 int x1, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1289 int y1, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1290 int64_t value) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1291 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1292 switch (image.GetFormat()) |
3227 | 1293 { |
1294 case Orthanc::PixelFormat_Grayscale8: | |
1295 { | |
1296 BresenhamPixelWriter<Orthanc::PixelFormat_Grayscale8> writer(image, value); | |
1297 writer.DrawSegment(x0, y0, x1, y1); | |
1298 break; | |
1299 } | |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1300 |
3227 | 1301 case Orthanc::PixelFormat_Grayscale16: |
1302 { | |
1303 BresenhamPixelWriter<Orthanc::PixelFormat_Grayscale16> writer(image, value); | |
1304 writer.DrawSegment(x0, y0, x1, y1); | |
1305 break; | |
1306 } | |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1307 |
3227 | 1308 case Orthanc::PixelFormat_SignedGrayscale16: |
1309 { | |
1310 BresenhamPixelWriter<Orthanc::PixelFormat_SignedGrayscale16> writer(image, value); | |
1311 writer.DrawSegment(x0, y0, x1, y1); | |
1312 break; | |
1313 } | |
1314 | |
1315 default: | |
1316 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1317 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1318 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1319 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1320 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1321 void ImageProcessing::DrawLineSegment(ImageAccessor& image, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1322 int x0, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1323 int y0, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1324 int x1, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1325 int y1, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1326 uint8_t red, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1327 uint8_t green, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1328 uint8_t blue, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1329 uint8_t alpha) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1330 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1331 switch (image.GetFormat()) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1332 { |
3227 | 1333 case Orthanc::PixelFormat_BGRA32: |
1334 { | |
1335 PixelTraits<Orthanc::PixelFormat_BGRA32>::PixelType pixel; | |
1336 pixel.red_ = red; | |
1337 pixel.green_ = green; | |
1338 pixel.blue_ = blue; | |
1339 pixel.alpha_ = alpha; | |
1340 | |
1341 BresenhamPixelWriter<Orthanc::PixelFormat_BGRA32> writer(image, pixel); | |
1342 writer.DrawSegment(x0, y0, x1, y1); | |
1343 break; | |
1344 } | |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1345 |
3227 | 1346 case Orthanc::PixelFormat_RGBA32: |
1347 { | |
1348 PixelTraits<Orthanc::PixelFormat_RGBA32>::PixelType pixel; | |
1349 pixel.red_ = red; | |
1350 pixel.green_ = green; | |
1351 pixel.blue_ = blue; | |
1352 pixel.alpha_ = alpha; | |
1353 | |
1354 BresenhamPixelWriter<Orthanc::PixelFormat_RGBA32> writer(image, pixel); | |
1355 writer.DrawSegment(x0, y0, x1, y1); | |
1356 break; | |
1357 } | |
2783
65699fcb4e99
PixelTraits<PixelFormat_RGBA32>
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2650
diff
changeset
|
1358 |
3227 | 1359 case Orthanc::PixelFormat_RGB24: |
1360 { | |
1361 PixelTraits<Orthanc::PixelFormat_RGB24>::PixelType pixel; | |
1362 pixel.red_ = red; | |
1363 pixel.green_ = green; | |
1364 pixel.blue_ = blue; | |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1365 |
3227 | 1366 BresenhamPixelWriter<Orthanc::PixelFormat_RGB24> writer(image, pixel); |
1367 writer.DrawSegment(x0, y0, x1, y1); | |
1368 break; | |
1369 } | |
1370 | |
1371 default: | |
1372 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1373 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1374 } |
3258 | 1375 |
1376 void ComputePolygonExtent(int32_t& left, int32_t& right, int32_t& top, int32_t& bottom, const std::vector<ImageProcessing::ImagePoint>& points) | |
1377 { | |
1378 left = std::numeric_limits<int32_t>::max(); | |
1379 right = std::numeric_limits<int32_t>::min(); | |
1380 top = std::numeric_limits<int32_t>::max(); | |
1381 bottom = std::numeric_limits<int32_t>::min(); | |
1382 | |
1383 for (size_t i = 0; i < points.size(); i++) | |
1384 { | |
1385 const ImageProcessing::ImagePoint& p = points[i]; | |
1386 left = std::min(p.GetX(), left); | |
1387 right = std::max(p.GetX(), right); | |
1388 bottom = std::max(p.GetY(), bottom); | |
1389 top = std::min(p.GetY(), top); | |
1390 } | |
1391 } | |
1392 | |
1393 template <PixelFormat TargetFormat> | |
1394 void FillPolygon_(ImageAccessor& image, | |
1395 const std::vector<ImageProcessing::ImagePoint>& points, | |
1396 int64_t value_) | |
1397 { | |
1398 typedef typename PixelTraits<TargetFormat>::PixelType TargetType; | |
1399 | |
1400 TargetType value = PixelTraits<TargetFormat>::IntegerToPixel(value_); | |
3323
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1401 int imageWidth = static_cast<int>(image.GetWidth()); |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1402 int imageHeight = static_cast<int>(image.GetHeight()); |
3258 | 1403 int32_t left; |
1404 int32_t right; | |
1405 int32_t top; | |
1406 int32_t bottom; | |
1407 | |
3323
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1408 // TODO: test clipping in UT (in Trello board) |
3258 | 1409 ComputePolygonExtent(left, right, top, bottom, points); |
1410 | |
3323
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1411 // clip the computed extent with the target image |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1412 // L and R |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1413 left = std::max(0, left); |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1414 left = std::min(imageWidth, left); |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1415 right = std::max(0, right); |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1416 right = std::min(imageWidth, right); |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1417 if (left > right) |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1418 std::swap(left, right); |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1419 |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1420 // T and B |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1421 top = std::max(0, top); |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1422 top = std::min(imageHeight, top); |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1423 bottom = std::max(0, bottom); |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1424 bottom = std::min(imageHeight, bottom); |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1425 if (top > bottom) |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1426 std::swap(top, bottom); |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1427 |
3258 | 1428 // from http://alienryderflex.com/polygon_fill/ |
1429 | |
1430 // convert all "corner" points to double only once | |
1431 std::vector<double> cpx; | |
1432 std::vector<double> cpy; | |
1433 size_t cpSize = points.size(); | |
1434 for (size_t i = 0; i < points.size(); i++) | |
1435 { | |
1436 cpx.push_back((double)points[i].GetX()); | |
1437 cpy.push_back((double)points[i].GetY()); | |
1438 } | |
1439 | |
1440 std::vector<int32_t> nodeX; | |
1441 nodeX.resize(cpSize); | |
1442 int nodes, pixelX, pixelY, i, j, swap ; | |
1443 | |
1444 // Loop through the rows of the image. | |
1445 for (pixelY = top; pixelY < bottom; pixelY++) | |
1446 { | |
1447 double y = (double)pixelY; | |
1448 // Build a list of nodes. | |
1449 nodes = 0; | |
1450 j = static_cast<int>(cpSize) - 1; | |
1451 | |
3259
6f9398eb902d
unit test Toolbox.SubstituteVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3258
diff
changeset
|
1452 for (i = 0; i < static_cast<int>(cpSize); i++) |
3258 | 1453 { |
1454 if ((cpy[i] < y && cpy[j] >= y) || (cpy[j] < y && cpy[i] >= y)) | |
1455 { | |
1456 nodeX[nodes++] = (int32_t)(cpx[i] + (y - cpy[i])/(cpy[j] - cpy[i]) * (cpx[j] - cpx[i])); | |
1457 } | |
1458 j=i; | |
1459 } | |
1460 | |
1461 // Sort the nodes, via a simple “Bubble” sort. | |
1462 i=0; | |
1463 while (i < nodes-1) | |
1464 { | |
1465 if (nodeX[i] > nodeX[i+1]) | |
1466 { | |
1467 swap = nodeX[i]; | |
1468 nodeX[i] = nodeX[i+1]; | |
1469 nodeX[i+1] = swap; | |
1470 if (i > 0) | |
1471 { | |
1472 i--; | |
1473 } | |
1474 } | |
1475 else | |
1476 { | |
1477 i++; | |
1478 } | |
1479 } | |
1480 | |
1481 TargetType* row = reinterpret_cast<TargetType*>(image.GetRow(pixelY)); | |
1482 // Fill the pixels between node pairs. | |
3323
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1483 for (i = 0; i < nodes; i += 2) |
3258 | 1484 { |
1485 if (nodeX[i] >= right) | |
1486 break; | |
1487 | |
3323
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1488 if (nodeX[i + 1] >= left) |
3258 | 1489 { |
3323
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1490 if (nodeX[i] < left) |
3258 | 1491 { |
1492 nodeX[i] = left; | |
1493 } | |
1494 | |
3323
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1495 if (nodeX[i + 1] > right) |
3258 | 1496 { |
3323
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1497 nodeX[i + 1] = right; |
3258 | 1498 } |
1499 | |
3323
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1500 for (pixelX = nodeX[i]; pixelX <= nodeX[i + 1]; pixelX++) |
3258 | 1501 { |
1502 *(row + pixelX) = value; | |
1503 } | |
1504 } | |
1505 } | |
1506 } | |
1507 } | |
1508 | |
1509 void ImageProcessing::FillPolygon(ImageAccessor& image, | |
1510 const std::vector<ImagePoint>& points, | |
1511 int64_t value) | |
1512 { | |
1513 switch (image.GetFormat()) | |
1514 { | |
1515 case Orthanc::PixelFormat_Grayscale8: | |
1516 { | |
1517 FillPolygon_<Orthanc::PixelFormat_Grayscale8>(image, points, value); | |
1518 break; | |
1519 } | |
1520 case Orthanc::PixelFormat_Grayscale16: | |
1521 { | |
1522 FillPolygon_<Orthanc::PixelFormat_Grayscale16>(image, points, value); | |
1523 break; | |
1524 } | |
1525 case Orthanc::PixelFormat_SignedGrayscale16: | |
1526 { | |
1527 FillPolygon_<Orthanc::PixelFormat_SignedGrayscale16>(image, points, value); | |
1528 break; | |
1529 } | |
1530 default: | |
1531 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
1532 } | |
1533 } | |
1534 | |
853 | 1535 } |