Skip to content

Commit

Permalink
Upgrade to latest version of ruby-vips
Browse files Browse the repository at this point in the history
ruby-vips 2.0.11 adds Vips::Image#add_alpha and Vips::Image#has_alpha?,
which call the corresponding C functions directly, so we can now remove
our own implementations.
  • Loading branch information
janko committed Apr 24, 2018
1 parent e0ae109 commit 35cf2f1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 deletions.
2 changes: 1 addition & 1 deletion image_processing.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_dependency "mini_magick", "~> 4.0"
spec.add_dependency "ruby-vips", ">= 2.0.10", "< 3"
spec.add_dependency "ruby-vips", ">= 2.0.11", "< 3"

spec.add_development_dependency "rake"
spec.add_development_dependency "minitest", "~> 5.8"
Expand Down
15 changes: 1 addition & 14 deletions lib/image_processing/vips.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def resize_and_pad(image, width, height, gravity: "centre", extend: nil, backgro
embed_options.reject! { |name, value| value.nil? }

image = thumbnail(image, width, height, **options)
image = add_alpha(image) if alpha && !has_alpha?(image)
image = image.add_alpha if alpha && !image.has_alpha?
image.gravity(gravity, width, height, **embed_options)
end

Expand Down Expand Up @@ -78,19 +78,6 @@ def thumbnail(image, width, height, sharpen: SHARPEN_MASK, **options)
image
end

# Port of libvips' vips_addalpha().
def add_alpha(image)
max_alpha = (image.interpretation == :grey16 || image.interpretation == :rgb16) ? 65535 : 255
image.bandjoin(max_alpha)
end

# Port of libvips' vips_hasalpha().
def has_alpha?(image)
image.bands == 2 ||
(image.bands == 4 && image.interpretation != :cmyk) ||
image.bands > 4
end

def default_dimensions(width, height)
raise Error, "either width or height must be specified" unless width || height

Expand Down
13 changes: 8 additions & 5 deletions test/vips_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,14 @@
end

it "produces correct image when shrinking" do
expected = fixture_image("pad.png")
assert_similar expected, @pipeline.convert("png").resize_and_pad!(400, 400, alpha: true)

png = @pipeline.bandjoin(255).convert!("png")
assert_similar expected, @pipeline.source(png).resize_and_pad!(400, 400, alpha: true)
result = @pipeline.convert("png").resize_and_pad!(400, 400, alpha: true)
assert_similar fixture_image("pad.png"), result
assert_equal 4, Vips::Image.new_from_file(result.path).bands

transparent_png = @pipeline.add_alpha.convert!("png")
result = @pipeline.source(transparent_png).resize_and_pad!(400, 400, alpha: true)
assert_similar fixture_image("pad.png"), result
assert_equal 4, Vips::Image.new_from_file(result.path).bands
end

it "produces correct image when enlarging" do
Expand Down

0 comments on commit 35cf2f1

Please sign in to comment.