1 |
greg |
2.1 |
#!/usr/bin/perl -w |
2 |
greg |
2.6 |
# RCSid $Id: falsecolor.pl,v 2.5 2011/10/26 03:42:49 greg Exp $ |
3 |
greg |
2.1 |
|
4 |
greg |
2.5 |
use warnings; |
5 |
greg |
2.1 |
use strict; |
6 |
|
|
use File::Temp qw/ tempdir /; |
7 |
|
|
use POSIX qw/ floor /; |
8 |
|
|
|
9 |
greg |
2.5 |
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 |
greg |
2.1 |
my $picture = '-'; |
21 |
|
|
my $cpict = ''; |
22 |
greg |
2.5 |
my $legwidth = 100; # Legend width and height |
23 |
greg |
2.1 |
my $legheight = 200; |
24 |
greg |
2.6 |
my $docont = ''; # Contours: -cl and -cb |
25 |
|
|
my $doposter = 0; # Posterization: -cp |
26 |
greg |
2.5 |
my $loff = 0; # Offset to align with values |
27 |
|
|
my $doextrem = 0; # Don't mark extrema |
28 |
greg |
2.1 |
my $needfile = 0; |
29 |
greg |
2.6 |
my $showpal = 0; # Show availabel colour palettes |
30 |
greg |
2.1 |
|
31 |
|
|
while ($#ARGV >= 0) { |
32 |
greg |
2.5 |
$_ = 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 |
greg |
2.1 |
if ($scale =~ m/[aA].*/) { |
43 |
|
|
$needfile = 1; |
44 |
|
|
} |
45 |
greg |
2.5 |
} 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 |
greg |
2.1 |
|
73 |
greg |
2.5 |
# Switches |
74 |
|
|
} elsif (m/-cl/) { # Contour lines |
75 |
greg |
2.1 |
$docont = 'a'; |
76 |
greg |
2.4 |
$loff = 0.48; |
77 |
greg |
2.5 |
} elsif (m/-cb/) { # Contour bands |
78 |
greg |
2.1 |
$docont = 'b'; |
79 |
greg |
2.4 |
$loff = 0.52; |
80 |
greg |
2.6 |
} elsif (m/-cp/) { # Posterize |
81 |
|
|
$doposter = 1; |
82 |
|
|
} elsif (m/-palettes/) { # Show all available palettes |
83 |
|
|
$scale = 45824; # 256 * 179 |
84 |
|
|
$showpal = 1; |
85 |
greg |
2.5 |
} elsif (m/-e/) { |
86 |
greg |
2.1 |
$doextrem = 1; |
87 |
|
|
$needfile = 1; |
88 |
|
|
|
89 |
greg |
2.5 |
# Oops! Illegal option |
90 |
greg |
2.1 |
} else { |
91 |
greg |
2.5 |
die("bad option \"$_\"\n"); |
92 |
greg |
2.1 |
} |
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 |
greg |
2.5 |
# Pretend that $td/stdin.rad is the actual filename. |
100 |
greg |
2.3 |
$picture = "$td/stdin.hdr"; |
101 |
greg |
2.1 |
open(FHpic, ">$picture") or |
102 |
|
|
die("Unable to write to file $picture\n"); |
103 |
greg |
2.5 |
# Input is from STDIN: Capture to file. |
104 |
|
|
while (<>) { |
105 |
|
|
print FHpic; |
106 |
|
|
} |
107 |
greg |
2.1 |
close(FHpic); |
108 |
greg |
2.3 |
|
109 |
greg |
2.5 |
if ($cpict eq '-') { |
110 |
greg |
2.6 |
$cpict = "$td/stdin.hdr"; |
111 |
greg |
2.5 |
} |
112 |
greg |
2.1 |
} |
113 |
|
|
|
114 |
|
|
# Find a good scale for auto mode. |
115 |
|
|
if ($scale =~ m/[aA].*/) { |
116 |
greg |
2.5 |
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 |
greg |
2.1 |
$scale = $mult / 179 * 10**$LogLmax; |
122 |
|
|
} |
123 |
|
|
|
124 |
greg |
2.3 |
my $pc0 = "$td/pc0.cal"; |
125 |
greg |
2.1 |
open(FHpc0, ">$pc0"); |
126 |
|
|
print FHpc0 <<EndOfPC0; |
127 |
|
|
PI : 3.14159265358979323846 ; |
128 |
|
|
scale : $scale ; |
129 |
|
|
mult : $mult ; |
130 |
|
|
ndivs : $ndivs ; |
131 |
|
|
gamma : 2.2; |
132 |
|
|
|
133 |
|
|
or(a,b) : if(a,a,b); |
134 |
|
|
EPS : 1e-7; |
135 |
|
|
neq(a,b) : if(a-b-EPS,1,b-a-EPS); |
136 |
|
|
btwn(a,x,b) : if(a-x,-1,b-x); |
137 |
|
|
clip(x) : if(x-1,1,if(x,x,0)); |
138 |
|
|
frac(x) : x - floor(x); |
139 |
|
|
boundary(a,b) : neq(floor(ndivs*a+.5),floor(ndivs*b+.5)); |
140 |
|
|
|
141 |
|
|
spec_red(x) = 1.6*x - .6; |
142 |
|
|
spec_grn(x) = if(x-.375, 1.6-1.6*x, 8/3*x); |
143 |
|
|
spec_blu(x) = 1 - 8/3*x; |
144 |
|
|
|
145 |
|
|
pm3d_red(x) = sqrt(x) ^ gamma; |
146 |
greg |
2.5 |
pm3d_grn(x) = (x*x*x) ^ gamma; |
147 |
greg |
2.6 |
pm3d_blu(x) = clip(sin(2*PI*clip(x))) ^ gamma; |
148 |
greg |
2.1 |
|
149 |
|
|
hot_red(x) = clip(3*x) ^ gamma; |
150 |
|
|
hot_grn(x) = clip(3*x - 1) ^ gamma; |
151 |
|
|
hot_blu(x) = clip(3*x - 2) ^ gamma; |
152 |
|
|
|
153 |
|
|
interp_arr2(i,x,f):(i+1-x)*f(i)+(x-i)*f(i+1); |
154 |
|
|
interp_arr(x,f):if(x-1,if(f(0)-x,interp_arr2(floor(x),x,f),f(f(0))),f(1)); |
155 |
|
|
|
156 |
|
|
def_redp(i):select(i,0.18848,0.05468174, |
157 |
|
|
0.00103547,8.311144e-08,7.449763e-06,0.0004390987,0.001367254, |
158 |
|
|
0.003076,0.01376382,0.06170773,0.1739422,0.2881156,0.3299725, |
159 |
|
|
0.3552663,0.372552,0.3921184,0.4363976,0.6102754,0.7757267, |
160 |
|
|
0.9087369,1,1,0.9863); |
161 |
|
|
def_red(x):interp_arr(x/0.0454545+1,def_redp); |
162 |
|
|
|
163 |
|
|
def_grnp(i):select(i,0.0009766,2.35501e-05, |
164 |
|
|
0.0008966244,0.0264977,0.1256843,0.2865799,0.4247083,0.4739468, |
165 |
|
|
0.4402732,0.3671876,0.2629843,0.1725325,0.1206819,0.07316644, |
166 |
|
|
0.03761026,0.01612362,0.004773749,6.830967e-06,0.00803605, |
167 |
|
|
0.1008085,0.3106831,0.6447838,0.9707); |
168 |
|
|
def_grn(x):interp_arr(x/0.0454545+1,def_grnp); |
169 |
|
|
|
170 |
|
|
def_blup(i):select(i,0.2666,0.3638662,0.4770437, |
171 |
|
|
0.5131397,0.5363797,0.5193677,0.4085123,0.1702815,0.05314236, |
172 |
|
|
0.05194055,0.08564082,0.09881395,0.08324373,0.06072902, |
173 |
|
|
0.0391076,0.02315354,0.01284458,0.005184709,0.001691774, |
174 |
|
|
2.432735e-05,1.212949e-05,0.006659406,0.02539); |
175 |
|
|
def_blu(x):interp_arr(x/0.0454545+1,def_blup); |
176 |
|
|
|
177 |
|
|
isconta = if(btwn(0,v,1),or(boundary(vleft,vright),boundary(vabove,vbelow)),-1); |
178 |
|
|
iscontb = if(btwn(0,v,1),btwn(.4,frac(ndivs*v),.6),-1); |
179 |
|
|
|
180 |
|
|
ra = 0; |
181 |
|
|
ga = 0; |
182 |
|
|
ba = 0; |
183 |
|
|
|
184 |
|
|
in = 1; |
185 |
|
|
|
186 |
|
|
ro = if(in,clip($redv),ra); |
187 |
|
|
go = if(in,clip($grnv),ga); |
188 |
|
|
bo = if(in,clip($bluv),ba); |
189 |
|
|
EndOfPC0 |
190 |
greg |
2.6 |
close FHpc0; |
191 |
greg |
2.1 |
|
192 |
greg |
2.3 |
my $pc1 = "$td/pc1.cal"; |
193 |
greg |
2.1 |
open(FHpc1, ">$pc1"); |
194 |
|
|
print FHpc1 <<EndOfPC1; |
195 |
|
|
norm : mult/scale/le(1); |
196 |
|
|
|
197 |
|
|
v = map(li(1)*norm); |
198 |
|
|
|
199 |
|
|
vleft = map(li(1,-1,0)*norm); |
200 |
|
|
vright = map(li(1,1,0)*norm); |
201 |
|
|
vabove = map(li(1,0,1)*norm); |
202 |
|
|
vbelow = map(li(1,0,-1)*norm); |
203 |
|
|
|
204 |
|
|
map(x) = x; |
205 |
|
|
|
206 |
|
|
ra = ri(nfiles); |
207 |
|
|
ga = gi(nfiles); |
208 |
|
|
ba = bi(nfiles); |
209 |
|
|
EndOfPC1 |
210 |
greg |
2.6 |
close FHpc1; |
211 |
greg |
2.1 |
|
212 |
|
|
my $pc0args = "-f $pc0"; |
213 |
|
|
my $pc1args = "-f $pc1"; |
214 |
|
|
|
215 |
greg |
2.6 |
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 |
greg |
2.1 |
if ($docont ne '') { |
234 |
greg |
2.6 |
# -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 |
greg |
2.1 |
} |
242 |
|
|
|
243 |
|
|
if ($cpict eq '') { |
244 |
greg |
2.6 |
$pc1args .= qq[ -e "ra=0;ga=0;ba=0"]; |
245 |
greg |
2.1 |
} elsif ($cpict eq $picture) { |
246 |
|
|
$cpict = ''; |
247 |
|
|
} |
248 |
|
|
|
249 |
|
|
# Logarithmic mapping |
250 |
|
|
if ($decades > 0) { |
251 |
greg |
2.6 |
$pc1args .= qq[ -e "map(x)=if(x-10^-$decades,log10(x)/$decades+1,0)"]; |
252 |
greg |
2.1 |
} |
253 |
|
|
|
254 |
|
|
# Colours in the legend |
255 |
greg |
2.3 |
my $scolpic = "$td/scol.hdr"; |
256 |
greg |
2.6 |
|
257 |
greg |
2.1 |
# Labels in the legend |
258 |
greg |
2.3 |
my $slabpic = "$td/slab.hdr"; |
259 |
greg |
2.1 |
my $cmd; |
260 |
|
|
|
261 |
greg |
2.3 |
if (($legwidth > 20) && ($legheight > 40)) { |
262 |
greg |
2.5 |
# Legend: Create the text labels |
263 |
greg |
2.1 |
my $sheight = floor($legheight / $ndivs + 0.5); |
264 |
greg |
2.4 |
$legheight = $sheight * $ndivs; |
265 |
|
|
$loff = floor($loff * $sheight + 0.5); |
266 |
greg |
2.1 |
my $text = "$label"; |
267 |
greg |
2.3 |
for (my $i=0; $i<$ndivs; $i++) { |
268 |
greg |
2.1 |
my $imap = ($ndivs - 0.5 - $i) / $ndivs; |
269 |
greg |
2.5 |
my $value = $scale; |
270 |
greg |
2.1 |
if ($decades > 0) { |
271 |
greg |
2.3 |
$value *= 10**(($imap - 1) * $decades); |
272 |
greg |
2.1 |
} else { |
273 |
greg |
2.3 |
$value *= $imap; |
274 |
greg |
2.1 |
} |
275 |
greg |
2.6 |
|
276 |
greg |
2.1 |
# Have no more than 3 decimal places |
277 |
|
|
$value =~ s/(\.[0-9]{3})[0-9]*/$1/; |
278 |
|
|
$text .= "\n$value"; |
279 |
|
|
} |
280 |
greg |
2.6 |
open PSIGN, "| psign -s -.15 -cf 1 1 1 -cb 0 0 0 -h $sheight > $slabpic"; |
281 |
|
|
print PSIGN "$text"; |
282 |
|
|
close PSIGN; |
283 |
greg |
2.4 |
|
284 |
greg |
2.5 |
# Legend: Create the background colours |
285 |
greg |
2.6 |
$cmd = qq[pcomb $pc0args]; |
286 |
|
|
$cmd .= q[ -e "v=(y+.5)/yres;vleft=v;vright=v"]; |
287 |
|
|
$cmd .= q[ -e "vbelow=(y-.5)/yres;vabove=(y+1.5)/yres"]; |
288 |
|
|
$cmd .= qq[ -x $legwidth -y $legheight > $scolpic]; |
289 |
greg |
2.4 |
system $cmd; |
290 |
greg |
2.1 |
} else { |
291 |
greg |
2.5 |
# Legend is too small to be legible. Don't bother doing one. |
292 |
greg |
2.1 |
$legwidth = 0; |
293 |
|
|
$legheight = 0; |
294 |
greg |
2.4 |
$loff = 0; |
295 |
greg |
2.6 |
|
296 |
greg |
2.5 |
# Create dummy colour scale and legend labels so we don't |
297 |
|
|
# need to change the final command line. |
298 |
greg |
2.1 |
open(FHscolpic, ">$scolpic"); |
299 |
greg |
2.2 |
print FHscolpic "\n-Y 1 +X 1\naaa\n"; |
300 |
greg |
2.1 |
close(FHscolpic); |
301 |
|
|
open(FHslabpic, ">$slabpic"); |
302 |
greg |
2.2 |
print FHslabpic "\n-Y 1 +X 1\naaa\n"; |
303 |
greg |
2.1 |
close(FHslabpic); |
304 |
|
|
} |
305 |
|
|
|
306 |
greg |
2.3 |
# Legend: Invert the text labels (for dropshadow) |
307 |
|
|
my $slabinvpic = "$td/slabinv.hdr"; |
308 |
greg |
2.6 |
$cmd = qq[pcomb -e "lo=1-gi(1)" $slabpic > $slabinvpic]; |
309 |
greg |
2.3 |
system $cmd; |
310 |
|
|
|
311 |
greg |
2.1 |
my $loff1 = $loff - 1; |
312 |
greg |
2.6 |
|
313 |
greg |
2.1 |
# Command line without extrema |
314 |
greg |
2.6 |
$cmd = qq[pcomb $pc0args $pc1args $picture $cpict]; |
315 |
|
|
$cmd .= qq[ | pcompos $scolpic 0 0 +t .1 $slabinvpic 2 $loff1]; |
316 |
|
|
$cmd .= qq[ -t .5 $slabpic 0 $loff - $legwidth 0]; |
317 |
greg |
2.1 |
|
318 |
|
|
if ($doextrem == 1) { |
319 |
greg |
2.5 |
# Get min/max image luminance |
320 |
greg |
2.1 |
my $cmd1 = 'pextrem -o ' . $picture; |
321 |
|
|
my $retval = `$cmd1`; |
322 |
|
|
# e.g. |
323 |
|
|
# x y r g b |
324 |
|
|
# 193 207 3.070068e-02 3.118896e-02 1.995850e-02 |
325 |
|
|
# 211 202 1.292969e+00 1.308594e+00 1.300781e+00 |
326 |
|
|
|
327 |
|
|
my @extrema = split(/\s/, $retval); |
328 |
greg |
2.3 |
my ($lxmin, $ymin, $rmin, $gmin, $bmin, $lxmax, $ymax, $rmax, $gmax, $bmax) = @extrema; |
329 |
greg |
2.5 |
$lxmin += $legwidth; |
330 |
|
|
$lxmax += $legwidth; |
331 |
greg |
2.1 |
|
332 |
greg |
2.5 |
# Weighted average of R,G,B |
333 |
greg |
2.1 |
my $minpos = "$lxmin $ymin"; |
334 |
|
|
my $minval = ($rmin * .27 + $gmin * .67 + $bmin * .06) * $mult; |
335 |
|
|
$minval =~ s/(\.[0-9]{3})[0-9]*/$1/; |
336 |
|
|
my $maxval = ($rmax * .27 + $gmax * .67 + $bmax * .06) * $mult; |
337 |
|
|
$maxval =~ s/(\.[0-9]{3})[0-9]*/$1/; |
338 |
|
|
|
339 |
greg |
2.5 |
# Create the labels for min/max intensity |
340 |
greg |
2.3 |
my $minvpic = "$td/minv.hdr"; |
341 |
greg |
2.6 |
system "psign -s -.15 -a 2 -h 16 $minval > $minvpic"; |
342 |
greg |
2.3 |
my $maxvpic = "$td/maxv.hdr"; |
343 |
greg |
2.6 |
system "psign -s -.15 -a 2 -h 16 $maxval > $maxvpic"; |
344 |
greg |
2.1 |
|
345 |
greg |
2.5 |
# Add extrema labels to command line |
346 |
greg |
2.6 |
$cmd .= qq[ $minvpic $minpos $maxvpic $lxmax $ymax]; |
347 |
greg |
2.1 |
} |
348 |
|
|
|
349 |
|
|
# Process image and combine with legend |
350 |
|
|
system "$cmd"; |
351 |
|
|
|
352 |
|
|
#EOF |