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 0000000..9294603 Binary files /dev/null and b/spec/fixtures/reference_images/greyscale-with-sepia.jpg differ 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