Class: Imaginator::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/imsg-grep/images/imaginator.rb

Defined Under Namespace

Classes: Fit

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Image

Returns a new instance of Image.



17
# File 'lib/imsg-grep/images/imaginator.rb', line 17

def initialize(path) = @path = path

Instance Method Details

#dimensionsObject



18
# File 'lib/imsg-grep/images/imaginator.rb', line 18

def dimensions       = @img.dimensions

#fit(cols, rows) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/imsg-grep/images/imaginator.rb', line 53

def fit cols, rows
  cfit = fit_cols(cols)
  rfit = fit_rows(rows)
  smol = [cfit, rfit].sort_by { [it.w, it.h] }.first
  # ^ they're proportional so whichever smallest is the one containable in both dimensions
  [smol, cfit:, rfit:]
end

#fit_cols(target_cols) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/imsg-grep/images/imaginator.rb', line 35

def fit_cols target_cols
  w_cell, h_cell = Imaginator.cell_size
  w_img,  h_img  = dimensions
  w = target_cols * w_cell # target width in px
  h = w / w_img * h_img    # target height in px
  r = (h / h_cell).ceil    # target rows
  Fit.new w:w.floor, h:h.floor, c:target_cols, r:r, pad_h:(r*h_cell), pad_w:w
end

#fit_rows(target_rows) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/imsg-grep/images/imaginator.rb', line 44

def fit_rows target_rows
  w_cell, h_cell = Imaginator.cell_size
  w_img,  h_img  = dimensions
  h = target_rows * h_cell # target height in px
  w = h / h_img * w_img    # target width in px
  c = (w / w_cell).ceil    # target cols
  Fit.new w:w.floor, h:h.floor, c:c, r:target_rows, pad_w:(c*w_cell), pad_h:h
end

#loadObject



21
22
23
24
25
26
# File 'lib/imsg-grep/images/imaginator.rb', line 21

def load
  return unless File.exist? @path
  # @img ||= Img2png::Image.new path: @path # do the the IO in swift, turns out not any faster
  @img ||= Img2png::Image.new data: IO.binread(@path)
  self
end

#png_transform(w:, h:, pad_h: nil, pad_w: nil) ⇒ Object



28
29
30
31
32
33
# File 'lib/imsg-grep/images/imaginator.rb', line 28

def png_transform w:, h:, pad_h:nil, pad_w:nil
  pad = pad_h || pad_w
  pad_w ||= w if pad
  pad_h ||= h if pad
  @img.convert fit_w:w, fit_h:h, box_w:pad_w, box_h:pad_h
end

#releaseObject



19
# File 'lib/imsg-grep/images/imaginator.rb', line 19

def release          = @img.release