summaryrefslogtreecommitdiff
path: root/xfa/src/fxbarcode/common/BC_CommonPerspectiveTransform.cpp
blob: cb59d99cdf57795ff28ca5deb66d646d7c511212 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
// Original code is licensed as follows:
/*
 * Copyright 2007 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "../barcode.h"
#include "BC_CommonPerspectiveTransform.h"
CBC_CommonPerspectiveTransform::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) :
    m_a11(a11), m_a21(a21), m_a31(a31),
    m_a12(a12), m_a22(a22), m_a32(a32),
    m_a13(a13), m_a23(a23), m_a33(a33)
{
}
CBC_CommonPerspectiveTransform::~CBC_CommonPerspectiveTransform()
{
}
CBC_CommonPerspectiveTransform *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)
{
    CBC_AutoPtr<CBC_CommonPerspectiveTransform> qToS(QuadrilateralToSquare(x0, y0, x1, y1, x2, y2, x3, y3));
    CBC_AutoPtr<CBC_CommonPerspectiveTransform> sToQ(SquareToQuadrilateral(x0p, y0p, x1p, y1p, x2p, y2p, x3p, y3p));
    return sToQ->Times(*(qToS.get()));
}
void CBC_CommonPerspectiveTransform::TransformPoints(CFX_FloatArray *points)
{
    FX_INT32 max = points->GetSize();
    FX_FLOAT a11 = m_a11;
    FX_FLOAT a12 = m_a12;
    FX_FLOAT a13 = m_a13;
    FX_FLOAT a21 = m_a21;
    FX_FLOAT a22 = m_a22;
    FX_FLOAT a23 = m_a23;
    FX_FLOAT a31 = m_a31;
    FX_FLOAT a32 = m_a32;
    FX_FLOAT a33 = m_a33;
    FX_INT32 i;
    for (i = 0; i < max; i += 2) {
        FX_FLOAT x = (*points)[i];
        FX_FLOAT y = (*points)[i + 1];
        FX_FLOAT denominator = a13 * x + a23 * y + a33;
        (*points)[i] = (a11 * x + a21 * y + a31) / denominator;
        (*points)[i + 1] = (a12 * x + a22 * y + a32) / denominator;
    }
}
CBC_CommonPerspectiveTransform *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)
{
    FX_FLOAT dy2 = y3 - y2;
    FX_FLOAT dy3 = y0 - y1 + y2 - y3;
    if ((dy2 == 0.0f) && (dy3 == 0.0f)) {
        return FX_NEW CBC_CommonPerspectiveTransform(x1 - x0, x2 - x1, x0,
                y1 - y0, y2 - y1, y0,
                0.0f, 0.0f, 1.0f);
    } else {
        FX_FLOAT dx1 = x1 - x2;
        FX_FLOAT dx2 = x3 - x2;
        FX_FLOAT dx3 = x0 - x1 + x2 - x3;
        FX_FLOAT dy1 = y1 - y2;
        FX_FLOAT denominator = dx1 * dy2 - dx2 * dy1;
        FX_FLOAT a13 = (dx3 * dy2 - dx2 * dy3) / denominator;
        FX_FLOAT a23 = (dx1 * dy3 - dx3 * dy1) / denominator;
        return FX_NEW CBC_CommonPerspectiveTransform(x1 - x0 + a13 * x1, x3 - x0 + a23 * x3, x0,
                y1 - y0 + a13 * y1, y3 - y0 + a23 * y3, y0,
                a13, a23, 1.0f);
    }
}
CBC_CommonPerspectiveTransform *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)
{
    CBC_AutoPtr<CBC_CommonPerspectiveTransform> temp1(SquareToQuadrilateral(x0, y0, x1, y1, x2, y2, x3, y3));
    return temp1->BuildAdjoint();
}
CBC_CommonPerspectiveTransform *CBC_CommonPerspectiveTransform::BuildAdjoint()
{
    return FX_NEW CBC_CommonPerspectiveTransform(m_a22 * m_a33 - m_a23 * m_a32,
            m_a23 * m_a31 - m_a21 * m_a33,
            m_a21 * m_a32 - m_a22 * m_a31,
            m_a13 * m_a32 - m_a12 * m_a33,
            m_a11 * m_a33 - m_a13 * m_a31,
            m_a12 * m_a31 - m_a11 * m_a32,
            m_a12 * m_a23 - m_a13 * m_a22,
            m_a13 * m_a21 - m_a11 * m_a23,
            m_a11 * m_a22 - m_a12 * m_a21);
}
CBC_CommonPerspectiveTransform *CBC_CommonPerspectiveTransform::Times(CBC_CommonPerspectiveTransform &other)
{
    return FX_NEW CBC_CommonPerspectiveTransform(m_a11 * other.m_a11 + m_a21 * other.m_a12 + m_a31 * other.m_a13,
            m_a11 * other.m_a21 + m_a21 * other.m_a22 + m_a31 * other.m_a23,
            m_a11 * other.m_a31 + m_a21 * other.m_a32 + m_a31 * other.m_a33,
            m_a12 * other.m_a11 + m_a22 * other.m_a12 + m_a32 * other.m_a13,
            m_a12 * other.m_a21 + m_a22 * other.m_a22 + m_a32 * other.m_a23,
            m_a12 * other.m_a31 + m_a22 * other.m_a32 + m_a32 * other.m_a33,
            m_a13 * other.m_a11 + m_a23 * other.m_a12 + m_a33 * other.m_a13,
            m_a13 * other.m_a21 + m_a23 * other.m_a22 + m_a33 * other.m_a23,
            m_a13 * other.m_a31 + m_a23 * other.m_a32 + m_a33 * other.m_a33);
}