Module: Pngnq

Defined in:
lib/pngnq.rb,
lib/pngnq/version.rb

Overview

A Ruby wrapper around the pngnq command line tool.

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.build_line_for(options) ⇒ String

Turns a provided hash into the command-line string format pngnq wants.



95
96
97
98
99
100
101
102
103
# File 'lib/pngnq.rb', line 95

def self.build_line_for(options)
  pairs = options.map do |key, value|
    next if value == false
    flag = self.mappings[key]
    val = self.value_mappings[value.to_s] || value.to_s
    flag.nil? ? nil : [flag, val].join(' ')
  end
  pairs.reject { |p| p.nil? }.join(' ').strip
end

.cocaineCocaine::CommandLine

Generates an instnace of a Cocaine command line runner.



40
41
42
# File 'lib/pngnq.rb', line 40

def self.cocaine
  Cocaine::CommandLine.new('pngnq', ":options :file")
end

.combine_defaults_with(options) ⇒ Hash

Mashes in a given hash with our defaults.

Options Hash (options):

  • :colors (Integer)

    the number of colors in the output image.

  • :force (Boolean)

    if true, the destination file will be overwritten.

  • :quantization (Symbol)

    the dithering option to use, either :no_dithering (default) or :floyd_steinberg.

  • :speed (Integer)

    from 1-10, 10 being fastest, with worst quality. Default is 1.

  • :gamma (Float)

    the gamma of the converted image; default value is 1.8.

  • :output (String)

    the output directory where the destination file will be stored.



58
59
60
61
62
63
64
65
66
67
# File 'lib/pngnq.rb', line 58

def self.combine_defaults_with(options)
  {
    :colors => 256,
    :force => true,
    :quantization => :no_dithering,
    :speed => 1,
    :gamma => 1.8,
    :output => '.',
  }.merge(options)
end

.convert(file, options = {}) ⇒ Boolean

Converts a file using pnqnq.

Options Hash (options):

  • :colors (Integer)

    the number of colors in the output image.

  • :force (Boolean)

    if true, the destination file will be overwritten.

  • :quantization (Symbol)

    the dithering option to use, either :no_dithering (default) or :floyd_steinberg.

  • :speed (Integer)

    from 1-10, 10 being fastest, with worst quality. Default is 1.

  • :gamma (Float)

    the gamma of the converted image; default value is 1.8.

  • :output (String)

    the output directory where the destination file will be stored.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pngnq.rb', line 23

def self.convert(file, options={})
  options = self.combine_defaults_with(options)
  command = self.build_line_for(options)

  new_file = file.gsub(/\.png$/, '-nq8.png')
  destination = File.join(options[:output], File.basename(file))

  FileUtils.rm(new_file) if File.exists?(new_file)
  FileUtils.rm(destination) if options[:force] && File.exists?(destination)

  result = self.cocaine.run(:options => command, :file => file)
  FileUtils.mv(new_file, destination) if result
  result
end

.mappingsHash

A hash of key-to-flag mappings.



71
72
73
74
75
76
77
78
79
80
# File 'lib/pngnq.rb', line 71

def self.mappings
  {
    :colors => '-n',
    :force => '-f',
    :quantization => '-Q',
    :speed => '-s',
    :gamma => '-g',
    :output => '-d',
  }
end

.value_mappingsHash

A hash of value-to-command-line-value mappings.



84
85
86
87
88
89
90
# File 'lib/pngnq.rb', line 84

def self.value_mappings
  {
    'true' => '',
    'no_dithering' => 'n',
    'floyd_steinberg' => 'f',
  }
end