lib/capybara/screenshot/diff/image_compare.rb in capybara-screenshot-diff-0.11.0 vs lib/capybara/screenshot/diff/image_compare.rb in capybara-screenshot-diff-0.11.1

- old
+ new

@@ -35,28 +35,27 @@ # Compare the two image files and return `true` or `false` as quickly as possible. # Return falsish if the old file does not exist or the image dimensions do not match. def quick_equal? return nil unless old_file_exists? return true if new_file_size == old_file_size - old_file, new_file = load_image_files(@old_file_name, @new_file_name) - return true if old_file == new_file - images = load_images(old_file, new_file) + old_bytes, new_bytes = load_image_files(@old_file_name, @new_file_name) + return true if old_bytes == new_bytes + images = load_images(old_bytes, new_bytes) + old_bytes = new_bytes = nil crop_images(images, @dimensions) if @dimensions - old_img = images.first - new_img = images.last + return false if sizes_changed?(*images) + return true if images.first.pixels == images.last.pixels - return false if sizes_changed?(old_img, new_img) + return false unless @color_distance_limit || @shift_distance_limit - return true if old_img.pixels == new_img.pixels + @left, @top, @right, @bottom = find_top(*images) - @left, @top, @right, @bottom = find_top(old_img, new_img) - return true if @top.nil? if @area_size_limit - @left, @top, @right, @bottom = find_diff_rectangle(old_img, new_img) + @left, @top, @right, @bottom = find_diff_rectangle(*images) return true if size <= @area_size_limit end false end @@ -259,11 +258,11 @@ shift_distance = shift_distance_at(new_img, old_img, x, y, color_distance_limit: @color_distance_limit) if !@max_color_distance || color_distance > @max_color_distance @max_color_distance = color_distance end - if !@max_shift_distance || shift_distance > @max_shift_distance + if shift_distance.to_i > @max_shift_distance.to_i @max_shift_distance = shift_distance end (color_distance == 0 || (@color_distance_limit && @color_distance_limit > 0 && color_distance <= @color_distance_limit)) && (shift_distance == 0 || (@shift_distance_limit && @shift_distance_limit > 0 && @@ -293,45 +292,55 @@ def shift_distance_at(new_img, old_img, x, y, color_distance_limit:) org_color = old_img[x, y] shift_distance = 0 loop do + bounds_breached = 0 if (y - shift_distance) >= 0 # top ([0, x - shift_distance].max..[x + shift_distance, new_img.width - 1].min).each do |dx| if color_matches(new_img, org_color, dx, y - shift_distance, color_distance_limit) return shift_distance end end + else + bounds_breached += 1 end if shift_distance > 0 if (x - shift_distance) >= 0 # left ([0, y - shift_distance + 1].max..[y + shift_distance, new_img.height - 2].min) .each do |dy| if color_matches(new_img, org_color, x - shift_distance, dy, color_distance_limit) return shift_distance end end + else + bounds_breached += 1 end if (y + shift_distance) < new_img.height # bottom ([0, x - shift_distance].max..[x + shift_distance, new_img.width - 1].min).each do |dx| if color_matches(new_img, org_color, dx, y + shift_distance, color_distance_limit) return shift_distance end end + else + bounds_breached += 1 end if (x + shift_distance) < new_img.width # right ([0, y - shift_distance + 1].max..[y + shift_distance, new_img.height - 2].min) .each do |dy| if color_matches(new_img, org_color, x + shift_distance, dy, color_distance_limit) return shift_distance end end + else + bounds_breached += 1 end end + break if bounds_breached == 4 shift_distance += 1 # puts "Bumped shift_distance to #{shift_distance}" end - shift_distance + nil end def color_matches(new_img, org_color, dx, dy, color_distance_limit) new_color = new_img[dx, dy] return new_color == org_color unless color_distance_limit