lib/capybara/screenshot/diff/drivers/vips_driver.rb in capybara-screenshot-diff-1.8.3 vs lib/capybara/screenshot/diff/drivers/vips_driver.rb in capybara-screenshot-diff-1.9.0

- old
+ new

@@ -13,10 +13,12 @@ module Screenshot module Diff # Compare two images and determine if they are equal, different, or within some comparison # range considering color values and difference area size. module Drivers + DEFAULT_HIGHLIGHT_COLOR = [255, 0, 0, 255].freeze + class VipsDriver < BaseDriver def find_difference_region(comparison) new_image, base_image, options = comparison.new_image, comparison.base_image, comparison.options diff_mask = VipsUtil.difference_mask(base_image, new_image, options[:color_distance_limit]) @@ -24,13 +26,12 @@ region = nil if region && same_as?(region, base_image) result = Difference.new(region, {}, comparison) unless result.blank? - meta = {} - meta[:difference_level] = difference_level(diff_mask, base_image) if comparison.options[:tolerance] - result.meta = meta + result.meta[:difference_level] = difference_level(diff_mask, base_image) if comparison.options[:tolerance] + result.meta[:diff_mask] = diff_mask end result end @@ -47,10 +48,12 @@ def filter_image_with_median(image, median_filter_window_size) image.median(median_filter_window_size) end def add_black_box(memo, region) + return memo unless region + memo.draw_rect([0, 0, 0, 0], *region.to_top_left_corner_coordinates, fill: true) end def difference_level(diff_mask, old_img, _region = nil) VipsUtil.difference_area_size_by(diff_mask).to_f / image_area_size(old_img) @@ -96,9 +99,17 @@ end end def same_pixels?(comparison) (comparison.new_image == comparison.base_image).min == 255 + end + + def merge(new_image, base_image) + base_image.composite2(new_image, :over) + end + + def highlight_mask(diff_mask, merged_image, color: DEFAULT_HIGHLIGHT_COLOR) + diff_mask.ifthenelse(color, merged_image * 0.75) end private def same_as?(region, base_image)