| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
############################################################################## |
| 4 |
# This is essentially a Perl program. We exec the Perl interpreter specifying |
| 5 |
# this same file as the Perl program and use the -x option to cause the Perl |
| 6 |
# interpreter to skip down to the Perl code. The reason we do this instead of |
| 7 |
# just making /usr/bin/perl the script interpreter (instead of /bin/sh) is |
| 8 |
# that the user may have multiple Perl interpreters and the one he wants to |
| 9 |
# use is properly located in the PATH. The user's choice of Perl interpreter |
| 10 |
# may be crucial, such as when the user also has a PERL5LIB environment |
| 11 |
# variable and it selects modules that work with only a certain main |
| 12 |
# interpreter program. |
| 13 |
# |
| 14 |
# An alternative some people use is to have /usr/bin/env as the script |
| 15 |
# interpreter. We don't do that because we think the existence and |
| 16 |
# compatibility of /bin/sh is more reliable. |
| 17 |
# |
| 18 |
# Note that we aren't concerned about efficiency because the user who needs |
| 19 |
# high efficiency can use directly the programs that this program invokes. |
| 20 |
# |
| 21 |
############################################################################## |
| 22 |
|
| 23 |
exec perl -w -x -S -- "$0" "$@" |
| 24 |
|
| 25 |
#!/usr/bin/perl |
| 26 |
use strict; |
| 27 |
use Getopt::Long; |
| 28 |
use File::Temp; |
| 29 |
use IO::Handle; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
sub pm_message($) { |
| 34 |
STDERR->print("ppmrainbow: $_[0]\n"); |
| 35 |
} |
| 36 |
|
| 37 |
sub pm_error($) { |
| 38 |
pm_message($_[0]); |
| 39 |
exit(1); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
my ($FALSE, $TRUE) = (0,1); |
| 44 |
|
| 45 |
(my $myname = $0) =~ s#\A.*/##; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
sub doVersionHack($) { |
| 50 |
my ($argvR) = @_; |
| 51 |
|
| 52 |
my $arg1 = $argvR->[0]; |
| 53 |
|
| 54 |
if (defined($arg1) && (($arg1 eq "--version") || ($arg1 eq "-version"))) { |
| 55 |
my $termStatus = system('pgmramp', '--version'); |
| 56 |
exit($termStatus == 0 ? 0 : 1); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
############################################################################## |
| 63 |
# |
| 64 |
# MAINLINE |
| 65 |
# |
| 66 |
############################################################################## |
| 67 |
|
| 68 |
doVersionHack(\@ARGV); |
| 69 |
|
| 70 |
my ($Twid, $Thgt, $tmpdir, $repeat, $verbose); |
| 71 |
|
| 72 |
# set defaults |
| 73 |
$Twid = 600; |
| 74 |
$Thgt = 8; |
| 75 |
$tmpdir = $ENV{"TMPDIR"} || "/tmp"; |
| 76 |
$repeat = $TRUE; |
| 77 |
$verbose = $FALSE; |
| 78 |
|
| 79 |
GetOptions("width=i" => \$Twid, |
| 80 |
"height=i" => \$Thgt, |
| 81 |
"tmpdir=s" => \$tmpdir, |
| 82 |
"repeat!" => \$repeat, |
| 83 |
"verbose!" => \$verbose); |
| 84 |
|
| 85 |
if ($Twid < 1 || $Thgt < 1) { |
| 86 |
pm_error("invalid width and/or height"); |
| 87 |
} |
| 88 |
my $verboseCommand = $verbose ? "set -x;" : ""; |
| 89 |
|
| 90 |
if (@ARGV < 1) { |
| 91 |
pm_error("You must specify at least one color as an argument"); |
| 92 |
} elsif (@ARGV < 2 && ! $repeat) { |
| 93 |
pm_error("With the -norepeat option, you must specify at least two colors " . |
| 94 |
"as arguments."); |
| 95 |
} |
| 96 |
|
| 97 |
my @colorlist; |
| 98 |
|
| 99 |
@colorlist = @ARGV; |
| 100 |
if ($repeat) { |
| 101 |
push @colorlist, $ARGV[0]; |
| 102 |
} |
| 103 |
|
| 104 |
my $ourtmp = File::Temp::tempdir("$tmpdir/ppmrainbowXXXX", UNLINK=>1); |
| 105 |
|
| 106 |
my $widthRemaining; |
| 107 |
my $n; |
| 108 |
my @outlist; |
| 109 |
|
| 110 |
$n = 0; |
| 111 |
$widthRemaining = $Twid; |
| 112 |
@outlist = (); |
| 113 |
|
| 114 |
while (@colorlist >= 2) { |
| 115 |
my $outfile = sprintf("%s/file.%03u.ppm", $ourtmp, $n); |
| 116 |
push(@outlist, $outfile); |
| 117 |
|
| 118 |
my $w = int(($widthRemaining-1)/(@colorlist-1))+1; |
| 119 |
my $rc = system("$verboseCommand pgmramp -lr $w $Thgt | " . |
| 120 |
"pgmtoppm \"$colorlist[0]-$colorlist[1]\" >$outfile"); |
| 121 |
if ($rc != 0) { |
| 122 |
pm_error("pgmramp|pgmtoppm pipe failed."); |
| 123 |
} |
| 124 |
$widthRemaining -= $w; |
| 125 |
$n++; |
| 126 |
shift @colorlist; |
| 127 |
} |
| 128 |
|
| 129 |
my $termStat = |
| 130 |
system("$verboseCommand pamcat -leftright @outlist"); |
| 131 |
if ($termStat != 0) { |
| 132 |
exit 1; |
| 133 |
} |
| 134 |
|
| 135 |
exit 0; |
| 136 |
|
| 137 |
END { |
| 138 |
if (@outlist) { |
| 139 |
unlink(@outlist); |
| 140 |
} |
| 141 |
if (defined($ourtmp)) { |
| 142 |
rmdir($ourtmp); |
| 143 |
} |
| 144 |
} |