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/barcode.h"
24 #include "BC_CommonPerspectiveTransform.h"
CBC_CommonPerspectiveTransform(FX_FLOAT a11,FX_FLOAT a21,FX_FLOAT a31,FX_FLOAT a12,FX_FLOAT a22,FX_FLOAT a32,FX_FLOAT a13,FX_FLOAT a23,FX_FLOAT a33)25 CBC_CommonPerspectiveTransform::CBC_CommonPerspectiveTransform(FX_FLOAT a11,
26                                                                FX_FLOAT a21,
27                                                                FX_FLOAT a31,
28                                                                FX_FLOAT a12,
29                                                                FX_FLOAT a22,
30                                                                FX_FLOAT a32,
31                                                                FX_FLOAT a13,
32                                                                FX_FLOAT a23,
33                                                                FX_FLOAT a33)
34     : m_a11(a11),
35       m_a12(a12),
36       m_a13(a13),
37       m_a21(a21),
38       m_a22(a22),
39       m_a23(a23),
40       m_a31(a31),
41       m_a32(a32),
42       m_a33(a33) {
43 }
~CBC_CommonPerspectiveTransform()44 CBC_CommonPerspectiveTransform::~CBC_CommonPerspectiveTransform() {}
45 CBC_CommonPerspectiveTransform*
QuadrilateralToQuadrilateral(FX_FLOAT x0,FX_FLOAT y0,FX_FLOAT x1,FX_FLOAT y1,FX_FLOAT x2,FX_FLOAT y2,FX_FLOAT x3,FX_FLOAT y3,FX_FLOAT x0p,FX_FLOAT y0p,FX_FLOAT x1p,FX_FLOAT y1p,FX_FLOAT x2p,FX_FLOAT y2p,FX_FLOAT x3p,FX_FLOAT y3p)46 CBC_CommonPerspectiveTransform::QuadrilateralToQuadrilateral(FX_FLOAT x0,
47                                                              FX_FLOAT y0,
48                                                              FX_FLOAT x1,
49                                                              FX_FLOAT y1,
50                                                              FX_FLOAT x2,
51                                                              FX_FLOAT y2,
52                                                              FX_FLOAT x3,
53                                                              FX_FLOAT y3,
54                                                              FX_FLOAT x0p,
55                                                              FX_FLOAT y0p,
56                                                              FX_FLOAT x1p,
57                                                              FX_FLOAT y1p,
58                                                              FX_FLOAT x2p,
59                                                              FX_FLOAT y2p,
60                                                              FX_FLOAT x3p,
61                                                              FX_FLOAT y3p) {
62   CBC_AutoPtr<CBC_CommonPerspectiveTransform> qToS(
63       QuadrilateralToSquare(x0, y0, x1, y1, x2, y2, x3, y3));
64   CBC_AutoPtr<CBC_CommonPerspectiveTransform> sToQ(
65       SquareToQuadrilateral(x0p, y0p, x1p, y1p, x2p, y2p, x3p, y3p));
66   return sToQ->Times(*(qToS.get()));
67 }
TransformPoints(CFX_FloatArray * points)68 void CBC_CommonPerspectiveTransform::TransformPoints(CFX_FloatArray* points) {
69   int32_t max = points->GetSize();
70   FX_FLOAT a11 = m_a11;
71   FX_FLOAT a12 = m_a12;
72   FX_FLOAT a13 = m_a13;
73   FX_FLOAT a21 = m_a21;
74   FX_FLOAT a22 = m_a22;
75   FX_FLOAT a23 = m_a23;
76   FX_FLOAT a31 = m_a31;
77   FX_FLOAT a32 = m_a32;
78   FX_FLOAT a33 = m_a33;
79   int32_t i;
80   for (i = 0; i < max; i += 2) {
81     FX_FLOAT x = (*points)[i];
82     FX_FLOAT y = (*points)[i + 1];
83     FX_FLOAT denominator = a13 * x + a23 * y + a33;
84     (*points)[i] = (a11 * x + a21 * y + a31) / denominator;
85     (*points)[i + 1] = (a12 * x + a22 * y + a32) / denominator;
86   }
87 }
88 CBC_CommonPerspectiveTransform*
SquareToQuadrilateral(FX_FLOAT x0,FX_FLOAT y0,FX_FLOAT x1,FX_FLOAT y1,FX_FLOAT x2,FX_FLOAT y2,FX_FLOAT x3,FX_FLOAT y3)89 CBC_CommonPerspectiveTransform::SquareToQuadrilateral(FX_FLOAT x0,
90                                                       FX_FLOAT y0,
91                                                       FX_FLOAT x1,
92                                                       FX_FLOAT y1,
93                                                       FX_FLOAT x2,
94                                                       FX_FLOAT y2,
95                                                       FX_FLOAT x3,
96                                                       FX_FLOAT y3) {
97   FX_FLOAT dy2 = y3 - y2;
98   FX_FLOAT dy3 = y0 - y1 + y2 - y3;
99   if ((dy2 == 0.0f) && (dy3 == 0.0f)) {
100     return new CBC_CommonPerspectiveTransform(x1 - x0, x2 - x1, x0, y1 - y0,
101                                               y2 - y1, y0, 0.0f, 0.0f, 1.0f);
102   } else {
103     FX_FLOAT dx1 = x1 - x2;
104     FX_FLOAT dx2 = x3 - x2;
105     FX_FLOAT dx3 = x0 - x1 + x2 - x3;
106     FX_FLOAT dy1 = y1 - y2;
107     FX_FLOAT denominator = dx1 * dy2 - dx2 * dy1;
108     FX_FLOAT a13 = (dx3 * dy2 - dx2 * dy3) / denominator;
109     FX_FLOAT a23 = (dx1 * dy3 - dx3 * dy1) / denominator;
110     return new CBC_CommonPerspectiveTransform(
111         x1 - x0 + a13 * x1, x3 - x0 + a23 * x3, x0, y1 - y0 + a13 * y1,
112         y3 - y0 + a23 * y3, y0, a13, a23, 1.0f);
113   }
114 }
115 CBC_CommonPerspectiveTransform*
QuadrilateralToSquare(FX_FLOAT x0,FX_FLOAT y0,FX_FLOAT x1,FX_FLOAT y1,FX_FLOAT x2,FX_FLOAT y2,FX_FLOAT x3,FX_FLOAT y3)116 CBC_CommonPerspectiveTransform::QuadrilateralToSquare(FX_FLOAT x0,
117                                                       FX_FLOAT y0,
118                                                       FX_FLOAT x1,
119                                                       FX_FLOAT y1,
120                                                       FX_FLOAT x2,
121                                                       FX_FLOAT y2,
122                                                       FX_FLOAT x3,
123                                                       FX_FLOAT y3) {
124   CBC_AutoPtr<CBC_CommonPerspectiveTransform> temp1(
125       SquareToQuadrilateral(x0, y0, x1, y1, x2, y2, x3, y3));
126   return temp1->BuildAdjoint();
127 }
BuildAdjoint()128 CBC_CommonPerspectiveTransform* CBC_CommonPerspectiveTransform::BuildAdjoint() {
129   return new CBC_CommonPerspectiveTransform(
130       m_a22 * m_a33 - m_a23 * m_a32, m_a23 * m_a31 - m_a21 * m_a33,
131       m_a21 * m_a32 - m_a22 * m_a31, m_a13 * m_a32 - m_a12 * m_a33,
132       m_a11 * m_a33 - m_a13 * m_a31, m_a12 * m_a31 - m_a11 * m_a32,
133       m_a12 * m_a23 - m_a13 * m_a22, m_a13 * m_a21 - m_a11 * m_a23,
134       m_a11 * m_a22 - m_a12 * m_a21);
135 }
Times(CBC_CommonPerspectiveTransform & other)136 CBC_CommonPerspectiveTransform* CBC_CommonPerspectiveTransform::Times(
137     CBC_CommonPerspectiveTransform& other) {
138   return new CBC_CommonPerspectiveTransform(
139       m_a11 * other.m_a11 + m_a21 * other.m_a12 + m_a31 * other.m_a13,
140       m_a11 * other.m_a21 + m_a21 * other.m_a22 + m_a31 * other.m_a23,
141       m_a11 * other.m_a31 + m_a21 * other.m_a32 + m_a31 * other.m_a33,
142       m_a12 * other.m_a11 + m_a22 * other.m_a12 + m_a32 * other.m_a13,
143       m_a12 * other.m_a21 + m_a22 * other.m_a22 + m_a32 * other.m_a23,
144       m_a12 * other.m_a31 + m_a22 * other.m_a32 + m_a32 * other.m_a33,
145       m_a13 * other.m_a11 + m_a23 * other.m_a12 + m_a33 * other.m_a13,
146       m_a13 * other.m_a21 + m_a23 * other.m_a22 + m_a33 * other.m_a23,
147       m_a13 * other.m_a31 + m_a23 * other.m_a32 + m_a33 * other.m_a33);
148 }
149