module PNG_Comparison

How to use:

1.Running from a command line

ruby cli_run.rb -h

or run a test run with
rake sample_run

2.Including this module to a class

 require 'png_comparison'

 class YourClass
   include PNG_Comparison
 end

 c = YourClass.new
 c.compare(img1: image1, img2: image2, diff_file: diff_file, threshold: threshold)

this will make available the method "PNG_Comparison::compare()" to your class. The parameters the same as above
img1,2,diff_file can be either filenames or instances of ChunkyPNG::Image class

3.Running compare() method directly from your program

require 'png_comparison'
...
PNG_Comparison::compare(img1: image1, img2: image2, diff_file: diff_file, threshold: threshold)

Tips and tricks:

For CRuby users: to speed up the program, please run:

* "gem install 'oily_png'" in cmd after.

this will make comparison run much faster for big images as it uses C extensions. But oily_png gem is only for C platform

For JRuby users:

Please adjust JVM memory when comparing big images to avoid OutOfMemory error :
 jruby -J-Xmx2200m png_comparison.rb [params]
or use  the above flag  when running your own scripts that will use the png_comparison gem.
Please bear in mind that the flag should be placed after 'jruby' but before your script name.
The above memory size should be enough for images like 1700x7200, which is quite a regular web page size today.

The main image difference algorithm is a kind of CIE76 (see https://en.wikipedia.org/wiki/Color_difference#CIE76).

Public Instance Methods

compare(img1:, img2:, diff_file:, threshold:) click to toggle source

This method will appear as an instance method if you include PNG_Comparison module to your class. will return true if images are identical, otherwize false

# File lib/png_comparison.rb, line 125
def compare(img1:, img2:, diff_file:, threshold:)
  begin
    start = Time.now
    comparison = PNG_Comparison::Comparator.compare_images(img1: img1, img2: img2, diff_file: diff_file, threshold: threshold.to_f)
    finish = Time.now
    puts "Comparison took #{finish - start} seconds."
  rescue Exception => e
    puts "This error has occured: #{e.message}"
    puts "The backtrace is #{e.backtrace}"
    return nil
  end
  comparison
end