ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/falsecolor.pl
Revision: 2.2
Committed: Tue Jan 4 17:00:02 2011 UTC (13 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +3 -3 lines
Log Message:
Fixed problem with zero label width/height

File Contents

# User Rev Content
1 greg 2.1 #!/usr/bin/perl -w
2 greg 2.2 # RCSid $Id: falsecolor.pl,v 2.1 2010/10/05 00:59:39 greg Exp $
3 greg 2.1
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 for drop-shadow
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 = 12;
80     } elsif ("$ARGV[0]" eq '-cb') { # Contour bands
81     $docont = 'b';
82     $loff = 13;
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     $picture = $td . '/' . $picture;
99     open(FHpic, ">$picture") or
100     die("Unable to write to file $picture\n");
101     close(FHpic);
102     }
103    
104     # Find a good scale for auto mode.
105     if ($scale =~ m/[aA].*/) {
106     my $LogLmax = `phisto $picture| tail -2| sed -n '1s/[0-9]*\$//p'`;
107     $scale = $mult / 179 * 10**$LogLmax;
108     }
109    
110     my $pc0 = $td . '/pc0.cal';
111     open(FHpc0, ">$pc0");
112     print FHpc0 <<EndOfPC0;
113     PI : 3.14159265358979323846 ;
114     scale : $scale ;
115     mult : $mult ;
116     ndivs : $ndivs ;
117     gamma : 2.2;
118    
119     or(a,b) : if(a,a,b);
120     EPS : 1e-7;
121     neq(a,b) : if(a-b-EPS,1,b-a-EPS);
122     btwn(a,x,b) : if(a-x,-1,b-x);
123     clip(x) : if(x-1,1,if(x,x,0));
124     frac(x) : x - floor(x);
125     boundary(a,b) : neq(floor(ndivs*a+.5),floor(ndivs*b+.5));
126    
127     spec_red(x) = 1.6*x - .6;
128     spec_grn(x) = if(x-.375, 1.6-1.6*x, 8/3*x);
129     spec_blu(x) = 1 - 8/3*x;
130    
131     pm3d_red(x) = sqrt(x) ^ gamma;
132     pm3d_grn(x) = x*x*x ^ gamma;
133     pm3d_blu(x) = clip(sin(2*PI*x)) ^ gamma;
134    
135     hot_red(x) = clip(3*x) ^ gamma;
136     hot_grn(x) = clip(3*x - 1) ^ gamma;
137     hot_blu(x) = clip(3*x - 2) ^ gamma;
138    
139     interp_arr2(i,x,f):(i+1-x)*f(i)+(x-i)*f(i+1);
140     interp_arr(x,f):if(x-1,if(f(0)-x,interp_arr2(floor(x),x,f),f(f(0))),f(1));
141    
142     def_redp(i):select(i,0.18848,0.05468174,
143     0.00103547,8.311144e-08,7.449763e-06,0.0004390987,0.001367254,
144     0.003076,0.01376382,0.06170773,0.1739422,0.2881156,0.3299725,
145     0.3552663,0.372552,0.3921184,0.4363976,0.6102754,0.7757267,
146     0.9087369,1,1,0.9863);
147     def_red(x):interp_arr(x/0.0454545+1,def_redp);
148    
149     def_grnp(i):select(i,0.0009766,2.35501e-05,
150     0.0008966244,0.0264977,0.1256843,0.2865799,0.4247083,0.4739468,
151     0.4402732,0.3671876,0.2629843,0.1725325,0.1206819,0.07316644,
152     0.03761026,0.01612362,0.004773749,6.830967e-06,0.00803605,
153     0.1008085,0.3106831,0.6447838,0.9707);
154     def_grn(x):interp_arr(x/0.0454545+1,def_grnp);
155    
156     def_blup(i):select(i,0.2666,0.3638662,0.4770437,
157     0.5131397,0.5363797,0.5193677,0.4085123,0.1702815,0.05314236,
158     0.05194055,0.08564082,0.09881395,0.08324373,0.06072902,
159     0.0391076,0.02315354,0.01284458,0.005184709,0.001691774,
160     2.432735e-05,1.212949e-05,0.006659406,0.02539);
161     def_blu(x):interp_arr(x/0.0454545+1,def_blup);
162    
163     isconta = if(btwn(0,v,1),or(boundary(vleft,vright),boundary(vabove,vbelow)),-1);
164     iscontb = if(btwn(0,v,1),btwn(.4,frac(ndivs*v),.6),-1);
165    
166     ra = 0;
167     ga = 0;
168     ba = 0;
169    
170     in = 1;
171    
172     ro = if(in,clip($redv),ra);
173     go = if(in,clip($grnv),ga);
174     bo = if(in,clip($bluv),ba);
175     EndOfPC0
176    
177     my $pc1 = $td . '/pc1.cal';
178     open(FHpc1, ">$pc1");
179     print FHpc1 <<EndOfPC1;
180     norm : mult/scale/le(1);
181    
182     v = map(li(1)*norm);
183    
184     vleft = map(li(1,-1,0)*norm);
185     vright = map(li(1,1,0)*norm);
186     vabove = map(li(1,0,1)*norm);
187     vbelow = map(li(1,0,-1)*norm);
188    
189     map(x) = x;
190    
191     ra = ri(nfiles);
192     ga = gi(nfiles);
193     ba = bi(nfiles);
194     EndOfPC1
195    
196     my $pc0args = "-f $pc0";
197     my $pc1args = "-f $pc1";
198    
199     # Contour lines or bands
200     if ($docont ne '') {
201     $pc0args .= " -e 'in=iscont$docont'";
202     }
203    
204     if ($cpict eq '') {
205     $pc1args .= " -e 'ra=0;ga=0;ba=0'";
206     } elsif ($cpict eq $picture) {
207     $cpict = '';
208     }
209    
210     # Logarithmic mapping
211     if ($decades > 0) {
212     $pc1args .= " -e 'map(x)=if(x-10^-$decades,log10(x)/$decades+1,0)'";
213     }
214    
215     # Colours in the legend
216     my $scolpic = $td . '/scol.hdr';
217     # Labels in the legend
218     my $slabpic = $td . '/slab.hdr';
219     my $cmd;
220    
221     if ($legwidth > 20 && $legheight > 40) {
222     # Legend: Create the background colours
223     $cmd = "pcomb $pc0args -e 'v=(y+.5)/yres;vleft=v;vright=v'";
224     $cmd .= " -e 'vbelow=(y-.5)/yres;vabove=(y+1.5)/yres'";
225     $cmd .= " -x $legwidth -y $legheight > $scolpic";
226     system $cmd;
227    
228     # Legend: Create the text labels
229     my $sheight = floor($legheight / $ndivs + 0.5);
230     my $text = "$label";
231     my $value;
232     my $i;
233     for ($i=0; $i<$ndivs; $i++) {
234     my $imap = ($ndivs - 0.5 - $i) / $ndivs;
235     if ($decades > 0) {
236     $value = $scale * 10**(($imap - 1) * $decades);
237     } else {
238     $value = $scale * $imap;
239     }
240     # Have no more than 3 decimal places
241     $value =~ s/(\.[0-9]{3})[0-9]*/$1/;
242     $text .= "\n$value";
243     }
244     $cmd = "echo '$text' | psign -s -.15 -cf 1 1 1 -cb 0 0 0";
245     $cmd .= " -h $sheight > $slabpic";
246     system $cmd;
247     } else {
248     # Legend is too small to be legible. Don't bother doing one.
249     $legwidth = 0;
250     $legheight = 0;
251     open(FHscolpic, ">$scolpic");
252 greg 2.2 print FHscolpic "\n-Y 1 +X 1\naaa\n";
253 greg 2.1 close(FHscolpic);
254     open(FHslabpic, ">$slabpic");
255 greg 2.2 print FHslabpic "\n-Y 1 +X 1\naaa\n";
256 greg 2.1 close(FHslabpic);
257     }
258    
259     my $loff1 = $loff - 1;
260     # Command line without extrema
261     $cmd = "pcomb $pc0args $pc1args $picture $cpict";
262     $cmd .= "| pcompos $scolpic 0 0 +t .1";
263     $cmd .= " \"\!pcomb -e 'lo=1-gi(1)' $slabpic\" 2 $loff1";
264     $cmd .= " -t .5 $slabpic 0 $loff - $legwidth 0";
265    
266     if ($doextrem == 1) {
267     # Get min/max image luminance
268     my $cmd1 = 'pextrem -o ' . $picture;
269     my $retval = `$cmd1`;
270     # e.g.
271     # x y r g b
272     # 193 207 3.070068e-02 3.118896e-02 1.995850e-02
273     # 211 202 1.292969e+00 1.308594e+00 1.300781e+00
274    
275     my @extrema = split(/\s/, $retval);
276     my $lxmin = $extrema[0] + $legwidth;
277     my $ymin = $extrema[1];
278     my $rmin = $extrema[2];
279     my $gmin = $extrema[3];
280     my $bmin = $extrema[4];
281     my $lxmax = $extrema[5] + $legwidth;
282     my $ymax = $extrema[6];
283     my $rmax = $extrema[7];
284     my $gmax = $extrema[8];
285     my $bmax = $extrema[9];
286    
287     # Weighted average of R,G,B
288     my $minpos = "$lxmin $ymin";
289     my $minval = ($rmin * .27 + $gmin * .67 + $bmin * .06) * $mult;
290     $minval =~ s/(\.[0-9]{3})[0-9]*/$1/;
291     my $maxpos = "$lxmax $ymax";
292     my $maxval = ($rmax * .27 + $gmax * .67 + $bmax * .06) * $mult;
293     $maxval =~ s/(\.[0-9]{3})[0-9]*/$1/;
294    
295     # Create the labels for min/max intensity
296     my $minvpic = $td . '/minv.hdr';
297     $cmd1 = "psign -s -.15 -a 2 -h 16 $minval > $minvpic";
298     system $cmd1;
299     my $maxvpic = $td . '/maxv.hdr';
300     $cmd1 = "psign -s -.15 -a 2 -h 16 $maxval > $maxvpic";
301     system $cmd1;
302    
303     # Add extrema labels to command line
304     $cmd .= " $minvpic $minpos $maxvpic $maxpos";
305     }
306    
307     # Process image and combine with legend
308     system "$cmd";
309    
310     #EOF