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/common/BC_CommonBitMatrix.h" 25 #include "xfa/src/fxbarcode/common/BC_CommonDecoderResult.h" 26 #include "xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.h" 27 #include "xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h" 28 #include "BC_QRBitMatrixParser.h" 29 #include "BC_QRDataBlock.h" 30 #include "BC_QRDecodedBitStreamParser.h" 31 #include "BC_QRCoderVersion.h" 32 #include "BC_QRCoderFormatInformation.h" 33 #include "BC_QRCoderDecoder.h" 34 CBC_QRCoderDecoder::CBC_QRCoderDecoder() { 35 m_rsDecoder = NULL; 36 } 37 38 void CBC_QRCoderDecoder::Init() { 39 m_rsDecoder = new CBC_ReedSolomonDecoder(CBC_ReedSolomonGF256::QRCodeFild); 40 } 41 CBC_QRCoderDecoder::~CBC_QRCoderDecoder() { 42 if (m_rsDecoder != NULL) { 43 delete m_rsDecoder; 44 } 45 m_rsDecoder = NULL; 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 CFX_ByteArray* ba = parser.ReadCodewords(e); 76 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 77 CBC_AutoPtr<CFX_ByteArray> codewords(ba); 78 CFX_PtrArray* dataBlocks = 79 CBC_QRDataBlock::GetDataBlocks(codewords.get(), version, ecLevel, e); 80 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 81 int32_t totalBytes = 0; 82 for (int32_t i = 0; i < dataBlocks->GetSize(); i++) { 83 totalBytes += ((CBC_QRDataBlock*)((*dataBlocks)[i]))->GetNumDataCodewords(); 84 } 85 CFX_ByteArray resultBytes; 86 for (int32_t j = 0; j < dataBlocks->GetSize(); j++) { 87 CBC_QRDataBlock* dataBlock = (CBC_QRDataBlock*)((*dataBlocks)[j]); 88 CFX_ByteArray* codewordBytes = dataBlock->GetCodewords(); 89 int32_t numDataCodewords = dataBlock->GetNumDataCodewords(); 90 CorrectErrors(codewordBytes, numDataCodewords, e); 91 if (e != BCExceptionNO) { 92 for (int32_t k = 0; k < dataBlocks->GetSize(); k++) { 93 delete (CBC_QRDataBlock*)(*dataBlocks)[k]; 94 } 95 dataBlocks->RemoveAll(); 96 delete dataBlocks; 97 dataBlocks = NULL; 98 return NULL; 99 } 100 for (int32_t i = 0; i < numDataCodewords; i++) { 101 resultBytes.Add((*codewordBytes)[i]); 102 } 103 } 104 for (int32_t k = 0; k < dataBlocks->GetSize(); k++) { 105 delete (CBC_QRDataBlock*)(*dataBlocks)[k]; 106 } 107 dataBlocks->RemoveAll(); 108 delete dataBlocks; 109 dataBlocks = NULL; 110 CBC_CommonDecoderResult* cdr = CBC_QRDecodedBitStreamParser::Decode( 111 &resultBytes, version, ecLevel, byteModeDecode, e); 112 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 113 return cdr; 114 } 115 void CBC_QRCoderDecoder::CorrectErrors(CFX_ByteArray* codewordBytes, 116 int32_t numDataCodewords, 117 int32_t& e) { 118 int32_t numCodewords = codewordBytes->GetSize(); 119 CFX_Int32Array codewordsInts; 120 codewordsInts.SetSize(numCodewords); 121 for (int32_t i = 0; i < numCodewords; i++) { 122 codewordsInts[i] = (int32_t)((*codewordBytes)[i] & 0xff); 123 } 124 int32_t numECCodewords = codewordBytes->GetSize() - numDataCodewords; 125 m_rsDecoder->Decode(&codewordsInts, numECCodewords, e); 126 BC_EXCEPTION_CHECK_ReturnVoid(e); 127 for (int32_t k = 0; k < numDataCodewords; k++) { 128 (*codewordBytes)[k] = (uint8_t)codewordsInts[k]; 129 } 130 } 131