class RETerm::Components::Image

Renders and image to the screen using drawille and chunkpng.

Public Class Methods

new(args={}) click to toggle source

Initialize the Image component

@param [Hash] args image params @option args [String] :file path to the file containing

the image
Calls superclass method RETerm::Component::new
# File lib/reterm/components/image.rb, line 11
def initialize(args={})
  super
  @file = args[:file]
end

Public Instance Methods

draw!() click to toggle source
# File lib/reterm/components/image.rb, line 16
def draw!
  refresh_win
end
requeseted_rows() click to toggle source
# File lib/reterm/components/image.rb, line 20
def requeseted_rows
  image.dimension.height
end
requested_cols() click to toggle source
# File lib/reterm/components/image.rb, line 24
def requested_cols
  image.dimension.width
end

Protected Instance Methods

canvas() click to toggle source
# File lib/reterm/components/image.rb, line 30
def canvas
  require 'drawille'
  @canvas ||= Drawille::Canvas.new
end
image() click to toggle source
# File lib/reterm/components/image.rb, line 35
def image
  require 'chunky_png'
  @image ||= ChunkyPNG::Image.from_file(@file)
end
refresh_win() click to toggle source
# File lib/reterm/components/image.rb, line 40
def refresh_win
  w = [image.dimension.width-1,  window.cols].min
  h = [image.dimension.height-1, window.rows].min

  (0..w).each do |x|
    (0..h).each do |y|
      r = ChunkyPNG::Color.r(image[x,y])
      g = ChunkyPNG::Color.g(image[x,y])
      b = ChunkyPNG::Color.b(image[x,y])
      canvas.set(x, y) if (r + b + g) > 100
    end
  end

  y = 1
  canvas.frame.split("\n").each { |line|
    window.mvaddstr(y, 1, line)
    y += 1
  }

  window.refresh
  update_reterm
end