| Class | RQRCode::QRUtil |
| In: |
lib/rqrcode/qrcode/qr_util.rb
|
| Parent: | Object |
| 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] |
| 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 |
# File lib/rqrcode/qrcode/qr_util.rb, line 83
83: def QRUtil.get_bch_digit( data )
84: digit = 0
85:
86: while data != 0
87: digit = digit + 1
88: data = (data).rszf(1)
89: end
90:
91: digit
92: end
# File lib/rqrcode/qrcode/qr_util.rb, line 65
65: def QRUtil.get_bch_type_info( data )
66: d = data << 10
67: while QRUtil.get_bch_digit(d) - QRUtil.get_bch_digit(G15) >= 0
68: d ^= (G15 << (QRUtil.get_bch_digit(d) - QRUtil.get_bch_digit(G15)))
69: end
70: (( data << 10 ) | d) ^ G15_MASK
71: end
# File lib/rqrcode/qrcode/qr_util.rb, line 74
74: def QRUtil.get_bch_type_number( data )
75: d = data << 12
76: while QRUtil.get_bch_digit(d) - QRUtil.get_bch_digit(G18) >= 0
77: d ^= (G18 << (QRUtil.get_bch_digit(d) - QRUtil.get_bch_digit(G18)))
78: end
79: ( data << 12 ) | d
80: end
# File lib/rqrcode/qrcode/qr_util.rb, line 124
124: def QRUtil.get_error_correct_polynomial( error_correct_length )
125: a = QRPolynomial.new( [1], 0 )
126:
127: ( 0...error_correct_length ).each do |i|
128: a = a.multiply( QRPolynomial.new( [1, QRMath.gexp(i)], 0 ) )
129: end
130:
131: a
132: end
# File lib/rqrcode/qrcode/qr_util.rb, line 135
135: def QRUtil.get_length_in_bits( mode, type )
136: if 1 <= type && type < 10
137:
138: # 1 - 9
139: case mode
140: when QRMODE[:mode_number] then 10
141: when QRMODE[:mode_alpha_num] then 9
142: when QRMODE[:mode_8bit_byte] then 8
143: when QRMODE[:mode_kanji] then 8
144: else
145: raise QRCodeRunTimeError, "mode: #{mode}"
146: end
147:
148: elsif type < 27
149:
150: # 10 -26
151: case mode
152: when QRMODE[:mode_number] then 12
153: when QRMODE[:mode_alpha_num] then 11
154: when QRMODE[:mode_8bit_byte] then 16
155: when QRMODE[:mode_kanji] then 10
156: else
157: raise QRCodeRunTimeError, "mode: #{mode}"
158: end
159:
160: elsif type < 41
161:
162: # 27 - 40
163: case mode
164: when QRMODE[:mode_number] then 14
165: when QRMODE[:mode_alpha_num] then 13
166: when QRMODE[:mode_8bit_byte] then 16
167: when QRMODE[:mode_kanji] then 12
168: else
169: raise QRCodeRunTimeError, "mode: #{mode}"
170: end
171:
172: else
173: raise QRCodeRunTimeError, "type: #{type}"
174: end
175: end
# File lib/rqrcode/qrcode/qr_util.rb, line 178
178: def QRUtil.get_lost_point( qr_code )
179: module_count = qr_code.module_count
180: lost_point = 0
181:
182: # level1
183: ( 0...module_count ).each do |row|
184: ( 0...module_count ).each do |col|
185: same_count = 0
186: dark = qr_code.is_dark( row, col )
187:
188: ( -1..1 ).each do |r|
189: next if row + r < 0 || module_count <= row + r
190:
191: ( -1..1 ).each do |c|
192: next if col + c < 0 || module_count <= col + c
193: next if r == 0 && c == 0
194: if dark == qr_code.is_dark( row + r, col + c )
195: same_count += 1
196: end
197: end
198: end
199:
200: if same_count > 5
201: lost_point += (3 + same_count - 5)
202: end
203: end
204: end
205:
206: # level 2
207: ( 0...( module_count - 1 ) ).each do |row|
208: ( 0...( module_count - 1 ) ).each do |col|
209: count = 0
210: count = count + 1 if qr_code.is_dark( row, col )
211: count = count + 1 if qr_code.is_dark( row + 1, col )
212: count = count + 1 if qr_code.is_dark( row, col + 1 )
213: count = count + 1 if qr_code.is_dark( row + 1, col + 1 )
214: lost_point = lost_point + 3 if (count == 0 || count == 4)
215: end
216: end
217:
218: # level 3
219: ( 0...module_count ).each do |row|
220: ( 0...( module_count - 6 ) ).each do |col|
221: if qr_code.is_dark( row, col ) && !qr_code.is_dark( row, col + 1 ) && qr_code.is_dark( row, col + 2 ) && qr_code.is_dark( row, col + 3 ) && qr_code.is_dark( row, col + 4 ) && !qr_code.is_dark( row, col + 5 ) && qr_code.is_dark( row, col + 6 )
222: lost_point = lost_point + 40
223: end
224: end
225: end
226:
227: ( 0...module_count ).each do |col|
228: ( 0...( module_count - 6 ) ).each do |row|
229: if qr_code.is_dark(row, col) && !qr_code.is_dark(row + 1, col) && qr_code.is_dark(row + 2, col) && qr_code.is_dark(row + 3, col) && qr_code.is_dark(row + 4, col) && !qr_code.is_dark(row + 5, col) && qr_code.is_dark(row + 6, col)
230: lost_point = lost_point + 40
231: end
232: end
233: end
234:
235: # level 4
236: dark_count = 0
237:
238: ( 0...module_count ).each do |col|
239: ( 0...module_count ).each do |row|
240: if qr_code.is_dark(row, col)
241: dark_count = dark_count + 1
242: end
243: end
244: end
245:
246: ratio = (100 * dark_count / module_count / module_count - 50).abs / 5
247: lost_point = lost_point * 10
248:
249: lost_point
250: end
# File lib/rqrcode/qrcode/qr_util.rb, line 100
100: def QRUtil.get_mask( mask_pattern, i, j )
101: case mask_pattern
102: when QRMASKPATTERN[:pattern000]
103: (i + j) % 2 == 0
104: when QRMASKPATTERN[:pattern001]
105: i % 2 == 0
106: when QRMASKPATTERN[:pattern010]
107: j % 3 == 0
108: when QRMASKPATTERN[:pattern011]
109: (i + j) % 3 == 0
110: when QRMASKPATTERN[:pattern100]
111: ((i / 2).floor + (j / 3).floor ) % 2 == 0
112: when QRMASKPATTERN[:pattern101]
113: (i * j) % 2 + (i * j) % 3 == 0
114: when QRMASKPATTERN[:pattern110]
115: ( (i * j) % 2 + (i * j) % 3) % 2 == 0
116: when QRMASKPATTERN[:pattern111]
117: ( (i * j) % 3 + (i + j) % 2) % 2 == 0
118: else
119: raise QRCodeRunTimeError, "bad mask_pattern: #{mask_pattern}"
120: end
121: end