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 "barcode.h" | |
24 #include "include/BC_ReedSolomonDecoder.h" | |
25 #include "include/BC_CommonBitMatrix.h" | |
26 #include "include/BC_CommonDecoderResult.h" | |
27 #include "include/BC_QRBitMatrixParser.h" | |
28 #include "include/BC_QRDataBlock.h" | |
29 #include "include/BC_QRDecodedBitStreamParser.h" | |
30 #include "include/BC_QRCoderVersion.h" | |
31 #include "include/BC_QRCoderFormatInformation.h" | |
32 #include "include/BC_ReedSolomonGF256.h" | |
33 #include "include/BC_QRCoderDecoder.h" | |
34 CBC_QRCoderDecoder::CBC_QRCoderDecoder() | |
35 { | |
36 m_rsDecoder = NULL; | |
37 } | |
38 | |
39 void CBC_QRCoderDecoder::Init() | |
40 { | |
41 m_rsDecoder = FX_NEW CBC_ReedSolomonDecoder(CBC_ReedSolomonGF256::QRCodeFild
); | |
42 } | |
43 CBC_QRCoderDecoder::~CBC_QRCoderDecoder() | |
44 { | |
45 if(m_rsDecoder != NULL) { | |
46 delete m_rsDecoder; | |
47 } | |
48 m_rsDecoder = NULL; | |
49 } | |
50 CBC_CommonDecoderResult* CBC_QRCoderDecoder::Decode(FX_BOOL* image, FX_INT32 wid
th, FX_INT32 height, FX_INT32 &e) | |
51 { | |
52 CBC_CommonBitMatrix bits; | |
53 bits.Init(width); | |
54 for(FX_INT32 i = 0; i < width; i++) { | |
55 for(FX_INT32 j = 0; j < height; j++) { | |
56 if(image[i * width + j]) { | |
57 bits.Set(j, i); | |
58 } | |
59 } | |
60 } | |
61 CBC_CommonDecoderResult* cdr = Decode(&bits, height, e); | |
62 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
63 return cdr; | |
64 } | |
65 CBC_CommonDecoderResult* CBC_QRCoderDecoder::Decode(CBC_CommonBitMatrix* bits, F
X_INT32 byteModeDecode, FX_INT32 &e) | |
66 { | |
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 = CBC_QRDataBlock::GetDataBlocks(codewords.get(), v
ersion, ecLevel, e); | |
79 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
80 FX_INT32 totalBytes = 0; | |
81 for (FX_INT32 i = 0; i < dataBlocks->GetSize(); i++) { | |
82 totalBytes += ((CBC_QRDataBlock*) ((*dataBlocks)[i]))->GetNumDataCodewor
ds(); | |
83 } | |
84 CFX_ByteArray resultBytes; | |
85 FX_INT32 resultOffset = 0; | |
86 for (FX_INT32 j = 0; j < dataBlocks->GetSize(); j++) { | |
87 CBC_QRDataBlock *dataBlock = (CBC_QRDataBlock *)((*dataBlocks)[j]); | |
88 CFX_ByteArray* codewordBytes = dataBlock->GetCodewords(); | |
89 FX_INT32 numDataCodewords = dataBlock->GetNumDataCodewords(); | |
90 CorrectErrors(codewordBytes, numDataCodewords, e); | |
91 if (e != BCExceptionNO) { | |
92 for(FX_INT32 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(FX_INT32 i = 0; i < numDataCodewords; i++) { | |
101 resultBytes.Add((*codewordBytes)[i]); | |
102 } | |
103 } | |
104 for(FX_INT32 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(&resultB
ytes, version, ecLevel, byteModeDecode, e); | |
111 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
112 return cdr; | |
113 } | |
114 void CBC_QRCoderDecoder::CorrectErrors(CFX_ByteArray* codewordBytes, FX_INT32 nu
mDataCodewords, FX_INT32 &e) | |
115 { | |
116 FX_INT32 numCodewords = codewordBytes->GetSize(); | |
117 CFX_Int32Array codewordsInts; | |
118 codewordsInts.SetSize(numCodewords); | |
119 for(FX_INT32 i = 0; i < numCodewords; i++) { | |
120 codewordsInts[i] = (FX_INT32)((*codewordBytes)[i] & 0xff); | |
121 } | |
122 FX_INT32 numECCodewords = codewordBytes->GetSize() - numDataCodewords; | |
123 m_rsDecoder->Decode(&codewordsInts, numECCodewords, e); | |
124 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
125 for(FX_INT32 k = 0; k < numDataCodewords; k++) { | |
126 (*codewordBytes)[k] = (FX_BYTE) codewordsInts[k]; | |
127 } | |
128 } | |
OLD | NEW |