ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/falsecolor.pl
Revision: 2.13
Committed: Mon Jan 15 22:35:34 2018 UTC (6 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R2
Changes since 2.12: +4 -3 lines
Log Message:
Fixed bug spotted by Rob G's test, triggered by bug fix in pcomb

File Contents

# User Rev Content
1 greg 2.1 #!/usr/bin/perl -w
2 greg 2.13 # RCSid $Id: falsecolor.pl,v 2.12 2017/09/25 18:48:11 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.9 my @palettes = ('def', 'spec', 'pm3d', 'hot', 'eco');
10 greg 2.5
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 greg 2.12 my $scaledigits = 3; # Number of maximum digits for numbers in legend
15 greg 2.5 my $decades = 0; # Default is linear mapping
16     my $pal = 'def'; # Palette
17     my $redv = "${pal}_red(v)"; # Mapping functions for R,G,B
18     my $grnv = "${pal}_grn(v)";
19     my $bluv = "${pal}_blu(v)";
20     my $ndivs = 8; # Number of lines in legend
21 greg 2.1 my $picture = '-';
22     my $cpict = '';
23 greg 2.5 my $legwidth = 100; # Legend width and height
24 greg 2.1 my $legheight = 200;
25 greg 2.12 my $haszero = 1; # print 0 in scale for falsecolor images only
26 greg 2.6 my $docont = ''; # Contours: -cl and -cb
27     my $doposter = 0; # Posterization: -cp
28 greg 2.5 my $doextrem = 0; # Don't mark extrema
29 greg 2.1 my $needfile = 0;
30 greg 2.6 my $showpal = 0; # Show availabel colour palettes
31 greg 2.1
32     while ($#ARGV >= 0) {
33 greg 2.5 $_ = shift;
34     # Options with qualifiers
35     if (m/-lw/) { # Legend width
36     $legwidth = shift;
37     } elsif (m/-lh/) { # Legend height
38     $legheight = shift;
39     } elsif (m/-m/) { # Multiplier
40     $mult = shift;
41 greg 2.9 } elsif (m/-spec/) {
42     die("depricated option '-spec'. Please use '-pal spec' instead.");
43 greg 2.5 } elsif (m/-s/) { # Scale
44     $scale = shift;
45 greg 2.1 if ($scale =~ m/[aA].*/) {
46     $needfile = 1;
47 greg 2.12 }
48     } elsif (m/-d/) { # Number of maximum digits for numbers in legend
49     $scaledigits = shift;
50 greg 2.5 } elsif (m/-l$/) { # Label
51     $label = shift;
52     } elsif (m/-log/) { # Logarithmic mapping
53     $decades = shift;
54     } elsif (m/-r/) { # Custom palette functions for R,G,B
55     $redv = shift;
56     } elsif (m/-g/) {
57     $grnv = shift;
58     } elsif (m/-b/) {
59     $bluv = shift;
60     } elsif (m/-pal$/) { # Color palette
61     $pal = shift;
62     if (! grep $_ eq $pal, @palettes) {
63     die("invalid palette '$pal'.\n");
64     }
65     $redv = "${pal}_red(v)";
66     $grnv = "${pal}_grn(v)";
67     $bluv = "${pal}_blu(v)";
68     } elsif (m/-i$/) { # Image for intensity mapping
69     $picture = shift;
70     } elsif (m/-p$/) { # Image for background
71     $cpict = shift;
72     } elsif (m/-ip/ || m/-pi/) {
73     $picture = shift;
74     $cpict = $picture;
75     } elsif (m/-n/) { # Number of contour lines
76     $ndivs = shift;
77 greg 2.1
78 greg 2.5 # Switches
79     } elsif (m/-cl/) { # Contour lines
80 greg 2.1 $docont = 'a';
81 greg 2.12 $haszero = 0;
82 greg 2.5 } elsif (m/-cb/) { # Contour bands
83 greg 2.1 $docont = 'b';
84 greg 2.12 $haszero = 0;
85 greg 2.6 } elsif (m/-cp/) { # Posterize
86     $doposter = 1;
87 greg 2.12 $haszero = 0;
88 greg 2.6 } elsif (m/-palettes/) { # Show all available palettes
89     $scale = 45824; # 256 * 179
90     $showpal = 1;
91 greg 2.5 } elsif (m/-e/) {
92 greg 2.1 $doextrem = 1;
93     $needfile = 1;
94 greg 2.5 # Oops! Illegal option
95 greg 2.1 } else {
96 greg 2.5 die("bad option \"$_\"\n");
97 greg 2.1 }
98     }
99    
100 greg 2.11 if (($legwidth <= 20) || ($legheight <= 40)) {
101     # Legend is too small to be legible. Don't bother doing one.
102     $legwidth = 0;
103     $legheight = 0;
104     }
105    
106 greg 2.1 # Temporary directory. Will be removed upon successful program exit.
107     my $td = tempdir( CLEANUP => 1 );
108    
109     if ($needfile == 1 && $picture eq '-') {
110 greg 2.5 # Pretend that $td/stdin.rad is the actual filename.
111 greg 2.3 $picture = "$td/stdin.hdr";
112 greg 2.1 open(FHpic, ">$picture") or
113     die("Unable to write to file $picture\n");
114 greg 2.5 # Input is from STDIN: Capture to file.
115     while (<>) {
116     print FHpic;
117     }
118 greg 2.1 close(FHpic);
119 greg 2.3
120 greg 2.5 if ($cpict eq '-') {
121 greg 2.6 $cpict = "$td/stdin.hdr";
122 greg 2.5 }
123 greg 2.1 }
124    
125     # Find a good scale for auto mode.
126     if ($scale =~ m/[aA].*/) {
127 greg 2.5 my @histo = split(/\s/, `phisto $picture| tail -2`);
128     # e.g. $ phisto tests/richmond.hdr| tail -2
129     # 3.91267 14
130     # 3.94282 6
131     my $LogLmax = $histo[0];
132 greg 2.1 $scale = $mult / 179 * 10**$LogLmax;
133 greg 2.12 }
134    
135     if ($docont ne '') {
136     # -cl -> $docont = a
137     # -cb -> $docont = b
138     my $newv = join( "(v-1/ndivs)*ndivs/(ndivs-1)", split("v", $redv) );
139     $redv = $newv;
140     $newv = join( "(v-1/ndivs)*ndivs/(ndivs-1)", split("v", $bluv) );
141     $bluv = $newv;
142     $newv = join( "(v-1/ndivs)*ndivs/(ndivs-1)", split("v", $grnv) );
143     $grnv = $newv;
144     } elsif ($doposter == 1) {
145     # -cp -> $doposter = 1
146     my $newv = join( "seg2(v)", split("v", $redv) );
147     $redv = $newv;
148     $newv = join( "seg2(v)", split("v", $bluv) );
149     $bluv = $newv;
150     $newv = join( "seg2(v)", split("v", $grnv) );
151     $grnv = $newv;
152 greg 2.1 }
153    
154 greg 2.3 my $pc0 = "$td/pc0.cal";
155 greg 2.1 open(FHpc0, ">$pc0");
156     print FHpc0 <<EndOfPC0;
157     PI : 3.14159265358979323846 ;
158     scale : $scale ;
159     mult : $mult ;
160     ndivs : $ndivs ;
161     gamma : 2.2;
162    
163     or(a,b) : if(a,a,b);
164     EPS : 1e-7;
165     neq(a,b) : if(a-b-EPS,1,b-a-EPS);
166     btwn(a,x,b) : if(a-x,-1,b-x);
167     clip(x) : if(x-1,1,if(x,x,0));
168     frac(x) : x - floor(x);
169 greg 2.12 boundary(a,b) : neq(floor(ndivs*a),floor(ndivs*b));
170 greg 2.1
171     spec_red(x) = 1.6*x - .6;
172     spec_grn(x) = if(x-.375, 1.6-1.6*x, 8/3*x);
173     spec_blu(x) = 1 - 8/3*x;
174    
175 greg 2.12 pm3d_red(x) = sqrt(clip(x)) ^ gamma;
176     pm3d_grn(x) = clip(x*x*x) ^ gamma;
177 greg 2.6 pm3d_blu(x) = clip(sin(2*PI*clip(x))) ^ gamma;
178 greg 2.1
179     hot_red(x) = clip(3*x) ^ gamma;
180     hot_grn(x) = clip(3*x - 1) ^ gamma;
181     hot_blu(x) = clip(3*x - 2) ^ gamma;
182    
183 greg 2.9 eco_red(x) = clip(2*x) ^ gamma;
184     eco_grn(x) = clip(2*(x-0.5)) ^ gamma;
185     eco_blu(x) = clip(2*(0.5-x)) ^ gamma;
186    
187 greg 2.1 interp_arr2(i,x,f):(i+1-x)*f(i)+(x-i)*f(i+1);
188     interp_arr(x,f):if(x-1,if(f(0)-x,interp_arr2(floor(x),x,f),f(f(0))),f(1));
189    
190     def_redp(i):select(i,0.18848,0.05468174,
191     0.00103547,8.311144e-08,7.449763e-06,0.0004390987,0.001367254,
192     0.003076,0.01376382,0.06170773,0.1739422,0.2881156,0.3299725,
193     0.3552663,0.372552,0.3921184,0.4363976,0.6102754,0.7757267,
194     0.9087369,1,1,0.9863);
195     def_red(x):interp_arr(x/0.0454545+1,def_redp);
196    
197     def_grnp(i):select(i,0.0009766,2.35501e-05,
198     0.0008966244,0.0264977,0.1256843,0.2865799,0.4247083,0.4739468,
199     0.4402732,0.3671876,0.2629843,0.1725325,0.1206819,0.07316644,
200     0.03761026,0.01612362,0.004773749,6.830967e-06,0.00803605,
201     0.1008085,0.3106831,0.6447838,0.9707);
202     def_grn(x):interp_arr(x/0.0454545+1,def_grnp);
203    
204     def_blup(i):select(i,0.2666,0.3638662,0.4770437,
205     0.5131397,0.5363797,0.5193677,0.4085123,0.1702815,0.05314236,
206     0.05194055,0.08564082,0.09881395,0.08324373,0.06072902,
207     0.0391076,0.02315354,0.01284458,0.005184709,0.001691774,
208     2.432735e-05,1.212949e-05,0.006659406,0.02539);
209     def_blu(x):interp_arr(x/0.0454545+1,def_blup);
210    
211 greg 2.12 isconta = if(btwn(1/ndivs/2,v,1+1/ndivs/2),or(boundary(vleft,vright),boundary(vabove,vbelow)),-1);
212     iscontb = if(btwn(1/ndivs/2,v,1+1/ndivs/2),-btwn(.1,frac(ndivs*v),.9),-1);
213    
214     seg(x)=(floor(v*ndivs)+.5)/ndivs;
215     seg2(x)=(seg(x)-1/ndivs)*ndivs/(ndivs-1);
216 greg 2.1
217     ra = 0;
218     ga = 0;
219     ba = 0;
220    
221     in = 1;
222    
223     ro = if(in,clip($redv),ra);
224     go = if(in,clip($grnv),ga);
225     bo = if(in,clip($bluv),ba);
226     EndOfPC0
227 greg 2.6 close FHpc0;
228 greg 2.1
229 greg 2.3 my $pc1 = "$td/pc1.cal";
230 greg 2.1 open(FHpc1, ">$pc1");
231     print FHpc1 <<EndOfPC1;
232     norm : mult/scale/le(1);
233    
234     v = map(li(1)*norm);
235    
236     vleft = map(li(1,-1,0)*norm);
237     vright = map(li(1,1,0)*norm);
238     vabove = map(li(1,0,1)*norm);
239     vbelow = map(li(1,0,-1)*norm);
240    
241     map(x) = x;
242    
243 greg 2.12
244 greg 2.1 ra = ri(nfiles);
245     ga = gi(nfiles);
246     ba = bi(nfiles);
247     EndOfPC1
248 greg 2.6 close FHpc1;
249 greg 2.1
250     my $pc0args = "-f $pc0";
251     my $pc1args = "-f $pc1";
252    
253 greg 2.6 if ($showpal == 1) {
254     my $pc = "pcompos -a 1";
255     foreach my $pal (@palettes) {
256     my $fcimg = "$td/$pal.hdr";
257     my $lbimg = "$td/${pal}_label.hdr";
258     system "psign -cb 0 0 0 -cf 1 1 1 -h 20 $pal > $lbimg";
259    
260     my $cmd = qq[pcomb $pc0args -e "v=x/256"];
261     $cmd .= qq[ -e "ro=clip(${pal}_red(v));go=clip(${pal}_grn(v));bo=clip(${pal}_blu(v))"];
262     $cmd .= qq[ -x 256 -y 30 > $fcimg];
263     system "$cmd";
264     $pc .= " $fcimg $lbimg";
265     }
266     system "$pc";
267     exit 0;
268     }
269    
270     # Contours
271 greg 2.1 if ($docont ne '') {
272 greg 2.6 # -cl -> $docont = a
273     # -cb -> $docont = b
274     $pc0args .= qq[ -e "in=iscont$docont"];
275 greg 2.1 }
276    
277     if ($cpict eq '') {
278 greg 2.6 $pc1args .= qq[ -e "ra=0;ga=0;ba=0"];
279 greg 2.1 } elsif ($cpict eq $picture) {
280     $cpict = '';
281     }
282    
283     # Logarithmic mapping
284     if ($decades > 0) {
285 greg 2.6 $pc1args .= qq[ -e "map(x)=if(x-10^-$decades,log10(x)/$decades+1,0)"];
286 greg 2.1 }
287    
288     # Colours in the legend
289 greg 2.3 my $scolpic = "$td/scol.hdr";
290 greg 2.6
291 greg 2.1 # Labels in the legend
292 greg 2.3 my $slabpic = "$td/slab.hdr";
293 greg 2.1 my $cmd;
294 greg 2.11 if ($legwidth > 0) {
295 greg 2.5 # Legend: Create the text labels
296 greg 2.12 my $sheight = floor($legheight / $ndivs );
297     my $theight = floor($legwidth/(8/1.67));
298     my $stheight = $sheight <= $theight ? $sheight : $theight;
299     my $vlegheight = $sheight * $ndivs * (1+1.5/$ndivs);
300     my $text = "$label\n";
301     my $tslabpic = "$td/slabT.hdr";
302     open PSIGN, "| psign -s -.15 -cf 1 1 1 -cb 0 0 0 -h $stheight > $tslabpic";
303     print PSIGN "$text";
304     close PSIGN;
305     my $loop = $ndivs+$haszero;
306     my $hlegheight = $sheight * ($loop) + $sheight * .5;
307     my $pcompost = qq[pcompos -b 0 0 0 =-0 $tslabpic 0 $hlegheight ];
308     for (my $i=0; $i<$loop; $i++) {
309     my $imap = ($ndivs - $i) / $ndivs;
310 greg 2.5 my $value = $scale;
311 greg 2.1 if ($decades > 0) {
312 greg 2.3 $value *= 10**(($imap - 1) * $decades);
313 greg 2.1 } else {
314 greg 2.3 $value *= $imap;
315 greg 2.1 }
316     # Have no more than 3 decimal places
317 greg 2.12 $value =~ s/(\.[0-9]{$scaledigits})[0-9]*/$1/;
318     $text = "$value\n";
319     $tslabpic = "$td/slab$i.hdr";
320     open PSIGN, "| psign -s -.15 -cf 1 1 1 -cb 0 0 0 -h $stheight > $tslabpic";
321     print PSIGN "$text";
322     close PSIGN;
323     $hlegheight = $sheight * ($loop - $i - 1) + $sheight * .5;
324     $pcompost .= qq[=-0 $tslabpic 0 $hlegheight ];
325 greg 2.1 }
326 greg 2.12 $pcompost .= qq[ > $slabpic];
327     system $pcompost;
328 greg 2.4
329 greg 2.5 # Legend: Create the background colours
330 greg 2.6 $cmd = qq[pcomb $pc0args];
331 greg 2.13 $cmd .= qq[ -x $legwidth -y $vlegheight];
332     $cmd .= qq[ -e "v=(y+.5-$sheight)/(yres/(1+1.5/$ndivs));vleft=v;vright=v"];
333 greg 2.12 $cmd .= qq[ -e "vbelow=(y-.5-$sheight)/(yres/(1+1.5/$ndivs));vabove=(y+1.5-$sheight)/(yres/(1+1.5/$ndivs))"];
334     $cmd .= qq[ -e "ra=0;ga=0;ba=0;"];
335 greg 2.13 $cmd .= qq[ > $scolpic];
336 greg 2.4 system $cmd;
337 greg 2.1 } else {
338 greg 2.5 # Create dummy colour scale and legend labels so we don't
339     # need to change the final command line.
340 greg 2.1 open(FHscolpic, ">$scolpic");
341 greg 2.2 print FHscolpic "\n-Y 1 +X 1\naaa\n";
342 greg 2.1 close(FHscolpic);
343     open(FHslabpic, ">$slabpic");
344 greg 2.2 print FHslabpic "\n-Y 1 +X 1\naaa\n";
345 greg 2.1 close(FHslabpic);
346     }
347    
348 greg 2.3 # Legend: Invert the text labels (for dropshadow)
349     my $slabinvpic = "$td/slabinv.hdr";
350 greg 2.6 $cmd = qq[pcomb -e "lo=1-gi(1)" $slabpic > $slabinvpic];
351 greg 2.3 system $cmd;
352    
353 greg 2.12
354     my $sh0 = -floor($legheight / $ndivs / 2);
355     if ($haszero < 1) {
356     $sh0 = -floor($legheight / ($ndivs)*1.5);
357     }
358 greg 2.6
359 greg 2.1 # Command line without extrema
360 greg 2.12
361     $cmd = qq[pcomb $pc0args $pc1args "$picture"];
362     $cmd .= qq[ "$cpict"] if ($cpict);
363     $cmd .= qq[ | pcompos -b 0 0 0 $scolpic 0 $sh0 +t .1 $slabinvpic 2 -1 ];
364     $cmd .= qq[ -t .5 $slabpic 0 0 - $legwidth 0];
365 greg 2.1
366     if ($doextrem == 1) {
367 greg 2.5 # Get min/max image luminance
368 greg 2.1 my $cmd1 = 'pextrem -o ' . $picture;
369     my $retval = `$cmd1`;
370     # e.g.
371     # x y r g b
372     # 193 207 3.070068e-02 3.118896e-02 1.995850e-02
373     # 211 202 1.292969e+00 1.308594e+00 1.300781e+00
374    
375     my @extrema = split(/\s/, $retval);
376 greg 2.3 my ($lxmin, $ymin, $rmin, $gmin, $bmin, $lxmax, $ymax, $rmax, $gmax, $bmax) = @extrema;
377 greg 2.5 $lxmin += $legwidth;
378     $lxmax += $legwidth;
379 greg 2.1
380 greg 2.5 # Weighted average of R,G,B
381 greg 2.1 my $minpos = "$lxmin $ymin";
382     my $minval = ($rmin * .27 + $gmin * .67 + $bmin * .06) * $mult;
383 greg 2.12 $minval =~ s/(\.[0-9]{$scaledigits})[0-9]*/$1/;
384 greg 2.1 my $maxval = ($rmax * .27 + $gmax * .67 + $bmax * .06) * $mult;
385 greg 2.12 $maxval =~ s/(\.[0-9]{$scaledigits})[0-9]*/$1/;
386 greg 2.1
387 greg 2.5 # Create the labels for min/max intensity
388 greg 2.3 my $minvpic = "$td/minv.hdr";
389 greg 2.6 system "psign -s -.15 -a 2 -h 16 $minval > $minvpic";
390 greg 2.3 my $maxvpic = "$td/maxv.hdr";
391 greg 2.6 system "psign -s -.15 -a 2 -h 16 $maxval > $maxvpic";
392 greg 2.1
393 greg 2.5 # Add extrema labels to command line
394 greg 2.6 $cmd .= qq[ $minvpic $minpos $maxvpic $lxmax $ymax];
395 greg 2.1 }
396    
397 greg 2.12
398    
399    
400 greg 2.1 # Process image and combine with legend
401     system "$cmd";
402    
403     #EOF