Class: Aspera::Preview::Backend::RMagick

Inherits:
Base
  • Object
show all
Defined in:
lib/aspera/preview/terminal.rb

Instance Method Summary collapse

Methods inherited from Base

#terminal_scaling

Constructor Details

#initialize(blob, **kwargs) ⇒ RMagick

Returns a new instance of RMagick.



32
33
34
35
36
37
# File 'lib/aspera/preview/terminal.rb', line 32

def initialize(blob, **kwargs)
  super(**kwargs)
  # do not require statically, as the package is optional
  require 'rmagick' # https://rmagick.github.io/index.html
  @image = Magick::ImageList.new.from_blob(blob)
end

Instance Method Details

#terminal_pixelsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/aspera/preview/terminal.rb', line 39

def terminal_pixels
  # quantum depth is 8 or 16, see: `magick xc: -format "%q" info:`
  shift_for_8_bit = Magick::MAGICKCORE_QUANTUM_DEPTH - 8
  # get all pixel colors, adjusted for Rainbow
  pixel_colors = []
  @image.scale(*terminal_scaling(@image.rows, @image.columns)).each_pixel do |pixel, col, row|
    pixel_rgb = [pixel.red, pixel.green, pixel.blue]
    pixel_rgb = pixel_rgb.map{ |color| color >> shift_for_8_bit} unless shift_for_8_bit.eql?(0)
    # init 2-dim array
    pixel_colors[row] ||= []
    pixel_colors[row][col] = pixel_rgb
  end
  pixel_colors
end