Module: Barf

Defined in:
lib/barf.rb,
lib/barf/version.rb

Constant Summary collapse

VERSION =
"1.0.3"

Class Method Summary collapse

Class Method Details



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/barf.rb', line 11

def self.print(path)
  image = MiniMagick::Image.open(path)

  terminal_width = `tput cols`.to_i

  # Height must be divisible by 2 for the half pixels to be clean.
  new_height     = [2, (image.height * (0.5 * terminal_width / image.width)).to_i * 2].max

  image.combine_options do |tmp|
    tmp.alpha 'remove'
    tmp.flatten
    tmp.resize "#{terminal_width}x#{new_height}!"
    tmp.dither 'FloydSteinberg'
    tmp.remap @palette
  end

  # Two dimensional array of pixels:
  image.get_pixels.each_slice(2) do |top, bottom|
    out = Parallel.map_with_index(top) do |pixel, index|
      "\u2584".bg(pixel).fg(bottom[index])
    end.join
    puts out
  end
  return nil
end