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 2009 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/BC_BinaryBitmap.h"
26#include "xfa/src/fxbarcode/BC_ResultPoint.h"
27#include "xfa/src/fxbarcode/BC_BinaryBitmap.h"
28#include "xfa/src/fxbarcode/BC_DecoderResult.h"
29#include "xfa/src/fxbarcode/common/BC_CommonBitMatrix.h"
30#include "xfa/src/fxbarcode/common/BC_CommonBitArray.h"
31#include "xfa/src/fxbarcode/common/BC_CommonDecoderResult.h"
32#include "xfa/src/fxbarcode/common/BC_CommonBitMatrix.h"
33#include "BC_PDF417DetectorResult.h"
34#include "BC_PDF417Detector.h"
35#include "BC_PDF417DetectorResult.h"
36#include "BC_PDF417Codeword.h"
37#include "BC_PDF417Common.h"
38#include "BC_PDF417BarcodeValue.h"
39#include "BC_PDF417BarcodeMetadata.h"
40#include "BC_PDF417BoundingBox.h"
41#include "BC_PDF417DetectionResultColumn.h"
42#include "BC_PDF417DetectionResultRowIndicatorColumn.h"
43#include "BC_PDF417DetectionResult.h"
44#include "BC_PDF417DecodedBitStreamParser.h"
45#include "BC_PDF417CodewordDecoder.h"
46#include "BC_PDF417DecodedBitStreamParser.h"
47#include "BC_PDF417ECModulusPoly.h"
48#include "BC_PDF417ECModulusGF.h"
49#include "BC_PDF417ECErrorCorrection.h"
50#include "BC_PDF417DecodedBitStreamParser.h"
51#include "BC_PDF417ScanningDecoder.h"
52#include "BC_PDF417Reader.h"
53#define Integer_MAX_VALUE 2147483647
54CBC_PDF417Reader::CBC_PDF417Reader() {}
55CBC_PDF417Reader::~CBC_PDF417Reader() {}
56CFX_ByteString CBC_PDF417Reader::Decode(CBC_BinaryBitmap* image, int32_t& e) {
57  return Decode(image, 0, e);
58}
59CFX_ByteString CBC_PDF417Reader::Decode(CBC_BinaryBitmap* image,
60                                        FX_BOOL multiple,
61                                        int32_t hints,
62                                        int32_t& e) {
63  CFX_ByteString results;
64  CBC_PDF417DetectorResult* detectorResult =
65      CBC_Detector::detect(image, hints, multiple, e);
66  BC_EXCEPTION_CHECK_ReturnValue(e, "");
67  for (int32_t i = 0; i < detectorResult->getPoints()->GetSize(); i++) {
68    CFX_PtrArray* points = (CFX_PtrArray*)detectorResult->getPoints()->GetAt(i);
69    CBC_CommonDecoderResult* ResultTemp = CBC_PDF417ScanningDecoder::decode(
70        detectorResult->getBits(), (CBC_ResultPoint*)points->GetAt(4),
71        (CBC_ResultPoint*)points->GetAt(5), (CBC_ResultPoint*)points->GetAt(6),
72        (CBC_ResultPoint*)points->GetAt(7), getMinCodewordWidth(*points),
73        getMaxCodewordWidth(*points), e);
74    if (ResultTemp == NULL) {
75      delete detectorResult;
76      e = BCExceptiontNotFoundInstance;
77      return "";
78    }
79    results += ResultTemp->GetText();
80    delete ResultTemp;
81  }
82  delete detectorResult;
83  return results;
84}
85CFX_ByteString CBC_PDF417Reader::Decode(CBC_BinaryBitmap* image,
86                                        int32_t hints,
87                                        int32_t& e) {
88  CFX_ByteString bs = Decode(image, FALSE, 0, e);
89  BC_EXCEPTION_CHECK_ReturnValue(e, "");
90  return bs;
91}
92int32_t CBC_PDF417Reader::getMaxWidth(CBC_ResultPoint* p1,
93                                      CBC_ResultPoint* p2) {
94  if (p1 == NULL || p2 == NULL) {
95    return 0;
96  }
97  return (int32_t)FXSYS_fabs(p1->GetX() - p2->GetX());
98}
99int32_t CBC_PDF417Reader::getMinWidth(CBC_ResultPoint* p1,
100                                      CBC_ResultPoint* p2) {
101  if (p1 == NULL || p2 == NULL) {
102    return Integer_MAX_VALUE;
103  }
104  return (int32_t)FXSYS_fabs(p1->GetX() - p2->GetX());
105}
106int32_t CBC_PDF417Reader::getMaxCodewordWidth(CFX_PtrArray& p) {
107  int32_t a =
108      getMaxWidth((CBC_ResultPoint*)p.GetAt(6), (CBC_ResultPoint*)p.GetAt(2)) *
109      CBC_PDF417Common::MODULES_IN_CODEWORD /
110      CBC_PDF417Common::MODULES_IN_STOP_PATTERN;
111  int32_t b =
112      getMaxWidth((CBC_ResultPoint*)p.GetAt(7), (CBC_ResultPoint*)p.GetAt(3)) *
113      CBC_PDF417Common::MODULES_IN_CODEWORD /
114      CBC_PDF417Common::MODULES_IN_STOP_PATTERN;
115  int32_t c = getMaxWidth((CBC_ResultPoint*)p.GetAt(0),
116                          (CBC_ResultPoint*)p.GetAt(4)) < a
117                  ? getMaxWidth((CBC_ResultPoint*)p.GetAt(0),
118                                (CBC_ResultPoint*)p.GetAt(4))
119                  : a;
120  int32_t d = getMaxWidth((CBC_ResultPoint*)p.GetAt(1),
121                          (CBC_ResultPoint*)p.GetAt(5)) < b
122                  ? getMaxWidth((CBC_ResultPoint*)p.GetAt(1),
123                                (CBC_ResultPoint*)p.GetAt(5))
124                  : b;
125  return c < d ? c : d;
126}
127int32_t CBC_PDF417Reader::getMinCodewordWidth(CFX_PtrArray& p) {
128  int32_t a =
129      getMinWidth((CBC_ResultPoint*)p.GetAt(6), (CBC_ResultPoint*)p.GetAt(2)) *
130      CBC_PDF417Common::MODULES_IN_CODEWORD /
131      CBC_PDF417Common::MODULES_IN_STOP_PATTERN;
132  int32_t b =
133      getMinWidth((CBC_ResultPoint*)p.GetAt(7), (CBC_ResultPoint*)p.GetAt(3)) *
134      CBC_PDF417Common::MODULES_IN_CODEWORD /
135      CBC_PDF417Common::MODULES_IN_STOP_PATTERN;
136  int32_t c = getMinWidth((CBC_ResultPoint*)p.GetAt(0),
137                          (CBC_ResultPoint*)p.GetAt(4)) < a
138                  ? getMinWidth((CBC_ResultPoint*)p.GetAt(0),
139                                (CBC_ResultPoint*)p.GetAt(4))
140                  : a;
141  int32_t d = getMinWidth((CBC_ResultPoint*)p.GetAt(1),
142                          (CBC_ResultPoint*)p.GetAt(5)) < b
143                  ? getMinWidth((CBC_ResultPoint*)p.GetAt(1),
144                                (CBC_ResultPoint*)p.GetAt(5))
145                  : b;
146  return c < d ? c : d;
147}
148