Class: LogoSoup::Core::ImageLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/logosoup/core/image_loader.rb

Overview

Loads an image from disk and returns sampled RGBA bytes.

Constant Summary collapse

RGBA_CHANNELS =
4

Class Method Summary collapse

Class Method Details

.call(path:, pixel_budget:, on_error: nil) ⇒ Hash

Parameters:

  • path (String)
  • pixel_budget (Integer)
  • on_error (:raise, nil) (defaults to: nil)

Returns:

  • (Hash)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/logosoup/core/image_loader.rb', line 15

def self.call(path:, pixel_budget:, on_error: nil)
  image = Vips::Image.new_from_file(path, access: :sequential)
  original_width = image.width
  original_height = image.height

  sample_width, sample_height, image_small = downsample(image, pixel_budget: pixel_budget)
  rgba = ensure_rgba_uchar(image_small, on_error: on_error)

  bytes = rgba.write_to_memory.bytes
  raise "Empty image bytes" if bytes.empty?

  {
    bytes: bytes,
    original_width: original_width,
    original_height: original_height,
    sample_width: sample_width,
    sample_height: sample_height
  }
end