Skip to content

Commit

Permalink
feat: add support for stripping alpha channel for vips image processor
Browse files Browse the repository at this point in the history
That's how most operations of the GdkPixbuf-based image processor work when saving to jpeg.

- match-multiple-operations - MAE (Mean Average Error): 0.000500558
- match-multiple-operations-and-straighten - MAE (Mean Average Error): 0.0422601
MAE 0.04 comes from improving consistency in vips implementation. When saving to jpeg, Pixbuf handled transparency
differently depending on whether straighten option was used (flattening alpha) or not (discarding alpha).
Vips is currently always discarding. Long-term it feels that flattening is more desired, but I'm trying to avoid
changing too much at once.
  • Loading branch information
knarewski committed Dec 10, 2024
1 parent c112ee5 commit 0ecf4d0
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 14 additions & 3 deletions lib/morandi/vips_image_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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'])

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions spec/morandi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 0ecf4d0

Please sign in to comment.