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.0400593
MAE 0.04 comes from improving consistency in vips implementation. Pixbuf handled transparency
differently depending on whether straighten option was used (flattening alpha) or not (discarding alpha),
now it's 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 1ca5840
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 3 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
11 changes: 11 additions & 0 deletions lib/morandi/vips_image_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def process!
@scale = 1.0
end

strip_alpha!

apply_gamma!
apply_rotate!
apply_crop!
Expand Down Expand Up @@ -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 1ca5840

Please sign in to comment.