8
9
10
11
12
13
14
15
16
17
18
19
20
21
23#include "fxbarcode/qrcode/BC_QRCoderMatrixUtil.h"
27#include "fxbarcode/common/BC_CommonByteMatrix.h"
28#include "fxbarcode/qrcode/BC_QRCoder.h"
29#include "fxbarcode/qrcode/BC_QRCoderBitVector.h"
30#include "fxbarcode/qrcode/BC_QRCoderErrorCorrectionLevel.h"
31#include "fxbarcode/qrcode/BC_QRCoderMaskUtil.h"
32#include "third_party/base/check.h"
33#include "third_party/base/check_op.h"
37constexpr uint8_t POSITION_DETECTION_PATTERN[7][7] = {
38 {1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 0, 0, 0, 1}, {1, 0, 1, 1, 1, 0, 1},
39 {1, 0, 1, 1, 1, 0, 1}, {1, 0, 1, 1, 1, 0, 1}, {1, 0, 0, 0, 0, 0, 1},
40 {1, 1, 1, 1, 1, 1, 1}};
42constexpr uint8_t POSITION_ADJUSTMENT_PATTERN[5][5] = {{1, 1, 1, 1, 1},
48constexpr size_t kNumCoordinate = 7;
49constexpr uint8_t kPositionAdjustmentPatternCoordinates[39][kNumCoordinate] = {
50 {6, 18, 0, 0, 0, 0, 0}, {6, 22, 0, 0, 0, 0, 0},
51 {6, 26, 0, 0, 0, 0, 0}, {6, 30, 0, 0, 0, 0, 0},
52 {6, 34, 0, 0, 0, 0, 0}, {6, 22, 38, 0, 0, 0, 0},
53 {6, 24, 42, 0, 0, 0, 0}, {6, 26, 46, 0, 0, 0, 0},
54 {6, 28, 50, 0, 0, 0, 0}, {6, 30, 54, 0, 0, 0, 0},
55 {6, 32, 58, 0, 0, 0, 0}, {6, 34, 62, 0, 0, 0, 0},
56 {6, 26, 46, 66, 0, 0, 0}, {6, 26, 48, 70, 0, 0, 0},
57 {6, 26, 50, 74, 0, 0, 0}, {6, 30, 54, 78, 0, 0, 0},
58 {6, 30, 56, 82, 0, 0, 0}, {6, 30, 58, 86, 0, 0, 0},
59 {6, 34, 62, 90, 0, 0, 0}, {6, 28, 50, 72, 94, 0, 0},
60 {6, 26, 50, 74, 98, 0, 0}, {6, 30, 54, 78, 102, 0, 0},
61 {6, 28, 54, 80, 106, 0, 0}, {6, 32, 58, 84, 110, 0, 0},
62 {6, 30, 58, 86, 114, 0, 0}, {6, 34, 62, 90, 118, 0, 0},
63 {6, 26, 50, 74, 98, 122, 0}, {6, 30, 54, 78, 102, 126, 0},
64 {6, 26, 52, 78, 104, 130, 0}, {6, 30, 56, 82, 108, 134, 0},
65 {6, 34, 60, 86, 112, 138, 0}, {6, 30, 58, 86, 114, 142, 0},
66 {6, 34, 62, 90, 118, 146, 0}, {6, 30, 54, 78, 102, 126, 150},
67 {6, 24, 50, 76, 102, 128, 154}, {6, 28, 54, 80, 106, 132, 158},
68 {6, 32, 58, 84, 110, 136, 162}, {6, 26, 54, 82, 110, 138, 166},
69 {6, 30, 58, 86, 114, 142, 170},
72const uint8_t TYPE_INFO_COORDINATES[15][2] = {
73 {8, 0}, {8, 1}, {8, 2}, {8, 3}, {8, 4}, {8, 5}, {8, 7}, {8, 8},
74 {7, 8}, {5, 8}, {4, 8}, {3, 8}, {2, 8}, {1, 8}, {0, 8},
77const int32_t VERSION_INFO_POLY = 0x1f25;
78const int32_t TYPE_INFO_POLY = 0x0537;
79const int32_t TYPE_INFO_MASK_PATTERN = 0x5412;
81bool IsEmpty(int32_t value) {
82 return (uint8_t)value == 0xff;
85bool IsValidValue(int32_t value) {
86 return ((uint8_t)value == 0xff || (uint8_t)value == 0x00 ||
87 (uint8_t)value == 0x01);
90int32_t FindMSBSet(int32_t value) {
91 int32_t numDigits = 0;
101 CBC_CommonByteMatrix* matrix) {
102 size_t szBitIndex = 0;
103 int32_t direction = -1;
104 int32_t x = matrix->GetWidth() - 1;
105 int32_t y = matrix->GetHeight() - 1;
110 while (y >= 0 && y <
static_cast<int32_t>(matrix->GetHeight())) {
115 for (int32_t i = 0; i < 2; i++) {
117 if (!IsEmpty(matrix->Get(xx, y))) {
121 if (szBitIndex < dataBits->Size()) {
122 bit = dataBits->At(szBitIndex);
127 DCHECK(CBC_QRCoder::IsValidMaskPattern(maskPattern));
130 matrix->Set(xx, y, bit);
134 direction = -direction;
138 return szBitIndex == dataBits->Size();
141int32_t CalculateBCHCode(int32_t value, int32_t poly) {
142 int32_t msbSetInPoly = FindMSBSet(poly);
143 value <<= msbSetInPoly - 1;
144 while (FindMSBSet(value) >= msbSetInPoly) {
145 value ^= poly << (FindMSBSet(value) - msbSetInPoly);
156 int32_t typeInfo = (ecLevel
->GetBits() << 3) | maskPattern;
158 int32_t bchCode = CalculateBCHCode(typeInfo, TYPE_INFO_POLY);
165 DCHECK_EQ(bits->Size(), 15);
171 int32_t bchCode = CalculateBCHCode(version, VERSION_INFO_POLY);
173 DCHECK_EQ(bits->Size(), 18);
178 CBC_CommonByteMatrix* matrix) {
180 if (!MakeTypeInfoBits(ecLevel, maskPattern, &typeInfoBits))
183 for (size_t i = 0; i < typeInfoBits.Size(); i++) {
184 int32_t bit = typeInfoBits.At(typeInfoBits.Size() - 1 - i);
185 int32_t x1 = TYPE_INFO_COORDINATES[i][0];
186 int32_t y1 = TYPE_INFO_COORDINATES[i][1];
187 matrix->Set(x1, y1, bit);
189 int32_t x2 = matrix->GetWidth() - i - 1;
191 matrix->Set(x2, y2, bit);
194 int32_t y2 = matrix->GetHeight() - 7 + (i - 8);
195 matrix->Set(x2, y2, bit);
201void MaybeEmbedVersionInfo(int32_t version, CBC_CommonByteMatrix* matrix) {
206 MakeVersionInfoBits(version, &versionInfoBits);
207 int32_t bitIndex = 6 * 3 - 1;
208 for (int32_t i = 0; i < 6; i++) {
209 for (int32_t j = 0; j < 3; j++) {
210 int32_t bit = versionInfoBits.At(bitIndex);
212 matrix->Set(i, matrix->GetHeight() - 11 + j, bit);
213 matrix->Set(matrix->GetHeight() - 11 + j, i, bit);
218bool EmbedTimingPatterns(CBC_CommonByteMatrix* matrix) {
219 for (size_t i = 8; i + 8 < matrix->GetWidth(); i++) {
220 const uint8_t bit =
static_cast<uint8_t>((i + 1) % 2);
221 if (!IsValidValue(matrix->Get(i, 6)))
224 if (IsEmpty(matrix->Get(i, 6)))
225 matrix->Set(i, 6, bit);
227 if (!IsValidValue(matrix->Get(6, i)))
230 if (IsEmpty(matrix->Get(6, i)))
231 matrix->Set(6, i, bit);
236bool EmbedDarkDotAtLeftBottomCorner(CBC_CommonByteMatrix* matrix) {
237 if (matrix->Get(8, matrix->GetHeight() - 8) == 0)
240 matrix->Set(8, matrix->GetHeight() - 8, 1);
244bool EmbedHorizontalSeparationPattern(int32_t xStart,
246 CBC_CommonByteMatrix* matrix) {
247 for (int32_t x = 0; x < 8; x++) {
248 if (!IsEmpty(matrix->Get(xStart + x, yStart)))
251 matrix->Set(xStart + x, yStart, 0);
256bool EmbedVerticalSeparationPattern(int32_t xStart,
258 CBC_CommonByteMatrix* matrix) {
259 for (int32_t y = 0; y < 7; y++) {
260 if (!IsEmpty(matrix->Get(xStart, yStart + y)))
263 matrix->Set(xStart, yStart + y, 0);
268bool EmbedPositionAdjustmentPattern(int32_t xStart,
270 CBC_CommonByteMatrix* matrix) {
271 for (int32_t y = 0; y < 5; y++) {
272 for (int32_t x = 0; x < 5; x++) {
273 if (!IsEmpty(matrix->Get(xStart + x, y + yStart)))
276 matrix->Set(xStart + x, yStart + y, POSITION_ADJUSTMENT_PATTERN[y][x]);
282bool EmbedPositionDetectionPattern(int32_t xStart,
284 CBC_CommonByteMatrix* matrix) {
285 for (int32_t y = 0; y < 7; y++) {
286 for (int32_t x = 0; x < 7; x++) {
287 if (!IsEmpty(matrix->Get(xStart + x, yStart + y)))
290 matrix->Set(xStart + x, yStart + y, POSITION_DETECTION_PATTERN[y][x]);
296bool EmbedPositionDetectionPatternsAndSeparators(CBC_CommonByteMatrix* matrix) {
297 constexpr int32_t pdpWidth = 7;
298 if (!EmbedPositionDetectionPattern(0, 0, matrix))
300 if (!EmbedPositionDetectionPattern(matrix->GetWidth() - pdpWidth, 0, matrix))
302 if (!EmbedPositionDetectionPattern(0, matrix->GetWidth() - pdpWidth, matrix))
305 constexpr int32_t hspWidth = 8;
306 if (!EmbedHorizontalSeparationPattern(0, hspWidth - 1, matrix))
308 if (!EmbedHorizontalSeparationPattern(matrix->GetWidth() - hspWidth,
309 hspWidth - 1, matrix)) {
312 if (!EmbedHorizontalSeparationPattern(0, matrix->GetWidth() - hspWidth,
317 constexpr int32_t vspSize = 7;
318 if (!EmbedVerticalSeparationPattern(vspSize, 0, matrix))
320 if (!EmbedVerticalSeparationPattern(matrix->GetHeight() - vspSize - 1, 0,
324 if (!EmbedVerticalSeparationPattern(vspSize, matrix->GetHeight() - vspSize,
331bool MaybeEmbedPositionAdjustmentPatterns(int32_t version,
332 CBC_CommonByteMatrix* matrix) {
336 const size_t index = version - 2;
337 if (index >=
std::size(kPositionAdjustmentPatternCoordinates))
340 const auto* coordinates = &kPositionAdjustmentPatternCoordinates[index][0];
341 for (size_t i = 0; i < kNumCoordinate; i++) {
342 const int32_t y = coordinates[i];
345 for (size_t j = 0; j < kNumCoordinate; j++) {
346 const int32_t x = coordinates[j];
350 if (IsEmpty(matrix->Get(x, y))) {
351 if (!EmbedPositionAdjustmentPattern(x - 2, y - 2, matrix))
359bool EmbedBasicPatterns(int32_t version, CBC_CommonByteMatrix* matrix) {
360 if (!EmbedPositionDetectionPatternsAndSeparators(matrix))
362 if (!EmbedDarkDotAtLeftBottomCorner(matrix))
364 if (!MaybeEmbedPositionAdjustmentPatterns(version, matrix))
366 if (!EmbedTimingPatterns(matrix))
378 CBC_CommonByteMatrix* matrix) {
379 if (!dataBits || !matrix)
384 if (!EmbedBasicPatterns(version, matrix))
386 if (!EmbedTypeInfo(ecLevel, maskPattern, matrix))
389 MaybeEmbedVersionInfo(version, matrix);
390 return EmbedDataBits(dataBits, maskPattern, matrix);
void AppendBits(int32_t value, int32_t numBits)
bool XOR(const CBC_QRCoderBitVector *other)
static bool GetDataMaskBit(int32_t maskPattern, int32_t x, int32_t y)
static bool BuildMatrix(CBC_QRCoderBitVector *dataBits, const CBC_QRCoderErrorCorrectionLevel *ecLevel, int32_t version, int32_t maskPattern, CBC_CommonByteMatrix *matrix)
static bool IsValidMaskPattern(int32_t maskPattern)