Skip to content

Commit 9478f7f

Browse files
pftgclaude
andcommitted
refactor: flatten VipsUtil into VipsDriver class methods
Move 4 utility methods from nested VipsUtil class to VipsDriver class << self block. Removes unnecessary nesting while keeping the same public API (VipsDriver.difference_mask, etc). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 5e4f8b1 commit 9478f7f

2 files changed

Lines changed: 18 additions & 19 deletions

File tree

lib/capybara/screenshot/diff/drivers/vips_driver.rb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class VipsDriver < BaseDriver
1919
def find_difference_region(comparison)
2020
new_image, base_image, options = comparison.new_image, comparison.base_image, comparison.options
2121

22-
diff_mask = VipsUtil.difference_mask(base_image, new_image, options[:color_distance_limit])
23-
region = VipsUtil.difference_region_by(diff_mask)
22+
diff_mask = self.class.difference_mask(base_image, new_image, options[:color_distance_limit])
23+
region = self.class.difference_region_by(diff_mask)
2424
# TODO: schedule research when we got this case for VIPs
2525
# region = nil if region && region_covers_entire_image?(region, base_image)
2626

@@ -55,7 +55,7 @@ def add_black_box(memo, region)
5555
end
5656

5757
def difference_level(diff_mask, old_img, _region = nil)
58-
VipsUtil.difference_area_size_by(diff_mask).to_f / image_area_size(old_img)
58+
self.class.difference_area_size_by(diff_mask).to_f / image_area_size(old_img)
5959
end
6060

6161
MAX_FILENAME_LENGTH = 200
@@ -115,24 +115,23 @@ def region_covers_entire_image?(region, base_image)
115115
region.width == width_for(base_image)
116116
end
117117

118-
class VipsUtil
119-
def self.difference_area(old_image, new_image, color_distance: 0)
120-
difference_mask = difference_mask(new_image, old_image, color_distance)
121-
difference_area_size_by(difference_mask)
118+
class << self
119+
def difference_area(old_image, new_image, color_distance: 0)
120+
mask = difference_mask(new_image, old_image, color_distance)
121+
difference_area_size_by(mask)
122122
end
123123

124-
def self.difference_area_size_by(difference_mask)
124+
def difference_area_size_by(difference_mask)
125125
diff_mask = difference_mask == 0
126126
diff_mask.hist_find.to_a[0][0].max
127127
end
128128

129-
def self.difference_mask(base_image, new_image, color_distance = nil)
129+
def difference_mask(base_image, new_image, color_distance = nil)
130130
result = (new_image - base_image).abs
131-
132131
color_distance ? result > color_distance : result
133132
end
134133

135-
def self.difference_region_by(diff_mask)
134+
def difference_region_by(diff_mask)
136135
columns, rows = diff_mask.bandor.project
137136

138137
left = columns.profile[1].min

test/unit/drivers/vips_driver_test.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ def sample_region
166166
end
167167
end
168168

169-
class VipsUtilTest < ActiveSupport::TestCase
170-
test "VipsUtil.difference_region_by detects difference regions without color threshold" do
169+
class VipsDriverClassMethodsTest < ActiveSupport::TestCase
170+
test "VipsDriver.difference_region_by detects difference regions without color threshold" do
171171
old_image = Vips::Image.new_from_file("#{TEST_IMAGES_DIR}/a.png")
172172
new_image = Vips::Image.new_from_file("#{TEST_IMAGES_DIR}/b.png")
173173

@@ -180,7 +180,7 @@ class VipsUtilTest < ActiveSupport::TestCase
180180
assert_equal [20.0, 15.0, 30.0, 25.0], [left, top, right, bottom]
181181
end
182182

183-
test "VipsUtil.difference_region_by respects color_distance threshold" do
183+
test "VipsDriver.difference_region_by respects color_distance threshold" do
184184
old_image = Vips::Image.new_from_file("#{TEST_IMAGES_DIR}/a.png")
185185
new_image = Vips::Image.new_from_file("#{TEST_IMAGES_DIR}/b.png")
186186

@@ -189,7 +189,7 @@ class VipsUtilTest < ActiveSupport::TestCase
189189
assert_equal [26.0, 18.0, 27.0, 19.0], [left, top, right, bottom]
190190
end
191191

192-
test "VipsUtil.difference_region_by returns correct region coordinates" do
192+
test "VipsDriver.difference_region_by returns correct region coordinates" do
193193
old_image = Vips::Image.new_from_file(TEST_IMAGES_DIR.join("a.png").to_path)
194194
new_image = Vips::Image.new_from_file(TEST_IMAGES_DIR.join("b.png").to_path)
195195

@@ -198,18 +198,18 @@ class VipsUtilTest < ActiveSupport::TestCase
198198
assert_equal [20.0, 15.0, 30.0, 25.0], [left, top, right, bottom]
199199
end
200200

201-
test "VipsUtil.difference_area calculates correct area of difference" do
201+
test "VipsDriver.difference_area calculates correct area of difference" do
202202
old_image = Vips::Image.new_from_file("#{TEST_IMAGES_DIR}/a.png")
203203
new_image = Vips::Image.new_from_file("#{TEST_IMAGES_DIR}/d.png").bandjoin(255)
204204

205-
assert_equal 8, VipsDriver::VipsUtil.difference_area(old_image, new_image, color_distance: 10)
205+
assert_equal 8, VipsDriver.difference_area(old_image, new_image, color_distance: 10)
206206
end
207207

208208
private
209209

210210
def difference(old_image, new_image, color_distance: nil)
211-
diff_mask = VipsDriver::VipsUtil.difference_mask(new_image, old_image, color_distance)
212-
VipsDriver::VipsUtil.difference_region_by(diff_mask).to_edge_coordinates
211+
diff_mask = VipsDriver.difference_mask(new_image, old_image, color_distance)
212+
VipsDriver.difference_region_by(diff_mask).to_edge_coordinates
213213
end
214214
end
215215
end

0 commit comments

Comments
 (0)