Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Toolbox/ImageGeometry.cpp @ 1950:6ffd65cfd2ee deep-learning
new branch: deep-learning
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 12 Aug 2022 15:48:35 +0200 |
parents | 7053b8a0aaec |
children | 07964689cb0b |
rev | line source |
---|---|
182 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
1871
7053b8a0aaec
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1870
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
7053b8a0aaec
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1870
diff
changeset
|
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
182 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
9 * modify it under the terms of the GNU Lesser General Public License |
182 | 10 * as published by the Free Software Foundation, either version 3 of |
11 * the License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
16 * Lesser General Public License for more details. |
1596
4fb8fdf03314
removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
17 * |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
18 * You should have received a copy of the GNU Lesser General Public |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
19 * License along with this program. If not, see |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
20 * <http://www.gnu.org/licenses/>. |
182 | 21 **/ |
22 | |
23 | |
24 #include "ImageGeometry.h" | |
25 | |
26 #include "Extent2D.h" | |
27 #include "SubpixelReader.h" | |
28 | |
1455
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
29 #include <Images/ImageProcessing.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
30 #include <Logging.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
31 #include <OrthancException.h> |
182 | 32 |
33 | |
34 namespace OrthancStone | |
35 { | |
36 static void AddTransformedPoint(Extent2D& extent, | |
37 const Matrix& a, | |
38 double x, | |
39 double y) | |
40 { | |
41 assert(a.size1() == 3 && | |
42 a.size2() == 3); | |
43 | |
44 Vector p = LinearAlgebra::Product(a, LinearAlgebra::CreateVector(x, y, 1)); | |
45 | |
46 if (!LinearAlgebra::IsCloseToZero(p[2])) | |
47 { | |
48 extent.AddPoint(p[0] / p[2], p[1] / p[2]); | |
49 } | |
50 } | |
51 | |
52 | |
190 | 53 bool GetProjectiveTransformExtent(unsigned int& x1, |
182 | 54 unsigned int& y1, |
55 unsigned int& x2, | |
56 unsigned int& y2, | |
57 const Matrix& a, | |
58 unsigned int sourceWidth, | |
59 unsigned int sourceHeight, | |
60 unsigned int targetWidth, | |
61 unsigned int targetHeight) | |
62 { | |
63 if (targetWidth == 0 || | |
64 targetHeight == 0) | |
65 { | |
66 return false; | |
67 } | |
68 | |
69 Extent2D extent; | |
70 AddTransformedPoint(extent, a, 0, 0); | |
71 AddTransformedPoint(extent, a, sourceWidth, 0); | |
72 AddTransformedPoint(extent, a, 0, sourceHeight); | |
73 AddTransformedPoint(extent, a, sourceWidth, sourceHeight); | |
74 | |
75 if (extent.IsEmpty()) | |
76 { | |
77 return false; | |
78 } | |
79 else | |
80 { | |
507 | 81 int tmp = static_cast<int>(std::floor(extent.GetX1())); |
182 | 82 if (tmp < 0) |
83 { | |
84 x1 = 0; | |
85 } | |
86 else | |
87 { | |
88 x1 = static_cast<unsigned int>(tmp); | |
89 } | |
90 | |
507 | 91 tmp = static_cast<int>(std::floor(extent.GetY1())); |
182 | 92 if (tmp < 0) |
93 { | |
94 y1 = 0; | |
95 } | |
96 else | |
97 { | |
98 y1 = static_cast<unsigned int>(tmp); | |
99 } | |
100 | |
507 | 101 tmp = static_cast<int>(std::ceil(extent.GetX2())); |
182 | 102 if (tmp < 0) |
103 { | |
104 return false; | |
105 } | |
106 else if (static_cast<unsigned int>(tmp) >= targetWidth) | |
107 { | |
108 x2 = targetWidth - 1; | |
109 } | |
110 else | |
111 { | |
112 x2 = static_cast<unsigned int>(tmp); | |
113 } | |
114 | |
507 | 115 tmp = static_cast<int>(std::ceil(extent.GetY2())); |
182 | 116 if (tmp < 0) |
117 { | |
118 return false; | |
119 } | |
120 else if (static_cast<unsigned int>(tmp) >= targetHeight) | |
121 { | |
122 y2 = targetHeight - 1; | |
123 } | |
124 else | |
125 { | |
126 y2 = static_cast<unsigned int>(tmp); | |
127 } | |
128 | |
129 return (x1 <= x2 && | |
130 y1 <= y2); | |
131 } | |
132 } | |
133 | |
134 | |
135 template <typename Reader, | |
136 bool HasOffsetX, | |
137 bool HasOffsetY> | |
138 static void ApplyAffineTransformToRow(typename Reader::PixelType* p, | |
139 Reader& reader, | |
140 unsigned int x1, | |
141 unsigned int x2, | |
142 float positionX, | |
143 float positionY, | |
144 float offsetX, | |
145 float offsetY) | |
146 { | |
147 typename Reader::PixelType value; | |
148 | |
149 for (unsigned int x = x1; x <= x2; x++, p++) | |
150 { | |
151 if (reader.GetValue(value, positionX, positionY)) | |
152 { | |
153 *p = value; | |
154 } | |
155 | |
156 if (HasOffsetX) | |
157 { | |
158 positionX += offsetX; | |
159 } | |
160 | |
161 if (HasOffsetY) | |
162 { | |
163 positionY += offsetY; | |
164 } | |
165 } | |
166 } | |
167 | |
168 | |
169 template <Orthanc::PixelFormat Format, | |
170 ImageInterpolation Interpolation> | |
171 static void ApplyAffineInternal(Orthanc::ImageAccessor& target, | |
172 const Orthanc::ImageAccessor& source, | |
340
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
173 const Matrix& a, |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
174 bool clear) |
182 | 175 { |
176 assert(target.GetFormat() == Format && | |
177 source.GetFormat() == Format); | |
178 | |
179 typedef SubpixelReader<Format, Interpolation> Reader; | |
180 typedef typename Reader::PixelType PixelType; | |
181 | |
340
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
182 if (clear) |
182 | 183 { |
340
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
184 if (Format == Orthanc::PixelFormat_RGB24) |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
185 { |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
186 Orthanc::ImageProcessing::Set(target, 0, 0, 0, 255); |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
187 } |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
188 else |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
189 { |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
190 Orthanc::ImageProcessing::Set(target, 0); |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
191 } |
182 | 192 } |
193 | |
194 Matrix inva; | |
195 if (!LinearAlgebra::InvertMatrixUnsafe(inva, a)) | |
196 { | |
197 // Singular matrix | |
198 return; | |
199 } | |
200 | |
201 Reader reader(source); | |
202 | |
203 unsigned int x1, y1, x2, y2; | |
204 | |
190 | 205 if (GetProjectiveTransformExtent(x1, y1, x2, y2, a, |
182 | 206 source.GetWidth(), source.GetHeight(), |
207 target.GetWidth(), target.GetHeight())) | |
208 { | |
209 const size_t targetPitch = target.GetPitch(); | |
191
46cb2eedc2e0
ShearWarpProjectiveTransform
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
190
diff
changeset
|
210 uint8_t *targetRow = reinterpret_cast<uint8_t*> |
46cb2eedc2e0
ShearWarpProjectiveTransform
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
190
diff
changeset
|
211 (reinterpret_cast<PixelType*>(target.GetRow(y1)) + x1); |
182 | 212 |
213 for (unsigned int y = y1; y <= y2; y++) | |
214 { | |
215 Vector start; | |
216 LinearAlgebra::AssignVector(start, static_cast<double>(x1) + 0.5, | |
217 static_cast<double>(y) + 0.5, 1); | |
218 start = boost::numeric::ublas::prod(inva, start); | |
219 assert(LinearAlgebra::IsNear(1.0, start(2))); | |
220 | |
221 Vector offset; | |
222 LinearAlgebra::AssignVector(offset, static_cast<double>(x1) + 1.5, | |
223 static_cast<double>(y) + 0.5, 1); | |
224 offset = boost::numeric::ublas::prod(inva, offset) - start; | |
225 assert(LinearAlgebra::IsNear(0.0, offset(2))); | |
226 | |
227 float startX = static_cast<float>(start[0]); | |
228 float startY = static_cast<float>(start[1]); | |
229 float offsetX = static_cast<float>(offset[0]); | |
230 float offsetY = static_cast<float>(offset[1]); | |
231 | |
232 PixelType* pixel = reinterpret_cast<PixelType*>(targetRow); | |
233 if (LinearAlgebra::IsCloseToZero(offsetX)) | |
234 { | |
235 ApplyAffineTransformToRow<Reader, false, true> | |
236 (pixel, reader, x1, x2, startX, startY, offsetX, offsetY); | |
237 } | |
238 else if (LinearAlgebra::IsCloseToZero(offsetY)) | |
239 { | |
240 ApplyAffineTransformToRow<Reader, true, false> | |
241 (pixel, reader, x1, x2, startX, startY, offsetX, offsetY); | |
242 } | |
243 else | |
244 { | |
245 ApplyAffineTransformToRow<Reader, true, true> | |
246 (pixel, reader, x1, x2, startX, startY, offsetX, offsetY); | |
247 } | |
248 | |
249 targetRow += targetPitch; | |
250 } | |
251 } | |
252 } | |
253 | |
254 | |
255 void ApplyAffineTransform(Orthanc::ImageAccessor& target, | |
256 const Orthanc::ImageAccessor& source, | |
257 double a11, | |
258 double a12, | |
259 double b1, | |
260 double a21, | |
261 double a22, | |
262 double b2, | |
340
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
263 ImageInterpolation interpolation, |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
264 bool clear) |
182 | 265 { |
266 if (source.GetFormat() != target.GetFormat()) | |
267 { | |
268 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
269 } | |
270 | |
271 if (interpolation != ImageInterpolation_Nearest && | |
272 interpolation != ImageInterpolation_Bilinear) | |
273 { | |
274 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
275 } | |
276 | |
277 Matrix a; | |
278 a.resize(3, 3); | |
279 a(0, 0) = a11; | |
280 a(0, 1) = a12; | |
281 a(0, 2) = b1; | |
282 a(1, 0) = a21; | |
283 a(1, 1) = a22; | |
284 a(1, 2) = b2; | |
285 a(2, 0) = 0; | |
286 a(2, 1) = 0; | |
287 a(2, 2) = 1; | |
288 | |
289 switch (source.GetFormat()) | |
290 { | |
291 case Orthanc::PixelFormat_Grayscale8: | |
292 switch (interpolation) | |
293 { | |
294 case ImageInterpolation_Nearest: | |
295 ApplyAffineInternal<Orthanc::PixelFormat_Grayscale8, | |
340
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
296 ImageInterpolation_Nearest>(target, source, a, clear); |
182 | 297 break; |
298 | |
299 case ImageInterpolation_Bilinear: | |
300 ApplyAffineInternal<Orthanc::PixelFormat_Grayscale8, | |
340
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
301 ImageInterpolation_Bilinear>(target, source, a, clear); |
182 | 302 break; |
303 | |
304 default: | |
305 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
306 } | |
307 break; | |
308 | |
309 case Orthanc::PixelFormat_Grayscale16: | |
310 switch (interpolation) | |
311 { | |
312 case ImageInterpolation_Nearest: | |
313 ApplyAffineInternal<Orthanc::PixelFormat_Grayscale16, | |
340
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
314 ImageInterpolation_Nearest>(target, source, a, clear); |
182 | 315 break; |
316 | |
317 case ImageInterpolation_Bilinear: | |
318 ApplyAffineInternal<Orthanc::PixelFormat_Grayscale16, | |
340
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
319 ImageInterpolation_Bilinear>(target, source, a, clear); |
182 | 320 break; |
321 | |
322 default: | |
323 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
324 } | |
325 break; | |
326 | |
327 case Orthanc::PixelFormat_SignedGrayscale16: | |
328 switch (interpolation) | |
329 { | |
330 case ImageInterpolation_Nearest: | |
331 ApplyAffineInternal<Orthanc::PixelFormat_SignedGrayscale16, | |
340
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
332 ImageInterpolation_Nearest>(target, source, a, clear); |
182 | 333 break; |
334 | |
335 case ImageInterpolation_Bilinear: | |
336 ApplyAffineInternal<Orthanc::PixelFormat_SignedGrayscale16, | |
340
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
337 ImageInterpolation_Bilinear>(target, source, a, clear); |
182 | 338 break; |
339 | |
340 default: | |
341 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
342 } | |
343 break; | |
344 | |
338 | 345 case Orthanc::PixelFormat_Float32: |
346 switch (interpolation) | |
347 { | |
348 case ImageInterpolation_Nearest: | |
349 ApplyAffineInternal<Orthanc::PixelFormat_Float32, | |
340
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
350 ImageInterpolation_Nearest>(target, source, a, clear); |
338 | 351 break; |
352 | |
353 case ImageInterpolation_Bilinear: | |
354 ApplyAffineInternal<Orthanc::PixelFormat_Float32, | |
340
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
355 ImageInterpolation_Bilinear>(target, source, a, clear); |
338 | 356 break; |
357 | |
358 default: | |
359 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
360 } | |
361 break; | |
362 | |
182 | 363 case Orthanc::PixelFormat_RGB24: |
364 switch (interpolation) | |
365 { | |
366 case ImageInterpolation_Nearest: | |
367 ApplyAffineInternal<Orthanc::PixelFormat_RGB24, | |
340
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
368 ImageInterpolation_Nearest>(target, source, a, clear); |
182 | 369 break; |
370 | |
371 default: | |
372 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
373 } | |
374 break; | |
375 | |
376 default: | |
377 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
378 } | |
379 } | |
380 | |
381 | |
382 template <Orthanc::PixelFormat Format, | |
383 ImageInterpolation Interpolation> | |
190 | 384 static void ApplyProjectiveInternal(Orthanc::ImageAccessor& target, |
385 const Orthanc::ImageAccessor& source, | |
386 const Matrix& a, | |
387 const Matrix& inva) | |
182 | 388 { |
389 assert(target.GetFormat() == Format && | |
390 source.GetFormat() == Format); | |
391 | |
392 typedef SubpixelReader<Format, Interpolation> Reader; | |
393 typedef typename Reader::PixelType PixelType; | |
394 | |
395 Reader reader(source); | |
396 unsigned int x1, y1, x2, y2; | |
397 | |
507 | 398 const float floatWidth = static_cast<float>(source.GetWidth()); |
399 const float floatHeight = static_cast<float>(source.GetHeight()); | |
182 | 400 |
190 | 401 if (GetProjectiveTransformExtent(x1, y1, x2, y2, a, |
182 | 402 source.GetWidth(), source.GetHeight(), |
403 target.GetWidth(), target.GetHeight())) | |
404 { | |
405 const size_t targetPitch = target.GetPitch(); | |
191
46cb2eedc2e0
ShearWarpProjectiveTransform
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
190
diff
changeset
|
406 uint8_t *targetRow = reinterpret_cast<uint8_t*> |
46cb2eedc2e0
ShearWarpProjectiveTransform
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
190
diff
changeset
|
407 (reinterpret_cast<PixelType*>(target.GetRow(y1)) + x1); |
182 | 408 |
409 for (unsigned int y = y1; y <= y2; y++) | |
410 { | |
411 PixelType *p = reinterpret_cast<PixelType*>(targetRow); | |
412 | |
413 for (unsigned int x = x1; x <= x2; x++) | |
414 { | |
415 Vector v; | |
416 LinearAlgebra::AssignVector(v, static_cast<double>(x) + 0.5, | |
417 static_cast<double>(y) + 0.5, 1); | |
418 | |
419 Vector vv = LinearAlgebra::Product(inva, v); | |
420 | |
421 assert(!LinearAlgebra::IsCloseToZero(vv[2])); | |
422 const double w = 1.0 / vv[2]; | |
423 const float sourceX = static_cast<float>(vv[0] * w); | |
424 const float sourceY = static_cast<float>(vv[1] * w); | |
425 | |
426 // Make sure no integer overflow will occur after truncation | |
427 // (the static_cast<unsigned int> could otherwise throw an | |
190 | 428 // exception in WebAssembly if strong projective effects) |
182 | 429 if (sourceX < floatWidth && |
430 sourceY < floatHeight) | |
431 { | |
432 reader.GetValue(*p, sourceX, sourceY); | |
433 } | |
434 | |
435 p++; | |
436 } | |
437 | |
438 targetRow += targetPitch; | |
439 } | |
440 } | |
441 } | |
442 | |
443 | |
190 | 444 void ApplyProjectiveTransform(Orthanc::ImageAccessor& target, |
445 const Orthanc::ImageAccessor& source, | |
446 const Matrix& a, | |
340
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
447 ImageInterpolation interpolation, |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
448 bool clear) |
182 | 449 { |
450 if (source.GetFormat() != target.GetFormat()) | |
451 { | |
452 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
453 } | |
454 | |
455 if (a.size1() != 3 || | |
456 a.size2() != 3) | |
457 { | |
458 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageSize); | |
459 } | |
460 | |
461 if (interpolation != ImageInterpolation_Nearest && | |
462 interpolation != ImageInterpolation_Bilinear) | |
463 { | |
464 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
465 } | |
466 | |
467 // Check whether we are dealing with an affine transform | |
468 if (LinearAlgebra::IsCloseToZero(a(2, 0)) && | |
469 LinearAlgebra::IsCloseToZero(a(2, 1))) | |
470 { | |
471 double w = a(2, 2); | |
472 if (LinearAlgebra::IsCloseToZero(w)) | |
473 { | |
190 | 474 LOG(ERROR) << "Singular projective matrix"; |
182 | 475 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); |
476 } | |
477 else | |
478 { | |
479 ApplyAffineTransform(target, source, | |
480 a(0, 0) / w, a(0, 1) / w, a(0, 2) / w, | |
481 a(1, 0) / w, a(1, 1) / w, a(1, 2) / w, | |
340
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
482 interpolation, clear); |
182 | 483 return; |
484 } | |
485 } | |
486 | |
340
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
487 if (clear) |
182 | 488 { |
340
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
489 if (target.GetFormat() == Orthanc::PixelFormat_RGB24) |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
490 { |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
491 Orthanc::ImageProcessing::Set(target, 0, 0, 0, 255); |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
492 } |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
493 else |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
494 { |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
495 Orthanc::ImageProcessing::Set(target, 0); |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
338
diff
changeset
|
496 } |
182 | 497 } |
498 | |
499 Matrix inva; | |
500 if (!LinearAlgebra::InvertMatrixUnsafe(inva, a)) | |
501 { | |
502 return; | |
503 } | |
504 | |
505 switch (source.GetFormat()) | |
506 { | |
507 case Orthanc::PixelFormat_Grayscale8: | |
508 switch (interpolation) | |
509 { | |
510 case ImageInterpolation_Nearest: | |
190 | 511 ApplyProjectiveInternal<Orthanc::PixelFormat_Grayscale8, |
512 ImageInterpolation_Nearest>(target, source, a, inva); | |
182 | 513 break; |
514 | |
515 case ImageInterpolation_Bilinear: | |
190 | 516 ApplyProjectiveInternal<Orthanc::PixelFormat_Grayscale8, |
517 ImageInterpolation_Bilinear>(target, source, a, inva); | |
182 | 518 break; |
519 | |
520 default: | |
521 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
522 } | |
523 break; | |
524 | |
525 case Orthanc::PixelFormat_Grayscale16: | |
526 switch (interpolation) | |
527 { | |
528 case ImageInterpolation_Nearest: | |
190 | 529 ApplyProjectiveInternal<Orthanc::PixelFormat_Grayscale16, |
530 ImageInterpolation_Nearest>(target, source, a, inva); | |
182 | 531 break; |
532 | |
533 case ImageInterpolation_Bilinear: | |
190 | 534 ApplyProjectiveInternal<Orthanc::PixelFormat_Grayscale16, |
535 ImageInterpolation_Bilinear>(target, source, a, inva); | |
182 | 536 break; |
537 | |
538 default: | |
539 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
540 } | |
541 break; | |
542 | |
543 case Orthanc::PixelFormat_SignedGrayscale16: | |
544 switch (interpolation) | |
545 { | |
546 case ImageInterpolation_Nearest: | |
190 | 547 ApplyProjectiveInternal<Orthanc::PixelFormat_SignedGrayscale16, |
548 ImageInterpolation_Nearest>(target, source, a, inva); | |
182 | 549 break; |
550 | |
551 case ImageInterpolation_Bilinear: | |
190 | 552 ApplyProjectiveInternal<Orthanc::PixelFormat_SignedGrayscale16, |
553 ImageInterpolation_Bilinear>(target, source, a, inva); | |
182 | 554 break; |
555 | |
556 default: | |
557 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
558 } | |
559 break; | |
560 | |
338 | 561 case Orthanc::PixelFormat_Float32: |
562 switch (interpolation) | |
563 { | |
564 case ImageInterpolation_Nearest: | |
565 ApplyProjectiveInternal<Orthanc::PixelFormat_Float32, | |
566 ImageInterpolation_Nearest>(target, source, a, inva); | |
567 break; | |
568 | |
569 case ImageInterpolation_Bilinear: | |
570 ApplyProjectiveInternal<Orthanc::PixelFormat_Float32, | |
571 ImageInterpolation_Bilinear>(target, source, a, inva); | |
572 break; | |
573 | |
574 default: | |
575 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
576 } | |
577 break; | |
578 | |
182 | 579 case Orthanc::PixelFormat_RGB24: |
580 switch (interpolation) | |
581 { | |
582 case ImageInterpolation_Nearest: | |
190 | 583 ApplyProjectiveInternal<Orthanc::PixelFormat_RGB24, |
584 ImageInterpolation_Nearest>(target, source, a, inva); | |
182 | 585 break; |
586 | |
587 default: | |
588 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
589 } | |
590 break; | |
591 | |
592 default: | |
593 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
594 } | |
595 } | |
596 } |