/[flash]/openlayers/pdf2tiles.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /openlayers/pdf2tiles.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 22 - (show annotations)
Mon Feb 25 00:19:40 2008 UTC (16 years, 2 months ago) by dpavlin
File MIME type: text/plain
File size: 3392 byte(s)
change magic to simplify procedure: all pages are now padded to tile size
and centered which resulted in some memory increase (up to tile boundry),
but simplified code a lot.
1 #!/usr/bin/perl -w
2
3 # pdf2tiles.pl
4 #
5 # 02/24/08 01:22:36 CET Dobrica Pavlinusic <dpavlin@rot13.org>
6
7 use strict;
8
9 use Imager;
10 use File::Path;
11 use Data::Dump qw/dump/;
12
13 my $pdf = shift @ARGV || die "usage: $0 filename.pdf";
14
15 my $debug = 1;
16
17 my $from_page = 1;
18 my $to_page = 4;
19
20 my $tiles_path = 'tiles/basic';
21
22 foreach my $path ( $tiles_path, 'ppm' ) {
23 rmtree $path if -d $path;
24 mkpath $path || die "can't create $path: $!";
25 }
26
27 print "redering test 100dpi page to get pixel size\n";
28 system("pdftoppm -f 1 -l 1 -r 100 $pdf ppm/test") == 0 or die "can't render test: $?";
29 my $test = 'ppm/test-000001.ppm';
30 open(my $fh, '<', $test) || die "can't open $test: $!";
31 my $res = <$fh>;
32 $res = <$fh>;
33 my ( $x_res, $y_res ) = split(/\s+/,$res);
34 print "## 100 dpi resolution = $x_res*$y_res\n";
35 close($fh);
36
37 $x_res *= ( $to_page - $from_page + 1 );
38
39 print "## total size = $x_res*$y_res\n";
40
41 # initial width = 2 tiles
42 my $curr_w = 256 * 2;
43
44 my $start_dpi = int( 100 / ( $x_res / $curr_w ) );
45
46 foreach my $zoom ( 1 .. 10 ) {
47
48 my $dpi = $start_dpi * $zoom;
49
50 my $ppm = sprintf("ppm/%03d", $dpi);
51
52 print "rendering pdf $pdf pages $from_page-$to_page in $dpi dpi\n";
53
54 my $cmd = "pdftoppm -f $from_page -l $to_page -r $dpi -aa yes -aaVector yes $pdf $ppm";
55 system($cmd) == 0 or die "can't start $cmd: $?";
56
57 my @page_imgs;
58
59 # size of all pages
60 my ( $x_size, $y_size ) = (0,0);
61
62 foreach my $page ( $from_page .. $to_page ) {
63
64 my $tmp = sprintf("ppm/%03d-%06d.ppm", $dpi, $page);
65 die "can't find page $tmp" unless -f $tmp;
66
67 my $p_img = Imager->new;
68 $p_img->read(file=>$tmp) or die "Can't load $tmp: ", $p_img->errstr;
69
70 $x_size += $p_img->getwidth();
71 my $h = $p_img->getheight();
72 $y_size = $h if $h > $y_size;
73
74 push @page_imgs, $p_img;
75 }
76
77 print "loaded $from_page-$to_page of $x_size*$y_size pixels\n";
78
79 my $back_color = Imager::Color->new(255, 127, 127);
80
81 sub pad {
82 my $s = shift;
83 return 256 - ( $s % 256);
84 }
85
86 my ( $full_x, $full_y ) = ( $x_size + pad( $x_size ), $y_size + pad( $y_size ) );
87
88 my $img = Imager->new( xsize => $full_x, ysize => $full_y );
89 $img->box(filled=>1, color=>$back_color);
90
91 my $x_pos = int( pad( $x_size ) / 2 );
92 my $y_pos = int( pad( $y_size ) / 2 );
93
94 foreach my $page_img ( @page_imgs ) {
95 $img->paste( left => $x_pos, top => $y_pos, img => $page_img );
96 $x_pos += $page_img->getwidth();
97 }
98
99 undef @page_imgs;
100
101 $img->write( file => sprintf("zoom-%03d.jpg", $dpi ) ) if $debug;
102
103 my $tiles_x = int( $x_size / 256 );
104 my $tiles_y = int( $y_size / 256 );
105
106 print "creating in $tiles_x*$tiles_y tiles from $full_x*$full_y\n";
107
108 for my $y ( 0 .. $tiles_y ) {
109 for my $x ( 0 .. $tiles_x ) {
110
111 my $size = {
112 left => $x * 256,
113 bottom => $full_y - $y * 256,
114 width => 256,
115 height => 256,
116 };
117
118 my $tile = $img->crop( %$size ) or die "can't crop $x*$y ",dump( $size );
119
120 # emulate TileCache disk layout
121 my $path = sprintf("%s/%02d/%03d/%03d/%03d/%03d/%03d/%03d.png",
122 $tiles_path,
123 $zoom - 1, # starts with 0
124 int( $x / 1000000 ),
125 int( $x / 1000 ) % 1000,
126 $x % 1000,
127 int( $y / 1000000 ),
128 int( $y / 1000 ) % 1000,
129 $y % 1000
130 );
131
132 my $dir = $path;
133 $dir =~ s,/[^/]+$,,;
134 mkpath $dir unless -d $dir;
135
136 $tile->write( file => $path ) or die $tile->errstr;
137
138 undef $tile;
139
140 printf("# %2d*%-2d -> %s\n", $x, $y, $path);
141
142 }
143 }
144
145 # break if zoom level over 300dpi
146 last if $dpi > 300;
147 }
148
149

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26