lib/capybara/screenshot/diff/drivers/vips_driver.rb in capybara-screenshot-diff-1.6.3 vs lib/capybara/screenshot/diff/drivers/vips_driver.rb in capybara-screenshot-diff-1.7.0

- old
+ new

@@ -27,10 +27,14 @@ @options = options || {} reset end + def skip_area=(new_skip_area) + # noop + end + # Resets the calculated data about the comparison with regard to the "new_image". # Data about the original image is kept. def reset end @@ -51,35 +55,37 @@ region = VipsUtil.difference_region_by(diff_mask) [region, diff_mask] end - def size(region) - return 0 unless region - - (region[2] - region[0]) * (region[3] - region[1]) - end - def adds_error_details_to(_log) end # old private def inscribed?(dimensions, i) dimension(i) == dimensions || i.width < dimensions[0] || i.height < dimensions[1] end - def crop(dimensions, i) - i.crop(*dimensions) + def crop(region, i) + result = i.crop(*region.to_top_left_corner_coordinates) + + # FIXME: Vips is caching operations, and if we ware going to read the same file, he will use cached version for this + # so after we cropped files and stored in the same file, the next load will recover old version instead of cropped + # Workaround to make vips works with cropped versions + Vips.cache_set_max(0) + Vips.vips_cache_set_max(1000) + + result end def filter_image_with_median(image, median_filter_window_size) image.median(median_filter_window_size) end def add_black_box(memo, region) - memo.draw_rect([0, 0, 0, 0], *region, fill: true) + memo.draw_rect([0, 0, 0, 0], *region.to_top_left_corner_coordinates, fill: true) end def chunky_png_comparator @chunky_png_comparator ||= ImageCompare.new( @new_file_name, @@ -129,33 +135,33 @@ result = result.bandjoin(255) if result.bands == 3 result end - def dimension_changed?(org_image, new_image) - return false if dimension(org_image) == dimension(new_image) + def dimension_changed?(old_image, new_image) + return false if dimension(old_image) == dimension(new_image) - change_msg = [org_image, new_image].map { |i| "#{i.width}x#{i.height}" }.join(" => ") + change_msg = [old_image, new_image].map { |i| "#{i.width}x#{i.height}" }.join(" => ") warn "Image size has changed for #{@new_file_name}: #{change_msg}" true end def dimension(image) [image.width, image.height] end - def draw_rectangles(images, (left, top, right, bottom), rgba) + def draw_rectangles(images, region, rgba) images.map do |image| - image.draw_rect(rgba, left - 1, top - 1, right - left + 2, bottom - top + 2) + image.draw_rect(rgba, region.left - 1, region.top - 1, region.width + 2, region.height + 2) end end class VipsUtil def self.difference(old_image, new_image, color_distance: 0) diff_mask = difference_mask(color_distance, new_image, old_image) - difference_region_by(diff_mask) + difference_region_by(diff_mask).to_edge_coordinates end def self.difference_area(old_image, new_image, color_distance: 0) difference_mask = difference_mask(color_distance, new_image, old_image) difference_area_size_by(difference_mask) @@ -169,17 +175,20 @@ def self.difference_mask(color_distance, old_image, new_image) (new_image - old_image).abs > color_distance end def self.difference_region_by(diff_mask) - columns, rows = diff_mask.project + columns, rows = diff_mask.bandor.project left = columns.profile[1].min right = columns.width - columns.flip("horizontal").profile[1].min + top = rows.profile[0].min bottom = rows.height - rows.flip("vertical").profile[0].min - [left, top, right, bottom] + return nil if right < left || bottom < top + + Region.from_edge_coordinates(left, top, right, bottom) end end end end end