Class: Monet::Compare

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/monet/compare.rb

Instance Method Summary collapse

Constructor Details

#initialize(strategy = ColorBlend) ⇒ Compare



10
11
12
# File 'lib/monet/compare.rb', line 10

def initialize(strategy=ColorBlend)
  @strategy_class = strategy
end

Instance Method Details

#compare(base_image, new_image) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/monet/compare.rb', line 14

def compare(base_image, new_image)
  puts "comparing #{base_image} with #{new_image}"
  begin
    new_png = ChunkyPNG::Image.from_file(new_image)
    base_png = ChunkyPNG::Image.from_file(base_image)

    diff_stats = []

    diff_strategy = @strategy_class.new(base_png, new_png)

    base_png.height.times do |y|
      base_png.row(y).each_with_index do |pixel, x|
        diff_strategy.calculate_for_pixel(pixel, x, y)
      end
    end

    changeset = Changeset.new(base_png, diff_strategy.score, new_image)
    diff_strategy.save(diff_filename(base_image)) if changeset.modified?

    changeset
  rescue Errno::ENOENT => e
    return BaselessImage.new(new_image)
  end
end