Class: Imogen::Iiif::Region

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

Defined Under Namespace

Classes: Featured

Class Method Summary collapse

Instance 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, region) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/imogen/iiif/region.rb', line 41

def self.convert(img, region)
  edges = Region.new(img).get(region)
  if edges.nil?
    yield img
  else
    if edges == :featured
      side = [img.width, img.height,768].min
      Featured.convert(img, side) { |x| yield x }
    else
      # edges are leftX, topY, rightX, bottomY
      # Vips wants left, top, width, height
      yield img.extract_area(edges[0], edges[1], edges[2] - edges[0], edges[3] - edges[1])
    end
  end
end

Instance Method Details

#get(region = nil) ⇒ Object

returns leftX, topY, rightX, bottomY



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/imogen/iiif/region.rb', line 6

def get(region=nil)
  if region.nil? || region.to_s == 'full'
    return nil
  elsif region.to_s == 'featured'
    return :featured
  elsif md = /^pct:(\d+(\.\d+)?),(\d+(\.\d+)?),(\d+(\.\d+)?),(\d+(\.\d+)?)$/.match(region)
    p = [Float(md[1]),Float(md[3]),Float(md[5]),Float(md[7])]
    if p[2] == 0 or p[3] == 0
      raise BadRequest.new("Invalid region: #{region}")
    end
    e = [
      max(0,(@width * p[0] / 100).round),
      max(0,(@height * p[1] / 100).round),
      min(@width,(@width * (p[0] + p[2]) / 100).round),
      min(@height,(@height * (p[1] + p[3]) / 100).round)
    ]
  elsif md = /^(\d+),(\d+),(\d+),(\d+)$/.match(region)
    p = [Integer(md[1]),Integer(md[2]),Integer(md[3]),Integer(md[4])]
    if p[2] == 0 or p[3] == 0
      raise BadRequest.new("Invalid region: #{region}")
    end
    e = [
      max(0,p[0]),
      max(0,p[1]),
      min(@width,(p[0] + p[2])),
      min(@height,(p[1] + p[3]))
    ]
  else
    raise BadRequest.new("Invalid region (syntax): #{region}")
  end
  if (e[0] > @width or e[1] > @height)
    raise BadRequest.new("Invalid region (disjoint): #{region}")
  end
  return e
end