Class: Capybara::Screenshot::Diff::Drivers::VipsDriver
- Inherits:
-
BaseDriver
- Object
- BaseDriver
- Capybara::Screenshot::Diff::Drivers::VipsDriver
show all
- Defined in:
- lib/capybara/screenshot/diff/drivers/vips_driver.rb
Defined Under Namespace
Classes: VipsUtil
Constant Summary
collapse
- MAX_FILENAME_LENGTH =
200
Constants inherited
from BaseDriver
BaseDriver::PNG_EXTENSION
Instance Method Summary
collapse
-
#add_black_box(memo, region) ⇒ Object
-
#crop(region, i) ⇒ Object
-
#difference_level(diff_mask, old_img, _region = nil) ⇒ Object
-
#dimension(image) ⇒ Object
-
#draw_rectangles(images, region, rgba, offset: 0) ⇒ Object
-
#filter_image_with_median(image, median_filter_window_size) ⇒ Object
-
#find_difference_region(comparison) ⇒ Object
-
#from_file(filename) ⇒ Object
-
#highlight_mask(diff_mask, merged_image, color: DEFAULT_HIGHLIGHT_COLOR) ⇒ Object
-
#load_images(old_file_name, new_file_name) ⇒ Object
-
#merge(new_image, base_image) ⇒ Object
-
#resize_image_to(image, new_width, new_height) ⇒ Object
-
#same_pixels?(comparison) ⇒ Boolean
-
#save_image_to(image, filename) ⇒ Object
Vips could not work with the same file.
Methods inherited from BaseDriver
#height_for, #image_area_size, #same_dimension?, #width_for
Instance Method Details
#add_black_box(memo, region) ⇒ Object
52
53
54
55
56
|
# File 'lib/capybara/screenshot/diff/drivers/vips_driver.rb', line 52
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
|
#crop(region, i) ⇒ Object
38
39
40
41
42
43
44
45
46
|
# File 'lib/capybara/screenshot/diff/drivers/vips_driver.rb', line 38
def crop(region, i)
i.crop(*region.to_top_left_corner_coordinates)
rescue Vips::Error => e
warn(
"[capybara-screenshot-diff] Crop has been failed for " \
"{ region: #{region.to_top_left_corner_coordinates.inspect}, image: #{dimension(i).join("x")} }"
)
raise e
end
|
#difference_level(diff_mask, old_img, _region = nil) ⇒ Object
58
59
60
|
# File 'lib/capybara/screenshot/diff/drivers/vips_driver.rb', line 58
def difference_level(diff_mask, old_img, _region = nil)
VipsUtil.difference_area_size_by(diff_mask).to_f / image_area_size(old_img)
end
|
#dimension(image) ⇒ Object
92
93
94
|
# File 'lib/capybara/screenshot/diff/drivers/vips_driver.rb', line 92
def dimension(image)
[width_for(image), height_for(image)]
end
|
#draw_rectangles(images, region, rgba, offset: 0) ⇒ Object
96
97
98
99
100
|
# File 'lib/capybara/screenshot/diff/drivers/vips_driver.rb', line 96
def draw_rectangles(images, region, rgba, offset: 0)
images.map do |image|
image.draw_rect(rgba, region.left - offset, region.top - offset, region.width + (offset * 2), region.height + (offset * 2))
end
end
|
48
49
50
|
# File 'lib/capybara/screenshot/diff/drivers/vips_driver.rb', line 48
def filter_image_with_median(image, median_filter_window_size)
image.median(median_filter_window_size)
end
|
#find_difference_region(comparison) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/capybara/screenshot/diff/drivers/vips_driver.rb', line 21
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])
region = VipsUtil.difference_region_by(diff_mask)
region = nil if region && same_as?(region, base_image)
result = Difference.new(region, {}, comparison)
unless result.blank?
result.meta[:difference_level] = difference_level(diff_mask, base_image) if comparison.options[:tolerance]
result.meta[:diff_mask] = diff_mask
end
result
end
|
#from_file(filename) ⇒ Object
83
84
85
86
87
88
89
90
|
# File 'lib/capybara/screenshot/diff/drivers/vips_driver.rb', line 83
def from_file(filename)
result = ::Vips::Image.new_from_file(filename.to_s)
result = result.colourspace(:srgb) if result.bands < 3
result = result.bandjoin(255) if result.bands == 3
result
end
|
#highlight_mask(diff_mask, merged_image, color: DEFAULT_HIGHLIGHT_COLOR) ⇒ Object
110
111
112
|
# File 'lib/capybara/screenshot/diff/drivers/vips_driver.rb', line 110
def highlight_mask(diff_mask, merged_image, color: DEFAULT_HIGHLIGHT_COLOR)
diff_mask.ifthenelse(color, merged_image * 0.75)
end
|
#load_images(old_file_name, new_file_name) ⇒ Object
79
80
81
|
# File 'lib/capybara/screenshot/diff/drivers/vips_driver.rb', line 79
def load_images(old_file_name, new_file_name)
[from_file(old_file_name), from_file(new_file_name)]
end
|
#merge(new_image, base_image) ⇒ Object
106
107
108
|
# File 'lib/capybara/screenshot/diff/drivers/vips_driver.rb', line 106
def merge(new_image, base_image)
base_image.composite2(new_image, :over)
end
|
#resize_image_to(image, new_width, new_height) ⇒ Object
75
76
77
|
# File 'lib/capybara/screenshot/diff/drivers/vips_driver.rb', line 75
def resize_image_to(image, new_width, new_height)
image.resize(new_width.to_f / new_height)
end
|
#same_pixels?(comparison) ⇒ Boolean
102
103
104
|
# File 'lib/capybara/screenshot/diff/drivers/vips_driver.rb', line 102
def same_pixels?(comparison)
(comparison.new_image == comparison.base_image).min == 255
end
|
#save_image_to(image, filename) ⇒ Object
Vips could not work with the same file. Per each process we require to create new file
65
66
67
68
69
70
71
72
73
|
# File 'lib/capybara/screenshot/diff/drivers/vips_driver.rb', line 65
def save_image_to(image, filename)
limited_filename = filename.to_s[-MAX_FILENAME_LENGTH..] || filename.to_s
::Dir::Tmpname.create([limited_filename, PNG_EXTENSION]) do |tmp_image_filename|
image.write_to_file(tmp_image_filename)
FileUtils.mv(tmp_image_filename, filename)
end
end
|