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