class Vamp::Graphic::Transfer5

Transfer dotter data into ASCII

Constants

BACKSLASH
BACKTICK
DOUBLEQUOTES
FULLSTOP
HASH
MINUS
PIPE
SINGLEQUOTE
SLASH
SPACE
STAR
UNDERSCORE

Attributes

char_height[R]
char_width[R]
context[R]
mapping[R]

Public Class Methods

new(context) click to toggle source
# File lib/vamp/graphic/transfer5.rb, line 107
def initialize(context)
  @context = context
  @char_width = 5
  @char_height = 5
  @mapping = {
      " "  => create_pattern(SPACE),
      "/"  => create_pattern(SLASH),
      "\\" => create_pattern(BACKSLASH),
      "`"  => create_pattern(BACKTICK),
      "|"  => create_pattern(PIPE),
      "-"  => create_pattern(MINUS),
      "_"  => create_pattern(UNDERSCORE),
      "."  => create_pattern(FULLSTOP),
      "\"" => create_pattern(DOUBLEQUOTES),
      "'"  => create_pattern(SINGLEQUOTE),
      "*"  => create_pattern(STAR),
      "\#" => create_pattern(HASH),
  }
end

Public Instance Methods

ascii() click to toggle source
# File lib/vamp/graphic/transfer5.rb, line 186
      def ascii
        a = ""
        (context.height / char_height).times do |y|
          (context.width / char_width).times do |x|
            m = get_matching(get_pattern(x * char_width, y * char_height))
            if (m[1] > 0)
              n = []
              n << get_matching(get_pattern(x * char_width + 1, y * char_height), true)
              n << get_matching(get_pattern(x * char_width - 1, y * char_height), true)
              n << get_matching(get_pattern(x * char_width, y * char_height + 1), true)
              n << get_matching(get_pattern(x * char_width, y * char_height - 1), true)
=begin
              n << get_matching(get_pattern(x * char_width + 2, y * char_height), true)
              n << get_matching(get_pattern(x * char_width - 2, y * char_height), true)
              n << get_matching(get_pattern(x * char_width, y * char_height + 2), true)
              n << get_matching(get_pattern(x * char_width, y * char_height - 2), true)
=end
              n.each do |v|
                if v[1] < m[1]
                  m = v
                end
              end
            end
            a += m[0]
          end
          a += "\n"
        end
        a.chomp
      end
create_data(pattern) click to toggle source
# File lib/vamp/graphic/transfer5.rb, line 127
def create_data(pattern)
  a = pattern.split("\n")
  fail "pattern has wrong height" if a.size != char_height
  char_height.times do |dy|
    fail "pattern has wrong width" if a[dy].size != char_width
  end
  a
end
create_pattern(pattern) click to toggle source
# File lib/vamp/graphic/transfer5.rb, line 136
def create_pattern(pattern)
  char = TextDotter.new(char_width, char_height)
  a = create_data(pattern)
  char_height.times do |y|
    char_width.times do |x|
      char.dot(x, y) if a[y][x] == "X"
    end
  end
  char
end
difference(pattern1, pattern2) click to toggle source
# File lib/vamp/graphic/transfer5.rb, line 162
def difference(pattern1, pattern2)
  m = 0
  char_width.times do |dx|
    char_height.times do |dy|
      m += 1 if pattern1.dot?(dx, dy) != pattern2.dot?(dx, dy)
    end
  end
  m
end
get_matching(pattern, without_blank = false) click to toggle source
# File lib/vamp/graphic/transfer5.rb, line 172
def get_matching(pattern, without_blank = false)
  ranking = {}
  mapping.each do |k, v|
    next if without_blank and v == " "
    r = difference(pattern, v)
    ranking[k] = r
  end
  match = ranking.min_by{|k, v| v}
  # require "pp"
  # pp match
  match[0..1]
end
get_pattern(x, y) click to toggle source
# File lib/vamp/graphic/transfer5.rb, line 147
def get_pattern(x, y)
  pattern = ""
  char_height.times do |dy|
    char_width.times do |dx|
      if context.in?(x + dx, y + dy)
        pattern += (context.dot?(x + dx, y + dy) ? "X" : "_")
      else
        pattern += "_"
      end
    end
    pattern += "\n"
  end
  create_pattern(pattern.strip)
end