Class: Informers::Utils::RawImage

Inherits:
Object
  • Object
show all
Defined in:
lib/informers/utils/image.rb

Constant Summary collapse

RESAMPLING_MAPPING =
{
  0 => "nearest",
  1 => "lanczos",
  2 => "bilinear",
  3 => "bicubic",
  4 => "box",
  5 => "hamming"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image) ⇒ RawImage

Returns a new instance of RawImage.



15
16
17
18
19
20
# File 'lib/informers/utils/image.rb', line 15

def initialize(image)
  @image = image
  @width = image.width
  @height = image.height
  @channels = image.bands
end

Instance Attribute Details

#channelsObject (readonly)

Returns the value of attribute channels.



13
14
15
# File 'lib/informers/utils/image.rb', line 13

def channels
  @channels
end

#heightObject (readonly)

Returns the value of attribute height.



13
14
15
# File 'lib/informers/utils/image.rb', line 13

def height
  @height
end

#imageObject (readonly)

Returns the value of attribute image.



13
14
15
# File 'lib/informers/utils/image.rb', line 13

def image
  @image
end

#widthObject (readonly)

Returns the value of attribute width.



13
14
15
# File 'lib/informers/utils/image.rb', line 13

def width
  @width
end

Class Method Details

.from_array(input) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/informers/utils/image.rb', line 100

def self.from_array(input)
  c, h, w = Utils.dims(input)
  pixel_data = Array.new(w * h * c)

  input.each_with_index do |cv, ci|
    cv.each_with_index do |hv, hi|
      hv.each_with_index do |v, wi|
        pixel_data[(hi * w * c) + (wi * c) + ci] = v
      end
    end
  end

  RawImage.new(Vips::Image.new_from_memory_copy(pixel_data.pack("C*"), w, h, c, :uchar))
end

.read(input) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/informers/utils/image.rb', line 86

def self.read(input)
  if input.is_a?(RawImage)
    input
  elsif input.is_a?(URI)
    require "open-uri"

    RawImage.new(Vips::Image.new_from_buffer(input.read, ""))
  elsif input.is_a?(String)
    RawImage.new(Vips::Image.new_from_file(input))
  else
    raise ArgumentError, "Unsupported input type: #{input.class.name}"
  end
end

Instance Method Details

#center_crop(crop_width, crop_height) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/informers/utils/image.rb', line 47

def center_crop(crop_width, crop_height)
  # If the image is already the desired size, return it
  if @width == crop_width && @height == crop_height
    return self
  end

  # Determine bounds of the image in the new canvas
  width_offset = (@width - crop_width) / 2.0
  height_offset = (@height - crop_height) / 2.0

  if width_offset >= 0 && height_offset >= 0
    # Cropped image lies entirely within the original image
    img = @image.crop(
      width_offset.floor,
      height_offset.floor,
      crop_width,
      crop_height
    )
  elsif width_offset <= 0 && height_offset <= 0
    raise Todo
  else
    raise Todo
  end

  RawImage.new(img)
end

#dataObject



22
23
24
# File 'lib/informers/utils/image.rb', line 22

def data
  @image.write_to_memory.unpack("C*")
end

#resize(width, height, resample: 2) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/informers/utils/image.rb', line 30

def resize(width, height, resample: 2)
  resample_method = RESAMPLING_MAPPING[resample] || resample

  case resample_method
  when "bilinear", "bicubic"
    img =
      @image.affine(
        [width / @width.to_f, 0, 0, height / @height.to_f],
        interpolate: Vips::Interpolate.new(resample_method.to_sym)
      )
  else
    raise Todo
  end

  RawImage.new(img)
end

#rgbObject

Raises:



74
75
76
77
78
79
80
# File 'lib/informers/utils/image.rb', line 74

def rgb
  if @channels == 3
    return self
  end

  raise Todo
end

#save(path) ⇒ Object



82
83
84
# File 'lib/informers/utils/image.rb', line 82

def save(path)
  @image.write_to_file(path)
end

#sizeObject



26
27
28
# File 'lib/informers/utils/image.rb', line 26

def size
  [@width, @height]
end