Merge XFA to PDFium master at 4dc95e7 on 10/28/2014
[pdfium.git] / xfa / src / fxbarcode / src / BC_PDF417Reader.cpp
1 // Copyright 2014 PDFium Authors. All rights reserved.\r
2 // Use of this source code is governed by a BSD-style license that can be\r
3 // found in the LICENSE file.\r
4 \r
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com\r
6 \r
7 #include "barcode.h"\r
8 #include "include/BC_Reader.h"\r
9 #include "include/BC_BinaryBitmap.h"\r
10 #include "include/BC_ResultPoint.h"\r
11 #include "include/BC_PDF417DetectorResult.h"\r
12 #include "include/BC_BinaryBitmap.h"\r
13 #include "include/BC_CommonBitMatrix.h"\r
14 #include "include/BC_CommonBitArray.h"\r
15 #include "include/BC_PDF417Detector.h"\r
16 #include "include/BC_PDF417DetectorResult.h"\r
17 #include "include/BC_DecoderResult.h"\r
18 #include "include/BC_PDF417Codeword.h"\r
19 #include "include/BC_CommonBitMatrix.h"\r
20 #include "include/BC_PDF417Common.h"\r
21 #include "include/BC_PDF417BarcodeValue.h"\r
22 #include "include/BC_PDF417BarcodeMetadata.h"\r
23 #include "include/BC_PDF417BoundingBox.h"\r
24 #include "include/BC_PDF417DetectionResultColumn.h"\r
25 #include "include/BC_PDF417DetectionResultRowIndicatorColumn.h"\r
26 #include "include/BC_PDF417DetectionResult.h"\r
27 #include "include/BC_PDF417DecodedBitStreamParser.h"\r
28 #include "include/BC_PDF417CodewordDecoder.h"\r
29 #include "include/BC_PDF417DecodedBitStreamParser.h"\r
30 #include "include/BC_PDF417ECModulusPoly.h"\r
31 #include "include/BC_PDF417ECModulusGF.h"\r
32 #include "include/BC_PDF417ECErrorCorrection.h"\r
33 #include "include/BC_PDF417DecodedBitStreamParser.h"\r
34 #include "include/BC_CommonDecoderResult.h"\r
35 #include "include/BC_PDF417ScanningDecoder.h"\r
36 #include "include/BC_PDF417Reader.h"\r
37 #define    Integer_MAX_VALUE   2147483647\r
38 CBC_PDF417Reader::CBC_PDF417Reader()\r
39 {\r
40 }\r
41 CBC_PDF417Reader::~CBC_PDF417Reader()\r
42 {\r
43 }\r
44 CFX_ByteString CBC_PDF417Reader::Decode(CBC_BinaryBitmap *image, FX_INT32 &e)\r
45 {\r
46     return Decode(image, 0, e);\r
47 }\r
48 CFX_ByteString CBC_PDF417Reader::Decode(CBC_BinaryBitmap *image, FX_BOOL multiple, FX_INT32 hints, FX_INT32 &e)\r
49 {\r
50     CFX_ByteString results;\r
51     CBC_PDF417DetectorResult* detectorResult = CBC_Detector::detect(image, hints, multiple, e);\r
52     BC_EXCEPTION_CHECK_ReturnValue(e, "");\r
53     for (FX_INT32 i = 0; i < detectorResult->getPoints()->GetSize(); i++) {\r
54         CFX_PtrArray* points = (CFX_PtrArray*)detectorResult->getPoints()->GetAt(i);\r
55         CBC_CommonDecoderResult* ResultTemp = CBC_PDF417ScanningDecoder::decode(detectorResult->getBits(), (CBC_ResultPoint*)points->GetAt(4), (CBC_ResultPoint*)points->GetAt(5),\r
56                                               (CBC_ResultPoint*)points->GetAt(6), (CBC_ResultPoint*)points->GetAt(7), getMinCodewordWidth(*points), getMaxCodewordWidth(*points), e);\r
57         if (ResultTemp == NULL) {\r
58             delete detectorResult;\r
59             e = BCExceptiontNotFoundInstance;\r
60             return "";\r
61         }\r
62         results += ResultTemp->GetText();\r
63         delete ResultTemp;\r
64     }\r
65     delete detectorResult;\r
66     return results;\r
67 }\r
68 CFX_ByteString CBC_PDF417Reader::Decode(CBC_BinaryBitmap *image, FX_INT32 hints, FX_INT32 &e)\r
69 {\r
70     CFX_ByteString bs = Decode(image, FALSE, 0, e);\r
71     BC_EXCEPTION_CHECK_ReturnValue(e, "");\r
72     return bs;\r
73 }\r
74 FX_INT32 CBC_PDF417Reader::getMaxWidth(CBC_ResultPoint* p1, CBC_ResultPoint* p2)\r
75 {\r
76     if (p1 == NULL || p2 == NULL) {\r
77         return 0;\r
78     }\r
79     return (FX_INT32) FXSYS_fabs(p1->GetX() - p2->GetX());\r
80 }\r
81 FX_INT32 CBC_PDF417Reader::getMinWidth(CBC_ResultPoint* p1, CBC_ResultPoint* p2)\r
82 {\r
83     if (p1 == NULL || p2 == NULL) {\r
84         return Integer_MAX_VALUE;\r
85     }\r
86     return (FX_INT32) FXSYS_fabs(p1->GetX() - p2->GetX());\r
87 }\r
88 FX_INT32 CBC_PDF417Reader::getMaxCodewordWidth(CFX_PtrArray& p)\r
89 {\r
90     FX_INT32 a = getMaxWidth((CBC_ResultPoint*)p.GetAt(6), (CBC_ResultPoint*)p.GetAt(2)) * CBC_PDF417Common::MODULES_IN_CODEWORD / CBC_PDF417Common::MODULES_IN_STOP_PATTERN;\r
91     FX_INT32 b = getMaxWidth((CBC_ResultPoint*)p.GetAt(7), (CBC_ResultPoint*)p.GetAt(3)) * CBC_PDF417Common::MODULES_IN_CODEWORD / CBC_PDF417Common::MODULES_IN_STOP_PATTERN;\r
92     FX_INT32 c = getMaxWidth((CBC_ResultPoint*)p.GetAt(0), (CBC_ResultPoint*)p.GetAt(4)) < a ? getMaxWidth((CBC_ResultPoint*)p.GetAt(0), (CBC_ResultPoint*)p.GetAt(4)) : a;\r
93     FX_INT32 d = getMaxWidth((CBC_ResultPoint*)p.GetAt(1), (CBC_ResultPoint*)p.GetAt(5)) < b ? getMaxWidth((CBC_ResultPoint*)p.GetAt(1), (CBC_ResultPoint*)p.GetAt(5)) : b;\r
94     return c < d ? c : d;\r
95 }\r
96 FX_INT32 CBC_PDF417Reader::getMinCodewordWidth(CFX_PtrArray& p)\r
97 {\r
98     FX_INT32 a = getMinWidth((CBC_ResultPoint*)p.GetAt(6), (CBC_ResultPoint*)p.GetAt(2)) * CBC_PDF417Common::MODULES_IN_CODEWORD / CBC_PDF417Common::MODULES_IN_STOP_PATTERN;\r
99     FX_INT32 b = getMinWidth((CBC_ResultPoint*)p.GetAt(7), (CBC_ResultPoint*)p.GetAt(3)) * CBC_PDF417Common::MODULES_IN_CODEWORD / CBC_PDF417Common::MODULES_IN_STOP_PATTERN;\r
100     FX_INT32 c = getMinWidth((CBC_ResultPoint*)p.GetAt(0), (CBC_ResultPoint*)p.GetAt(4)) < a ? getMinWidth((CBC_ResultPoint*)p.GetAt(0), (CBC_ResultPoint*)p.GetAt(4)) : a;\r
101     FX_INT32 d = getMinWidth((CBC_ResultPoint*)p.GetAt(1), (CBC_ResultPoint*)p.GetAt(5)) < b ? getMinWidth((CBC_ResultPoint*)p.GetAt(1), (CBC_ResultPoint*)p.GetAt(5)) : b;\r
102     return c < d ? c : d;\r
103 }\r