OLD | NEW |
| (Empty) |
1 // Copyright 2014 PDFium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
6 // Original code is licensed as follows: | |
7 /* | |
8 * Copyright 2007 ZXing authors | |
9 * | |
10 * Licensed under the Apache License, Version 2.0 (the "License"); | |
11 * you may not use this file except in compliance with the License. | |
12 * You may obtain a copy of the License at | |
13 * | |
14 * http://www.apache.org/licenses/LICENSE-2.0 | |
15 * | |
16 * Unless required by applicable law or agreed to in writing, software | |
17 * distributed under the License is distributed on an "AS IS" BASIS, | |
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
19 * See the License for the specific language governing permissions and | |
20 * limitations under the License. | |
21 */ | |
22 | |
23 #include "xfa/src/fxbarcode/common/BC_CommonPerspectiveTransform.h" | |
24 | |
25 #include <memory> | |
26 | |
27 #include "core/include/fxcrt/fx_basic.h" | |
28 #include "xfa/src/fxbarcode/utils.h" | |
29 | |
30 CBC_CommonPerspectiveTransform::CBC_CommonPerspectiveTransform(FX_FLOAT a11, | |
31 FX_FLOAT a21, | |
32 FX_FLOAT a31, | |
33 FX_FLOAT a12, | |
34 FX_FLOAT a22, | |
35 FX_FLOAT a32, | |
36 FX_FLOAT a13, | |
37 FX_FLOAT a23, | |
38 FX_FLOAT a33) | |
39 : m_a11(a11), | |
40 m_a12(a12), | |
41 m_a13(a13), | |
42 m_a21(a21), | |
43 m_a22(a22), | |
44 m_a23(a23), | |
45 m_a31(a31), | |
46 m_a32(a32), | |
47 m_a33(a33) { | |
48 } | |
49 CBC_CommonPerspectiveTransform::~CBC_CommonPerspectiveTransform() {} | |
50 CBC_CommonPerspectiveTransform* | |
51 CBC_CommonPerspectiveTransform::QuadrilateralToQuadrilateral(FX_FLOAT x0, | |
52 FX_FLOAT y0, | |
53 FX_FLOAT x1, | |
54 FX_FLOAT y1, | |
55 FX_FLOAT x2, | |
56 FX_FLOAT y2, | |
57 FX_FLOAT x3, | |
58 FX_FLOAT y3, | |
59 FX_FLOAT x0p, | |
60 FX_FLOAT y0p, | |
61 FX_FLOAT x1p, | |
62 FX_FLOAT y1p, | |
63 FX_FLOAT x2p, | |
64 FX_FLOAT y2p, | |
65 FX_FLOAT x3p, | |
66 FX_FLOAT y3p) { | |
67 std::unique_ptr<CBC_CommonPerspectiveTransform> qToS( | |
68 QuadrilateralToSquare(x0, y0, x1, y1, x2, y2, x3, y3)); | |
69 std::unique_ptr<CBC_CommonPerspectiveTransform> sToQ( | |
70 SquareToQuadrilateral(x0p, y0p, x1p, y1p, x2p, y2p, x3p, y3p)); | |
71 return sToQ->Times(*(qToS.get())); | |
72 } | |
73 void CBC_CommonPerspectiveTransform::TransformPoints(CFX_FloatArray* points) { | |
74 int32_t max = points->GetSize(); | |
75 FX_FLOAT a11 = m_a11; | |
76 FX_FLOAT a12 = m_a12; | |
77 FX_FLOAT a13 = m_a13; | |
78 FX_FLOAT a21 = m_a21; | |
79 FX_FLOAT a22 = m_a22; | |
80 FX_FLOAT a23 = m_a23; | |
81 FX_FLOAT a31 = m_a31; | |
82 FX_FLOAT a32 = m_a32; | |
83 FX_FLOAT a33 = m_a33; | |
84 int32_t i; | |
85 for (i = 0; i < max; i += 2) { | |
86 FX_FLOAT x = (*points)[i]; | |
87 FX_FLOAT y = (*points)[i + 1]; | |
88 FX_FLOAT denominator = a13 * x + a23 * y + a33; | |
89 (*points)[i] = (a11 * x + a21 * y + a31) / denominator; | |
90 (*points)[i + 1] = (a12 * x + a22 * y + a32) / denominator; | |
91 } | |
92 } | |
93 CBC_CommonPerspectiveTransform* | |
94 CBC_CommonPerspectiveTransform::SquareToQuadrilateral(FX_FLOAT x0, | |
95 FX_FLOAT y0, | |
96 FX_FLOAT x1, | |
97 FX_FLOAT y1, | |
98 FX_FLOAT x2, | |
99 FX_FLOAT y2, | |
100 FX_FLOAT x3, | |
101 FX_FLOAT y3) { | |
102 FX_FLOAT dy2 = y3 - y2; | |
103 FX_FLOAT dy3 = y0 - y1 + y2 - y3; | |
104 if ((dy2 == 0.0f) && (dy3 == 0.0f)) { | |
105 return new CBC_CommonPerspectiveTransform(x1 - x0, x2 - x1, x0, y1 - y0, | |
106 y2 - y1, y0, 0.0f, 0.0f, 1.0f); | |
107 } else { | |
108 FX_FLOAT dx1 = x1 - x2; | |
109 FX_FLOAT dx2 = x3 - x2; | |
110 FX_FLOAT dx3 = x0 - x1 + x2 - x3; | |
111 FX_FLOAT dy1 = y1 - y2; | |
112 FX_FLOAT denominator = dx1 * dy2 - dx2 * dy1; | |
113 FX_FLOAT a13 = (dx3 * dy2 - dx2 * dy3) / denominator; | |
114 FX_FLOAT a23 = (dx1 * dy3 - dx3 * dy1) / denominator; | |
115 return new CBC_CommonPerspectiveTransform( | |
116 x1 - x0 + a13 * x1, x3 - x0 + a23 * x3, x0, y1 - y0 + a13 * y1, | |
117 y3 - y0 + a23 * y3, y0, a13, a23, 1.0f); | |
118 } | |
119 } | |
120 CBC_CommonPerspectiveTransform* | |
121 CBC_CommonPerspectiveTransform::QuadrilateralToSquare(FX_FLOAT x0, | |
122 FX_FLOAT y0, | |
123 FX_FLOAT x1, | |
124 FX_FLOAT y1, | |
125 FX_FLOAT x2, | |
126 FX_FLOAT y2, | |
127 FX_FLOAT x3, | |
128 FX_FLOAT y3) { | |
129 std::unique_ptr<CBC_CommonPerspectiveTransform> temp1( | |
130 SquareToQuadrilateral(x0, y0, x1, y1, x2, y2, x3, y3)); | |
131 return temp1->BuildAdjoint(); | |
132 } | |
133 CBC_CommonPerspectiveTransform* CBC_CommonPerspectiveTransform::BuildAdjoint() { | |
134 return new CBC_CommonPerspectiveTransform( | |
135 m_a22 * m_a33 - m_a23 * m_a32, m_a23 * m_a31 - m_a21 * m_a33, | |
136 m_a21 * m_a32 - m_a22 * m_a31, m_a13 * m_a32 - m_a12 * m_a33, | |
137 m_a11 * m_a33 - m_a13 * m_a31, m_a12 * m_a31 - m_a11 * m_a32, | |
138 m_a12 * m_a23 - m_a13 * m_a22, m_a13 * m_a21 - m_a11 * m_a23, | |
139 m_a11 * m_a22 - m_a12 * m_a21); | |
140 } | |
141 CBC_CommonPerspectiveTransform* CBC_CommonPerspectiveTransform::Times( | |
142 CBC_CommonPerspectiveTransform& other) { | |
143 return new CBC_CommonPerspectiveTransform( | |
144 m_a11 * other.m_a11 + m_a21 * other.m_a12 + m_a31 * other.m_a13, | |
145 m_a11 * other.m_a21 + m_a21 * other.m_a22 + m_a31 * other.m_a23, | |
146 m_a11 * other.m_a31 + m_a21 * other.m_a32 + m_a31 * other.m_a33, | |
147 m_a12 * other.m_a11 + m_a22 * other.m_a12 + m_a32 * other.m_a13, | |
148 m_a12 * other.m_a21 + m_a22 * other.m_a22 + m_a32 * other.m_a23, | |
149 m_a12 * other.m_a31 + m_a22 * other.m_a32 + m_a32 * other.m_a33, | |
150 m_a13 * other.m_a11 + m_a23 * other.m_a12 + m_a33 * other.m_a13, | |
151 m_a13 * other.m_a21 + m_a23 * other.m_a22 + m_a33 * other.m_a23, | |
152 m_a13 * other.m_a31 + m_a23 * other.m_a32 + m_a33 * other.m_a33); | |
153 } | |
OLD | NEW |