ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/falsecolor.pl
(Generate patch)

Comparing ray/src/px/falsecolor.pl (file contents):
Revision 2.4 by greg, Wed Mar 23 21:57:11 2011 UTC vs.
Revision 2.8 by greg, Thu Apr 26 21:03:58 2012 UTC

# Line 1 | Line 1
1   #!/usr/bin/perl -w
2   # RCSid $Id$
3  
4 + use warnings;
5   use strict;
6   use File::Temp qw/ tempdir /;
7   use POSIX qw/ floor /;
8  
9 < my $mult = 179.0;    # Multiplier. Default W/sr/m2 -> cd/m2
10 < my $label = 'cd/m2';    # Units shown in legend
11 < my $scale = 1000;    # Top of the scale
12 < my $decades = 0;    # Default is linear mapping
13 < my $redv = 'def_red(v)';    # Mapping function for R,G,B
14 < my $grnv = 'def_grn(v)';
15 < my $bluv = 'def_blu(v)';
16 < my $ndivs = 8;    # Number of lines in legend
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
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
24 > my $docont = '';               # Contours: -cl and -cb
25 > my $doposter = 0;              # Posterization: -cp
26 > my $loff = 0;                  # Offset to align with values
27 > my $doextrem = 0;              # Don't mark extrema
28   my $needfile = 0;
29 + my $showpal = 0;               # Show availabel colour palettes
30  
31   while ($#ARGV >= 0) {
32 <        # Options with qualifiers
33 <    if ("$ARGV[0]" eq '-lw') {    # Legend width
34 <        $legwidth = $ARGV[1];
35 <        shift @ARGV;
36 <    } elsif ("$ARGV[0]" eq '-lh') {    # Legend height
37 <        $legheight = $ARGV[1];
38 <        shift @ARGV;
39 <    } elsif ("$ARGV[0]" eq '-m') {    # Multiplier
40 <        $mult = $ARGV[1];
41 <        shift @ARGV;
36 <    } elsif ("$ARGV[0]" eq '-s') {    # Scale
37 <        $scale = $ARGV[1];
38 <        shift @ARGV;
32 >    $_ = 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          if ($scale =~ m/[aA].*/) {
43              $needfile = 1;
44          }
45 <    } elsif ("$ARGV[0]" eq '-l') {    # Label
46 <        $label = $ARGV[1];
47 <        shift @ARGV;
48 <    } elsif ("$ARGV[0]" eq '-log') {    # Logarithmic mapping
49 <        $decades = $ARGV[1];
50 <        shift @ARGV;
51 <    } elsif ("$ARGV[0]" eq '-r') {
52 <        $redv = $ARGV[1];
53 <        shift @ARGV;
54 <    } elsif ("$ARGV[0]" eq '-g') {
55 <        $grnv = $ARGV[1];
56 <        shift @ARGV;
57 <    } elsif ("$ARGV[0]" eq '-b') {
58 <        $bluv = $ARGV[1];
59 <        shift @ARGV;
60 <    } elsif ("$ARGV[0]" eq '-pal') {
61 <        $redv = "$ARGV[1]_red(v)";
62 <        $grnv = "$ARGV[1]_grn(v)";
63 <        $bluv = "$ARGV[1]_blu(v)";
64 <        shift @ARGV;
65 <    } elsif ("$ARGV[0]" eq '-i') {    # Image for intensity mapping
66 <        $picture = $ARGV[1];
67 <        shift @ARGV;
68 <    } elsif ("$ARGV[0]" eq '-p') {    # Image for background
69 <        $cpict = $ARGV[1];
70 <        shift @ARGV;
71 <    } 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;
45 >    } 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  
73 <        # Switches
74 <    } elsif ("$ARGV[0]" eq '-cl') {    # Contour lines
73 >    # Switches
74 >    } elsif (m/-cl/) {         # Contour lines
75          $docont = 'a';
76          $loff = 0.48;
77 <    } elsif ("$ARGV[0]" eq '-cb') {    # Contour bands
77 >    } elsif (m/-cb/) {         # Contour bands
78          $docont = 'b';
79          $loff = 0.52;
80 <    } elsif ("$ARGV[0]" eq '-e') {
80 >    } elsif (m/-cp/) {              # Posterize
81 >        $doposter = 1;
82 >    } elsif (m/-palettes/) {        # Show all available palettes
83 >        $scale   = 45824;           # 256 * 179
84 >        $showpal = 1;
85 >    } elsif (m/-e/) {
86          $doextrem = 1;
87          $needfile = 1;
88  
89 <        # Oops! Illegal option
89 >    # Oops! Illegal option
90      } else {
91 <        die("bad option \"$ARGV[0]\"\n");
91 >        die("bad option \"$_\"\n");
92      }
91    shift @ARGV;
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 <        # Pretend that $td/stdin.rad is the actual filename.
99 >    # Pretend that $td/stdin.rad is the actual filename.
100      $picture = "$td/stdin.hdr";
101      open(FHpic, ">$picture") or
102              die("Unable to write to file $picture\n");
103 <        # Input is from STDIN: Capture to file.
104 <        while (<>) {
105 <                print FHpic;
106 <        }
103 >    # Input is from STDIN: Capture to file.
104 >    while (<>) {
105 >        print FHpic;
106 >    }
107      close(FHpic);
108  
109 <        if ($cpict eq '-') {
110 <                $cpict =  "$td/stdin.hdr";
111 <        }
109 >    if ($cpict eq '-') {
110 >        $cpict = "$td/stdin.hdr";
111 >    }
112   }
113  
114   # Find a good scale for auto mode.
115   if ($scale =~ m/[aA].*/) {
116 <        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];
116 >    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      $scale = $mult / 179 * 10**$LogLmax;
122   }
123  
# Line 142 | Line 143 | 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 < pm3d_grn(x) = x*x*x ^ gamma;
147 < pm3d_blu(x) = clip(sin(2*PI*x)) ^ gamma;
146 > pm3d_grn(x) = (x*x*x) ^ gamma;
147 > pm3d_blu(x) = clip(sin(2*PI*clip(x))) ^ gamma;
148  
149   hot_red(x) = clip(3*x) ^ gamma;
150   hot_grn(x) = clip(3*x - 1) ^ gamma;
# Line 186 | Line 187 | ro = if(in,clip($redv),ra);
187   go = if(in,clip($grnv),ga);
188   bo = if(in,clip($bluv),ba);
189   EndOfPC0
190 + close FHpc0;
191  
192   my $pc1 = "$td/pc1.cal";
193   open(FHpc1, ">$pc1");
# Line 205 | Line 207 | ra = ri(nfiles);
207   ga = gi(nfiles);
208   ba = bi(nfiles);
209   EndOfPC1
210 + close FHpc1;
211  
212   my $pc0args = "-f $pc0";
213   my $pc1args = "-f $pc1";
214  
215 < # Contour lines or bands
215 > 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   if ($docont ne '') {
234 <    $pc0args .= " -e 'in=iscont$docont'";
234 >    # -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   }
242  
243   if ($cpict eq '') {
244 <    $pc1args .= " -e 'ra=0;ga=0;ba=0'";
244 >    $pc1args .= qq[ -e "ra=0;ga=0;ba=0"];
245   } elsif ($cpict eq $picture) {
246      $cpict = '';
247   }
248  
249   # Logarithmic mapping
250   if ($decades > 0) {
251 <    $pc1args .= " -e 'map(x)=if(x-10^-$decades,log10(x)/$decades+1,0)'";
251 >    $pc1args .= qq[ -e "map(x)=if(x-10^-$decades,log10(x)/$decades+1,0)"];
252   }
253  
254   # Colours in the legend
255   my $scolpic = "$td/scol.hdr";
256 +
257   # Labels in the legend
258   my $slabpic = "$td/slab.hdr";
259   my $cmd;
260  
261   if (($legwidth > 20) && ($legheight > 40)) {
262 <        # Legend: Create the text labels
262 >    # Legend: Create the text labels
263      my $sheight = floor($legheight / $ndivs + 0.5);
264      $legheight = $sheight * $ndivs;
265      $loff = floor($loff * $sheight + 0.5);
266      my $text = "$label";
267      for (my $i=0; $i<$ndivs; $i++) {
268          my $imap = ($ndivs - 0.5 - $i) / $ndivs;
269 <                my $value = $scale;
269 >        my $value = $scale;
270          if ($decades > 0) {
271              $value *= 10**(($imap - 1) * $decades);
272          } else {
273              $value *= $imap;
274          }
275 +
276          # Have no more than 3 decimal places
277          $value =~ s/(\.[0-9]{3})[0-9]*/$1/;
278          $text .= "\n$value";
279      }
280 <    $cmd = "echo '$text' | psign -s -.15 -cf 1 1 1 -cb 0 0 0";
281 <    $cmd .= " -h $sheight > $slabpic";
282 <    system $cmd;
280 >    open PSIGN, "| psign -s -.15 -cf 1 1 1 -cb 0 0 0 -h $sheight > $slabpic";
281 >    print PSIGN "$text\n";
282 >    close PSIGN;
283  
284 <        # Legend: Create the background colours
285 <    $cmd = "pcomb $pc0args -e 'v=(y+.5)/yres;vleft=v;vright=v'";
286 <    $cmd .= " -e 'vbelow=(y-.5)/yres;vabove=(y+1.5)/yres'";
287 <    $cmd .= " -x $legwidth -y $legheight > $scolpic";
284 >    # Legend: Create the background colours
285 >    $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      system $cmd;
290   } else {
291 <        # Legend is too small to be legible. Don't bother doing one.
291 >    # Legend is too small to be legible. Don't bother doing one.
292      $legwidth = 0;
293      $legheight = 0;
294      $loff = 0;
295 <        # Create dummy colour scale and legend labels so we don't
296 <        # need to change the final command line.
295 >
296 >    # Create dummy colour scale and legend labels so we don't
297 >    # need to change the final command line.
298      open(FHscolpic, ">$scolpic");
299      print FHscolpic "\n-Y 1 +X 1\naaa\n";
300      close(FHscolpic);
# Line 275 | Line 305 | if (($legwidth > 20) && ($legheight > 40)) {
305  
306   # Legend: Invert the text labels (for dropshadow)
307   my $slabinvpic = "$td/slabinv.hdr";
308 < $cmd = "pcomb -e 'lo=1-gi(1)' $slabpic > $slabinvpic";
308 > $cmd = qq[pcomb -e "lo=1-gi(1)" $slabpic > $slabinvpic];
309   system $cmd;
310  
311   my $loff1 = $loff - 1;
312 +
313   # Command line without extrema
314 < $cmd = "pcomb $pc0args $pc1args $picture $cpict";
315 < $cmd .= "| pcompos $scolpic 0 0 +t .1 $slabinvpic 2 $loff1";
316 < $cmd .= " -t .5 $slabpic 0 $loff - $legwidth 0";
314 > $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  
318   if ($doextrem == 1) {
319 <        # Get min/max image luminance
319 >    # Get min/max image luminance
320      my $cmd1 = 'pextrem -o ' . $picture;
321      my $retval = `$cmd1`;
322      # e.g.
# Line 295 | Line 326 | if ($doextrem == 1) {
326  
327      my @extrema = split(/\s/, $retval);
328      my ($lxmin, $ymin, $rmin, $gmin, $bmin, $lxmax, $ymax, $rmax, $gmax, $bmax) = @extrema;
329 <        $lxmin += $legwidth;
330 <        $lxmax += $legwidth;
329 >    $lxmin += $legwidth;
330 >    $lxmax += $legwidth;
331  
332 <        # Weighted average of R,G,B
332 >    # Weighted average of R,G,B
333      my $minpos = "$lxmin $ymin";
334      my $minval = ($rmin * .27 + $gmin * .67 + $bmin * .06) * $mult;
335      $minval =~ s/(\.[0-9]{3})[0-9]*/$1/;
305    my $maxpos = "$lxmax $ymax";
336      my $maxval = ($rmax * .27 + $gmax * .67 + $bmax * .06) * $mult;
337      $maxval =~ s/(\.[0-9]{3})[0-9]*/$1/;
338  
339 <        # Create the labels for min/max intensity
339 >    # Create the labels for min/max intensity
340      my $minvpic = "$td/minv.hdr";
341 <    $cmd1 = "psign -s -.15 -a 2 -h 16 $minval > $minvpic";
312 <    system $cmd1;
341 >    system "psign -s -.15 -a 2 -h 16 $minval > $minvpic";
342      my $maxvpic = "$td/maxv.hdr";
343 <    $cmd1 = "psign -s -.15 -a 2 -h 16 $maxval > $maxvpic";
315 <    system $cmd1;
343 >    system "psign -s -.15 -a 2 -h 16 $maxval > $maxvpic";
344  
345 <        # Add extrema labels to command line
346 <    $cmd .= " $minvpic $minpos $maxvpic $maxpos";
345 >    # Add extrema labels to command line
346 >    $cmd .= qq[ $minvpic $minpos $maxvpic $lxmax $ymax];
347   }
348  
349   # Process image and combine with legend

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines