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 2008 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 "xfa/src/fxbarcode/BC_Reader.h"
25#include "xfa/src/fxbarcode/common/BC_CommonBitArray.h"
26#include "BC_OneDReader.h"
27#include "BC_OneDimReader.h"
28#include "BC_OnedEAN8Reader.h"
29CBC_OnedEAN8Reader::CBC_OnedEAN8Reader() {}
30CBC_OnedEAN8Reader::~CBC_OnedEAN8Reader() {}
31int32_t CBC_OnedEAN8Reader::DecodeMiddle(CBC_CommonBitArray* row,
32                                         CFX_Int32Array* startRange,
33                                         CFX_ByteString& resultResult,
34                                         int32_t& e) {
35  CFX_Int32Array counters;
36  counters.Add(0);
37  counters.Add(0);
38  counters.Add(0);
39  counters.Add(0);
40  int32_t end = row->GetSize();
41  int32_t rowOffset = (*startRange)[1];
42  int32_t rowOffsetLeft = rowOffset;
43  for (int32_t x = 0; x < 4 && rowOffset < end; x++) {
44    int32_t bestMatch =
45        DecodeDigit(row, &counters, rowOffset,
46                    &(CBC_OneDimReader::L_PATTERNS[0][0]), 10, e);
47    BC_EXCEPTION_CHECK_ReturnValue(e, 0);
48    resultResult += (FX_CHAR)('0' + bestMatch);
49    for (int32_t i = 0; i < counters.GetSize(); i++) {
50      rowOffset += counters[i];
51    }
52  }
53  int32_t RowOffsetLen = (rowOffset - rowOffsetLeft) / 4;
54  CFX_Int32Array result;
55  result.Add(CBC_OneDimReader::MIDDLE_PATTERN[0]);
56  result.Add(CBC_OneDimReader::MIDDLE_PATTERN[1]);
57  result.Add(CBC_OneDimReader::MIDDLE_PATTERN[2]);
58  result.Add(CBC_OneDimReader::MIDDLE_PATTERN[3]);
59  result.Add(CBC_OneDimReader::MIDDLE_PATTERN[4]);
60  CFX_Int32Array* middleRange =
61      FindGuardPattern(row, rowOffset, TRUE, &result, e);
62  BC_EXCEPTION_CHECK_ReturnValue(e, 0);
63  int32_t rowOffsetMid = rowOffset;
64  rowOffset = (*middleRange)[1];
65  if ((rowOffset - rowOffsetMid) > RowOffsetLen) {
66    e = BCExceptionNotFound;
67    BC_EXCEPTION_CHECK_ReturnValue(e, 0);
68  }
69  if (middleRange != NULL) {
70    delete middleRange;
71    middleRange = NULL;
72  }
73  for (int32_t y = 0; y < 4 && rowOffset < end; y++) {
74    int32_t bestMatch =
75        DecodeDigit(row, &counters, rowOffset,
76                    &(CBC_OneDimReader::L_PATTERNS[0][0]), 10, e);
77    BC_EXCEPTION_CHECK_ReturnValue(e, 0);
78    resultResult += (FX_CHAR)('0' + bestMatch);
79    for (int32_t i = 0; i < counters.GetSize(); i++) {
80      rowOffset += counters[i];
81    }
82  }
83  return rowOffset;
84}
85