@@ -8,6 +8,7 @@ class ImageExtractor
88 MEMORY_ARGS = "-limit memory 256MiB -limit map 512MiB"
99 GHOSTSCRIPT_ARGS = "-q -dBATCH -dMaxBitmap=50000000 -dNOPAUSE -sDEVICE=tiff24nc -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r100x100"
1010 DEFAULT_FORMAT = :png
11+ CHUNK_SIZE = 10
1112
1213 # Extract a list of PDFs as rasterized page images, according to the
1314 # configuration in options.
@@ -31,20 +32,20 @@ def convert(pdf, size, format, previous=nil)
3132 pages = @pages || '1-' + Docsplit . extract_length ( pdf ) . to_s
3233 FileUtils . mkdir_p ( directory ) unless File . exists? ( directory )
3334 tiff_file = File . join ( tempdir , "#{ basename } .tif" )
34- out_file = File . join ( directory , "#{ basename } _%05d.#{ format } " )
3535 common = "#{ MEMORY_ARGS } #{ DENSITY_ARG } #{ resize_arg ( size ) } #{ quality_arg ( format ) } "
3636 if previous
3737 FileUtils . cp ( Dir [ directory_for ( previous ) + '/*' ] , directory )
3838 cmd = "MAGICK_TMPDIR=#{ tempdir } OMP_NUM_THREADS=2 gm mogrify #{ common } -unsharp 0x0.5+0.75 \" #{ directory } /*.#{ format } \" 2>&1"
3939 else
4040 cmd = "gs #{ GHOSTSCRIPT_ARGS } -sOutputFile=#{ tiff_file } -- #{ pdf } "
41- page_list ( pages , 10 ) . each do |nums |
41+ page_list ( pages , CHUNK_SIZE ) . each_with_index do |nums , chunk |
42+ out_file = File . join ( directory , "#{ basename } _chunk#{ chunk } _%05d.#{ format } " )
4243 cmd += " && MAGICK_TMPDIR=#{ tempdir } OMP_NUM_THREADS=2 gm convert +adjoin #{ common } \" #{ tiff_file } #{ pages_arg ( nums ) } \" \" #{ out_file } \" 2>&1"
4344 end
4445 end
4546 result = `#{ cmd } ` . chomp
4647 raise ExtractionFailed , result if $? != 0
47- renumber_images ( pages , out_file , format )
48+ renumber_images ( pages , File . join ( directory , basename + '*.' + format ) , format ) unless previous
4849 FileUtils . remove_entry_secure tempdir if File . exists? ( tempdir )
4950 end
5051
@@ -109,10 +110,11 @@ def page_list(pages, chunk_count=nil)
109110 # When GraphicsMagick is through, it will have generated a number of
110111 # incrementing page images, starting at 0. Renumber them with their correct
111112 # page numbers.
112- def renumber_images ( pages , template , format )
113- suffixer = /_0+(\d +)\. #{ format } \Z /
114- images = Dir [ template . sub ( '%05d' , '0*' ) ] . map do |path |
115- index = path [ suffixer , 1 ] . to_i
113+ def renumber_images ( pages , glob , format )
114+ suffixer = /_chunk(\d +)_0+(\d +)\. #{ format } \Z /
115+ images = Dir [ glob ] . map do |path |
116+ chunk = path [ suffixer , 1 ] . to_i
117+ index = chunk * CHUNK_SIZE + path [ suffixer , 2 ] . to_i
116118 { :path => path , :index => index , :page_number => index + 1 }
117119 end
118120 numbers = page_list ( pages ) . reverse
0 commit comments