class CuteRB::Utils

Constants

PATTERN_POSITION_TABLE
Permission is granted for use, copying, modification, distribution,
and distribution of modified versions of this work as long as the
above copyright notice is included.

++

Public Class Methods

contents_area(canny) click to toggle source
# File lib/utils.rb, line 131
def self.contents_area(canny)
  short = [canny.columns, canny.rows].min
  long = [canny.columns, canny.rows].max
  integral = Array.new(long)
  canny.rotate!(90, '>')

  for y in 0...canny.rows
    integral[y] = canny.export_pixels(0, y, canny.columns, 1, 'R').map{|px| px == 65535 ? 1 : 0}.sum(integral[y-1] || 0)
  end

  start = 0
  count = 0
  for offset in 0...(long-short)
    c = integral[short + offset] - (integral[offset - 1] || 0)
    if c > count
      start = offset
      count = c
    end
  end

  return start
end
dump(data) click to toggle source
# File lib/utils.rb, line 110
def self.dump(data)
  length = Math.sqrt(data.length).to_i
  for row in 0...length
    for col in 0...length
      case data[row * length + col]
      when 'b'
        print "\e[47m  \e[m"
      when 'w'
        print "\e[40m  \e[m"
      when 'B'
        print "\e[30m\e[47m--\e[m\e[m"
      when 'W'
        print "\e[37m\e[40m--\e[m\e[m"
      else
        p data[row * length + col]
      end
    end
    puts ""
  end
end
place_position_adjust_pattern(dst, version) click to toggle source
Permission is granted for use, copying, modification, distribution,
and distribution of modified versions of this work as long as the
above copyright notice is included.

++

# File lib/utils.rb, line 91
def self.place_position_adjust_pattern(dst, version)
  length = Math.sqrt(dst.length).to_i
  positions = PATTERN_POSITION_TABLE[version - 1]

  positions.each do |row|
    positions.each do |col|
      next if dst[row * length + col] == dst[row * length + col].upcase

      ( -2..2 ).each do |r|
        ( -2..2 ).each do |c|
          y = row + r
          x = col + c
          dst[y * length + x] = (r.abs == 2 || c.abs == 2 || (r == 0 && c == 0)) ? 'B' : 'W'
        end
      end
    end
  end
end
place_position_probe_pattern(dst, row, col) click to toggle source
Permission is granted for use, copying, modification, distribution,
and distribution of modified versions of this work as long as the
above copyright notice is included.

++

# File lib/utils.rb, line 64
def self.place_position_probe_pattern(dst, row, col)
  length = Math.sqrt(dst.length).to_i
  (-1..7).each do |r|
    next if !(row + r).between?(0, length - 1)

    (-1..7).each do |c|
      next if !(col + c).between?(0, length - 1)

      is_vert_line = (r.between?(0, 6) && (c == 0 || c == 6))
      is_horiz_line = (c.between?(0, 6) && (r == 0 || r == 6))
      is_square = r.between?(2,4) && c.between?(2, 4)
      y = row + r
      x = col + c
      dst[y * length + x] = (is_vert_line || is_horiz_line || is_square) ? 'B' : 'W'
    end
  end
end