Skip to content

Commit c8a56b7

Browse files
committed
Disable sharpening by default in vips backend
Sharpening by default sometimes produces worse results than without sharpening. Even though newer versions of libvips improve upon this, it's still better to prioritize correctness by default. So, we disable sharpening by default, like we did with MiniMagick backend. Closes #113
1 parent 3b870f2 commit c8a56b7

3 files changed

Lines changed: 22 additions & 18 deletions

File tree

doc/vips.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,16 @@ ImageProcessing::Vips
497497

498498
## Sharpening
499499

500-
All `#resize_*` operations will automatically sharpen the resulting thumbnails
501-
after resizing, using the following [convolution mask]:
500+
With `#resize_*` operators, you can additional sharpen the thumbnails via the
501+
`:sharpen` option:
502+
503+
```rb
504+
ImageProcessing::Vips
505+
.source(image)
506+
.resize_to_limit!(400, 400, sharpen: true)
507+
```
508+
509+
This will use the following [convolution mask] by default:
502510

503511
```rb
504512
Vips::Image.new_from_array [
@@ -507,7 +515,7 @@ Vips::Image.new_from_array [
507515
[-1, -1, -1]], 24
508516
```
509517

510-
You can assign a different convolution mask via the `:sharpen` option:
518+
You can also assign your own convolution mask:
511519

512520
```rb
513521
sharpen_mask = Vips::Image.new_from_array [
@@ -520,14 +528,6 @@ ImageProcessing::Vips
520528
.resize_to_limit!(400, 400, sharpen: sharpen_mask)
521529
```
522530

523-
You can disable automatic sharpening by setting `:sharpen` to `false`:
524-
525-
```rb
526-
ImageProcessing::Vips
527-
.source(image)
528-
.resize_to_limit!(400, 400, sharpen: false)
529-
```
530-
531531
[ruby-vips]: https://github.com/libvips/ruby-vips
532532
[libvips]: https://github.com/libvips/libvips
533533
[installation instructions]: https://github.com/libvips/libvips/wiki#building-and-installing

lib/image_processing/vips.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def remove(*args) image.tap { |img| img.remove(*args) } end
155155

156156
# Resizes the image according to the specified parameters, and sharpens
157157
# the resulting thumbnail.
158-
def thumbnail(width, height, sharpen: SHARPEN_MASK, **options)
158+
def thumbnail(width, height, sharpen: nil, **options)
159159
if self.image.is_a?(String) # path
160160
# resize on load
161161
image = ::Vips::Image.thumbnail(self.image, width, height: height, **options)
@@ -167,7 +167,11 @@ def thumbnail(width, height, sharpen: SHARPEN_MASK, **options)
167167
image = self.image.thumbnail_image(width, height: height, **options)
168168
end
169169

170-
image = image.conv(sharpen, precision: :integer) if sharpen
170+
if sharpen
171+
sharpen_mask = sharpen.is_a?(TrueClass) ? SHARPEN_MASK : sharpen
172+
image = image.conv(sharpen_mask, precision: :integer)
173+
end
174+
171175
image
172176
end
173177

test/vips_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@
181181
end
182182

183183
it "sharpening uses integer precision" do
184-
sharpened = @pipeline.resize_to_limit(400, 400).call(save: false)
184+
sharpened = @pipeline.resize_to_limit(400, 400, sharpen: true).call(save: false)
185185
assert_equal :uchar, sharpened.format
186186
end
187187
end
@@ -229,7 +229,7 @@
229229
end
230230

231231
it "sharpening uses integer precision" do
232-
sharpened = @pipeline.resize_to_limit(400, 400).call(save: false)
232+
sharpened = @pipeline.resize_to_limit(400, 400, sharpen: true).call(save: false)
233233
assert_equal :uchar, sharpened.format
234234
end
235235
end
@@ -265,7 +265,7 @@
265265
end
266266

267267
it "sharpening uses integer precision" do
268-
sharpened = @pipeline.resize_to_limit(400, 400).call(save: false)
268+
sharpened = @pipeline.resize_to_limit(400, 400, sharpen: true).call(save: false)
269269
assert_equal :uchar, sharpened.format
270270
end
271271
end
@@ -319,7 +319,7 @@
319319
end
320320

321321
it "sharpening uses integer precision" do
322-
sharpened = @pipeline.resize_to_limit(400, 400).call(save: false)
322+
sharpened = @pipeline.resize_to_limit(400, 400, sharpen: true).call(save: false)
323323
assert_equal :uchar, sharpened.format
324324
end
325325
end
@@ -386,7 +386,7 @@
386386
end
387387

388388
it "sharpening uses integer precision" do
389-
sharpened = @portrait_pipeline.resize_to_cover(400, 400).call(save: false)
389+
sharpened = @portrait_pipeline.resize_to_cover(400, 400, sharpen: true).call(save: false)
390390
assert_equal :uchar, sharpened.format
391391
end
392392

0 commit comments

Comments
 (0)