/[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 23 - (show annotations)
Mon Feb 25 00:56:00 2008 UTC (16 years, 2 months ago) by dpavlin
File MIME type: text/plain
File size: 3943 byte(s)
- added margins between pages
- correctly place times (but projections still get in the way of zooming in openlayers)
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 $start_dpi = int( 100 / ( $x_res / $curr_w ) ) - 1;
52
53 foreach my $zoom ( 1 .. 10 ) {
54
55 my $dpi = $start_dpi * $zoom;
56
57 my $ppm = sprintf("ppm/%03d", $dpi);
58
59 print "rendering pdf $pdf pages $from_page-$to_page in $dpi dpi\n";
60
61 my $cmd = "pdftoppm -f $from_page -l $to_page -r $dpi -aa yes -aaVector yes $pdf $ppm";
62 system($cmd) == 0 or die "can't start $cmd: $?";
63
64 my @page_imgs;
65
66 # size of all pages
67 my ( $x_size, $y_size ) = (0,0);
68
69 foreach my $page ( $from_page .. $to_page ) {
70
71 my $tmp = sprintf("ppm/%03d-%06d.ppm", $dpi, $page);
72 die "can't find page $tmp" unless -f $tmp;
73
74 my $p_img = Imager->new;
75 $p_img->read(file=>$tmp) or die "Can't load $tmp: ", $p_img->errstr;
76
77 $x_size += $p_img->getwidth() + $margin_px;
78 my $h = $p_img->getheight();
79 $y_size = $h if $h > $y_size;
80
81 push @page_imgs, $p_img;
82 }
83
84 if ( $x_size > $curr_w ) {
85 print "WARNING: calculated size with margins $x_size > $curr_w\n";
86 $x_size = $curr_w;
87 }
88
89 print "loaded $from_page-$to_page of $x_size*$y_size pixels\n";
90
91 sub pad {
92 my $s = shift;
93 my $l = $s % 256;
94 return $l ? 256 - $l : 0;
95 }
96
97 my ( $full_x, $full_y ) = ( $x_size + pad( $x_size ), $y_size + pad( $y_size ) );
98
99 my $img = Imager->new( xsize => $full_x, ysize => $full_y );
100 $img->box(filled=>1, color=>$back_color);
101
102 my $x_pos = int( pad( $x_size ) / 2 );
103 my $y_pos = int( pad( $y_size ) / 2 );
104
105 foreach my $page_img ( @page_imgs ) {
106 $img->paste( left => $x_pos, top => $y_pos, img => $page_img );
107 $x_pos += $page_img->getwidth() + $margin_px;
108 }
109
110 undef @page_imgs;
111
112 $img->write( file => sprintf("zoom-%03d.jpg", $dpi ) ) if $debug;
113
114 my $tiles_x = int( $x_size / 256 );
115 my $tiles_y = int( $y_size / 256 );
116
117 my $tx_offset = int( ( $curr_w - $x_size ) / ( 2 * 256 ) );
118 my $ty_offset = int( ( $curr_h - $y_size ) / ( 2 * 256 ) );
119
120 print "creating in $tiles_x*$tiles_y tiles from $full_x*$full_y\n";
121
122 for my $y ( 0 .. $tiles_y ) {
123 for my $x ( 0 .. $tiles_x ) {
124
125 my $size = {
126 left => $x * 256,
127 bottom => $full_y - $y * 256,
128 width => 256,
129 height => 256,
130 };
131
132 my $tile = $img->crop( %$size ) or die "can't crop $x*$y ",dump( $size );
133
134 my $tx = $x + $tx_offset;
135 my $ty = $y + $ty_offset;
136
137 # emulate TileCache disk layout
138 my $path = sprintf("%s/%02d/%03d/%03d/%03d/%03d/%03d/%03d.png",
139 $tiles_path,
140 $zoom - 1, # starts with 0
141 int( $tx / 1000000 ),
142 int( $tx / 1000 ) % 1000,
143 $tx % 1000,
144 int( $ty / 1000000 ),
145 int( $ty / 1000 ) % 1000,
146 $ty % 1000
147 );
148
149 my $dir = $path;
150 $dir =~ s,/[^/]+$,,;
151 mkpath $dir unless -d $dir;
152
153 $tile->write( file => $path ) or die $tile->errstr;
154
155 undef $tile;
156
157 printf("# %2dx%-2d => %2dx%-2d %s\n", $x, $y, $tx, $ty, $path);
158
159 }
160 }
161
162 # break if zoom level over 300dpi
163 last if $dpi > 300;
164
165 # increase virtual size twice
166 $curr_w += $curr_w;
167 $curr_h += $curr_h;
168 }
169
170

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26