1 |
|
#!/usr/bin/perl -w |
2 |
|
# RCSid $Id$ |
3 |
|
|
4 |
+ |
use warnings; |
5 |
|
use strict; |
6 |
|
use File::Temp qw/ tempdir /; |
7 |
|
use POSIX qw/ floor /; |
8 |
|
|
9 |
< |
my $mult = 179.0; # Multiplier. Default W/sr/m2 -> cd/m2 |
10 |
< |
my $label = 'cd/m2'; # Units shown in legend |
11 |
< |
my $scale = 1000; # Top of the scale |
12 |
< |
my $decades = 0; # Default is linear mapping |
13 |
< |
my $redv = 'def_red(v)'; # Mapping function for R,G,B |
14 |
< |
my $grnv = 'def_grn(v)'; |
15 |
< |
my $bluv = 'def_blu(v)'; |
16 |
< |
my $ndivs = 8; # Number of lines in legend |
9 |
> |
my @palettes = ('def', 'spec', 'pm3d', 'hot'); |
10 |
> |
|
11 |
> |
my $mult = 179.0; # Multiplier. Default W/sr/m2 -> cd/m2 |
12 |
> |
my $label = 'cd/m2'; # Units shown in legend |
13 |
> |
my $scale = 1000; # Top of the scale |
14 |
> |
my $decades = 0; # Default is linear mapping |
15 |
> |
my $pal = 'def'; # Palette |
16 |
> |
my $redv = "${pal}_red(v)"; # Mapping functions for R,G,B |
17 |
> |
my $grnv = "${pal}_grn(v)"; |
18 |
> |
my $bluv = "${pal}_blu(v)"; |
19 |
> |
my $ndivs = 8; # Number of lines in legend |
20 |
|
my $picture = '-'; |
21 |
|
my $cpict = ''; |
22 |
< |
my $legwidth = 100; # Legend width and height |
22 |
> |
my $legwidth = 100; # Legend width and height |
23 |
|
my $legheight = 200; |
24 |
< |
my $docont = ''; # Contours |
25 |
< |
my $loff = 0; # Offset to align with values |
26 |
< |
my $doextrem = 0; # Don't mark extrema |
24 |
> |
my $docont = ''; # Contours: -cl and -cb |
25 |
> |
my $doposter = 0; # Posterization: -cp |
26 |
> |
my $loff = 0; # Offset to align with values |
27 |
> |
my $doextrem = 0; # Don't mark extrema |
28 |
|
my $needfile = 0; |
29 |
+ |
my $showpal = 0; # Show availabel colour palettes |
30 |
|
|
31 |
|
while ($#ARGV >= 0) { |
32 |
< |
# Options with qualifiers |
33 |
< |
if ("$ARGV[0]" eq '-lw') { # Legend width |
34 |
< |
$legwidth = $ARGV[1]; |
35 |
< |
shift @ARGV; |
36 |
< |
} elsif ("$ARGV[0]" eq '-lh') { # Legend height |
37 |
< |
$legheight = $ARGV[1]; |
38 |
< |
shift @ARGV; |
39 |
< |
} elsif ("$ARGV[0]" eq '-m') { # Multiplier |
40 |
< |
$mult = $ARGV[1]; |
41 |
< |
shift @ARGV; |
36 |
< |
} elsif ("$ARGV[0]" eq '-s') { # Scale |
37 |
< |
$scale = $ARGV[1]; |
38 |
< |
shift @ARGV; |
32 |
> |
$_ = shift; |
33 |
> |
# Options with qualifiers |
34 |
> |
if (m/-lw/) { # Legend width |
35 |
> |
$legwidth = shift; |
36 |
> |
} elsif (m/-lh/) { # Legend height |
37 |
> |
$legheight = shift; |
38 |
> |
} elsif (m/-m/) { # Multiplier |
39 |
> |
$mult = shift; |
40 |
> |
} elsif (m/-s/) { # Scale |
41 |
> |
$scale = shift; |
42 |
|
if ($scale =~ m/[aA].*/) { |
43 |
|
$needfile = 1; |
44 |
|
} |
45 |
< |
} elsif ("$ARGV[0]" eq '-l') { # Label |
46 |
< |
$label = $ARGV[1]; |
47 |
< |
shift @ARGV; |
48 |
< |
} elsif ("$ARGV[0]" eq '-log') { # Logarithmic mapping |
49 |
< |
$decades = $ARGV[1]; |
50 |
< |
shift @ARGV; |
51 |
< |
} elsif ("$ARGV[0]" eq '-r') { |
52 |
< |
$redv = $ARGV[1]; |
53 |
< |
shift @ARGV; |
54 |
< |
} elsif ("$ARGV[0]" eq '-g') { |
55 |
< |
$grnv = $ARGV[1]; |
56 |
< |
shift @ARGV; |
57 |
< |
} elsif ("$ARGV[0]" eq '-b') { |
58 |
< |
$bluv = $ARGV[1]; |
59 |
< |
shift @ARGV; |
60 |
< |
} elsif ("$ARGV[0]" eq '-pal') { |
61 |
< |
$redv = "$ARGV[1]_red(v)"; |
62 |
< |
$grnv = "$ARGV[1]_grn(v)"; |
63 |
< |
$bluv = "$ARGV[1]_blu(v)"; |
64 |
< |
shift @ARGV; |
65 |
< |
} elsif ("$ARGV[0]" eq '-i') { # Image for intensity mapping |
66 |
< |
$picture = $ARGV[1]; |
67 |
< |
shift @ARGV; |
68 |
< |
} elsif ("$ARGV[0]" eq '-p') { # Image for background |
69 |
< |
$cpict = $ARGV[1]; |
70 |
< |
shift @ARGV; |
71 |
< |
} elsif ("$ARGV[0]" eq '-ip' || "$ARGV[0]" eq '-pi') { |
69 |
< |
$picture = $ARGV[1]; |
70 |
< |
$cpict = $ARGV[1]; |
71 |
< |
shift @ARGV; |
72 |
< |
} elsif ("$ARGV[0]" eq '-n') { # Number of contour lines |
73 |
< |
$ndivs = $ARGV[1]; |
74 |
< |
shift @ARGV; |
45 |
> |
} elsif (m/-l$/) { # Label |
46 |
> |
$label = shift; |
47 |
> |
} elsif (m/-log/) { # Logarithmic mapping |
48 |
> |
$decades = shift; |
49 |
> |
} elsif (m/-r/) { # Custom palette functions for R,G,B |
50 |
> |
$redv = shift; |
51 |
> |
} elsif (m/-g/) { |
52 |
> |
$grnv = shift; |
53 |
> |
} elsif (m/-b/) { |
54 |
> |
$bluv = shift; |
55 |
> |
} elsif (m/-pal$/) { # Color palette |
56 |
> |
$pal = shift; |
57 |
> |
if (! grep $_ eq $pal, @palettes) { |
58 |
> |
die("invalid palette '$pal'.\n"); |
59 |
> |
} |
60 |
> |
$redv = "${pal}_red(v)"; |
61 |
> |
$grnv = "${pal}_grn(v)"; |
62 |
> |
$bluv = "${pal}_blu(v)"; |
63 |
> |
} elsif (m/-i$/) { # Image for intensity mapping |
64 |
> |
$picture = shift; |
65 |
> |
} elsif (m/-p$/) { # Image for background |
66 |
> |
$cpict = shift; |
67 |
> |
} elsif (m/-ip/ || m/-pi/) { |
68 |
> |
$picture = shift; |
69 |
> |
$cpict = $picture; |
70 |
> |
} elsif (m/-n/) { # Number of contour lines |
71 |
> |
$ndivs = shift; |
72 |
|
|
73 |
< |
# Switches |
74 |
< |
} elsif ("$ARGV[0]" eq '-cl') { # Contour lines |
73 |
> |
# Switches |
74 |
> |
} elsif (m/-cl/) { # Contour lines |
75 |
|
$docont = 'a'; |
76 |
|
$loff = 0.48; |
77 |
< |
} elsif ("$ARGV[0]" eq '-cb') { # Contour bands |
77 |
> |
} elsif (m/-cb/) { # Contour bands |
78 |
|
$docont = 'b'; |
79 |
|
$loff = 0.52; |
80 |
< |
} elsif ("$ARGV[0]" eq '-e') { |
80 |
> |
} elsif (m/-cp/) { # Posterize |
81 |
> |
$doposter = 1; |
82 |
> |
} elsif (m/-palettes/) { # Show all available palettes |
83 |
> |
$scale = 45824; # 256 * 179 |
84 |
> |
$showpal = 1; |
85 |
> |
} elsif (m/-e/) { |
86 |
|
$doextrem = 1; |
87 |
|
$needfile = 1; |
88 |
|
|
89 |
< |
# Oops! Illegal option |
89 |
> |
# Oops! Illegal option |
90 |
|
} else { |
91 |
< |
die("bad option \"$ARGV[0]\"\n"); |
91 |
> |
die("bad option \"$_\"\n"); |
92 |
|
} |
91 |
– |
shift @ARGV; |
93 |
|
} |
94 |
|
|
95 |
|
# Temporary directory. Will be removed upon successful program exit. |
96 |
|
my $td = tempdir( CLEANUP => 1 ); |
97 |
|
|
98 |
|
if ($needfile == 1 && $picture eq '-') { |
99 |
< |
# Pretend that $td/stdin.rad is the actual filename. |
99 |
> |
# Pretend that $td/stdin.rad is the actual filename. |
100 |
|
$picture = "$td/stdin.hdr"; |
101 |
|
open(FHpic, ">$picture") or |
102 |
|
die("Unable to write to file $picture\n"); |
103 |
< |
# Input is from STDIN: Capture to file. |
104 |
< |
while (<>) { |
105 |
< |
print FHpic; |
106 |
< |
} |
103 |
> |
# Input is from STDIN: Capture to file. |
104 |
> |
while (<>) { |
105 |
> |
print FHpic; |
106 |
> |
} |
107 |
|
close(FHpic); |
108 |
|
|
109 |
< |
if ($cpict eq '-') { |
110 |
< |
$cpict = "$td/stdin.hdr"; |
111 |
< |
} |
109 |
> |
if ($cpict eq '-') { |
110 |
> |
$cpict = "$td/stdin.hdr"; |
111 |
> |
} |
112 |
|
} |
113 |
|
|
114 |
|
# Find a good scale for auto mode. |
115 |
|
if ($scale =~ m/[aA].*/) { |
116 |
< |
my @histo = split(/\s/, `phisto $picture| tail -2`); |
117 |
< |
# e.g. $ phisto tests/richmond.hdr| tail -2 |
118 |
< |
# 3.91267 14 |
119 |
< |
# 3.94282 6 |
120 |
< |
my $LogLmax = $histo[0]; |
116 |
> |
my @histo = split(/\s/, `phisto $picture| tail -2`); |
117 |
> |
# e.g. $ phisto tests/richmond.hdr| tail -2 |
118 |
> |
# 3.91267 14 |
119 |
> |
# 3.94282 6 |
120 |
> |
my $LogLmax = $histo[0]; |
121 |
|
$scale = $mult / 179 * 10**$LogLmax; |
122 |
|
} |
123 |
|
|
143 |
|
spec_blu(x) = 1 - 8/3*x; |
144 |
|
|
145 |
|
pm3d_red(x) = sqrt(x) ^ gamma; |
146 |
< |
pm3d_grn(x) = x*x*x ^ gamma; |
147 |
< |
pm3d_blu(x) = clip(sin(2*PI*x)) ^ gamma; |
146 |
> |
pm3d_grn(x) = (x*x*x) ^ gamma; |
147 |
> |
pm3d_blu(x) = clip(sin(2*PI*clip(x))) ^ gamma; |
148 |
|
|
149 |
|
hot_red(x) = clip(3*x) ^ gamma; |
150 |
|
hot_grn(x) = clip(3*x - 1) ^ gamma; |
187 |
|
go = if(in,clip($grnv),ga); |
188 |
|
bo = if(in,clip($bluv),ba); |
189 |
|
EndOfPC0 |
190 |
+ |
close FHpc0; |
191 |
|
|
192 |
|
my $pc1 = "$td/pc1.cal"; |
193 |
|
open(FHpc1, ">$pc1"); |
207 |
|
ga = gi(nfiles); |
208 |
|
ba = bi(nfiles); |
209 |
|
EndOfPC1 |
210 |
+ |
close FHpc1; |
211 |
|
|
212 |
|
my $pc0args = "-f $pc0"; |
213 |
|
my $pc1args = "-f $pc1"; |
214 |
|
|
215 |
< |
# Contour lines or bands |
215 |
> |
if ($showpal == 1) { |
216 |
> |
my $pc = "pcompos -a 1"; |
217 |
> |
foreach my $pal (@palettes) { |
218 |
> |
my $fcimg = "$td/$pal.hdr"; |
219 |
> |
my $lbimg = "$td/${pal}_label.hdr"; |
220 |
> |
system "psign -cb 0 0 0 -cf 1 1 1 -h 20 $pal > $lbimg"; |
221 |
> |
|
222 |
> |
my $cmd = qq[pcomb $pc0args -e "v=x/256"]; |
223 |
> |
$cmd .= qq[ -e "ro=clip(${pal}_red(v));go=clip(${pal}_grn(v));bo=clip(${pal}_blu(v))"]; |
224 |
> |
$cmd .= qq[ -x 256 -y 30 > $fcimg]; |
225 |
> |
system "$cmd"; |
226 |
> |
$pc .= " $fcimg $lbimg"; |
227 |
> |
} |
228 |
> |
system "$pc"; |
229 |
> |
exit 0; |
230 |
> |
} |
231 |
> |
|
232 |
> |
# Contours |
233 |
|
if ($docont ne '') { |
234 |
< |
$pc0args .= " -e 'in=iscont$docont'"; |
234 |
> |
# -cl -> $docont = a |
235 |
> |
# -cb -> $docont = b |
236 |
> |
$pc0args .= qq[ -e "in=iscont$docont"]; |
237 |
> |
} elsif ($doposter == 1) { |
238 |
> |
# -cp -> $doposter = 1 |
239 |
> |
$pc0args .= qq[ -e "ro=${pal}_red(seg(v));go=${pal}_grn(seg(v));bo=${pal}_blu(seg(v))"]; |
240 |
> |
$pc0args .= q[ -e "seg(x)=(floor(v*ndivs)+.5)/ndivs"]; |
241 |
|
} |
242 |
|
|
243 |
|
if ($cpict eq '') { |
244 |
< |
$pc1args .= " -e 'ra=0;ga=0;ba=0'"; |
244 |
> |
$pc1args .= qq[ -e "ra=0;ga=0;ba=0"]; |
245 |
|
} elsif ($cpict eq $picture) { |
246 |
|
$cpict = ''; |
247 |
|
} |
248 |
|
|
249 |
|
# Logarithmic mapping |
250 |
|
if ($decades > 0) { |
251 |
< |
$pc1args .= " -e 'map(x)=if(x-10^-$decades,log10(x)/$decades+1,0)'"; |
251 |
> |
$pc1args .= qq[ -e "map(x)=if(x-10^-$decades,log10(x)/$decades+1,0)"]; |
252 |
|
} |
253 |
|
|
254 |
|
# Colours in the legend |
255 |
|
my $scolpic = "$td/scol.hdr"; |
256 |
+ |
|
257 |
|
# Labels in the legend |
258 |
|
my $slabpic = "$td/slab.hdr"; |
259 |
|
my $cmd; |
260 |
|
|
261 |
|
if (($legwidth > 20) && ($legheight > 40)) { |
262 |
< |
# Legend: Create the text labels |
262 |
> |
# Legend: Create the text labels |
263 |
|
my $sheight = floor($legheight / $ndivs + 0.5); |
264 |
|
$legheight = $sheight * $ndivs; |
265 |
|
$loff = floor($loff * $sheight + 0.5); |
266 |
|
my $text = "$label"; |
267 |
|
for (my $i=0; $i<$ndivs; $i++) { |
268 |
|
my $imap = ($ndivs - 0.5 - $i) / $ndivs; |
269 |
< |
my $value = $scale; |
269 |
> |
my $value = $scale; |
270 |
|
if ($decades > 0) { |
271 |
|
$value *= 10**(($imap - 1) * $decades); |
272 |
|
} else { |
273 |
|
$value *= $imap; |
274 |
|
} |
275 |
+ |
|
276 |
|
# Have no more than 3 decimal places |
277 |
|
$value =~ s/(\.[0-9]{3})[0-9]*/$1/; |
278 |
|
$text .= "\n$value"; |
279 |
|
} |
280 |
< |
$cmd = "echo '$text' | psign -s -.15 -cf 1 1 1 -cb 0 0 0"; |
253 |
< |
$cmd .= " -h $sheight > $slabpic"; |
254 |
< |
system $cmd; |
280 |
> |
system "psign -s -.15 -cf 1 1 1 -cb 0 0 0 -h $sheight $text > $slabpic"; |
281 |
|
|
282 |
< |
# Legend: Create the background colours |
283 |
< |
$cmd = "pcomb $pc0args -e 'v=(y+.5)/yres;vleft=v;vright=v'"; |
284 |
< |
$cmd .= " -e 'vbelow=(y-.5)/yres;vabove=(y+1.5)/yres'"; |
285 |
< |
$cmd .= " -x $legwidth -y $legheight > $scolpic"; |
282 |
> |
# Legend: Create the background colours |
283 |
> |
$cmd = qq[pcomb $pc0args]; |
284 |
> |
$cmd .= q[ -e "v=(y+.5)/yres;vleft=v;vright=v"]; |
285 |
> |
$cmd .= q[ -e "vbelow=(y-.5)/yres;vabove=(y+1.5)/yres"]; |
286 |
> |
$cmd .= qq[ -x $legwidth -y $legheight > $scolpic]; |
287 |
|
system $cmd; |
288 |
|
} else { |
289 |
< |
# Legend is too small to be legible. Don't bother doing one. |
289 |
> |
# Legend is too small to be legible. Don't bother doing one. |
290 |
|
$legwidth = 0; |
291 |
|
$legheight = 0; |
292 |
|
$loff = 0; |
293 |
< |
# Create dummy colour scale and legend labels so we don't |
294 |
< |
# need to change the final command line. |
293 |
> |
|
294 |
> |
# Create dummy colour scale and legend labels so we don't |
295 |
> |
# need to change the final command line. |
296 |
|
open(FHscolpic, ">$scolpic"); |
297 |
|
print FHscolpic "\n-Y 1 +X 1\naaa\n"; |
298 |
|
close(FHscolpic); |
303 |
|
|
304 |
|
# Legend: Invert the text labels (for dropshadow) |
305 |
|
my $slabinvpic = "$td/slabinv.hdr"; |
306 |
< |
$cmd = "pcomb -e 'lo=1-gi(1)' $slabpic > $slabinvpic"; |
306 |
> |
$cmd = qq[pcomb -e "lo=1-gi(1)" $slabpic > $slabinvpic]; |
307 |
|
system $cmd; |
308 |
|
|
309 |
|
my $loff1 = $loff - 1; |
310 |
+ |
|
311 |
|
# Command line without extrema |
312 |
< |
$cmd = "pcomb $pc0args $pc1args $picture $cpict"; |
313 |
< |
$cmd .= "| pcompos $scolpic 0 0 +t .1 $slabinvpic 2 $loff1"; |
314 |
< |
$cmd .= " -t .5 $slabpic 0 $loff - $legwidth 0"; |
312 |
> |
$cmd = qq[pcomb $pc0args $pc1args $picture $cpict]; |
313 |
> |
$cmd .= qq[ | pcompos $scolpic 0 0 +t .1 $slabinvpic 2 $loff1]; |
314 |
> |
$cmd .= qq[ -t .5 $slabpic 0 $loff - $legwidth 0]; |
315 |
|
|
316 |
|
if ($doextrem == 1) { |
317 |
< |
# Get min/max image luminance |
317 |
> |
# Get min/max image luminance |
318 |
|
my $cmd1 = 'pextrem -o ' . $picture; |
319 |
|
my $retval = `$cmd1`; |
320 |
|
# e.g. |
324 |
|
|
325 |
|
my @extrema = split(/\s/, $retval); |
326 |
|
my ($lxmin, $ymin, $rmin, $gmin, $bmin, $lxmax, $ymax, $rmax, $gmax, $bmax) = @extrema; |
327 |
< |
$lxmin += $legwidth; |
328 |
< |
$lxmax += $legwidth; |
327 |
> |
$lxmin += $legwidth; |
328 |
> |
$lxmax += $legwidth; |
329 |
|
|
330 |
< |
# Weighted average of R,G,B |
330 |
> |
# Weighted average of R,G,B |
331 |
|
my $minpos = "$lxmin $ymin"; |
332 |
|
my $minval = ($rmin * .27 + $gmin * .67 + $bmin * .06) * $mult; |
333 |
|
$minval =~ s/(\.[0-9]{3})[0-9]*/$1/; |
305 |
– |
my $maxpos = "$lxmax $ymax"; |
334 |
|
my $maxval = ($rmax * .27 + $gmax * .67 + $bmax * .06) * $mult; |
335 |
|
$maxval =~ s/(\.[0-9]{3})[0-9]*/$1/; |
336 |
|
|
337 |
< |
# Create the labels for min/max intensity |
337 |
> |
# Create the labels for min/max intensity |
338 |
|
my $minvpic = "$td/minv.hdr"; |
339 |
< |
$cmd1 = "psign -s -.15 -a 2 -h 16 $minval > $minvpic"; |
312 |
< |
system $cmd1; |
339 |
> |
system "psign -s -.15 -a 2 -h 16 $minval > $minvpic"; |
340 |
|
my $maxvpic = "$td/maxv.hdr"; |
341 |
< |
$cmd1 = "psign -s -.15 -a 2 -h 16 $maxval > $maxvpic"; |
315 |
< |
system $cmd1; |
341 |
> |
system "psign -s -.15 -a 2 -h 16 $maxval > $maxvpic"; |
342 |
|
|
343 |
< |
# Add extrema labels to command line |
344 |
< |
$cmd .= " $minvpic $minpos $maxvpic $maxpos"; |
343 |
> |
# Add extrema labels to command line |
344 |
> |
$cmd .= qq[ $minvpic $minpos $maxvpic $lxmax $ymax]; |
345 |
|
} |
346 |
|
|
347 |
|
# Process image and combine with legend |