summaryrefslogtreecommitdiff
path: root/xfa/src/fxbarcode/src/BC_CommonPerspectiveTransform.cpp
blob: 98234729e5fa5e538bf578c0311c4085998022e9 (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
// 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

#include "barcode.h"
#include "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);
}