1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# module : graphics/inkscape
# copyright : PRAGMA Advanced Document Engineering
# version : 2002-2005
# author : Hans Hagen
#
# project : ConTeXt / eXaMpLe
# concept : Hans Hagen
# info : j.hagen@xs4all.nl
# www : www.pragma-ade.com
# ['base/variables','variables'].each do |r| begin require r ; rescue Exception ; else break ; end ; end
require 'base/variables'
class ImageMagick
include Variables
def initialize(logger=nil)
unless logger then
puts('magick class needs a logger')
exit
end
@variables = Hash.new
@logger = logger
reset
end
def reset
['compression','depth','colorspace','quality'].each do |key|
setvariable(key)
end
end
def supported?(filename) # ? pdf
filename =~ /.*\.(png|gif|tif|tiff|jpg|jpeg|eps|ai\d*)/io
end
def convert(suffix='pdf')
inpfilename = getvariable('inputfile').dup
outfilename = getvariable('outputfile').dup
outfilename = inpfilename.dup if outfilename.empty?
outfilename.gsub!(/(\.[^\.]*?)$/, ".#{suffix}")
if inpfilename.empty? || outfilename.empty? then
report("no filenames given")
return false
end
if inpfilename == outfilename then
report("filenames must differ (#{inpfilename} #{outfilename})")
return false
end
unless FileTest.file?(inpfilename) then
report("unknown file #{inpfilename}")
return false
end
if inpfilename =~ /\.tif+$/io then
tmpfilename = 'temp.png'
arguments = "#{inpfilename} #{tmpfilename}"
begin
debug("imagemagick: #{arguments}")
ok = System.run('imagemagick',arguments)
rescue
report("aborted due to error")
return false
else
return false unless ok
end
inpfilename = tmpfilename
end
compression = depth = colorspace = quality = ''
if getvariable('compression') =~ /(zip|jpeg)/o then
compression = " -compress #{$1}"
end
if getvariable('depth') =~ /(8|16)/o then
depth = "-depth #{$1}"
end
if getvariable('colorspace') =~ /(gray|rgb|cmyk)/o then
colorspace = "-colorspace #{$1}"
end
case getvariable('quality')
when 'low' then quality = '-quality 0'
when 'medium' then quality = '-quality 75'
when 'high' then quality = '-quality 100'
end
report("converting #{inpfilename} to #{outfilename}")
arguments = [compression,depth,colorspace,quality,inpfilename,outfilename].join(' ').gsub(/\s+/,' ')
begin
debug("imagemagick: #{arguments}")
ok = System.run('imagemagick',arguments)
rescue
report("aborted due to error")
return false
else
return ok
end
end
def autoconvert
inpfilename = getvariable('inputfile')
outfilename = getvariable('outputfile')
if inpfilename.empty? || ! FileTest.file?(inpfilename) then
report("missing file #{inpfilename}")
return
end
outfilename = inpfilename.dup if outfilename.empty?
tmpfilename = 'temp.jpg'
reset
megabyte = 1024*1024
ok = false
if FileTest.size(inpfilename)>2*megabyte
setvariable('compression','zip')
ok = convert
else
setvariable('compression','jpeg')
if FileTest.size(inpfilename)>10*megabyte then
setvariable('quality',85)
elsif FileTest.size(inpfilename)>5*megabyte then
setvariable('quality',90)
else
setvariable('quality',95)
end
report("auto quality #{getvariable('quality')}")
setvariable('outputfile', tmpfilename)
ok = convert('jpg')
setvariable('inputfile', tmpfilename)
setvariable('outputfile', outfilename)
ok = convert
begin
File.delete(tmpfilename)
rescue
report("#{tmpfilename} cannot be deleted")
end
end
reset
return ok
end
end
|