Debian Codegraph @

editor/pnmquantall

raw
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 # pnmquantall
28 ##############################################################################
29 #
30 # HISTORY:
31 #
32 # This was in the original 1989 Pbmplus as a C shell program. In Netpbm 9.13
33 # (April 2001), it was converted to Bash. (Actually, it was thought to be
34 # Bourne shell, but it used arrays). In Netpbm 10.58 (March 2012), it was
35 # converted to Perl for better portability.
36 #
37 # The 2012 Perl conversion also changed the name from Ppmquantall to
38 # Pnmquantall. It had already handled non-PPM input files for many years.
39 #
40 # The original program was more complex: Because in those days Pnmcolormap
41 # and Pnmremap did not exist, Ppmquantall concatenated all the input images
42 # together and ran Ppmquant (later Pnmquant) on the combination. It then
43 # split the combination image apart to make one output image per input image.
44 # Today, Pnmquant is just a combination of Pnmcolormap and Pnmremap, and
45 # we are able to use them better separately in Ppmquantall: We still make
46 # the combination image, but use it only to compute the colormap with
47 # Pnmcolormap. We then apply that colormap separately to each input image
48 # to produce an output image.
49 #
50 # Bryan Henderson wrote the current version from scratch in March 2012
51 # and contributed it to the public domain.
52 #
53 ##############################################################################
54
55 use strict;
56 use warnings;
57 use English;
58 use Fcntl; # gets open flags
59 use File::Copy;
60 use IO::Handle;
61
62 my $TRUE=1; my $FALSE = 0;
63
64
65
66 sub pm_message($) {
67 STDERR->print("pnmquantall: $_[0]\n");
68 }
69
70
71 sub pm_error($) {
72
73 pm_message($_[0]);
74
75 exit(1);
76 }
77
78
79 sub doVersionHack($) {
80 my ($argvR) = @_;
81
82 my $arg1 = $argvR->[0];
83
84 if (defined($arg1) && (($arg1 eq "--version") || ($arg1 eq "-version"))) {
85 my $termStatus = system('pnmcolormap', '--version');
86 exit($termStatus == 0 ? 0 : 1);
87 }
88 }
89
90
91
92 sub parseArgs($$$$) {
93 my ($argvR, $extR, $newColorCtR, $fileNamesR) = @_;
94
95 my @argv = @{$argvR};
96
97 my $firstArgPos;
98
99 if (@argv > 0 && $argv[0] eq "-ext") {
100 if (@argv < 2) {
101 pm_message("-ext requires a value");
102 exit(100);
103 } else {
104 $$extR = $argv[1];
105 $firstArgPos = 2;
106 }
107 } else {
108 $$extR = "";
109 $firstArgPos = 0;
110 }
111
112 if (@argv < $firstArgPos + 2) {
113 pm_message("Not enough arguments. You need at least the number " .
114 "of colors and one file name");
115 exit(100);
116 }
117
118 $$newColorCtR = $argv[$firstArgPos];
119
120 @{$fileNamesR} = @argv[$firstArgPos + 1 .. @argv-1];
121 }
122
123
124
125 sub tempFile($) {
126
127 my ($suffix) = @_;
128
129 # We trust Perl's File::Temp to do a better job of creating the temp
130 # file, but it doesn't exist before Perl 5.6.1.
131
132 if (eval { require File::Temp; 1 }) {
133 return File::Temp::tempfile("pnmquant_XXXX",
134 SUFFIX=>$suffix,
135 DIR=>File::Spec->tmpdir(),
136 UNLINK=>$TRUE);
137 } else {
138 my $fileName;
139 local *file; # For some inexplicable reason, must be local, not my
140 my $i;
141 $i = 0;
142 do {
143 $fileName = File::Spec->tmpdir() . "/pnmquant_" . $i++ . $suffix;
144 } until sysopen(*file, $fileName, O_RDWR|O_CREAT|O_EXCL);
145
146 return(*file, $fileName);
147 }
148 }
149
150
151
152 sub copyFile($$) {
153 my ($fromFd, $toFd) = @_;
154 #-----------------------------------------------------------------------------
155 # Copy everything from open file $from Fd to open file $toFd.
156 #
157 # (File::Copy does the same thing, but I don't know if we can rely
158 # on it being available).
159 #-----------------------------------------------------------------------------
160 for (my $eof = $FALSE; !$eof; ) {
161 my $bytesReadCt = sysread($fromFd, my $fileContent, 4096);
162 if (!defined($bytesReadCt)) {
163 pm_error("Failed to read from file descriptor, errno = $ERRNO");
164 } elsif ($bytesReadCt == 0) {
165 $eof = $TRUE;
166 } else {
167 for (my $totalWrittenCt = 0; $totalWrittenCt < $bytesReadCt; ) {
168 my $bytesWrittenCt = syswrite($toFd, $fileContent);
169 if (!defined($bytesWrittenCt)) {
170 pm_error("Failed to write to file descriptor, " .
171 "errno=$ERRNO");
172 } elsif ($bytesWrittenCt == 0) {
173 pm_error("Failed to Write to file descriptor");
174 }
175 $totalWrittenCt += $bytesWrittenCt;
176 }
177 }
178 }
179 }
180
181
182
183 sub makeColorMap($$$$) {
184 my ($fileNamesR, $newColorCt, $colorMapFileName, $errorR) = @_;
185
186 my $pamcatPid = open(my $pamcatOut, '-|', "pamcat",
187 "-topbottom", "-white", "-jleft",
188 @{$fileNamesR}) or
189 pm_error("Failed to start 'pamcat' process");
190
191 # Set Standard Output to the colormap file because the 'pnmcolormap'
192 # process will write to Standard Output. But save and later restore the
193 # current Standard Output.
194
195 open(OLDOUT, ">&STDOUT");
196 select(OLDOUT); # avoids Perl bug where it says we never use OLDOUT
197 open(STDOUT, '>', $colorMapFileName) or
198 pm_error("Failed to open output file $colorMapFileName");
199
200 my $pnmcolormapPid = open(my $pnmcolormapIn, '|-',
201 'pnmcolormap', $newColorCt) or
202 pm_error("Failed to start 'pnmcolormap' process");
203
204 copyFile($pamcatOut, $pnmcolormapIn);
205
206 close($pnmcolormapIn);
207
208 my $pamcatWaitRc = waitpid($pamcatPid, 0);
209 my $pamcatTermStat = $?;
210
211 if ($pamcatTermStat != 0) {
212 my $fileCt = scalar(@{$fileNamesR});
213 $$errorR = "'pamcat' of $fileCt files failed.";
214 }
215
216 # For some reason that appears to defy Perl documentation, someone reaps
217 # the 'pnmcolormap' process so we cannot wait for it and get its
218 # termination status now.
219
220 close(STDOUT);
221 open(STDOUT, ">&OLDOUT");
222 }
223
224
225
226 sub remapFiles($$$$) {
227 my ($fileNamesR, $colorMapFileName, $ext, $errorR) = @_;
228
229 my ($outputFh, $outputFileName) = tempFile(".pnm");
230 if (!defined($outputFh)) {
231 $$errorR = "Unable to create temporary file. Errno=$ERRNO";
232 } else {
233 for (my $i = 0; $i < @{$fileNamesR} && !$$errorR; ++$i) {
234 my $inFileName = $fileNamesR->[$i];
235
236 my $pnmremapCmd =
237 "pnmremap '$inFileName' -mapfile=$colorMapFileName " .
238 ">$outputFileName";
239
240 my $pnmremapTermStatus = system($pnmremapCmd);
241
242 if ($pnmremapTermStatus != 0) {
243 $$errorR =
244 "Shell command to quantize '$inFileName' failed: " .
245 "'$pnmremapCmd'";
246 } else {
247 my $newFileName = $inFileName . $ext;
248
249 unlink($newFileName);
250 File::Copy::move($outputFileName, $newFileName)
251 or $$errorR = "Rename to '$newFileName' failed.";
252 }
253 }
254 unlink($outputFileName); # In case something failed
255 }
256 }
257
258
259
260 ###############################################################################
261 # MAINLINE
262 ###############################################################################
263
264 my $progError;
265
266 doVersionHack(\@ARGV);
267
268 parseArgs(\@ARGV, \my $ext, \my $newColorCt, \my @fileNames);
269
270 # We write to pipes to child processes and don't want to die just because
271 # the child process terminates early:
272 $SIG{PIPE} = 'IGNORE';
273
274 my ($colorMapFh, $colorMapFileName) = tempFile(".pnm");
275 if (!defined($colorMapFh)) {
276 $progError = "Unable to create temporary file. Errno=$ERRNO";
277 }
278
279 if (!$progError) {
280 makeColorMap(\@fileNames, $newColorCt, $colorMapFileName, \$progError);
281 }
282 if (!$progError) {
283 remapFiles(\@fileNames, $colorMapFileName, $ext, \$progError);
284 }
285
286 my $exitStatus;
287
288 if ($progError) {
289 pm_message("Failed. $progError");
290 $exitStatus = 1;
291 } else {
292 $exitStatus = 0;
293 }
294
295 unlink($colorMapFileName);
296
297 exit($exitStatus);
298
299
300