-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompose-pnm-files-multi-page
executable file
·94 lines (76 loc) · 2.53 KB
/
compose-pnm-files-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
#!/usr/bin/ruby -w
pngtopnm = "pngtopnm"
pnmtopng = "pnmtopng"
page_glob_template = 'poster-inline-breaks-%02d-00*.pnm'
complete_page_template_pnm = 'poster-complete-%02d.pnm'
complete_page_template_png = 'poster-complete-%02d.png'
png_files = Dir['poster-inline-breaks-*-00*.png']
png_files.sort!
page_numbers = png_files.collect { |e| e.gsub(/poster-inline-breaks-(\d\d)-.*/,'\1') }.sort.uniq
puts "There seem to be #{page_numbers.length} pages of poster..."
widths = Array.new( page_numbers.length )
heights = Array.new( page_numbers.length, 0 )
# Calculate the final dimensions of each poster:
png_files.each do |png_file|
png_width, png_height = `png-find-grid-revised/png-size #{png_file}`.chomp.split(/x/).collect { |e| Integer(e) }
png_file =~ /^poster-inline-breaks-(\d+)-/
page = Integer($1)
if widths[page]
if png_width != widths[page]
raise "BUG: Not all the same width!"
end
else
widths[page] = png_width
end
heights[page] += png_height
end
puts "Poster pages will have dimensions:"
widths.each_index do |i|
puts sprintf( "%02d: %d x %d", i, widths[i], heights[i] )
end
# Now create the huge PNM files, by stripping the headings from each
# and concatenating:
pages_started = Array.new( page_numbers.length, false )
png_files.each do |png_file|
png_file =~ /^poster-inline-breaks-(\d+)-/
page_string = $1
page = Integer(page_string)
output_filename = sprintf(complete_page_template_pnm,page);
unless pages_started[page]
puts "Starting new page: #{page}"
# Output the heading and indicated we've started:
output_filename = sprintf(complete_page_template_pnm,page)
open( output_filename, "w" ) do |o|
o.puts("P5")
o.puts("#{widths[page]} #{heights[page]}")
o.puts("255")
end
pages_started[page] = true
end
puts " Adding strip: #{png_file}"
open( output_filename, "a" ) do |o|
Kernel.open( "|-", "r" ) do |f|
if f
f.gets # Read the P5
f.gets # Read the width and height
f.gets # Read the 255
o.write(f.read)
else
begin
exec( pngtopnm, png_file )
rescue
raise "Couldn't exec #{command}: #{$!}\n"
end
end
end
end
end
## This takes ages and is basically pointless since we almost
## always want to work with a reduced size version instead.
#
# page_numbers.each_index do |i|
# puts "Creating PNG version of page #{i}:"
# pnm = sprintf( complete_page_template_pnm, i )
# png = sprintf( complete_page_template_png, i )
# system "#{pnmtopng} #{pnm} > #{png}"
# end