Module: Jpegtran

Extended by:
Jpegtran
Included in:
Jpegtran
Defined in:
lib/jpegtran.rb,
lib/jpegtran/version.rb,
lib/jpegtran/configuration.rb

Defined Under Namespace

Classes: Configuration, Error

Constant Summary collapse

BOOLEAN_ARGS =
[
  :arithmetic, :grayscale, :optimize, :perfect, :progressive,
  :transpose, :transverse, :trim, :verbose
]
COPY_OPTIONS =
[
  :none, :comments, :all
]
FLIP_OPTIONS =
[
  :horizontal, :vertical
]
VERSION =
"0.3.1"

Instance Method Summary collapse

Instance Method Details

#configurationObject



30
31
32
# File 'lib/jpegtran.rb', line 30

def configuration
  @configuration ||= Configuration.new
end

#configure {|configuration| ... } ⇒ Object

Yields:



34
35
36
# File 'lib/jpegtran.rb', line 34

def configure
  yield configuration
end

#configured?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/jpegtran.rb', line 22

def configured?
  !executable.nil?
end

#executableObject



26
27
28
# File 'lib/jpegtran.rb', line 26

def executable
  configuration.executable
end

#optimize(path, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/jpegtran.rb', line 38

def optimize(path, options = {})
  cmd = [executable]

  # Boolean arguments
  options.each_pair do |k, v|
    if BOOLEAN_ARGS.include?(k) && v
      cmd << "-#{k}"
    end
  end

  # Fixnum arguments
  [ :rotate, :restart ].each do |arg|
    if Fixnum === options[arg]
      cmd << "-#{arg} #{options[arg]}"
    elsif options.include?(arg)
      raise Error.new("Invalid value for :#{arg} option. Fixnum expected.")
    end
  end

  # String arguments
  [ :crop, :scans ].each do |arg|
    if String === options[arg]
      cmd << "-#{arg} #{options[arg]}"
    elsif options.include?(arg)
      raise Error.new("Invalid value for :#{arg} option. Structured string expected. See 'jpegtran' reference.")
    end
  end

  # Copy
  if options.include?(:copy)
    value = options[:copy].to_sym

    if COPY_OPTIONS.include?(value)
      cmd << "-copy #{value}"
    else
      raise Error.new("Invalid value for :copy. Expected #{COPY_OPTIONS}")
    end
  end

  # Flip
  if options.include?(:flip)
    value = options[:flip].to_sym

    if FLIP_OPTIONS.include?(value)
      cmd << "-flip #{value}"
    else
      raise Error.new("Invalid value for :flip. Expected #{FLIP_OPTIONS}")
    end
  end

  # Outfile
  if options.include?(:outfile)
    if String === options[:outfile]
      out = options[:outfile]
    else
      raise Error.new("Invalid value for :outfile option. String expected.")
    end
  else
    out = path.to_s
  end

  cmd << "-outfile #{out}"
  cmd << path.to_s
  cmd = cmd.join(" ")

  IO.popen(cmd) do |f|
    f.read.chomp
  end
end