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 2013 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_PDF417Codeword.h"
25#include "BC_PDF417BoundingBox.h"
26#include "BC_PDF417DetectionResultColumn.h"
27int32_t CBC_DetectionResultColumn::MAX_NEARBY_DISTANCE = 5;
28CBC_DetectionResultColumn::CBC_DetectionResultColumn(
29    CBC_BoundingBox* boundingBox) {
30  m_boundingBox = boundingBox;
31  m_codewords = new CFX_PtrArray;
32  m_codewords->SetSize(boundingBox->getMaxY() - boundingBox->getMinY() + 1);
33}
34CBC_DetectionResultColumn::~CBC_DetectionResultColumn() {
35  for (int32_t i = 0; i < m_codewords->GetSize(); i++) {
36    delete (CBC_Codeword*)m_codewords->GetAt(i);
37  }
38  m_codewords->RemoveAll();
39  delete m_codewords;
40}
41CBC_Codeword* CBC_DetectionResultColumn::getCodewordNearby(int32_t imageRow) {
42  CBC_Codeword* codeword = getCodeword(imageRow);
43  if (codeword != NULL) {
44    return codeword;
45  }
46  for (int32_t i = 1; i < MAX_NEARBY_DISTANCE; i++) {
47    int32_t nearImageRow = imageRowToCodewordIndex(imageRow) - i;
48    if (nearImageRow >= 0) {
49      codeword = (CBC_Codeword*)m_codewords->GetAt(nearImageRow);
50      if (codeword != NULL) {
51        return codeword;
52      }
53    }
54    nearImageRow = imageRowToCodewordIndex(imageRow) + i;
55    if (nearImageRow < m_codewords->GetSize()) {
56      codeword = (CBC_Codeword*)m_codewords->GetAt(nearImageRow);
57      if (codeword != NULL) {
58        return codeword;
59      }
60    }
61  }
62  return NULL;
63}
64int32_t CBC_DetectionResultColumn::imageRowToCodewordIndex(int32_t imageRow) {
65  return imageRow - m_boundingBox->getMinY();
66}
67int32_t CBC_DetectionResultColumn::codewordIndexToImageRow(
68    int32_t codewordIndex) {
69  return m_boundingBox->getMinY() + codewordIndex;
70}
71void CBC_DetectionResultColumn::setCodeword(int32_t imageRow,
72                                            CBC_Codeword* codeword) {
73  m_codewords->SetAt(imageRowToCodewordIndex(imageRow), codeword);
74}
75CBC_Codeword* CBC_DetectionResultColumn::getCodeword(int32_t imageRow) {
76  return (CBC_Codeword*)m_codewords->GetAt(imageRowToCodewordIndex(imageRow));
77}
78CBC_BoundingBox* CBC_DetectionResultColumn::getBoundingBox() {
79  return m_boundingBox;
80}
81CFX_PtrArray* CBC_DetectionResultColumn::getCodewords() {
82  return m_codewords;
83}
84CFX_ByteString CBC_DetectionResultColumn::toString() {
85  CFX_ByteString result;
86  int32_t row = 0;
87  for (int32_t i = 0; i < m_codewords->GetSize(); i++) {
88    CBC_Codeword* codeword = (CBC_Codeword*)m_codewords->GetAt(i);
89    if (codeword == NULL) {
90      result += (FX_CHAR)row;
91      row++;
92      continue;
93    }
94    result += (FX_CHAR)row;
95    result += codeword->getRowNumber();
96    result += codeword->getValue();
97    row++;
98  }
99  return result;
100}
101