/[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 22 by dpavlin, Mon Feb 25 00:19:40 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 $debug = 1;  my $debug = 0;
16    
17  my $from_page   = 1;  my $from_page   = 1;
18  my $to_page             = 4;  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';
26    
# Line 34  my ( $x_res, $y_res ) = split(/\s+/,$res Line 39  my ( $x_res, $y_res ) = split(/\s+/,$res
39  print "## 100 dpi resolution = $x_res*$y_res\n";  print "## 100 dpi resolution = $x_res*$y_res\n";
40  close($fh);  close($fh);
41    
42  $x_res *= ( $to_page - $from_page + 1 );  $x_res *= $total_pages;
43    $x_res += $total_pages * $margin_px;
44    
45  print "## total size = $x_res*$y_res\n";  print "## total size = $x_res*$y_res\n";
46    
47  # initial width = 2 tiles  # initial = 2*1 tiles
48  my $curr_w = 256 * 2;  my $curr_w = 256 * 2;
49    my $curr_h = 256;
50    
51  my $start_dpi = int( 100 / ( $x_res / $curr_w ) );  my $start_dpi = int( 100 / ( $x_res / $curr_w ) ) - 1;
52    
53  foreach my $zoom ( 1 .. 10 ) {  foreach my $zoom ( 1 .. 10 ) {
54    
# Line 67  foreach my $zoom ( 1 .. 10 ) { Line 74  foreach my $zoom ( 1 .. 10 ) {
74                  my $p_img = Imager->new;                  my $p_img = Imager->new;
75                  $p_img->read(file=>$tmp) or die "Can't load $tmp: ", $p_img->errstr;                  $p_img->read(file=>$tmp) or die "Can't load $tmp: ", $p_img->errstr;
76    
77                  $x_size += $p_img->getwidth();                  $x_size += $p_img->getwidth() + $margin_px;
78                  my $h = $p_img->getheight();                  my $h = $p_img->getheight();
79                  $y_size = $h if $h > $y_size;                  $y_size = $h if $h > $y_size;
80    
81                  push @page_imgs, $p_img;                  push @page_imgs, $p_img;
82          }          }
83    
84          print "loaded $from_page-$to_page of $x_size*$y_size pixels\n";          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          my $back_color = Imager::Color->new(255, 127, 127);          print "loaded $from_page-$to_page of $x_size*$y_size pixels\n";
90    
91          sub pad {          sub pad {
92                  my $s = shift;                  my $s = shift;
93                  return 256 - ( $s % 256);                  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 ) );          my ( $full_x, $full_y ) = ( $x_size + pad( $x_size ), $y_size + pad( $y_size ) );
# Line 93  foreach my $zoom ( 1 .. 10 ) { Line 104  foreach my $zoom ( 1 .. 10 ) {
104    
105          foreach my $page_img ( @page_imgs ) {          foreach my $page_img ( @page_imgs ) {
106                  $img->paste( left => $x_pos, top => $y_pos, img => $page_img );                  $img->paste( left => $x_pos, top => $y_pos, img => $page_img );
107                  $x_pos += $page_img->getwidth();                  $x_pos += $page_img->getwidth() + $margin_px;
108          }          }
109    
110          undef @page_imgs;          undef @page_imgs;
# Line 103  foreach my $zoom ( 1 .. 10 ) { Line 114  foreach my $zoom ( 1 .. 10 ) {
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            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";          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 ) {
# Line 117  foreach my $zoom ( 1 .. 10 ) { Line 131  foreach my $zoom ( 1 .. 10 ) {
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                            my $tx = $x + $tx_offset;
135                            my $ty = $y + $ty_offset;
136    
137                          # emulate TileCache disk layout                          # emulate TileCache disk layout
138                          my $path = sprintf("%s/%02d/%03d/%03d/%03d/%03d/%03d/%03d.png",                          my $path = sprintf("%s/%02d/%03d/%03d/%03d/%03d/%03d/%03d.png",
139                                  $tiles_path,                                  $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 137  foreach my $zoom ( 1 .. 10 ) { Line 154  foreach my $zoom ( 1 .. 10 ) {
154    
155                          undef $tile;                          undef $tile;
156    
157                          printf("# %2d*%-2d -> %s\n", $x, $y, $path);                          printf("# %2dx%-2d => %2dx%-2d %s\n", $x, $y, $tx, $ty, $path);
158    
159                  }                  }
160          }          }
161    
162          # break if zoom level over 300dpi          # break if zoom level over 300dpi
163          last if $dpi > 300;          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.22  
changed lines
  Added in v.23

  ViewVC Help
Powered by ViewVC 1.1.26