root/lang/perl/misc/kaleid.pl

Revision 23745, 2.3 kB (checked in by sekimura, 8 months ago)

initial import

Line 
1#!/usr/bin/perl
2use strict;
3use warnings;
4
5use Imager;
6use Imager::Fill;
7use Getopt::Long;
8use File::Basename;
9
10GetOptions(
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;
18unless ($outfile) {
19    my $ext = ( fileparse( $file, '\..*' ) )[2];
20    ( $outfile = $file ) =~ s/$ext/-kaleid$ext/;
21}
22
23my $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"
28my $flip = $orig->copy->flip( dir => "v" )->rotate( degrees => 90 );
29
30my $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
50my $out = gen_kaleid( $orig, $maxwidth );
51$out->write( file => $outfile );
52
53sub 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
Note: See TracBrowser for help on using the browser.