Class: Biv::Image

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

Overview

An image to be printed in a blockily

Instance Method Summary collapse

Constructor Details

#initialize(file, width: nil, height: nil, true_color: false, hd: true, bg: [0xFF, 0xFF, 0xFF]) ⇒ Image

Load a new image for viewing



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/biv.rb', line 10

def initialize(file, width: nil, height: nil, true_color: false, hd: true, bg: [0xFF, 0xFF, 0xFF])
  fail ArgumentError, 'Missing image width' if width.nil?

  begin
    @original_image = Magick::Image.read(file).first
  rescue
    raise ArgumentError, "I don't know how to open '#{file}'"
  end
  @width = width
  @height = height
  @true_color = true_color
  @hd = hd
  @bg = bg

  @height *= 2 if !@height.nil? && @hd
  preprocess

  fail ArgumentError, 'I failed to load the image' if @image.nil?
end

Instance Method Details

#to_aObject

Convert an image to an array of printable strings



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/biv.rb', line 31

def to_a
  buffer = []

  for_row_indexes.each do |y|
    line = ''
    0.upto(@image.columns - 1).each do |x|
      p = get_pixel(x, y)
      block = make_block p
      line << block
    end
    line << "\x1b[0m" # Reset the color at the end of the line
    buffer << line
  end

  buffer
end

#to_sObject

Convert an image to a string



49
50
51
# File 'lib/biv.rb', line 49

def to_s
  to_a.join "\n"
end