| 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 |
############################################################################## |
| 27 |
# pnmquant |
| 28 |
############################################################################## |
| 29 |
# By Bryan Henderson, San Jose CA; December 2001. |
| 30 |
# |
| 31 |
# Contributed to the public domain by its author. |
| 32 |
############################################################################## |
| 33 |
|
| 34 |
use strict; |
| 35 |
use English; |
| 36 |
use Getopt::Long; |
| 37 |
use File::Spec; |
| 38 |
#use Fcntl ":seek"; # not available in Perl 5.00503 |
| 39 |
use Fcntl; # gets open flags |
| 40 |
use IO::Handle; |
| 41 |
|
| 42 |
my ($TRUE, $FALSE) = (1,0); |
| 43 |
|
| 44 |
sub pm_message($) { |
| 45 |
STDERR->print("pnmquant: $_[0]\n"); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
my ($SEEK_SET, $SEEK_CUR, $SEEK_END) = (0, 1, 2); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
sub doVersionHack($) { |
| 55 |
my ($argvR) = @_; |
| 56 |
|
| 57 |
my $arg1 = $argvR->[0]; |
| 58 |
|
| 59 |
if (defined($arg1) && (($arg1 eq "--version") || ($arg1 eq "-version"))) { |
| 60 |
my $termStatus = system('pnmcolormap', '--version'); |
| 61 |
exit($termStatus == 0 ? 0 : 1); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
sub tempFile($) { |
| 68 |
|
| 69 |
my ($suffix) = @_; |
| 70 |
|
| 71 |
# We trust Perl's File::Temp to do a better job of creating the temp |
| 72 |
# file, but it doesn't exist before Perl 5.6.1. |
| 73 |
|
| 74 |
if (eval { require File::Temp; 1 }) { |
| 75 |
return File::Temp::tempfile("pnmquant_XXXX", |
| 76 |
SUFFIX=>$suffix, |
| 77 |
DIR=>File::Spec->tmpdir(), |
| 78 |
UNLINK=>$TRUE); |
| 79 |
} else { |
| 80 |
my $fileName; |
| 81 |
local *file; # For some inexplicable reason, must be local, not my |
| 82 |
my $i; |
| 83 |
$i = 0; |
| 84 |
do { |
| 85 |
$fileName = File::Spec->tmpdir() . "/pnmquant_" . $i++ . $suffix; |
| 86 |
} until sysopen(*file, $fileName, O_RDWR|O_CREAT|O_EXCL); |
| 87 |
|
| 88 |
return(*file, $fileName); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
sub parseCommandLine(@) { |
| 95 |
|
| 96 |
local @ARGV = @_; # GetOptions takes input from @ARGV only |
| 97 |
|
| 98 |
my %cmdline; |
| 99 |
|
| 100 |
my $optsAreValid = GetOptions(\%cmdline, |
| 101 |
"center", |
| 102 |
"meancolor", |
| 103 |
"meanpixel", |
| 104 |
"spreadbrightness", |
| 105 |
"spreadluminosity", |
| 106 |
"floyd|fs!", |
| 107 |
"norandom", |
| 108 |
"randomseed=i", |
| 109 |
"quiet", |
| 110 |
"plain"); |
| 111 |
|
| 112 |
if (!$optsAreValid) { |
| 113 |
pm_message("Invalid option syntax"); |
| 114 |
exit(1); |
| 115 |
} |
| 116 |
if (@ARGV > 2) { |
| 117 |
pm_message("This program takes at most 2 arguments. You specified " . |
| 118 |
scalar(@ARGV)); |
| 119 |
exit(1); |
| 120 |
} |
| 121 |
elsif (@ARGV < 1) { |
| 122 |
pm_message("You must specify the number of colors as an argument."); |
| 123 |
exit(1); |
| 124 |
} |
| 125 |
my $infile; |
| 126 |
$cmdline{ncolors} = $ARGV[0]; |
| 127 |
|
| 128 |
if (!($cmdline{ncolors} =~ m{ ^[[:digit:]]+$ }x ) || |
| 129 |
$cmdline{ncolors} == 0) { |
| 130 |
pm_message("Number of colors argument '$cmdline{ncolors}' " . |
| 131 |
"is not a positive integer."); |
| 132 |
exit(1); |
| 133 |
} |
| 134 |
|
| 135 |
if (@ARGV > 1) { |
| 136 |
$cmdline{infile} = $ARGV[1]; |
| 137 |
} else { |
| 138 |
$cmdline{infile} = "-"; |
| 139 |
} |
| 140 |
|
| 141 |
return(\%cmdline); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
sub setAutoflush($) { |
| 147 |
my ($fh) = @_; |
| 148 |
|
| 149 |
# Better would be $fh->autoflush() (with use IO:Handle), but older Perl |
| 150 |
# doesn't have it. |
| 151 |
|
| 152 |
my $oldFh = select($fh); |
| 153 |
$OUTPUT_AUTOFLUSH = $TRUE; |
| 154 |
select ($oldFh); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
sub openSeekableAsStdin($) { |
| 160 |
my ($infile) = @_; |
| 161 |
|
| 162 |
# Open the input file $infile and connect it to Standard Input |
| 163 |
# (assuming Standard Input is now open as the Perl file handle STDIN). |
| 164 |
# If $infile is "-", that means just leave Standard Input alone. But if |
| 165 |
# Standard Input is not seekable, copy it to a temporary regular |
| 166 |
# file and return a file handle for that. I.e. the file handle we |
| 167 |
# return is guaranteed to be seekable. |
| 168 |
|
| 169 |
# Note: this all works only because STDIN is already set up on file |
| 170 |
# descriptor 0. Otherwise, open(STDIN, ...) would just create a brand |
| 171 |
# new file handle named STDIN on some arbitrary file descriptor. |
| 172 |
|
| 173 |
if ($infile eq "-") { |
| 174 |
my $seekWorked = sysseek(STDIN, 0, $SEEK_SET); |
| 175 |
if ($seekWorked) { |
| 176 |
# STDIN is already as we need it. |
| 177 |
} else { |
| 178 |
# It isn't seekable, so we must copy it to a temporary regular file |
| 179 |
# and return that as the input file. |
| 180 |
|
| 181 |
my ($inFh, $inFilename) = tempFile(".pnm"); |
| 182 |
if (!defined($inFh)) { |
| 183 |
die("Unable to create temporary file. Errno=$ERRNO"); |
| 184 |
} |
| 185 |
unlink($inFilename) or |
| 186 |
die("Unable to unlink temporary file. Errno=$ERRNO"); |
| 187 |
|
| 188 |
setAutoflush($inFh); |
| 189 |
|
| 190 |
while (<STDIN>) { |
| 191 |
print($inFh $_); |
| 192 |
} |
| 193 |
sysseek($inFh, 0, $SEEK_SET) |
| 194 |
or die("Seek of temporary input file failed! " . |
| 195 |
"Errno = $ERRNO"); |
| 196 |
*INFH = *$inFh; # Because open() rejects '<&$inFh' |
| 197 |
open(STDIN, "<&INFH"); |
| 198 |
tell(INFH); # Avoids bogus "INFH is not referenced" warning |
| 199 |
} |
| 200 |
} else { |
| 201 |
open(STDIN, "<$infile") |
| 202 |
or die("Unable to open input file '$infile'. Errno=$ERRNO"); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
sub makeColormap($$$$$$$) { |
| 209 |
|
| 210 |
my ($ncolors, $opt_center, $opt_meanpixel, $opt_meancolor, |
| 211 |
$opt_spreadbrightness, $opt_spreadluminosity, $opt_quiet) = @_; |
| 212 |
|
| 213 |
# Make a colormap of $ncolors colors from the image on Standard Input. |
| 214 |
# Put it in a temporary file and return its name. |
| 215 |
|
| 216 |
my ($mapfileFh, $mapfileSpec) = tempFile(".pnm"); |
| 217 |
|
| 218 |
if (!defined($mapfileFh)) { |
| 219 |
pm_message("Unable to create temporary file for colormap. " . |
| 220 |
"errno = $ERRNO"); |
| 221 |
exit(1); |
| 222 |
} |
| 223 |
|
| 224 |
my $averageOpt; |
| 225 |
|
| 226 |
my $colorSummaryOptCt = |
| 227 |
(defined($opt_meanpixel) ? 1 : 0) + |
| 228 |
(defined($opt_meancolor) ? 1 : 0) + |
| 229 |
(defined($opt_center) ? 1 : 0); |
| 230 |
|
| 231 |
if ($colorSummaryOptCt > 1) { |
| 232 |
pm_message("You can specify only one of " . |
| 233 |
"-meanpixel, -meancolor, and -center"); |
| 234 |
exit(1); |
| 235 |
} |
| 236 |
if (defined($opt_meanpixel)) { |
| 237 |
$averageOpt = "-meanpixel"; |
| 238 |
} elsif (defined($opt_meancolor)) { |
| 239 |
$averageOpt = "-meancolor"; |
| 240 |
} else { |
| 241 |
$averageOpt = "-center"; |
| 242 |
} |
| 243 |
|
| 244 |
my $spreadOptCt = |
| 245 |
(defined($opt_spreadluminosity) ? 1 : 0) + |
| 246 |
(defined($opt_spreadbrightness) ? 1 : 0); |
| 247 |
|
| 248 |
if ($spreadOptCt > 1) { |
| 249 |
pm_message("You can specify only one of " . |
| 250 |
"-spreadluminosity and -spreadbrightness"); |
| 251 |
exit(1); |
| 252 |
} |
| 253 |
|
| 254 |
my $spreadOpt; |
| 255 |
if (defined($opt_spreadluminosity)) { |
| 256 |
$spreadOpt = "-spreadluminosity"; |
| 257 |
} else { |
| 258 |
$spreadOpt = "-spreadbrightness"; |
| 259 |
} |
| 260 |
|
| 261 |
my @options; |
| 262 |
@options = ($averageOpt, $spreadOpt); |
| 263 |
if (defined($opt_quiet)) { |
| 264 |
push(@options, '-quiet'); |
| 265 |
} |
| 266 |
|
| 267 |
open(STDOUT, ">$mapfileSpec"); |
| 268 |
|
| 269 |
my $maprc = system("pnmcolormap", $ncolors, @options); |
| 270 |
|
| 271 |
if ($maprc != 0) { |
| 272 |
pm_message("pnmcolormap failed, rc=$maprc"); |
| 273 |
exit(1); |
| 274 |
} |
| 275 |
return $mapfileSpec; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
sub remap($$$$$$) { |
| 281 |
|
| 282 |
my ($mapfileSpec, $opt_floyd, $opt_norandom, $opt_randomseed, |
| 283 |
$opt_plain, $opt_quiet) = @_; |
| 284 |
|
| 285 |
# Remap the image on Standard Input to Standard Output, using the colors |
| 286 |
# from the colormap file named $mapfileSpec. |
| 287 |
|
| 288 |
my @options; |
| 289 |
@options = (); # initial value |
| 290 |
|
| 291 |
if ($opt_floyd) { |
| 292 |
push(@options, "-floyd"); |
| 293 |
} |
| 294 |
if ($opt_norandom) { |
| 295 |
if (defined($opt_randomseed)) { |
| 296 |
pm_message("You cannot specify -randomseed with -norandom"); |
| 297 |
exit(1); |
| 298 |
} |
| 299 |
push(@options, "-norandom"); |
| 300 |
} |
| 301 |
if (defined($opt_randomseed)) { |
| 302 |
if ($opt_randomseed < 0) { |
| 303 |
pm_message("-randomseed value must not be negative. " . |
| 304 |
"You specified $opt_randomseed"); |
| 305 |
exit(10); |
| 306 |
} |
| 307 |
push(@options, "-randomseed=$opt_randomseed"); |
| 308 |
} |
| 309 |
if ($opt_plain) { |
| 310 |
push(@options, "-plain"); |
| 311 |
} |
| 312 |
if ($opt_quiet) { |
| 313 |
push(@options, "-quiet"); |
| 314 |
} |
| 315 |
|
| 316 |
my $remaprc = system("pnmremap", "-mapfile=$mapfileSpec", @options); |
| 317 |
|
| 318 |
if ($remaprc != 0) { |
| 319 |
pm_message("pnmremap failed, rc=$remaprc"); |
| 320 |
exit(1); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
############################################################################## |
| 327 |
# MAIN PROGRAM |
| 328 |
############################################################################## |
| 329 |
|
| 330 |
doVersionHack(\@ARGV); |
| 331 |
|
| 332 |
my $cmdlineR = parseCommandLine(@ARGV); |
| 333 |
|
| 334 |
openSeekableAsStdin($cmdlineR->{infile}); |
| 335 |
|
| 336 |
# Save Standard Output for our eventual output |
| 337 |
open(OLDOUT, ">&STDOUT"); |
| 338 |
select(OLDOUT); # avoids Perl bug where it says we never use OLDOUT |
| 339 |
|
| 340 |
|
| 341 |
my $mapfileSpec = makeColormap($cmdlineR->{ncolors}, |
| 342 |
$cmdlineR->{center}, |
| 343 |
$cmdlineR->{meanpixel}, |
| 344 |
$cmdlineR->{meancolor}, |
| 345 |
$cmdlineR->{spreadbrightness}, |
| 346 |
$cmdlineR->{spreadluminosity}, |
| 347 |
$cmdlineR->{quiet}); |
| 348 |
|
| 349 |
# Note that we use sysseek() instead of seek(), because we're positioning |
| 350 |
# the file to be read by our non-Perl child process, rather than for reading |
| 351 |
# through the Perl I/O layer. |
| 352 |
|
| 353 |
sysseek(STDIN, 0, $SEEK_SET) |
| 354 |
or die("seek back to zero on input file failed."); |
| 355 |
|
| 356 |
|
| 357 |
open(STDOUT, ">&OLDOUT"); |
| 358 |
|
| 359 |
remap($mapfileSpec, |
| 360 |
$cmdlineR->{floyd}, |
| 361 |
$cmdlineR->{norandom}, |
| 362 |
$cmdlineR->{randomseed}, |
| 363 |
$cmdlineR->{plain}, |
| 364 |
$cmdlineR->{quiet}); |
| 365 |
|
| 366 |
unlink($mapfileSpec) or |
| 367 |
die("Unable to unlink map file. Errno=$ERRNO"); |