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 "fxbarcode/qrcode/BC_QRCoderMatrixUtil.h"
24
25 #include "core/fxcrt/fx_memory.h"
26 #include "fxbarcode/common/BC_CommonByteMatrix.h"
27 #include "fxbarcode/qrcode/BC_QRCoder.h"
28 #include "fxbarcode/qrcode/BC_QRCoderBitVector.h"
29 #include "fxbarcode/qrcode/BC_QRCoderErrorCorrectionLevel.h"
30 #include "fxbarcode/qrcode/BC_QRCoderMaskUtil.h"
31
32 namespace {
33
34 constexpr uint8_t POSITION_DETECTION_PATTERN[7][7] = {
35 {1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 0, 0, 0, 1}, {1, 0, 1, 1, 1, 0, 1},
36 {1, 0, 1, 1, 1, 0, 1}, {1, 0, 1, 1, 1, 0, 1}, {1, 0, 0, 0, 0, 0, 1},
37 {1, 1, 1, 1, 1, 1, 1}};
38
39 constexpr uint8_t POSITION_ADJUSTMENT_PATTERN[5][5] = {{1, 1, 1, 1, 1},
40 {1, 0, 0, 0, 1},
41 {1, 0, 1, 0, 1},
42 {1, 0, 0, 0, 1},
43 {1, 1, 1, 1, 1}};
44
45 constexpr size_t kNumCoordinate = 7;
46 constexpr uint8_t kPositionAdjustmentPatternCoordinates[39][kNumCoordinate] = {
47 {6, 18, 0, 0, 0, 0, 0}, {6, 22, 0, 0, 0, 0, 0},
48 {6, 26, 0, 0, 0, 0, 0}, {6, 30, 0, 0, 0, 0, 0},
49 {6, 34, 0, 0, 0, 0, 0}, {6, 22, 38, 0, 0, 0, 0},
50 {6, 24, 42, 0, 0, 0, 0}, {6, 26, 46, 0, 0, 0, 0},
51 {6, 28, 50, 0, 0, 0, 0}, {6, 30, 54, 0, 0, 0, 0},
52 {6, 32, 58, 0, 0, 0, 0}, {6, 34, 62, 0, 0, 0, 0},
53 {6, 26, 46, 66, 0, 0, 0}, {6, 26, 48, 70, 0, 0, 0},
54 {6, 26, 50, 74, 0, 0, 0}, {6, 30, 54, 78, 0, 0, 0},
55 {6, 30, 56, 82, 0, 0, 0}, {6, 30, 58, 86, 0, 0, 0},
56 {6, 34, 62, 90, 0, 0, 0}, {6, 28, 50, 72, 94, 0, 0},
57 {6, 26, 50, 74, 98, 0, 0}, {6, 30, 54, 78, 102, 0, 0},
58 {6, 28, 54, 80, 106, 0, 0}, {6, 32, 58, 84, 110, 0, 0},
59 {6, 30, 58, 86, 114, 0, 0}, {6, 34, 62, 90, 118, 0, 0},
60 {6, 26, 50, 74, 98, 122, 0}, {6, 30, 54, 78, 102, 126, 0},
61 {6, 26, 52, 78, 104, 130, 0}, {6, 30, 56, 82, 108, 134, 0},
62 {6, 34, 60, 86, 112, 138, 0}, {6, 30, 58, 86, 114, 142, 0},
63 {6, 34, 62, 90, 118, 146, 0}, {6, 30, 54, 78, 102, 126, 150},
64 {6, 24, 50, 76, 102, 128, 154}, {6, 28, 54, 80, 106, 132, 158},
65 {6, 32, 58, 84, 110, 136, 162}, {6, 26, 54, 82, 110, 138, 166},
66 {6, 30, 58, 86, 114, 142, 170},
67 };
68
69 const uint8_t TYPE_INFO_COORDINATES[15][2] = {
70 {8, 0}, {8, 1}, {8, 2}, {8, 3}, {8, 4}, {8, 5}, {8, 7}, {8, 8},
71 {7, 8}, {5, 8}, {4, 8}, {3, 8}, {2, 8}, {1, 8}, {0, 8},
72 };
73
74 const int32_t VERSION_INFO_POLY = 0x1f25;
75 const int32_t TYPE_INFO_POLY = 0x0537;
76 const int32_t TYPE_INFO_MASK_PATTERN = 0x5412;
77
IsEmpty(int32_t value)78 bool IsEmpty(int32_t value) {
79 return (uint8_t)value == 0xff;
80 }
81
IsValidValue(int32_t value)82 bool IsValidValue(int32_t value) {
83 return ((uint8_t)value == 0xff || (uint8_t)value == 0x00 ||
84 (uint8_t)value == 0x01);
85 }
86
FindMSBSet(int32_t value)87 int32_t FindMSBSet(int32_t value) {
88 int32_t numDigits = 0;
89 while (value != 0) {
90 value >>= 1;
91 ++numDigits;
92 }
93 return numDigits;
94 }
95
EmbedDataBits(CBC_QRCoderBitVector * dataBits,int32_t maskPattern,CBC_CommonByteMatrix * matrix)96 bool EmbedDataBits(CBC_QRCoderBitVector* dataBits,
97 int32_t maskPattern,
98 CBC_CommonByteMatrix* matrix) {
99 size_t szBitIndex = 0;
100 int32_t direction = -1;
101 int32_t x = matrix->GetWidth() - 1;
102 int32_t y = matrix->GetHeight() - 1;
103 while (x > 0) {
104 if (x == 6)
105 x -= 1;
106
107 while (y >= 0 && y < matrix->GetHeight()) {
108 if (y == 6) {
109 y += direction;
110 continue;
111 }
112 for (int32_t i = 0; i < 2; i++) {
113 int32_t xx = x - i;
114 if (!IsEmpty(matrix->Get(xx, y))) {
115 continue;
116 }
117 int32_t bit;
118 if (szBitIndex < dataBits->Size()) {
119 bit = dataBits->At(szBitIndex);
120 szBitIndex++;
121 } else {
122 bit = 0;
123 }
124 ASSERT(CBC_QRCoder::IsValidMaskPattern(maskPattern));
125 if (CBC_QRCoderMaskUtil::GetDataMaskBit(maskPattern, xx, y))
126 bit ^= 0x01;
127 matrix->Set(xx, y, bit);
128 }
129 y += direction;
130 }
131 direction = -direction;
132 y += direction;
133 x -= 2;
134 }
135 return szBitIndex == dataBits->Size();
136 }
137
CalculateBCHCode(int32_t value,int32_t poly)138 int32_t CalculateBCHCode(int32_t value, int32_t poly) {
139 int32_t msbSetInPoly = FindMSBSet(poly);
140 value <<= msbSetInPoly - 1;
141 while (FindMSBSet(value) >= msbSetInPoly) {
142 value ^= poly << (FindMSBSet(value) - msbSetInPoly);
143 }
144 return value;
145 }
146
MakeTypeInfoBits(const CBC_QRCoderErrorCorrectionLevel * ecLevel,int32_t maskPattern,CBC_QRCoderBitVector * bits)147 bool MakeTypeInfoBits(const CBC_QRCoderErrorCorrectionLevel* ecLevel,
148 int32_t maskPattern,
149 CBC_QRCoderBitVector* bits) {
150 if (!CBC_QRCoder::IsValidMaskPattern(maskPattern))
151 return false;
152
153 int32_t typeInfo = (ecLevel->GetBits() << 3) | maskPattern;
154 bits->AppendBits(typeInfo, 5);
155 int32_t bchCode = CalculateBCHCode(typeInfo, TYPE_INFO_POLY);
156 bits->AppendBits(bchCode, 10);
157 CBC_QRCoderBitVector maskBits;
158 maskBits.AppendBits(TYPE_INFO_MASK_PATTERN, 15);
159 if (!bits->XOR(&maskBits))
160 return false;
161
162 ASSERT(bits->Size() == 15);
163 return true;
164 }
165
MakeVersionInfoBits(int32_t version,CBC_QRCoderBitVector * bits)166 void MakeVersionInfoBits(int32_t version, CBC_QRCoderBitVector* bits) {
167 bits->AppendBits(version, 6);
168 int32_t bchCode = CalculateBCHCode(version, VERSION_INFO_POLY);
169 bits->AppendBits(bchCode, 12);
170 ASSERT(bits->Size() == 18);
171 }
172
EmbedTypeInfo(const CBC_QRCoderErrorCorrectionLevel * ecLevel,int32_t maskPattern,CBC_CommonByteMatrix * matrix)173 bool EmbedTypeInfo(const CBC_QRCoderErrorCorrectionLevel* ecLevel,
174 int32_t maskPattern,
175 CBC_CommonByteMatrix* matrix) {
176 CBC_QRCoderBitVector typeInfoBits;
177 if (!MakeTypeInfoBits(ecLevel, maskPattern, &typeInfoBits))
178 return false;
179
180 for (size_t i = 0; i < typeInfoBits.Size(); i++) {
181 int32_t bit = typeInfoBits.At(typeInfoBits.Size() - 1 - i);
182 int32_t x1 = TYPE_INFO_COORDINATES[i][0];
183 int32_t y1 = TYPE_INFO_COORDINATES[i][1];
184 matrix->Set(x1, y1, bit);
185 if (i < 8) {
186 int32_t x2 = matrix->GetWidth() - i - 1;
187 int32_t y2 = 8;
188 matrix->Set(x2, y2, bit);
189 } else {
190 int32_t x2 = 8;
191 int32_t y2 = matrix->GetHeight() - 7 + (i - 8);
192 matrix->Set(x2, y2, bit);
193 }
194 }
195 return true;
196 }
197
MaybeEmbedVersionInfo(int32_t version,CBC_CommonByteMatrix * matrix)198 void MaybeEmbedVersionInfo(int32_t version, CBC_CommonByteMatrix* matrix) {
199 if (version < 7)
200 return;
201
202 CBC_QRCoderBitVector versionInfoBits;
203 MakeVersionInfoBits(version, &versionInfoBits);
204 int32_t bitIndex = 6 * 3 - 1;
205 for (int32_t i = 0; i < 6; i++) {
206 for (int32_t j = 0; j < 3; j++) {
207 int32_t bit = versionInfoBits.At(bitIndex);
208 bitIndex--;
209 matrix->Set(i, matrix->GetHeight() - 11 + j, bit);
210 matrix->Set(matrix->GetHeight() - 11 + j, i, bit);
211 }
212 }
213 }
214
EmbedTimingPatterns(CBC_CommonByteMatrix * matrix)215 bool EmbedTimingPatterns(CBC_CommonByteMatrix* matrix) {
216 for (int32_t i = 8; i < matrix->GetWidth() - 8; i++) {
217 int32_t bit = (i + 1) % 2;
218 if (!IsValidValue(matrix->Get(i, 6)))
219 return false;
220
221 if (IsEmpty(matrix->Get(i, 6)))
222 matrix->Set(i, 6, bit);
223
224 if (!IsValidValue(matrix->Get(6, i)))
225 return false;
226
227 if (IsEmpty(matrix->Get(6, i)))
228 matrix->Set(6, i, bit);
229 }
230 return true;
231 }
232
EmbedDarkDotAtLeftBottomCorner(CBC_CommonByteMatrix * matrix)233 bool EmbedDarkDotAtLeftBottomCorner(CBC_CommonByteMatrix* matrix) {
234 if (matrix->Get(8, matrix->GetHeight() - 8) == 0)
235 return false;
236
237 matrix->Set(8, matrix->GetHeight() - 8, 1);
238 return true;
239 }
240
EmbedHorizontalSeparationPattern(int32_t xStart,int32_t yStart,CBC_CommonByteMatrix * matrix)241 bool EmbedHorizontalSeparationPattern(int32_t xStart,
242 int32_t yStart,
243 CBC_CommonByteMatrix* matrix) {
244 for (int32_t x = 0; x < 8; x++) {
245 if (!IsEmpty(matrix->Get(xStart + x, yStart)))
246 return false;
247
248 matrix->Set(xStart + x, yStart, 0);
249 }
250 return true;
251 }
252
EmbedVerticalSeparationPattern(int32_t xStart,int32_t yStart,CBC_CommonByteMatrix * matrix)253 bool EmbedVerticalSeparationPattern(int32_t xStart,
254 int32_t yStart,
255 CBC_CommonByteMatrix* matrix) {
256 for (int32_t y = 0; y < 7; y++) {
257 if (!IsEmpty(matrix->Get(xStart, yStart + y)))
258 return false;
259
260 matrix->Set(xStart, yStart + y, 0);
261 }
262 return true;
263 }
264
EmbedPositionAdjustmentPattern(int32_t xStart,int32_t yStart,CBC_CommonByteMatrix * matrix)265 bool EmbedPositionAdjustmentPattern(int32_t xStart,
266 int32_t yStart,
267 CBC_CommonByteMatrix* matrix) {
268 for (int32_t y = 0; y < 5; y++) {
269 for (int32_t x = 0; x < 5; x++) {
270 if (!IsEmpty(matrix->Get(xStart + x, y + yStart)))
271 return false;
272
273 matrix->Set(xStart + x, yStart + y, POSITION_ADJUSTMENT_PATTERN[y][x]);
274 }
275 }
276 return true;
277 }
278
EmbedPositionDetectionPattern(int32_t xStart,int32_t yStart,CBC_CommonByteMatrix * matrix)279 bool EmbedPositionDetectionPattern(int32_t xStart,
280 int32_t yStart,
281 CBC_CommonByteMatrix* matrix) {
282 for (int32_t y = 0; y < 7; y++) {
283 for (int32_t x = 0; x < 7; x++) {
284 if (!IsEmpty(matrix->Get(xStart + x, yStart + y)))
285 return false;
286
287 matrix->Set(xStart + x, yStart + y, POSITION_DETECTION_PATTERN[y][x]);
288 }
289 }
290 return true;
291 }
292
EmbedPositionDetectionPatternsAndSeparators(CBC_CommonByteMatrix * matrix)293 bool EmbedPositionDetectionPatternsAndSeparators(CBC_CommonByteMatrix* matrix) {
294 constexpr int32_t pdpWidth = 7;
295 if (!EmbedPositionDetectionPattern(0, 0, matrix))
296 return false;
297 if (!EmbedPositionDetectionPattern(matrix->GetWidth() - pdpWidth, 0, matrix))
298 return false;
299 if (!EmbedPositionDetectionPattern(0, matrix->GetWidth() - pdpWidth, matrix))
300 return false;
301
302 constexpr int32_t hspWidth = 8;
303 if (!EmbedHorizontalSeparationPattern(0, hspWidth - 1, matrix))
304 return false;
305 if (!EmbedHorizontalSeparationPattern(matrix->GetWidth() - hspWidth,
306 hspWidth - 1, matrix)) {
307 return false;
308 }
309 if (!EmbedHorizontalSeparationPattern(0, matrix->GetWidth() - hspWidth,
310 matrix)) {
311 return false;
312 }
313
314 constexpr int32_t vspSize = 7;
315 if (!EmbedVerticalSeparationPattern(vspSize, 0, matrix))
316 return false;
317 if (!EmbedVerticalSeparationPattern(matrix->GetHeight() - vspSize - 1, 0,
318 matrix)) {
319 return false;
320 }
321 if (!EmbedVerticalSeparationPattern(vspSize, matrix->GetHeight() - vspSize,
322 matrix)) {
323 return false;
324 }
325 return true;
326 }
327
MaybeEmbedPositionAdjustmentPatterns(int32_t version,CBC_CommonByteMatrix * matrix)328 bool MaybeEmbedPositionAdjustmentPatterns(int32_t version,
329 CBC_CommonByteMatrix* matrix) {
330 if (version < 2)
331 return true;
332
333 const size_t index = version - 2;
334 if (index >= FX_ArraySize(kPositionAdjustmentPatternCoordinates))
335 return false;
336
337 const auto* coordinates = &kPositionAdjustmentPatternCoordinates[index][0];
338 for (size_t i = 0; i < kNumCoordinate; i++) {
339 const int32_t y = coordinates[i];
340 if (y == 0)
341 break;
342 for (size_t j = 0; j < kNumCoordinate; j++) {
343 const int32_t x = coordinates[j];
344 if (x == 0)
345 break;
346
347 if (IsEmpty(matrix->Get(x, y))) {
348 if (!EmbedPositionAdjustmentPattern(x - 2, y - 2, matrix))
349 return false;
350 }
351 }
352 }
353 return true;
354 }
355
EmbedBasicPatterns(int32_t version,CBC_CommonByteMatrix * matrix)356 bool EmbedBasicPatterns(int32_t version, CBC_CommonByteMatrix* matrix) {
357 if (!EmbedPositionDetectionPatternsAndSeparators(matrix))
358 return false;
359 if (!EmbedDarkDotAtLeftBottomCorner(matrix))
360 return false;
361 if (!MaybeEmbedPositionAdjustmentPatterns(version, matrix))
362 return false;
363 if (!EmbedTimingPatterns(matrix))
364 return false;
365 return true;
366 }
367
368 } // namespace
369
BuildMatrix(CBC_QRCoderBitVector * dataBits,const CBC_QRCoderErrorCorrectionLevel * ecLevel,int32_t version,int32_t maskPattern,CBC_CommonByteMatrix * matrix)370 bool CBC_QRCoderMatrixUtil::BuildMatrix(
371 CBC_QRCoderBitVector* dataBits,
372 const CBC_QRCoderErrorCorrectionLevel* ecLevel,
373 int32_t version,
374 int32_t maskPattern,
375 CBC_CommonByteMatrix* matrix) {
376 if (!dataBits || !matrix)
377 return false;
378
379 matrix->clear(0xff);
380
381 if (!EmbedBasicPatterns(version, matrix))
382 return false;
383 if (!EmbedTypeInfo(ecLevel, maskPattern, matrix))
384 return false;
385
386 MaybeEmbedVersionInfo(version, matrix);
387 return EmbedDataBits(dataBits, maskPattern, matrix);
388 }
389