Module: Flipbook::Diff

Defined in:
lib/flipbook/diff.rb,
sig/flipbook.rbs

Class Method Summary collapse

Class Method Details

.bounds(previous, current) ⇒ [Integer, Integer, Integer, Integer]?

Returns the smallest changed rectangle, or nil when the images match.

Parameters:

Returns:

  • ([Integer, Integer, Integer, Integer], nil)


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
# File 'lib/flipbook/diff.rb', line 8

def bounds(previous, current)
  unless previous.is_a?(Tessel::Image) && current.is_a?(Tessel::Image)
    raise TypeError, "frames must be Tessel::Image values"
  end
  unless previous.width == current.width && previous.height == current.height
    raise ArgumentError, "frame dimensions must match"
  end

  left = current.width
  top = current.height
  right = -1
  bottom = -1
  before = previous.bytes
  after = current.bytes
  current.height.times do |y|
    current.width.times do |x|
      offset = (y * current.width + x) * 4
      next if before.byteslice(offset, 4) == after.byteslice(offset, 4)

      left = x if x < left
      top = y if y < top
      right = x if x > right
      bottom = y if y > bottom
    end
  end
  right.negative? ? nil : [left, top, right - left + 1, bottom - top + 1]
end

.crop(image, rectangle) ⇒ Object



36
37
38
39
# File 'lib/flipbook/diff.rb', line 36

def crop(image, rectangle)
  x, y, width, height = rectangle
  image.crop(x, y, width, height)
end