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_CommonBitMatrix.h" | |
25 #include "include/BC_QRCoderVersion.h" | |
26 #include "include/BC_QRCoderFormatInformation.h" | |
27 #include "include/BC_QRDataMask.h" | |
28 #include "include/BC_QRBitMatrixParser.h" | |
29 CBC_QRBitMatrixParser::CBC_QRBitMatrixParser() | |
30 { | |
31 } | |
32 void CBC_QRBitMatrixParser::Init(CBC_CommonBitMatrix *bitMatrix, FX_INT32 &e) | |
33 { | |
34 m_dimension = bitMatrix->GetDimension(e); | |
35 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
36 m_tempBitMatrix = bitMatrix; | |
37 if(m_dimension < 21 || (m_dimension & 0x03) != 1) { | |
38 e = BCExceptionRead; | |
39 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
40 } | |
41 m_bitMatrix = m_tempBitMatrix; | |
42 m_parsedFormatInfo = NULL; | |
43 m_version = NULL; | |
44 } | |
45 CBC_QRBitMatrixParser::~CBC_QRBitMatrixParser() | |
46 { | |
47 if(m_parsedFormatInfo != NULL) { | |
48 delete m_parsedFormatInfo; | |
49 m_parsedFormatInfo = NULL; | |
50 } | |
51 m_version = NULL; | |
52 } | |
53 CBC_QRCoderFormatInformation* CBC_QRBitMatrixParser::ReadFormatInformation(FX_IN
T32 &e) | |
54 { | |
55 if(m_parsedFormatInfo != NULL) { | |
56 return m_parsedFormatInfo; | |
57 } | |
58 FX_INT32 formatInfoBits = 0; | |
59 FX_INT32 j; | |
60 for( j = 0; j < 6; j++) { | |
61 formatInfoBits = CopyBit(8, j, formatInfoBits); | |
62 } | |
63 formatInfoBits = CopyBit(8, 7, formatInfoBits); | |
64 formatInfoBits = CopyBit(8, 8, formatInfoBits); | |
65 formatInfoBits = CopyBit(7, 8, formatInfoBits); | |
66 for(FX_INT32 i = 5; i >= 0; i--) { | |
67 formatInfoBits = CopyBit(i, 8, formatInfoBits); | |
68 } | |
69 m_parsedFormatInfo = CBC_QRCoderFormatInformation::DecodeFormatInformation(f
ormatInfoBits); | |
70 if(m_parsedFormatInfo != NULL) { | |
71 return m_parsedFormatInfo; | |
72 } | |
73 FX_INT32 dimension = m_bitMatrix->GetDimension(e); | |
74 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
75 formatInfoBits = 0; | |
76 FX_INT32 iMin = dimension - 8; | |
77 for(j = dimension - 1; j >= iMin; j--) { | |
78 formatInfoBits = CopyBit(j, 8, formatInfoBits); | |
79 } | |
80 for(FX_INT32 k = dimension - 7; k < dimension; k++) { | |
81 formatInfoBits = CopyBit(8, k , formatInfoBits); | |
82 } | |
83 m_parsedFormatInfo = CBC_QRCoderFormatInformation::DecodeFormatInformation(f
ormatInfoBits); | |
84 if(m_parsedFormatInfo != NULL) { | |
85 return m_parsedFormatInfo; | |
86 } | |
87 e = BCExceptionRead; | |
88 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
89 return NULL; | |
90 } | |
91 CBC_QRCoderVersion* CBC_QRBitMatrixParser::ReadVersion(FX_INT32 &e) | |
92 { | |
93 if(m_version != NULL) { | |
94 return m_version; | |
95 } | |
96 FX_INT32 dimension = m_bitMatrix->GetDimension(e); | |
97 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
98 FX_INT32 provisionVersion = (dimension - 17) >> 2; | |
99 if(provisionVersion <= 6) { | |
100 CBC_QRCoderVersion* qrv = CBC_QRCoderVersion::GetVersionForNumber(provis
ionVersion, e); | |
101 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
102 return qrv; | |
103 } | |
104 FX_INT32 versionBits = 0; | |
105 for (FX_INT32 i = 5; i >= 0; i--) { | |
106 FX_INT32 jMin = dimension - 11; | |
107 for (FX_INT32 j = dimension - 9; j >= jMin; j--) { | |
108 versionBits = CopyBit(i, j, versionBits); | |
109 } | |
110 } | |
111 m_version = CBC_QRCoderVersion::DecodeVersionInformation(versionBits, e); | |
112 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
113 if (m_version != NULL && m_version->GetDimensionForVersion() == dimension) { | |
114 return m_version; | |
115 } | |
116 versionBits = 0; | |
117 for (FX_INT32 j = 5; j >= 0; j--) { | |
118 FX_INT32 iMin = dimension - 11; | |
119 for (FX_INT32 i = dimension - 9; i >= iMin; i--) { | |
120 versionBits = CopyBit(i, j, versionBits); | |
121 } | |
122 } | |
123 m_version = CBC_QRCoderVersion::DecodeVersionInformation(versionBits, e); | |
124 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
125 if (m_version != NULL && m_version->GetDimensionForVersion() == dimension) { | |
126 return m_version; | |
127 } | |
128 e = BCExceptionRead; | |
129 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
130 return NULL; | |
131 } | |
132 FX_INT32 CBC_QRBitMatrixParser::CopyBit(FX_INT32 i, FX_INT32 j, FX_INT32 version
Bits) | |
133 { | |
134 return m_bitMatrix->Get(j, i) ? (versionBits << 1) | 0x1 : versionBits << 1; | |
135 } | |
136 CFX_ByteArray* CBC_QRBitMatrixParser::ReadCodewords(FX_INT32 &e) | |
137 { | |
138 CBC_QRCoderFormatInformation *formatInfo = ReadFormatInformation(e); | |
139 BC_EXCEPTION_CHECK_ReturnValue(e, NULL) | |
140 CBC_QRCoderVersion *version = ReadVersion(e)
; | |
141 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
142 CBC_QRDataMask *dataMask = CBC_QRDataMask::ForRef
erence((FX_INT32)(formatInfo->GetDataMask()), e); | |
143 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
144 FX_INT32 dimension = m_bitMatrix->G
etDimension(e); | |
145 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
146 dataMask->UnmaskBitMatirx(m_bitMatrix, dimension); | |
147 CBC_CommonBitMatrix* cbm = version->BuildFunctionPattern(e); | |
148 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
149 CBC_AutoPtr<CBC_CommonBitMatrix > functionPattern(cbm); | |
150 FX_BOOL readingUp = TRUE; | |
151 CFX_ByteArray * temp = FX_NEW CFX_ByteArray; | |
152 temp->SetSize(version->GetTotalCodeWords()); | |
153 CBC_AutoPtr<CFX_ByteArray> result(temp); | |
154 FX_INT32 resultOffset = 0; | |
155 FX_INT32 currentByte = 0; | |
156 FX_INT32 bitsRead = 0; | |
157 for(FX_INT32 j = dimension - 1; j > 0; j -= 2) { | |
158 if(j == 6) { | |
159 j--; | |
160 } | |
161 for(FX_INT32 count = 0; count < dimension; count++) { | |
162 FX_INT32 i = readingUp ? dimension - 1 - count : count; | |
163 for(FX_INT32 col = 0; col < 2; col++) { | |
164 if(!functionPattern->Get(j - col, i)) { | |
165 bitsRead++; | |
166 currentByte <<= 1; | |
167 if(m_bitMatrix->Get(j - col, i)) { | |
168 currentByte |= 1; | |
169 } | |
170 if(bitsRead == 8) { | |
171 (*result)[resultOffset++] = (FX_BYTE) currentByte; | |
172 bitsRead = 0; | |
173 currentByte = 0; | |
174 } | |
175 } | |
176 } | |
177 } | |
178 readingUp ^= TRUE; | |
179 } | |
180 if(resultOffset != version->GetTotalCodeWords()) { | |
181 e = BCExceptionRead; | |
182 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
183 } | |
184 return result.release(); | |
185 } | |
OLD | NEW |