/[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

Diff of /openlayers/pdf2tiles.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 18 by dpavlin, Sun Feb 24 12:41:41 2008 UTC revision 24 by dpavlin, Mon Feb 25 00:58:52 2008 UTC
# Line 12  use Data::Dump qw/dump/; Line 12  use Data::Dump qw/dump/;
12    
13  my $pdf = shift @ARGV || die "usage: $0 filename.pdf";  my $pdf = shift @ARGV || die "usage: $0 filename.pdf";
14    
15  my $limit = '';  my $debug = 0;
16  $limit = '-f 1 -l 1';  
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';  my $tiles_path = 'tiles/basic';
 rmtree $tiles_path if -d $tiles_path;  
 mkpath $tiles_path || die "can't create $tiles_path: $!";  
26    
27  foreach my $zoom ( 1 .. 12 ) {  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 $dpi = $zoom * 15;          my @page_imgs;
68    
69          my $ppm = "tmp-$dpi";          # size of all pages
70          my $tmp = "$ppm-000001.ppm";          my ( $x_size, $y_size ) = (0,0);
71    
72          print "rendering pdf $pdf in $dpi dpi to $tmp\n";          foreach my $page ( $from_page .. $to_page ) {
73    
74          system "pdftoppm $limit -r $dpi -aa yes -aaVector yes $pdf $ppm";                  my $tmp = sprintf("ppm/%03d-%06d.ppm", $dpi, $page);
75                    die "can't find page $tmp" unless -f $tmp;
76    
77          die "can't render" unless -f $tmp;                  my $p_img = Imager->new;
78                    $p_img->read(file=>$tmp) or die "Can't load $tmp: ", $p_img->errstr;
79    
80          my $img = Imager->new;                  $x_size += $p_img->getwidth() + $margin_px;
81          $img->read(file=>$tmp) or die "Can't load $tmp: ", $img->errstr;                  my $h = $p_img->getheight();
82                    $y_size = $h if $h > $y_size;
83    
84          my $x_size = $img->getwidth();                  push @page_imgs, $p_img;
85          my $y_size = $img->getheight();          }
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          print "loaded $tmp $x_size*$y_size pixels\n";          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 );          my $tiles_x = int( $x_size / 256 );
118          my $tiles_y = int( $y_size / 256 );          my $tiles_y = int( $y_size / 256 );
119    
120          print "creating in $tiles_x*$tiles_y tiles\n";          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 ) {          for my $y ( 0 .. $tiles_y ) {
126                  for my $x ( 0 .. $tiles_x ) {                  for my $x ( 0 .. $tiles_x ) {
127    
128                          my $size = {                          my $size = {
129                                  left    => $x * 256,                                  left    => $x * 256,
130                                  bottom  => $y_size - $y * 256,                                  bottom  => $full_y - $y * 256,
131                                  width   => $x == $tiles_x ? $x_size % 256 : 256,                                  width   => 256,
132                                  height  => $y == $tiles_y ? $y_size % 256 : 256,                                  height  => 256,
133                          };                          };
134    
135                          my $tile = $img->crop( %$size ) or die "can't crop $x*$y ",dump( $size );                          my $tile = $img->crop( %$size ) or die "can't crop $x*$y ",dump( $size );
136    
137                          if ( ( $x == $tiles_x ) || ( $y == $tiles_y ) ) {                          my $tx = $x + $tx_offset;
138                                  warn "## expand tile to full size\n";                          my $ty = $y + $ty_offset;
                                 my $t2 = Imager->new(xsize => 256, ysize => 256);  
                                 $t2->paste(  
                                         top => 256 - $size->{height},  
                                         left => 0,  
                                         src => $tile,  
                                 );  
                                 $tile = $t2;  
                         }  
139    
140                          # emulate TileCache disk layout                          # emulate TileCache disk layout
141                          my $path = sprintf("%s/%02d/%03d/%03d/%03d/%03d/%03d/%03d.png",                          my $path = sprintf("%s/%02d/%03d/%03d/%03d/%03d/%03d/%03d.png",
142                                  $tiles_path,                                  $tiles_path,
143                                  $zoom - 1,      # starts with 0                                  $zoom - 1,      # starts with 0
144                                  int( $x / 1000000 ),                                  int( $tx / 1000000 ),
145                                  int( $x / 1000 ) % 1000,                                  int( $tx / 1000 ) % 1000,
146                                  $x % 1000,                                  $tx % 1000,
147                                  int( $y / 1000000 ),                                  int( $ty / 1000000 ),
148                                  int( $y / 1000 ) % 1000,                                  int( $ty / 1000 ) % 1000,
149                                  $y % 1000                                  $ty % 1000
150                          );                          );
151    
152                          my $dir = $path;                          my $dir = $path;
# Line 88  foreach my $zoom ( 1 .. 12 ) { Line 157  foreach my $zoom ( 1 .. 12 ) {
157    
158                          undef $tile;                          undef $tile;
159    
160                          print "# $x*$y -> $path\n";                          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    

Legend:
Removed from v.18  
changed lines
  Added in v.24

  ViewVC Help
Powered by ViewVC 1.1.26