diff --git a/CHANGELOG.md b/CHANGELOG.md index 22b4863..fa5d57e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Vips rotate - Vips straighten - Vips gamma +- Vips stripping alpha ### Removed - [BREAKING] dropped support for a broken 'dominant' border colour diff --git a/lib/morandi/vips_image_processor.rb b/lib/morandi/vips_image_processor.rb index 373a50b..706a70e 100644 --- a/lib/morandi/vips_image_processor.rb +++ b/lib/morandi/vips_image_processor.rb @@ -66,10 +66,12 @@ def process! apply_crop! apply_filters! - return unless @options['output.limit'] && @output_width && @output_height + if @options['output.limit'] && @output_width && @output_height + scale_factor = [@output_width, @output_height].max.to_f / [@img.width, @img.height].max + @img = @img.resize(scale_factor) if scale_factor < 1.0 + end - scale_factor = [@output_width, @output_height].max.to_f / [@img.width, @img.height].max - @img = @img.resize(scale_factor) if scale_factor < 1.0 + strip_alpha! end def write_to_png(_write_to, _orientation = :any) @@ -88,6 +90,15 @@ def write_to_jpeg(target_path, quality = nil) private + # Remove the alpha channel if present. Vips supports alpha, but the current Pixbuf processor happens to strip it in + # most cases (straighten and cropping beyond image bounds are exceptions) + # + # Alternatively, alpha can be left intact for more accurate processing and transparent output or merged into an + # image using Vips::Image#flatten for less resource-intensive processing + def strip_alpha! + @img = @img.extract_band(0, n: @img.bands - 1) if @img.has_alpha? + end + def apply_gamma! return unless @options['gamma'] && not_equal_to_one(@options['gamma']) diff --git a/spec/fixtures/reference_images/vips/match-multiple-operations-and-straighten.jpg b/spec/fixtures/reference_images/vips/match-multiple-operations-and-straighten.jpg new file mode 100644 index 0000000..feb0993 Binary files /dev/null and b/spec/fixtures/reference_images/vips/match-multiple-operations-and-straighten.jpg differ diff --git a/spec/fixtures/reference_images/vips/match-multiple-operations.jpg b/spec/fixtures/reference_images/vips/match-multiple-operations.jpg new file mode 100644 index 0000000..f5ff1cf Binary files /dev/null and b/spec/fixtures/reference_images/vips/match-multiple-operations.jpg differ diff --git a/spec/morandi_spec.rb b/spec/morandi_spec.rb index cbe5039..4eb044e 100644 --- a/spec/morandi_spec.rb +++ b/spec/morandi_spec.rb @@ -433,7 +433,7 @@ end end - context 'with transparent png input', vips_wip: processor_name == 'vips' do + context 'with transparent png input' do let(:file_in) { 'spec/fixtures/match-with-transparency.png' } let(:options) do { @@ -448,7 +448,7 @@ expect(File).to exist(file_out) expect(processed_image_type).to eq('jpeg') - expect(file_out).to match_reference_image('match-multiple-operations') + expect(file_out).to match_reference_image(reference_image_prefix, 'match-multiple-operations') end context 'with straighten option' do @@ -460,7 +460,7 @@ expect(File).to exist(file_out) expect(processed_image_type).to eq('jpeg') - expect(file_out).to match_reference_image('match-multiple-operations-and-straighten') + expect(file_out).to match_reference_image(reference_image_prefix, 'match-multiple-operations-and-straighten') end end end