avoid a possible sign-extension for offsets/sizes in SGI
[imager.git] / transform.perl
CommitLineData
02d1d628
AMH
1#!perl -w
2use strict;
3use Imager;
4use Imager::Transform;
5
6my %opts;
7my @in;
8my $expr;
9my $func;
10my $out;
11my %con;
12my $numre = Imager::Expr->numre;
13while (defined(my $arg = shift)) {
14 if ($arg =~ /^-/) {
15 if ($arg eq '-g') {
16 my $work = shift or die "$0: argument missing for -g\n";
17 if ($work =~ /^(\d+)x(\d+)?$/) {
18 $opts{width} = $1;
19 $opts{height} = $2 if defined $2;
20 }
21 elsif ($work =~ /^x(\d+)$/) {
22 $opts{height} = $2;
23 }
24 else {
25 die "$0: invalid geometry supplied to -g\n";
26 }
27 }
28 elsif ($arg eq '-f') {
29 $func = shift or die "$0: argument missing for -f\n";
30 $expr = Imager::Transform->new($func)
31 or die "$0: unknown transformation $func\n";
32 }
33 elsif ($arg eq '-d') {
34 my $func = shift or die "$0: argument missing for -d\n";
35 my $desc = Imager::Transform->describe($func)
36 or die "$0: unknown transformation $func\n";
37 print $desc;
38 exit;
39 }
40 elsif ($arg eq '-l') {
41 print join("\n", sort Imager::Transform->list),"\n";
42 exit;
43 }
44 elsif ($arg eq '-o') {
45 $out = shift or die "$0: argument missing for -o\n";
46 }
47 elsif ($arg eq '--') {
48 push(@in, @ARGV);
49 last;
50 }
51 else {
52 die "$0: Unknown option $arg\n";
53 }
54 }
55 else {
56 if ($arg =~ /^([^\W\d]\w*)=($numre)$/) {
57 exists $con{$1}
58 and die "$0: constant $1 already defined\n";
59 $con{$1} = $2;
60 }
61 else {
62 push(@in, $arg);
63 }
64 }
65}
66
67$expr or usage();
68$expr->inputs <= @in
69 or die "$0: not enough input images specified for $func\n";
70
71for my $in (@in) {
72 my $im = Imager->new();
73 $im->read(file=>$in)
74 or die "Cannot read $in: ",$im->errstr,"\n";
75 $in = $im;
76}
77
78defined $out or $out = $func.".jpg";
79
80$opts{jpegquality} = 100;
81my $im = $expr->transform(\%opts, \%con, @in)
82 or die "$0: error transforming: ",$expr->errstr,"\n";
83
84$im->write(file=>$out, jpegquality=>100)
85 or die "0: Cannot write $out: ",$im->errstr,"\n";
86
87
88sub usage {
89 print <<EOS;
90Usage: $0 -f <func> <constant>=<value> ... <input-image>...
91 $0 -l
92 $0 -d <func>
93 -f <func> - function to evaluate
94 -l - list function names
95 -d <func> - describe <func>
96 -g <digits>x<digits> - dimensions of output image
97 -o <file> - output file
98EOS
99 exit
100}