Skip to content

Commit

Permalink
use quality 0..100 by default in lossy mode of pngquant worker #77
Browse files Browse the repository at this point in the history
  • Loading branch information
toy committed May 31, 2015
1 parent bd2432c commit f6713ca
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## unreleased

* Use quality `0..100` by default in lossy mode of pngquant worker [#77](https://github.com/toy/image_optim/issues/77) [@toy](https://github.com/toy)

## v0.21.0 (2015-05-30)

* Use exifr 1.2.2 with fix for a bug [#85](https://github.com/toy/image_optim/issues/85) [@toy](https://github.com/toy)
Expand Down
19 changes: 12 additions & 7 deletions lib/image_optim/worker/pngquant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@ class Worker
# http://pngquant.org/
class Pngquant < Worker
QUALITY_OPTION =
option(:quality, 100..100, NonNegativeIntegerRange, 'min..max - don\'t '\
'save below min, use less colors below max (both in range `0..100`; '\
'in yaml - `!ruby/range 0..100`), ignored in default/lossless '\
'mode') do |v, opt_def|
option(:quality, '100..100, 0..100 in lossy mode',
NonNegativeIntegerRange, 'min..max - don\'t '\
'save below min, use less colors below max (both in range `0..100`; '\
'in yaml - `!ruby/range 0..100`), ignored in default/lossless '\
'mode') do |v, opt_def|
if allow_lossy
min = OptionHelpers.limit_with_range(v.begin, 0..100)
min..OptionHelpers.limit_with_range(v.end, min..100)
if v == opt_def.default
0..100
else
min = OptionHelpers.limit_with_range(v.begin, 0..100)
min..OptionHelpers.limit_with_range(v.end, min..100)
end
else
if v != opt_def.default
warn "#{self.class.bin_sym} #{opt_def.name} #{v} ignored " \
'in lossless mode'
end
opt_def.default
100..100
end
end

Expand Down
27 changes: 27 additions & 0 deletions spec/image_optim/worker/pngquant_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'spec_helper'
require 'image_optim/worker/pngquant'

describe ImageOptim::Worker::Pngquant do
describe 'quality option' do
describe 'default' do
subject{ described_class::QUALITY_OPTION.default }

it{ is_expected.to match(/100\.\.100.*0\.\.100/) }
end

describe 'value' do
let(:options){ {} }
subject{ described_class.new(ImageOptim.new, options).quality }

context 'by default' do
it{ is_expected.to eq(100..100) }
end

context 'when lossy allowed by default' do
let(:options){ {:allow_lossy => true} }

it{ is_expected.to eq(0..100) }
end
end
end
end
2 changes: 1 addition & 1 deletion spec/image_optim_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def temp_copy(image)
base_options = {:skip_missing_workers => false}
[
['lossless', base_options, 0],
['lossy', base_options.merge(:allow_lossy => true), 0.01],
['lossy', base_options.merge(:allow_lossy => true), 0.025],
].each do |type, options, max_difference|
image_optim = ImageOptim.new(options)
describe type do
Expand Down

0 comments on commit f6713ca

Please sign in to comment.