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