Debian Codegraph @

editor/ppmshadow

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 # ppmshadow
28 ##############################################################################
29 #
30 # by John Walker -- http://www.fourmilab.ch/
31 # version = 1.2;
32 # --> with minor changes by Bryan Henderson to adapt to Netbpm.
33 # See above web site for the real John Walker work, named pnmshadow.
34 #
35 # Bryan Henderson later made some major style changes (use strict, etc) and
36 # eliminated most use of shells. See Netbpm HISTORY file.
37 #
38 # Pnmshadow is a brutal sledgehammer implemented in Perl which
39 # adds attractive shadows to images, as often seen in titles
40 # of World-Wide Web pages. This program does not actually
41 # *do* any image processing--it simply invokes components of
42 # Jef Poskanzer's PBMplus package (which must be present on
43 # the path when this script is run) to bludgeon the source
44 # image into a plausible result.
45 #
46 # This program is in the public domain.
47 #
48 ##############################################################################
49
50 use strict;
51 use File::Temp;
52 require 5.0;
53 # The good open() syntax, with the mode separate from the file name,
54 # came after 5.0. So did mkdir() with default mode.
55
56 my $true=1; my $false=0;
57
58
59
60 sub doVersionHack($) {
61 my ($argvR) = @_;
62
63 my $arg1 = $argvR->[0];
64
65 if (defined($arg1) && (($arg1 eq "--version") || ($arg1 eq "-version"))) {
66 my $termStatus = system('pamarith', '--version');
67 exit($termStatus == 0 ? 0 : 1);
68 }
69 }
70
71
72
73 sub imageDimensions($) {
74 my ($fileName) = @_;
75 #-----------------------------------------------------------------------------
76 # Return the dimensions of the Netpbm image in the file named $fileName.
77 #-----------------------------------------------------------------------------
78 my ($width, $height, $depth);
79 my $pamfileOutput = `pamfile $fileName`;
80 if ($pamfileOutput =~
81 m/.*\sP[BGP]M\s.*,\s*(\d*)\sby\s(\d*)\s*maxval\s(\d*)/) {
82 ($width, $height, $depth) = ($1, $2, $3);
83 } else {
84 die("Unrecognized output from 'pamfile' shell command");
85 }
86 return ($width, $height, $depth);
87 }
88
89 sub backgroundColor($) {
90 my ($fileName) = @_;
91 #-----------------------------------------------------------------------------
92 # Return the color of the background of the image in the file named $fileName.
93 #-----------------------------------------------------------------------------
94 # We call the color of the top left pixel the background color.
95
96 my $ppmhistOut = qx{pamcut 0 0 1 1 $fileName | ppmhist -noheader -float};
97
98 my ($ired, $igrn, $iblu, $lum, $count);
99
100 if ($ppmhistOut =~
101 m{\s*([01].\d+)\s*([01].\d+)\s*([01].\d+)\s*([01].\d+)\s*(\d+)}) {
102 ($ired, $igrn, $iblu, $lum, $count) = ($1, $2, $3, $4, $5);
103 } else {
104 die("Unrecognized format of output from 'ppmhist' shell command");
105 }
106 my $irgb = sprintf("rgbi:%f/%f/%f", $ired, $igrn, $iblu);
107
108 return $irgb;
109 }
110
111
112
113 sub makeConvolutionKernel($$) {
114 my ($convkernelfile, $ckern) = @_;
115
116 # Create convolution kernel file to generate shadow
117
118 open(OF, ">$convkernelfile") or die();
119 printf(OF "P2\n$ckern $ckern\n%d\n", $ckern * $ckern * 2);
120 my $a = ($ckern * $ckern) + 1;
121 my $i;
122 for ($i = 0; $i < $ckern; $i++) {
123 my $j;
124 for ($j = 0; $j < $ckern; $j++) {
125 printf(OF "%d%s", $a, ($j < ($ckern - 1)) ? " " : "\n");
126 }
127 }
128 close(OF);
129 }
130
131
132
133 ##############################################################################
134 # MAINLINE
135 ##############################################################################
136
137 doVersionHack(\@ARGV);
138
139 # Process command line options
140
141 my $ifile; # Input file name
142 my ($xoffset, $yoffset);
143
144 my $convolve = 11; # Default blur convolution kernel size
145 my $keeptemp = $false; # Don't preserve intermediate files
146 my $translucent = $false; # Default not translucent
147
148 while (@ARGV) {
149 my $arg = shift;
150
151 if ((substr($arg, 0, 1) eq '-') && (length($arg) > 1)) {
152 my $opt;
153 $opt = substr($arg, 1, 1);
154 $opt =~ tr/A-Z/a-z/;
155 if ($opt eq 'b') { # -B n -- Blur size
156 if (!defined($convolve = shift)) {
157 die("Argument missing after -b option\n");
158 }
159 if (($convolve < 11) && (($convolve & 1) == 0)) {
160 $convolve++; # Round up even kernel specification
161 }
162 } elsif ($opt eq 'k') { # -K -- Keep temporary files
163 $keeptemp = $true;
164 } elsif ($opt eq 't') { # -T -- Translucent image
165 $translucent = $true;
166 } elsif ($opt eq 'x') { # -X n -- X offset
167 if (!defined($xoffset = shift)) {
168 die("Argument missing after -x option\n");
169 }
170 if ($xoffset < 0) {
171 $xoffset = -$xoffset;
172 }
173 } elsif ($opt eq 'y') { # -Y n -- Y offset
174 if (!defined($yoffset = shift)) {
175 die("Argument missing after -x option\n");
176 }
177 if ($yoffset < 0) {
178 $yoffset = -$xoffset;
179 }
180 } else {
181 die("Unknown option '$opt'\n");
182 }
183 } else {
184 if (defined $ifile) {
185 die("Duplicate input file specification.");
186 }
187 $ifile = $arg;
188 }
189 }
190
191 # Create temporary directory
192
193 my $tmpdir = $ENV{TMPDIR} || "/tmp";
194 my $ourtmp;
195
196 if ($keeptemp) {
197 $ourtmp = "$tmpdir/ppmshadow$$";
198 mkdir($ourtmp, 0777) or
199 die("Unable to create directory for temporary files '$ourtmp");
200 } else {
201 $ourtmp = File::Temp::tempdir("$tmpdir/ppmshadowXXXX", CLEANUP=>1);
202 }
203
204 # Apply defaults for arguments not specified
205
206 if (!(defined $xoffset)) {
207 # Xoffset defaults to half the blur distance
208 $xoffset = int($convolve / 2);
209 }
210
211 if (!(defined $yoffset)) {
212 # Yoffset defaults to Xoffset, however specified
213 $yoffset = $xoffset;
214 }
215
216 # Save the Standard Output open instance so we can use the STDOUT
217 # file descriptor to pass files to our children.
218 open(OLDOUT, ">&STDOUT");
219 select(OLDOUT); # avoids Perl bug where it says we never use STDOUT
220
221 my $infile = "$ourtmp/infile.ppm";
222
223 if (defined($ifile) && $ifile ne "-") {
224 open(STDIN, "<$ifile") or die();
225 }
226 open(STDOUT, ">$infile") or die("Unable to open '$infile' as STDOUT");
227 system("ppmtoppm");
228
229 # You would think we could and should close stdin and stdout now, but if
230 # we do that, system() pipelines later on fail mysteriously. They don't
231 # seem to be able to open stdin and stdout pipes properly if stdin and
232 # stdout didn't already exist. 2002.09.07 BJH
233
234 my ($sourceImageWidth, $sourceImageHeight, $sourceImageDepth) =
235 imageDimensions($infile);
236
237 my $bgColorIrgb = backgroundColor($infile);
238
239 # Create an all-background-color image (same size as original image),
240 # named $backgroundfile.
241
242 my $backgroundfile = "$ourtmp/background.ppm";
243 system("ppmmake $bgColorIrgb $sourceImageWidth $sourceImageHeight " .
244 "-maxval $sourceImageDepth " .
245 ">$backgroundfile");
246
247 # Create mask file for background, named $bgmaskfile. It is a PBM, white
248 # wherever there is background image in the input.
249
250 my $bgmaskfile = "$ourtmp/bgmask.pbm";
251 system("ppmchange -remainder=black $bgColorIrgb white $infile | " .
252 "ppmtopgm | pgmtopbm -threshold -value=0.5 >$bgmaskfile");
253
254 my $ckern = $convolve <= 11 ? $convolve : 11;
255
256 my $convkernelfile = "$ourtmp/convkernel.pgm";
257
258 makeConvolutionKernel($convkernelfile, $ckern);
259
260 if ($translucent) {
261
262 # Convolve the input color image with the kernel
263 # to create a translucent shadow image.
264
265 system("pnmconvol -quiet $convkernelfile $infile >$ourtmp/blurred.ppm");
266 unlink("$convkernelfile") unless $keeptemp;
267 while ($ckern < $convolve) {
268 system("pnmsmooth $ourtmp/blurred.ppm >$ourtmp/convolvedx.ppm");
269 rename("$ourtmp/convolvedx.ppm", "$ourtmp/blurred.ppm");
270 ++$ckern;
271 }
272 } else {
273
274 # Convolve the positive mask with the kernel to create shadow
275
276 my $blurredblackshadfile = "$ourtmp/blurredblackshad.pgm";
277 system("pamdepth -quiet $sourceImageDepth $bgmaskfile | " .
278 "pnmconvol -quiet $convkernelfile >$blurredblackshadfile");
279 unlink($convkernelfile) unless $keeptemp;
280
281 while ($ckern < $convolve) {
282 my $smoothedfile = "$ourtmp/smoothed.pgm";
283 system("pnmsmooth $blurredblackshadfile >$smoothedfile");
284 rename($smoothedfile, $blurredblackshadfile);
285 ++$ckern;
286 }
287
288 # Multiply the shadow by the background color
289
290 system("pamarith -multiply $blurredblackshadfile $backgroundfile " .
291 ">$ourtmp/blurred.ppm");
292 unlink($blurredblackshadfile) unless $keeptemp;
293 }
294
295 # Cut shadow image down to size of our frame.
296
297 my $shadowfile = "$ourtmp/shadow.ppm";
298 {
299 my $width = $sourceImageWidth - $xoffset;
300 my $height = $sourceImageHeight - $yoffset;
301 open(STDIN, "<$ourtmp/blurred.ppm") or die();
302 open(STDOUT, ">$shadowfile") or die();
303 system("pamcut", "-left=0", "-top=0",
304 "-width=$width", "-height=$height");
305 }
306 unlink("$ourtmp/blurred.ppm") unless $keeptemp;
307
308
309 # Paste shadow onto background.
310
311 my $shadbackfile = "$ourtmp/shadback.ppm";
312 open(STDOUT, ">$shadbackfile") or die();
313 system("pnmpaste", "-replace", $shadowfile, $xoffset, $yoffset,
314 $backgroundfile);
315 unlink($shadowfile) unless $keeptemp;
316 unlink($backgroundfile) unless $keeptemp;
317
318
319 # Create composite file, send to original Standard Output.
320
321 open(STDOUT, ">&OLDOUT");
322
323 system("pamcomp -invert -alpha $bgmaskfile $infile $shadbackfile");
324 unlink($bgmaskfile) unless $keeptemp;
325 unlink($infile) unless $keeptemp;
326 unlink($shadbackfile) unless $keeptemp;
327
328 if (!$keeptemp) {
329 rmdir($ourtmp) or die ("Unable to remove temporary directory '$ourtmp'");
330 }