Skip to content

Commit d4b8a65

Browse files
committed
switch from MEPP to PSNR for comparing image after optimization
1 parent 6026f25 commit d4b8a65

4 files changed

Lines changed: 15 additions & 13 deletions

File tree

spec/image_optim_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ def temp_copy(image)
6161

6262
base_options = {skip_missing_workers: false}
6363
[
64-
['lossless', base_options, 0],
65-
['lossy', base_options.merge(allow_lossy: true), 0.00101],
66-
].each do |type, options, max_difference|
64+
['lossless', base_options, Float::INFINITY],
65+
['lossy', base_options.merge(allow_lossy: true), 30],
66+
].each do |type, options, psnr_min|
6767
it "does it #{type}" do
6868
image_optim = ImageOptim.new(options)
6969
copies = test_images.map{ |image| temp_copy(image) }
@@ -78,7 +78,7 @@ def temp_copy(image)
7878
expect(optimized).not_to have_same_data_as(original)
7979

8080
compare_to = rotate_images.include?(original) ? rotated : original
81-
expect(optimized).to be_similar_to(compare_to, max_difference)
81+
expect(optimized).to be_similar_to(compare_to, psnr_min)
8282
end
8383
end
8484
end

spec/images/invisiblepixels/generate

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ require 'shellwords'
77

88
side = 64
99

10+
colors = Array.new(256){ [rand(256), rand(256), rand(256)] }
11+
1012
IO.popen(%W[
1113
convert
1214
-depth 8
@@ -18,7 +20,7 @@ IO.popen(%W[
1820
side.times do |a|
1921
side.times do |b|
2022
alpha = [0, 1, 0x7f, 0xff][((a / 8) + (b / 8)) % 4]
21-
f << [rand(256), rand(256), rand(256), alpha].pack('C*')
23+
f << [*colors.sample, alpha].pack('C*')
2224
end
2325
end
2426
end
-5.97 KB
Loading

spec/spec_helper.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ def flatten_animation(image)
4242
end
4343
end
4444

45-
def mepp(image_a, image_b)
45+
def psnr(image_a, image_b)
4646
coalesce_a = flatten_animation(image_a)
4747
coalesce_b = flatten_animation(image_b)
4848
output = ImageOptim::Cmd.capture((IMAGEMAGICK_PREFIX + %W[
4949
compare
50-
-metric MEPP
50+
-metric PSNR
5151
-alpha Background
5252
#{coalesce_a.to_s.shellescape}
5353
#{coalesce_b.to_s.shellescape}
@@ -59,21 +59,21 @@ def mepp(image_a, image_b)
5959
end
6060

6161
num_r = '\d+(?:\.\d+(?:[eE][-+]?\d+)?)?'
62-
output[/\((#{num_r}), #{num_r}\)/, 1].to_f
62+
num = output[/\A(#{num_r})/, 1].to_f
63+
num == 0 ? Float::INFINITY : num
6364
end
6465

6566
RSpec::Matchers.define :be_smaller_than do |expected|
6667
match{ |actual| actual.size < expected.size }
6768
end
6869

69-
RSpec::Matchers.define :be_similar_to do |expected, max_difference|
70+
RSpec::Matchers.define :be_similar_to do |expected, psnr_min|
7071
match do |actual|
71-
@diff = mepp(actual, expected)
72-
@diff <= max_difference
72+
@diff = psnr(actual, expected)
73+
@diff >= psnr_min
7374
end
7475
failure_message do |actual|
75-
"expected #{actual} to have at most #{max_difference} difference from " \
76-
"#{expected}, got mean error per pixel of #{@diff}"
76+
"expected peaks signal to noise ratio between #{actual} and #{expected} to be #{psnr_min}, got #{@diff}"
7777
end
7878
end
7979

0 commit comments

Comments
 (0)