blib/lib/ICC/Shared.pm

filter: Statementstmt 12.0% 98 / 817 Branchbran 2.5% 10 / 392 Conditioncond 0.5% 1 / 174 MC/DCmcdc 0.0% 0 / 102 Subroutinesub 26.6% 28 / 105 Podpod 90.9% 60 / 66 totaltotal 11.9% 197 / 1656 CC 293 SCAR 53.8
CC
293
Cov
8%
cspline_matrix
930.0
cspline
600.0
lagrange_matrix
380.0
CRAP 217.3
« blib/lib/ICC/Profile/clut.pm ↑ Summary blib/lib/ICC/Profile/gbd_.pm »
1 package ICC::Shared;
2  
3 41 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 41 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 41 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 41 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 41 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 41 ▶use Math::Matrix;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 41 ▶use POSIX qw(:math_h :float_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 41 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 41 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 41 ▶use YAML::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 41 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 41 our $VERSION = 0.57;
16  
17 # revised 2018-09-09
18 #
19 # Copyright © 2004-2018 by William B. Birkett
20  
21 # this module contains common methods, constants and functions
22  
23 # enable static variables
24 41 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # constants
27 41 ▶use constant D50 => [96.42, 100, 82.49]; # ICC D50 XYZ
Subroutine BEGIN: called CC=1 SCAR=0.0
28 41 ▶use constant d50 => [0.9642, 1.0, 0.8249]; # ICC D50 XYZNumber
Subroutine BEGIN: called CC=1 SCAR=0.0
29 41 ▶use constant d50P => [map {$_ * 32768/65535} 0.9642, 1.0, 0.8249]; # ICC D50 16-bit XYZ PCS
Subroutine BEGIN: called CC=1 SCAR=0.0
30 41 ▶use constant PI => atan2(0, -1); # pi
Subroutine BEGIN: called CC=1 SCAR=0.0
31 41 ▶use constant radian => 180/PI; # radian (in degrees)
Subroutine BEGIN: called CC=1 SCAR=0.0
32 41 ▶use constant ln10 => log(10); # natural log of 10
Subroutine BEGIN: called CC=1 SCAR=0.0
33 41 ▶use constant sin16 => sin(16/radian); # sin(16˚)
Subroutine BEGIN: called CC=1 SCAR=0.0
34 41 ▶use constant cos16 => cos(16/radian); # cos(16˚)
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 BEGIN {
37
38 # set constant vector elements to be read-only
39 41 ▶ for (@{(D50)}) {Internals::SvREADONLY($_, 1)}
Subroutine BEGIN: called CC=4 SCAR=13.9
40 41 for (@{(d50)}) {Internals::SvREADONLY($_, 1)}
41 41 for (@{(d50P)}) {Internals::SvREADONLY($_, 1)}
42
43 }
44  
45 # list of constants and functions to export
46 41 our @EXPORT = qw(D50 d50 d50P PI radian ln10 xyz2Lab Lab2xyz xyz2Lxyz Lxyz2xyz Lab2Lxyz Lxyz2Lab
47 XYZ2Lab Lab2XYZ XYZ2Lxyz Lxyz2XYZ XYZ2xyz xyz2XYZ XYZ2xyY xyY2XYZ x2L L2x dLdx dxdL XYZ2W xyz2dwv
48 xyz2Lab_jac Lab2xyz_jac dEab dEcmc dE94 dE00 dE99 dCh CCT CCT2 bbrad bbxy bbuv XYZ2ucs xy2ucs daylight
49 linear linear_matrix cspline cspline_matrix lagrange lagrange_matrix dotProduct crossProduct
50 flatten clip_struct round s15f162v v2s15f16 makeProfileFolder getICCPath filterPath setFile parse_tokens
51 is_vector is_num_vector is_matrix is_num_matrix);
52  
53 # add POSIX functions/constants
54 41 push(@EXPORT, @{$POSIX::EXPORT_TAGS{'math_h'}});
55 41 push(@EXPORT, @{$POSIX::EXPORT_TAGS{'float_h'}});
56  
57 #----------- common methods -----------
58  
59 # copy an object (using Storable)
60 # parameters: ([number_of_copies])
61 # returns: (new_object_references)
62 sub copy {
63  
64 # get parameters
65 0 ▶ my ($self, $n) = @_;
Subroutine copy: not called CC=8 SCAR=42.8
Pod documented
66  
67 # set default to one copy
68 0 ▶ $n = 1 if (! defined($n));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 0 0
69  
70 # return empty if zero copies
71 0 ▶ return() if ($n == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 0 0 0
72  
73 # verify number of copies is a positive integer
74 0 ▶ (! ref($n) && $n == int($n) && $n > 0) or croak('number of copies not a positive integer');
Branch: 1 branch
BranchTrueFalseTotal
not ref $n and $n == int $n and $n > 0 or croak("number of copies not a positive integer") 0 0 0
Condition0%0/6
not ref $n and $n == int $n and $n > 0
!ll&&!rl&&r
000
not ref $n and $n == int $n
!ll&&!rl&&r
000
MC/DC0%0/3
not ref $n and $n == int $n and $n > 0
not ref $n $n == int $n $n > 0
Truth table0%0/4
not(A) and B and C
A= not ref $nB= $n == int $nC= $n > 0
ABCresult
0XX0
10X0
1100
1111
75  
76 # verify parameter is a reference
77 0 ▶ (ref($self)) or croak("can't copy class $self");
Branch: 1 branch
BranchTrueFalseTotal
ref $self or croak("can't copy class $self") 0 0 0
78  
79 # return copies
80 0 ▶ return(wantarray ? map {Storable::dclone($self)} (1 .. $n) : Storable::dclone($self));
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
81  
82 }
83  
84 # print object contents
85 # format parameter is an array structure
86 # parameter: ([format])
87 # returns: (string)
88 sub dump {
89  
90 # get parameters
91 0 ▶ my ($self, $format) = @_;
Subroutine dump: not called CC=2 SCAR=17.9
Pod documented
92  
93 # verify object has 'sdump' method
94 0 ▶ ($self->can('sdump')) or croak("object lacks 'sdump' method");
Branch: 1 branch
BranchTrueFalseTotal
$self->can("sdump") or croak("object lacks 'sdump' method") 0 0 0
95  
96 # get string from 'sdump'
97 0 my $s = $self->sdump($format);
98  
99 # print string
100 0 print $s, "\n";
101  
102 # return string
103 0 return($s);
104  
105 }
106  
107 #------- color encoding functions -------
108  
109 # convert xyz to L*a*b*
110 # parameters: (input_array -or- input_structure)
111 # returns: (output_array -or- output_structure)
112 sub xyz2Lab {
113  
114 # push subroutine reference and number of parameters
115 0 ▶ push(@_, \&_xyz2Lab);
Subroutine xyz2Lab: not called CC=1 SCAR=6.9
Pod documented
116  
117 # convert input
118 0 &_convert3;
119  
120 }
121  
122 # convert L*a*b* to xyz
123 # parameters: (input_array -or- input_structure)
124 # returns: (output_array -or- output_structure)
125 sub Lab2xyz {
126  
127 # push subroutine reference and number of parameters
128 0 ▶ push(@_, \&_Lab2xyz);
Subroutine Lab2xyz: not called CC=1 SCAR=6.9
Pod documented
129  
130 # convert input
131 0 &_convert3;
132  
133 }
134  
135 # convert xyz to LxLyLz
136 # parameters: (input_array -or- input_structure)
137 # returns: (output_array -or- output_structure)
138 sub xyz2Lxyz {
139  
140 # push subroutine reference and number of parameters
141 0 ▶ push(@_, \&_xyz2Lxyz);
Subroutine xyz2Lxyz: not called CC=1 SCAR=6.9
Pod documented
142  
143 # convert input
144 0 &_convert3;
145  
146 }
147  
148 # convert LxLyLz to xyz
149 # parameters: (input_array -or- input_structure)
150 # returns: (output_array -or- output_structure)
151 sub Lxyz2xyz {
152  
153 # push subroutine reference and number of parameters
154 0 ▶ push(@_, \&_Lxyz2xyz);
Subroutine Lxyz2xyz: not called CC=1 SCAR=6.9
Pod documented
155  
156 # convert input
157 0 &_convert3;
158  
159 }
160  
161 # convert L*a*b* to LxLyLz
162 # parameters: (input_array -or- input_structure)
163 # returns: (output_array -or- output_structure)
164 sub Lab2Lxyz {
165  
166 # push subroutine reference and number of parameters
167 0 ▶ push(@_, \&_Lab2Lxyz);
Subroutine Lab2Lxyz: not called CC=1 SCAR=6.9
Pod documented
168  
169 # convert input
170 0 &_convert3;
171  
172 }
173  
174 # convert LxLyLz to L*a*b*
175 # parameters: (input_array -or- input_structure)
176 # returns: (output_array -or- output_structure)
177 sub Lxyz2Lab {
178  
179 # push subroutine reference and number of parameters
180 0 ▶ push(@_, \&_Lxyz2Lab);
Subroutine Lxyz2Lab: not called CC=1 SCAR=6.9
Pod documented
181  
182 # convert input
183 0 &_convert3;
184  
185 }
186  
187 # convert XYZ to L*a*b*
188 # default illuminant is D50
189 # parameters: (input_array -or- input_structure, [white_point_vector])
190 # returns: (output_array -or- output_structure)
191 sub XYZ2Lab {
192  
193 # push subroutine reference and number of parameters
194 0 ▶ push(@_, \&_XYZ2Lab);
Subroutine XYZ2Lab: not called CC=1 SCAR=6.9
Pod documented
195  
196 # convert input
197 0 &_convert4;
198  
199 }
200  
201 # convert L*a*b* to XYZ
202 # default illuminant is D50
203 # parameters: (input_array -or- input_structure, [white_point_vector])
204 # returns: (output_array -or- output_structure)
205 sub Lab2XYZ {
206  
207 # push subroutine reference and number of parameters
208 0 ▶ push(@_, \&_Lab2XYZ);
Subroutine Lab2XYZ: not called CC=1 SCAR=6.9
Pod documented
209  
210 # convert input
211 0 &_convert4;
212  
213 }
214  
215 # convert XYZ to LxLyLz
216 # default illuminant is D50
217 # parameters: (input_array -or- input_structure, [white_point_vector])
218 # returns: (output_array -or- output_structure)
219 sub XYZ2Lxyz {
220  
221 # push subroutine reference and number of parameters
222 0 ▶ push(@_, \&_XYZ2Lxyz);
Subroutine XYZ2Lxyz: not called CC=1 SCAR=6.9
Pod documented
223  
224 # convert input
225 0 &_convert4;
226  
227 }
228  
229 # convert LxLyLz to XYZ
230 # default illuminant is D50
231 # parameters: (input_array -or- input_structure, [white_point_vector])
232 # returns: (output_array -or- output_structure)
233 sub Lxyz2XYZ {
234  
235 # push subroutine reference and number of parameters
236 0 ▶ push(@_, \&_Lxyz2XYZ);
Subroutine Lxyz2XYZ: not called CC=1 SCAR=6.9
Pod documented
237  
238 # convert input
239 0 &_convert4;
240  
241 }
242  
243 # convert XYZ to xyz
244 # default illuminant is D50
245 # parameters: (input_array -or- input_structure, [white_point_vector])
246 # returns: (output_array -or- output_structure)
247 sub XYZ2xyz {
248  
249 # push subroutine reference and number of parameters
250 0 ▶ push(@_, \&_XYZ2xyz);
Subroutine XYZ2xyz: not called CC=1 SCAR=6.9
Pod documented
251  
252 # convert input
253 0 &_convert4;
254  
255 }
256  
257 # convert xyz to XYZ
258 # default illuminant is D50
259 # parameters: (input_array -or- input_structure, [white_point_vector])
260 # returns: (output_array -or- output_structure)
261 sub xyz2XYZ {
262  
263 # push subroutine reference and number of parameters
264 0 ▶ push(@_, \&_xyz2XYZ);
Subroutine xyz2XYZ: not called CC=1 SCAR=6.9
Pod documented
265  
266 # convert input
267 0 &_convert4;
268  
269 }
270  
271 # # convert XYZ to xyY
272 # parameters: (input_array -or- input_structure)
273 # returns: (output_array -or- output_structure)
274 sub XYZ2xyY {
275  
276 # push subroutine reference and number of parameters
277 0 ▶ push(@_, \&_XYZ2xyY);
Subroutine XYZ2xyY: not called CC=1 SCAR=6.9
Pod documented
278  
279 # convert input
280 0 &_convert3;
281  
282 }
283  
284 # convert xyY to XYZ
285 # parameters: (input_array -or- input_structure)
286 # returns: (output_array -or- output_structure)
287 sub xyY2XYZ {
288  
289 # push subroutine reference and number of parameters
290 0 ▶ push(@_, \&_xyY2XYZ);
Subroutine xyY2XYZ: not called CC=1 SCAR=6.9
Pod documented
291  
292 # convert input
293 0 &_convert3;
294  
295 }
296  
297 # convert x to L
298 # parameter: (x)
299 # returns: (L)
300 sub x2L {
301  
302 # return L
303 30 ▶ return(($_[0] > 216/24389) ? 116 * $_[0]**(1/3) - 16 : $_[0] * 24389/27);
Subroutine x2L: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] > 0.0088564516790356311027 ? : 30 0 30
304  
305 }
306  
307 # convert L to x
308 # parameter: (L)
309 # returns: (x)
310 sub L2x {
311  
312 # return x
313 120 ▶ return(($_[0] > 8) ? (($_[0] + 16)/116)**3 : $_[0] * 27/24389);
Subroutine L2x: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] > 8 ? : 120 0 120
314  
315 }
316  
317 # compute dL/dx
318 # parameter: (x)
319 # returns: (dL/dx)
320 sub dLdx {
321  
322 # get x
323 0 ▶ my $x = shift();
Subroutine dLdx: not called CC=2 SCAR=17.9
Pod documented
324  
325 # return dL/dx
326 0 ▶ return(($x > 216/24389) ? 116 * $x**(-2/3)/3 : 24389/27);
Branch: 1 branch
BranchTrueFalseTotal
$x > 0.0088564516790356311027 ? : 0 0 0
327  
328 }
329  
330 # compute dx/dL
331 # parameter: (L)
332 # returns: (dx/dL)
333 sub dxdL {
334  
335 # get L
336 0 ▶ my $L = shift();
Subroutine dxdL: not called CC=2 SCAR=17.9
Pod documented
337  
338 # return dx/dL
339 0 ▶ return(($L > 8) ? 3 * (($L + 16)/116)**2/116 : 27/24389);
Branch: 1 branch
BranchTrueFalseTotal
$L > 8 ? : 0 0 0
340  
341 }
342  
343 # compute xyz to L*a*b* Jacobian matrix
344 # parameters: (x, y, z)
345 # returns: (Jacobian_matrix)
346 sub xyz2Lab_jac {
347  
348 # get parameters
349 0 ▶ my ($x, $y, $z) = @_;
Subroutine xyz2Lab_jac: not called CC=1 SCAR=6.9
Pod documented
350  
351 # compute ∂Lx/∂x, ∂Ly/∂y, ∂Lz/∂z values
352 0 my $dLx = dLdx($x);
353 0 my $dLy = dLdx($y);
354 0 my $dLz = dLdx($z);
355  
356 # return Jacobian matrix
357 0 return(Math::Matrix->new(
358 [0, $dLy, 0],
359 [$dLx * 500/116, -$dLy * 500/116, 0],
360 [0, $dLy * 200/116, -$dLz * 200/116]
361 ));
362
363 }
364  
365 # compute L*a*b* to xyz Jacobian matrix
366 # parameters: (L, a, b)
367 # returns: (Jacobian_matrix)
368 sub Lab2xyz_jac {
369  
370 # get parameters
371 0 ▶ my ($L, $a, $b) = @_;
Subroutine Lab2xyz_jac: not called CC=1 SCAR=6.9
Pod documented
372  
373 # compute ∂x/∂Lx, ∂y/∂Ly, ∂z/∂Lz values
374 0 my $dx = dxdL($L + 116 * $a/500);
375 0 my $dy = dxdL($L);
376 0 my $dz = dxdL($L - 116 * $b/200);
377  
378 # return Jacobian matrix
379 0 return(Math::Matrix->new(
380 [$dx, $dx * 116/500, 0],
381 [$dy, 0, 0],
382 [$dz, 0, -$dz * 116/200]
383 ));
384
385 }
386  
387 # convert XYZ to CIE Whiteness
388 # parameters: (X, Y, Z, ref_to_WP_vector)
389 # returns: (W)
390 sub XYZ2W {
391  
392 # get parameters
393 0 ▶ my ($X, $Y, $Z, $wtpt) = @_;
Subroutine XYZ2W: not called CC=1 SCAR=6.9
Pod documented
394  
395 # compute chromaticity values
396 0 my @xyYs = _XYZ2xyY($X, $Y, $Z); # sample
397 0 my @xyYw = _XYZ2xyY(@{$wtpt}); # white point
398  
399 # return Whiteness
400 0 return($Y + 800 * ($xyYw[0] - $xyYs[0]) + 1700 * ($xyYw[1] - $xyYs[1]));
401  
402 }
403  
404 # convert xyz to density-weighted value
405 # parameters: (x, y, z)
406 # returns: (dw_value)
407 sub xyz2dwv {
408  
409 # get parameters
410 0 ▶ my ($x, $y, $z) = @_;
Subroutine xyz2dwv: not called CC=4 SCAR=30.0
Pod documented
411  
412 # return 0 if any parameter <= 0
413 0 ▶ return(0) if ($x <= 0 || $y <= 0 || $x <= 0);
Branch: 1 branch
BranchTrueFalseTotal
if $x <= 0 or $y <= 0 or $x <= 0 0 0 0
Condition0%0/6
$x <= 0 or $y <= 0 or $x <= 0
l!l&&r!l&&!r
000
$x <= 0 or $y <= 0
l!l&&r!l&&!r
000
MC/DC0%0/3
$x <= 0 or $y <= 0 or $x <= 0
$x <= 0 $y <= 0 $x <= 0
Truth table0%0/4
A or B or C
A= $x <= 0B= $y <= 0C= $x <= 0
ABCresult
1XX1
01X1
0011
0000
414  
415 # compute weight values
416 0 my $wx = -log($x/($x + $y + $z));
417 0 my $wy = -log($y/($x + $y + $z));
418 0 my $wz = -log($z/($x + $y + $z));
419  
420 # return density-weighted value
421 0 return(($wx * $x + $wy * $y + $wz * $z)/($wx + $wy + $wz));
422  
423 }
424  
425 #-------- color difference functions --------
426  
427 # compute ∆E*ab color difference
428 # parameters: (L*a*b*_1, L*a*b*_2)
429 # returns: (∆E*ab_value)
430 sub dEab {
431  
432 # return ∆E*ab
433 0 ▶ return(sqrt(($_[0] - $_[3])**2 + ($_[1] - $_[4])**2 + ($_[2] - $_[5])**2));
Subroutine dEab: not called CC=1 SCAR=6.9
Pod documented
434  
435 }
436  
437 # compute CMC color difference
438 # note: this function is not commutative
439 # L*a*b*_1 is the reference, L*a*b*_2 is the sample
440 # optional parameters are l (lightness) and c (chroma)
441 # default values of optional parameters are 2:1 (l:c)
442 # note: this function assumes D65, 10• colorimetry
443 # parameters: (L*a*b*_1, L*a*b*_2, [l, c])
444 # returns: (∆E*cmc_value)
445 sub dEcmc {
446  
447 # local variables
448 0 ▶ my ($dL, $C1, $C2, $dC, $dH2, $h1);
Subroutine dEcmc: not called CC=9 SCAR=45.0
Pod documented
449 0 my ($F, $T, $Sl, $Sc, $Sh, $l, $c);
450  
451 # compute ∆L*
452 0 $dL = $_[0] - $_[3];
453  
454 # compute C1*
455 0 $C1 = sqrt($_[1]**2 + $_[2]**2);
456  
457 # compute C2*
458 0 $C2 = sqrt($_[4]**2 + $_[5]**2);
459  
460 # compute ∆C*
461 0 $dC = $C1 - $C2;
462  
463 # compute ∆H*^2
464 0 $dH2 = ($_[1] - $_[4])**2 + ($_[2] - $_[5])**2 - $dC**2;
465  
466 # compute h1
467 0 ▶ $h1 = ($_[2] || $_[1]) ? radian * atan2($_[2], $_[1]) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[2] || $_[1] ? : 0 0 0
Condition0%0/3
$_[2] or $_[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[2] or $_[1]
$_[2] $_[1]
Truth table0%0/3
A or B
A= $_[2]B= $_[1]
ABresult
1X1
011
000
468  
469 # adjust h1 if negative
470 0 ▶ $h1 += 360 if ($h1 < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $h1 < 0 0 0 0
471  
472 # compute F
473 0 $F = sqrt($C1**4/($C1**4 + 1900));
474  
475 # compute T
476 0 ▶ $T = ($h1 >= 164 && $h1 <= 345) ? 0.56 + abs(0.2 * cos(($h1 + 168)/radian)) : 0.36 + abs(0.4 * cos(($h1 + 35)/radian));
Branch: 1 branch
BranchTrueFalseTotal
$h1 >= 164 && $h1 <= 345 ? : 0 0 0
Condition0%0/3
$h1 >= 164 and $h1 <= 345
!ll&&!rl&&r
000
MC/DC0%0/2
$h1 >= 164 and $h1 <= 345
$h1 >= 164 $h1 <= 345
Truth table0%0/3
A and B
A= $h1 >= 164B= $h1 <= 345
ABresult
0X0
100
111
477  
478 # compute Sl
479 0 ▶ $Sl = $_[0] < 16 ? 0.511 : 0.040975 * $_[0]/(1 + 0.01765 * $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
$_[0] < 16 ? : 0 0 0
480  
481 # compute Sc
482 0 $Sc = 0.0638 * $C1/(1 + 0.0131 * $C1) + 0.638;
483  
484 # compute Sh
485 0 $Sh = $Sc * ($F * $T + 1 - $F);
486  
487 # get l
488 0 ▶ $l = defined($_[6]) ? $_[6] : 2;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[6] ? : 0 0 0
489  
490 # get c
491 0 ▶ $c = defined($_[7]) ? $_[7] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[7] ? : 0 0 0
492  
493 # return ∆Ecmc
494 0 return(sqrt(($dL/($l * $Sl))**2 + ($dC/($c * $Sc))**2 + $dH2/($Sh**2)));
495  
496 }
497  
498 # compute ∆E*94 (graphic arts) color difference
499 # parameters: (L*a*b*_1, L*a*b*_2)
500 # returns: (∆E*94_value)
501 sub dE94 {
502  
503 # local variables
504 0 ▶ my ($dL, $C1, $C2, $dC, $dH2, $dH, $Cm);
Subroutine dE94: not called CC=1 SCAR=6.9
Pod documented
505  
506 # compute ∆L*
507 0 $dL = $_[0] - $_[3];
508  
509 # compute C1*
510 0 $C1 = sqrt($_[1]**2 + $_[2]**2);
511  
512 # compute C2*
513 0 $C2 = sqrt($_[4]**2 + $_[5]**2);
514  
515 # compute ∆C*
516 0 $dC = $C1 - $C2;
517  
518 # compute ∆H*^2
519 0 $dH2 = ($_[1] - $_[4])**2 + ($_[2] - $_[5])**2 - $dC**2;
520  
521 # compute geometric mean C*ab (makes function commutative)
522 0 $Cm = sqrt($C1 * $C2);
523  
524 # return ∆E*94
525 0 return(sqrt($dL**2 + ($dC/(1 + 0.045 * $Cm))**2 + ($dH2/(1 + 0.015 * $Cm)**2)));
526  
527 }
528  
529 # compute ∆E*00 color difference
530 # parameters: (L*a*b*_1, L*a*b*_2)
531 # returns: (∆E*00_value)
532 sub dE00 {
533  
534 # local variables
535 0 ▶ my ($dL, $C1, $C2, $Lm, $Cm, $amult, $a1p, $a2p, $C1p, $C2p, $Cmp);
Subroutine dE00: not called CC=12 SCAR=50.5
Pod documented
536 0 my ($dCp, $h1p, $h2p, $dhp, $dHp, $Hp, $T, $Sl, $Sc, $Sh, $Rt);
537  
538 # compute ∆L*
539 0 $dL = $_[3] - $_[0];
540  
541 # compute mean L*
542 0 $Lm = ($_[3] + $_[0])/2;
543  
544 # compute C1*
545 0 $C1 = sqrt($_[1]**2 + $_[2]**2);
546  
547 # compute C2*
548 0 $C2 = sqrt($_[4]**2 + $_[5]**2);
549  
550 # compute mean C*
551 0 $Cm = ($C1 + $C2)/2;
552  
553 # compute a' multiplier
554 0 $amult = (1 + (1 - sqrt($Cm**7/($Cm**7 + 6103515625)))/2);
555  
556 # compute a1'
557 0 $a1p = $_[1] * $amult;
558  
559 # compute a2'
560 0 $a2p = $_[4] * $amult;
561  
562 # compute C1'
563 0 $C1p = sqrt($a1p**2 + $_[2]**2);
564  
565 # compute C2'
566 0 $C2p = sqrt($a2p**2 + $_[5]**2);
567
568 # compute mean C'
569 0 $Cmp = ($C1p + $C2p)/2;
570
571 # compute ∆C'
572 0 $dCp = $C2p - $C1p;
573  
574 # compute h1'
575 0 ▶ $h1p = ($_[2] || $a1p) ? radian * atan2($_[2], $a1p) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[2] || $a1p ? : 0 0 0
Condition0%0/3
$_[2] or $a1p
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[2] or $a1p
$_[2] $a1p
Truth table0%0/3
A or B
A= $_[2]B= $a1p
ABresult
1X1
011
000
576  
577 # adjust h1' if negative
578 0 ▶ $h1p += 360 if ($h1p < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $h1p < 0 0 0 0
579  
580 # compute h2'
581 0 ▶ $h2p = ($_[5] || $a2p) ? radian * atan2($_[5], $a2p) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[5] || $a2p ? : 0 0 0
Condition0%0/3
$_[5] or $a2p
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[5] or $a2p
$_[5] $a2p
Truth table0%0/3
A or B
A= $_[5]B= $a2p
ABresult
1X1
011
000
582  
583 # adjust h2' if negative
584 0 ▶ $h2p += 360 if ($h2p < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $h2p < 0 0 0 0
585  
586 # abs(h1' - h2') > 180
587 0 ▶ if (abs($h1p - $h2p) > 180) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $h1p - $h2p > 180) { } 0 0 0
588
589 # if h2' > h1'
590 0 ▶ if ($h2p > $h1p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h2p > $h1p) { } 0 0 0
591
592 # compute ∆h'
593 0 $dhp = $h2p - $h1p - 360;
594
595 } else {
596
597 # compute ∆h'
598 0 $dhp = $h2p - $h1p + 360;
599
600 }
601
602 } else {
603
604 # compute ∆h'
605 0 $dhp = $h2p - $h1p;
606
607 }
608  
609 # compute ∆H'
610 0 $dHp = 2 * sqrt($C1p * $C2p) * sin($dhp/(radian * 2));
611  
612 # if C1' or C2' is zero
613 0 ▶ if ($C1p == 0 || $C2p == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($C1p == 0 or $C2p == 0) { } 0 0 0
Condition0%0/3
$C1p == 0 or $C2p == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$C1p == 0 or $C2p == 0
$C1p == 0 $C2p == 0
Truth table0%0/3
A or B
A= $C1p == 0B= $C2p == 0
ABresult
1X1
011
000
614
615 # compute H'
616 0 $Hp = $h1p + $h2p;
617
618 } else {
619
620 # if abs(h1' - h2') > 180
621 0 ▶ if (abs($h1p - $h2p) > 180) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $h1p - $h2p > 180) { } 0 0 0
622
623 # compute H'
624 0 $Hp = ($h1p + $h2p + 360)/2;
625
626 } else {
627
628 # compute H'
629 0 $Hp = ($h1p + $h2p)/2;
630
631 }
632
633 }
634  
635 # compute T
636 0 $T = 1 - 0.17 * cos(($Hp - 30)/radian) + 0.24 * cos((2 * $Hp)/radian) + 0.32 * cos((3 * $Hp + 6)/radian) - 0.20 * cos((4 * $Hp - 63)/radian);
637  
638 # compute Sl
639 0 $Sl = 1 + (0.015 * ($Lm - 50)**2)/sqrt(20 + ($Lm - 50)**2);
640  
641 # compute Sc
642 0 $Sc = 1 + 0.045 * $Cmp;
643  
644 # compute Sh
645 0 $Sh = 1 + 0.015 * $Cmp * $T;
646  
647 # compute Rt
648 0 $Rt = -2 * sqrt($Cmp**7/($Cmp**7 + 6103515625)) * sin((60 * exp(-(($Hp - 275)/25)**2))/radian);
649  
650 # return ∆E*00 difference
651 0 return(sqrt(($dL/$Sl)**2 + ($dCp/$Sc)**2 + ($dHp/$Sh)**2 + $Rt * ($dCp/$Sc) * ($dHp/$Sh)));
652  
653 }
654  
655 # compute DIN 99 color difference
656 # optional parameters are Ke (lightness) and Kch (chroma)
657 # default values of optional parameters are Ke = 1, Kch = 1
658 # parameters: (L*a*b*_1, L*a*b*_2, [Ke, Kch])
659 # returns: (DIN_99_value)
660 sub dE99 {
661  
662 # compute DIN 99 Lab for sample 1
663 0 ▶ my @Lab1 = _Lab2DIN99(@_[0 .. 2, 6, 7]);
Subroutine dE99: not called CC=1 SCAR=6.9
Pod documented
664  
665 # compute DIN 99 Lab for sample 2
666 0 my @Lab2 = _Lab2DIN99(@_[3 .. 7]);
667  
668 # return DIN 99 color difference
669 0 return(sqrt(($Lab1[0] - $Lab2[0])**2 + ($Lab1[1] - $Lab2[1])**2 +($Lab1[2] - $Lab2[2])**2));
670  
671 }
672  
673 # compute ∆Ch chroma difference (aka ∆F)
674 # parameters: (L*a*b*_1, L*a*b*_2)
675 sub dCh {
676  
677 # return chroma difference
678 0 ▶ return(sqrt(($_[1] - $_[4])**2 + ($_[2] - $_[5])**2));
Subroutine dCh: not called CC=1 SCAR=6.9
Pod documented
679  
680 }
681  
682 #--------- illuminant functions ---------
683  
684 # correlated color temperature (CCT)
685 # minimizes the UCS u,v error value
686 # the error value should be less than 5E-2
687 # parameters: (x, y)
688 # returns: (CCT, error_value)
689 sub CCT {
690  
691 # get parameters
692 0 ▶ my ($x, $y) = @_;
Subroutine CCT: not called CC=5 SCAR=34.0
Pod documented
693  
694 # local variables
695 0 my ($ut, $vt, $T, $dT, $u, $v, $err, $ux, $vx, $errx, $derv, $derv0);
696  
697 # compute target u,v values
698 0 ($ut, $vt) = xy2ucs($x, $y);
699  
700 # compute CCT using McCamy's approximation
701 0 $T = CCT2($x, $y);
702  
703 # compute current u,v values
704 0 ($u, $v) = bbuv($T);
705  
706 # compute current error
707 0 $err = sqrt(($u - $ut)**2 + ($v - $vt)**2);
708  
709 # compute delta values
710 0 ($ux, $vx) = bbuv($T + 1E-3);
711  
712 # compute delta error
713 0 $errx = sqrt(($ux - $ut)**2 + ($vx - $vt)**2);
714  
715 # compute derr/dT
716 0 $derv = ($errx - $err)/1E-3;
717  
718 # initialize delta T
719 0 ▶ $dT = $derv > 0 ? -2 : 2;
Branch: 1 branch
BranchTrueFalseTotal
$derv > 0 ? : 0 0 0
720  
721 # optimization loop
722 0 for (0 .. 30) {
723
724 # adjust T value
725 0 $T += $dT;
726
727 # compute current u,v values
728 0 ($u, $v) = bbuv($T);
729
730 # compute current error
731 0 $err = sqrt(($u - $ut)**2 + ($v - $vt)**2);
732
733 # compute delta values
734 0 ($ux, $vx) = bbuv($T + 1E-3);
735
736 # compute delta error
737 0 $errx = sqrt(($ux - $ut)**2 + ($vx - $vt)**2);
738
739 # save previous derr/dT values
740 0 $derv0 = $derv;
741
742 # compute new derr/dT
743 0 $derv = ($errx - $err)/1E-3;
744
745 # quit loop if derr/dT < 1E-9
746 0 ▶ last if (abs($derv) < 1E-9);
Branch: 1 branch
BranchTrueFalseTotal
if abs $derv < "1e-09" 0 0 0
747
748 # adjust delta T if sign of derivative changes
749 0 ▶ $dT /= -2 if (($derv > 0) ^ ($derv0 > 0));
Branch: 1 branch
BranchTrueFalseTotal
if $derv > 0 ^ $derv0 > 0 0 0 0
750
751 }
752  
753 # return CCT
754 0 return($T, $err);
755  
756 }
757  
758 # correlated color temperature (CCT)
759 # using McCamy's approximation
760 # parameters: (x, y)
761 # returns: (CCT)
762 sub CCT2 {
763  
764 # get parameters
765 0 ▶ my ($x, $y) = @_;
Subroutine CCT2: not called CC=1 SCAR=6.9
Pod documented
766  
767 # compute n
768 0 my $n = ($x - 0.3320)/($y - 0.1858);
769  
770 # return CCT
771 0 return(-449 * $n**3 + 3525 * $n**2 - 6823.3 * $n + 5520.33);
772  
773 }
774  
775 # black body radiance (Planck's law)
776 # using constants and formula per CIE 15
777 # wavelength in nm, temperature in degrees Kelvin
778 # parameters: (wavelength, temperature)
779 # returns: (radiance)
780 sub bbrad {
781  
782 # get parameters
783 0 ▶ my ($lambda, $T) = @_;
Subroutine bbrad: not called CC=1 SCAR=6.9
Pod documented
784  
785 # CIE constants
786 0 my $c1 = 3.741771E-16; # 2πhc²
787 0 my $c2 = 1.4388E-2; # hc/kB
788  
789 # convert wavelength to meters
790 0 $lambda *= 1E-9;
791  
792 # return radiance value
793 0 return($c1/(PI * $lambda**5 * (exp($c2/($lambda * $T)) - 1)));
794  
795 }
796  
797 # compute chromaticity values of black body radiator
798 # parameter: (temperature)
799 # returns: (x, y)
800 sub bbxy {
801  
802 # get temperature
803 0 ▶ my $T = shift();
Subroutine bbxy: not called CC=3 SCAR=24.8
Pod documented
804  
805 # local variables
806 0 my ($b, $X, $Y, $Z, $d);
807  
808 # load CIE color matching functions (YAML format)
809 0 state $cmf = YAML::Tiny->read(getICCPath('Data/CIE_cmfs_360-830_x_1.yml'))->[0];
810  
811 # for each wavelength (360 - 830 nm)
812 0 for my $i (0 .. 470) {
813
814 # compute black body reflectance
815 0 $b->[$i] = bbrad($i + 360, $T);
816
817 }
818  
819 # compute colorimetry
820 0 $X = dotProduct($cmf->{'CIE1931x'}, $b);
821 0 $Y = dotProduct($cmf->{'CIE1931y'}, $b);
822 0 $Z = dotProduct($cmf->{'CIE1931z'}, $b);
823  
824 # compute denominator
825 0 ▶ ($d = $X + $Y + $Z) or croak('X + Y + Z = 0 when computing chromaticity');
Branch: 1 branch
BranchTrueFalseTotal
$d = $X + $Y + $Z or croak("X + Y + Z = 0 when computing chromaticity") 0 0 0
826  
827 # return x,y
828 0 return($X/$d, $Y/$d);
829  
830 }
831  
832 # compute UCS 1960 values of black body radiator
833 # parameter: (temperature)
834 # returns: (u, v)
835 sub bbuv {
836  
837 # get temperature
838 0 ▶ my $T = shift();
Subroutine bbuv: not called CC=3 SCAR=24.8
Pod documented
839  
840 # local variables
841 0 my ($b, $X, $Y, $Z, $d);
842  
843 # load CIE color matching functions (YAML format)
844 0 state $cmf = YAML::Tiny->read(getICCPath('Data/CIE_cmfs_360-830_x_1.yml'))->[0];
845  
846 # for each wavelength (360 - 830 nm)
847 0 for my $i (0 .. 470) {
848
849 # compute black body reflectance
850 0 $b->[$i] = bbrad($i + 360, $T);
851
852 }
853  
854 # compute colorimetry
855 0 $X = dotProduct($cmf->{'CIE1931x'}, $b);
856 0 $Y = dotProduct($cmf->{'CIE1931y'}, $b);
857 0 $Z = dotProduct($cmf->{'CIE1931z'}, $b);
858  
859 # compute denominator
860 0 ▶ ($d = $X + 15 * $Y + 3 * $Z) or croak('X + 15Y + 3Z = 0 when computing UCS values');
Branch: 1 branch
BranchTrueFalseTotal
$d = $X + 15 * $Y + 3 * $Z or croak("X + 15Y + 3Z = 0 when computing UCS values") 0 0 0
861  
862 # return u,v
863 0 return(4 * $X/$d, 6 * $Y/$d);
864  
865 }
866  
867 # convert XYZ to UCS 1960
868 # used to compute color differences for CCT
869 # parameters: (X, Y, Z)
870 # returns: (u, v)
871 sub XYZ2ucs {
872  
873 # get parameters
874 0 ▶ my ($X, $Y, $Z) = @_;
Subroutine XYZ2ucs: not called CC=2 SCAR=17.9
Pod documented
875  
876 # compute denominator
877 0 ▶ (my $d = $X + 15 * $Y + 3 * $Z) or croak('X + 15Y + 3Z = 0 when computing UCS values');
Branch: 1 branch
BranchTrueFalseTotal
my $d = $X + 15 * $Y + 3 * $Z or croak("X + 15Y + 3Z = 0 when computing UCS values") 0 0 0
878  
879 # return u,v
880 0 return(4 * $X/$d, 6 * $Y/$d);
881  
882 }
883  
884 # convert chromaticity to UCS 1960
885 # used to compute color differences for CCT
886 # parameters: (x, y)
887 # returns: (u, v)
888 sub xy2ucs {
889  
890 # get parameters
891 0 ▶ my ($x, $y) = @_;
Subroutine xy2ucs: not called CC=2 SCAR=17.9
Pod documented
892  
893 # denominator
894 0 ▶ (my $d = 12 * $y - 2 * $x + 3) or croak('12Y - 2X + 3 = 0 when computing UCS values');
Branch: 1 branch
BranchTrueFalseTotal
my $d = 12 * $y - 2 * $x + 3 or croak("12Y - 2X + 3 = 0 when computing UCS values") 0 0 0
895  
896 # return u,v
897 0 return(4 * $x/$d, 6 * $y/$d);
898  
899 }
900  
901 # compute daylight SPD
902 # parameter: (color_temperature)
903 # returns: (range, SPD_vector)
904 sub daylight {
905  
906 # get parameter
907 0 ▶ my $cct = shift();
Subroutine daylight: not called CC=5 SCAR=34.0
Pod documented
908  
909 # local variables
910 0 my ($range, $spd, $xD, $yD, $M1, $M2);
911
912 # verify color temperature
913 0 ▶ ($cct >= 4000 && $cct <= 25000) or croak('CCT must be between 4000˚K and 25000˚K');
Branch: 1 branch
BranchTrueFalseTotal
$cct >= 4000 and $cct <= 25000 or croak("CCT must be between 4000\313\232K and 25000\313\232K") 0 0 0
Condition0%0/3
$cct >= 4000 and $cct <= 25000
!ll&&!rl&&r
000
MC/DC0%0/2
$cct >= 4000 and $cct <= 25000
$cct >= 4000 $cct <= 25000
Truth table0%0/3
A and B
A= $cct >= 4000B= $cct <= 25000
ABresult
0X0
100
111
914  
915 # load CIE daylight Eigenvectors (YAML format)
916 0 state $eigen = YAML::Tiny->read(getICCPath('Data/CIE_daylight_300-830_x_5.yml'))->[0];
917  
918 # set range
919 0 $range = [300, 830, 5];
920
921 # if CCT > 7000
922 0 ▶ if ($cct > 7000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cct > 7000) { } 0 0 0
923
924 # compute x value
925 0 $xD = -2.0064e9/$cct**3 + 1.9018e6/$cct**2 + 0.24748e3/$cct + 0.23704;
926
927 } else {
928
929 # compute x value
930 0 $xD = -4.6070e9/$cct**3 + 2.9678e6/$cct**2 + 0.09911e3/$cct + 0.244063;
931
932 }
933  
934 # compute y value
935 0 $yD = -3.000 * $xD**2 + 2.870 * $xD - 0.275;
936  
937 # compute M1
938 0 $M1 = (-1.3515 - 1.7703 * $xD + 5.9114 * $yD)/(0.0241 + 0.2562 * $xD - 0.7341 * $yD);
939  
940 # compute M2
941 0 $M2 = (0.0300 - 31.4424 * $xD + 30.0717 * $yD)/(0.0241 + 0.2562 * $xD - 0.7341 * $yD);
942  
943 # for each wavelength
944 0 for my $i (0 .. 106) {
945
946 # compute spectral power
947 0 $spd->[$i] = $eigen->{'CIE_S0'}->[$i] + $M1 * $eigen->{'CIE_S1'}->[$i] + $M2 * $eigen->{'CIE_S2'}->[$i];
948
949 }
950  
951 # return
952 0 return($range, $spd);
953  
954 }
955  
956 #--------- interpolation functions ---------
957  
958 # linear interpolation function
959 # interpolates and/or extrapolates equally spaced data
960 # input/output range structure: [start_nm, end_nm, increment]
961 # optional extrapolation method is 'copy' or 'linear', none returns zeros
962 # parameters: (input_vector, input_range, output_range, [extrapolation_method])
963 # returns: (output_vector)
964 sub linear {
965  
966 # get parameters
967 0 ▶ my ($vector_in, $range_in, $range_out, $ext) = @_;
Subroutine linear: not called CC=16 SCAR=56.1
Pod documented
968  
969 # local variables
970 0 my ($ix, $ox, $w, $vector_out, $t, $low);
971  
972 # verify input vector size
973 0 ▶ (($ix = $#{$vector_in}) > 0) or croak('input vector must contain two or more elements');
Branch: 1 branch
BranchTrueFalseTotal
($ix = $#{$vector_in;}) > 0 or croak("input vector must contain two or more elements") 0 0 0
974  
975 # verify input range
976 0 ▶ (abs($ix * $range_in->[2] - $range_in->[1] + $range_in->[0]) < 1E-12 && $range_in->[2] > 0) or croak('invalid input range');
Branch: 1 branch
BranchTrueFalseTotal
abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12" and $range_in->[2] > 0 or croak("invalid input range") 0 0 0
Condition0%0/3
abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12" and $range_in->[2] > 0
!ll&&!rl&&r
000
MC/DC0%0/2
abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12" and $range_in->[2] > 0
abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12" $range_in->[2] > 0
Truth table0%0/3
A and B
A= abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12"B= $range_in->[2] > 0
ABresult
0X0
100
111
977  
978 # compute output upper index from range
979 0 ▶ (($ox = round(($range_out->[1] - $range_out->[0])/$range_out->[2])) > 0) or croak('output upper index must be > 0');
Branch: 1 branch
BranchTrueFalseTotal
($ox = round(($range_out->[1] - $range_out->[0]) / $range_out->[2])) > 0 or croak("output upper index must be > 0") 0 0 0
980  
981 # verify output range
982 0 ▶ (abs($ox * $range_out->[2] - $range_out->[1] + $range_out->[0]) < 1E-12 && $range_out->[2] > 0) or croak('invalid output range');
Branch: 1 branch
BranchTrueFalseTotal
abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12" and $range_out->[2] > 0 or croak("invalid output range") 0 0 0
Condition0%0/3
abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12" and $range_out->[2] > 0
!ll&&!rl&&r
000
MC/DC0%0/2
abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12" and $range_out->[2] > 0
abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12" $range_out->[2] > 0
Truth table0%0/3
A and B
A= abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12"B= $range_out->[2] > 0
ABresult
0X0
100
111
983  
984 # for each output vector element
985 0 for my $i (0 .. $ox) {
986
987 # compute wavelength
988 0 $w = $range_out->[0] + $i * $range_out->[2];
989
990 # if wavelength < start of source
991 0 ▶ if ($w < $range_in->[0]) {
Branch: 3 branches
BranchTrueFalseTotal
if ($w < $range_in->[0]) { } 0 0 0
elsif ($w > $range_in->[1]) { } 0 0 0
elsif ($w == $range_in->[1]) { } 0 0 0
992
993 # if extrapolation defined
994 0 ▶ if (defined($ext)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ext) { } 0 0 0
995
996 # if linear extrapolation
997 0 ▶ if ($ext eq 'linear') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext eq 'linear') { } 0 0 0
998
999 # compute ratio
1000 0 $t = ($w - $range_in->[0])/$range_in->[2];
1001
1002 # extrapolate
1003 0 $vector_out->[$i] = (1 - $t) * $vector_in->[0] + $t * $vector_in->[1];
1004
1005 # if copy extrapolation
1006 } elsif ('copy') {
1007
1008 # set element to first knot
1009 0 $vector_out->[$i] = $vector_in->[0];
1010
1011 } else {
1012
1013 # error
1014 croak('invalid extrapolation type');
1015
1016 }
1017
1018 } else {
1019
1020 # set element to 0
1021 0 $vector_out->[$i] = 0;
1022
1023 }
1024
1025 # if wavelength > end of source
1026 } elsif ($w > $range_in->[1]) {
1027
1028 # if extrapolation defined
1029 0 ▶ if (defined($ext)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ext) { } 0 0 0
1030
1031 # if linear extrapolation
1032 0 ▶ if ($ext eq 'linear') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext eq 'linear') { } 0 0 0
1033
1034 # compute ratio
1035 0 $t = ($range_in->[1] - $w)/$range_in->[2];
1036
1037 # extrapolate
1038 0 $vector_out->[$i] = (1 - $t) * $vector_in->[-1] + $t * $vector_in->[-2];
1039
1040 # if copy extrapolation
1041 } elsif ('copy') {
1042
1043 # set element to last knot
1044 0 $vector_out->[$i] = $vector_in->[-1];
1045
1046 } else {
1047
1048 # error
1049 croak('invalid extrapolation type');
1050
1051 }
1052
1053 } else {
1054
1055 # set element to 0
1056 0 $vector_out->[$i] = 0;
1057
1058 }
1059
1060 # if wavelength == end of source
1061 } elsif ($w == $range_in->[1]) {
1062
1063 # set element to last knot
1064 0 $vector_out->[$i] = $vector_in->[-1];
1065
1066 } else {
1067
1068 # compute ratio and lower index
1069 0 ($t, $low) = POSIX::modf(($w - $range_in->[0])/$range_in->[2]);
1070
1071 # if ratio is non-zero
1072 0 ▶ if ($t) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t) { } 0 0 0
1073
1074 # interpolate value
1075 0 $vector_out->[$i] = (1 - $t) * $vector_in->[$low] + $t * $vector_in->[$low + 1];
1076
1077 } else {
1078
1079 # use low source value
1080 0 $vector_out->[$i] = $vector_in->[$low];
1081
1082 }
1083
1084 }
1085
1086 }
1087  
1088 # return
1089 0 return($vector_out);
1090  
1091 }
1092  
1093 # compute linear interpolation matrix
1094 # input/output range structure: [start_nm, end_nm, increment]
1095 # optional extrapolation method is 'copy' or 'linear', none returns zeros
1096 # parameters: (input_range, output_range, [extrapolation_method])
1097 # returns: (interpolation_matrix)
1098 sub linear_matrix {
1099  
1100 # get parameters
1101 0 ▶ my ($range_in, $range_out, $ext) = @_;
Subroutine linear_matrix: not called CC=16 SCAR=56.1
Pod documented
1102  
1103 # local variables
1104 0 my ($ix, $ox, $mat, $w, $low, $t);
1105  
1106 # check if ICC::Support::Lapack module is loaded
1107 0 state $lapack = defined($INC{'ICC/Support/Lapack.pm'});
1108  
1109 # compute input vector size from range
1110 0 $ix = round(($range_in->[1] - $range_in->[0])/$range_in->[2]);
1111  
1112 # verify input range
1113 0 ▶ ($ix > 0 && abs($ix * $range_in->[2] - $range_in->[1] + $range_in->[0]) < 1E-12 && $range_in->[2] > 0) or croak('invalid input range');
Branch: 1 branch
BranchTrueFalseTotal
$ix > 0 and abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12" and $range_in->[2] > 0 or croak("invalid input range") 0 0 0
Condition0%0/6
$ix > 0 and abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12" and $range_in->[2] > 0
!ll&&!rl&&r
000
$ix > 0 and abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12"
!ll&&!rl&&r
000
MC/DC0%0/3
$ix > 0 and abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12" and $range_in->[2] > 0
$ix > 0 abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12" $range_in->[2] > 0
Truth table0%0/4
A and B and C
A= $ix > 0B= abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12"C= $range_in->[2] > 0
ABCresult
0XX0
10X0
1100
1111
1114  
1115 # compute output vector size from range
1116 0 $ox = round(($range_out->[1] - $range_out->[0])/$range_out->[2]);
1117  
1118 # verify output range
1119 0 ▶ ($ox > 0 && abs($ox * $range_out->[2] - $range_out->[1] + $range_out->[0]) < 1E-12 && $range_out->[2] > 0) or croak('invalid output range');
Branch: 1 branch
BranchTrueFalseTotal
$ox > 0 and abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12" and $range_out->[2] > 0 or croak("invalid output range") 0 0 0
Condition0%0/6
$ox > 0 and abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12" and $range_out->[2] > 0
!ll&&!rl&&r
000
$ox > 0 and abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12"
!ll&&!rl&&r
000
MC/DC0%0/3
$ox > 0 and abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12" and $range_out->[2] > 0
$ox > 0 abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12" $range_out->[2] > 0
Truth table0%0/4
A and B and C
A= $ox > 0B= abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12"C= $range_out->[2] > 0
ABCresult
0XX0
10X0
1100
1111
1120  
1121 # if ICC::Support::Lapack module is loaded
1122 0 ▶ if ($lapack) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lapack) { } 0 0 0
1123
1124 # make matrix of zeros
1125 0 $mat = ICC::Support::Lapack::zeros($ox + 1, $ix + 1);
1126
1127 } else {
1128
1129 # make matrix of zeros
1130 0 $mat = [map {[(0) x ($ix + 1)]} (0 .. $ox)];
1131
1132 }
1133  
1134 # for each output
1135 0 for my $i (0 .. $ox) {
1136
1137 # compute wavelength
1138 0 $w = $range_out->[0] + $i * $range_out->[2];
1139
1140 # if wavelength < start of source
1141 0 ▶ if ($w < $range_in->[0]) {
Branch: 3 branches
BranchTrueFalseTotal
if ($w < $range_in->[0]) { } 0 0 0
elsif ($w > $range_in->[1]) { } 0 0 0
elsif ($w == $range_in->[1]) { } 0 0 0
1142
1143 # if extrapolation defined
1144 0 ▶ if (defined($ext)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ext) 0 0 0
1145
1146 # if linear extrapolation
1147 0 ▶ if ($ext eq 'linear') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext eq 'linear') { } 0 0 0
1148
1149 # compute ratio
1150 0 $t = ($w - $range_in->[0])/$range_in->[2];
1151
1152 # set elements to ratio
1153 0 $mat->[$i][0] = 1 - $t;
1154 0 $mat->[$i][1] = $t;
1155
1156 # if copy extrapolation
1157 } elsif ('copy') {
1158
1159 # set element to first knot
1160 0 $mat->[$i][0] = 1;
1161
1162 } else {
1163
1164 # error
1165 croak('invalid extrapolation type');
1166
1167 }
1168
1169 }
1170
1171 # if wavelength > end of source
1172 } elsif ($w > $range_in->[1]) {
1173
1174 # if extrapolation defined
1175 0 ▶ if (defined($ext)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ext) 0 0 0
1176
1177 # if linear extrapolation
1178 0 ▶ if ($ext eq 'linear') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext eq 'linear') { } 0 0 0
1179
1180 # compute ratio
1181 0 $t = ($range_in->[1] - $w)/$range_in->[2];
1182
1183 # set elements to ratio
1184 0 $mat->[$i][$ix - 1] = $t;
1185 0 $mat->[$i][$ix] = 1 - $t;
1186
1187 # if copy extrapolation
1188 } elsif ('copy') {
1189
1190 # set element to last knot
1191 0 $mat->[$i][$ix] = 1;
1192
1193 } else {
1194
1195 # error
1196 croak('invalid extrapolation type');
1197
1198 }
1199
1200 }
1201
1202 # if wavelength == end of source
1203 } elsif ($w == $range_in->[1]) {
1204
1205 # set element to last knot
1206 0 $mat->[$i][$ix] = 1;
1207
1208 } else {
1209
1210 # compute ratio and lower index
1211 0 ($t, $low) = POSIX::modf(($w - $range_in->[0])/$range_in->[2]);
1212
1213 # set elements to ratio
1214 0 $mat->[$i][$low + 1] = $t;
1215 0 $mat->[$i][$low] = 1 - $t;
1216
1217 }
1218
1219 }
1220  
1221 # return
1222 0 return(bless($mat, 'Math::Matrix'));
1223  
1224 }
1225  
1226 # cubic spline interpolation function
1227 # interpolates and/or extrapolates equally spaced data
1228 # input/output range structure: [start_nm, end_nm, increment]
1229 # optional extrapolation method is 'copy' or 'linear', none returns zeros
1230 # parameters: (input_vector, input_range, output_range, [extrapolation_method])
1231 # returns: (output_vector)
1232 sub cspline {
1233  
1234 # get parameters
1235 0 ▶ my ($vector_in, $range_in, $range_out, $ext) = @_;
Subroutine cspline: not called CC=24 SCAR=64.0
Pod documented
1236  
1237 # local variables
1238 0 my ($ix, $ox, $mat, $derv, $info, $w, $vector_out);
1239 0 my ($low, $t, $tc, $h00, $h01, $h10, $h11);
1240  
1241 # verify input vector size
1242 0 ▶ (($ix = $#{$vector_in}) > 0) or croak('input vector must contain two or more elements');
Branch: 1 branch
BranchTrueFalseTotal
($ix = $#{$vector_in;}) > 0 or croak("input vector must contain two or more elements") 0 0 0
1243  
1244 # verify input range
1245 0 ▶ (abs($ix * $range_in->[2] - $range_in->[1] + $range_in->[0]) < 1E-12 && $range_in->[2] > 0) or croak('invalid input range');
Branch: 1 branch
BranchTrueFalseTotal
abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12" and $range_in->[2] > 0 or croak("invalid input range") 0 0 0
Condition0%0/3
abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12" and $range_in->[2] > 0
!ll&&!rl&&r
000
MC/DC0%0/2
abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12" and $range_in->[2] > 0
abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12" $range_in->[2] > 0
Truth table0%0/3
A and B
A= abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12"B= $range_in->[2] > 0
ABresult
0X0
100
111
1246  
1247 # compute output upper index from range
1248 0 ▶ (($ox = round(($range_out->[1] - $range_out->[0])/$range_out->[2])) > 0) or croak('output upper index must be > 0');
Branch: 1 branch
BranchTrueFalseTotal
($ox = round(($range_out->[1] - $range_out->[0]) / $range_out->[2])) > 0 or croak("output upper index must be > 0") 0 0 0
1249  
1250 # verify output range
1251 0 ▶ (abs($ox * $range_out->[2] - $range_out->[1] + $range_out->[0]) < 1E-12 && $range_out->[2] > 0) or croak('invalid output range');
Branch: 1 branch
BranchTrueFalseTotal
abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12" and $range_out->[2] > 0 or croak("invalid output range") 0 0 0
Condition0%0/3
abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12" and $range_out->[2] > 0
!ll&&!rl&&r
000
MC/DC0%0/2
abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12" and $range_out->[2] > 0
abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12" $range_out->[2] > 0
Truth table0%0/3
A and B
A= abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12"B= $range_out->[2] > 0
ABresult
0X0
100
111
1252  
1253 # check if ICC::Support::Lapack module is loaded
1254 0 state $lapack = defined($INC{'ICC/Support/Lapack.pm'});
1255  
1256 # if ICC::Support::Lapack module is loaded
1257 0 ▶ if ($lapack) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lapack) { } 0 0 0
1258
1259 # for each input element
1260 0 for my $i (0 .. $ix) {
1261
1262 # compute rhs (3 * (y[i+1] - y[i-1]))
1263 0 ▶ $mat->[$i][0] = 3 * ($vector_in->[$i + 1 > $ix ? $ix : $i + 1] - $vector_in->[$i - 1 < 0 ? 0 : $i - 1]);
Branch: 2 branches
BranchTrueFalseTotal
$i + 1 > $ix ? : 0 0 0
$i - 1 < 0 ? : 0 0 0
1264
1265 }
1266
1267 # solve for derivative matrix
1268 0 ($info, $derv) = ICC::Support::Lapack::trisolve([(1) x $ix], [2, (4) x ($ix - 1), 2], [(1) x $ix], $mat);
1269
1270 # otherwise, use Math::Matrix package (slow)
1271 } else {
1272
1273 # make tri-diagonal matrix object
1274 0 $mat = Math::Matrix->tridiagonal([2, (4) x ($ix - 1), 2]);
1275
1276 # for each input element
1277 0 for my $i (0 .. $ix) {
1278
1279 # append rhs (3 * (y[i+1] - y[i-1]))
1280 0 ▶ $mat->[$i][$ix + 1] = 3 * ($vector_in->[$i + 1 > $ix ? $ix : $i + 1] - $vector_in->[$i - 1 < 0 ? 0 : $i - 1]);
Branch: 2 branches
BranchTrueFalseTotal
$i + 1 > $ix ? : 0 0 0
$i - 1 < 0 ? : 0 0 0
1281
1282 }
1283
1284 # solve for derivative matrix
1285 0 $derv = $mat->solve();
1286
1287 }
1288  
1289 # for each output vector element
1290 0 for my $i (0 .. $ox) {
1291
1292 # compute wavelength
1293 0 $w = $range_out->[0] + $i * $range_out->[2];
1294
1295 # if wavelength < start of source
1296 0 ▶ if ($w < $range_in->[0]) {
Branch: 3 branches
BranchTrueFalseTotal
if ($w < $range_in->[0]) { } 0 0 0
elsif ($w > $range_in->[1]) { } 0 0 0
elsif ($w == $range_in->[1]) { } 0 0 0
1297
1298 # if extrapolation defined
1299 0 ▶ if (defined($ext)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ext) { } 0 0 0
1300
1301 # if linear extrapolation
1302 0 ▶ if ($ext eq 'linear') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext eq 'linear') { } 0 0 0
1303
1304 # set element to linear value
1305 0 $vector_out->[$i] = $vector_in->[0] + $derv->[0][0] * ($w - $range_in->[0])/$range_in->[2];
1306
1307 # if copy extrapolation
1308 } elsif ('copy') {
1309
1310 # set element to first knot
1311 0 $vector_out->[$i] = $vector_in->[0];
1312
1313 } else {
1314
1315 # error
1316 croak('invalid extrapolation type');
1317
1318 }
1319
1320 } else {
1321
1322 # set element to 0
1323 0 $vector_out->[$i] = 0;
1324
1325 }
1326
1327 # if wavelength > end of source
1328 } elsif ($w > $range_in->[1]) {
1329
1330 # if extrapolation defined
1331 0 ▶ if (defined($ext)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ext) { } 0 0 0
1332
1333 # if linear extrapolation
1334 0 ▶ if ($ext eq 'linear') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext eq 'linear') { } 0 0 0
1335
1336 # set element to linear value
1337 0 $vector_out->[$i] = $vector_in->[-1] + $derv->[-1][0] * ($w - $range_in->[1])/$range_in->[2];
1338
1339 # if copy extrapolation
1340 } elsif ('copy') {
1341
1342 # set element to last knot
1343 0 $vector_out->[$i] = $vector_in->[-1];
1344
1345 } else {
1346
1347 # error
1348 croak('invalid extrapolation type');
1349
1350 }
1351
1352 } else {
1353
1354 # set element to 0
1355 0 $vector_out->[$i] = 0;
1356
1357 }
1358
1359 # if wavelength == end of source
1360 } elsif ($w == $range_in->[1]) {
1361
1362 # set element to last knot
1363 0 $vector_out->[$i] = $vector_in->[-1];
1364
1365 } else {
1366
1367 # compute ratio and lower index
1368 0 ($t, $low) = POSIX::modf(($w - $range_in->[0])/$range_in->[2]);
1369
1370 # if ratio is non-zero
1371 0 ▶ if ($t) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t) { } 0 0 0
1372
1373 # if ICC::Support::Lapack module is loaded
1374 0 ▶ if ($lapack) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lapack) { } 0 0 0
1375
1376 # interpolate value
1377 0 $vector_out->[$i] = ICC::Support::Lapack::hermite($t, $vector_in->[$low], $vector_in->[$low + 1], $derv->[$low][0], $derv->[$low + 1][0]);
1378
1379 } else {
1380
1381 # compute Hermite coefficients
1382 0 $tc = 1 - $t;
1383 0 $h00 = (1 + 2 * $t) * $tc * $tc;
1384 0 $h01 = 1 - $h00;
1385 0 $h10 = $t * $tc * $tc;
1386 0 $h11 = -$t * $t * $tc;
1387
1388 # interpolate value
1389 0 $vector_out->[$i] = $h00 * $vector_in->[$low] + $h01 * $vector_in->[$low + 1] + $h10 * $derv->[$low][0] + $h11 * $derv->[$low + 1][0];
1390
1391 }
1392
1393 } else {
1394
1395 # use lower source value
1396 0 $vector_out->[$i] = $vector_in->[$low];
1397
1398 }
1399
1400 }
1401
1402 }
1403  
1404 # return
1405 0 return($vector_out);
1406  
1407 }
1408  
1409 # compute cubic spline interpolation matrix
1410 # input/output range structure: [start_nm, end_nm, increment]
1411 # optional extrapolation method is 'copy' or 'linear', none returns zeros
1412 # parameters: (input_range, output_range, [extrapolation_method])
1413 # returns: (interpolation_matrix)
1414 sub cspline_matrix {
1415  
1416 # get parameters
1417 0 ▶ my ($range_in, $range_out, $ext) = @_;
Subroutine cspline_matrix: not called CC=30 SCAR=68.4
Pod documented
1418  
1419 # local variables
1420 0 my ($ix, $ox, $rhs, $info, $derv, $mat);
1421 0 my ($w, $low, $t, $tc, $h00, $h01, $h10, $h11);
1422  
1423 # check if ICC::Support::Lapack module is loaded
1424 0 state $lapack = defined($INC{'ICC/Support/Lapack.pm'});
1425  
1426 # compute input vector size from range
1427 0 $ix = round(($range_in->[1] - $range_in->[0])/$range_in->[2]);
1428  
1429 # verify input range
1430 0 ▶ ($ix > 0 && abs($ix * $range_in->[2] - $range_in->[1] + $range_in->[0]) < 1E-12 && $range_in->[2] > 0) or croak('invalid input range');
Branch: 1 branch
BranchTrueFalseTotal
$ix > 0 and abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12" and $range_in->[2] > 0 or croak("invalid input range") 0 0 0
Condition0%0/6
$ix > 0 and abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12" and $range_in->[2] > 0
!ll&&!rl&&r
000
$ix > 0 and abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12"
!ll&&!rl&&r
000
MC/DC0%0/3
$ix > 0 and abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12" and $range_in->[2] > 0
$ix > 0 abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12" $range_in->[2] > 0
Truth table0%0/4
A and B and C
A= $ix > 0B= abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12"C= $range_in->[2] > 0
ABCresult
0XX0
10X0
1100
1111
1431  
1432 # compute output vector size from range
1433 0 $ox = round(($range_out->[1] - $range_out->[0])/$range_out->[2]);
1434  
1435 # verify output range
1436 0 ▶ ($ox > 0 && abs($ox * $range_out->[2] - $range_out->[1] + $range_out->[0]) < 1E-12 && $range_out->[2] > 0) or croak('invalid output range');
Branch: 1 branch
BranchTrueFalseTotal
$ox > 0 and abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12" and $range_out->[2] > 0 or croak("invalid output range") 0 0 0
Condition0%0/6
$ox > 0 and abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12" and $range_out->[2] > 0
!ll&&!rl&&r
000
$ox > 0 and abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12"
!ll&&!rl&&r
000
MC/DC0%0/3
$ox > 0 and abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12" and $range_out->[2] > 0
$ox > 0 abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12" $range_out->[2] > 0
Truth table0%0/4
A and B and C
A= $ox > 0B= abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12"C= $range_out->[2] > 0
ABCresult
0XX0
10X0
1100
1111
1437  
1438 # if ICC::Support::Lapack module is loaded
1439 0 ▶ if ($lapack) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lapack) { } 0 0 0
1440
1441 # make rhs matrix (filled with zeros)
1442 0 $rhs = ICC::Support::Lapack::zeros($ix + 1);
1443
1444 # for each row
1445 0 for my $i (1 .. $ix - 1) {
1446
1447 # set diagonal values
1448 0 $rhs->[$i - 1][$i] = 3;
1449 0 $rhs->[$i + 1][$i] = -3;
1450
1451 }
1452
1453 # set endpoint values
1454 0 $rhs->[0][0] = -3;
1455 0 $rhs->[1][0] = -3;
1456 0 $rhs->[$ix][$ix] = 3;
1457 0 $rhs->[$ix - 1][$ix] = 3;
1458
1459 # solve for derivative matrix
1460 0 ($info, $derv) = ICC::Support::Lapack::trisolve([(1) x $ix], [2, (4) x ($ix - 1), 2], [(1) x $ix], $rhs);
1461
1462 # make matrix of zeros
1463 0 $mat = ICC::Support::Lapack::zeros($ox + 1, $ix + 1);
1464
1465 # otherwise, use Math::Matrix package (slow)
1466 } else {
1467
1468 # make rhs matrix (fill with zeros)
1469 0 $rhs = bless([map {[(0) x ($ix + 1)]} (0 .. $ix)], 'Math::Matrix');
1470
1471 # for each row
1472 0 for my $i (1 .. $ix - 1) {
1473
1474 # set diagonal values
1475 0 $rhs->[$i - 1][$i] = 3;
1476 0 $rhs->[$i + 1][$i] = -3;
1477
1478 }
1479
1480 # set endpoint values
1481 0 $rhs->[0][0] = -3;
1482 0 $rhs->[1][0] = -3;
1483 0 $rhs->[$ix][$ix] = 3;
1484 0 $rhs->[$ix - 1][$ix] = 3;
1485
1486 # solve for derivative matrix
1487 0 $derv = Math::Matrix->tridiagonal([2, (4) x ($ix - 1), 2])->concat($rhs)->solve();
1488
1489 # make matrix of zeros
1490 0 $mat = [map {[(0) x ($ix + 1)]} (0 .. $ox)];
1491
1492 }
1493  
1494 # for each output
1495 0 for my $i (0 .. $ox) {
1496
1497 # compute wavelength
1498 0 $w = $range_out->[0] + $i * $range_out->[2];
1499
1500 # if wavelength < start of source
1501 0 ▶ if ($w < $range_in->[0]) {
Branch: 3 branches
BranchTrueFalseTotal
if ($w < $range_in->[0]) { } 0 0 0
elsif ($w > $range_in->[1]) { } 0 0 0
elsif ($w == $range_in->[1]) { } 0 0 0
1502
1503 # if extrapolation defined
1504 0 ▶ if (defined($ext)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ext) 0 0 0
1505
1506 # if linear extrapolation
1507 0 ▶ if ($ext eq 'linear') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext eq 'linear') { } 0 0 0
1508
1509 # compute ratio
1510 0 $t = ($w - $range_in->[0])/$range_in->[2];
1511
1512 # for each input
1513 0 for my $j (0 .. $ix) {
1514
1515 # set element to linear value
1516 0 ▶ $mat->[$i][$j] = ($j == 0 ? 1 : 0) + $derv->[0][$j] * $t;
Branch: 1 branch
BranchTrueFalseTotal
$j == 0 ? : 0 0 0
1517
1518 }
1519
1520 # if copy extrapolation
1521 } elsif ('copy') {
1522
1523 # set element to first knot
1524 0 $mat->[$i][0] = 1;
1525
1526 } else {
1527
1528 # error
1529 croak('invalid extrapolation type');
1530
1531 }
1532
1533 }
1534
1535 # if wavelength > end of source
1536 } elsif ($w > $range_in->[1]) {
1537
1538 # if extrapolation defined
1539 0 ▶ if (defined($ext)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ext) 0 0 0
1540
1541 # if linear extrapolation
1542 0 ▶ if ($ext eq 'linear') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext eq 'linear') { } 0 0 0
1543
1544 # compute ratio
1545 0 $t = ($w - $range_in->[1])/$range_in->[2];
1546
1547 # for each input
1548 0 for my $j (0 .. $ix) {
1549
1550 # set element to linear value
1551 0 ▶ $mat->[$i][$j] = ($j == $ix ? 1 : 0) + $derv->[-1][$j] * $t;
Branch: 1 branch
BranchTrueFalseTotal
$j == $ix ? : 0 0 0
1552
1553 }
1554
1555 # if copy extrapolation
1556 } elsif ('copy') {
1557
1558 # set element to last knot
1559 0 $mat->[$i][$ix] = 1;
1560
1561 } else {
1562
1563 # error
1564 croak('invalid extrapolation type');
1565
1566 }
1567
1568 }
1569
1570 # if wavelength == end of source
1571 } elsif ($w == $range_in->[1]) {
1572
1573 # set element to last knot
1574 0 $mat->[$i][$ix] = 1;
1575
1576 } else {
1577
1578 # compute ratio and lower index
1579 0 ($t, $low) = POSIX::modf(($w - $range_in->[0])/$range_in->[2]);
1580
1581 # if ratio is non-zero
1582 0 ▶ if ($t) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t) { } 0 0 0
1583
1584 # if ICC::Support::Lapack module is loaded
1585 0 ▶ if ($lapack) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lapack) { } 0 0 0
1586
1587 # for each input
1588 0 for my $j (0 .. $ix) {
1589
1590 # interpolate value
1591 0 ▶ $mat->[$i][$j] = ICC::Support::Lapack::hermite($t, ($low == $j ? 1 : 0), ($low + 1 == $j ? 1 : 0), $derv->[$low][$j], $derv->[$low + 1][$j]);
Branch: 2 branches
BranchTrueFalseTotal
$low == $j ? : 0 0 0
$low + 1 == $j ? : 0 0 0
1592
1593 }
1594
1595 } else {
1596
1597 # for each input
1598 0 for my $j (0 .. $ix) {
1599
1600 # compute Hermite coefficients
1601 0 $tc = 1 - $t;
1602 0 $h00 = (1 + 2 * $t) * $tc * $tc;
1603 0 $h01 = 1 - $h00;
1604 0 $h10 = $t * $tc * $tc;
1605 0 $h11 = -$t * $t * $tc;
1606
1607 # interpolate partial derivative value
1608 0 ▶ $mat->[$i][$j] = $h00 * ($low == $j ? 1 : 0) + $h01 * ($low + 1 == $j ? 1 : 0) + $h10 * $derv->[$low][$j] + $h11 * $derv->[$low + 1][$j];
Branch: 2 branches
BranchTrueFalseTotal
$low == $j ? : 0 0 0
$low + 1 == $j ? : 0 0 0
1609
1610 }
1611
1612 }
1613
1614 } else {
1615
1616 # set to one
1617 0 $mat->[$i][$low] = 1;
1618
1619 }
1620
1621 }
1622
1623 }
1624  
1625 # return
1626 0 return(bless($mat, 'Math::Matrix'));
1627  
1628 }
1629  
1630 # Lagrange interpolation function (ASTM E 2022)
1631 # interpolates and/or extrapolates equally spaced data
1632 # input/output range structure: [start_nm, end_nm, increment]
1633 # optional extrapolation method is 'copy' or 'linear', none returns zeros
1634 # parameters: (input_vector, input_range, output_range, [extrapolation_method])
1635 # returns: (output_vector)
1636 sub lagrange {
1637  
1638 # get parameters
1639 0 ▶ my ($vector_in, $range_in, $range_out, $ext) = @_;
Subroutine lagrange: not called CC=18 SCAR=58.3
Pod documented
1640  
1641 # local variables
1642 0 my ($ix, $ox, $w, $vector_out, $t, $low);
1643  
1644 # verify input vector size
1645 0 ▶ (($ix = $#{$vector_in}) > 0) or croak('input vector must contain two or more elements');
Branch: 1 branch
BranchTrueFalseTotal
($ix = $#{$vector_in;}) > 0 or croak("input vector must contain two or more elements") 0 0 0
1646  
1647 # verify input range
1648 0 ▶ (abs($ix * $range_in->[2] - $range_in->[1] + $range_in->[0]) < 1E-12 && $range_in->[2] > 0) or croak('invalid input range');
Branch: 1 branch
BranchTrueFalseTotal
abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12" and $range_in->[2] > 0 or croak("invalid input range") 0 0 0
Condition0%0/3
abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12" and $range_in->[2] > 0
!ll&&!rl&&r
000
MC/DC0%0/2
abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12" and $range_in->[2] > 0
abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12" $range_in->[2] > 0
Truth table0%0/3
A and B
A= abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12"B= $range_in->[2] > 0
ABresult
0X0
100
111
1649  
1650 # compute output upper index from range
1651 0 ▶ (($ox = round(($range_out->[1] - $range_out->[0])/$range_out->[2])) > 0) or croak('output upper index must be > 0');
Branch: 1 branch
BranchTrueFalseTotal
($ox = round(($range_out->[1] - $range_out->[0]) / $range_out->[2])) > 0 or croak("output upper index must be > 0") 0 0 0
1652  
1653 # verify output range
1654 0 ▶ (abs($ox * $range_out->[2] - $range_out->[1] + $range_out->[0]) < 1E-12 && $range_out->[2] > 0) or croak('invalid output range');
Branch: 1 branch
BranchTrueFalseTotal
abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12" and $range_out->[2] > 0 or croak("invalid output range") 0 0 0
Condition0%0/3
abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12" and $range_out->[2] > 0
!ll&&!rl&&r
000
MC/DC0%0/2
abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12" and $range_out->[2] > 0
abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12" $range_out->[2] > 0
Truth table0%0/3
A and B
A= abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12"B= $range_out->[2] > 0
ABresult
0X0
100
111
1655  
1656 # for each output vector element
1657 0 for my $i (0 .. $ox) {
1658
1659 # compute wavelength
1660 0 $w = $range_out->[0] + $i * $range_out->[2];
1661
1662 # if wavelength < start of source
1663 0 ▶ if ($w < $range_in->[0]) {
Branch: 3 branches
BranchTrueFalseTotal
if ($w < $range_in->[0]) { } 0 0 0
elsif ($w > $range_in->[1]) { } 0 0 0
elsif ($w == $range_in->[1]) { } 0 0 0
1664
1665 # if extrapolation defined
1666 0 ▶ if (defined($ext)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ext) { } 0 0 0
1667
1668 # if linear extrapolation
1669 0 ▶ if ($ext eq 'linear') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext eq 'linear') { } 0 0 0
1670
1671 # compute ratio
1672 0 $t = ($range_in->[0] - $w)/$range_in->[2];
1673
1674 # compute extrapolated value
1675 0 $vector_out->[$i] = (1 + 3 * $t/2) * $vector_in->[0];
1676 0 $vector_out->[$i] += -2 * $t * $vector_in->[1];
1677 0 $vector_out->[$i] += $t * $vector_in->[2]/2;
1678
1679 # if copy extrapolation
1680 } elsif ('copy') {
1681
1682 # set element to first knot
1683 0 $vector_out->[$i] = $vector_in->[0];
1684
1685 } else {
1686
1687 # error
1688 croak('invalid extrapolation type');
1689
1690 }
1691
1692 } else {
1693
1694 # set element to 0
1695 0 $vector_out->[$i] = 0;
1696
1697 }
1698
1699 # if wavelength > end of source
1700 } elsif ($w > $range_in->[1]) {
1701
1702 # if extrapolation defined
1703 0 ▶ if (defined($ext)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ext) { } 0 0 0
1704
1705 # if linear extrapolation
1706 0 ▶ if ($ext eq 'linear') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext eq 'linear') { } 0 0 0
1707
1708 # compute ratio
1709 0 $t = ($w - $range_in->[1])/$range_in->[2];
1710
1711 # compute extrapolated value
1712 0 $vector_out->[$i] = (1 + 3 * $t/2) * $vector_in->[$ix];
1713 0 $vector_out->[$i] += -2 * $t * $vector_in->[$ix - 1];
1714 0 $vector_out->[$i] += $t * $vector_in->[$ix - 2]/2;
1715
1716 # if copy extrapolation
1717 } elsif ('copy') {
1718
1719 # set element to last knot
1720 0 $vector_out->[$i] = $vector_in->[-1];
1721
1722 } else {
1723
1724 # error
1725 croak('invalid extrapolation type');
1726
1727 }
1728
1729 } else {
1730
1731 # set element to 0
1732 0 $vector_out->[$i] = 0;
1733
1734 }
1735
1736 # if wavelength == end of source
1737 } elsif ($w == $range_in->[1]) {
1738
1739 # set element to last knot
1740 0 $vector_out->[$i] = $vector_in->[-1];
1741
1742 } else {
1743
1744 # compute ratio and lower index
1745 0 ($t, $low) = POSIX::modf(($w - $range_in->[0])/$range_in->[2]);
1746
1747 # if ratio is zero
1748 0 ▶ if ($t == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($t == 0) { } 0 0 0
elsif ($low == 0) { } 0 0 0
elsif ($low == $ix - 1) { } 0 0 0
1749
1750 # set element to low knot
1751 0 $vector_out->[$i] = $vector_in->[$low];
1752
1753 # quadratic Lagrange interpolation (lower)
1754 } elsif ($low == 0) {
1755
1756 # compute Lagrange quadratic interpolated value
1757 0 $vector_out->[$i] = ($t - 1) * ($t - 2) * $vector_in->[0]/2;
1758 0 $vector_out->[$i] += -$t * ($t - 2) * $vector_in->[1];
1759 0 $vector_out->[$i] += ($t - 1) * $t * $vector_in->[2]/2;
1760
1761 # quadratic Lagrange interpolation (upper)
1762 } elsif ($low == $ix - 1) {
1763
1764 # complement ratio
1765 0 $t = 1 - $t;
1766
1767 # compute Lagrange quadratic interpolated value
1768 0 $vector_out->[$i] = ($t - 1) * ($t - 2) * $vector_in->[$ix]/2;
1769 0 $vector_out->[$i] += -$t * ($t - 2) * $vector_in->[$ix - 1];
1770 0 $vector_out->[$i] += ($t - 1) * $t * $vector_in->[$ix - 2]/2;
1771
1772 # cubic Lagrange interpolation
1773 } else {
1774
1775 # increment ratio
1776 0 $t = 1 + $t;
1777
1778 # compute Lagrange cubic interpolated value
1779 0 $vector_out->[$i] = ($t - 1) * ($t - 2) * ($t - 3) * $vector_in->[$low - 1]/-6;
1780 0 $vector_out->[$i] += $t * ($t - 2) * ($t - 3) * $vector_in->[$low]/2;
1781 0 $vector_out->[$i] += ($t - 1) * $t * ($t - 3) * $vector_in->[$low + 1]/-2;
1782 0 $vector_out->[$i] += ($t - 1) * ($t - 2) * $t * $vector_in->[$low + 2]/6;
1783
1784 }
1785
1786 }
1787
1788 }
1789  
1790 # return
1791 0 return($vector_out);
1792  
1793 }
1794  
1795 # compute Lagrange interpolation matrix (ASTM E 2022)
1796 # input/output range structure: [start_nm, end_nm, increment]
1797 # optional extrapolation method is 'copy' or 'linear', none returns zeros
1798 # parameters: (input_range, output_range, [extrapolation_method])
1799 # returns: (interpolation_matrix)
1800 sub lagrange_matrix {
1801  
1802 # get parameters
1803 0 ▶ my ($range_in, $range_out, $ext) = @_;
Subroutine lagrange_matrix: not called CC=19 SCAR=59.4
Pod documented
1804  
1805 # local variables
1806 0 my ($ix, $ox, $mat, $w, $low, $t);
1807  
1808 # check if ICC::Support::Lapack module is loaded
1809 0 state $lapack = defined($INC{'ICC/Support/Lapack.pm'});
1810  
1811 # compute input vector size from range
1812 0 $ix = round(($range_in->[1] - $range_in->[0])/$range_in->[2]);
1813  
1814 # verify input range
1815 0 ▶ ($ix > 0 && abs($ix * $range_in->[2] - $range_in->[1] + $range_in->[0]) < 1E-12 && $range_in->[2] > 0) or croak('invalid input range');
Branch: 1 branch
BranchTrueFalseTotal
$ix > 0 and abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12" and $range_in->[2] > 0 or croak("invalid input range") 0 0 0
Condition0%0/6
$ix > 0 and abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12" and $range_in->[2] > 0
!ll&&!rl&&r
000
$ix > 0 and abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12"
!ll&&!rl&&r
000
MC/DC0%0/3
$ix > 0 and abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12" and $range_in->[2] > 0
$ix > 0 abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12" $range_in->[2] > 0
Truth table0%0/4
A and B and C
A= $ix > 0B= abs $ix * $range_in->[2] - $range_in->[1] + $range_in->[0] < "1e-12"C= $range_in->[2] > 0
ABCresult
0XX0
10X0
1100
1111
1816  
1817 # compute output vector size from range
1818 0 $ox = round(($range_out->[1] - $range_out->[0])/$range_out->[2]);
1819  
1820 # verify output range
1821 0 ▶ ($ox > 0 && abs($ox * $range_out->[2] - $range_out->[1] + $range_out->[0]) < 1E-12 && $range_out->[2] > 0) or croak('invalid output range');
Branch: 1 branch
BranchTrueFalseTotal
$ox > 0 and abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12" and $range_out->[2] > 0 or croak("invalid output range") 0 0 0
Condition0%0/6
$ox > 0 and abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12" and $range_out->[2] > 0
!ll&&!rl&&r
000
$ox > 0 and abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12"
!ll&&!rl&&r
000
MC/DC0%0/3
$ox > 0 and abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12" and $range_out->[2] > 0
$ox > 0 abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12" $range_out->[2] > 0
Truth table0%0/4
A and B and C
A= $ox > 0B= abs $ox * $range_out->[2] - $range_out->[1] + $range_out->[0] < "1e-12"C= $range_out->[2] > 0
ABCresult
0XX0
10X0
1100
1111
1822  
1823 # if ICC::Support::Lapack module is loaded
1824 0 ▶ if ($lapack) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lapack) { } 0 0 0
1825
1826 # make matrix of zeros
1827 0 $mat = ICC::Support::Lapack::zeros($ox + 1, $ix + 1);
1828
1829 } else {
1830
1831 # make matrix of zeros
1832 0 $mat = [map {[(0) x ($ix + 1)]} (0 .. $ox)];
1833
1834 }
1835  
1836 # for each output
1837 0 for my $i (0 .. $ox) {
1838
1839 # compute wavelength
1840 0 $w = $range_out->[0] + $i * $range_out->[2];
1841
1842 # if wavelength < start of source
1843 0 ▶ if ($w < $range_in->[0]) {
Branch: 3 branches
BranchTrueFalseTotal
if ($w < $range_in->[0]) { } 0 0 0
elsif ($w > $range_in->[1]) { } 0 0 0
elsif ($w == $range_in->[1]) { } 0 0 0
1844
1845 # if extrapolation defined
1846 0 ▶ if (defined($ext)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ext) 0 0 0
1847
1848 # if linear extrapolation
1849 0 ▶ if ($ext eq 'linear') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext eq 'linear') { } 0 0 0
1850
1851 # compute ratio
1852 0 $t = ($range_in->[0] - $w)/$range_in->[2];
1853
1854 # set matrix elements
1855 0 $mat->[$i][0] = 1 + 3 * $t/2;
1856 0 $mat->[$i][1] = -2 * $t;
1857 0 $mat->[$i][2] = $t/2;
1858
1859 # if copy extrapolation
1860 } elsif ('copy') {
1861
1862 # set element to first knot
1863 0 $mat->[$i][0] = 1;
1864
1865 } else {
1866
1867 # error
1868 croak('invalid extrapolation type');
1869
1870 }
1871
1872 }
1873
1874 # if wavelength > end of source
1875 } elsif ($w > $range_in->[1]) {
1876
1877 # if extrapolation defined
1878 0 ▶ if (defined($ext)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ext) 0 0 0
1879
1880 # if linear extrapolation
1881 0 ▶ if ($ext eq 'linear') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext eq 'linear') { } 0 0 0
1882
1883 # compute ratio
1884 0 $t = ($w - $range_in->[1])/$range_in->[2];
1885
1886 # set matrix elements
1887 0 $mat->[$i][$ix] = 1 + 3 * $t/2;
1888 0 $mat->[$i][$ix - 1] = -2 * $t;
1889 0 $mat->[$i][$ix - 2] = $t/2;
1890
1891 # if copy extrapolation
1892 } elsif ('copy') {
1893
1894 # set element to last knot
1895 0 $mat->[$i][$ix] = 1;
1896
1897 } else {
1898
1899 # error
1900 croak('invalid extrapolation type');
1901
1902 }
1903
1904 }
1905
1906 # if wavelength == end of source
1907 } elsif ($w == $range_in->[1]) {
1908
1909 # set element to last knot
1910 0 $mat->[$i][$ix] = 1;
1911
1912 } else {
1913
1914 # compute ratio and lower index
1915 0 ($t, $low) = POSIX::modf(($w - $range_in->[0])/$range_in->[2]);
1916
1917 # if ratio is zero
1918 0 ▶ if ($t == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($t == 0) { } 0 0 0
elsif ($low == 0) { } 0 0 0
elsif ($low == $ix - 1) { } 0 0 0
1919
1920 # set element to low knot
1921 0 $mat->[$i][$low] = 1;
1922
1923 # quadratic Lagrange interpolation (lower)
1924 } elsif ($low == 0) {
1925
1926 # compute Lagrange quadratic interpolation coefficients
1927 0 $mat->[$i][0] = ($t - 1) * ($t - 2)/2;
1928 0 $mat->[$i][1] = -$t * ($t - 2);
1929 0 $mat->[$i][2] = ($t - 1) * $t/2;
1930
1931 # quadratic Lagrange interpolation (upper)
1932 } elsif ($low == $ix - 1) {
1933
1934 # complement ratio
1935 0 $t = 1 - $t;
1936
1937 # compute Lagrange quadratic interpolation coefficients
1938 0 $mat->[$i][$ix] = ($t - 1) * ($t - 2)/2;
1939 0 $mat->[$i][$ix - 1] = -$t * ($t - 2);
1940 0 $mat->[$i][$ix - 2] = ($t - 1) * $t/2;
1941
1942 # cubic Lagrange interpolation
1943 } else {
1944
1945 # increment ratio
1946 0 $t = 1 + $t;
1947
1948 # compute Lagrange cubic interpolation coefficients
1949 0 $mat->[$i][$low - 1] = ($t - 1) * ($t - 2) * ($t - 3)/-6;
1950 0 $mat->[$i][$low] = $t * ($t - 2) * ($t - 3)/2;
1951 0 $mat->[$i][$low + 1] = ($t - 1) * $t * ($t - 3)/-2;
1952 0 $mat->[$i][$low + 2] = ($t - 1) * ($t - 2) * $t/6;
1953
1954 }
1955
1956 }
1957
1958 }
1959  
1960 # return
1961 0 return(bless($mat, 'Math::Matrix'));
1962  
1963 }
1964  
1965 #--------- vector functions ---------
1966  
1967 # compute vector dot product
1968 # parameters: (vector_1, vector_2)
1969 # vectors must have equal dimensions
1970 # returns: (dot_product)
1971 sub dotProduct {
1972  
1973 # check if ICC::Support::Lapack module is loaded
1974 0 ▶ state $lapack = defined($INC{'ICC/Support/Lapack.pm'});
Subroutine dotProduct: not called CC=4 SCAR=30.0
Pod documented
1975  
1976 # if ICC::Support::Lapack module is loaded
1977 0 ▶ if ($lapack) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lapack) { } 0 0 0
1978
1979 # return dot product
1980 0 return(ICC::Support::Lapack::dot(@_));
1981
1982 } else {
1983
1984 # verify vector sizes match
1985 0 ▶ ($#{$_[0]} == $#{$_[1]}) or croak('vector size mismatch');
Branch: 1 branch
BranchTrueFalseTotal
$#{$_[0];} == $#{$_[1];} or croak("vector size mismatch") 0 0 0
1986
1987 # initialize sum
1988 0 my $sum = 0;
1989
1990 # for each pair of array elements
1991 0 for (0 .. $#{$_[0]}) {
1992
1993 # accumulate product
1994 0 $sum += $_[0][$_] * $_[1][$_];
1995
1996 }
1997
1998 # return dot product
1999 0 return($sum);
2000
2001 }
2002
2003 }
2004  
2005 # compute vector cross product
2006 # parameters: (vector_1, vector_2)
2007 # vectors must be three-dimensional
2008 # returns: (cross_product_vector)
2009 sub crossProduct {
2010  
2011 # verify vectors are three-dimensional
2012 0 ▶ ($#{$_[0]} == 2 && $#{$_[1]} == 2) or croak('vectors not three-dimensional');
Subroutine crossProduct: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$#{$_[0];} == 2 and $#{$_[1];} == 2 or croak("vectors not three-dimensional") 0 0 0
Condition0%0/3
$#{$_[0];} == 2 and $#{$_[1];} == 2
!ll&&!rl&&r
000
MC/DC0%0/2
$#{$_[0];} == 2 and $#{$_[1];} == 2
$#{$_[0];} == 2 $#{$_[1];} == 2
Truth table0%0/3
A and B
A= $#{$_[0];} == 2B= $#{$_[1];} == 2
ABresult
0X0
100
111
2013  
2014 # return cross product
2015 0 return([$_[0][1] * $_[1][2] - $_[0][2] * $_[1][1], $_[0][2] * $_[1][0] - $_[0][0] * $_[1][2], $_[0][0] * $_[1][1] - $_[0][1] * $_[1][0]]);
2016  
2017 }
2018  
2019 #--------- utility functions ---------
2020  
2021 # flatten slice structure
2022 # structure may contain scalars, arrays or Math::Matrix objects
2023 # the structure is converted to an array of scalars
2024 # parameters: (ref_to_slice_structure)
2025 # returns: (reference_to_flattened_slice)
2026 sub flatten {
2027  
2028 # local variables
2029 612 ▶ my (@out);
Subroutine flatten: called CC=6 SCAR=19.2
Pod documented
2030  
2031 # while input array
2032 612 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3945 612 4557
2033
2034 # shift input value
2035 3945 my $v = shift();
2036
2037 # if undefined
2038 3945 ▶ if (! defined($v)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $v) { } 0 3945 3945
elsif (not ref $v) { } 3672 273 3945
elsif (ref $v eq 'ARRAY' or UNIVERSAL::isa($v, 'Math::Matrix')) { } 273 0 273
2039
2040 # warn
2041 0 carp('slice contains undefined value');
2042
2043 # if scalar
2044 } elsif (! ref($v)) {
2045
2046 # push on output
2047 3672 push(@out, $v);
2048
2049 # if array reference or Math::Matrix object
2050 ▶ } elsif (ref($v) eq 'ARRAY' || UNIVERSAL::isa($v, 'Math::Matrix')) {
Condition33%1/3
ref $v eq "ARRAY" or UNIVERSAL::isa($v, "Math::Matrix")
l!l&&r!l&&!r
27300
MC/DC0%0/2
ref $v eq "ARRAY" or UNIVERSAL::isa($v, "Math::Matrix")
ref $v eq "ARRAY" UNIVERSAL::isa($v, "Math::Matrix")
Truth table33%1/3
A or B
A= ref $v eq "ARRAY"B= UNIVERSAL::isa($v, "Math::Matrix")
ABresult
1X1
011
000
2051
2052 # dereference and push on input
2053 273 push(@_, @{$v});
2054
2055 } else {
2056
2057 # error
2058 0 croak('illegal slice structure');
2059
2060 }
2061
2062 }
2063  
2064 # return reference to flattened slice
2065 612 return(\@out);
2066  
2067 }
2068  
2069 # recursive clipping function
2070 # structure may contain scalars, arrays or Math::Matrix objects
2071 # clips scalar elements < 0.0 or > 1.0
2072 # parameter: (reference_to_structure)
2073 sub clip_struct {
2074  
2075 # if array reference or Math::Matrix object
2076 0 ▶ if (ref($_[0]) eq 'ARRAY' || UNIVERSAL::isa($_[0], 'Math::Matrix')) {
Subroutine clip_struct: not called CC=7 SCAR=40.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'ARRAY' or UNIVERSAL::isa($_[0], 'Math::Matrix')) { } 0 0 0
Condition0%0/3
ref $_[0] eq "ARRAY" or UNIVERSAL::isa($_[0], "Math::Matrix")
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] eq "ARRAY" or UNIVERSAL::isa($_[0], "Math::Matrix")
ref $_[0] eq "ARRAY" UNIVERSAL::isa($_[0], "Math::Matrix")
Truth table0%0/3
A or B
A= ref $_[0] eq "ARRAY"B= UNIVERSAL::isa($_[0], "Math::Matrix")
ABresult
1X1
011
000
2077
2078 # for each array element
2079 0 for (@{$_[0]}) {
2080
2081 # if a reference
2082 0 ▶ if (ref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 0 0 0
2083
2084 # call myself
2085 0 clip_struct($_)
2086
2087 } else {
2088
2089 # clip scalar
2090 0 ▶ $_ = 0.0 if ($_ < 0.0);
Branch: 1 branch
BranchTrueFalseTotal
if $_ < 0 0 0 0
2091 0 ▶ $_ = 1.0 if ($_ > 1.0);
Branch: 1 branch
BranchTrueFalseTotal
if $_ > 1 0 0 0
2092
2093 }
2094
2095 }
2096
2097 } else {
2098
2099 # error
2100 0 croak('error clipping structure');
2101
2102 }
2103
2104 }
2105  
2106 # round off number to nearest integer
2107 # parameter: (numeric_value)
2108 # returns: (integer_value)
2109 sub round {
2110  
2111 # return rounded value
2112 0 ▶ $_[0] > 0 ? int($_[0] + 0.5) : int($_[0] - 0.5)
Subroutine round: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] > 0 ? : 0 0 0
2113  
2114 }
2115  
2116 # convert array of s15Fixed16Number values to numeric
2117 # parameters: (input_array)
2118 # returns: (converted_array)
2119 sub s15f162v {
2120  
2121 # get parameters
2122 15 ▶ my (@in) = @_;
Subroutine s15f162v: called CC=2 SCAR=7.1
Pod documented
2123  
2124 # return converted array
2125 15 ▶ return(map {($_ & 0x80000000) ? $_/65536 - 65536 : $_/65536} @in);
Branch: 1 branch
BranchTrueFalseTotal
$_ & 2147483648 ? : 0 45 45
2126  
2127 }
2128  
2129 # convert array of numeric values to s15Fixed16Number
2130 # parameters: (input_array)
2131 # returns: (converted_array)
2132 sub v2s15f16 {
2133  
2134 # get parameters
2135 11 ▶ my (@in) = @_;
Subroutine v2s15f16: called CC=2 SCAR=7.1
Pod documented
2136  
2137 # return converted array
2138 11 ▶ return(map {($_ < 0) ? ($_ + 65536) * 65536 : $_ * 65536} @in);
Branch: 1 branch
BranchTrueFalseTotal
$_ < 0 ? : 0 45 45
2139  
2140 }
2141  
2142 # make profile folder
2143 # makes '~profiles' folder, and an alias
2144 # default alias folder is '~/Library/ColorSync/Profiles/'
2145 # customer and/or job will be undefined if not in path
2146 # parameters: (file/folder_path, [alias_folder_path])
2147 # returns: (profiles_folder_path, directory_segs, customer, job)
2148 sub makeProfileFolder {
2149  
2150 # get parameters
2151 0 ▶ my ($path, $alias) = @_;
Subroutine makeProfileFolder: not called CC=13 SCAR=52.0
Pod documented
2152  
2153 # local variables
2154 0 my ($vol, $dir, $file, @dsegs, $dref);
2155 0 my (@jobs, $i, $cust, $job, $folder, $sym);
2156  
2157 # verify parameter is a valid path
2158 0 ▶ (-e $path) or croak('invalid path parameter');
Branch: 1 branch
BranchTrueFalseTotal
-e $path or croak("invalid path parameter") 0 0 0
2159  
2160 # split the absolute path (note use of 'no_file' flag)
2161 0 ($vol, $dir, $file) = File::Spec->splitpath(File::Spec->rel2abs($path), -d $path);
2162  
2163 # split directory into segments
2164 0 @dsegs = File::Spec->splitdir($dir);
2165  
2166 # remove any trailing path delimiters
2167 0 ▶ while (@dsegs > 1 && $dsegs[-1] eq '') {pop(@dsegs)};
Branch: 1 branch
BranchTrueFalseTotal
if (@dsegs > 1 and $dsegs[-1] eq "") 0 0 0
Condition0%0/3
@dsegs > 1 and $dsegs[-1] eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
@dsegs > 1 and $dsegs[-1] eq ""
@dsegs > 1 $dsegs[-1] eq ""
Truth table0%0/3
A and B
A= @dsegs > 1B= $dsegs[-1] eq ""
ABresult
0X0
100
111
2168
2169 # make copy of directory segments array
2170 0 $dref = [@dsegs];
2171  
2172 # find indices of 'jobs' directory segments (if any)
2173 0 @jobs = grep {lc($dsegs[$_]) eq 'jobs'} (0 .. $#dsegs);
2174  
2175 # if customer directory segment exists
2176 0 ▶ if (@jobs && ($i = $jobs[0] + 1) <= $#dsegs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@jobs and ($i = $jobs[0] + 1) <= $#dsegs) 0 0 0
Condition0%0/3
@jobs and ($i = $jobs[0] + 1) <= $#dsegs
!ll&&!rl&&r
000
MC/DC0%0/2
@jobs and ($i = $jobs[0] + 1) <= $#dsegs
@jobs ($i = $jobs[0] + 1) <= $#dsegs
Truth table0%0/3
A and B
A= @jobsB= ($i = $jobs[0] + 1) <= $#dsegs
ABresult
0X0
100
111
2177
2178 # set customer
2179 0 $cust = $dsegs[$i];
2180
2181 # replace spaces with underscores
2182 0 $cust =~ s/ /_/g;
2183
2184 }
2185  
2186 # if job number directory segment exists
2187 0 ▶ if (@jobs && ($i = $jobs[0] + 2) <= $#dsegs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@jobs and ($i = $jobs[0] + 2) <= $#dsegs) 0 0 0
Condition0%0/3
@jobs and ($i = $jobs[0] + 2) <= $#dsegs
!ll&&!rl&&r
000
MC/DC0%0/2
@jobs and ($i = $jobs[0] + 2) <= $#dsegs
@jobs ($i = $jobs[0] + 2) <= $#dsegs
Truth table0%0/3
A and B
A= @jobsB= ($i = $jobs[0] + 2) <= $#dsegs
ABresult
0X0
100
111
2188
2189 # match job number
2190 0 ▶ if ($dsegs[$i] =~ m/^(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsegs[$i] =~ /^(\d+)/) { } 0 0 0
2191
2192 # set job number
2193 0 $job = $1;
2194
2195 } else {
2196
2197 # set job number to segment
2198 0 $job = $dsegs[$i];
2199
2200 # replace spacer with underscores
2201 0 $job =~ s/ /_/g;
2202
2203 }
2204
2205 # truncate directory segments array
2206 0 splice(@dsegs, $i + 1);
2207
2208 }
2209  
2210 # add '~profiles' segment
2211 0 push(@dsegs, '~profiles');
2212  
2213 # join directory segments
2214 0 $dir = File::Spec->catdir(@dsegs);
2215  
2216 # make profile folder path
2217 0 $folder = File::Spec->catpath($vol, $dir, '');
2218  
2219 # make profile folder
2220 0 ▶ mkdir($folder) if (! -d $folder);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $folder 0 0 0
2221  
2222 # make profile folder alias name (note colons appear as '/' in OS X paths)
2223 0 $sym = 'alias_to_' . join(':', @dsegs);
2224  
2225 # if alias parameter provided
2226 0 ▶ if (defined($alias)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $alias) { } 0 0 0
2227
2228 # verify a valid directory
2229 0 ▶ (-d $alias) or croak('invalid alias parameter');
Branch: 1 branch
BranchTrueFalseTotal
-d $alias or croak("invalid alias parameter") 0 0 0
2230
2231 # append alias folder name
2232 0 $alias .= "/$sym";
2233
2234 } else {
2235
2236 # append alias folder name
2237 0 $alias = "$ENV{'HOME'}/Library/ColorSync/Profiles/$sym";
2238
2239 }
2240  
2241 # make profile folder alias (Unix symbolic link)
2242 0 ▶ symlink($folder, $alias) if (! -d $alias);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $alias 0 0 0
2243  
2244 # return
2245 0 return($folder, $dref, $cust, $job);
2246  
2247 }
2248  
2249 # get 'ICC' directory path
2250 # optional parameter is appended to path
2251 # parameter: ([file/folder])
2252 # returns: (path)
2253 sub getICCPath {
2254  
2255 # get path to this module
2256 0 ▶ my $path = $INC{'ICC/Shared.pm'};
Subroutine getICCPath: not called CC=2 SCAR=17.9
Pod documented
2257  
2258 # if file/folder name supplied
2259 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
2260
2261 # replace module file name
2262 0 $path =~ s/Shared.pm$/$_[0]/;
2263
2264 } else {
2265
2266 # remove module file name
2267 0 $path =~ s/Shared.pm$//;
2268
2269 }
2270  
2271 # return path (may be invalid)
2272 0 return($path);
2273  
2274 }
2275  
2276 # filter file path
2277 # replaces '~' with user home directory
2278 # removes '\' characters (Unix) or replaces '/' with '\' (Windows)
2279 # parameter: (file_path)
2280 sub filterPath {
2281  
2282 # return if path undefined or a reference
2283 0 ▶ return() if (! defined($_[0]) || ref($_[0]));
Subroutine filterPath: not called CC=4 SCAR=30.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or ref $_[0] 0 0 0
Condition0%0/3
not defined $_[0] or ref $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $_[0] or ref $_[0]
not defined $_[0] ref $_[0]
Truth table0%0/3
not(A) or B
A= not defined $_[0]B= ref $_[0]
ABresult
1X1
011
000
2284  
2285 # if Windows
2286 0 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 0 0
2287
2288 # replace '~'
2289 0 $_[0] =~ s/^~/$ENV{'USERPROFILE'}/;
2290
2291 # replace '/' with '\'
2292 0 $_[0] =~ s/\//\\/g;
2293
2294 } else {
2295
2296 # replace '~'
2297 0 $_[0] =~ s/^~/$ENV{'HOME'}/;
2298  
2299 # remove '\'
2300 0 $_[0] =~ s/\\//g;
2301
2302 }
2303  
2304 }
2305  
2306 # set Mac OSX file creator and type
2307 # calls the 'SetFile' utility, if present
2308 # parameters: (file_path, creator, type)
2309 sub setFile {
2310  
2311 # get parameters
2312 0 ▶ my ($path, $creator, $type) = @_;
Subroutine setFile: not called CC=3 SCAR=24.8
Pod documented
2313  
2314 # replace '~'
2315 0 $path =~ s/^~/$ENV{'HOME'}/;
2316  
2317 # escape spaces and special characters
2318 0 $path =~ s/([^\w\-\/+.@])/\\$1/g;
2319  
2320 # if SetFile in /usr/bin directory
2321 0 ▶ if (-f '/usr/bin/Setfile') {
Branch: 2 branches
BranchTrueFalseTotal
if (-f '/usr/bin/Setfile') { } 0 0 0
elsif (-f '/Developer/usr/bin/SetFile') { } 0 0 0
2322
2323 # make system call
2324 0 qx(/usr/bin/Setfile -c $creator -t $type $path);
2325
2326 # if SetFile in /Developer/usr/bin directory
2327 } elsif (-f '/Developer/usr/bin/SetFile') {
2328
2329 # make system call
2330 0 qx(/Developer/usr/bin/SetFile -c $creator -t $type $path);
2331
2332 }
2333  
2334 }
2335  
2336 # parse token string
2337 # string is split into tokens
2338 # token parameters are processed with 'eval'
2339 # returned array contains tokens and their parameters
2340 # parameters: (token_string)
2341 # returns: (ref_to_array)
2342 sub parse_tokens {
2343  
2344 # get parameter
2345 0 ▶ my $str = shift();
Subroutine parse_tokens: not called CC=15 SCAR=54.8
Pod documented
2346  
2347 # local parameters
2348 0 my ($nest, $j, $char, @px, $sub, @pars, @eval);
2349  
2350 # return if parameter undefined
2351 0 ▶ return([]) if (! defined($str));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 0 0
2352  
2353 # if parameter not a scalar
2354 0 ▶ if (ref($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $str) 0 0 0
2355
2356 # warn
2357 0 carp("invalid 'parse_token' parameter\n");
2358
2359 # return
2360 0 return([]);
2361
2362 }
2363  
2364 # initialize loop
2365 0 $nest = 0;
2366 0 $j = 0;
2367  
2368 # for each character
2369 0 for my $i (0 .. (length($str) - 1)) {
2370
2371 # get character
2372 0 $char = substr($str, $i, 1);
2373
2374 # if a '('
2375 0 ▶ if ($char eq '(') {
Branch: 2 branches
BranchTrueFalseTotal
if ($char eq '(') { } 0 0 0
elsif ($char eq ')') { } 0 0 0
2376
2377 # if first nesting level
2378 0 ▶ if ($nest == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nest == 0) 0 0 0
2379
2380 # save index (start parameter group)
2381 0 $px[$j]->[0] = $i;
2382
2383 }
2384
2385 # increment nesting level
2386 0 $nest++;
2387
2388 # if a ')'
2389 } elsif ($char eq ')') {
2390
2391 # if second nesting level
2392 0 ▶ if ($nest == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nest == 1) 0 0 0
2393
2394 # save index (end parameter group)
2395 0 $px[$j]->[1] = $i;
2396
2397 # increment parameter index
2398 0 $j++;
2399
2400 }
2401
2402 # decrement nesting level
2403 0 $nest--;
2404
2405 }
2406
2407 }
2408  
2409 # check final nesting level (should be 0)
2410 0 ▶ carp("missing ')' in parameters\n") if ($nest > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $nest > 0 0 0 0
2411 0 ▶ carp("missing '(' in parameters\n") if ($nest < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $nest < 0 0 0 0
2412  
2413 # for each parameter group
2414 0 for my $i (0 .. $#px) {
2415
2416 # get parameter substring
2417 0 $sub = substr($str, $px[$i]->[0], $px[$i]->[1] - $px[$i]->[0] + 1);
2418
2419 # evaluate parameter substring
2420 0 @eval = eval($sub);
2421
2422 # split text within (), if 'eval' failed
2423 0 ▶ @eval = split(/[\s,]+/, substr($str, $px[$i]->[0] + 1, $px[$i]->[1] - $px[$i]->[0] - 1)) if ((! @eval) && ($px[$i]->[1] - $px[$i]->[0] > 1));
Branch: 1 branch
BranchTrueFalseTotal
if not @eval and $px[$i][1] - $px[$i][0] > 1 0 0 0
Condition0%0/3
not @eval and $px[$i][1] - $px[$i][0] > 1
!ll&&!rl&&r
000
MC/DC0%0/2
not @eval and $px[$i][1] - $px[$i][0] > 1
not @eval $px[$i][1] - $px[$i][0] > 1
Truth table0%0/3
not(A) and B
A= not @evalB= $px[$i][1] - $px[$i][0] > 1
ABresult
0X0
100
111
2424
2425 # save parameters
2426 0 $pars[$i] = [@eval];
2427
2428 }
2429  
2430 # for each parameter group (reverse order)
2431 0 for my $i (reverse(0 .. $#px)) {
2432
2433 # replace parameter group with ' ($i) '
2434 0 substr($str, $px[$i]->[0], $px[$i]->[1] - $px[$i]->[0] + 1) = " ($i) ";
2435
2436 }
2437  
2438 # split string into tokens, then replace '($i)' with 'eval'd parameters
2439 0 ▶ return([map {m/\((\d+)\)/ ? $pars[$1] : $_} split(/[\s,]+/, $str)]);
Branch: 1 branch
BranchTrueFalseTotal
/\((\d+)\)/ ? : 0 0 0
2440  
2441 }
2442  
2443 #--------- testing parameters ---------
2444  
2445 # test if a vector
2446 # elements may be numeric or 'undef'
2447 sub is_vector {
2448  
2449 # return test result
2450 0 ▶ return(ref($_[0]) eq 'ARRAY' && @{$_[0]} == grep {! defined || Scalar::Util::looks_like_number($_)} @{$_[0]});
Subroutine is_vector: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
not defined $_ or &Scalar::Util::looks_like_number($_) 0 0 0
Condition0%0/3
ref $_[0] eq 'ARRAY' && @{$_[0];} == grep({not defined $_ or &Scalar::Util::looks_like_number($_);} @{$_[0];})
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] eq 'ARRAY' && @{$_[0];} == grep({not defined $_ or &Scalar::Util::looks_like_number($_);} @{$_[0];})
ref $_[0] eq 'ARRAY' @{$_[0];} == grep({not defined $_ or &Scalar::Util::looks_like_number($_);} @{$_[0];})
Truth table0%0/3
A && B
A= ref $_[0] eq 'ARRAY'B= @{$_[0];} == grep({not defined $_ or &Scalar::Util::looks_like_number($_);} @{$_[0];})
ABresult
0X0
100
111
2451  
2452 }
2453  
2454 # test if a numeric vector
2455 # elements must all be numeric
2456 sub is_num_vector {
2457  
2458 # return test result
2459 0 ▶ return(ref($_[0]) eq 'ARRAY' && @{$_[0]} == grep {Scalar::Util::looks_like_number($_)} @{$_[0]});
Subroutine is_num_vector: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/3
ref $_[0] eq 'ARRAY' && @{$_[0];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[0];})
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] eq 'ARRAY' && @{$_[0];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[0];})
ref $_[0] eq 'ARRAY' @{$_[0];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[0];})
Truth table0%0/3
A && B
A= ref $_[0] eq 'ARRAY'B= @{$_[0];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[0];})
ABresult
0X0
100
111
2460  
2461 }
2462  
2463 # test if a matrix
2464 # elements may be numeric or 'undef'
2465 sub is_matrix {
2466  
2467 # return test result
2468 0 ▶ return((ref($_[0]) eq 'ARRAY' || ref($_[0]) eq 'Math::Matrix') && @{$_[0]} == grep {is_vector($_)} @{$_[0]});
Subroutine is_matrix: not called CC=3 SCAR=24.8
Pod documented
Condition0%0/6
(ref $_[0] eq 'ARRAY' || ref $_[0] eq 'Math::Matrix') && @{$_[0];} == grep({is_vector($_);} @{$_[0];})
!ll&&!rl&&r
000
ref $_[0] eq "ARRAY" or ref $_[0] eq "Math::Matrix"
l!l&&r!l&&!r
000
MC/DC0%0/3
(ref $_[0] eq 'ARRAY' || ref $_[0] eq 'Math::Matrix') && @{$_[0];} == grep({is_vector($_);} @{$_[0];})
ref $_[0] eq "ARRAY" ref $_[0] eq "Math::Matrix" @{$_[0];} == grep({is_vector($_);} @{$_[0];})
Truth table0%0/5
A or B && C
A= ref $_[0] eq "ARRAY"B= ref $_[0] eq "Math::Matrix"C= @{$_[0];} == grep({is_vector($_);} @{$_[0];})
ABCresult
00X0
1X00
0100
1X11
0111
2469  
2470 }
2471  
2472 # test if a numeric matrix
2473 # elements must all be numeric
2474 sub is_num_matrix {
2475  
2476 # return test result
2477 0 ▶ return((ref($_[0]) eq 'ARRAY' || ref($_[0]) eq 'Math::Matrix') && @{$_[0]} == grep {is_num_vector($_)} @{$_[0]});
Subroutine is_num_matrix: not called CC=3 SCAR=24.8
Pod documented
Condition0%0/6
(ref $_[0] eq 'ARRAY' || ref $_[0] eq 'Math::Matrix') && @{$_[0];} == grep({is_num_vector($_);} @{$_[0];})
!ll&&!rl&&r
000
ref $_[0] eq "ARRAY" or ref $_[0] eq "Math::Matrix"
l!l&&r!l&&!r
000
MC/DC0%0/3
(ref $_[0] eq 'ARRAY' || ref $_[0] eq 'Math::Matrix') && @{$_[0];} == grep({is_num_vector($_);} @{$_[0];})
ref $_[0] eq "ARRAY" ref $_[0] eq "Math::Matrix" @{$_[0];} == grep({is_num_vector($_);} @{$_[0];})
Truth table0%0/5
A or B && C
A= ref $_[0] eq "ARRAY"B= ref $_[0] eq "Math::Matrix"C= @{$_[0];} == grep({is_num_vector($_);} @{$_[0];})
ABCresult
00X0
1X00
0100
1X11
0111
2478  
2479 }
2480  
2481 #--------- internal functions ---------
2482  
2483 # convert structure (3 parameters)
2484 # parameters: (input_array -or- input_structure, subroutine_reference)
2485 # returns: (output_array -or- output_structure)
2486 sub _convert3 {
2487  
2488 # get subroutine code reference
2489 0 ▶ my ($sub) = pop();
Subroutine _convert3: not called CC=7 SCAR=40.3
2490  
2491 # if valid array input (3 scalars)
2492 0 ▶ if (@_ == 3 && 3 == grep {! ref()} @_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 3 and 3 == grep({not ref $_;} @_)) { } 0 0 0
elsif (@_ == 1 and ref $_[0] eq 'ARRAY' || UNIVERSAL::isa($_[0], 'Math::Matrix')) { } 0 0 0
Condition0%0/3
@_ == 3 and 3 == grep({not ref $_;} @_)
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 3 and 3 == grep({not ref $_;} @_)
@_ == 3 3 == grep({not ref $_;} @_)
Truth table0%0/3
A and B
A= @_ == 3B= 3 == grep({not ref $_;} @_)
ABresult
0X0
100
111
2493
2494 # call subroutine (passing @_)
2495 0 &$sub;
2496
2497 # if valid structure input (array_ref -or- Math::Matrix object
2498 ▶ } elsif (@_ == 1 && (ref($_[0]) eq 'ARRAY' || UNIVERSAL::isa($_[0], 'Math::Matrix'))) {
Condition0%0/6
@_ == 1 and ref $_[0] eq "ARRAY" || UNIVERSAL::isa($_[0], "Math::Matrix")
!ll&&!rl&&r
000
ref $_[0] eq "ARRAY" or UNIVERSAL::isa($_[0], "Math::Matrix")
l!l&&r!l&&!r
000
MC/DC0%0/3
@_ == 1 and ref $_[0] eq "ARRAY" || UNIVERSAL::isa($_[0], "Math::Matrix")
@_ == 1 ref $_[0] eq "ARRAY" UNIVERSAL::isa($_[0], "Math::Matrix")
Truth table0%0/4
A and B or C
A= @_ == 1B= ref $_[0] eq "ARRAY"C= UNIVERSAL::isa($_[0], "Math::Matrix")
ABCresult
0XX0
1000
11X1
1011
2499
2500 # process structure
2501 0 _crawl($_[0], my $out = [], $sub);
2502
2503 # bless output array if input a Math::Matrix object
2504 0 ▶ bless($out, 'Math::Matrix') if (UNIVERSAL::isa($_[0], 'Math::Matrix'));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], "Math::Matrix") 0 0 0
2505
2506 # return output
2507 0 return($out);
2508
2509 } else {
2510
2511 # error
2512 0 croak('invalid input structure');
2513
2514 }
2515
2516 }
2517  
2518 # convert structure (4 parameters)
2519 # parameters: (input_array -or- input_structure, subroutine_reference)
2520 # returns: (output_array -or- output_structure)
2521 sub _convert4 {
2522  
2523 # get subroutine code reference
2524 0 ▶ my ($sub) = pop(@_);
Subroutine _convert4: not called CC=10 SCAR=47.0
2525  
2526 # get white point vector (input_structure)
2527 0 ▶ my $wtpt = (@_ == 2) ? pop() : D50;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 0 0 0
2528
2529 # push D50 white point vector, if none (input_array)
2530 0 ▶ push(@_, D50) if (@_ == 3);
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 3 0 0 0
2531  
2532 # if valid array input (3 scalars and white point vector)
2533 0 ▶ if (@_ == 4 && ref($_[3]) eq 'ARRAY' && 3 == grep {! ref()} @_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 4 and ref $_[3] eq 'ARRAY' and 3 == grep({not ref $_;} @_)) { } 0 0 0
elsif (@_ == 1 and ref $_[0] eq 'ARRAY' || UNIVERSAL::isa($_[0], 'Math::Matrix')) { } 0 0 0
Condition0%0/6
@_ == 4 and ref $_[3] eq "ARRAY" and 3 == grep({not ref $_;} @_)
!ll&&!rl&&r
000
@_ == 4 and ref $_[3] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
@_ == 4 and ref $_[3] eq "ARRAY" and 3 == grep({not ref $_;} @_)
@_ == 4 ref $_[3] eq "ARRAY" 3 == grep({not ref $_;} @_)
Truth table0%0/4
A and B and C
A= @_ == 4B= ref $_[3] eq "ARRAY"C= 3 == grep({not ref $_;} @_)
ABCresult
0XX0
10X0
1100
1111
2534
2535 # call subroutine (passing @_)
2536 0 &$sub;
2537
2538 # if valid structure input (array_ref -or- Math::Matrix object)
2539 ▶ } elsif (@_ == 1 && (ref($_[0]) eq 'ARRAY' || UNIVERSAL::isa($_[0], 'Math::Matrix'))) {
Condition0%0/6
@_ == 1 and ref $_[0] eq "ARRAY" || UNIVERSAL::isa($_[0], "Math::Matrix")
!ll&&!rl&&r
000
ref $_[0] eq "ARRAY" or UNIVERSAL::isa($_[0], "Math::Matrix")
l!l&&r!l&&!r
000
MC/DC0%0/3
@_ == 1 and ref $_[0] eq "ARRAY" || UNIVERSAL::isa($_[0], "Math::Matrix")
@_ == 1 ref $_[0] eq "ARRAY" UNIVERSAL::isa($_[0], "Math::Matrix")
Truth table0%0/4
A and B or C
A= @_ == 1B= ref $_[0] eq "ARRAY"C= UNIVERSAL::isa($_[0], "Math::Matrix")
ABCresult
0XX0
1000
11X1
1011
2540
2541 # process structure
2542 0 _crawl($_[0], my $out = [], $sub, $wtpt);
2543
2544 # bless output array if input a Math::Matrix object
2545 0 ▶ bless($out, 'Math::Matrix') if (UNIVERSAL::isa($_[0], 'Math::Matrix'));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], "Math::Matrix") 0 0 0
2546
2547 # return output
2548 0 return($out);
2549
2550 } else {
2551
2552 # error
2553 0 croak('invalid input structure');
2554
2555 }
2556
2557 }
2558  
2559 # process structure
2560 # recursive subroutine
2561 # parameters: (input_structure, output_structure, subroutine_reference, [white_point_vector])
2562 sub _crawl {
2563  
2564 # get parameters
2565 0 ▶ my ($in, $out, $sub, $wtpt) = @_;
Subroutine _crawl: not called CC=5 SCAR=34.0
2566  
2567 # if input a reference to an array of 3 scalars
2568 0 ▶ if (@{$in} == 3 && 3 == grep {! ref()} @{$in}) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$in;} == 3 and 3 == grep({not ref $_;} @{$in;})) { } 0 0 0
elsif (@{$in;} == grep({ref $_ eq 'ARRAY';} @{$in;})) { } 0 0 0
Condition0%0/3
@{$in;} == 3 and 3 == grep({not ref $_;} @{$in;})
!ll&&!rl&&r
000
MC/DC0%0/2
@{$in;} == 3 and 3 == grep({not ref $_;} @{$in;})
@{$in;} == 3 3 == grep({not ref $_;} @{$in;})
Truth table0%0/3
A and B
A= @{$in;} == 3B= 3 == grep({not ref $_;} @{$in;})
ABresult
0X0
100
111
2569
2570 # call subroutine
2571 0 @{$out} = &$sub(@{$in}, $wtpt);
2572
2573 # if input a reference to an array of array references
2574 0 } elsif (@{$in} == grep {ref() eq 'ARRAY'} @{$in}) {
2575
2576 # for each array reference
2577 0 for my $i (0 .. $#{$in}) {
2578
2579 # process next level
2580 0 _crawl($in->[$i], $out->[$i] = [], $sub, $wtpt);
2581
2582 }
2583
2584 } else {
2585
2586 # error
2587 0 croak('invalid input structure');
2588
2589 }
2590
2591 }
2592  
2593 # convert xyz to L*a*b*
2594 # parameters: (x, y, z)
2595 # returns: (L, a, b)
2596 sub _xyz2Lab {
2597  
2598 # get parameters
2599 10 ▶ my ($x, $y, $z) = @_;
Subroutine _xyz2Lab: called CC=1 SCAR=0.0
2600  
2601 # compute L* value
2602 10 my $L = x2L($y);
2603  
2604 # return L*a*b* values
2605 10 return($L, 500 * (x2L($x) - $L)/116, 200 * ($L - x2L($z))/116);
2606  
2607 }
2608  
2609 # convert L*a*b* to xyz
2610 # parameters: (L, a, b)
2611 # returns: (x, y, z)
2612 sub _Lab2xyz {
2613  
2614 # get parameters
2615 40 ▶ my ($L, $a, $b) = @_;
Subroutine _Lab2xyz: called CC=1 SCAR=0.0
2616  
2617 # return xyz values
2618 40 return (L2x($L + 116 * $a/500), L2x($L), L2x($L - 116 * $b/200));
2619  
2620 }
2621  
2622 # convert xyz to LxLyLz
2623 # parameters: (x, y, z)
2624 # returns: (Lx, Ly, Lz)
2625 sub _xyz2Lxyz {
2626  
2627 # get parameters
2628 0 ▶ my ($x, $y, $z) = @_;
Subroutine _xyz2Lxyz: not called CC=1 SCAR=6.9
2629  
2630 # return LxLyLz values
2631 0 return(x2L($x), x2L($y), x2L($z));
2632  
2633 }
2634  
2635 # convert LxLyLz to xyz
2636 # parameters: (Lx, Ly, Lz)
2637 # returns: (x, y, z)
2638 sub _Lxyz2xyz {
2639  
2640 # get parameters
2641 0 ▶ my ($Lx, $Ly, $Lz) = @_;
Subroutine _Lxyz2xyz: not called CC=1 SCAR=6.9
2642  
2643 # return xyz values
2644 0 return (L2x($Lx), L2x($Ly), L2x($Lz));
2645  
2646 }
2647  
2648 # convert L*a*b* to LxLyLz
2649 # parameters: (L, a, b)
2650 # returns: (Lx, Ly, Lz)
2651 sub _Lab2Lxyz {
2652  
2653 # get parameters
2654 0 ▶ my ($L, $a, $b) = @_;
Subroutine _Lab2Lxyz: not called CC=1 SCAR=6.9
2655  
2656 # return LxLyLz values
2657 0 return($L + 116 * $a/500, $L, $L - 116 * $b/200);
2658  
2659 }
2660  
2661 # convert LxLyLz to L*a*b*
2662 # parameters: (Lx, Ly, Lz)
2663 # returns: (L, a, b)
2664 sub _Lxyz2Lab {
2665  
2666 # get parameters
2667 0 ▶ my ($Lx, $Ly, $Lz) = @_;
Subroutine _Lxyz2Lab: not called CC=1 SCAR=6.9
2668  
2669 # return L*a*b* values
2670 0 return($Ly, 500 * ($Lx - $Ly)/116, 200 * ($Ly - $Lz)/116);
2671  
2672 }
2673  
2674 # convert XYZ to L*a*b*
2675 # parameters: (X, Y, Z, ref_to_WP_vector)
2676 # returns: (L, a, b)
2677 sub _XYZ2Lab {
2678  
2679 # get parameters
2680 0 ▶ my ($x, $y, $z, $WPxyz) = @_;
Subroutine _XYZ2Lab: not called CC=1 SCAR=6.9
2681  
2682 # compute L* value
2683 0 my $L = x2L($y/$WPxyz->[1]);
2684  
2685 # return L*a*b* values
2686 0 return($L, 500 * (x2L($x/$WPxyz->[0]) - $L)/116, 200 * ($L - x2L($z/$WPxyz->[2]))/116);
2687  
2688 }
2689  
2690 # convert L*a*b* to XYZ
2691 # parameters: (L, a, b, ref_to_WP_vector)
2692 # returns: (X, Y, Z)
2693 sub _Lab2XYZ {
2694  
2695 # get parameters
2696 0 ▶ my ($L, $a, $b, $WPxyz) = @_;
Subroutine _Lab2XYZ: not called CC=1 SCAR=6.9
2697  
2698 # return XYZ values
2699 0 return (L2x($L + 116 * $a/500) * $WPxyz->[0], L2x($L) * $WPxyz->[1], L2x($L - 116 * $b/200) * $WPxyz->[2]);
2700  
2701 }
2702  
2703 # convert XYZ to LxLyLz
2704 # parameters: (X, Y, Z, ref_to_WP_vector)
2705 # returns: (Lx, Ly, Lz)
2706 sub _XYZ2Lxyz {
2707  
2708 # get parameters
2709 0 ▶ my ($x, $y, $z, $WPxyz) = @_;
Subroutine _XYZ2Lxyz: not called CC=1 SCAR=6.9
2710  
2711 # return values
2712 0 return(x2L($x/$WPxyz->[0]), x2L($y/$WPxyz->[1]), x2L($z/$WPxyz->[2]));
2713  
2714 }
2715  
2716 # convert LxLyLz to XYZ
2717 # parameters: (Lx, Ly, Lz, ref_to_WP_vector)
2718 # returns: (X, Y, Z)
2719 sub _Lxyz2XYZ {
2720  
2721 # get parameters
2722 0 ▶ my ($Lx, $Ly, $Lz, $WPxyz) = @_;
Subroutine _Lxyz2XYZ: not called CC=1 SCAR=6.9
2723  
2724 # return values
2725 0 return (L2x($Lx) * $WPxyz->[0], L2x($Ly) * $WPxyz->[1], L2x($Lz) * $WPxyz->[2]);
2726  
2727 }
2728  
2729 # convert XYZ to xyz
2730 # parameters: (X, Y, Z, ref_to_WP_vector)
2731 # returns: (x, y, z)
2732 sub _XYZ2xyz {
2733  
2734 # get parameters
2735 0 ▶ my ($x, $y, $z, $WPxyz) = @_;
Subroutine _XYZ2xyz: not called CC=1 SCAR=6.9
2736  
2737 # return values
2738 0 return($x/$WPxyz->[0], $y/$WPxyz->[1], $z/$WPxyz->[2]);
2739  
2740 }
2741  
2742 # convert xyz to XYZ
2743 # parameters: (x, y, z, ref_to_WP_vector)
2744 # returns: (X, Y, Z)
2745 sub _xyz2XYZ {
2746  
2747 # get parameters
2748 0 ▶ my ($x, $y, $z, $WPxyz) = @_;
Subroutine _xyz2XYZ: not called CC=1 SCAR=6.9
2749  
2750 # return values
2751 0 return($x * $WPxyz->[0], $y * $WPxyz->[1], $z * $WPxyz->[2]);
2752  
2753 }
2754  
2755 # convert XYZ to xyY
2756 # parameters: (X, Y, Z)
2757 # returns: (x, y, Y)
2758 sub _XYZ2xyY {
2759  
2760 # get parameters
2761 0 ▶ my ($X, $Y, $Z) = @_;
Subroutine _XYZ2xyY: not called CC=2 SCAR=17.9
2762  
2763 # compute denominator
2764 0 ▶ (my $d = $X + $Y + $Z) or croak('X + Y + Z = 0 when computing chromaticity');
Branch: 1 branch
BranchTrueFalseTotal
my $d = $X + $Y + $Z or croak("X + Y + Z = 0 when computing chromaticity") 0 0 0
2765  
2766 # return values
2767 0 return($X/$d, $Y/$d, $Y);
2768  
2769 }
2770  
2771 # convert xyY to XYZ
2772 # parameters: (x, y, Y)
2773 # returns: (X, Y, Z)
2774 sub _xyY2XYZ {
2775  
2776 # get parameters
2777 0 ▶ my ($x, $y, $Y) = @_;
Subroutine _xyY2XYZ: not called CC=2 SCAR=17.9
2778  
2779 # if y is zero
2780 0 ▶ if ($y == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y == 0) { } 0 0 0
2781
2782 # error
2783 0 croak('cannot compute XYZ when y = 0');
2784
2785 } else {
2786
2787 # return values
2788 0 return($Y * $x/$y, $Y, $Y * (1 - $x - $y)/$y);
2789
2790 }
2791  
2792 }
2793  
2794 # convert L*a*b* values to DIN 99 Lab values
2795 # optional parameters are Ke (lightness) and Kch (chroma)
2796 # default values of optional parameters are Ke = 1, Kch = 1
2797 # parameters: (L*a*b*, [Ke, Kch])
2798 # returns: (DIN_99_Lab)
2799 sub _Lab2DIN99 {
2800  
2801 # local variables
2802 0 ▶ my ($e, $f, $G, $h99, $C99, $Ke, $Kch);
Subroutine _Lab2DIN99: not called CC=3 SCAR=24.8
2803  
2804 # compute redness
2805 0 $e = cos16 * $_[1] + sin16 * $_[2];
2806  
2807 # compute yellowness
2808 0 $f = 0.7 * (cos16 * $_[2] - sin16 * $_[1]);
2809  
2810 # compute chroma
2811 0 $G = sqrt($e**2 + $f**2);
2812  
2813 # compute DIN 99 hue angle
2814 0 $h99 = atan2($f, $e);
2815  
2816 # get Ke
2817 0 ▶ $Ke = defined($_[3]) ? $_[3] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[3] ? : 0 0 0
2818  
2819 # get Kch
2820 0 ▶ $Kch = defined($_[4]) ? $_[4] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[4] ? : 0 0 0
2821  
2822 # compute DIN 99 chroma
2823 0 $C99 = (log(1 + 0.045 * $G))/(0.045 * $Kch * $Ke);
2824  
2825 # return DIN 99 Lab
2826 0 return(105.509 * (log(1 + 0.0158 * $_[0])) * $Ke, $C99 * cos($h99), $C99 * sin($h99));
2827  
2828 }
2829  
2830 #--------- additional Math::Matrix methods ---------
2831  
2832 package Math::Matrix;
2833  
2834 # print object contents to string
2835 # format is an array structure
2836 # parameter: ([format])
2837 # returns: (string)
2838 sub sdump {
2839  
2840 # get parameters
2841 0 ▶ my ($self, $p) = @_;
Subroutine sdump: not called CC=4 SCAR=30.0
Pod undocumented
2842  
2843 # local variables
2844 0 my ($s, $fmt);
2845  
2846 # resolve parameter to a scalar
2847 0 ▶ $p = defined($p) ? ref($p) eq 'ARRAY' ? $p->[0] : $p : '10.5f';
Branch: 2 branches
BranchTrueFalseTotal
defined $p ? : 0 0 0
ref $p eq 'ARRAY' ? : 0 0 0
2848  
2849 # set string to object ID
2850 0 $s = sprintf("'%s' object, (0x%x)\n", ref($self), Scalar::Util::refaddr($self));
2851  
2852 # append string
2853 0 $s .= "matrix values\n";
2854  
2855 # for each row
2856 0 for my $i (0 .. $#{$self}) {
2857
2858 # make number format from parameter
2859 0 $fmt = " %$p" x @{$self->[$i]};
2860
2861 # append matrix row
2862 0 $s .= sprintf("$fmt\n", @{$self->[$i]});
2863
2864 }
2865  
2866 # return
2867 0 return($s);
2868  
2869 }
2870  
2871 # print object contents
2872 # format parameter is an array structure
2873 # parameter: ([format])
2874 # returns: (string)
2875 sub dump {
2876  
2877 # get parameters
2878 0 ▶ my ($self, $format) = @_;
Subroutine dump: not called CC=1 SCAR=6.9
Pod undocumented
2879  
2880 # get string from 'sdump'
2881 0 my $s = $self->sdump($format);
2882  
2883 # print string
2884 0 print $s, "\n";
2885  
2886 # return string
2887 0 return($s);
2888  
2889 }
2890  
2891 # clip matrix elements
2892 # default limits are [0, 1]
2893 # limit is disabled with value of 'none'
2894 # parameters: ([lower_limit], [upper_limit])
2895 # returns: (modified_matrix_object)
2896 sub clip {
2897  
2898 # get parameters
2899 0 ▶ my ($self, $low, $up) = @_;
Subroutine clip: not called CC=19 SCAR=59.4
Pod undocumented
2900  
2901 # set defaults, as needed
2902 0 ▶ $low = 0.0 if (! defined($low));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $low 0 0 0
2903 0 ▶ $low = undef if ($low eq 'none');
Branch: 1 branch
BranchTrueFalseTotal
if $low eq "none" 0 0 0
2904 0 ▶ $up = 1.0 if (! defined($up));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $up 0 0 0
2905 0 ▶ $up = undef if ($up eq 'none');
Branch: 1 branch
BranchTrueFalseTotal
if $up eq "none" 0 0 0
2906  
2907 # verify limits
2908 0 ▶ (! defined($low) || Scalar::Util::looks_like_number($low)) or croak('invalid lower clipping limit');
Branch: 1 branch
BranchTrueFalseTotal
not defined $low or &Scalar::Util::looks_like_number($low) or croak("invalid lower clipping limit") 0 0 0
Condition0%0/3
not defined $low or &Scalar::Util::looks_like_number($low)
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $low or &Scalar::Util::looks_like_number($low)
not defined $low &Scalar::Util::looks_like_number($low)
Truth table0%0/3
not(A) or B
A= not defined $lowB= &Scalar::Util::looks_like_number($low)
ABresult
1X1
011
000
2909 0 ▶ (! defined($up) || Scalar::Util::looks_like_number($up)) or croak('invalid upper clipping limit');
Branch: 1 branch
BranchTrueFalseTotal
not defined $up or &Scalar::Util::looks_like_number($up) or croak("invalid upper clipping limit") 0 0 0
Condition0%0/3
not defined $up or &Scalar::Util::looks_like_number($up)
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $up or &Scalar::Util::looks_like_number($up)
not defined $up &Scalar::Util::looks_like_number($up)
Truth table0%0/3
not(A) or B
A= not defined $upB= &Scalar::Util::looks_like_number($up)
ABresult
1X1
011
000
2910 0 ▶ (! defined($low) || ! defined($up) || $up >= $low) or croak('invalid clipping limits');
Branch: 1 branch
BranchTrueFalseTotal
not defined $low && defined $up or $up >= $low or croak("invalid clipping limits") 0 0 0
Condition0%0/6
not defined $low && defined $up or $up >= $low
l!l&&r!l&&!r
000
defined $low && defined $up
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $low && defined $up or $up >= $low
defined $low defined $up $up >= $low
Truth table0%0/4
not(A && B) or C
A= defined $lowB= defined $upC= $up >= $low
ABCresult
0XX1
10X1
1111
1100
2911  
2912 # for each row
2913 0 for my $i (0 .. $#{$self}) {
2914
2915 # for each column
2916 0 for my $j (0 .. $#{$self->[$i]}) {
2917
2918 # if matrix element is numeric
2919 0 ▶ if (Scalar::Util::looks_like_number($self->[$i][$j])) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Scalar::Util::looks_like_number($self->[$i][$j])) 0 0 0
2920
2921 # apply limits to matrix element
2922 0 ▶ $self->[$i][$j] = $low if (defined($low) && $self->[$i][$j] < $low);
Branch: 1 branch
BranchTrueFalseTotal
if defined $low and $self->[$i][$j] < $low 0 0 0
Condition0%0/3
defined $low and $self->[$i][$j] < $low
!ll&&!rl&&r
000
MC/DC0%0/2
defined $low and $self->[$i][$j] < $low
defined $low $self->[$i][$j] < $low
Truth table0%0/3
A and B
A= defined $lowB= $self->[$i][$j] < $low
ABresult
0X0
100
111
2923 0 ▶ $self->[$i][$j] = $up if (defined($up) && $self->[$i][$j] > $up);
Branch: 1 branch
BranchTrueFalseTotal
if defined $up and $self->[$i][$j] > $up 0 0 0
Condition0%0/3
defined $up and $self->[$i][$j] > $up
!ll&&!rl&&r
000
MC/DC0%0/2
defined $up and $self->[$i][$j] > $up
defined $up $self->[$i][$j] > $up
Truth table0%0/3
A and B
A= defined $upB= $self->[$i][$j] > $up
ABresult
0X0
100
111
2924
2925 }
2926
2927 }
2928
2929 }
2930  
2931 # return
2932 0 return($self);
2933  
2934 }
2935  
2936 # exponentiate matrix elements
2937 # exponent may be a scalar or a vector
2938 # no testing, 'inf' or 'nan' values are possible
2939 # parameter: (exponent)
2940 # returns: (new_matrix_object)
2941 sub power {
2942  
2943 # get parameters
2944 0 ▶ my ($self, $x) = @_;
Subroutine power: not called CC=11 SCAR=48.8
Pod undocumented
2945  
2946 # local variables
2947 0 my ($result);
2948  
2949 # check if ICC::Support::Lapack module is loaded
2950 0 state $lapack = defined($INC{'ICC/Support/Lapack.pm'});
2951  
2952 # if ICC::Support::Lapack module is loaded
2953 0 ▶ if ($lapack) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lapack) { } 0 0 0
2954
2955 # compute using Lapack module
2956 0 $result = ICC::Support::Lapack::power($self, $x);
2957
2958 } else {
2959
2960 # if exponent a scalar
2961 0 ▶ if (! ref($x)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $x) { } 0 0 0
elsif (ref $x eq 'ARRAY') { } 0 0 0
2962
2963 # verify exponent is a number
2964 0 ▶ (Scalar::Util::looks_like_number($x)) or croak('exponent must be a real number');
Branch: 1 branch
BranchTrueFalseTotal
&Scalar::Util::looks_like_number($x) or croak("exponent must be a real number") 0 0 0
2965
2966 # for each row
2967 0 for my $i (0 .. $#{$self}) {
2968
2969 # for each column
2970 0 for my $j (0 .. $#{$self->[0]}) {
2971
2972 # compute element
2973 0 $result->[$i][$j] = $self->[$i][$j]**$x;
2974
2975 }
2976
2977 }
2978
2979 # if exponent a vector
2980 } elsif (ref($x) eq 'ARRAY') {
2981
2982 # verify exponents are numbers
2983 0 ▶ (@{$x} == grep {Scalar::Util::looks_like_number($_)} @{$x}) or croak('exponents must be real numbers');
Branch: 1 branch
BranchTrueFalseTotal
@{$x;} == grep({&Scalar::Util::looks_like_number($_);} @{$x;}) or croak("exponents must be real numbers") 0 0 0
2984
2985 # verify vector size same as column size
2986 0 ▶ ($#{$x} == $#{$self->[0]}) or croak('exponent vector wrong size');
Branch: 1 branch
BranchTrueFalseTotal
$#{$x;} == $#{$$self[0];} or croak("exponent vector wrong size") 0 0 0
2987
2988 # for each row
2989 0 for my $i (0 .. $#{$self}) {
2990
2991 # for each column
2992 0 for my $j (0 .. $#{$self->[0]}) {
2993
2994 # compute element, avoiding math overflow
2995 0 $result->[$i][$j] = $self->[$i][$j]**$x->[$j];
2996
2997 }
2998
2999 }
3000
3001 } else {
3002
3003 # error
3004 0 croak('exponent must be a scalar or vector');
3005
3006 }
3007
3008 }
3009  
3010 # return new object
3011 0 return(bless($result, 'Math::Matrix'));
3012  
3013 }
3014  
3015 # convert matrix working space from xyz to XYZ
3016 # default XYZ white point is D50
3017 # parameter: ([XYZ_white_point])
3018 # returns: (new_matrix_object)
3019 sub xyz2XYZ {
3020  
3021 # get parameters
3022 0 ▶ my ($self, $wtpt) = @_;
Subroutine xyz2XYZ: not called CC=7 SCAR=40.3
Pod undocumented
3023  
3024 # local variables
3025 0 my ($mat);
3026  
3027 # verify a 3x3 matrix
3028 0 ▶ (@{$self} == 3 && @{$self->[0]} == 3) or croak('must be a 3x3 matrix');
Branch: 1 branch
BranchTrueFalseTotal
@{$self;} == 3 and @{$self->[0];} == 3 or croak("must be a 3x3 matrix") 0 0 0
Condition0%0/3
@{$self;} == 3 and @{$self->[0];} == 3
!ll&&!rl&&r
000
MC/DC0%0/2
@{$self;} == 3 and @{$self->[0];} == 3
@{$self;} == 3 @{$self->[0];} == 3
Truth table0%0/3
A and B
A= @{$self;} == 3B= @{$self->[0];} == 3
ABresult
0X0
100
111
3029  
3030 # set white point to D50 if undefined
3031 0 ▶ $wtpt = [96.42, 100, 82.49] if (! defined($wtpt));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wtpt 0 0 0
3032  
3033 # clone matrix
3034 0 $mat = Storable::dclone($self);
3035  
3036 # for each row
3037 0 for my $i (0 .. 2) {
3038
3039 # for each column
3040 0 for my $j (0 .. 2) {
3041
3042 # modify non-diagonal element
3043 0 ▶ $mat->[$i][$j] *= $wtpt->[$i]/$wtpt->[$j] if ($i != $j);
Branch: 1 branch
BranchTrueFalseTotal
if $i != $j 0 0 0
3044
3045 }
3046
3047 }
3048  
3049 # return
3050 0 return($mat);
3051  
3052 }
3053  
3054 # convert matrix working space from XYZ to xyz
3055 # default XYZ white point is D50
3056 # parameter: ([XYZ_white_point])
3057 # returns: (new_matrix_object)
3058 sub XYZ2xyz {
3059  
3060 # get parameters
3061 0 ▶ my ($self, $wtpt) = @_;
Subroutine XYZ2xyz: not called CC=7 SCAR=40.3
Pod undocumented
3062  
3063 # local variables
3064 0 my ($mat);
3065  
3066 # verify a 3x3 matrix
3067 0 ▶ (@{$self} == 3 && @{$self->[0]} == 3) or croak('must be a 3x3 matrix');
Branch: 1 branch
BranchTrueFalseTotal
@{$self;} == 3 and @{$self->[0];} == 3 or croak("must be a 3x3 matrix") 0 0 0
Condition0%0/3
@{$self;} == 3 and @{$self->[0];} == 3
!ll&&!rl&&r
000
MC/DC0%0/2
@{$self;} == 3 and @{$self->[0];} == 3
@{$self;} == 3 @{$self->[0];} == 3
Truth table0%0/3
A and B
A= @{$self;} == 3B= @{$self->[0];} == 3
ABresult
0X0
100
111
3068  
3069 # set white point to D50 if undefined
3070 0 ▶ $wtpt = [96.42, 100, 82.49] if (! defined($wtpt));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wtpt 0 0 0
3071  
3072 # clone matrix
3073 0 $mat = Storable::dclone($self);
3074  
3075 # for each row
3076 0 for my $i (0 .. 2) {
3077
3078 # for each column
3079 0 for my $j (0 .. 2) {
3080
3081 # modify non-diagonal element
3082 0 ▶ $mat->[$i][$j] *= $wtpt->[$j]/$wtpt->[$i] if ($i != $j);
Branch: 1 branch
BranchTrueFalseTotal
if $i != $j 0 0 0
3083
3084 }
3085
3086 }
3087  
3088 # return
3089 0 return($mat);
3090  
3091 }
3092  
3093 41 1;
« blib/lib/ICC/Profile/clut.pm ↑ Summary blib/lib/ICC/Profile/gbd_.pm »