Class: Facy::ImageViewer::Image

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

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Image

Returns a new instance of Image.



27
28
29
30
31
32
# File 'lib/facy/image_viewer.rb', line 27

def initialize(path, options={})
  @image = Magick::ImageList.new(path)
  @fit_terminal = !!options[:fit_terminal]
rescue Exception => e
  p e.message
end

Instance Method Details

#ansi_analyzeObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/facy/image_viewer.rb', line 57

def ansi_analyze
  raise "use rgb_analyze before ansi_analyze" unless @rgb
  ret = []
  @rgb.map! do |row|
    ret << row.map{|pixcel|
      AnsiRGB.wrap_with_code(@double ? "  " : " ", pixcel)
    }.join
  end
  @ansi = ret.join("\n")
rescue Exception => e
  error e.message
end

#drawObject



34
35
36
37
38
39
# File 'lib/facy/image_viewer.rb', line 34

def draw
  convert_to_fit_terminal_size if @fit_terminal
  rgb_analyze
  ansi_analyze
  puts_ansi
end

#get_term_sizeObject



75
76
77
# File 'lib/facy/image_viewer.rb', line 75

def get_term_size
  `stty size`.split(" ").map(&:to_i)
end

#puts_ansiObject



70
71
72
73
# File 'lib/facy/image_viewer.rb', line 70

def puts_ansi
  raise "use ansi_analyze before to_ansi" unless @ansi
  puts @ansi
end

#rgb_analyzeObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/facy/image_viewer.rb', line 41

def rgb_analyze
  @rgb = []
  cols = @image.columns
  rows = @image.rows
  rows.times do |y|
    cols.times do |x|
      @rgb[y] ||= []
      pixcel = @image.pixel_color(x, y)
      r = pixcel.red / 256
      g = pixcel.green / 256
      b = pixcel.blue / 256
      @rgb[y] << [r, g, b]
    end
  end
end