OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 // Original code is licensed as follows: | 6 // Original code is licensed as follows: |
7 /* | 7 /* |
8 * Copyright 2008 ZXing authors | 8 * Copyright 2008 ZXing authors |
9 * | 9 * |
10 * Licensed under the Apache License, Version 2.0 (the "License"); | 10 * Licensed under the Apache License, Version 2.0 (the "License"); |
(...skipping 17 matching lines...) Expand all Loading... |
28 #include "xfa/fxbarcode/common/BC_CommonDecoderResult.h" | 28 #include "xfa/fxbarcode/common/BC_CommonDecoderResult.h" |
29 #include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.h" | 29 #include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.h" |
30 #include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h" | 30 #include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h" |
31 #include "xfa/fxbarcode/qrcode/BC_QRBitMatrixParser.h" | 31 #include "xfa/fxbarcode/qrcode/BC_QRBitMatrixParser.h" |
32 #include "xfa/fxbarcode/qrcode/BC_QRCoderFormatInformation.h" | 32 #include "xfa/fxbarcode/qrcode/BC_QRCoderFormatInformation.h" |
33 #include "xfa/fxbarcode/qrcode/BC_QRCoderVersion.h" | 33 #include "xfa/fxbarcode/qrcode/BC_QRCoderVersion.h" |
34 #include "xfa/fxbarcode/qrcode/BC_QRDataBlock.h" | 34 #include "xfa/fxbarcode/qrcode/BC_QRDataBlock.h" |
35 #include "xfa/fxbarcode/qrcode/BC_QRDecodedBitStreamParser.h" | 35 #include "xfa/fxbarcode/qrcode/BC_QRDecodedBitStreamParser.h" |
36 | 36 |
37 CBC_QRCoderDecoder::CBC_QRCoderDecoder() { | 37 CBC_QRCoderDecoder::CBC_QRCoderDecoder() { |
38 m_rsDecoder = NULL; | 38 m_rsDecoder = nullptr; |
39 } | 39 } |
40 | 40 |
41 void CBC_QRCoderDecoder::Init() { | 41 void CBC_QRCoderDecoder::Init() { |
42 m_rsDecoder = new CBC_ReedSolomonDecoder(CBC_ReedSolomonGF256::QRCodeFild); | 42 m_rsDecoder = new CBC_ReedSolomonDecoder(CBC_ReedSolomonGF256::QRCodeFild); |
43 } | 43 } |
44 CBC_QRCoderDecoder::~CBC_QRCoderDecoder() { | 44 CBC_QRCoderDecoder::~CBC_QRCoderDecoder() { |
45 delete m_rsDecoder; | 45 delete m_rsDecoder; |
46 } | 46 } |
47 CBC_CommonDecoderResult* CBC_QRCoderDecoder::Decode(FX_BOOL* image, | 47 CBC_CommonDecoderResult* CBC_QRCoderDecoder::Decode(FX_BOOL* image, |
48 int32_t width, | 48 int32_t width, |
49 int32_t height, | 49 int32_t height, |
50 int32_t& e) { | 50 int32_t& e) { |
51 CBC_CommonBitMatrix bits; | 51 CBC_CommonBitMatrix bits; |
52 bits.Init(width); | 52 bits.Init(width); |
53 for (int32_t i = 0; i < width; i++) { | 53 for (int32_t i = 0; i < width; i++) { |
54 for (int32_t j = 0; j < height; j++) { | 54 for (int32_t j = 0; j < height; j++) { |
55 if (image[i * width + j]) { | 55 if (image[i * width + j]) { |
56 bits.Set(j, i); | 56 bits.Set(j, i); |
57 } | 57 } |
58 } | 58 } |
59 } | 59 } |
60 CBC_CommonDecoderResult* cdr = Decode(&bits, height, e); | 60 CBC_CommonDecoderResult* cdr = Decode(&bits, height, e); |
61 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 61 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr); |
62 return cdr; | 62 return cdr; |
63 } | 63 } |
64 CBC_CommonDecoderResult* CBC_QRCoderDecoder::Decode(CBC_CommonBitMatrix* bits, | 64 CBC_CommonDecoderResult* CBC_QRCoderDecoder::Decode(CBC_CommonBitMatrix* bits, |
65 int32_t byteModeDecode, | 65 int32_t byteModeDecode, |
66 int32_t& e) { | 66 int32_t& e) { |
67 CBC_QRBitMatrixParser parser; | 67 CBC_QRBitMatrixParser parser; |
68 parser.Init(bits, e); | 68 parser.Init(bits, e); |
69 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 69 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr); |
70 CBC_QRCoderVersion* version = parser.ReadVersion(e); | 70 CBC_QRCoderVersion* version = parser.ReadVersion(e); |
71 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 71 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr); |
72 CBC_QRCoderFormatInformation* temp = parser.ReadFormatInformation(e); | 72 CBC_QRCoderFormatInformation* temp = parser.ReadFormatInformation(e); |
73 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 73 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr); |
74 CBC_QRCoderErrorCorrectionLevel* ecLevel = temp->GetErrorCorrectionLevel(); | 74 CBC_QRCoderErrorCorrectionLevel* ecLevel = temp->GetErrorCorrectionLevel(); |
75 std::unique_ptr<CFX_ByteArray> codewords(parser.ReadCodewords(e)); | 75 std::unique_ptr<CFX_ByteArray> codewords(parser.ReadCodewords(e)); |
76 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 76 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr); |
77 CFX_ArrayTemplate<CBC_QRDataBlock*>* dataBlocks = | 77 CFX_ArrayTemplate<CBC_QRDataBlock*>* dataBlocks = |
78 CBC_QRDataBlock::GetDataBlocks(codewords.get(), version, ecLevel, e); | 78 CBC_QRDataBlock::GetDataBlocks(codewords.get(), version, ecLevel, e); |
79 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 79 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr); |
80 int32_t totalBytes = 0; | 80 int32_t totalBytes = 0; |
81 for (int32_t i = 0; i < dataBlocks->GetSize(); i++) { | 81 for (int32_t i = 0; i < dataBlocks->GetSize(); i++) { |
82 totalBytes += (*dataBlocks)[i]->GetNumDataCodewords(); | 82 totalBytes += (*dataBlocks)[i]->GetNumDataCodewords(); |
83 } | 83 } |
84 CFX_ByteArray resultBytes; | 84 CFX_ByteArray resultBytes; |
85 for (int32_t j = 0; j < dataBlocks->GetSize(); j++) { | 85 for (int32_t j = 0; j < dataBlocks->GetSize(); j++) { |
86 CBC_QRDataBlock* dataBlock = (*dataBlocks)[j]; | 86 CBC_QRDataBlock* dataBlock = (*dataBlocks)[j]; |
87 CFX_ByteArray* codewordBytes = dataBlock->GetCodewords(); | 87 CFX_ByteArray* codewordBytes = dataBlock->GetCodewords(); |
88 int32_t numDataCodewords = dataBlock->GetNumDataCodewords(); | 88 int32_t numDataCodewords = dataBlock->GetNumDataCodewords(); |
89 CorrectErrors(codewordBytes, numDataCodewords, e); | 89 CorrectErrors(codewordBytes, numDataCodewords, e); |
90 if (e != BCExceptionNO) { | 90 if (e != BCExceptionNO) { |
91 for (int32_t k = 0; k < dataBlocks->GetSize(); k++) { | 91 for (int32_t k = 0; k < dataBlocks->GetSize(); k++) { |
92 delete (*dataBlocks)[k]; | 92 delete (*dataBlocks)[k]; |
93 } | 93 } |
94 delete dataBlocks; | 94 delete dataBlocks; |
95 return NULL; | 95 return nullptr; |
96 } | 96 } |
97 for (int32_t i = 0; i < numDataCodewords; i++) { | 97 for (int32_t i = 0; i < numDataCodewords; i++) { |
98 resultBytes.Add((*codewordBytes)[i]); | 98 resultBytes.Add((*codewordBytes)[i]); |
99 } | 99 } |
100 } | 100 } |
101 for (int32_t k = 0; k < dataBlocks->GetSize(); k++) { | 101 for (int32_t k = 0; k < dataBlocks->GetSize(); k++) { |
102 delete (*dataBlocks)[k]; | 102 delete (*dataBlocks)[k]; |
103 } | 103 } |
104 delete dataBlocks; | 104 delete dataBlocks; |
105 CBC_CommonDecoderResult* cdr = CBC_QRDecodedBitStreamParser::Decode( | 105 CBC_CommonDecoderResult* cdr = CBC_QRDecodedBitStreamParser::Decode( |
106 &resultBytes, version, ecLevel, byteModeDecode, e); | 106 &resultBytes, version, ecLevel, byteModeDecode, e); |
107 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 107 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr); |
108 return cdr; | 108 return cdr; |
109 } | 109 } |
110 void CBC_QRCoderDecoder::CorrectErrors(CFX_ByteArray* codewordBytes, | 110 void CBC_QRCoderDecoder::CorrectErrors(CFX_ByteArray* codewordBytes, |
111 int32_t numDataCodewords, | 111 int32_t numDataCodewords, |
112 int32_t& e) { | 112 int32_t& e) { |
113 int32_t numCodewords = codewordBytes->GetSize(); | 113 int32_t numCodewords = codewordBytes->GetSize(); |
114 CFX_Int32Array codewordsInts; | 114 CFX_Int32Array codewordsInts; |
115 codewordsInts.SetSize(numCodewords); | 115 codewordsInts.SetSize(numCodewords); |
116 for (int32_t i = 0; i < numCodewords; i++) { | 116 for (int32_t i = 0; i < numCodewords; i++) { |
117 codewordsInts[i] = (int32_t)((*codewordBytes)[i] & 0xff); | 117 codewordsInts[i] = (int32_t)((*codewordBytes)[i] & 0xff); |
118 } | 118 } |
119 int32_t numECCodewords = codewordBytes->GetSize() - numDataCodewords; | 119 int32_t numECCodewords = codewordBytes->GetSize() - numDataCodewords; |
120 m_rsDecoder->Decode(&codewordsInts, numECCodewords, e); | 120 m_rsDecoder->Decode(&codewordsInts, numECCodewords, e); |
121 BC_EXCEPTION_CHECK_ReturnVoid(e); | 121 BC_EXCEPTION_CHECK_ReturnVoid(e); |
122 for (int32_t k = 0; k < numDataCodewords; k++) { | 122 for (int32_t k = 0; k < numDataCodewords; k++) { |
123 (*codewordBytes)[k] = (uint8_t)codewordsInts[k]; | 123 (*codewordBytes)[k] = (uint8_t)codewordsInts[k]; |
124 } | 124 } |
125 } | 125 } |
OLD | NEW |