Skip to content

Commit

Permalink
pngquant worker (kudos to adammathys for #32 and smasry for #40)
Browse files Browse the repository at this point in the history
  • Loading branch information
toy committed Aug 7, 2014
1 parent 0dce505 commit 4660964
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
4 changes: 4 additions & 0 deletions bin/image_optim
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

require 'image_optim/runner'
require 'image_optim/true_false_nil'
require 'image_optim/non_negative_integer_range'

options = {}

option_parser = OptionParser.new do |op|
ImageOptim::TrueFalseNil.add_to_option_parser(op)
ImageOptim::NonNegativeIntegerRange.add_to_option_parser(op)

op.banner = <<-TEXT.gsub(/^\s*\|/, '')
|#{ImageOptim.full_version}
Expand Down Expand Up @@ -66,6 +68,8 @@ option_parser = OptionParser.new do |op|
[Integer, 'N']
when Array >= type
[Array, 'a,b,c']
when ImageOptim::NonNegativeIntegerRange == type
[type, 'M-N']
else
fail "Unknown type #{type}"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/image_optim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def apply_threading(enum)
end

%w[
pngcrush pngout optipng advpng
pngcrush pngout optipng advpng pngquant
jhead jpegoptim jpegtran
gifsicle
svgo
Expand Down
11 changes: 11 additions & 0 deletions lib/image_optim/non_negative_integer_range.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class ImageOptim
# Denote range of non negative integers for worker option
class NonNegativeIntegerRange
# Add handling of range of non negative integers in OptionParser instance
def self.add_to_option_parser(option_parser)
option_parser.accept(self, /(\d+)(?:-|\.\.)(\d+)/) do |_, m, n|
m.to_i..n.to_i
end
end
end
end
43 changes: 43 additions & 0 deletions lib/image_optim/worker/pngquant.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'image_optim/worker'
require 'image_optim/option_helpers'
require 'image_optim/non_negative_integer_range'

class ImageOptim
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`)') do |v|
min = OptionHelpers.limit_with_range(v.begin, 0..100)
min..OptionHelpers.limit_with_range(v.end, min..100)
end

SPEED_OPTION =
option(:speed, 3, 'speed/quality trade-off: '\
'`1` - slow, '\
'`3` - default, '\
'`11` - fast & rough') do |v|
OptionHelpers.limit_with_range(v.to_i, 1..11)
end

# Always run first
def run_order
-5
end

def optimize(src, dst)
args = %W[
--quality=#{quality.begin}-#{quality.end}
--speed=#{speed}
--output=#{dst}
--force
--
#{src}
]
execute(:pngquant, *args) && optimized?(src, dst)
end
end
end
end

0 comments on commit 4660964

Please sign in to comment.