| OLD | NEW |
| (Empty) |
| 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/qrcode/BC_QRCoderDecoder.h" | |
| 24 | |
| 25 #include <memory> | |
| 26 | |
| 27 #include "xfa/src/fxbarcode/common/BC_CommonBitMatrix.h" | |
| 28 #include "xfa/src/fxbarcode/common/BC_CommonDecoderResult.h" | |
| 29 #include "xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.h" | |
| 30 #include "xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h" | |
| 31 #include "xfa/src/fxbarcode/qrcode/BC_QRBitMatrixParser.h" | |
| 32 #include "xfa/src/fxbarcode/qrcode/BC_QRCoderFormatInformation.h" | |
| 33 #include "xfa/src/fxbarcode/qrcode/BC_QRCoderVersion.h" | |
| 34 #include "xfa/src/fxbarcode/qrcode/BC_QRDataBlock.h" | |
| 35 #include "xfa/src/fxbarcode/qrcode/BC_QRDecodedBitStreamParser.h" | |
| 36 | |
| 37 CBC_QRCoderDecoder::CBC_QRCoderDecoder() { | |
| 38 m_rsDecoder = NULL; | |
| 39 } | |
| 40 | |
| 41 void CBC_QRCoderDecoder::Init() { | |
| 42 m_rsDecoder = new CBC_ReedSolomonDecoder(CBC_ReedSolomonGF256::QRCodeFild); | |
| 43 } | |
| 44 CBC_QRCoderDecoder::~CBC_QRCoderDecoder() { | |
| 45 delete m_rsDecoder; | |
| 46 } | |
| 47 CBC_CommonDecoderResult* CBC_QRCoderDecoder::Decode(FX_BOOL* image, | |
| 48 int32_t width, | |
| 49 int32_t height, | |
| 50 int32_t& e) { | |
| 51 CBC_CommonBitMatrix bits; | |
| 52 bits.Init(width); | |
| 53 for (int32_t i = 0; i < width; i++) { | |
| 54 for (int32_t j = 0; j < height; j++) { | |
| 55 if (image[i * width + j]) { | |
| 56 bits.Set(j, i); | |
| 57 } | |
| 58 } | |
| 59 } | |
| 60 CBC_CommonDecoderResult* cdr = Decode(&bits, height, e); | |
| 61 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 62 return cdr; | |
| 63 } | |
| 64 CBC_CommonDecoderResult* CBC_QRCoderDecoder::Decode(CBC_CommonBitMatrix* bits, | |
| 65 int32_t byteModeDecode, | |
| 66 int32_t& e) { | |
| 67 CBC_QRBitMatrixParser parser; | |
| 68 parser.Init(bits, e); | |
| 69 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 70 CBC_QRCoderVersion* version = parser.ReadVersion(e); | |
| 71 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 72 CBC_QRCoderFormatInformation* temp = parser.ReadFormatInformation(e); | |
| 73 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 74 CBC_QRCoderErrorCorrectionLevel* ecLevel = temp->GetErrorCorrectionLevel(); | |
| 75 std::unique_ptr<CFX_ByteArray> codewords(parser.ReadCodewords(e)); | |
| 76 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 77 CFX_PtrArray* dataBlocks = | |
| 78 CBC_QRDataBlock::GetDataBlocks(codewords.get(), version, ecLevel, e); | |
| 79 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 80 int32_t totalBytes = 0; | |
| 81 for (int32_t i = 0; i < dataBlocks->GetSize(); i++) { | |
| 82 totalBytes += ((CBC_QRDataBlock*)((*dataBlocks)[i]))->GetNumDataCodewords(); | |
| 83 } | |
| 84 CFX_ByteArray resultBytes; | |
| 85 for (int32_t j = 0; j < dataBlocks->GetSize(); j++) { | |
| 86 CBC_QRDataBlock* dataBlock = (CBC_QRDataBlock*)((*dataBlocks)[j]); | |
| 87 CFX_ByteArray* codewordBytes = dataBlock->GetCodewords(); | |
| 88 int32_t numDataCodewords = dataBlock->GetNumDataCodewords(); | |
| 89 CorrectErrors(codewordBytes, numDataCodewords, e); | |
| 90 if (e != BCExceptionNO) { | |
| 91 for (int32_t k = 0; k < dataBlocks->GetSize(); k++) { | |
| 92 delete (CBC_QRDataBlock*)(*dataBlocks)[k]; | |
| 93 } | |
| 94 dataBlocks->RemoveAll(); | |
| 95 delete dataBlocks; | |
| 96 dataBlocks = NULL; | |
| 97 return NULL; | |
| 98 } | |
| 99 for (int32_t i = 0; i < numDataCodewords; i++) { | |
| 100 resultBytes.Add((*codewordBytes)[i]); | |
| 101 } | |
| 102 } | |
| 103 for (int32_t k = 0; k < dataBlocks->GetSize(); k++) { | |
| 104 delete (CBC_QRDataBlock*)(*dataBlocks)[k]; | |
| 105 } | |
| 106 dataBlocks->RemoveAll(); | |
| 107 delete dataBlocks; | |
| 108 dataBlocks = NULL; | |
| 109 CBC_CommonDecoderResult* cdr = CBC_QRDecodedBitStreamParser::Decode( | |
| 110 &resultBytes, version, ecLevel, byteModeDecode, e); | |
| 111 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 112 return cdr; | |
| 113 } | |
| 114 void CBC_QRCoderDecoder::CorrectErrors(CFX_ByteArray* codewordBytes, | |
| 115 int32_t numDataCodewords, | |
| 116 int32_t& e) { | |
| 117 int32_t numCodewords = codewordBytes->GetSize(); | |
| 118 CFX_Int32Array codewordsInts; | |
| 119 codewordsInts.SetSize(numCodewords); | |
| 120 for (int32_t i = 0; i < numCodewords; i++) { | |
| 121 codewordsInts[i] = (int32_t)((*codewordBytes)[i] & 0xff); | |
| 122 } | |
| 123 int32_t numECCodewords = codewordBytes->GetSize() - numDataCodewords; | |
| 124 m_rsDecoder->Decode(&codewordsInts, numECCodewords, e); | |
| 125 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
| 126 for (int32_t k = 0; k < numDataCodewords; k++) { | |
| 127 (*codewordBytes)[k] = (uint8_t)codewordsInts[k]; | |
| 128 } | |
| 129 } | |
| OLD | NEW |