breaking out image processing

This commit is contained in:
John Bintz 2010-01-05 21:39:35 -05:00
parent dbd73b1126
commit 247c12ad8e
4 changed files with 90 additions and 36 deletions

View File

@ -1,7 +1,7 @@
.PHONY : test test-coverage
test :
testrb -b tests/*
testrb -b tests/*.rb
test-coverage :
rcov -x '\/jruby\/lib\/' -x '\/tests\/data\/' tests/*.rb

View File

@ -45,41 +45,6 @@ class Filter
recalc_pixels
end
#
# Build a temporary PNG from an SVG file
#
def inkscape(input, target)
params = []
width = @config['width']
height = @config['height']
inkscape_target = target
if @config['rotate']
case @config['rotate']
when 90, -90
t = width; width = height; height = t
inkscape_target = target + "-pre.png"
end
end
if width && (width.to_i != 0); params << "-w #{width} "; end
if height && (height.to_i != 0); params << "-h #{height} "; end
system("inkscape -e \"#{inkscape_target}\" -y 1.0 #{params.join(" ")} \"#{input}\"")
if @config['rotate']
command = [
"\"#{inkscape_target}\"",
"-rotate #{@config['rotate']}",
"\"#{target}\""
]
convert(command)
File.unlink(inkscape_target)
end
end
def convert(command, verbose = false)
if verbose
puts "convert " + (verbose ? "-verbose " : "" ) + [ command ].flatten.join(" ")

View File

@ -0,0 +1,57 @@
module ImageProcessing
def setup_inkscape(target)
params = []
width = @config['width']
height = @config['height']
inkscape_target = target
if @config['rotate']
case @config['rotate']
when 90, -90
t = width; width = height; height = t
inkscape_target = target + "-pre.png"
end
end
if width && (width.to_i != 0)
params << "-w #{width}"
end
if height && (height.to_i != 0)
params << "-h #{height}"
end
[ params, inkscape_target ]
end
#
# Build a PNG from an SVG file
#
def inkscape(input, target)
params, inkscape_target = setup_inkscape(target)
call_system("inkscape -e \"#{inkscape_target}\" -y 1.0 #{params.join(" ")} \"#{input}\"")
if @config['rotate']
command = [
"\"#{inkscape_target}\"",
"-rotate #{@config['rotate']}",
"\"#{target}\""
]
convert(command)
File.unlink(inkscape_target)
end
end
def convert(command, verbose = false)
if verbose
puts "convert " + (verbose ? "-verbose " : "" ) + [ command ].flatten.join(" ")
end
call_system("convert " + (verbose ? "-verbose " : "" ) + [ command ].flatten.join(" "))
end
def call_system(command)
Kernel.system(command)
end
end

View File

@ -0,0 +1,32 @@
require "test/unit"
require File.dirname(__FILE__) + '/../modules/ImageProcessing.rb'
class TestImageProcessing < Test::Unit::TestCase
def setup
@instance = Class.new do
include ImageProcessing
attr_accessor :config
end.new
end
def test_setup_inkscape
[
[ {}, [], 'target' ],
[ { 'width' => 'test' }, [], 'target' ],
[ { 'width' => 200 }, ['-w 200'], 'target' ],
[ { 'height' => 'test' }, [], 'target' ],
[ { 'height' => 200 }, ['-h 200'], 'target' ],
[ { 'rotate' => 0 }, [], 'target' ],
[ { 'rotate' => 90 }, [], 'target-pre.png' ],
[ { 'rotate' => 90, 'width' => 50, 'height' => 75 }, ['-w 75', '-h 50'], 'target-pre.png' ],
].each do |config, expected_params, expected_target|
@instance.config = config
params, inkscape_target = @instance.setup_inkscape('target')
assert_equal expected_params, params
assert_equal inkscape_target, expected_target
end
end
end