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
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
\r
6 // Original code is licensed as follows:
\r
8 * Copyright 2007 ZXing authors
\r
10 * Licensed under the Apache License, Version 2.0 (the "License");
\r
11 * you may not use this file except in compliance with the License.
\r
12 * You may obtain a copy of the License at
\r
14 * http://www.apache.org/licenses/LICENSE-2.0
\r
16 * Unless required by applicable law or agreed to in writing, software
\r
17 * distributed under the License is distributed on an "AS IS" BASIS,
\r
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
19 * See the License for the specific language governing permissions and
\r
20 * limitations under the License.
\r
23 #include "../barcode.h"
\r
24 #include "../common/BC_CommonBitMatrix.h"
\r
25 #include "../common/reedsolomon/BC_ReedSolomonDecoder.h"
\r
26 #include "../common/reedsolomon/BC_ReedSolomonGF256.h"
\r
27 #include "BC_DataMatrixDecoder.h"
\r
28 #include "BC_DataMatrixBitMatrixParser.h"
\r
29 #include "BC_DataMatrixVersion.h"
\r
30 #include "BC_DataMatrixDataBlock.h"
\r
31 #include "BC_DataMatrixDecodedBitStreamParser.h"
\r
32 CBC_DataMatrixDecoder::CBC_DataMatrixDecoder()
\r
36 void CBC_DataMatrixDecoder::Init()
\r
38 m_rsDecoder = FX_NEW CBC_ReedSolomonDecoder(CBC_ReedSolomonGF256::DataMatrixField);
\r
40 CBC_DataMatrixDecoder::~CBC_DataMatrixDecoder()
\r
42 if(m_rsDecoder != NULL) {
\r
47 CBC_CommonDecoderResult *CBC_DataMatrixDecoder::Decode(CBC_CommonBitMatrix *bits, FX_INT32 &e)
\r
49 CBC_DataMatrixBitMatrixParser parser;
\r
50 parser.Init(bits, e);
\r
51 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
\r
52 CBC_DataMatrixVersion *version = parser.GetVersion();
\r
53 CFX_ByteArray* byteTemp = parser.ReadCodewords(e);
\r
54 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
\r
55 CBC_AutoPtr<CFX_ByteArray> codewords(byteTemp);
\r
56 CFX_PtrArray *dataBlocks = CBC_DataMatrixDataBlock::GetDataBlocks(codewords.get(), version, e);
\r
57 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
\r
58 FX_INT32 dataBlocksCount = dataBlocks->GetSize();
\r
59 FX_INT32 totalBytes = 0;
\r
61 for (i = 0; i < dataBlocksCount; i++) {
\r
62 totalBytes += ((CBC_DataMatrixDataBlock*)(*dataBlocks)[i])->GetNumDataCodewords();
\r
64 CFX_ByteArray resultBytes;
\r
65 resultBytes.SetSize(totalBytes);
\r
66 for (j = 0; j < dataBlocksCount; j++) {
\r
67 CFX_ByteArray *codewordBytes = ((CBC_DataMatrixDataBlock*)(*dataBlocks)[j])->GetCodewords();
\r
68 FX_INT32 numDataCodewords = ((CBC_DataMatrixDataBlock*)(*dataBlocks)[j])->GetNumDataCodewords();
\r
69 CorrectErrors(*codewordBytes, numDataCodewords, e);
\r
70 if (e != BCExceptionNO) {
\r
71 for(FX_INT32 i = 0; i < dataBlocks->GetSize(); i++) {
\r
72 delete (CBC_DataMatrixDataBlock*)(*dataBlocks)[i];
\r
79 for (i = 0; i < numDataCodewords; i++) {
\r
80 resultBytes[i * dataBlocksCount + j] = (*codewordBytes)[i];
\r
83 for(i = 0; i < (dataBlocks->GetSize()); i++) {
\r
84 delete (CBC_DataMatrixDataBlock*)(*dataBlocks)[i];
\r
88 CBC_CommonDecoderResult *resultR = CBC_DataMatrixDecodedBitStreamParser::Decode(resultBytes, e);
\r
89 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
\r
92 void CBC_DataMatrixDecoder::CorrectErrors(CFX_ByteArray &codewordBytes, FX_INT32 numDataCodewords, FX_INT32 &e)
\r
94 FX_INT32 numCodewords = codewordBytes.GetSize();
\r
95 CFX_Int32Array codewordsInts;
\r
96 codewordsInts.SetSize(numCodewords);
\r
98 for (i = 0; i < numCodewords; i++) {
\r
99 codewordsInts[i] = codewordBytes[i] & 0xFF;
\r
101 FX_INT32 numECCodewords = codewordBytes.GetSize() - numDataCodewords;
\r
102 m_rsDecoder->Decode(&codewordsInts, numECCodewords, e);
\r
103 if (e != BCExceptionNO) {
\r
104 e = BCExceptionChecksumException;
\r
107 for (i = 0; i < numDataCodewords; i++) {
\r
108 codewordBytes[i] = (FX_BYTE) codewordsInts[i];
\r