Class: Imogen::Iiif::Region::Featured

Inherits:
Transform
  • Object
show all
Defined in:
lib/imogen/iiif/region.rb

Constant Summary collapse

SQUARISH =
5.to_f / 6
ONE_THIRD =
1.to_f / 3

Class Method Summary collapse

Methods inherited from Transform

#initialize, #max, #min

Constructor Details

This class inherits a constructor from Imogen::Iiif::Transform

Class Method Details

.convert(img, scale = 768, opts = {}) {|smartcrop.thumbnail_image(scale, height: scale)| ... } ⇒ Object

Yields:

  • (smartcrop.thumbnail_image(scale, height: scale))


59
60
61
62
63
64
65
66
67
68
69
# File 'lib/imogen/iiif/region.rb', line 59

def self.convert(img, scale = 768, opts = {})
  middle_dims = [(img.width * 2 * ONE_THIRD).floor, (img.height * 2 * ONE_THIRD).floor]
  x_offset = (img.width * ONE_THIRD/2).floor
  y_offset = (img.height * ONE_THIRD/2).floor
  crop_scale = middle_dims.min
  smart_crop_opts = {interesting: (squarish?(img) ? :centre : :entropy)}.merge(opts)
  window = img.extract_area(x_offset, y_offset, middle_dims[0], middle_dims[1])
  smartcrop = window.smartcrop(crop_scale, crop_scale, **smart_crop_opts)
  # Vips counts with negative offsets from left and top
  yield smartcrop.thumbnail_image(scale, height: scale)
end

.get(img, scale = 768, opts = {}) ⇒ Object

returns leftX, topY, rightX, bottomY



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/imogen/iiif/region.rb', line 72

def self.get(img, scale = 768, opts = {})
  middle_dims = [(img.width * 2 * ONE_THIRD).floor, (img.height * 2 * ONE_THIRD).floor]
  x_offset = (img.width * ONE_THIRD/2).floor
  y_offset = (img.height * ONE_THIRD/2).floor
  crop_scale = middle_dims.min
  smart_crop_opts = {interesting: (squarish?(img) ? :centre : :entropy)}.merge(opts)
  window = img.extract_area(x_offset, y_offset, middle_dims[0], middle_dims[1])
  smartcrop = window.smartcrop(crop_scale, crop_scale, **smart_crop_opts)
  # Vips counts with negative offsets from left and top
  left = (window.xoffset + smartcrop.xoffset)*-1
  top = (window.yoffset + smartcrop.yoffset)*-1
  right = left + smartcrop.width
  bottom = top + smartcrop.height
  return [left, top, right, bottom]
end

.squarish?(img) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
# File 'lib/imogen/iiif/region.rb', line 88

def self.squarish?(img)
  if img.is_a? Vips::Image
    dims = [img.width, img.height]
    ratio = dims.min.to_f / dims.max
    return ratio >= Featured::SQUARISH
  else
    raise "#{img.class.name} is not a Vips::Image"
  end
end