Mercurial > hg > orthanc-stone
annotate Framework/Toolbox/ImageGeometry.cpp @ 338:b3b3fa0e3689 am-2
BitmapStack
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 19 Oct 2018 12:50:38 +0200 |
parents | 3e42fc27eb91 |
children | f5d5814a41a0 |
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 | |
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
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 | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "ImageGeometry.h" | |
23 | |
24 #include "Extent2D.h" | |
25 #include "SubpixelReader.h" | |
26 | |
212
5412adf19980
resort to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
27 #include <Core/Images/ImageProcessing.h> |
5412adf19980
resort to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
28 #include <Core/Logging.h> |
335 | 29 #include <Core/OrthancException.h> |
182 | 30 |
31 | |
32 namespace OrthancStone | |
33 { | |
34 static void AddTransformedPoint(Extent2D& extent, | |
35 const Matrix& a, | |
36 double x, | |
37 double y) | |
38 { | |
39 assert(a.size1() == 3 && | |
40 a.size2() == 3); | |
41 | |
42 Vector p = LinearAlgebra::Product(a, LinearAlgebra::CreateVector(x, y, 1)); | |
43 | |
44 if (!LinearAlgebra::IsCloseToZero(p[2])) | |
45 { | |
46 extent.AddPoint(p[0] / p[2], p[1] / p[2]); | |
47 } | |
48 } | |
49 | |
50 | |
190 | 51 bool GetProjectiveTransformExtent(unsigned int& x1, |
182 | 52 unsigned int& y1, |
53 unsigned int& x2, | |
54 unsigned int& y2, | |
55 const Matrix& a, | |
56 unsigned int sourceWidth, | |
57 unsigned int sourceHeight, | |
58 unsigned int targetWidth, | |
59 unsigned int targetHeight) | |
60 { | |
61 if (targetWidth == 0 || | |
62 targetHeight == 0) | |
63 { | |
64 return false; | |
65 } | |
66 | |
67 Extent2D extent; | |
68 AddTransformedPoint(extent, a, 0, 0); | |
69 AddTransformedPoint(extent, a, sourceWidth, 0); | |
70 AddTransformedPoint(extent, a, 0, sourceHeight); | |
71 AddTransformedPoint(extent, a, sourceWidth, sourceHeight); | |
72 | |
73 if (extent.IsEmpty()) | |
74 { | |
75 return false; | |
76 } | |
77 else | |
78 { | |
79 int tmp; | |
80 | |
81 tmp = std::floor(extent.GetX1()); | |
82 if (tmp < 0) | |
83 { | |
84 x1 = 0; | |
85 } | |
86 else | |
87 { | |
88 x1 = static_cast<unsigned int>(tmp); | |
89 } | |
90 | |
91 tmp = std::floor(extent.GetY1()); | |
92 if (tmp < 0) | |
93 { | |
94 y1 = 0; | |
95 } | |
96 else | |
97 { | |
98 y1 = static_cast<unsigned int>(tmp); | |
99 } | |
100 | |
101 tmp = std::ceil(extent.GetX2()); | |
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 | |
115 tmp = std::ceil(extent.GetY2()); | |
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 else | |
156 { | |
157 Reader::Traits::SetZero(*p); | |
158 } | |
159 | |
160 if (HasOffsetX) | |
161 { | |
162 positionX += offsetX; | |
163 } | |
164 | |
165 if (HasOffsetY) | |
166 { | |
167 positionY += offsetY; | |
168 } | |
169 } | |
170 } | |
171 | |
172 | |
173 template <Orthanc::PixelFormat Format, | |
174 ImageInterpolation Interpolation> | |
175 static void ApplyAffineInternal(Orthanc::ImageAccessor& target, | |
176 const Orthanc::ImageAccessor& source, | |
177 const Matrix& a) | |
178 { | |
179 assert(target.GetFormat() == Format && | |
180 source.GetFormat() == Format); | |
181 | |
182 typedef SubpixelReader<Format, Interpolation> Reader; | |
183 typedef typename Reader::PixelType PixelType; | |
184 | |
185 if (Format == Orthanc::PixelFormat_RGB24) | |
186 { | |
187 Orthanc::ImageProcessing::Set(target, 0, 0, 0, 255); | |
188 } | |
189 else | |
190 { | |
191 Orthanc::ImageProcessing::Set(target, 0); | |
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, | |
263 ImageInterpolation interpolation) | |
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, | |
295 ImageInterpolation_Nearest>(target, source, a); | |
296 break; | |
297 | |
298 case ImageInterpolation_Bilinear: | |
299 ApplyAffineInternal<Orthanc::PixelFormat_Grayscale8, | |
300 ImageInterpolation_Bilinear>(target, source, a); | |
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, | |
313 ImageInterpolation_Nearest>(target, source, a); | |
314 break; | |
315 | |
316 case ImageInterpolation_Bilinear: | |
317 ApplyAffineInternal<Orthanc::PixelFormat_Grayscale16, | |
318 ImageInterpolation_Bilinear>(target, source, a); | |
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, | |
331 ImageInterpolation_Nearest>(target, source, a); | |
332 break; | |
333 | |
334 case ImageInterpolation_Bilinear: | |
335 ApplyAffineInternal<Orthanc::PixelFormat_SignedGrayscale16, | |
336 ImageInterpolation_Bilinear>(target, source, a); | |
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, | |
349 ImageInterpolation_Nearest>(target, source, a); | |
350 break; | |
351 | |
352 case ImageInterpolation_Bilinear: | |
353 ApplyAffineInternal<Orthanc::PixelFormat_Float32, | |
354 ImageInterpolation_Bilinear>(target, source, a); | |
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, | |
367 ImageInterpolation_Nearest>(target, source, a); | |
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 | |
397 const float floatWidth = source.GetWidth(); | |
398 const float floatHeight = source.GetHeight(); | |
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 else | |
434 { | |
435 Reader::Traits::SetZero(*p); | |
436 } | |
437 | |
438 p++; | |
439 } | |
440 | |
441 targetRow += targetPitch; | |
442 } | |
443 } | |
444 } | |
445 | |
446 | |
190 | 447 void ApplyProjectiveTransform(Orthanc::ImageAccessor& target, |
448 const Orthanc::ImageAccessor& source, | |
449 const Matrix& a, | |
450 ImageInterpolation interpolation) | |
182 | 451 { |
452 if (source.GetFormat() != target.GetFormat()) | |
453 { | |
454 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
455 } | |
456 | |
457 if (a.size1() != 3 || | |
458 a.size2() != 3) | |
459 { | |
460 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageSize); | |
461 } | |
462 | |
463 if (interpolation != ImageInterpolation_Nearest && | |
464 interpolation != ImageInterpolation_Bilinear) | |
465 { | |
466 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
467 } | |
468 | |
469 // Check whether we are dealing with an affine transform | |
470 if (LinearAlgebra::IsCloseToZero(a(2, 0)) && | |
471 LinearAlgebra::IsCloseToZero(a(2, 1))) | |
472 { | |
473 double w = a(2, 2); | |
474 if (LinearAlgebra::IsCloseToZero(w)) | |
475 { | |
190 | 476 LOG(ERROR) << "Singular projective matrix"; |
182 | 477 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); |
478 } | |
479 else | |
480 { | |
481 ApplyAffineTransform(target, source, | |
482 a(0, 0) / w, a(0, 1) / w, a(0, 2) / w, | |
483 a(1, 0) / w, a(1, 1) / w, a(1, 2) / w, | |
484 interpolation); | |
485 return; | |
486 } | |
487 } | |
488 | |
489 if (target.GetFormat() == Orthanc::PixelFormat_RGB24) | |
490 { | |
491 Orthanc::ImageProcessing::Set(target, 0, 0, 0, 255); | |
492 } | |
493 else | |
494 { | |
495 Orthanc::ImageProcessing::Set(target, 0); | |
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 } |