Class: Aspera::Cli::TerminalImage::Backend::RMagick

Inherits:
Base
  • Object
show all
Defined in:
lib/aspera/cli/terminal_image.rb

Instance Method Summary collapse

Methods inherited from Base

#terminal_scaling

Constructor Details

#initialize(blob, **kwargs) ⇒ RMagick

Initialize the RMagick-backed decoder for a binary image payload.

Parameters:

  • blob (String) —

    encoded image binary content

  • kwargs (Hash) —

    forwarding options accepted by initialize



56
57
58
59
60
61
# File 'lib/aspera/cli/terminal_image.rb', line 56

def initialize(blob, **kwargs)
  super(**kwargs)
  # Load lazily because this dependency is optional.
  require 'rmagick' # https://rmagick.github.io/index.html
  @image = Magick::ImageList.new.from_blob(blob)
end

Instance Method Details

#terminal_pixels ⇒ Array<Array<Array<Integer>>>

Decode the image and return RGB pixels scaled for terminal rendering.

Returns:

  • (Array<Array<Array<Integer>>>) —

    rows of [red, green, blue] pixel triplets



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/aspera/cli/terminal_image.rb', line 66

def terminal_pixels
  # ImageMagick channel depth is typically 8 or 16 bits.
  # See: `magick xc: -format "%q" info:`
  shift_for_8_bit = Magick::MAGICKCORE_QUANTUM_DEPTH - 8
  # Extract RGB values and normalize them to 8-bit channels 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)
    # Initialize the destination 2D pixel matrix row by row.
    pixel_colors[row] ||= []
    pixel_colors[row][col] = pixel_rgb
  end
  pixel_colors
end