File: transform.perl

package info (click to toggle)
libimager-perl 1.005%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,308 kB
  • ctags: 4,067
  • sloc: perl: 30,915; ansic: 27,680; makefile: 55; cpp: 4
file content (100 lines) | stat: -rw-r--r-- 2,269 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!perl -w
use strict;
use Imager;
use Imager::Transform;

my %opts;
my @in;
my $expr;
my $func;
my $out;
my %con;
my $numre = Imager::Expr->numre;
while (defined(my $arg = shift)) {
  if ($arg =~ /^-/) {
    if ($arg eq '-g') {
      my $work = shift or die "$0: argument missing for -g\n";
      if ($work =~ /^(\d+)x(\d+)?$/) {
	$opts{width} = $1;
	$opts{height} = $2 if defined $2;
      }
      elsif ($work =~ /^x(\d+)$/) {
	$opts{height} = $2;
      }
      else {
	die "$0: invalid geometry supplied to -g\n";
      }
    }
    elsif ($arg eq '-f') {
      $func = shift or die "$0: argument missing for -f\n";
      $expr = Imager::Transform->new($func)
	or die "$0: unknown transformation $func\n";
    }
    elsif ($arg eq '-d') {
      my $func = shift or die "$0: argument missing for -d\n";
      my $desc = Imager::Transform->describe($func)
	  or die "$0: unknown transformation $func\n";
      print $desc;
      exit;
    }
    elsif ($arg eq '-l') {
      print join("\n", sort Imager::Transform->list),"\n";
      exit;
    }
    elsif ($arg eq '-o') {
      $out = shift or die "$0: argument missing for -o\n";
    }
    elsif ($arg eq '--') {
      push(@in, @ARGV);
      last;
    }
    else {
      die "$0: Unknown option $arg\n";
    }
  }
  else {
    if ($arg =~ /^([^\W\d]\w*)=($numre)$/) {
      exists $con{$1} 
	and die "$0: constant $1 already defined\n";
      $con{$1} = $2;
    }
    else {
      push(@in, $arg);
    }
  }
}

$expr or usage();
$expr->inputs <= @in
  or die "$0: not enough input images specified for $func\n";

for my $in (@in) {
  my $im = Imager->new();
  $im->read(file=>$in)
    or die "Cannot read $in: ",$im->errstr,"\n";
  $in = $im;
}

defined $out or $out = $func.".jpg";

$opts{jpegquality} = 100;
my $im = $expr->transform(\%opts, \%con, @in)
  or die "$0: error transforming: ",$expr->errstr,"\n";

$im->write(file=>$out, jpegquality=>100)
  or die "0: Cannot write $out: ",$im->errstr,"\n";


sub usage {
  print <<EOS;
Usage: $0 -f <func> <constant>=<value> ... <input-image>...
       $0 -l
       $0 -d <func>
 -f <func>  - function to evaluate
 -l         - list function names
 -d <func>  - describe <func>
 -g <digits>x<digits> - dimensions of output image
 -o <file>  - output file
EOS
  exit
}