/[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 17 by dpavlin, Sun Feb 24 12:30:19 2008 UTC revision 23 by dpavlin, Mon Feb 25 00:56:00 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;
 $limit = '-f 1 -l 1';  
16    
17  foreach my $zoom ( 1 .. 12 ) {  my $from_page   = 1;
18    my $to_page             = 2;
19    
20          my $dpi = $zoom * 25;  my $total_pages = $to_page - $from_page + 1;
21    
22          my $ppm = "tmp-$dpi";  my $margin_px   = 3;
23          my $tmp = "$ppm-000001.ppm";  my $back_color = Imager::Color->new(255, 255, 240);
24    
25          print "rendering pdf $pdf in $dpi dpi to $tmp\n";  my $tiles_path = 'tiles/basic';
26    
27          system "pdftoppm $limit -r $dpi -aa yes -aaVector yes $pdf $ppm";  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          die "can't render" unless -f $tmp;                  my $p_img = Imager->new;
75                    $p_img->read(file=>$tmp) or die "Can't load $tmp: ", $p_img->errstr;
76    
77          my $img = Imager->new;                  $x_size += $p_img->getwidth() + $margin_px;
78          $img->read(file=>$tmp) or die "Can't load $tmp: ", $img->errstr;                  my $h = $p_img->getheight();
79                    $y_size = $h if $h > $y_size;
80    
81          my $x_size = $img->getwidth();                  push @page_imgs, $p_img;
82          my $y_size = $img->getheight();          }
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          print "loaded $tmp $x_size*$y_size pixels\n";          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 );          my $tiles_x = int( $x_size / 256 );
115          my $tiles_y = int( $y_size / 256 );          my $tiles_y = int( $y_size / 256 );
116    
117          print "creating in $tiles_x*$tiles_y tiles...\n";          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 ) {          for my $y ( 0 .. $tiles_y ) {
123                  for my $x ( 0 .. $tiles_x ) {                  for my $x ( 0 .. $tiles_x ) {
124    
125                          my $size = {                          my $size = {
126                                  left    => $x * 256,                                  left    => $x * 256,
127                                  bottom  => $y_size - $y * 256,                                  bottom  => $full_y - $y * 256,
128                                  width   => $x == $tiles_x ? $x_size % 256 : 256,                                  width   => 256,
129                                  height  => $y == $tiles_y ? $y_size % 256 : 256,                                  height  => 256,
130                          };                          };
131    
132                          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 );
133    
134                          if ( ( $x == $tiles_x ) || ( $y == $tiles_y ) ) {                          my $tx = $x + $tx_offset;
135                                  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;  
                         }  
136    
137                          # emulate TileCache disk layout                          # emulate TileCache disk layout
138                          my $path = sprintf("tiles/basic/%02d/%03d/%03d/%03d/%03d/%03d/%03d.png",                          my $path = sprintf("%s/%02d/%03d/%03d/%03d/%03d/%03d/%03d.png",
139                                    $tiles_path,
140                                  $zoom - 1,      # starts with 0                                  $zoom - 1,      # starts with 0
141                                  int( $x / 1000000 ),                                  int( $tx / 1000000 ),
142                                  int( $x / 1000 ) % 1000,                                  int( $tx / 1000 ) % 1000,
143                                  $x % 1000,                                  $tx % 1000,
144                                  int( $y / 1000000 ),                                  int( $ty / 1000000 ),
145                                  int( $y / 1000 ) % 1000,                                  int( $ty / 1000 ) % 1000,
146                                  $y % 1000                                  $ty % 1000
147                          );                          );
148    
149                          my $dir = $path;                          my $dir = $path;
# Line 83  foreach my $zoom ( 1 .. 12 ) { Line 154  foreach my $zoom ( 1 .. 12 ) {
154    
155                          undef $tile;                          undef $tile;
156    
157                          print "# $x*$y -> $path\n";                          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    

Legend:
Removed from v.17  
changed lines
  Added in v.23

  ViewVC Help
Powered by ViewVC 1.1.26