|
1 #!/usr/bin/perl -w |
|
2 # RCSid $Id: falsecolor.pl,v 2.4 2011/03/23 21:57:11 greg Exp $ |
|
3 |
|
4 use strict; |
|
5 use File::Temp qw/ tempdir /; |
|
6 use POSIX qw/ floor /; |
|
7 |
|
8 my $mult = 179.0; # Multiplier. Default W/sr/m2 -> cd/m2 |
|
9 my $label = 'cd/m2'; # Units shown in legend |
|
10 my $scale = 1000; # Top of the scale |
|
11 my $decades = 0; # Default is linear mapping |
|
12 my $redv = 'def_red(v)'; # Mapping function for R,G,B |
|
13 my $grnv = 'def_grn(v)'; |
|
14 my $bluv = 'def_blu(v)'; |
|
15 my $ndivs = 8; # Number of lines in legend |
|
16 my $picture = '-'; |
|
17 my $cpict = ''; |
|
18 my $legwidth = 100; # Legend width and height |
|
19 my $legheight = 200; |
|
20 my $docont = ''; # Contours |
|
21 my $loff = 0; # Offset to align with values |
|
22 my $doextrem = 0; # Don't mark extrema |
|
23 my $needfile = 0; |
|
24 |
|
25 while ($#ARGV >= 0) { |
|
26 # Options with qualifiers |
|
27 if ("$ARGV[0]" eq '-lw') { # Legend width |
|
28 $legwidth = $ARGV[1]; |
|
29 shift @ARGV; |
|
30 } elsif ("$ARGV[0]" eq '-lh') { # Legend height |
|
31 $legheight = $ARGV[1]; |
|
32 shift @ARGV; |
|
33 } elsif ("$ARGV[0]" eq '-m') { # Multiplier |
|
34 $mult = $ARGV[1]; |
|
35 shift @ARGV; |
|
36 } elsif ("$ARGV[0]" eq '-s') { # Scale |
|
37 $scale = $ARGV[1]; |
|
38 shift @ARGV; |
|
39 if ($scale =~ m/[aA].*/) { |
|
40 $needfile = 1; |
|
41 } |
|
42 } elsif ("$ARGV[0]" eq '-l') { # Label |
|
43 $label = $ARGV[1]; |
|
44 shift @ARGV; |
|
45 } elsif ("$ARGV[0]" eq '-log') { # Logarithmic mapping |
|
46 $decades = $ARGV[1]; |
|
47 shift @ARGV; |
|
48 } elsif ("$ARGV[0]" eq '-r') { |
|
49 $redv = $ARGV[1]; |
|
50 shift @ARGV; |
|
51 } elsif ("$ARGV[0]" eq '-g') { |
|
52 $grnv = $ARGV[1]; |
|
53 shift @ARGV; |
|
54 } elsif ("$ARGV[0]" eq '-b') { |
|
55 $bluv = $ARGV[1]; |
|
56 shift @ARGV; |
|
57 } elsif ("$ARGV[0]" eq '-pal') { |
|
58 $redv = "$ARGV[1]_red(v)"; |
|
59 $grnv = "$ARGV[1]_grn(v)"; |
|
60 $bluv = "$ARGV[1]_blu(v)"; |
|
61 shift @ARGV; |
|
62 } elsif ("$ARGV[0]" eq '-i') { # Image for intensity mapping |
|
63 $picture = $ARGV[1]; |
|
64 shift @ARGV; |
|
65 } elsif ("$ARGV[0]" eq '-p') { # Image for background |
|
66 $cpict = $ARGV[1]; |
|
67 shift @ARGV; |
|
68 } 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; |
|
75 |
|
76 # Switches |
|
77 } elsif ("$ARGV[0]" eq '-cl') { # Contour lines |
|
78 $docont = 'a'; |
|
79 $loff = 0.48; |
|
80 } elsif ("$ARGV[0]" eq '-cb') { # Contour bands |
|
81 $docont = 'b'; |
|
82 $loff = 0.52; |
|
83 } elsif ("$ARGV[0]" eq '-e') { |
|
84 $doextrem = 1; |
|
85 $needfile = 1; |
|
86 |
|
87 # Oops! Illegal option |
|
88 } else { |
|
89 die("bad option \"$ARGV[0]\"\n"); |
|
90 } |
|
91 shift @ARGV; |
|
92 } |
|
93 |
|
94 # Temporary directory. Will be removed upon successful program exit. |
|
95 my $td = tempdir( CLEANUP => 1 ); |
|
96 |
|
97 if ($needfile == 1 && $picture eq '-') { |
|
98 # Pretend that $td/stdin.rad is the actual filename. |
|
99 $picture = "$td/stdin.hdr"; |
|
100 open(FHpic, ">$picture") or |
|
101 die("Unable to write to file $picture\n"); |
|
102 # Input is from STDIN: Capture to file. |
|
103 while (<>) { |
|
104 print FHpic; |
|
105 } |
|
106 close(FHpic); |
|
107 |
|
108 if ($cpict eq '-') { |
|
109 $cpict = "$td/stdin.hdr"; |
|
110 } |
|
111 } |
|
112 |
|
113 # Find a good scale for auto mode. |
|
114 if ($scale =~ m/[aA].*/) { |
|
115 my @histo = split(/\s/, `phisto $picture| tail -2`); |
|
116 # e.g. $ phisto tests/richmond.hdr| tail -2 |
|
117 # 3.91267 14 |
|
118 # 3.94282 6 |
|
119 my $LogLmax = $histo[0]; |
|
120 $scale = $mult / 179 * 10**$LogLmax; |
|
121 } |
|
122 |
|
123 my $pc0 = "$td/pc0.cal"; |
|
124 open(FHpc0, ">$pc0"); |
|
125 print FHpc0 <<EndOfPC0; |
|
126 PI : 3.14159265358979323846 ; |
|
127 scale : $scale ; |
|
128 mult : $mult ; |
|
129 ndivs : $ndivs ; |
|
130 gamma : 2.2; |
|
131 |
|
132 or(a,b) : if(a,a,b); |
|
133 EPS : 1e-7; |
|
134 neq(a,b) : if(a-b-EPS,1,b-a-EPS); |
|
135 btwn(a,x,b) : if(a-x,-1,b-x); |
|
136 clip(x) : if(x-1,1,if(x,x,0)); |
|
137 frac(x) : x - floor(x); |
|
138 boundary(a,b) : neq(floor(ndivs*a+.5),floor(ndivs*b+.5)); |
|
139 |
|
140 spec_red(x) = 1.6*x - .6; |
|
141 spec_grn(x) = if(x-.375, 1.6-1.6*x, 8/3*x); |
|
142 spec_blu(x) = 1 - 8/3*x; |
|
143 |
|
144 pm3d_red(x) = sqrt(x) ^ gamma; |
|
145 pm3d_grn(x) = x*x*x ^ gamma; |
|
146 pm3d_blu(x) = clip(sin(2*PI*x)) ^ gamma; |
|
147 |
|
148 hot_red(x) = clip(3*x) ^ gamma; |
|
149 hot_grn(x) = clip(3*x - 1) ^ gamma; |
|
150 hot_blu(x) = clip(3*x - 2) ^ gamma; |
|
151 |
|
152 interp_arr2(i,x,f):(i+1-x)*f(i)+(x-i)*f(i+1); |
|
153 interp_arr(x,f):if(x-1,if(f(0)-x,interp_arr2(floor(x),x,f),f(f(0))),f(1)); |
|
154 |
|
155 def_redp(i):select(i,0.18848,0.05468174, |
|
156 0.00103547,8.311144e-08,7.449763e-06,0.0004390987,0.001367254, |
|
157 0.003076,0.01376382,0.06170773,0.1739422,0.2881156,0.3299725, |
|
158 0.3552663,0.372552,0.3921184,0.4363976,0.6102754,0.7757267, |
|
159 0.9087369,1,1,0.9863); |
|
160 def_red(x):interp_arr(x/0.0454545+1,def_redp); |
|
161 |
|
162 def_grnp(i):select(i,0.0009766,2.35501e-05, |
|
163 0.0008966244,0.0264977,0.1256843,0.2865799,0.4247083,0.4739468, |
|
164 0.4402732,0.3671876,0.2629843,0.1725325,0.1206819,0.07316644, |
|
165 0.03761026,0.01612362,0.004773749,6.830967e-06,0.00803605, |
|
166 0.1008085,0.3106831,0.6447838,0.9707); |
|
167 def_grn(x):interp_arr(x/0.0454545+1,def_grnp); |
|
168 |
|
169 def_blup(i):select(i,0.2666,0.3638662,0.4770437, |
|
170 0.5131397,0.5363797,0.5193677,0.4085123,0.1702815,0.05314236, |
|
171 0.05194055,0.08564082,0.09881395,0.08324373,0.06072902, |
|
172 0.0391076,0.02315354,0.01284458,0.005184709,0.001691774, |
|
173 2.432735e-05,1.212949e-05,0.006659406,0.02539); |
|
174 def_blu(x):interp_arr(x/0.0454545+1,def_blup); |
|
175 |
|
176 isconta = if(btwn(0,v,1),or(boundary(vleft,vright),boundary(vabove,vbelow)),-1); |
|
177 iscontb = if(btwn(0,v,1),btwn(.4,frac(ndivs*v),.6),-1); |
|
178 |
|
179 ra = 0; |
|
180 ga = 0; |
|
181 ba = 0; |
|
182 |
|
183 in = 1; |
|
184 |
|
185 ro = if(in,clip($redv),ra); |
|
186 go = if(in,clip($grnv),ga); |
|
187 bo = if(in,clip($bluv),ba); |
|
188 EndOfPC0 |
|
189 |
|
190 my $pc1 = "$td/pc1.cal"; |
|
191 open(FHpc1, ">$pc1"); |
|
192 print FHpc1 <<EndOfPC1; |
|
193 norm : mult/scale/le(1); |
|
194 |
|
195 v = map(li(1)*norm); |
|
196 |
|
197 vleft = map(li(1,-1,0)*norm); |
|
198 vright = map(li(1,1,0)*norm); |
|
199 vabove = map(li(1,0,1)*norm); |
|
200 vbelow = map(li(1,0,-1)*norm); |
|
201 |
|
202 map(x) = x; |
|
203 |
|
204 ra = ri(nfiles); |
|
205 ga = gi(nfiles); |
|
206 ba = bi(nfiles); |
|
207 EndOfPC1 |
|
208 |
|
209 my $pc0args = "-f $pc0"; |
|
210 my $pc1args = "-f $pc1"; |
|
211 |
|
212 # Contour lines or bands |
|
213 if ($docont ne '') { |
|
214 $pc0args .= " -e 'in=iscont$docont'"; |
|
215 } |
|
216 |
|
217 if ($cpict eq '') { |
|
218 $pc1args .= " -e 'ra=0;ga=0;ba=0'"; |
|
219 } elsif ($cpict eq $picture) { |
|
220 $cpict = ''; |
|
221 } |
|
222 |
|
223 # Logarithmic mapping |
|
224 if ($decades > 0) { |
|
225 $pc1args .= " -e 'map(x)=if(x-10^-$decades,log10(x)/$decades+1,0)'"; |
|
226 } |
|
227 |
|
228 # Colours in the legend |
|
229 my $scolpic = "$td/scol.hdr"; |
|
230 # Labels in the legend |
|
231 my $slabpic = "$td/slab.hdr"; |
|
232 my $cmd; |
|
233 |
|
234 if (($legwidth > 20) && ($legheight > 40)) { |
|
235 # Legend: Create the text labels |
|
236 my $sheight = floor($legheight / $ndivs + 0.5); |
|
237 $legheight = $sheight * $ndivs; |
|
238 $loff = floor($loff * $sheight + 0.5); |
|
239 my $text = "$label"; |
|
240 for (my $i=0; $i<$ndivs; $i++) { |
|
241 my $imap = ($ndivs - 0.5 - $i) / $ndivs; |
|
242 my $value = $scale; |
|
243 if ($decades > 0) { |
|
244 $value *= 10**(($imap - 1) * $decades); |
|
245 } else { |
|
246 $value *= $imap; |
|
247 } |
|
248 # Have no more than 3 decimal places |
|
249 $value =~ s/(\.[0-9]{3})[0-9]*/$1/; |
|
250 $text .= "\n$value"; |
|
251 } |
|
252 $cmd = "echo '$text' | psign -s -.15 -cf 1 1 1 -cb 0 0 0"; |
|
253 $cmd .= " -h $sheight > $slabpic"; |
|
254 system $cmd; |
|
255 |
|
256 # Legend: Create the background colours |
|
257 $cmd = "pcomb $pc0args -e 'v=(y+.5)/yres;vleft=v;vright=v'"; |
|
258 $cmd .= " -e 'vbelow=(y-.5)/yres;vabove=(y+1.5)/yres'"; |
|
259 $cmd .= " -x $legwidth -y $legheight > $scolpic"; |
|
260 system $cmd; |
|
261 } else { |
|
262 # Legend is too small to be legible. Don't bother doing one. |
|
263 $legwidth = 0; |
|
264 $legheight = 0; |
|
265 $loff = 0; |
|
266 # Create dummy colour scale and legend labels so we don't |
|
267 # need to change the final command line. |
|
268 open(FHscolpic, ">$scolpic"); |
|
269 print FHscolpic "\n-Y 1 +X 1\naaa\n"; |
|
270 close(FHscolpic); |
|
271 open(FHslabpic, ">$slabpic"); |
|
272 print FHslabpic "\n-Y 1 +X 1\naaa\n"; |
|
273 close(FHslabpic); |
|
274 } |
|
275 |
|
276 # Legend: Invert the text labels (for dropshadow) |
|
277 my $slabinvpic = "$td/slabinv.hdr"; |
|
278 $cmd = "pcomb -e 'lo=1-gi(1)' $slabpic > $slabinvpic"; |
|
279 system $cmd; |
|
280 |
|
281 my $loff1 = $loff - 1; |
|
282 # Command line without extrema |
|
283 $cmd = "pcomb $pc0args $pc1args $picture $cpict"; |
|
284 $cmd .= "| pcompos $scolpic 0 0 +t .1 $slabinvpic 2 $loff1"; |
|
285 $cmd .= " -t .5 $slabpic 0 $loff - $legwidth 0"; |
|
286 |
|
287 if ($doextrem == 1) { |
|
288 # Get min/max image luminance |
|
289 my $cmd1 = 'pextrem -o ' . $picture; |
|
290 my $retval = `$cmd1`; |
|
291 # e.g. |
|
292 # x y r g b |
|
293 # 193 207 3.070068e-02 3.118896e-02 1.995850e-02 |
|
294 # 211 202 1.292969e+00 1.308594e+00 1.300781e+00 |
|
295 |
|
296 my @extrema = split(/\s/, $retval); |
|
297 my ($lxmin, $ymin, $rmin, $gmin, $bmin, $lxmax, $ymax, $rmax, $gmax, $bmax) = @extrema; |
|
298 $lxmin += $legwidth; |
|
299 $lxmax += $legwidth; |
|
300 |
|
301 # Weighted average of R,G,B |
|
302 my $minpos = "$lxmin $ymin"; |
|
303 my $minval = ($rmin * .27 + $gmin * .67 + $bmin * .06) * $mult; |
|
304 $minval =~ s/(\.[0-9]{3})[0-9]*/$1/; |
|
305 my $maxpos = "$lxmax $ymax"; |
|
306 my $maxval = ($rmax * .27 + $gmax * .67 + $bmax * .06) * $mult; |
|
307 $maxval =~ s/(\.[0-9]{3})[0-9]*/$1/; |
|
308 |
|
309 # Create the labels for min/max intensity |
|
310 my $minvpic = "$td/minv.hdr"; |
|
311 $cmd1 = "psign -s -.15 -a 2 -h 16 $minval > $minvpic"; |
|
312 system $cmd1; |
|
313 my $maxvpic = "$td/maxv.hdr"; |
|
314 $cmd1 = "psign -s -.15 -a 2 -h 16 $maxval > $maxvpic"; |
|
315 system $cmd1; |
|
316 |
|
317 # Add extrema labels to command line |
|
318 $cmd .= " $minvpic $minpos $maxvpic $maxpos"; |
|
319 } |
|
320 |
|
321 # Process image and combine with legend |
|
322 system "$cmd"; |
|
323 |
|
324 #EOF |