/
qr_util.rb
256 lines (216 loc) · 6.5 KB
/
qr_util.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# frozen_string_literal: true
module RQRCodeCore
class QRUtil
PATTERN_POSITION_TABLE = [
[],
[6, 18],
[6, 22],
[6, 26],
[6, 30],
[6, 34],
[6, 22, 38],
[6, 24, 42],
[6, 26, 46],
[6, 28, 50],
[6, 30, 54],
[6, 32, 58],
[6, 34, 62],
[6, 26, 46, 66],
[6, 26, 48, 70],
[6, 26, 50, 74],
[6, 30, 54, 78],
[6, 30, 56, 82],
[6, 30, 58, 86],
[6, 34, 62, 90],
[6, 28, 50, 72, 94],
[6, 26, 50, 74, 98],
[6, 30, 54, 78, 102],
[6, 28, 54, 80, 106],
[6, 32, 58, 84, 110],
[6, 30, 58, 86, 114],
[6, 34, 62, 90, 118],
[6, 26, 50, 74, 98, 122],
[6, 30, 54, 78, 102, 126],
[6, 26, 52, 78, 104, 130],
[6, 30, 56, 82, 108, 134],
[6, 34, 60, 86, 112, 138],
[6, 30, 58, 86, 114, 142],
[6, 34, 62, 90, 118, 146],
[6, 30, 54, 78, 102, 126, 150],
[6, 24, 50, 76, 102, 128, 154],
[6, 28, 54, 80, 106, 132, 158],
[6, 32, 58, 84, 110, 136, 162],
[6, 26, 54, 82, 110, 138, 166],
[6, 30, 58, 86, 114, 142, 170]
].freeze
G15 = 1 << 10 | 1 << 8 | 1 << 5 | 1 << 4 | 1 << 2 | 1 << 1 | 1 << 0
G18 = 1 << 12 | 1 << 11 | 1 << 10 | 1 << 9 | 1 << 8 | 1 << 5 | 1 << 2 | 1 << 0
G15_MASK = 1 << 14 | 1 << 12 | 1 << 10 | 1 << 4 | 1 << 1
DEMERIT_POINTS_1 = 3
DEMERIT_POINTS_2 = 3
DEMERIT_POINTS_3 = 40
DEMERIT_POINTS_4 = 10
BITS_FOR_MODE = {
QRMODE[:mode_number] => [10, 12, 14],
QRMODE[:mode_alpha_numk] => [9, 11, 13],
QRMODE[:mode_8bit_byte] => [8, 16, 16],
QRMODE[:mode_kanji] => [8, 10, 12]
}.freeze
def self.max_size
PATTERN_POSITION_TABLE.count
end
def self.get_bch_format_info(data)
d = data << 10
while QRUtil.get_bch_digit(d) - QRUtil.get_bch_digit(G15) >= 0
d ^= (G15 << (QRUtil.get_bch_digit(d) - QRUtil.get_bch_digit(G15)))
end
((data << 10) | d) ^ G15_MASK
end
def self.rszf(num, count)
# zero fill right shift
(num >> count) & ((2**((num.size * 8) - count)) - 1)
end
def self.get_bch_version(data)
d = data << 12
while QRUtil.get_bch_digit(d) - QRUtil.get_bch_digit(G18) >= 0
d ^= (G18 << (QRUtil.get_bch_digit(d) - QRUtil.get_bch_digit(G18)))
end
(data << 12) | d
end
def self.get_bch_digit(data)
digit = 0
while data != 0
digit += 1
data = QRUtil.rszf(data, 1)
end
digit
end
def self.get_pattern_positions(version)
PATTERN_POSITION_TABLE[version - 1]
end
def self.get_mask(mask_pattern, i, j)
if mask_pattern > QRMASKCOMPUTATIONS.size
raise QRCodeRunTimeError, "bad mask_pattern: #{mask_pattern}"
end
QRMASKCOMPUTATIONS[mask_pattern].call(i, j)
end
def self.get_error_correct_polynomial(error_correct_length)
a = QRPolynomial.new([1], 0)
(0...error_correct_length).each do |i|
a = a.multiply(QRPolynomial.new([1, QRMath.gexp(i)], 0))
end
a
end
def self.get_length_in_bits(mode, version)
if !QRMODE.value?(mode)
raise QRCodeRunTimeError, "Unknown mode: #{mode}"
end
if version > 40
raise QRCodeRunTimeError, "Unknown version: #{version}"
end
if version.between?(1, 9)
# 1 - 9
macro_version = 0
elsif version <= 26
# 10 - 26
macro_version = 1
elsif version <= 40
# 27 - 40
macro_version = 2
end
BITS_FOR_MODE[mode][macro_version]
end
def self.get_lost_points(modules)
demerit_points = 0
demerit_points += QRUtil.demerit_points_1_same_color(modules)
demerit_points += QRUtil.demerit_points_2_full_blocks(modules)
demerit_points += QRUtil.demerit_points_3_dangerous_patterns(modules)
demerit_points += QRUtil.demerit_points_4_dark_ratio(modules)
demerit_points
end
def self.demerit_points_1_same_color(modules)
demerit_points = 0
module_count = modules.size
# level1
(0...module_count).each do |row|
(0...module_count).each do |col|
same_count = 0
dark = modules[row][col]
(-1..1).each do |r|
next if row + r < 0 || module_count <= row + r
(-1..1).each do |c|
next if col + c < 0 || module_count <= col + c
next if r == 0 && c == 0
if dark == modules[row + r][col + c]
same_count += 1
end
end
end
if same_count > 5
demerit_points += (DEMERIT_POINTS_1 + same_count - 5)
end
end
end
demerit_points
end
def self.demerit_points_2_full_blocks(modules)
demerit_points = 0
module_count = modules.size
# level 2
(0...(module_count - 1)).each do |row|
(0...(module_count - 1)).each do |col|
count = 0
count += 1 if modules[row][col]
count += 1 if modules[row + 1][col]
count += 1 if modules[row][col + 1]
count += 1 if modules[row + 1][col + 1]
if count == 0 || count == 4
demerit_points += DEMERIT_POINTS_2
end
end
end
demerit_points
end
def self.demerit_points_3_dangerous_patterns(modules)
demerit_points = 0
module_count = modules.size
# level 3
modules.each do |row|
(module_count - 6).times do |col_idx|
if row[col_idx] &&
!row[col_idx + 1] &&
row[col_idx + 2] &&
row[col_idx + 3] &&
row[col_idx + 4] &&
!row[col_idx + 5] &&
row[col_idx + 6]
demerit_points += DEMERIT_POINTS_3
end
end
end
(0...module_count).each do |col|
(0...(module_count - 6)).each do |row|
if modules[row][col] &&
!modules[row + 1][col] &&
modules[row + 2][col] &&
modules[row + 3][col] &&
modules[row + 4][col] &&
!modules[row + 5][col] &&
modules[row + 6][col]
demerit_points += DEMERIT_POINTS_3
end
end
end
demerit_points
end
def self.demerit_points_4_dark_ratio(modules)
# level 4
dark_count = modules.reduce(0) do |sum, col|
sum + col.count(true)
end
ratio = dark_count / (modules.size * modules.size)
ratio_delta = (100 * ratio - 50).abs / 5
ratio_delta * DEMERIT_POINTS_4
end
end
end