/[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 24 - (show annotations)
Mon Feb 25 00:58:52 2008 UTC (16 years, 2 months ago) by dpavlin
File MIME type: text/plain
File size: 4054 byte(s)
take vertical dpi also into consideration
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 = 0;
16
17 my $from_page = 1;
18 my $to_page = 2;
19
20 my $total_pages = $to_page - $from_page + 1;
21
22 my $margin_px = 3;
23 my $back_color = Imager::Color->new(255, 255, 240);
24
25 my $tiles_path = 'tiles/basic';
26
27 foreach my $path ( $tiles_path, 'ppm' ) {
28 rmtree $path if -d $path;
29 mkpath $path || die "can't create $path: $!";
30 }
31
32 print "redering test 100dpi page to get pixel size\n";
33 system("pdftoppm -f 1 -l 1 -r 100 $pdf ppm/test") == 0 or die "can't render test: $?";
34 my $test = 'ppm/test-000001.ppm';
35 open(my $fh, '<', $test) || die "can't open $test: $!";
36 my $res = <$fh>;
37 $res = <$fh>;
38 my ( $x_res, $y_res ) = split(/\s+/,$res);
39 print "## 100 dpi resolution = $x_res*$y_res\n";
40 close($fh);
41
42 $x_res *= $total_pages;
43 $x_res += $total_pages * $margin_px;
44
45 print "## total size = $x_res*$y_res\n";
46
47 # initial = 2*1 tiles
48 my $curr_w = 256 * 2;
49 my $curr_h = 256;
50
51 my $x_dpi = int( 100 / ( $x_res / $curr_w ) ) - 1; # 1 dpi margins
52 my $y_dpi = int( 100 / ( $y_res / $curr_h ) );
53
54 my $start_dpi = $x_dpi < $y_dpi ? $x_dpi : $y_dpi;
55
56 foreach my $zoom ( 1 .. 10 ) {
57
58 my $dpi = $start_dpi * $zoom;
59
60 my $ppm = sprintf("ppm/%03d", $dpi);
61
62 print "rendering pdf $pdf pages $from_page-$to_page in $dpi dpi\n";
63
64 my $cmd = "pdftoppm -f $from_page -l $to_page -r $dpi -aa yes -aaVector yes $pdf $ppm";
65 system($cmd) == 0 or die "can't start $cmd: $?";
66
67 my @page_imgs;
68
69 # size of all pages
70 my ( $x_size, $y_size ) = (0,0);
71
72 foreach my $page ( $from_page .. $to_page ) {
73
74 my $tmp = sprintf("ppm/%03d-%06d.ppm", $dpi, $page);
75 die "can't find page $tmp" unless -f $tmp;
76
77 my $p_img = Imager->new;
78 $p_img->read(file=>$tmp) or die "Can't load $tmp: ", $p_img->errstr;
79
80 $x_size += $p_img->getwidth() + $margin_px;
81 my $h = $p_img->getheight();
82 $y_size = $h if $h > $y_size;
83
84 push @page_imgs, $p_img;
85 }
86
87 if ( $x_size > $curr_w ) {
88 print "WARNING: calculated size with margins $x_size > $curr_w\n";
89 $x_size = $curr_w;
90 }
91
92 print "loaded $from_page-$to_page of $x_size*$y_size pixels\n";
93
94 sub pad {
95 my $s = shift;
96 my $l = $s % 256;
97 return $l ? 256 - $l : 0;
98 }
99
100 my ( $full_x, $full_y ) = ( $x_size + pad( $x_size ), $y_size + pad( $y_size ) );
101
102 my $img = Imager->new( xsize => $full_x, ysize => $full_y );
103 $img->box(filled=>1, color=>$back_color);
104
105 my $x_pos = int( pad( $x_size ) / 2 );
106 my $y_pos = int( pad( $y_size ) / 2 );
107
108 foreach my $page_img ( @page_imgs ) {
109 $img->paste( left => $x_pos, top => $y_pos, img => $page_img );
110 $x_pos += $page_img->getwidth() + $margin_px;
111 }
112
113 undef @page_imgs;
114
115 $img->write( file => sprintf("zoom-%03d.jpg", $dpi ) ) if $debug;
116
117 my $tiles_x = int( $x_size / 256 );
118 my $tiles_y = int( $y_size / 256 );
119
120 my $tx_offset = int( ( $curr_w - $x_size ) / ( 2 * 256 ) );
121 my $ty_offset = int( ( $curr_h - $y_size ) / ( 2 * 256 ) );
122
123 print "creating in $tiles_x*$tiles_y tiles from $full_x*$full_y\n";
124
125 for my $y ( 0 .. $tiles_y ) {
126 for my $x ( 0 .. $tiles_x ) {
127
128 my $size = {
129 left => $x * 256,
130 bottom => $full_y - $y * 256,
131 width => 256,
132 height => 256,
133 };
134
135 my $tile = $img->crop( %$size ) or die "can't crop $x*$y ",dump( $size );
136
137 my $tx = $x + $tx_offset;
138 my $ty = $y + $ty_offset;
139
140 # emulate TileCache disk layout
141 my $path = sprintf("%s/%02d/%03d/%03d/%03d/%03d/%03d/%03d.png",
142 $tiles_path,
143 $zoom - 1, # starts with 0
144 int( $tx / 1000000 ),
145 int( $tx / 1000 ) % 1000,
146 $tx % 1000,
147 int( $ty / 1000000 ),
148 int( $ty / 1000 ) % 1000,
149 $ty % 1000
150 );
151
152 my $dir = $path;
153 $dir =~ s,/[^/]+$,,;
154 mkpath $dir unless -d $dir;
155
156 $tile->write( file => $path ) or die $tile->errstr;
157
158 undef $tile;
159
160 printf("# %2dx%-2d => %2dx%-2d %s\n", $x, $y, $tx, $ty, $path);
161
162 }
163 }
164
165 # break if zoom level over 300dpi
166 last if $dpi > 300;
167
168 # increase virtual size twice
169 $curr_w += $curr_w;
170 $curr_h += $curr_h;
171 }
172
173

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26