lib/capybara/screenshot/diff/image_compare.rb in capybara-screenshot-diff-0.1.0 vs lib/capybara/screenshot/diff/image_compare.rb in capybara-screenshot-diff-0.2.1
- old
+ new
@@ -4,25 +4,35 @@
module Screenshot
module Diff
class ImageCompare
include ChunkyPNG::Color
- def self.compare(file_name, old_file_name, dimensions = nil)
- name = file_name.chomp('.png')
+ def self.compare(*args)
+ new(*args).compare
+ end
+
+ def initialize(old_file_name, new_file_name, dimensions = nil)
+ @old_file_name = old_file_name
+ @file_name = new_file_name
+ @dimensions = dimensions
+ end
+
+ def compare
+ name = @file_name.chomp('.png')
org_file_name = "#{name}_0.png~"
new_file_name = "#{name}_1.png~"
- return nil unless File.exist? old_file_name
+ return nil unless File.exist? @old_file_name
- images = load_images(old_file_name, file_name)
+ images = load_images(@old_file_name, @file_name)
unless images
clean_tmp_files(new_file_name, org_file_name)
return false
end
- crop_images(images, dimensions) if dimensions
+ crop_images(images, @dimensions) if @dimensions
org_img = images.first
new_img = images.last
if sizes_changed?(org_img, new_img, name)
save_images(new_file_name, new_img, org_file_name, org_img)
return true
@@ -31,89 +41,89 @@
if org_img.pixels == new_img.pixels
clean_tmp_files(new_file_name, org_file_name)
return false
end
- bottom, left, right, top = find_diff_rectangle(org_img, new_img)
- draw_rectangles(images, bottom, left, right, top)
+ @left, @top, @right, @bottom = find_diff_rectangle(org_img, new_img)
+ draw_rectangles(images, @bottom, @left, @right, @top)
save_images(new_file_name, new_img, org_file_name, org_img)
true
end
- def self.save_images(new_file_name, new_img, org_file_name, org_img)
+ def dimensions
+ [@left, @top, @right, @bottom]
+ end
+
+ private
+
+ def save_images(new_file_name, new_img, org_file_name, org_img)
org_img.save(org_file_name)
new_img.save(new_file_name)
end
- def self.clean_tmp_files(new_file_name, org_file_name)
- File.delete(org_file_name) if File.exists?(org_file_name)
- File.delete(new_file_name) if File.exists?(new_file_name)
+ def clean_tmp_files(new_file_name, org_file_name)
+ File.delete(org_file_name) if File.exist?(org_file_name)
+ File.delete(new_file_name) if File.exist?(new_file_name)
end
- private
+ def load_images(old_file_name, file_name)
+ old_file = File.binread(old_file_name)
+ new_file = File.binread(file_name)
- def self.load_images(old_file_name, file_name)
- old_file = File.read(old_file_name)
- new_file = File.read(file_name)
-
return false if old_file == new_file
[ChunkyPNG::Image.from_blob(old_file), ChunkyPNG::Image.from_blob(new_file)]
end
- def self.sizes_changed?(org_image, new_image, name)
- if org_image.dimension != new_image.dimension
- puts "Image size has changed for #{name}: #{[org_image, new_image].map { |i| "#{i.width}x#{i.height}" }.join(' => ')}"
- return true
- end
+ def sizes_changed?(org_image, new_image, name)
+ return unless org_image.dimension != new_image.dimension
+ change_msg = [org_image, new_image].map { |i| "#{i.width}x#{i.height}" }.join(' => ')
+ puts "Image size has changed for #{name}: #{change_msg}"
+ true
end
- def self.crop_images(images, dimensions)
+ def crop_images(images, dimensions)
images.map! do |i|
if i.dimension.to_a == dimensions || i.width < dimensions[0] || i.height < dimensions[1]
i
else
i.crop(0, 0, *dimensions)
end
end
end
- def self.draw_rectangles(images, bottom, left, right, top)
- (1..2).each do |i|
- images.each do |image|
- image.rect(left - 1, top - 1, right + 1, bottom + 1, ChunkyPNG::Color.rgb(255, 0, 0))
- end
+ def draw_rectangles(images, bottom, left, right, top)
+ images.each do |image|
+ image.rect(left - 1, top - 1, right + 1, bottom + 1, ChunkyPNG::Color.rgb(255, 0, 0))
end
end
- def self.find_diff_rectangle(org_img, new_img)
+ def find_diff_rectangle(org_img, new_img)
top = bottom = nil
left = org_img.width
right = -1
org_img.height.times do |y|
(0...left).find do |x|
- if org_img[x, y] != new_img[x, y]
- top ||= y
- bottom = y
- left = x
- right = x if x > right
- end
+ next if org_img[x, y] == new_img[x, y]
+ top ||= y
+ bottom = y
+ left = x
+ right = x if x > right
+ x
end
(org_img.width - 1).step(right + 1, -1).find do |x|
if org_img[x, y] != new_img[x, y]
bottom = y
right = x
end
end
end
(org_img.height - 1).step(bottom + 1, -1).find do |y|
((left + 1)..(right - 1)).find do |x|
- if org_img[x, y] != new_img[x, y]
- bottom = y
- end
+ bottom = y if org_img[x, y] != new_img[x, y]
end
end
- return bottom, left, right, top
+ [left, top, right, bottom]
end
end
end
end
end