summaryrefslogtreecommitdiff
path: root/xfa/src/fxbarcode/pdf417/BC_PDF417DetectionResultRowIndicatorColumn.cpp
blob: 89e46a74cd31217d9eca75e4828759e5e572afc5 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// 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 2013 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_ResultPoint.h"
#include "BC_PDF417BarcodeMetadata.h"
#include "BC_PDF417BoundingBox.h"
#include "BC_PDF417Codeword.h"
#include "BC_PDF417BarcodeValue.h"
#include "BC_PDF417Common.h"
#include "BC_PDF417DetectionResultColumn.h"
#include "BC_PDF417DetectionResultRowIndicatorColumn.h"
CBC_DetectionResultRowIndicatorColumn::CBC_DetectionResultRowIndicatorColumn(CBC_BoundingBox* boundingBox, FX_BOOL isLeft)
    : CBC_DetectionResultColumn(boundingBox)
{
    m_isLeft = isLeft;
}
CBC_DetectionResultRowIndicatorColumn::~CBC_DetectionResultRowIndicatorColumn()
{
}
void CBC_DetectionResultRowIndicatorColumn::setRowNumbers()
{
    for (FX_INT32 i = 0; i < m_codewords->GetSize(); i++) {
        CBC_Codeword * codeword = (CBC_Codeword*)m_codewords->GetAt(i);
        if (codeword != NULL) {
            codeword->setRowNumberAsRowIndicatorColumn();
        }
    }
}
FX_INT32 CBC_DetectionResultRowIndicatorColumn::adjustCompleteIndicatorColumnRowNumbers(CBC_BarcodeMetadata barcodeMetadata)
{
    CFX_PtrArray* codewords = getCodewords();
    setRowNumbers();
    removeIncorrectCodewords(codewords, barcodeMetadata);
    CBC_BoundingBox* boundingBox = getBoundingBox();
    CBC_ResultPoint* top = m_isLeft ? boundingBox->getTopLeft() : boundingBox->getTopRight();
    CBC_ResultPoint* bottom = m_isLeft ? boundingBox->getBottomLeft() : boundingBox->getBottomRight();
    FX_INT32 firstRow = imageRowToCodewordIndex((FX_INT32) top->GetY());
    FX_INT32 lastRow = imageRowToCodewordIndex((FX_INT32) bottom->GetY());
    FX_FLOAT averageRowHeight = (lastRow - firstRow) / (FX_FLOAT) barcodeMetadata.getRowCount();
    FX_INT32 barcodeRow = -1;
    FX_INT32 maxRowHeight = 1;
    FX_INT32 currentRowHeight = 0;
    for (FX_INT32 codewordsRow = firstRow; codewordsRow < lastRow; codewordsRow++) {
        if (codewords->GetAt(codewordsRow) == NULL) {
            continue;
        }
        CBC_Codeword* codeword = (CBC_Codeword*)codewords->GetAt(codewordsRow);
        FX_INT32 rowDifference = codeword->getRowNumber() - barcodeRow;
        if (rowDifference == 0) {
            currentRowHeight++;
        } else if (rowDifference == 1) {
            maxRowHeight = maxRowHeight > currentRowHeight ? maxRowHeight : currentRowHeight;
            currentRowHeight = 1;
            barcodeRow = codeword->getRowNumber();
        } else if (rowDifference < 0) {
            codewords->SetAt(codewordsRow, NULL);
        } else if (codeword->getRowNumber() >= barcodeMetadata.getRowCount()) {
            codewords->SetAt(codewordsRow, NULL);
        } else if (rowDifference > codewordsRow) {
            codewords->SetAt(codewordsRow, NULL);
        } else {
            FX_INT32 checkedRows;
            if (maxRowHeight > 2) {
                checkedRows = (maxRowHeight - 2) * rowDifference;
            } else {
                checkedRows = rowDifference;
            }
            FX_BOOL closePreviousCodewordFound = checkedRows >= codewordsRow;
            for (FX_INT32 i = 1; i <= checkedRows && !closePreviousCodewordFound; i++) {
                closePreviousCodewordFound = codewords->GetAt(codewordsRow - i) != NULL;
            }
            if (closePreviousCodewordFound) {
                codewords->SetAt(codewordsRow, NULL);
            } else {
                barcodeRow = codeword->getRowNumber();
                currentRowHeight = 1;
            }
        }
    }
    return (FX_INT32) (averageRowHeight + 0.5);
}
CFX_Int32Array* CBC_DetectionResultRowIndicatorColumn::getRowHeights(FX_INT32 &e)
{
    CBC_BarcodeMetadata* barcodeMetadata = getBarcodeMetadata();
    if (barcodeMetadata == NULL) {
        e = BCExceptionCannotMetadata;
        return NULL;
    }
    adjustIncompleteIndicatorColumnRowNumbers(*barcodeMetadata);
    CFX_Int32Array* result = FX_NEW CFX_Int32Array;
    result->SetSize(barcodeMetadata->getRowCount());
    for (FX_INT32 i = 0; i < getCodewords()->GetSize(); i++) {
        CBC_Codeword* codeword = (CBC_Codeword*)getCodewords()->GetAt(i);
        if (codeword != NULL) {
            result->SetAt(codeword->getRowNumber(),  result->GetAt(codeword->getRowNumber()) + 1);
        }
    }
    return result;
}
FX_INT32 CBC_DetectionResultRowIndicatorColumn::adjustIncompleteIndicatorColumnRowNumbers(CBC_BarcodeMetadata barcodeMetadata)
{
    CBC_BoundingBox* boundingBox = getBoundingBox();
    CBC_ResultPoint* top = m_isLeft ? boundingBox->getTopLeft() : boundingBox->getTopRight();
    CBC_ResultPoint* bottom = m_isLeft ? boundingBox->getBottomLeft() : boundingBox->getBottomRight();
    FX_INT32 firstRow = imageRowToCodewordIndex((FX_INT32) top->GetY());
    FX_INT32 lastRow = imageRowToCodewordIndex((FX_INT32) bottom->GetY());
    FX_FLOAT averageRowHeight = (lastRow - firstRow) / (FX_FLOAT) barcodeMetadata.getRowCount();
    CFX_PtrArray* codewords = getCodewords();
    FX_INT32 barcodeRow = -1;
    FX_INT32 maxRowHeight = 1;
    FX_INT32 currentRowHeight = 0;
    for (FX_INT32 codewordsRow = firstRow; codewordsRow < lastRow; codewordsRow++) {
        if (codewords->GetAt(codewordsRow) == NULL) {
            continue;
        }
        CBC_Codeword* codeword = (CBC_Codeword*)codewords->GetAt(codewordsRow);
        codeword->setRowNumberAsRowIndicatorColumn();
        FX_INT32 rowDifference = codeword->getRowNumber() - barcodeRow;
        if (rowDifference == 0) {
            currentRowHeight++;
        } else if (rowDifference == 1) {
            maxRowHeight = maxRowHeight > currentRowHeight ? maxRowHeight : currentRowHeight;
            currentRowHeight = 1;
            barcodeRow = codeword->getRowNumber();
        } else if (codeword->getRowNumber() >= barcodeMetadata.getRowCount()) {
            codewords->SetAt(codewordsRow, NULL);
        } else {
            barcodeRow = codeword->getRowNumber();
            currentRowHeight = 1;
        }
    }
    return (FX_INT32) (averageRowHeight + 0.5);
}
CBC_BarcodeMetadata* CBC_DetectionResultRowIndicatorColumn::getBarcodeMetadata()
{
    CFX_PtrArray* codewords = getCodewords();
    CBC_BarcodeValue barcodeColumnCount;
    CBC_BarcodeValue barcodeRowCountUpperPart;
    CBC_BarcodeValue barcodeRowCountLowerPart;
    CBC_BarcodeValue barcodeECLevel;
    for (FX_INT32 i = 0; i < codewords->GetSize(); i++) {
        CBC_Codeword* codeword = (CBC_Codeword*)codewords->GetAt(i);
        if (codeword == NULL) {
            continue;
        }
        codeword->setRowNumberAsRowIndicatorColumn();
        FX_INT32 rowIndicatorValue = codeword->getValue() % 30;
        FX_INT32 codewordRowNumber = codeword->getRowNumber();
        if (!m_isLeft) {
            codewordRowNumber += 2;
        }
        switch (codewordRowNumber % 3) {
            case 0:
                barcodeRowCountUpperPart.setValue(rowIndicatorValue * 3 + 1);
                break;
            case 1:
                barcodeECLevel.setValue(rowIndicatorValue / 3);
                barcodeRowCountLowerPart.setValue(rowIndicatorValue % 3);
                break;
            case 2:
                barcodeColumnCount.setValue(rowIndicatorValue + 1);
                break;
        }
    }
    if ((barcodeColumnCount.getValue()->GetSize() == 0) ||
            (barcodeRowCountUpperPart.getValue()->GetSize() == 0) ||
            (barcodeRowCountLowerPart.getValue()->GetSize() == 0) ||
            (barcodeECLevel.getValue()->GetSize() == 0) ||
            barcodeColumnCount.getValue()->GetAt(0) < 1 ||
            barcodeRowCountUpperPart.getValue()->GetAt(0) + barcodeRowCountLowerPart.getValue()->GetAt(0) < CBC_PDF417Common::MIN_ROWS_IN_BARCODE ||
            barcodeRowCountUpperPart.getValue()->GetAt(0) + barcodeRowCountLowerPart.getValue()->GetAt(0) > CBC_PDF417Common::MAX_ROWS_IN_BARCODE) {
        return NULL;
    }
    CBC_BarcodeMetadata* barcodeMetadata = FX_NEW CBC_BarcodeMetadata(barcodeColumnCount.getValue()->GetAt(0), barcodeRowCountUpperPart.getValue()->GetAt(0), barcodeRowCountLowerPart.getValue()->GetAt(0), barcodeECLevel.getValue()->GetAt(0));
    removeIncorrectCodewords(codewords, *barcodeMetadata);
    return barcodeMetadata;
}
FX_BOOL CBC_DetectionResultRowIndicatorColumn::isLeft()
{
    return m_isLeft;
}
CFX_ByteString CBC_DetectionResultRowIndicatorColumn::toString()
{
    return (CFX_ByteString)"IsLeft: " + (CFX_ByteString)m_isLeft + '\n' + CBC_DetectionResultColumn::toString();
}
void CBC_DetectionResultRowIndicatorColumn::removeIncorrectCodewords(CFX_PtrArray* codewords, CBC_BarcodeMetadata barcodeMetadata)
{
    for (FX_INT32 codewordRow = 0; codewordRow < codewords->GetSize(); codewordRow++) {
        CBC_Codeword* codeword = (CBC_Codeword*)codewords->GetAt(codewordRow);
        if (codeword == NULL) {
            continue;
        }
        FX_INT32 rowIndicatorValue = codeword->getValue() % 30;
        FX_INT32 codewordRowNumber = codeword->getRowNumber();
        if (codewordRowNumber > barcodeMetadata.getRowCount()) {
            codewords->SetAt(codewordRow, NULL);
            continue;
        }
        if (!m_isLeft) {
            codewordRowNumber += 2;
        }
        switch (codewordRowNumber % 3) {
            case 0:
                if (rowIndicatorValue * 3 + 1 != barcodeMetadata.getRowCountUpperPart()) {
                    codewords->SetAt(codewordRow, NULL);
                }
                break;
            case 1:
                if (rowIndicatorValue / 3 != barcodeMetadata.getErrorCorrectionLevel() ||
                        rowIndicatorValue % 3 != barcodeMetadata.getRowCountLowerPart()) {
                    codewords->SetAt(codewordRow, NULL);
                }
                break;
            case 2:
                if (rowIndicatorValue + 1 != barcodeMetadata.getColumnCount()) {
                    codewords->SetAt(codewordRow, NULL);
                }
                break;
        }
    }
}