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