ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/falsecolor.pl
Revision: 2.5
Committed: Wed Oct 26 03:42:49 2011 UTC (12 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R1
Changes since 2.4: +86 -92 lines
Log Message:
New features and bug fixes by Axel Jacobs

File Contents

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