Class: ImageCompare::Modes::Base

Inherits:
Object
  • Object
show all
Includes:
ColorMethods
Defined in:
lib/image_compare/modes/base.rb

Direct Known Subclasses

Color, Delta, Grayscale, RGB

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ColorMethods

#blue, #brightness, #green, #orange, #red, #transparent, #yellow

Constructor Details

#initialize(threshold: 0.0, lower_threshold: 0.0, exclude_rect: nil, include_rect: nil) ⇒ Base

Returns a new instance of Base.



11
12
13
14
15
16
17
# File 'lib/image_compare/modes/base.rb', line 11

def initialize(threshold: 0.0, lower_threshold: 0.0, exclude_rect: nil, include_rect: nil)
  @include_rect = Rectangle.new(*include_rect) unless include_rect.nil?
  @exclude_rect = Rectangle.new(*exclude_rect) unless exclude_rect.nil?
  @threshold = threshold
  @lower_threshold = lower_threshold
  @result = Result.new(self, threshold: threshold, lower_threshold: lower_threshold)
end

Instance Attribute Details

#boundsObject (readonly)

Returns the value of attribute bounds.



9
10
11
# File 'lib/image_compare/modes/base.rb', line 9

def bounds
  @bounds
end

#exclude_rectObject (readonly)

Returns the value of attribute exclude_rect.



9
10
11
# File 'lib/image_compare/modes/base.rb', line 9

def exclude_rect
  @exclude_rect
end

#include_rectObject (readonly)

Returns the value of attribute include_rect.



9
10
11
# File 'lib/image_compare/modes/base.rb', line 9

def include_rect
  @include_rect
end

#lower_thresholdObject (readonly)

Returns the value of attribute lower_threshold.



9
10
11
# File 'lib/image_compare/modes/base.rb', line 9

def lower_threshold
  @lower_threshold
end

#resultObject (readonly)

Returns the value of attribute result.



9
10
11
# File 'lib/image_compare/modes/base.rb', line 9

def result
  @result
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



9
10
11
# File 'lib/image_compare/modes/base.rb', line 9

def threshold
  @threshold
end

Instance Method Details

#areaObject



59
60
61
62
63
# File 'lib/image_compare/modes/base.rb', line 59

def area
  area = include_rect.area
  return area if exclude_rect.nil?
  area - exclude_rect.area
end

#compare(a, b) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/image_compare/modes/base.rb', line 19

def compare(a, b)
  result.image = a
  @include_rect ||= a.bounding_rect
  @bounds = Rectangle.new(*include_rect.bounds)

  b.compare_each_pixel(a, area: include_rect) do |b_pixel, a_pixel, x, y|
    next if !exclude_rect.nil? && exclude_rect.contains_point?(x, y)
    next if pixels_equal?(b_pixel, a_pixel)
    update_result(b_pixel, a_pixel, x, y)
  end

  result.score = score
  result
end

#diff(bg, diff) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/image_compare/modes/base.rb', line 34

def diff(bg, diff)
  diff_image = background(bg).highlight_rectangle(exclude_rect, :blue)
  diff.each do |pixels_pair|
    pixels_diff(diff_image, *pixels_pair)
  end
  create_diff_image(bg, diff_image)
    .highlight_rectangle(bounds)
    .highlight_rectangle(include_rect, :green)
end

#scoreObject



44
45
46
# File 'lib/image_compare/modes/base.rb', line 44

def score
  result.diff.length.to_f / area
end

#update_bounds(x, y) ⇒ Object



52
53
54
55
56
57
# File 'lib/image_compare/modes/base.rb', line 52

def update_bounds(x, y)
  bounds.left = [x, bounds.left].max
  bounds.top = [y, bounds.top].max
  bounds.right = [x, bounds.right].min
  bounds.bot = [y, bounds.bot].min
end

#update_result(*_args, x, y) ⇒ Object



48
49
50
# File 'lib/image_compare/modes/base.rb', line 48

def update_result(*_args, x, y)
  update_bounds(x, y)
end