Skip to content
This repository was archived by the owner on Dec 26, 2024. It is now read-only.

Commit

Permalink
Make sharpen mask on image reduction configurable.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Nicoll committed Oct 27, 2016
1 parent fbca1f5 commit 807c7e5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ GIT
PATH
remote: .
specs:
carrierwave-vips (1.1.2)
carrierwave-vips (1.1.3)
carrierwave (>= 0.11.0)
ruby-vips (~> 1.0, >= 1.0.2)

Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ You can use the following methods to change your images.

To see how vips stands up to other image processing libraries, see this benchmark: https://github.com/stanislaw/carrierwave-vips-benchmarks

Configuration
-------------

When reducing the size of images, a sharpening mask is used. To change or disable this behavior:

```
CarrierWave::Vips.configure do |c|
c.sharpen_mask = false # Disable sharpening mask on image reduction
c.sharpen_mask = [ # Default mask
[ -1, -1, -1 ],
[ -1, 24, -1 ],
[ -1, -1, -1 ]
]
c.sharpen_scale = 16
end
```

See VIPS::Image.new_from_array for more information on what these two do.

Special considerations
----------------------

Expand Down
4 changes: 2 additions & 2 deletions carrierwave-vips.gemspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Gem::Specification.new do |s|
s.name = 'carrierwave-vips'
s.version = '1.1.2'
s.date = '2016-09-06'
s.version = '1.1.3'
s.date = '2016-10-26'
s.summary = "Adds VIPS support to CarrierWave"
s.description = "Adds VIPS support to CarrierWave"
s.authors = ["Jeremy Nicoll"]
Expand Down
27 changes: 19 additions & 8 deletions lib/carrierwave/vips.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
module CarrierWave
module Vips

SHARPEN_MASK = begin
conv_mask = [
[ -1, -1, -1 ],
[ -1, 24, -1 ],
[ -1, -1, -1 ]
]
::Vips::Image.new_from_array conv_mask, 16
def self.configure
@config ||= begin
c = Struct.new(:sharpen_mask, :sharpen_scale).new
c.sharpen_mask = [ [ -1, -1, -1 ], [ -1, 24, -1 ], [ -1, -1, -1 ] ]
c.sharpen_scale = 16
c
end
@config
yield @config if block_given?
@config
end

def self.included(base)
Expand Down Expand Up @@ -256,7 +259,7 @@ def resize_image(image, width, height, min_or_max = :min)
image = image.resize(ratio, kernel: :nearest)
else
image = image.resize(ratio, kernel: :cubic)
image = image.conv SHARPEN_MASK
image = image.conv(cwv_sharpen_mask) if cwv_config.sharpen_mask
end
image
end
Expand Down Expand Up @@ -284,5 +287,13 @@ def ext(path)
matches && matches[1].downcase
end

def cwv_config
CarrierWave::Vips.configure
end

def cwv_sharpen_mask(mask = cwv_config.sharpen_mask, scale = cwv_config.sharpen_scale)
::Vips::Image.new_from_array(mask, scale)
end

end # Vips
end # CarrierWave

0 comments on commit 807c7e5

Please sign in to comment.