Class: Riiif::Resize

Inherits:
Object
  • Object
show all
Defined in:
app/services/riiif/resize.rb

Overview

Represents a resize operation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size, image_info) ⇒ Resize

Returns a new instance of Resize.

Parameters:

  • size (IIIF::Image::Size)

    the result the user requested

  • image_info


6
7
8
9
# File 'app/services/riiif/resize.rb', line 6

def initialize(size, image_info)
  @size = size
  @image_info = image_info
end

Instance Attribute Details

#image_infoObject (readonly)

Returns the value of attribute image_info.



11
12
13
# File 'app/services/riiif/resize.rb', line 11

def image_info
  @image_info
end

#sizeObject (readonly)

Returns the value of attribute size.



11
12
13
# File 'app/services/riiif/resize.rb', line 11

def size
  @size
end

Instance Method Details

#heightInteger

Returns the height in pixels.

Returns:

  • (Integer)

    the height in pixels



34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/services/riiif/resize.rb', line 34

def height
  case size
  when IIIF::Image::Size::Absolute
    size.height
  when IIIF::Image::Size::Percent
    image_info.height * Integer(size.percentage).to_f / 100
  when IIIF::Image::Size::Width
    size.height_for_aspect_ratio(image_info.aspect)
  else
    image_info.height
  end
end

#reduce(factor) ⇒ IIIF::Image::Size

This is used for a second resize by imagemagick after resizing by kdu. No need to scale most resize operations (only percent)

Parameters:

  • factor (Integer)

    to scale by

Returns:

  • (IIIF::Image::Size)

    a copy of self if factor is zero.



79
80
81
82
83
84
85
86
87
# File 'app/services/riiif/resize.rb', line 79

def reduce(factor)
  case size
  when IIIF::Image::Size::Percent
    pct = size.percentage * 2**factor
    IIIF::Image::Size::Percent.new(pct)
  else
    size.dup
  end
end

#reduce?Boolean

Should we reduce this image with KDU?

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
71
72
# File 'app/services/riiif/resize.rb', line 62

def reduce?
  case size
  when IIIF::Image::Size::Full, IIIF::Image::Size::Max
    false
  when IIIF::Image::Size::Absolute
    aspect_ratio = width.to_f / height
    in_delta?(image_info.aspect, aspect_ratio, 0.001)
  else
    true
  end
end

#reduction_factor(max_factor = 5) ⇒ Integer

Returns the reduction factor for this operation.

Returns:

  • (Integer)

    the reduction factor for this operation



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/services/riiif/resize.rb', line 90

def reduction_factor(max_factor = 5)
  return nil unless reduce?
  scale = [width.to_f / image_info.width,
           height.to_f / image_info.height].min
  factor = 0
  raise "I don't know how to scale to #{scale}" if scale > 1
  next_pct = 0.5
  while scale <= next_pct && factor < max_factor
    next_pct /= 2.0
    factor += 1
  end
  factor
end

#to_imagemagickString

Returns a resize directive for imagemagick to use.

Returns:

  • (String)

    a resize directive for imagemagick to use



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/riiif/resize.rb', line 14

def to_imagemagick
  case size
  when IIIF::Image::Size::Percent
    "#{size.percentage}%"
  when IIIF::Image::Size::Width
    size.width
  when IIIF::Image::Size::Height
    "x#{size.height}"
  when IIIF::Image::Size::Absolute
    "#{size.width}x#{size.height}!"
  when IIIF::Image::Size::BestFit
    "#{size.width}x#{size.height}"
  when IIIF::Image::Size::Max, IIIF::Image::Size::Full
    nil
  else
    raise "unknown size #{size.class}"
  end
end

#widthInteger

Returns the width in pixels.

Returns:

  • (Integer)

    the width in pixels



48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/services/riiif/resize.rb', line 48

def width
  case size
  when IIIF::Image::Size::Absolute
    size.width
  when IIIF::Image::Size::Percent
    image_info.width * Integer(size.percentage).to_f / 100
  when IIIF::Image::Size::Height
    size.width_for_aspect_ratio(image_info.aspect)
  else
    image_info.width
  end
end