<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -3,84 +3,92 @@
 pngtopnm = &quot;/home/mark/netpbm-10.34/converter/other/pngtopnm&quot;
 pnmtopng = &quot;/home/mark/netpbm-10.34/converter/other/pnmtopng&quot;
 
-png_files = Dir['poster-*-00*.png']
+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'
 
-page_numbers = Hash.new
+png_files = Dir['poster-inline-breaks-*-00*.png']
+png_files.sort!
 
-png_files.each do |p|
-  p =~ /^poster-inline-breaks-(\d+)-/
-  page_numbers[$1] = true
-  p_converted = p.gsub(/png$/,'pnm')
-  if FileTest.exists?( p_converted )
-    puts &quot;#{p} already converted&quot;
-  else
-    puts &quot;Converting #{p} to PGM&quot;
-    system &quot;#{pngtopnm} #{p} &gt; #{p_converted}&quot;
-  end
-end
+page_numbers = png_files.collect { |e| e.gsub(/poster-inline-breaks-(\d\d)-.*/,'\1') }.sort.uniq
 
-pages = page_numbers.length
+puts &quot;There seem to be #{page_numbers.length} pages of poster...&quot;
 
-def pnm_size( filename )
-  
-  size_string = nil
-  
-  open(filename) do |f|
-    f.gets # Read the P5
-    size_string = f.gets # Read the width and height
-  end
+widths  = Array.new( page_numbers.length )
+heights = Array.new( page_numbers.length, 0 )
+
+# Calculate the final dimensions of each poster:
 
-  if size_string =~ /^(\d+) (\d+) */
-    return [ Integer($1), Integer($2) ]
+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 &quot;BUG: Not all the same width!&quot;
+    end
   else
-    return nil
+    widths[page] = png_width
   end
+  heights[page] += png_height
+end
 
+puts &quot;Poster pages will have dimensions:&quot;
+widths.each_index do |i|
+  puts sprintf( &quot;%02d: %d x %d&quot;, i, widths[i], heights[i] )
 end
 
-0.upto(pages-1) do |i|
+# Now create the huge PNM files, by stripping the headings from each
+# and concatenating:
 
-  width = nil
-  total_height = 0
+pages_started = Array.new( page_numbers.length, false )
 
-  pnm_files = Dir[sprintf('poster-inline-breaks-%02d-00*.pnm',i)]
-  pnm_files.sort!
+png_files.each do |png_file|
 
-  pnm_files.each do |p|
+  png_file =~ /^poster-inline-breaks-(\d+)-/
+  page_string = $1
+  page = Integer(page_string)
 
-    w, h = pnm_size( p )
+  output_filename = sprintf(complete_page_template_pnm,page);
 
-    if width
-      if w != width
-        raise &quot;BUG: Not all the same width!&quot;
-      end
-    else
-      width = w
+  unless pages_started[page]
+    puts &quot;Starting new page: #{page}&quot;
+    # Output the heading and indicated we've started:
+    output_filename = sprintf(complete_page_template_pnm,page)
+    open( output_filename, &quot;w&quot; ) do |o|
+      o.puts(&quot;P5&quot;)
+      o.puts(&quot;#{widths[page]} #{heights[page]}&quot;)
+      o.puts(&quot;255&quot;)
     end
-
-    total_height += h
-    
+    pages_started[page] = true
   end
 
-  output_file = sprintf('poster-complete-%02d.pnm',i)
-  output_file_png = sprintf('poster-complete-%02d.png',i)
-
-  open( output_file, &quot;w&quot; ) do |o|
-    o.puts(&quot;P5&quot;)
-    o.puts(&quot;#{width} #{total_height}&quot;)
-    o.puts(&quot;255&quot;)
-    pnm_files.each do |p|
-      puts &quot;Concatenating #{p}...&quot;
-      open(p) do |f|
+  puts &quot;  Adding strip: #{png_file}&quot;
+  open( output_filename, &quot;a&quot; ) do |o|
+    Kernel.open( &quot;|-&quot;, &quot;r&quot; ) 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 &quot;Couldn't exec #{command}: #{$!}\n&quot;
+        end
       end
     end
   end
 
-  # puts &quot;Converting to a PNG&quot;
-  # system &quot;#{pnmtopng} #{output_file} &gt; #{output_file_png}&quot;
-
 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 &quot;Creating PNG version of page #{i}:&quot;
+#   pnm = sprintf( complete_page_template_pnm, i )
+#   png = sprintf( complete_page_template_png, i )
+#   system &quot;#{pnmtopng} #{pnm} &gt; #{png}&quot;
+# end</diff>
      <filename>compose-pnm-files-multi-page</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>7655ba1c657af9853a516e6c039c993bff3af7cc</id>
    </parent>
  </parents>
  <author>
    <name>Mark Longair</name>
    <email>mark@claret.study</email>
  </author>
  <url>http://github.com/mhl/unicode-poster/commit/c663bec349a353abe5f4bcc78b75e28406d82b81</url>
  <id>c663bec349a353abe5f4bcc78b75e28406d82b81</id>
  <committed-date>2008-07-18T11:28:08-07:00</committed-date>
  <authored-date>2008-07-18T11:28:08-07:00</authored-date>
  <message>Save 12 GiB of disk space by converting to PGM and concatenating in one go</message>
  <tree>234d9c97fe826fbab4a3cce2618641929302d34b</tree>
  <committer>
    <name>Mark Longair</name>
    <email>mark@claret.study</email>
  </committer>
</commit>
