Class: CuteRB::Utils

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

Constant Summary collapse

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. ++

[
  [],
  [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]
]

Class Method Summary collapse

Class Method Details

.contents_area(canny) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# 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) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 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) ⇒ Object

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. ++



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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) ⇒ Object

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. ++



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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