Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: strip alpha channel in vips image processor #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading