summaryrefslogtreecommitdiff
path: root/xfa/src/fxbarcode/src/BC_OneDimReader.cpp
blob: 3e440d90369e82aaca9ec5d24e9a57aa929716ab (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// 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_Reader.h"
#include "include/BC_OneDReader.h"
#include "include/BC_CommonBitArray.h"
#include "include/BC_OneDimReader.h"
const FX_INT32 CBC_OneDimReader::MAX_AVG_VARIANCE = (FX_INT32)(256 * 0.48f);
const FX_INT32 CBC_OneDimReader::MAX_INDIVIDUAL_VARIANCE = (FX_INT32)(256 * 0.7f);
const FX_INT32 CBC_OneDimReader::START_END_PATTERN[3] = {1, 1, 1};
const FX_INT32 CBC_OneDimReader::MIDDLE_PATTERN[5] = {1, 1, 1, 1, 1};
const FX_INT32 CBC_OneDimReader::L_PATTERNS[10][4] = {
    {3, 2, 1, 1},
    {2, 2, 2, 1},
    {2, 1, 2, 2},
    {1, 4, 1, 1},
    {1, 1, 3, 2},
    {1, 2, 3, 1},
    {1, 1, 1, 4},
    {1, 3, 1, 2},
    {1, 2, 1, 3},
    {3, 1, 1, 2}
};
const FX_INT32 CBC_OneDimReader::L_AND_G_PATTERNS[20][4] = {
    {3, 2, 1, 1},
    {2, 2, 2, 1},
    {2, 1, 2, 2},
    {1, 4, 1, 1},
    {1, 1, 3, 2},
    {1, 2, 3, 1},
    {1, 1, 1, 4},
    {1, 3, 1, 2},
    {1, 2, 1, 3},
    {3, 1, 1, 2},
    {1, 1, 2, 3},
    {1, 2, 2, 2},
    {2, 2, 1, 2},
    {1, 1, 4, 1},
    {2, 3, 1, 1},
    {1, 3, 2, 1},
    {4, 1, 1, 1},
    {2, 1, 3, 1},
    {3, 1, 2, 1},
    {2, 1, 1, 3}
};
CBC_OneDimReader::CBC_OneDimReader()
{
}
CBC_OneDimReader::~CBC_OneDimReader()
{
}
CFX_Int32Array *CBC_OneDimReader::FindStartGuardPattern(CBC_CommonBitArray *row, FX_INT32 &e)
{
    FX_BOOL foundStart = FALSE;
    CFX_Int32Array *startRange = NULL;
    CFX_Int32Array startEndPattern;
    startEndPattern.SetSize(3);
    startEndPattern[0] = START_END_PATTERN[0];
    startEndPattern[1] = START_END_PATTERN[1];
    startEndPattern[2] = START_END_PATTERN[2];
    FX_INT32 nextStart = 0;
    while (!foundStart) {
        if(startRange != NULL) {
            delete startRange;
            startRange = NULL;
        }
        startRange = FindGuardPattern(row, nextStart, FALSE, &startEndPattern, e);
        BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
        FX_INT32 start = (*startRange)[0];
        nextStart = (*startRange)[1];
        if (start <= 1) {
            break;
        }
        FX_INT32 quietStart = start - (nextStart - start);
        if (quietStart >= 0) {
            FX_BOOL booT = row->IsRange(quietStart, start, FALSE, e);
            BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
            foundStart = booT;
        }
    }
    return startRange;
}
CFX_ByteString CBC_OneDimReader::DecodeRow(FX_INT32 rowNumber, CBC_CommonBitArray *row, FX_INT32 hints, FX_INT32 &e)
{
    CFX_Int32Array* StartPattern = FindStartGuardPattern(row, e);
    BC_EXCEPTION_CHECK_ReturnValue(e, "");
    CBC_AutoPtr<CFX_Int32Array > result(StartPattern);
    CFX_ByteString temp = DecodeRow(rowNumber, row, result.get(), hints, e);
    BC_EXCEPTION_CHECK_ReturnValue(e, "");
    return temp;
}
CFX_ByteString CBC_OneDimReader::DecodeRow(FX_INT32 rowNumber, CBC_CommonBitArray *row, CFX_Int32Array *startGuardRange, FX_INT32 hints, FX_INT32 &e)
{
    CFX_ByteString result;
    FX_INT32 endStart = DecodeMiddle(row, startGuardRange, result, e);
    BC_EXCEPTION_CHECK_ReturnValue(e, "");
    FX_BOOL b = CheckChecksum(result, e);
    BC_EXCEPTION_CHECK_ReturnValue(e, "");
    if (!b) {
        e = BCExceptionChecksumException;
        return "";
    }
    return result;
}
FX_BOOL CBC_OneDimReader::CheckChecksum(CFX_ByteString &s, FX_INT32 &e)
{
    FX_BOOL temp = CheckStandardUPCEANChecksum(s, e);
    BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
    return temp;
}
FX_BOOL CBC_OneDimReader::CheckStandardUPCEANChecksum(CFX_ByteString &s, FX_INT32 &e)
{
    FX_INT32 length = s.GetLength();
    if (length == 0) {
        return FALSE;
    }
    FX_INT32 sum = 0;
    for (FX_INT32 i = length - 2; i >= 0; i -= 2) {
        FX_INT32 digit = (FX_INT32) s[i] - (FX_INT32) '0';
        if (digit < 0 || digit > 9) {
            e = BCExceptionFormatException;
            return FALSE;
        }
        sum += digit;
    }
    sum *= 3;
    for (FX_INT32 j = length - 1; j >= 0; j -= 2) {
        FX_INT32 digit = (FX_INT32) s[j] - (FX_INT32) '0';
        if (digit < 0 || digit > 9) {
            e = BCExceptionFormatException;
            return FALSE;
        }
        sum += digit;
    }
    return sum % 10 == 0;
}
CFX_Int32Array *CBC_OneDimReader::DecodeEnd(CBC_CommonBitArray* row, FX_INT32 endStart, FX_INT32 &e)
{
    CFX_Int32Array startEndPattern;
    startEndPattern.Add(START_END_PATTERN[0]);
    startEndPattern.Add(START_END_PATTERN[1]);
    startEndPattern.Add(START_END_PATTERN[2]);
    CFX_Int32Array* FindGuard = FindGuardPattern(row, endStart, FALSE, &startEndPattern, e);
    BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
    return FindGuard;
}
CFX_Int32Array *CBC_OneDimReader::FindGuardPattern(CBC_CommonBitArray *row, FX_INT32 rowOffset, FX_BOOL whiteFirst, CFX_Int32Array *pattern, FX_INT32 &e)
{
    FX_INT32 patternLength = pattern->GetSize();
    CFX_Int32Array counters;
    counters.SetSize(patternLength);
    FX_INT32 width = row->GetSize();
    FX_BOOL isWhite = FALSE;
    while (rowOffset < width) {
        isWhite = !row->Get(rowOffset);
        if (whiteFirst == isWhite) {
            break;
        }
        rowOffset++;
    }
    FX_INT32 counterPosition = 0;
    FX_INT32 patternStart = rowOffset;
    for (FX_INT32 x = rowOffset; x < width; x++) {
        FX_BOOL pixel = row->Get(x);
        if (pixel ^ isWhite) {
            counters[counterPosition]++;
        } else {
            if (counterPosition == patternLength - 1) {
                if (PatternMatchVariance(&counters, &(*pattern)[0], MAX_INDIVIDUAL_VARIANCE) < MAX_AVG_VARIANCE) {
                    CFX_Int32Array *result = FX_NEW CFX_Int32Array();
                    result->SetSize(2);
                    (*result)[0] = patternStart;
                    (*result)[1] = x;
                    return result;
                }
                patternStart += counters[0] + counters[1];
                for (FX_INT32 y = 2; y < patternLength; y++) {
                    counters[y - 2] = counters[y];
                }
                counters[patternLength - 2] = 0;
                counters[patternLength - 1] = 0;
                counterPosition--;
            } else {
                counterPosition++;
            }
            counters[counterPosition] = 1;
            isWhite = !isWhite;
        }
    }
    e = BCExceptionNotFound;
    BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
    return NULL;
}
FX_INT32 CBC_OneDimReader::DecodeDigit(CBC_CommonBitArray *row, CFX_Int32Array *counters, FX_INT32 rowOffset, const FX_INT32* patterns, FX_INT32 patternLength, FX_INT32 &e)
{
    RecordPattern(row, rowOffset, counters, e);
    BC_EXCEPTION_CHECK_ReturnValue(e, 0);
    FX_INT32 bestVariance = MAX_AVG_VARIANCE;
    FX_INT32 bestMatch = -1;
    FX_INT32 max = patternLength;
    for (FX_INT32 i = 0; i < max; i++) {
        FX_INT32 variance = PatternMatchVariance(counters, &patterns[i * 4], MAX_INDIVIDUAL_VARIANCE);
        if (variance < bestVariance) {
            bestVariance = variance;
            bestMatch = i;
        }
    }
    if (bestMatch >= 0) {
        return bestMatch;
    } else {
        e = BCExceptionNotFound;
        return 0;
    }
    return 0;
}