Mercurial > hg > orthanc-stone
annotate Framework/Toolbox/ImageGeometry.cpp @ 212:5412adf19980
resort to OrthancFramework
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 01 Jun 2018 17:48:47 +0200 |
parents | e9c7a78a3e77 |
children | 3e42fc27eb91 |
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> |
182 | 29 |
30 | |
31 namespace OrthancStone | |
32 { | |
33 static void AddTransformedPoint(Extent2D& extent, | |
34 const Matrix& a, | |
35 double x, | |
36 double y) | |
37 { | |
38 assert(a.size1() == 3 && | |
39 a.size2() == 3); | |
40 | |
41 Vector p = LinearAlgebra::Product(a, LinearAlgebra::CreateVector(x, y, 1)); | |
42 | |
43 if (!LinearAlgebra::IsCloseToZero(p[2])) | |
44 { | |
45 extent.AddPoint(p[0] / p[2], p[1] / p[2]); | |
46 } | |
47 } | |
48 | |
49 | |
190 | 50 bool GetProjectiveTransformExtent(unsigned int& x1, |
182 | 51 unsigned int& y1, |
52 unsigned int& x2, | |
53 unsigned int& y2, | |
54 const Matrix& a, | |
55 unsigned int sourceWidth, | |
56 unsigned int sourceHeight, | |
57 unsigned int targetWidth, | |
58 unsigned int targetHeight) | |
59 { | |
60 if (targetWidth == 0 || | |
61 targetHeight == 0) | |
62 { | |
63 return false; | |
64 } | |
65 | |
66 Extent2D extent; | |
67 AddTransformedPoint(extent, a, 0, 0); | |
68 AddTransformedPoint(extent, a, sourceWidth, 0); | |
69 AddTransformedPoint(extent, a, 0, sourceHeight); | |
70 AddTransformedPoint(extent, a, sourceWidth, sourceHeight); | |
71 | |
72 if (extent.IsEmpty()) | |
73 { | |
74 return false; | |
75 } | |
76 else | |
77 { | |
78 int tmp; | |
79 | |
80 tmp = std::floor(extent.GetX1()); | |
81 if (tmp < 0) | |
82 { | |
83 x1 = 0; | |
84 } | |
85 else | |
86 { | |
87 x1 = static_cast<unsigned int>(tmp); | |
88 } | |
89 | |
90 tmp = std::floor(extent.GetY1()); | |
91 if (tmp < 0) | |
92 { | |
93 y1 = 0; | |
94 } | |
95 else | |
96 { | |
97 y1 = static_cast<unsigned int>(tmp); | |
98 } | |
99 | |
100 tmp = std::ceil(extent.GetX2()); | |
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 | |
114 tmp = std::ceil(extent.GetY2()); | |
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 else | |
155 { | |
156 Reader::Traits::SetZero(*p); | |
157 } | |
158 | |
159 if (HasOffsetX) | |
160 { | |
161 positionX += offsetX; | |
162 } | |
163 | |
164 if (HasOffsetY) | |
165 { | |
166 positionY += offsetY; | |
167 } | |
168 } | |
169 } | |
170 | |
171 | |
172 template <Orthanc::PixelFormat Format, | |
173 ImageInterpolation Interpolation> | |
174 static void ApplyAffineInternal(Orthanc::ImageAccessor& target, | |
175 const Orthanc::ImageAccessor& source, | |
176 const Matrix& a) | |
177 { | |
178 assert(target.GetFormat() == Format && | |
179 source.GetFormat() == Format); | |
180 | |
181 typedef SubpixelReader<Format, Interpolation> Reader; | |
182 typedef typename Reader::PixelType PixelType; | |
183 | |
184 if (Format == Orthanc::PixelFormat_RGB24) | |
185 { | |
186 Orthanc::ImageProcessing::Set(target, 0, 0, 0, 255); | |
187 } | |
188 else | |
189 { | |
190 Orthanc::ImageProcessing::Set(target, 0); | |
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, | |
262 ImageInterpolation interpolation) | |
263 { | |
264 if (source.GetFormat() != target.GetFormat()) | |
265 { | |
266 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
267 } | |
268 | |
269 if (interpolation != ImageInterpolation_Nearest && | |
270 interpolation != ImageInterpolation_Bilinear) | |
271 { | |
272 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
273 } | |
274 | |
275 Matrix a; | |
276 a.resize(3, 3); | |
277 a(0, 0) = a11; | |
278 a(0, 1) = a12; | |
279 a(0, 2) = b1; | |
280 a(1, 0) = a21; | |
281 a(1, 1) = a22; | |
282 a(1, 2) = b2; | |
283 a(2, 0) = 0; | |
284 a(2, 1) = 0; | |
285 a(2, 2) = 1; | |
286 | |
287 switch (source.GetFormat()) | |
288 { | |
289 case Orthanc::PixelFormat_Grayscale8: | |
290 switch (interpolation) | |
291 { | |
292 case ImageInterpolation_Nearest: | |
293 ApplyAffineInternal<Orthanc::PixelFormat_Grayscale8, | |
294 ImageInterpolation_Nearest>(target, source, a); | |
295 break; | |
296 | |
297 case ImageInterpolation_Bilinear: | |
298 ApplyAffineInternal<Orthanc::PixelFormat_Grayscale8, | |
299 ImageInterpolation_Bilinear>(target, source, a); | |
300 break; | |
301 | |
302 default: | |
303 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
304 } | |
305 break; | |
306 | |
307 case Orthanc::PixelFormat_Grayscale16: | |
308 switch (interpolation) | |
309 { | |
310 case ImageInterpolation_Nearest: | |
311 ApplyAffineInternal<Orthanc::PixelFormat_Grayscale16, | |
312 ImageInterpolation_Nearest>(target, source, a); | |
313 break; | |
314 | |
315 case ImageInterpolation_Bilinear: | |
316 ApplyAffineInternal<Orthanc::PixelFormat_Grayscale16, | |
317 ImageInterpolation_Bilinear>(target, source, a); | |
318 break; | |
319 | |
320 default: | |
321 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
322 } | |
323 break; | |
324 | |
325 case Orthanc::PixelFormat_SignedGrayscale16: | |
326 switch (interpolation) | |
327 { | |
328 case ImageInterpolation_Nearest: | |
329 ApplyAffineInternal<Orthanc::PixelFormat_SignedGrayscale16, | |
330 ImageInterpolation_Nearest>(target, source, a); | |
331 break; | |
332 | |
333 case ImageInterpolation_Bilinear: | |
334 ApplyAffineInternal<Orthanc::PixelFormat_SignedGrayscale16, | |
335 ImageInterpolation_Bilinear>(target, source, a); | |
336 break; | |
337 | |
338 default: | |
339 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
340 } | |
341 break; | |
342 | |
343 case Orthanc::PixelFormat_RGB24: | |
344 switch (interpolation) | |
345 { | |
346 case ImageInterpolation_Nearest: | |
347 ApplyAffineInternal<Orthanc::PixelFormat_RGB24, | |
348 ImageInterpolation_Nearest>(target, source, a); | |
349 break; | |
350 | |
351 default: | |
352 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
353 } | |
354 break; | |
355 | |
356 default: | |
357 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
358 } | |
359 } | |
360 | |
361 | |
362 template <Orthanc::PixelFormat Format, | |
363 ImageInterpolation Interpolation> | |
190 | 364 static void ApplyProjectiveInternal(Orthanc::ImageAccessor& target, |
365 const Orthanc::ImageAccessor& source, | |
366 const Matrix& a, | |
367 const Matrix& inva) | |
182 | 368 { |
369 assert(target.GetFormat() == Format && | |
370 source.GetFormat() == Format); | |
371 | |
372 typedef SubpixelReader<Format, Interpolation> Reader; | |
373 typedef typename Reader::PixelType PixelType; | |
374 | |
375 Reader reader(source); | |
376 unsigned int x1, y1, x2, y2; | |
377 | |
378 const float floatWidth = source.GetWidth(); | |
379 const float floatHeight = source.GetHeight(); | |
380 | |
190 | 381 if (GetProjectiveTransformExtent(x1, y1, x2, y2, a, |
182 | 382 source.GetWidth(), source.GetHeight(), |
383 target.GetWidth(), target.GetHeight())) | |
384 { | |
385 const size_t targetPitch = target.GetPitch(); | |
191
46cb2eedc2e0
ShearWarpProjectiveTransform
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
190
diff
changeset
|
386 uint8_t *targetRow = reinterpret_cast<uint8_t*> |
46cb2eedc2e0
ShearWarpProjectiveTransform
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
190
diff
changeset
|
387 (reinterpret_cast<PixelType*>(target.GetRow(y1)) + x1); |
182 | 388 |
389 for (unsigned int y = y1; y <= y2; y++) | |
390 { | |
391 PixelType *p = reinterpret_cast<PixelType*>(targetRow); | |
392 | |
393 for (unsigned int x = x1; x <= x2; x++) | |
394 { | |
395 Vector v; | |
396 LinearAlgebra::AssignVector(v, static_cast<double>(x) + 0.5, | |
397 static_cast<double>(y) + 0.5, 1); | |
398 | |
399 Vector vv = LinearAlgebra::Product(inva, v); | |
400 | |
401 assert(!LinearAlgebra::IsCloseToZero(vv[2])); | |
402 const double w = 1.0 / vv[2]; | |
403 const float sourceX = static_cast<float>(vv[0] * w); | |
404 const float sourceY = static_cast<float>(vv[1] * w); | |
405 | |
406 // Make sure no integer overflow will occur after truncation | |
407 // (the static_cast<unsigned int> could otherwise throw an | |
190 | 408 // exception in WebAssembly if strong projective effects) |
182 | 409 if (sourceX < floatWidth && |
410 sourceY < floatHeight) | |
411 { | |
412 reader.GetValue(*p, sourceX, sourceY); | |
413 } | |
414 else | |
415 { | |
416 Reader::Traits::SetZero(*p); | |
417 } | |
418 | |
419 p++; | |
420 } | |
421 | |
422 targetRow += targetPitch; | |
423 } | |
424 } | |
425 } | |
426 | |
427 | |
190 | 428 void ApplyProjectiveTransform(Orthanc::ImageAccessor& target, |
429 const Orthanc::ImageAccessor& source, | |
430 const Matrix& a, | |
431 ImageInterpolation interpolation) | |
182 | 432 { |
433 if (source.GetFormat() != target.GetFormat()) | |
434 { | |
435 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
436 } | |
437 | |
438 if (a.size1() != 3 || | |
439 a.size2() != 3) | |
440 { | |
441 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageSize); | |
442 } | |
443 | |
444 if (interpolation != ImageInterpolation_Nearest && | |
445 interpolation != ImageInterpolation_Bilinear) | |
446 { | |
447 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
448 } | |
449 | |
450 // Check whether we are dealing with an affine transform | |
451 if (LinearAlgebra::IsCloseToZero(a(2, 0)) && | |
452 LinearAlgebra::IsCloseToZero(a(2, 1))) | |
453 { | |
454 double w = a(2, 2); | |
455 if (LinearAlgebra::IsCloseToZero(w)) | |
456 { | |
190 | 457 LOG(ERROR) << "Singular projective matrix"; |
182 | 458 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); |
459 } | |
460 else | |
461 { | |
462 ApplyAffineTransform(target, source, | |
463 a(0, 0) / w, a(0, 1) / w, a(0, 2) / w, | |
464 a(1, 0) / w, a(1, 1) / w, a(1, 2) / w, | |
465 interpolation); | |
466 return; | |
467 } | |
468 } | |
469 | |
470 if (target.GetFormat() == Orthanc::PixelFormat_RGB24) | |
471 { | |
472 Orthanc::ImageProcessing::Set(target, 0, 0, 0, 255); | |
473 } | |
474 else | |
475 { | |
476 Orthanc::ImageProcessing::Set(target, 0); | |
477 } | |
478 | |
479 Matrix inva; | |
480 if (!LinearAlgebra::InvertMatrixUnsafe(inva, a)) | |
481 { | |
482 return; | |
483 } | |
484 | |
485 switch (source.GetFormat()) | |
486 { | |
487 case Orthanc::PixelFormat_Grayscale8: | |
488 switch (interpolation) | |
489 { | |
490 case ImageInterpolation_Nearest: | |
190 | 491 ApplyProjectiveInternal<Orthanc::PixelFormat_Grayscale8, |
492 ImageInterpolation_Nearest>(target, source, a, inva); | |
182 | 493 break; |
494 | |
495 case ImageInterpolation_Bilinear: | |
190 | 496 ApplyProjectiveInternal<Orthanc::PixelFormat_Grayscale8, |
497 ImageInterpolation_Bilinear>(target, source, a, inva); | |
182 | 498 break; |
499 | |
500 default: | |
501 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
502 } | |
503 break; | |
504 | |
505 case Orthanc::PixelFormat_Grayscale16: | |
506 switch (interpolation) | |
507 { | |
508 case ImageInterpolation_Nearest: | |
190 | 509 ApplyProjectiveInternal<Orthanc::PixelFormat_Grayscale16, |
510 ImageInterpolation_Nearest>(target, source, a, inva); | |
182 | 511 break; |
512 | |
513 case ImageInterpolation_Bilinear: | |
190 | 514 ApplyProjectiveInternal<Orthanc::PixelFormat_Grayscale16, |
515 ImageInterpolation_Bilinear>(target, source, a, inva); | |
182 | 516 break; |
517 | |
518 default: | |
519 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
520 } | |
521 break; | |
522 | |
523 case Orthanc::PixelFormat_SignedGrayscale16: | |
524 switch (interpolation) | |
525 { | |
526 case ImageInterpolation_Nearest: | |
190 | 527 ApplyProjectiveInternal<Orthanc::PixelFormat_SignedGrayscale16, |
528 ImageInterpolation_Nearest>(target, source, a, inva); | |
182 | 529 break; |
530 | |
531 case ImageInterpolation_Bilinear: | |
190 | 532 ApplyProjectiveInternal<Orthanc::PixelFormat_SignedGrayscale16, |
533 ImageInterpolation_Bilinear>(target, source, a, inva); | |
182 | 534 break; |
535 | |
536 default: | |
537 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
538 } | |
539 break; | |
540 | |
541 case Orthanc::PixelFormat_RGB24: | |
542 switch (interpolation) | |
543 { | |
544 case ImageInterpolation_Nearest: | |
190 | 545 ApplyProjectiveInternal<Orthanc::PixelFormat_RGB24, |
546 ImageInterpolation_Nearest>(target, source, a, inva); | |
182 | 547 break; |
548 | |
549 default: | |
550 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
551 } | |
552 break; | |
553 | |
554 default: | |
555 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
556 } | |
557 } | |
558 } |