#!/usr/bin/perl use strict; use warnings; use Imager; use Imager::Fill; use Getopt::Long; use File::Basename; GetOptions( 'file=s' => \my $file, 'maxwidth=i' => \my $maxwidth, 'width=i' => \my $width, 'out=s', => \my $outfile, ); $maxwidth ||= 1024; unless ($outfile) { my $ext = ( fileparse( $file, '\..*' ) )[2]; ( $outfile = $file ) =~ s/$ext/-kaleid$ext/; } my $orig = Imager->new(); $orig->read( file => $file ) or die $orig->errstr(); $orig = $orig->crop( width => $width, height => $width ); # make the right uppper triangle of "d" from the left lower triangle of "p" my $flip = $orig->copy->flip( dir => "v" )->rotate( degrees => 90 ); my $polygon = Imager->new( xsize => $orig->getwidth, ysize => $orig->getheight, channels => 4 ); $polygon->box( fill => Imager::Fill->new( image => $flip, combine => 'normal' ) ); $polygon->polygon( points => [ [ 0, 0 ], [ 0, $orig->getheight ], [ $orig->getwidth, $orig->getheight ] ], color => Imager::Color->new( 0, 0, 0, 0 ), #transparent ); ## paste triangle on orignal image $orig->box( fill => Imager::Fill->new( image => $polygon, combine => 'normal' ) ); my $out = gen_kaleid( $orig, $maxwidth ); $out->write( file => $outfile ); sub gen_kaleid { my ( $input, $xmax ) = @_; my $kal = sub { my ($img) = @_; my $h_flip = $img->copy->flip( dir => "h" ); my $v_flip = $img->copy->flip( dir => "v" ); my $vh_flip = $img->copy->flip( dir => "vh" ); my $result = Imager->new( xsize => $img->getwidth * 2, ysize => $img->getheight * 2, bits => $img->bits, channels => $img->getchannels ); $result->paste( left => 0, top => 0, src => $img ); $result->paste( left => $img->getwidth, top => 0, src => $h_flip ); $result->paste( left => 0, top => $img->getheight, src => $v_flip ); $result->paste( left => $img->getwidth, top => $img->getheight, src => $vh_flip ); return $result; }; $input = $kal->($input) while ( $input && $input->getwidth < $xmax ); return $input; }