Class: QrCodeMaker::QRUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/qr_code_maker/encoder.rb

Constant Summary collapse

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]
}.freeze
ARCH_BITS =
ENV.fetch("RQRCODE_CORE_ARCH_BITS", nil)&.to_i || 1.size * 8

Class Method Summary collapse

Class Method Details

.demerit_points_1_same_color(modules) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/qr_code_maker/encoder.rb', line 357

def self.demerit_points_1_same_color(modules)
  demerit_points = 0
  module_count = modules.size
  max_index = module_count - 1

  module_count.times do |row|
    modules_row = modules[row]
    module_count.times do |col|
      same_count = 0
      dark = modules_row[col]

      if row > 0
        row_above = modules[row - 1]
        same_count += 1 if col > 0 && dark == row_above[col - 1]
        same_count += 1 if dark == row_above[col]
        same_count += 1 if col < max_index && dark == row_above[col + 1]
      end

      same_count += 1 if col > 0 && dark == modules_row[col - 1]
      same_count += 1 if col < max_index && dark == modules_row[col + 1]

      if row < max_index
        row_below = modules[row + 1]
        same_count += 1 if col > 0 && dark == row_below[col - 1]
        same_count += 1 if dark == row_below[col]
        same_count += 1 if col < max_index && dark == row_below[col + 1]
      end

      demerit_points += (DEMERIT_POINTS_1 + same_count - 5) if same_count > 5
    end
  end
  demerit_points
end

.demerit_points_2_full_blocks(modules) ⇒ Object



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/qr_code_maker/encoder.rb', line 391

def self.demerit_points_2_full_blocks(modules)
  demerit_points = 0
  module_count = modules.size
  max_row = module_count - 1

  max_row.times do |row|
    row_curr = modules[row]
    row_next = modules[row + 1]

    max_row.times do |col|
      val = row_curr[col]
      if val == row_next[col] && val == row_curr[col + 1] && val == row_next[col + 1]
        demerit_points += DEMERIT_POINTS_2
      end
    end
  end
  demerit_points
end

.demerit_points_3_dangerous_patterns(modules) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/qr_code_maker/encoder.rb', line 410

def self.demerit_points_3_dangerous_patterns(modules)
  demerit_points = 0
  module_count = modules.size
  pattern_len = 7
  max_start = module_count - pattern_len + 1

  modules.each do |row|
    max_start.times do |col|
      if row[col] && !row[col + 1] && row[col + 2] &&
          row[col + 3] && row[col + 4] && !row[col + 5] && row[col + 6]
        demerit_points += DEMERIT_POINTS_3
      end
    end
  end

  module_count.times do |col|
    max_start.times 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

.demerit_points_4_dark_ratio(modules) ⇒ Object



436
437
438
439
440
441
442
443
# File 'lib/qr_code_maker/encoder.rb', line 436

def self.demerit_points_4_dark_ratio(modules)
  dark_count = modules.reduce(0) do |sum, col|
    sum + col.count(true)
  end
  ratio = dark_count.to_f / (modules.size * modules.size)
  ratio_delta = (100 * ratio - 50).abs / 5
  ratio_delta * DEMERIT_POINTS_4
end

.get_bch_digit(data) ⇒ Object



307
308
309
310
311
312
313
314
# File 'lib/qr_code_maker/encoder.rb', line 307

def self.get_bch_digit(data)
  digit = 0
  while data != 0
    digit += 1
    data = rszf(data, 1)
  end
  digit
end

.get_bch_format_info(data) ⇒ Object



287
288
289
290
291
292
293
# File 'lib/qr_code_maker/encoder.rb', line 287

def self.get_bch_format_info(data)
  d = data << 10
  while get_bch_digit(d) - get_bch_digit(G15) >= 0
    d ^= (G15 << (get_bch_digit(d) - get_bch_digit(G15)))
  end
  ((data << 10) | d) ^ G15_MASK
end

.get_bch_version(data) ⇒ Object



299
300
301
302
303
304
305
# File 'lib/qr_code_maker/encoder.rb', line 299

def self.get_bch_version(data)
  d = data << 12
  while get_bch_digit(d) - get_bch_digit(G18) >= 0
    d ^= (G18 << (get_bch_digit(d) - get_bch_digit(G18)))
  end
  (data << 12) | d
end

.get_error_correct_polynomial(error_correct_length) ⇒ Object



325
326
327
328
329
330
331
# File 'lib/qr_code_maker/encoder.rb', line 325

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

.get_length_in_bits(mode, version) ⇒ Object

Raises:



333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/qr_code_maker/encoder.rb', line 333

def self.get_length_in_bits(mode, version)
  raise QRCodeRunTimeError, "Unknown mode: #{mode}" unless QRMODE.value?(mode)
  raise QRCodeRunTimeError, "Unknown version: #{version}" if version > 40

  if version.between?(1, 9)
    macro_version = 0
  elsif version <= 26
    macro_version = 1
  elsif version <= 40
    macro_version = 2
  end

  BITS_FOR_MODE[mode][macro_version]
end

.get_lost_points(modules) ⇒ Object



348
349
350
351
352
353
354
355
# File 'lib/qr_code_maker/encoder.rb', line 348

def self.get_lost_points(modules)
  demerit_points = 0
  demerit_points += demerit_points_1_same_color(modules)
  demerit_points += demerit_points_2_full_blocks(modules)
  demerit_points += demerit_points_3_dangerous_patterns(modules)
  demerit_points += demerit_points_4_dark_ratio(modules)
  demerit_points
end

.get_mask(mask_pattern, i, j) ⇒ Object

Raises:



320
321
322
323
# File 'lib/qr_code_maker/encoder.rb', line 320

def self.get_mask(mask_pattern, i, j)
  raise QRCodeRunTimeError, "bad mask_pattern: #{mask_pattern}" if mask_pattern > QRMASKCOMPUTATIONS.size
  QRMASKCOMPUTATIONS[mask_pattern].call(i, j)
end

.get_pattern_positions(version) ⇒ Object



316
317
318
# File 'lib/qr_code_maker/encoder.rb', line 316

def self.get_pattern_positions(version)
  PATTERN_POSITION_TABLE[version - 1]
end

.max_sizeObject



283
284
285
# File 'lib/qr_code_maker/encoder.rb', line 283

def self.max_size
  PATTERN_POSITION_TABLE.count
end

.rszf(num, count) ⇒ Object



295
296
297
# File 'lib/qr_code_maker/encoder.rb', line 295

def self.rszf(num, count)
  (num >> count) & ((1 << (ARCH_BITS - count)) - 1)
end