| 1 | #!/usr/bin/perl |
|---|
| 2 | use strict; |
|---|
| 3 | use warnings; |
|---|
| 4 | |
|---|
| 5 | use Imager; |
|---|
| 6 | use Imager::Fill; |
|---|
| 7 | use Getopt::Long; |
|---|
| 8 | use File::Basename; |
|---|
| 9 | |
|---|
| 10 | GetOptions( |
|---|
| 11 | 'file=s' => \my $file, |
|---|
| 12 | 'maxwidth=i' => \my $maxwidth, |
|---|
| 13 | 'width=i' => \my $width, |
|---|
| 14 | 'out=s', => \my $outfile, |
|---|
| 15 | ); |
|---|
| 16 | |
|---|
| 17 | $maxwidth ||= 1024; |
|---|
| 18 | unless ($outfile) { |
|---|
| 19 | my $ext = ( fileparse( $file, '\..*' ) )[2]; |
|---|
| 20 | ( $outfile = $file ) =~ s/$ext/-kaleid$ext/; |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | my $orig = Imager->new(); |
|---|
| 24 | $orig->read( file => $file ) or die $orig->errstr(); |
|---|
| 25 | $orig = $orig->crop( width => $width, height => $width ); |
|---|
| 26 | |
|---|
| 27 | # make the right uppper triangle of "d" from the left lower triangle of "p" |
|---|
| 28 | my $flip = $orig->copy->flip( dir => "v" )->rotate( degrees => 90 ); |
|---|
| 29 | |
|---|
| 30 | my $polygon = Imager->new( |
|---|
| 31 | xsize => $orig->getwidth, |
|---|
| 32 | ysize => $orig->getheight, |
|---|
| 33 | channels => 4 |
|---|
| 34 | ); |
|---|
| 35 | $polygon->box( |
|---|
| 36 | fill => Imager::Fill->new( image => $flip, combine => 'normal' ) ); |
|---|
| 37 | $polygon->polygon( |
|---|
| 38 | points => [ |
|---|
| 39 | [ 0, 0 ], |
|---|
| 40 | [ 0, $orig->getheight ], |
|---|
| 41 | [ $orig->getwidth, $orig->getheight ] |
|---|
| 42 | ], |
|---|
| 43 | color => Imager::Color->new( 0, 0, 0, 0 ), #transparent |
|---|
| 44 | ); |
|---|
| 45 | |
|---|
| 46 | ## paste triangle on orignal image |
|---|
| 47 | $orig->box( |
|---|
| 48 | fill => Imager::Fill->new( image => $polygon, combine => 'normal' ) ); |
|---|
| 49 | |
|---|
| 50 | my $out = gen_kaleid( $orig, $maxwidth ); |
|---|
| 51 | $out->write( file => $outfile ); |
|---|
| 52 | |
|---|
| 53 | sub gen_kaleid { |
|---|
| 54 | my ( $input, $xmax ) = @_; |
|---|
| 55 | my $kal = sub { |
|---|
| 56 | my ($img) = @_; |
|---|
| 57 | my $h_flip = $img->copy->flip( dir => "h" ); |
|---|
| 58 | my $v_flip = $img->copy->flip( dir => "v" ); |
|---|
| 59 | my $vh_flip = $img->copy->flip( dir => "vh" ); |
|---|
| 60 | my $result = Imager->new( |
|---|
| 61 | xsize => $img->getwidth * 2, |
|---|
| 62 | ysize => $img->getheight * 2, |
|---|
| 63 | bits => $img->bits, |
|---|
| 64 | channels => $img->getchannels |
|---|
| 65 | ); |
|---|
| 66 | $result->paste( |
|---|
| 67 | left => 0, |
|---|
| 68 | top => 0, |
|---|
| 69 | src => $img |
|---|
| 70 | ); |
|---|
| 71 | $result->paste( |
|---|
| 72 | left => $img->getwidth, |
|---|
| 73 | top => 0, |
|---|
| 74 | src => $h_flip |
|---|
| 75 | ); |
|---|
| 76 | $result->paste( |
|---|
| 77 | left => 0, |
|---|
| 78 | top => $img->getheight, |
|---|
| 79 | src => $v_flip |
|---|
| 80 | ); |
|---|
| 81 | $result->paste( |
|---|
| 82 | left => $img->getwidth, |
|---|
| 83 | top => $img->getheight, |
|---|
| 84 | src => $vh_flip |
|---|
| 85 | ); |
|---|
| 86 | return $result; |
|---|
| 87 | }; |
|---|
| 88 | $input = $kal->($input) while ( $input && $input->getwidth < $xmax ); |
|---|
| 89 | return $input; |
|---|
| 90 | } |
|---|
| 91 | |
|---|