Module: ChunkyPNG::Canvas::Operations

Includes:
ChunkyPNG::Color
Defined in:
lib/chunky_png/operations.rb

Overview

Module operations

Instance Method Summary collapse

Instance Method Details

#compare_imgs(pattern_img) ⇒ Float, Array

Performs the image comparison pixel by pixel

Parameters:

  • result_img (String)

    complete path to where is intended save the image

Returns:

  • (Float)

    Percentual difference between the two images

  • (Array)

    Difference image



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/chunky_png/operations.rb', line 13

def compare_imgs(pattern_img)
  fail('height doesn\'t match betwwen the two images') unless pattern_img.height == height
  fail('width doesn\'t match betwwen the two images') unless pattern_img.width == width

  output = ChunkyPNG::Image.new(pattern_img.width, pattern_img.height, WHITE)

  diff = [0]
  pattern_img.height.times do |y|
    pattern_img.row(y).each_with_index do |pixel, x|
      next if pixel == self[x, y]
      score = pixel_difference(pixel, self[x, y])
      output[x, y] = ChunkyPNG.grayscale(MAX - (score * MAX).round)
      diff << score
    end
  end

  # Returns percentage difference and difference image
  [diff.inject { |a, e| a + e } / pattern_img.pixels.length * 100, output]
end