-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate-output-multi-page
executable file
·204 lines (158 loc) · 6.46 KB
/
generate-output-multi-page
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/ruby -w
def usage_and_exit
STDERR.puts <<EOUSAGE
Usage: <paper> [ <dpi> ]
... where <paper> is A4 or A0, and <dpi> is probably 300 or 600. The
value of <dpi> defaults to 600, and the script will generate .pcl and
.pjl output if it\'s 600.
EOUSAGE
exit(-1)
end
unless (((ARGV.length == 1) || (ARGV.length == 2)) && ((ARGV[0] == "A4") || (ARGV[0] == "A0")))
usage_and_exit
end
paper = ARGV[0]
if paper == "A0"
paper_width_in_inches = 33.11
paper_height_in_inches = 46.81
elsif paper == "A4"
paper_width_in_inches = 8.27
paper_height_in_inches = 11.69
end
dpi = ARGV[1]
if dpi
begin
dpi = Integer(dpi)
rescue
usage_and_exit
end
else
dpi = 600
end
puts "paper_height_in_inches_* #{paper_width_in_inches} x #{paper_height_in_inches}"
pnmtops = "pnmtops"
pamscale = "pnmscale"
pnmtopng = "pnmtopng"
pgmtoppm = "pgmtoppm"
# ------------------------------------------------------------------------
# You probably don't have these:
# ppmtopcl3 = "/home/mark/ppmtopcl3"
# pjlsetpcl = "/home/mark/pjl-setpcl"
# pjlreset = "/home/mark/pjl-reset"
# ------------------------------------------------------------------------
pnm_files = Dir['poster-complete-*.pnm']
pnm_files.sort!
pnm_files.each do |pnmfile|
pnmfile_base = pnmfile.gsub(/.pnm/,'')
pnmfile_reduce = pnmfile_base + "-#{paper}-#{dpi}dpi.pam"
pngfile_reduce = pnmfile_base + "-#{paper}-#{dpi}dpi.png"
ppmfile_reduce = pnmfile_base + "-#{paper}-#{dpi}dpi.ppm"
# pclfile_reduce = pnmfile_base + "-#{paper}-#{dpi}dpi.pcl"
# pjlfile_reduce = pnmfile_base + "-#{paper}-#{dpi}dpi.pjl"
epsfile = pnmfile_base + "-#{paper}-#{dpi}dpi.eps"
real_width = nil
real_height = nil
open(pnmfile) do |f|
f.gets
line = f.gets
unless line =~ /^(\d+) (\d+)/
STDERR.puts "This doesn't look like a pnm file to me."
exit(-1)
end
real_width = Integer($1)
real_height = Integer($2)
end
scaling_width = real_width + 200
scaling_height = real_height
puts "width with margins for scaling (scaling_width) #{scaling_width}"
puts "height with margins for scaling (scaling_height) #{scaling_height}"
# pnmtops likes dimensions in inches
paper_width_in_inches_smaller = paper_width_in_inches * 0.99
paper_height_in_inches_smaller = paper_height_in_inches * 0.99
puts "paper_*_in_inches_smaller #{paper_width_in_inches_smaller} x #{paper_height_in_inches_smaller}"
# Not exactly sqrt(2) in fact...
proportions_of_paper = paper_height_in_inches / paper_width_in_inches
proportions_of_bitmap = scaling_height / Float(scaling_width)
final_height_in_inches = nil
final_width_in_inches = nil
if( proportions_of_bitmap > proportions_of_paper )
puts("scaling to height...")
# Then the height in our bitmap is the one we should scale to.
final_height_in_pixels_with_possible_margin = Integer(paper_height_in_inches_smaller * Float(dpi))
puts " final_height_in_pixels_with_possible_margin: #{final_height_in_pixels_with_possible_margin}"
pixel_scaling = scaling_height / Float(final_height_in_pixels_with_possible_margin)
puts " pixel_scaling: #{pixel_scaling}"
final_width_in_pixels = Integer(real_width / pixel_scaling)
final_height_in_pixels = Integer(real_height / pixel_scaling)
puts " final_*_in_pixels: #{final_width_in_pixels} x #{final_height_in_pixels}"
final_width_in_inches = final_width_in_pixels / Float(dpi)
final_height_in_inches = final_height_in_pixels / Float(dpi)
puts " final_*_in_inches: #{final_width_in_inches} x #{final_height_in_inches}"
if FileTest.exists?( pnmfile_reduce )
puts " #{pnmfile_reduce} exists, not regenerating..."
else
puts " Generating reduced PNM."
command = "#{pamscale} -height #{final_height_in_pixels} #{pnmfile} > #{pnmfile_reduce}"
puts " Running: #{command}"
system command
end
if FileTest.exists?( pngfile_reduce )
puts " #{pngfile_reduce} exists, not regenerating..."
else
puts " Generating reduce PNG from that."
command = "#{pnmtopng} -compression 3 #{pnmfile_reduce} > #{pngfile_reduce}"
puts " Running: #{command}"
system command
end
command = "#{pnmtops} -imageheight #{final_height_in_inches} #{pnmfile_reduce} > #{epsfile}"
puts " Generating EPS file: #{command}"
system command
else
puts("scaling to width...")
# Then the width in our bitmap is the one we should scale to.
final_width_in_pixels_with_possible_margin = Integer(paper_width_in_inches_smaller * Float(dpi))
puts " final_width_in_pixels_with_possible_margin: #{final_width_in_pixels_with_possible_margin}"
pixel_scaling = scaling_width / Float(final_width_in_pixels_with_possible_margin)
puts " pixel_scaling: #{pixel_scaling}"
final_width_in_pixels = Integer(real_width / pixel_scaling)
final_height_in_pixels = Integer(real_height / pixel_scaling)
puts " final_*_in_pixels: #{final_width_in_pixels} x #{final_height_in_pixels}"
final_width_in_inches = final_width_in_pixels / Float(dpi)
final_height_in_inches = final_height_in_pixels / Float(dpi)
puts " final_*_in_inches: #{final_width_in_inches} x #{final_height_in_inches}"
if FileTest.exists?( pnmfile_reduce )
puts " #{pnmfile_reduce} exists, not regenerating..."
else
puts " Generating reduced PNM."
command = "#{pamscale} -width #{final_width_in_pixels} #{pnmfile} > #{pnmfile_reduce}"
puts " Running: #{command}"
system command
end
if FileTest.exists?( pngfile_reduce )
puts " #{pngfile_reduce} exists, not regenerating..."
else
puts " Generating reduce PNG from that."
command = "#{pnmtopng} -compression=3 #{pnmfile_reduce} > #{pngfile_reduce}"
puts " Running: #{command}"
system command
end
command = "#{pnmtops} -imageheight #{final_height_in_inches} #{pnmfile_reduce} > #{epsfile}"
puts " Generating EPS file: #{command}"
system command
end
# if 600 == dpi
#
# command = "#{pgmtoppm} rgb:ff/ff/ff #{pnmfile_reduce} > #{ppmfile_reduce}"
# puts " Generating PPM file: #{command}"
# system command
#
# command = "#{ppmtopcl3} < #{ppmfile_reduce} > #{pclfile_reduce}"
# puts " Generating PCL file: #{command}"
# system command
#
# command = "cat #{pjlsetpcl} #{pclfile_reduce} #{pjlreset} > #{pjlfile_reduce}"
# puts " Generating PJL file: #{command}"
# system command
#
# end
end