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