| 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 2007 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 "barcode.h" | |
| 24 #include "include/BC_DataMatrixDecoder.h" | |
| 25 #include "include/BC_ReedSolomonDecoder.h" | |
| 26 #include "include/BC_ReedSolomonGF256.h" | |
| 27 #include "include/BC_CommonBitMatrix.h" | |
| 28 #include "include/BC_DataMatrixBitMatrixParser.h" | |
| 29 #include "include/BC_DataMatrixVersion.h" | |
| 30 #include "include/BC_DataMatrixDataBlock.h" | |
| 31 #include "include/BC_DataMatrixDecodedBitStreamParser.h" | |
| 32 CBC_DataMatrixDecoder::CBC_DataMatrixDecoder() | |
| 33 { | |
| 34 m_rsDecoder = NULL; | |
| 35 } | |
| 36 void CBC_DataMatrixDecoder::Init() | |
| 37 { | |
| 38 m_rsDecoder = FX_NEW CBC_ReedSolomonDecoder(CBC_ReedSolomonGF256::DataMatrix
Field); | |
| 39 } | |
| 40 CBC_DataMatrixDecoder::~CBC_DataMatrixDecoder() | |
| 41 { | |
| 42 if(m_rsDecoder != NULL) { | |
| 43 delete m_rsDecoder; | |
| 44 } | |
| 45 m_rsDecoder = NULL; | |
| 46 } | |
| 47 CBC_CommonDecoderResult *CBC_DataMatrixDecoder::Decode(CBC_CommonBitMatrix *bits
, FX_INT32 &e) | |
| 48 { | |
| 49 CBC_DataMatrixBitMatrixParser parser; | |
| 50 parser.Init(bits, e); | |
| 51 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 52 CBC_DataMatrixVersion *version = parser.GetVersion(); | |
| 53 CFX_ByteArray* byteTemp = parser.ReadCodewords(e); | |
| 54 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 55 CBC_AutoPtr<CFX_ByteArray> codewords(byteTemp); | |
| 56 CFX_PtrArray *dataBlocks = CBC_DataMatrixDataBlock::GetDataBlocks(codewords.
get(), version, e); | |
| 57 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 58 FX_INT32 dataBlocksCount = dataBlocks->GetSize(); | |
| 59 FX_INT32 totalBytes = 0; | |
| 60 FX_INT32 i, j; | |
| 61 for (i = 0; i < dataBlocksCount; i++) { | |
| 62 totalBytes += ((CBC_DataMatrixDataBlock*)(*dataBlocks)[i])->GetNumDataCo
dewords(); | |
| 63 } | |
| 64 CFX_ByteArray resultBytes; | |
| 65 resultBytes.SetSize(totalBytes); | |
| 66 for (j = 0; j < dataBlocksCount; j++) { | |
| 67 CFX_ByteArray *codewordBytes = ((CBC_DataMatrixDataBlock*)(*dataBlocks)[
j])->GetCodewords(); | |
| 68 FX_INT32 numDataCodewords = ((CBC_DataMatrixDataBlock*)(*dataBlocks)[j])
->GetNumDataCodewords(); | |
| 69 CorrectErrors(*codewordBytes, numDataCodewords, e); | |
| 70 if (e != BCExceptionNO) { | |
| 71 for(FX_INT32 i = 0; i < dataBlocks->GetSize(); i++) { | |
| 72 delete (CBC_DataMatrixDataBlock*)(*dataBlocks)[i]; | |
| 73 } | |
| 74 delete dataBlocks; | |
| 75 dataBlocks = NULL; | |
| 76 return NULL; | |
| 77 } | |
| 78 FX_INT32 i; | |
| 79 for (i = 0; i < numDataCodewords; i++) { | |
| 80 resultBytes[i * dataBlocksCount + j] = (*codewordBytes)[i]; | |
| 81 } | |
| 82 } | |
| 83 for(i = 0; i < (dataBlocks->GetSize()); i++) { | |
| 84 delete (CBC_DataMatrixDataBlock*)(*dataBlocks)[i]; | |
| 85 } | |
| 86 delete dataBlocks; | |
| 87 dataBlocks = NULL; | |
| 88 CBC_CommonDecoderResult *resultR = CBC_DataMatrixDecodedBitStreamParser::Dec
ode(resultBytes, e); | |
| 89 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 90 return resultR; | |
| 91 } | |
| 92 void CBC_DataMatrixDecoder::CorrectErrors(CFX_ByteArray &codewordBytes, FX_INT32
numDataCodewords, FX_INT32 &e) | |
| 93 { | |
| 94 FX_INT32 numCodewords = codewordBytes.GetSize(); | |
| 95 CFX_Int32Array codewordsInts; | |
| 96 codewordsInts.SetSize(numCodewords); | |
| 97 FX_INT32 i; | |
| 98 for (i = 0; i < numCodewords; i++) { | |
| 99 codewordsInts[i] = codewordBytes[i] & 0xFF; | |
| 100 } | |
| 101 FX_INT32 numECCodewords = codewordBytes.GetSize() - numDataCodewords; | |
| 102 m_rsDecoder->Decode(&codewordsInts, numECCodewords, e); | |
| 103 if (e != BCExceptionNO) { | |
| 104 e = BCExceptionChecksumException; | |
| 105 return ; | |
| 106 } | |
| 107 for (i = 0; i < numDataCodewords; i++) { | |
| 108 codewordBytes[i] = (FX_BYTE) codewordsInts[i]; | |
| 109 } | |
| 110 } | |
| OLD | NEW |