Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
BC_QRCoderMaskUtil.cpp
Go to the documentation of this file.
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_QRCoderMaskUtil.h"
24
25#include "fxbarcode/common/BC_CommonByteMatrix.h"
26#include "fxbarcode/qrcode/BC_QRCoder.h"
27#include "fxbarcode/qrcode/BC_QRCoderErrorCorrectionLevel.h"
28#include "third_party/base/check.h"
29#include "third_party/base/notreached.h"
30
31namespace {
32
33int32_t ApplyMaskPenaltyRule1Internal(CBC_CommonByteMatrix* matrix,
34 bool isHorizontal) {
35 int32_t penalty = 0;
36 int32_t numSameBitCells = 0;
37 int32_t prevBit = -1;
38 size_t width = matrix->GetWidth();
39 size_t height = matrix->GetHeight();
40 size_t iLimit = isHorizontal ? height : width;
41 size_t jLimit = isHorizontal ? width : height;
42 pdfium::span<const uint8_t> array = matrix->GetArray();
43 for (size_t i = 0; i < iLimit; ++i) {
44 for (size_t j = 0; j < jLimit; ++j) {
45 int32_t bit = isHorizontal ? array[i * width + j] : array[j * width + i];
46 if (bit == prevBit) {
47 numSameBitCells += 1;
48 if (numSameBitCells == 5) {
49 penalty += 3;
50 } else if (numSameBitCells > 5) {
51 penalty += 1;
52 }
53 } else {
54 numSameBitCells = 1;
55 prevBit = bit;
56 }
57 }
58 numSameBitCells = 0;
59 }
60 return penalty;
61}
62
63} // namespace
64
65// static
67 CBC_CommonByteMatrix* matrix) {
68 return ApplyMaskPenaltyRule1Internal(matrix, true) +
69 ApplyMaskPenaltyRule1Internal(matrix, false);
70}
71
72// static
74 CBC_CommonByteMatrix* matrix) {
75 int32_t penalty = 0;
76 pdfium::span<const uint8_t> array = matrix->GetArray();
77 size_t width = matrix->GetWidth();
78 size_t height = matrix->GetHeight();
79 for (size_t y = 0; y + 1 < height; y++) {
80 for (size_t x = 0; x + 1 < width; x++) {
81 int32_t value = array[y * width + x];
82 if (value == array[y * width + x + 1] &&
83 value == array[(y + 1) * width + x] &&
84 value == array[(y + 1) * width + x + 1]) {
85 penalty++;
86 }
87 }
88 }
89 return 3 * penalty;
90}
91
92// static
94 CBC_CommonByteMatrix* matrix) {
95 int32_t penalty = 0;
96 pdfium::span<const uint8_t> array = matrix->GetArray();
97 size_t width = matrix->GetWidth();
98 size_t height = matrix->GetHeight();
99 for (size_t y = 0; y < height; ++y) {
100 for (size_t x = 0; x < width; ++x) {
101 if (x == 0 && (y <= 6 || (y + 7 >= height && y + 1 <= height))) {
102 continue;
103 }
104 if (x + 7 == width && y <= 6) {
105 continue;
106 }
107 if (y == 0 && (x <= 6 || (x + 7 >= width && x + 1 <= width))) {
108 continue;
109 }
110 if (y + 7 == height && x <= 6) {
111 continue;
112 }
113 if (x + 6 < width && array[y * width + x] == 1 &&
114 array[y * width + x + 1] == 0 && array[y * width + x + 2] == 1 &&
115 array[y * width + x + 3] == 1 && array[y * width + x + 4] == 1 &&
116 array[y * width + x + 5] == 0 && array[y * width + x + 6] == 1 &&
117 ((x + 10 < width && array[y * width + x + 7] == 0 &&
118 array[y * width + x + 8] == 0 && array[y * width + x + 9] == 0 &&
119 array[y * width + x + 10] == 0) ||
120 (x >= 4 && array[y * width + x - 1] == 0 &&
121 array[y * width + x - 2] == 0 && array[y * width + x - 3] == 0 &&
122 array[y * width + x - 4] == 0))) {
123 penalty += 40;
124 }
125 if (y + 6 < height && array[y * width + x] == 1 &&
126 array[(y + 1) * width + x] == 0 && array[(y + 2) * width + x] == 1 &&
127 array[(y + 3) * width + x] == 1 && array[(y + 4) * width + x] == 1 &&
128 array[(y + 5) * width + x] == 0 && array[(y + 6) * width + x] == 1 &&
129 ((y + 10 < height && array[(y + 7) * width + x] == 0 &&
130 array[(y + 8) * width + x] == 0 &&
131 array[(y + 9) * width + x] == 0 &&
132 array[(y + 10) * width + x] == 0) ||
133 (y >= 4 && array[(y - 1) * width + x] == 0 &&
134 array[(y - 2) * width + x] == 0 &&
135 array[(y - 3) * width + x] == 0 &&
136 array[(y - 4) * width + x] == 0))) {
137 penalty += 40;
138 }
139 }
140 }
141 return penalty;
142}
143
144// static
146 CBC_CommonByteMatrix* matrix) {
147 int32_t numDarkCells = 0;
148 pdfium::span<const uint8_t> array = matrix->GetArray();
149 size_t width = matrix->GetWidth();
150 size_t height = matrix->GetHeight();
151 for (size_t y = 0; y < height; ++y) {
152 for (size_t x = 0; x < width; ++x) {
153 if (array[y * width + x] == 1)
154 numDarkCells += 1;
155 }
156 }
157 size_t numTotalCells = matrix->GetHeight() * matrix->GetWidth();
158 double darkRatio = static_cast<double>(numDarkCells) / numTotalCells;
159 return abs(static_cast<int32_t>(darkRatio * 100 - 50) / 5) * 5 * 10;
160}
161
162// static
163bool CBC_QRCoderMaskUtil::GetDataMaskBit(int32_t maskPattern,
164 int32_t x,
165 int32_t y) {
166 DCHECK(CBC_QRCoder::IsValidMaskPattern(maskPattern));
167
168 int32_t intermediate = 0;
169 int32_t temp = 0;
170 switch (maskPattern) {
171 case 0:
172 intermediate = (y + x) & 0x1;
173 break;
174 case 1:
175 intermediate = y & 0x1;
176 break;
177 case 2:
178 intermediate = x % 3;
179 break;
180 case 3:
181 intermediate = (y + x) % 3;
182 break;
183 case 4:
184 intermediate = ((y >> 1) + (x / 3)) & 0x1;
185 break;
186 case 5:
187 temp = y * x;
188 intermediate = (temp & 0x1) + (temp % 3);
189 break;
190 case 6:
191 temp = y * x;
192 intermediate = (((temp & 0x1) + (temp % 3)) & 0x1);
193 break;
194 case 7:
195 temp = y * x;
196 intermediate = (((temp % 3) + ((y + x) & 0x1)) & 0x1);
197 break;
198 default:
199 NOTREACHED_NORETURN();
200 }
201 return intermediate == 0;
202}
static int32_t ApplyMaskPenaltyRule1(CBC_CommonByteMatrix *matrix)
static bool GetDataMaskBit(int32_t maskPattern, int32_t x, int32_t y)
static int32_t ApplyMaskPenaltyRule2(CBC_CommonByteMatrix *matrix)
static int32_t ApplyMaskPenaltyRule3(CBC_CommonByteMatrix *matrix)
static int32_t ApplyMaskPenaltyRule4(CBC_CommonByteMatrix *matrix)