From efaec3ee5f40a9547e2bdaf3df496fc4c0db0402 Mon Sep 17 00:00:00 2001 From: Konrad Date: Thu, 14 Nov 2024 16:53:21 +0100 Subject: [PATCH] test: add specs for testing greyscale processing Pixbuf happens to convert everything to RGB, but other processors are not guaranteed to have such property, thus the additional specs. I've also added a new matcher to make colourspace check easy. --- spec/colour_helper.rb | 4 +++ .../reference_images/greyscale-with-sepia.jpg | Bin 0 -> 44234 bytes spec/morandi_spec.rb | 30 +++++++++++++++++- spec/spec_helper.rb | 3 +- spec/support/match_colourspace.rb | 21 ++++++++++++ 5 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 spec/fixtures/reference_images/greyscale-with-sepia.jpg create mode 100644 spec/support/match_colourspace.rb diff --git a/spec/colour_helper.rb b/spec/colour_helper.rb index 7049c4c..2cb7878 100644 --- a/spec/colour_helper.rb +++ b/spec/colour_helper.rb @@ -9,6 +9,10 @@ def generate_test_image_plasma_checkers(at_file_path, width: 600, height: 300) generate_test_image(at_file_path, fill: fill, width: width, height: height) end + def generate_test_image_greyscale(at_file_path, width: 600, height: 300) + generate_test_image(at_file_path, fill: 'gradient:white-black', width: width, height: height) + end + def generate_test_image(at_file_path, fill:, width: 600, height: 300) fill = Array(fill).flatten diff --git a/spec/fixtures/reference_images/greyscale-with-sepia.jpg b/spec/fixtures/reference_images/greyscale-with-sepia.jpg new file mode 100644 index 0000000000000000000000000000000000000000..9294603b7cc0b4d913c4f8c0c18e425302aa5389 GIT binary patch literal 44234 zcmeHQ3se(V8lEr-_$XD1mWrOlN4r|>(V{$rLO89f@v(b6SVieMEj}vG?Xrqhc^TB* z((3k1-L9qbikeli;7SDBT9N3Ai;EHrzId!qWhGRgsRk1vknDeF@)&E|)2;0a@{i}- znPl$V$$b3(@4a)s|GU;&>zCB<`E%yYp#%bo62L#o+Ca^MPxoyuF61Na0vk;Wg*0qJ zp{qzJ6v45`O(b$9n^5HD=H}`~4oM@n7T_w{RY(iT*Z zR=AB{Gh+JY9eV~%ctv$;m3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xP;mvVjbwmbj2Hm6b7cc@FFI)({k2xB9NpMlJJ^-jp2H{F4d zWQ#0Qc=XY9|59w&o^*>)IPyvnih%-!y??*SNh0+x<0e?Vcq~zS9vN1%(Pj}4m@YyK zvGK0^jLX1_FdMTQ#B&@RNf5$uxVOqlvm~Q=td7W_5ukhgvXQ{EI`o=^u((@$+9VF@ zWIoJd;P;^Wi#XUO-_RZil@jfNqm({69Qa&TAd0cnG9ZpQ8Q{Q_vMG7;`QqgqwTw`Q zoaAzq)HYR+z+<9gqll>@8BMBRdz77timJY-YK*GR52{+fXt|^|Xd#)3+m&;RX2JiC zN2m34Mrs&tr?Rbm1#ImFRw~2`a<#a9;cvP1vLH$>vQpKvA^D2iw|R1FYyoQO497^)q zi`u}{t~T!A30_)S_W$iBXH^ek~r`ykpV*EXmX(?XEgl%M@}I zR`ec}>-JjKrE$Bp=ZZ!vVok~p>D>(DJ}Z^C9mXoQ{kf?pe-9j2ro?tL9>?i146Mu&e3xV^WQyW6PppF;!}(ELgcY!tx~~%dn+U&@t*WRxOMb5?-c=5B;|!SHhO! zlOgz!_?JX;MW5_rfoEyXrKG9|H8&P*H;Zb|*BG*xz=8mdManEcF4(8@6DNiza06NK zsrB>%O^icba z)tahlK*_T&m0Gozi5{+t%FL6WfTg3!a;s5VS@FAx#S5GYV#P7ay7Iufef*}v+HZ_$ zuikYD^tCHN{?d;iN%{cr? Nh|&PFrD$v2e*s|hL)riU literal 0 HcmV?d00001 diff --git a/spec/morandi_spec.rb b/spec/morandi_spec.rb index 5a32f08..140bd7c 100644 --- a/spec/morandi_spec.rb +++ b/spec/morandi_spec.rb @@ -17,6 +17,9 @@ let(:processed_image_type) { processed_image_info[0].name } let(:processed_image_width) { processed_image_info[1] } let(:processed_image_height) { processed_image_info[2] } + let(:generate_image) do + generate_test_image_plasma_checkers(file_in, width: original_image_width, height: original_image_height) + end before(:all) do FileUtils.mkdir_p('sample') @@ -29,7 +32,7 @@ before do next if File.exist?(file_in) - generate_test_image_plasma_checkers(file_in, width: original_image_width, height: original_image_height) + generate_image end after do |ex| @@ -456,6 +459,31 @@ end end end + + context 'with a non-rgb image' do + let(:generate_image) do + generate_test_image_greyscale(file_in, width: original_image_width, height: original_image_height) + end + + it 'changes greyscale image to srgb' do + expect(file_in).to match_colourspace('gray') # Testing a setup to protect from a hidden regression + process_image + + expect(file_out).to match_colourspace('srgb') + end + + # Colour filters implementation operates on RGB-based constants, thus a dedicated test + context 'with colour filter' do + let(:options) { super().merge('fx' => 'sepia') } + + it 'creates a valid, srgb image' do + process_image + + expect(file_out).to match_reference_image('greyscale-with-sepia') + expect(file_out).to match_colourspace('srgb') + end + end + end end context 'pixbuf processor' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d3d4052..5486662 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -19,7 +19,8 @@ require 'super_diff/rspec' require 'pry' -require 'support/match_reference_image' +Dir['spec/support/**/*.rb'].each { |f| require "./#{f}" } + require_relative 'visual_report_helper' require_relative 'colour_helper' diff --git a/spec/support/match_colourspace.rb b/spec/support/match_colourspace.rb new file mode 100644 index 0000000..136af41 --- /dev/null +++ b/spec/support/match_colourspace.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require 'open3' + +# For testing that under a given path resides an image with desired colourspace. +# The colourspace is currently extracted using imagemagick's `identify`, resulting in values like 'gray' or 'srgb' +# According to docs (https://www.imagemagick.org/script/escape.php), it may include number of channels and meta channels +RSpec::Matchers.define :match_colourspace do |expected_colourspace| + match do |tested_path| + raise(ArgumentError, "path #{tested_path} is not a file") unless File.file?(tested_path) + + @colourspace, status = Open3.capture2('identify', '-format', '%[channels]', tested_path) + raise "Failed to read colorspace of #{tested_path}" unless status.success? + + @colourspace == expected_colourspace + end + + failure_message do + "Colourspaces don't match. Expected: #{expected_colourspace}, got: #{@colourspace}" + end +end