ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/falsecolor.pl
Revision: 2.22
Committed: Mon Nov 14 18:45:38 2022 UTC (18 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.21: +5 -6 lines
Log Message:
perf(falsecolor): Minor adjustments to matrix value formatting

File Contents

# User Rev Content
1 greg 2.1 #!/usr/bin/perl -w
2 greg 2.22 # RCSid $Id: falsecolor.pl,v 2.21 2022/11/14 00:35:05 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.14 my @palettes = ('def', 'spec', 'pm3d', 'hot', 'eco', 'tbo');
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.20 my @overlayWH = (0,0); # Overlay matrix width and height (default = none)
26     my @overlayRect; # Overlay rectangle (left, lower, right, upper)
27 greg 2.12 my $haszero = 1; # print 0 in scale for falsecolor images only
28 greg 2.20 my $docont = ''; # Contours: -cl, -cb, and -c0
29 greg 2.6 my $doposter = 0; # Posterization: -cp
30 greg 2.5 my $doextrem = 0; # Don't mark extrema
31 greg 2.1 my $needfile = 0;
32 greg 2.18 my $showpal = 0; # Show available colour palettes
33 greg 2.1
34 greg 2.19 my @savedARGV = @ARGV; # Save for final header
35    
36 greg 2.1 while ($#ARGV >= 0) {
37 greg 2.5 $_ = shift;
38     # Options with qualifiers
39     if (m/-lw/) { # Legend width
40     $legwidth = shift;
41     } elsif (m/-lh/) { # Legend height
42     $legheight = shift;
43     } elsif (m/-m/) { # Multiplier
44     $mult = shift;
45 greg 2.9 } elsif (m/-spec/) {
46 greg 2.20 die("depricated option '-spec'. Please use '-pal spec' instead.\n");
47 greg 2.5 } elsif (m/-s/) { # Scale
48     $scale = shift;
49 greg 2.1 if ($scale =~ m/[aA].*/) {
50     $needfile = 1;
51 greg 2.12 }
52     } elsif (m/-d/) { # Number of maximum digits for numbers in legend
53     $scaledigits = shift;
54 greg 2.5 } elsif (m/-l$/) { # Label
55     $label = shift;
56     } elsif (m/-log/) { # Logarithmic mapping
57     $decades = shift;
58     } elsif (m/-r/) { # Custom palette functions for R,G,B
59     $redv = shift;
60     } elsif (m/-g/) {
61     $grnv = shift;
62     } elsif (m/-b/) {
63     $bluv = shift;
64     } elsif (m/-pal$/) { # Color palette
65     $pal = shift;
66     if (! grep $_ eq $pal, @palettes) {
67     die("invalid palette '$pal'.\n");
68     }
69     $redv = "${pal}_red(v)";
70     $grnv = "${pal}_grn(v)";
71     $bluv = "${pal}_blu(v)";
72     } elsif (m/-i$/) { # Image for intensity mapping
73     $picture = shift;
74     } elsif (m/-p$/) { # Image for background
75     $cpict = shift;
76     } elsif (m/-ip/ || m/-pi/) {
77     $picture = shift;
78     $cpict = $picture;
79     } elsif (m/-n/) { # Number of contour lines
80     $ndivs = shift;
81 greg 2.20 } elsif (m/-odim$/) { # Overlay width and height
82     $overlayWH[0] = shift;
83     $overlayWH[1] = shift;
84     $needfile ||= $overlayWH[0] && $overlayWH[1];
85     } elsif (m/-orct/) { # Overlay rectangle
86     $overlayRect[0] = shift;
87     $overlayRect[1] = shift;
88     $overlayRect[2] = shift;
89     $overlayRect[3] = shift;
90 greg 2.1
91 greg 2.5 # Switches
92     } elsif (m/-cl/) { # Contour lines
93 greg 2.1 $docont = 'a';
94 greg 2.12 $haszero = 0;
95 greg 2.5 } elsif (m/-cb/) { # Contour bands
96 greg 2.1 $docont = 'b';
97 greg 2.12 $haszero = 0;
98 greg 2.20 } elsif (m/-cp/) { # Posterize
99 greg 2.6 $doposter = 1;
100 greg 2.12 $haszero = 0;
101 greg 2.20 } elsif (m/-c0/) { # Turn off falsecolor operation
102     $docont = '0';
103     $legwidth = 0;
104     $legheight = 0;
105 greg 2.6 } elsif (m/-palettes/) { # Show all available palettes
106     $scale = 45824; # 256 * 179
107     $showpal = 1;
108 greg 2.5 } elsif (m/-e/) {
109 greg 2.1 $doextrem = 1;
110     $needfile = 1;
111 greg 2.5 # Oops! Illegal option
112 greg 2.1 } else {
113 greg 2.5 die("bad option \"$_\"\n");
114 greg 2.1 }
115     }
116    
117 greg 2.11 if (($legwidth <= 20) || ($legheight <= 40)) {
118     # Legend is too small to be legible. Don't bother doing one.
119     $legwidth = 0;
120     $legheight = 0;
121     }
122    
123 greg 2.1 # Temporary directory. Will be removed upon successful program exit.
124     my $td = tempdir( CLEANUP => 1 );
125    
126 greg 2.20 if ($needfile && $picture eq '-') {
127 greg 2.5 # Pretend that $td/stdin.rad is the actual filename.
128 greg 2.3 $picture = "$td/stdin.hdr";
129 greg 2.1 open(FHpic, ">$picture") or
130     die("Unable to write to file $picture\n");
131 greg 2.5 # Input is from STDIN: Capture to file.
132     while (<>) {
133     print FHpic;
134     }
135 greg 2.1 close(FHpic);
136 greg 2.3
137 greg 2.5 if ($cpict eq '-') {
138 greg 2.19 $cpict = $picture;
139 greg 2.5 }
140 greg 2.1 }
141    
142     # Find a good scale for auto mode.
143     if ($scale =~ m/[aA].*/) {
144 greg 2.16 my @histo = split(/\s/, `phisto $picture`);
145 greg 2.5 # e.g. $ phisto tests/richmond.hdr| tail -2
146     # 3.91267 14
147     # 3.94282 6
148 greg 2.15 my $LogLmax = $histo[-4];
149 greg 2.1 $scale = $mult / 179 * 10**$LogLmax;
150 greg 2.14 }
151 greg 2.12
152 greg 2.20 if ($doposter) {
153     # -cp -> $doposter = 1
154     my $newv = join( "seg2(v)", split("v", $redv) );
155     $redv = $newv;
156     $newv = join( "seg2(v)", split("v", $bluv) );
157     $bluv = $newv;
158     $newv = join( "seg2(v)", split("v", $grnv) );
159     $grnv = $newv;
160     } elsif ($docont) {
161 greg 2.12 # -cl -> $docont = a
162     # -cb -> $docont = b
163     my $newv = join( "(v-1/ndivs)*ndivs/(ndivs-1)", split("v", $redv) );
164     $redv = $newv;
165     $newv = join( "(v-1/ndivs)*ndivs/(ndivs-1)", split("v", $bluv) );
166     $bluv = $newv;
167     $newv = join( "(v-1/ndivs)*ndivs/(ndivs-1)", split("v", $grnv) );
168     $grnv = $newv;
169 greg 2.1 }
170    
171 greg 2.3 my $pc0 = "$td/pc0.cal";
172 greg 2.1 open(FHpc0, ">$pc0");
173     print FHpc0 <<EndOfPC0;
174     PI : 3.14159265358979323846 ;
175     scale : $scale ;
176     mult : $mult ;
177     ndivs : $ndivs ;
178     gamma : 2.2;
179    
180     or(a,b) : if(a,a,b);
181     EPS : 1e-7;
182     neq(a,b) : if(a-b-EPS,1,b-a-EPS);
183     btwn(a,x,b) : if(a-x,-1,b-x);
184 greg 2.18 clip(x) : min(1,max(x,0));
185 greg 2.1 frac(x) : x - floor(x);
186 greg 2.12 boundary(a,b) : neq(floor(ndivs*a),floor(ndivs*b));
187 greg 2.18 round(x) : floor(x+.5);
188 greg 2.1
189     spec_red(x) = 1.6*x - .6;
190     spec_grn(x) = if(x-.375, 1.6-1.6*x, 8/3*x);
191     spec_blu(x) = 1 - 8/3*x;
192    
193 greg 2.12 pm3d_red(x) = sqrt(clip(x)) ^ gamma;
194     pm3d_grn(x) = clip(x*x*x) ^ gamma;
195 greg 2.6 pm3d_blu(x) = clip(sin(2*PI*clip(x))) ^ gamma;
196 greg 2.1
197     hot_red(x) = clip(3*x) ^ gamma;
198     hot_grn(x) = clip(3*x - 1) ^ gamma;
199     hot_blu(x) = clip(3*x - 2) ^ gamma;
200    
201 greg 2.9 eco_red(x) = clip(2*x) ^ gamma;
202     eco_grn(x) = clip(2*(x-0.5)) ^ gamma;
203     eco_blu(x) = clip(2*(0.5-x)) ^ gamma;
204    
205 greg 2.1 interp_arr2(i,x,f):(i+1-x)*f(i)+(x-i)*f(i+1);
206     interp_arr(x,f):if(x-1,if(f(0)-x,interp_arr2(floor(x),x,f),f(f(0))),f(1));
207    
208 greg 2.14 interp_tbo2(x,f):f(round(x*255)+1)+(f(min(255,round(x*255)+1)+1)-f(round(x*255)+1))*(x*255-round(x*255));
209     interp_tbo(x,f):if(x-1,f(256),if(x,interp_tbo2(x,f),f(1)));
210    
211 greg 2.1 def_redp(i):select(i,0.18848,0.05468174,
212     0.00103547,8.311144e-08,7.449763e-06,0.0004390987,0.001367254,
213     0.003076,0.01376382,0.06170773,0.1739422,0.2881156,0.3299725,
214     0.3552663,0.372552,0.3921184,0.4363976,0.6102754,0.7757267,
215     0.9087369,1,1,0.9863);
216     def_red(x):interp_arr(x/0.0454545+1,def_redp);
217    
218     def_grnp(i):select(i,0.0009766,2.35501e-05,
219     0.0008966244,0.0264977,0.1256843,0.2865799,0.4247083,0.4739468,
220     0.4402732,0.3671876,0.2629843,0.1725325,0.1206819,0.07316644,
221     0.03761026,0.01612362,0.004773749,6.830967e-06,0.00803605,
222     0.1008085,0.3106831,0.6447838,0.9707);
223     def_grn(x):interp_arr(x/0.0454545+1,def_grnp);
224    
225     def_blup(i):select(i,0.2666,0.3638662,0.4770437,
226     0.5131397,0.5363797,0.5193677,0.4085123,0.1702815,0.05314236,
227     0.05194055,0.08564082,0.09881395,0.08324373,0.06072902,
228     0.0391076,0.02315354,0.01284458,0.005184709,0.001691774,
229     2.432735e-05,1.212949e-05,0.006659406,0.02539);
230     def_blu(x):interp_arr(x/0.0454545+1,def_blup);
231    
232 greg 2.14 tbo_redp(i):select(i,0.18995,0.19483,0.19956,0.20415,0.2086,0.21291,0.21708,0.22111,0.225,0.22875,0.23236,0.23582,0.23915,0.24234,0.24539,0.2483,0.25107,0.25369,0.25618,0.25853,0.26074,0.2628,0.26473,0.26652,0.26816,0.26967,0.27103,0.27226,0.27334,0.27429,0.27509,0.27576,0.27628,0.27667,0.27691,0.27701,0.27698,0.2768,0.27648,0.27603,0.27543,0.27469,0.27381,0.27273,0.27106,0.26878,0.26592,0.26252,0.25862,0.25425,0.24946,0.24427,0.23874,0.23288,0.22676,0.22039,0.21382,0.20708,0.20021,0.19326,0.18625,0.17923,0.17223,0.16529,0.15844,0.15173,0.14519,0.13886,0.13278,0.12698,0.12151,0.11639,0.11167,0.10738,0.10357,0.10026,0.0975,0.09532,0.09377,0.09287,0.09267,0.0932,0.09451,0.09662,0.09958,0.10342,0.10815,0.11374,0.12014,0.12733,0.13526,0.14391,0.15323,0.16319,0.17377,0.18491,0.19659,0.20877,0.22142,0.23449,0.24797,0.2618,0.27597,0.29042,0.30513,0.32006,0.33517,0.35043,0.36581,0.38127,0.39678,0.41229,0.42778,0.44321,0.45854,0.47375,0.48879,0.50362,0.51822,0.53255,0.54658,0.56026,0.57357,0.58646,0.59891,0.61088,0.62233,0.63323,0.64362,0.65394,0.66428,0.67462,0.68494,0.69525,0.70553,0.71577,0.72596,0.7361,0.74617,0.75617,0.76608,0.77591,0.78563,0.79524,0.80473,0.8141,0.82333,0.83241,0.84133,0.8501,0.85868,0.86709,0.8753,0.88331,0.89112,0.8987,0.90605,0.91317,0.92004,0.92666,0.93301,0.93909,0.94489,0.95039,0.9556,0.96049,0.96507,0.96931,0.97323,0.97679,0.98,0.98289,0.98549,0.98781,0.98986,0.99163,0.99314,0.99438,0.99535,0.99607,0.99654,0.99675,0.99672,0.99644,0.99593,0.99517,0.99419,0.99297,0.99153,0.98987,0.98799,0.9859,0.9836,0.98108,0.97837,0.97545,0.97234,0.96904,0.96555,0.96187,0.95801,0.95398,0.94977,0.94538,0.94084,0.93612,0.93125,0.92623,0.92105,0.91572,0.91024,0.90463,0.89888,0.89298,0.88691,0.88066,0.87422,0.8676,0.86079,0.8538,0.84662,0.83926,0.83172,0.82399,0.81608,0.80799,0.79971,0.79125,0.7826,0.77377,0.76476,0.75556,0.74617,0.73661,0.72686,0.71692,0.7068,0.6965,0.68602,0.67535,0.66449,0.65345,0.64223,0.63082,0.61923,0.60746,0.5955,0.58336,0.57103,0.55852,0.54583,0.53295,0.51989,0.50664,0.49321,0.4796);
233     tbo_red(x):interp_tbo(x,tbo_redp) ^ gamma;
234    
235     tbo_grnp(i):select(i,0.07176,0.08339,0.09498,0.10652,0.11802,0.12947,0.14087,0.15223,0.16354,0.17481,0.18603,0.1972,0.20833,0.21941,0.23044,0.24143,0.25237,0.26327,0.27412,0.28492,0.29568,0.30639,0.31706,0.32768,0.33825,0.34878,0.35926,0.3697,0.38008,0.39043,0.40072,0.41097,0.42118,0.43134,0.44145,0.45152,0.46153,0.47151,0.48144,0.49132,0.50115,0.51094,0.52069,0.5304,0.54015,0.54995,0.55979,0.56967,0.57958,0.5895,0.59943,0.60937,0.61931,0.62923,0.63913,0.64901,0.65886,0.66866,0.67842,0.68812,0.69775,0.70732,0.7168,0.7262,0.73551,0.74472,0.75381,0.76279,0.77165,0.78037,0.78896,0.7974,0.80569,0.81381,0.82177,0.82955,0.83714,0.84455,0.85175,0.85875,0.86554,0.87211,0.87844,0.88454,0.8904,0.896,0.90142,0.90673,0.91193,0.91701,0.92197,0.9268,0.93151,0.93609,0.94053,0.94484,0.94901,0.95304,0.95692,0.96065,0.96423,0.96765,0.97092,0.97403,0.97697,0.97974,0.98234,0.98477,0.98702,0.98909,0.99098,0.99268,0.99419,0.99551,0.99663,0.99755,0.99828,0.99879,0.9991,0.99919,0.99907,0.99873,0.99817,0.99739,0.99638,0.99514,0.99366,0.99195,0.98999,0.98775,0.98524,0.98246,0.97941,0.9761,0.97255,0.96875,0.9647,0.96043,0.95593,0.95121,0.94627,0.94113,0.93579,0.93025,0.92452,0.91861,0.91253,0.90627,0.89986,0.89328,0.88655,0.87968,0.87267,0.86553,0.85826,0.85087,0.84337,0.83576,0.82806,0.82025,0.81236,0.80439,0.79634,0.78823,0.78005,0.77181,0.76352,0.75519,0.74682,0.73842,0.73,0.7214,0.7125,0.7033,0.69382,0.68408,0.67408,0.66386,0.65341,0.64277,0.63193,0.62093,0.60977,0.59846,0.58703,0.57549,0.56386,0.55214,0.54036,0.52854,0.51667,0.50479,0.49291,0.48104,0.4692,0.4574,0.44565,0.43399,0.42241,0.41093,0.39958,0.38836,0.37729,0.36638,0.35566,0.34513,0.33482,0.32473,0.31489,0.3053,0.29599,0.28696,0.27824,0.26981,0.26152,0.25334,0.24526,0.2373,0.22945,0.2217,0.21407,0.20654,0.19912,0.19182,0.18462,0.17753,0.17055,0.16368,0.15693,0.15028,0.14374,0.13731,0.13098,0.12477,0.11867,0.11268,0.1068,0.10102,0.09536,0.0898,0.08436,0.07902,0.0738,0.06868,0.06367,0.05878,0.05399,0.04931,0.04474,0.04028,0.03593,0.03169,0.02756,0.02354,0.01963,0.01583);
236     tbo_grn(x):interp_tbo(x,tbo_grnp) ^ gamma;
237    
238     tbo_blup(i):select(i,0.23217,0.26149,0.29024,0.31844,0.34607,0.37314,0.39964,0.42558,0.45096,0.47578,0.50004,0.52373,0.54686,0.56942,0.59142,0.61286,0.63374,0.65406,0.67381,0.693,0.71162,0.72968,0.74718,0.76412,0.7805,0.79631,0.81156,0.82624,0.84037,0.85393,0.86692,0.87936,0.89123,0.90254,0.91328,0.92347,0.93309,0.94214,0.95064,0.95857,0.96594,0.97275,0.97899,0.98461,0.9893,0.99303,0.99583,0.99773,0.99876,0.99896,0.99835,0.99697,0.99485,0.99202,0.98851,0.98436,0.97959,0.97423,0.96833,0.9619,0.95498,0.94761,0.93981,0.93161,0.92305,0.91416,0.90496,0.8955,0.8858,0.8759,0.86581,0.85559,0.84525,0.83484,0.82437,0.81389,0.80342,0.79299,0.78264,0.7724,0.7623,0.75237,0.74265,0.73316,0.72393,0.715,0.70599,0.69651,0.6866,0.67627,0.66556,0.65448,0.64308,0.63137,0.61938,0.60713,0.59466,0.58199,0.56914,0.55614,0.54303,0.52981,0.51653,0.50321,0.48987,0.47654,0.46325,0.45002,0.43688,0.42386,0.41098,0.39826,0.38575,0.37345,0.3614,0.34963,0.33816,0.32701,0.31622,0.30581,0.29581,0.28623,0.27712,0.26849,0.26038,0.2528,0.24579,0.23937,0.23356,0.22835,0.2237,0.2196,0.21602,0.21294,0.21032,0.20815,0.2064,0.20504,0.20406,0.20343,0.20311,0.2031,0.20336,0.20386,0.20459,0.20552,0.20663,0.20788,0.20926,0.21074,0.2123,0.21391,0.21555,0.21719,0.2188,0.22038,0.22188,0.22328,0.22456,0.2257,0.22667,0.22744,0.228,0.22831,0.22836,0.22811,0.22754,0.22663,0.22536,0.22369,0.22161,0.21918,0.2165,0.21358,0.21043,0.20706,0.20348,0.19971,0.19577,0.19165,0.18738,0.18297,0.17842,0.17376,0.16899,0.16412,0.15918,0.15417,0.1491,0.14398,0.13883,0.13367,0.12849,0.12332,0.11817,0.11305,0.10797,0.10294,0.09798,0.0931,0.08831,0.08362,0.07905,0.07461,0.07031,0.06616,0.06218,0.05837,0.05475,0.05134,0.04814,0.04516,0.04243,0.03993,0.03753,0.03521,0.03297,0.03082,0.02875,0.02677,0.02487,0.02305,0.02131,0.01966,0.01809,0.0166,0.0152,0.01387,0.01264,0.01148,0.01041,0.00942,0.00851,0.00769,0.00695,0.00629,0.00571,0.00522,0.00481,0.00449,0.00424,0.00408,0.00401,0.00401,0.0041,0.00427,0.00453,0.00486,0.00529,0.00579,0.00638,0.00705,0.0078,0.00863,0.00955,0.01055);
239     tbo_blu(x):interp_tbo(x,tbo_blup) ^ gamma;
240    
241 greg 2.12 isconta = if(btwn(1/ndivs/2,v,1+1/ndivs/2),or(boundary(vleft,vright),boundary(vabove,vbelow)),-1);
242     iscontb = if(btwn(1/ndivs/2,v,1+1/ndivs/2),-btwn(.1,frac(ndivs*v),.9),-1);
243 greg 2.20 iscont0 = -1;
244 greg 2.12
245     seg(x)=(floor(v*ndivs)+.5)/ndivs;
246     seg2(x)=(seg(x)-1/ndivs)*ndivs/(ndivs-1);
247 greg 2.1
248     ra = 0;
249     ga = 0;
250     ba = 0;
251    
252     in = 1;
253    
254     ro = if(in,clip($redv),ra);
255     go = if(in,clip($grnv),ga);
256     bo = if(in,clip($bluv),ba);
257     EndOfPC0
258 greg 2.6 close FHpc0;
259 greg 2.1
260 greg 2.3 my $pc1 = "$td/pc1.cal";
261 greg 2.1 open(FHpc1, ">$pc1");
262     print FHpc1 <<EndOfPC1;
263     norm : mult/scale/le(1);
264    
265     v = map(li(1)*norm);
266    
267     vleft = map(li(1,-1,0)*norm);
268     vright = map(li(1,1,0)*norm);
269     vabove = map(li(1,0,1)*norm);
270     vbelow = map(li(1,0,-1)*norm);
271    
272     map(x) = x;
273    
274     ra = ri(nfiles);
275     ga = gi(nfiles);
276     ba = bi(nfiles);
277     EndOfPC1
278 greg 2.6 close FHpc1;
279 greg 2.1
280     my $pc0args = "-f $pc0";
281     my $pc1args = "-f $pc1";
282    
283 greg 2.20 if ($showpal) {
284 greg 2.6 my $pc = "pcompos -a 1";
285     foreach my $pal (@palettes) {
286     my $fcimg = "$td/$pal.hdr";
287     my $lbimg = "$td/${pal}_label.hdr";
288     system "psign -cb 0 0 0 -cf 1 1 1 -h 20 $pal > $lbimg";
289     my $cmd = qq[pcomb $pc0args -e "v=x/256"];
290     $cmd .= qq[ -e "ro=clip(${pal}_red(v));go=clip(${pal}_grn(v));bo=clip(${pal}_blu(v))"];
291     $cmd .= qq[ -x 256 -y 30 > $fcimg];
292     system "$cmd";
293     $pc .= " $fcimg $lbimg";
294     }
295     system "$pc";
296     exit 0;
297     }
298    
299 greg 2.20 if ($cpict eq '' && $docont ne '0') {
300 greg 2.6 $pc1args .= qq[ -e "ra=0;ga=0;ba=0"];
301 greg 2.1 } elsif ($cpict eq $picture) {
302     $cpict = '';
303     }
304    
305     # Logarithmic mapping
306     if ($decades > 0) {
307 greg 2.6 $pc1args .= qq[ -e "map(x)=if(x-10^-$decades,log10(x)/$decades+1,0)"];
308 greg 2.1 }
309    
310 greg 2.20 # Contours
311     if ($docont ne '') {
312     # -cl -> $docont = a
313     # -cb -> $docont = b
314     # -c0 -> $docont = FALSE
315     $pc0args .= qq[ -e "in=iscont$docont"];
316     }
317 greg 2.1 # Colours in the legend
318 greg 2.3 my $scolpic = "$td/scol.hdr";
319 greg 2.6
320 greg 2.1 # Labels in the legend
321 greg 2.3 my $slabpic = "$td/slab.hdr";
322 greg 2.1 my $cmd;
323 greg 2.11 if ($legwidth > 0) {
324 greg 2.5 # Legend: Create the text labels
325 greg 2.12 my $sheight = floor($legheight / $ndivs );
326     my $theight = floor($legwidth/(8/1.67));
327     my $stheight = $sheight <= $theight ? $sheight : $theight;
328     my $vlegheight = $sheight * $ndivs * (1+1.5/$ndivs);
329     my $tslabpic = "$td/slabT.hdr";
330 greg 2.18 system "psign -s -.15 -cf 1 1 1 -cb 0 0 0 -h $stheight $label > $tslabpic";
331 greg 2.12 my $loop = $ndivs+$haszero;
332     my $hlegheight = $sheight * ($loop) + $sheight * .5;
333 greg 2.19 my $pcompost = qq[pcompos -b 0 0 0 =-0 $tslabpic 0 $hlegheight ];
334 greg 2.12 for (my $i=0; $i<$loop; $i++) {
335     my $imap = ($ndivs - $i) / $ndivs;
336 greg 2.5 my $value = $scale;
337 greg 2.1 if ($decades > 0) {
338 greg 2.3 $value *= 10**(($imap - 1) * $decades);
339 greg 2.1 } else {
340 greg 2.3 $value *= $imap;
341 greg 2.1 }
342 greg 2.20 # Limit decimal places
343     if ($scaledigits <= 0) {
344     $value =~ s/\.[0-9]*//;
345     } else {
346     $value =~ s/(\.[0-9]{$scaledigits})[0-9]*/$1/;
347     }
348 greg 2.12 $tslabpic = "$td/slab$i.hdr";
349 greg 2.18 system "psign -s -.15 -cf 1 1 1 -cb 0 0 0 -h $stheight $value > $tslabpic";
350 greg 2.12 $hlegheight = $sheight * ($loop - $i - 1) + $sheight * .5;
351     $pcompost .= qq[=-0 $tslabpic 0 $hlegheight ];
352 greg 2.1 }
353 greg 2.12 $pcompost .= qq[ > $slabpic];
354     system $pcompost;
355 greg 2.4
356 greg 2.5 # Legend: Create the background colours
357 greg 2.6 $cmd = qq[pcomb $pc0args];
358 greg 2.13 $cmd .= qq[ -x $legwidth -y $vlegheight];
359     $cmd .= qq[ -e "v=(y+.5-$sheight)/(yres/(1+1.5/$ndivs));vleft=v;vright=v"];
360 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))"];
361     $cmd .= qq[ -e "ra=0;ga=0;ba=0;"];
362 greg 2.13 $cmd .= qq[ > $scolpic];
363 greg 2.4 system $cmd;
364 greg 2.1 } else {
365 greg 2.5 # Create dummy colour scale and legend labels so we don't
366     # need to change the final command line.
367 greg 2.17 system "pcomb -x 1 -y 1 -e lo=1 > $scolpic";
368     system "pcomb -x 1 -y 1 -e lo=1 > $slabpic";
369 greg 2.1 }
370    
371 greg 2.3 # Legend: Invert the text labels (for dropshadow)
372     my $slabinvpic = "$td/slabinv.hdr";
373 greg 2.20 system qq[pcomb -e "lo=1-gi(1)" $slabpic > $slabinvpic];
374 greg 2.12
375     my $sh0 = -floor($legheight / $ndivs / 2);
376     if ($haszero < 1) {
377     $sh0 = -floor($legheight / ($ndivs)*1.5);
378     }
379 greg 2.6
380 greg 2.1 # Command line without extrema
381 greg 2.12
382     $cmd = qq[pcomb $pc0args $pc1args "$picture"];
383     $cmd .= qq[ "$cpict"] if ($cpict);
384 greg 2.20 $cmd .= qq[ | pcompos -h -b 0 0 0 $scolpic 0 $sh0 +t .1 $slabinvpic 2 -1];
385 greg 2.12 $cmd .= qq[ -t .5 $slabpic 0 0 - $legwidth 0];
386 greg 2.1
387 greg 2.20 my $cheight = 32;
388    
389     if ($overlayWH[0] && $overlayWH[1]) {
390 greg 2.22 # Overlay picture matrix values
391 greg 2.20 my @picWH = split ' ', `getinfo -d < $picture`;
392     @picWH = ($picWH[3], $picWH[1]);
393     if ($#overlayRect != 3) {
394     @overlayRect = (0, 0, @picWH);
395     }
396     if ($overlayRect[2] <= $overlayRect[0] ||
397     $overlayRect[3] <= $overlayRect[1]) {
398     die("Illegal overlay rectangle\n");
399     }
400     # Compute spacing between values
401     my @cropWH = ($overlayRect[2]-$overlayRect[0], $overlayRect[3]-$overlayRect[1]);
402     my $ohspacing = $cropWH[0] / $overlayWH[0];
403     my $ovspacing = $cropWH[1] / $overlayWH[1];
404     # Compute character height from spacing
405     $cheight = int($ovspacing * 0.67 + 0.5);
406     if ($cheight >= 1.67/10 * $ohspacing) {
407     $cheight = int(1.67/10 * $ohspacing);
408     }
409     if ($cheight < 10) {
410     die "Overlay matrix spacing too tight\n";
411     }
412     my $cmd1 = qq[pcompos -x $cropWH[0] -y $cropWH[1] "$picture" @overlayRect[0..1]];
413     $cmd1 .= qq[ | pfilt -1 -b -x /$cheight -y /$cheight];
414     $cmd1 .= qq[ | pfilt -1 -r .5 -x $cropWH[0] -y $cropWH[1]];
415     $cmd1 .= qq[ | pvalue -o -h -H -b -d -e $mult];
416     # Compute matrix label center positions in subimage
417     my @xpos;
418     foreach (0 .. ($overlayWH[0]-1)) {
419     $xpos[$_] = int($ohspacing * ($_ + 0.5));
420     }
421     my @ypos;
422     foreach (0 .. ($overlayWH[1]-1)) {
423     $ypos[$_] = int($ovspacing * ($_ + 0.5));
424     }
425     open(FHsamp, "$cmd1 |");
426     $cmd1 = qq[pcompos -h -b 0 0 0 -x $cropWH[0] -y $cropWH[1]];
427     my $pscmd = qq[psign -s -.15 -cf 1 1 1 -cb 0 0 0 -h $cheight];
428     for (my $y = 0; $y < $cropWH[1]; $y++) {
429     my $ymatch = grep /^$y$/, @ypos;
430 greg 2.21 my $cmd2 = qq[pcompos -b 0 0 0 -x $cropWH[0]];
431 greg 2.20 for (my $x = 0; $x < $cropWH[0]; $x++) {
432     my $sampv = <FHsamp>;
433     next if (! $ymatch);
434     next if (! grep /^$x$/, @xpos);
435     chomp $sampv;
436     # Reformatting assumes %e from pvalue
437     $sampv =~ /^\s*([0-9])\.([0-9]+)[eE]([-+]?[0-9]+)$/;
438     my $manti = $1;
439     my $mantf = $2;
440     my $expi = $3;
441 greg 2.22 if ($expi < -3) {
442 greg 2.20 # use exponent format
443     } elsif ($expi < 0) {
444     my $pref = '0.';
445     for (my $i = $expi; ++$i < 0; ) {
446     $pref .= '0';
447     }
448     $sampv = $pref . $manti . $mantf;
449     } elsif ($expi < length($mantf)) {
450     $sampv = sprintf("%g", $sampv);
451 greg 2.22 } elsif ($expi <= 6) {
452 greg 2.20 $sampv = $manti . $mantf;
453     for (my $i = $expi - length($mantf); $i-- > 0; ) {
454     $sampv .= '0';
455     }
456     } # else use exponent format
457 greg 2.21 $cmd2 .= qq[ =0- "!$pscmd $sampv" $x 0];
458 greg 2.20 }
459 greg 2.21 next if (! $ymatch);
460     my $rowpic = "$td/mrow$y.hdr";
461     system "$cmd2 > $rowpic";
462     $cmd1 .= qq[ =-0 $rowpic 0 ] . ($cropWH[1]-1 - $y);
463 greg 2.20 }
464     close(FHsamp);
465     my $overpic = "$td/overlay.hdr";
466     system "$cmd1 > $overpic";
467     my $overinvpic = "$td/overinv.hdr";
468     system qq[pcomb -e "lo=1-gi(1)" $overpic > $overinvpic];
469     my $xleft = $legwidth + $overlayRect[0];
470     my $ybottom = $overlayRect[1];
471     $cmd .= qq[ +t .1 $overinvpic $xleft $ybottom];
472     # Offset from drop shadow
473     $xleft -= 2;
474     $ybottom += 1;
475     $cmd .= qq[ -t .5 $overpic $xleft $ybottom];
476     }
477    
478     if ($doextrem) {
479 greg 2.5 # Get min/max image luminance
480 greg 2.1 my $cmd1 = 'pextrem -o ' . $picture;
481     my $retval = `$cmd1`;
482     # e.g.
483     # x y r g b
484     # 193 207 3.070068e-02 3.118896e-02 1.995850e-02
485     # 211 202 1.292969e+00 1.308594e+00 1.300781e+00
486    
487     my @extrema = split(/\s/, $retval);
488 greg 2.3 my ($lxmin, $ymin, $rmin, $gmin, $bmin, $lxmax, $ymax, $rmax, $gmax, $bmax) = @extrema;
489 greg 2.5 $lxmin += $legwidth;
490     $lxmax += $legwidth;
491 greg 2.1
492 greg 2.5 # Weighted average of R,G,B
493 greg 2.1 my $minval = ($rmin * .27 + $gmin * .67 + $bmin * .06) * $mult;
494     my $maxval = ($rmax * .27 + $gmax * .67 + $bmax * .06) * $mult;
495 greg 2.20 if ($scaledigits <= 0) {
496     $minval =~ s/\.[0-9]*//;
497     $maxval =~ s/\.[0-9]*//;
498     } else {
499     $minval =~ s/(\.[0-9]{$scaledigits})[0-9]*/$1/;
500     $maxval =~ s/(\.[0-9]{$scaledigits})[0-9]*/$1/;
501     }
502 greg 2.5 # Create the labels for min/max intensity
503 greg 2.3 my $minvpic = "$td/minv.hdr";
504 greg 2.20 system "psign -s -.15 -a 2 -h $cheight $minval > $minvpic";
505 greg 2.3 my $maxvpic = "$td/maxv.hdr";
506 greg 2.20 system "psign -s -.15 -a 2 -h $cheight $maxval > $maxvpic";
507 greg 2.1
508 greg 2.5 # Add extrema labels to command line
509 greg 2.20 $cmd .= qq[ =00 $minvpic $lxmin $ymin =00 $maxvpic $lxmax $ymax];
510 greg 2.1 }
511    
512 greg 2.20 # Clean up and simplify info header with out command arguments
513 greg 2.19 $cmd .= qq[ | getinfo -r "pcompos " "falsecolor @savedARGV"];
514 greg 2.12
515 greg 2.1 # Process image and combine with legend
516 greg 2.22 exec $cmd;
517 greg 2.1
518     #EOF