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

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

Parameters:

  • blob (String)

    encoded image binary content

  • kwargs (Hash)

    forwarding options accepted by [‘initialize`](lib/aspera/preview/terminal.rb:16)



43
44
45
46
47
48
# File 'lib/aspera/preview/terminal.rb', line 43

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_pixelsArray<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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/aspera/preview/terminal.rb', line 53

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