blib/lib/ICC/Shared.pm

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