lib/ICC/Support/PCS.pm

filter: Statementstmt 5.5% 19 / 344 Branchbran 0.7% 2 / 262 Conditioncond 1.9% 1 / 51 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 16.1% 5 / 31 Podpod 11.1% 1 / 9 totaltotal 3.8% 28 / 731 CC 168 SCAR 52.6
CC
168
Cov
3%
_fwd
702.0
_rev
182.0
_fwd_jac
182.0
CRAP 192.4
« lib/ICC/Support/nNET.pm ↑ Summary blib/lib/ICC/Support/PCS.pm »
1 package ICC::Support::PCS;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = 0.73;
7  
8 # revised 2017-07-08
9 #
10 # Copyright © 2004-2018 by William B. Birkett
11  
12 # add development directory
13 2 ▶use lib 'lib';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # inherit from Shared
16 2 ▶use parent qw(ICC::Shared);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =encoding utf8
19  
20 list of supported PCS connection spaces
21  
22 0 - 8-bit ICC CIELAB (100, 0, 0 => 255/255, 128/255, 128/255 = 1, 0.50196, 0.50196)
23 0 - 16-bit ICC CIELAB (100, 0, 0 => 65535/65535, 32896/65535, 32896/65535 = 1, 0.50196, 0.50196)
24 1 - 16-bit ICC legacy L*a*b* (100, 0, 0 => 65280/65535, 32768/65535, 32768/65535 = 0.99611, 0.50001, 0.50001)
25 2 - 16-bit EFI/Monaco L*a*b* (100, 0, 0 => 65535/65535, 32768/65535, 32768/65535 = 1, 0.50001, 0.50001)
26 3 - L*a*b* (100, 0, 0 => 100, 0, 0)
27 4 - LxLyLz (100, 0, 0 => 100, 100, 100)
28 5 - unit LxLyLz (100, 0, 0 => 1, 1, 1)
29 6 - xyY (100, 0, 0 => 0.34570, 0.35854, 100)
30 7 - 16-bit ICC XYZ (100, 0, 0 => 0.9642 * 32768/65535, 32768/65535, 0.8249 * 32768/65535 = 0.48211, 0.50001, 0.41246)
31 8 - 32-bit ICC XYZNumber (100, 0, 0 => 0.9642, 1.0, 0.8249)
32 9 - xyz (100, 0, 0 => 1, 1, 1)
33 10 - XYZ (100, 0, 0 => 96.42, 100, 82.49)
34  
35 explanation and application
36  
37 option 0 is for both 8-bit and 16-bit CIELAB encoding. it is listed twice to show the equivalence.
38 option 1 is the 16-bit L*a*b* encoding from the v2 specification. option 1 also applies to mft2 and ncl2 tags within v4 profiles.
39 option 2 is a non-standard L*a*b* encoding used by EFI and Monaco.
40 option 3 is standard L*a*b* encoding, used in measurement files and floating point tags (e.g. D2Bx, B2Dx).
41 option 4 is L* encoding of the xyz channels.
42 option 5 is unit L* encoding of the xyz channels.
43 option 6 is chromaticity plus Y.
44 option 7 is the 16-bit XYZ encoding used by v2 and v4. 8-bit XYZ encoding is undefined by the ICC specification.
45 option 8 is the 32-bit format used by XYZ tags, and the format used to set absolute colorimetry when creating PCS objects.
46 option 9 is X/Xn, Y/Yn, Z/Zn, as defined in ISO 13655.
47 option 10 is standard XYZ encoding, used in measurement files.
48  
49 =cut
50  
51 # make PCS connection object
52 # structure of the input/output parameter arrays is: (pcs_connection_space, [white_point, [black_point]])
53 # white point and black point values are optional. default values are D50 for white point and (0, 0, 0) for black point.
54 # white point and black point are encoded as ICC XYZNumbers, which is how they are stored within ICC profiles.
55 # for explanation of tone compression linearity, see 'tone_compression_notes.txt'.
56 # default tone compression linearity = 0 (linear tone compression).
57 # parameters: ()
58 # parameters: (ref_to_input_parameter_array, ref_to_output_parameter_array, [tone_compression_linearity])
59 sub new {
60  
61 # get object class
62 1 ▶ my $class = shift();
Subroutine new: called CC=4 SCAR=16.6
Pod undocumented
63
64 # create empty PCS object
65 1 my $self = [
66 {}, # object header
67 [], # parameter array
68 [], # tone compression array
69 0 # clipping flag
70 ];
71
72 # if 2 or 3 parameters
73 1 ▶ if (@_ == 2 || @_ == 3) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 2 or @_ == 3) { } 0 1 1
elsif (@_) { } 0 1 1
Condition33%1/3
@_ == 2 or @_ == 3
l!l&&r!l&&!r
001
MC/DC0%0/2
@_ == 2 or @_ == 3
@_ == 2 @_ == 3
Truth table33%1/3
A or B
A= @_ == 2B= @_ == 3
ABresult
1X1
011
000
74
75 # create new object from parameters
76 0 _new_pcs($self, @_);
77
78 # if any parameters
79 } elsif (@_) {
80
81 # error
82 0 croak('wrong number of parameters');
83
84 }
85
86 # bless object
87 1 bless($self, $class);
88
89 # return object reference
90 1 return($self);
91
92 }
93  
94 # get/set clipping flag
95 # if flag is true, values are clipped
96 # parameters: ([new_flag_value])
97 # returns: (flag_value)
98 sub clip {
99
100 # get object reference
101 0 ▶ my $self = shift();
Subroutine clip: not called CC=3 SCAR=24.8
Pod undocumented
102
103 # if there are parameters
104 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
105
106 # if one parameter
107 0 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
108
109 # set object clipping mask value
110 0 $self->[3] = shift();
111
112 } else {
113
114 # error
115 0 croak('more than one parameter');
116
117 }
118
119 }
120
121 # return clipping mask value
122 0 return($self->[3]);
123
124 }
125  
126 # get/set tone compression linearity
127 # parameters: ([new_linearity_value])
128 # returns: (linearity_value)
129 sub linearity {
130
131 # get object reference
132 0 ▶ my $self = shift();
Subroutine linearity: not called CC=3 SCAR=24.8
Pod undocumented
133
134 # if there are parameters
135 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
136
137 # if one parameter
138 0 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
139
140 # set linearity value
141 0 $self->[2][1] = shift();
142
143 # update tone compression coefficients
144 0 tc_pars($self);
145
146 } else {
147
148 # error
149 0 croak('more than one parameter');
150
151 }
152
153 }
154
155 # return linearity value
156 0 return($self->[2][1]);
157
158 }
159  
160 # set input/output gamut scale
161 # sets input/output black point vector to the white point vector x (1 - scale)
162 # a zero value leaves the corresponding black point unchanged
163 # parameters: (input_gamut_scale_factor, output_gamut_scale_factor)
164 sub scale {
165
166 # get object reference
167 0 ▶ my $self = shift();
Subroutine scale: not called CC=7 SCAR=40.3
Pod undocumented
168
169 # verify parameters
170 0 ▶ (@_ == 2 && 2 == grep {! ref()} @_) || croak('invalid scale inputs');
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 and 2 == grep({not ref $_;} @_) or croak("invalid scale inputs") 0 0 0
Condition0%0/3
@_ == 2 and 2 == grep({not ref $_;} @_)
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 2 and 2 == grep({not ref $_;} @_)
@_ == 2 2 == grep({not ref $_;} @_)
Truth table0%0/3
A and B
A= @_ == 2B= 2 == grep({not ref $_;} @_)
ABresult
0X0
100
111
171
172 # for input and output
173 0 for my $i (0 .. 1) {
174
175 # if gamut scale factor != 0, and white point defined
176 0 ▶ if ($_[$i] != 0 && defined($self->[1][$i][1])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[$i] != 0 and defined $self->[1][$i][1]) 0 0 0
Condition0%0/3
$_[$i] != 0 and defined $self->[1][$i][1]
!ll&&!rl&&r
000
MC/DC0%0/2
$_[$i] != 0 and defined $self->[1][$i][1]
$_[$i] != 0 defined $self->[1][$i][1]
Truth table0%0/3
A and B
A= $_[$i] != 0B= defined $self->[1][$i][1]
ABresult
0X0
100
111
177
178 # for XYZ
179 0 for my $j (0 .. 2) {
180
181 # set black point to scaled white point value
182 0 $self->[1][$i][2][$j] = (1 - $_[$i]) * $self->[1][$i][1][$j];
183
184 }
185
186 }
187
188 }
189
190 # update tone compression coefficients
191 0 tc_pars($self);
192
193 }
194  
195 # transform data
196 # supported input types:
197 # parameters: (list, [hash])
198 # parameters: (vector, [hash])
199 # parameters: (matrix, [hash])
200 # parameters: (Math::Matrix_object, [hash])
201 # parameters: (structure, [hash])
202 # returns: (same_type_as_input)
203 sub transform {
204  
205 # set hash value (0 or 1)
206 0 ▶ my $h = ref($_[-1]) eq 'HASH' ? 1 : 0;
Subroutine transform: not called CC=12 SCAR=50.5
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'HASH' ? : 0 0 0
207  
208 # if input a 'Math::Matrix' object
209 0 ▶ if (@_ == $h + 2 && UNIVERSAL::isa($_[1], 'Math::Matrix')) {
Branch: 3 branches
BranchTrueFalseTotal
if (@_ == $h + 2 and UNIVERSAL::isa($_[1], 'Math::Matrix')) { } 0 0 0
elsif (@_ == $h + 2 and ref $_[1] eq 'ARRAY') { } 0 0 0
elsif (@_ == $h + 1 + grep({&Scalar::Util::looks_like_number($_);} @_)) { } 0 0 0
Condition0%0/3
@_ == $h + 2 and UNIVERSAL::isa($_[1], "Math::Matrix")
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == $h + 2 and UNIVERSAL::isa($_[1], "Math::Matrix")
@_ == $h + 2 UNIVERSAL::isa($_[1], "Math::Matrix")
Truth table0%0/3
A and B
A= @_ == $h + 2B= UNIVERSAL::isa($_[1], "Math::Matrix")
ABresult
0X0
100
111
210
211 # call matrix transform
212 0 &_trans2;
213
214 # if input an array reference
215 ▶ } elsif (@_ == $h + 2 && ref($_[1]) eq 'ARRAY') {
Condition0%0/3
@_ == $h + 2 and ref $_[1] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == $h + 2 and ref $_[1] eq "ARRAY"
@_ == $h + 2 ref $_[1] eq "ARRAY"
Truth table0%0/3
A and B
A= @_ == $h + 2B= ref $_[1] eq "ARRAY"
ABresult
0X0
100
111
216
217 # if array contains numbers (vector)
218 0 ▶ if (! ref($_[1][0]) && @{$_[1]} == grep {Scalar::Util::looks_like_number($_)} @{$_[1]}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $_[1][0] and @{$_[1];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[1];})) { } 0 0 0
elsif (ref $_[1][0] eq 'ARRAY' and @{$_[1];} == grep({ref $_ eq 'ARRAY' and &Scalar::Util::looks_like_number($_->[0]);} @{$_[1];})) { } 0 0 0
Condition0%0/3
not ref $_[1][0] and @{$_[1];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[1];})
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $_[1][0] and @{$_[1];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[1];})
not ref $_[1][0] @{$_[1];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[1];})
Truth table0%0/3
not(A) and B
A= not ref $_[1][0]B= @{$_[1];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[1];})
ABresult
0X0
100
111
219
220 # call vector transform
221 0 &_trans1;
222
223 # if array contains vectors (2-D array)
224 0 ▶ } elsif (ref($_[1][0]) eq 'ARRAY' && @{$_[1]} == grep {ref($_) eq 'ARRAY' && Scalar::Util::looks_like_number($_->[0])} @{$_[1]}) {
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq "ARRAY" and &Scalar::Util::looks_like_number($_->[0]) 0 0 0
Condition0%0/3
ref $_[1][0] eq "ARRAY" and @{$_[1];} == grep({ref $_ eq "ARRAY" and &Scalar::Util::looks_like_number($_->[0]);} @{$_[1];})
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[1][0] eq "ARRAY" and @{$_[1];} == grep({ref $_ eq "ARRAY" and &Scalar::Util::looks_like_number($_->[0]);} @{$_[1];})
ref $_[1][0] eq "ARRAY" @{$_[1];} == grep({ref $_ eq "ARRAY" and &Scalar::Util::looks_like_number($_->[0]);} @{$_[1];})
Truth table0%0/3
A and B
A= ref $_[1][0] eq "ARRAY"B= @{$_[1];} == grep({ref $_ eq "ARRAY" and &Scalar::Util::looks_like_number($_->[0]);} @{$_[1];})
ABresult
0X0
100
111
225
226 # call matrix transform
227 0 &_trans2;
228
229 } else {
230
231 # call structure transform
232 0 &_trans3;
233
234 }
235
236 # if input a list (of numbers)
237 0 } elsif (@_ == $h + 1 + grep {Scalar::Util::looks_like_number($_)} @_) {
238
239 # call list transform
240 0 &_trans0;
241
242 } else {
243
244 # error
245 0 croak('invalid transform input');
246
247 }
248  
249 }
250  
251 # invert data
252 # supported input types:
253 # parameters: (list, [hash])
254 # parameters: (vector, [hash])
255 # parameters: (matrix, [hash])
256 # parameters: (Math::Matrix_object, [hash])
257 # parameters: (structure, [hash])
258 # returns: (same_type_as_input)
259 sub inverse {
260  
261 # set hash value (0 or 1)
262 0 ▶ my $h = ref($_[-1]) eq 'HASH' ? 1 : 0;
Subroutine inverse: not called CC=12 SCAR=50.5
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'HASH' ? : 0 0 0
263  
264 # if input a 'Math::Matrix' object
265 0 ▶ if (@_ == $h + 2 && UNIVERSAL::isa($_[1], 'Math::Matrix')) {
Branch: 3 branches
BranchTrueFalseTotal
if (@_ == $h + 2 and UNIVERSAL::isa($_[1], 'Math::Matrix')) { } 0 0 0
elsif (@_ == $h + 2 and ref $_[1] eq 'ARRAY') { } 0 0 0
elsif (@_ == $h + 1 + grep({&Scalar::Util::looks_like_number($_);} @_)) { } 0 0 0
Condition0%0/3
@_ == $h + 2 and UNIVERSAL::isa($_[1], "Math::Matrix")
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == $h + 2 and UNIVERSAL::isa($_[1], "Math::Matrix")
@_ == $h + 2 UNIVERSAL::isa($_[1], "Math::Matrix")
Truth table0%0/3
A and B
A= @_ == $h + 2B= UNIVERSAL::isa($_[1], "Math::Matrix")
ABresult
0X0
100
111
266
267 # call matrix transform
268 0 &_inv2;
269
270 # if input an array reference
271 ▶ } elsif (@_ == $h + 2 && ref($_[1]) eq 'ARRAY') {
Condition0%0/3
@_ == $h + 2 and ref $_[1] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == $h + 2 and ref $_[1] eq "ARRAY"
@_ == $h + 2 ref $_[1] eq "ARRAY"
Truth table0%0/3
A and B
A= @_ == $h + 2B= ref $_[1] eq "ARRAY"
ABresult
0X0
100
111
272
273 # if array contains numbers (vector)
274 0 ▶ if (! ref($_[1][0]) && @{$_[1]} == grep {Scalar::Util::looks_like_number($_)} @{$_[1]}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $_[1][0] and @{$_[1];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[1];})) { } 0 0 0
elsif (ref $_[1][0] eq 'ARRAY' and @{$_[1];} == grep({ref $_ eq 'ARRAY' and &Scalar::Util::looks_like_number($_->[0]);} @{$_[1];})) { } 0 0 0
Condition0%0/3
not ref $_[1][0] and @{$_[1];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[1];})
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $_[1][0] and @{$_[1];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[1];})
not ref $_[1][0] @{$_[1];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[1];})
Truth table0%0/3
not(A) and B
A= not ref $_[1][0]B= @{$_[1];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[1];})
ABresult
0X0
100
111
275
276 # call vector transform
277 0 &_inv1;
278
279 # if array contains vectors (2-D array)
280 0 ▶ } elsif (ref($_[1][0]) eq 'ARRAY' && @{$_[1]} == grep {ref($_) eq 'ARRAY' && Scalar::Util::looks_like_number($_->[0])} @{$_[1]}) {
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq "ARRAY" and &Scalar::Util::looks_like_number($_->[0]) 0 0 0
Condition0%0/3
ref $_[1][0] eq "ARRAY" and @{$_[1];} == grep({ref $_ eq "ARRAY" and &Scalar::Util::looks_like_number($_->[0]);} @{$_[1];})
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[1][0] eq "ARRAY" and @{$_[1];} == grep({ref $_ eq "ARRAY" and &Scalar::Util::looks_like_number($_->[0]);} @{$_[1];})
ref $_[1][0] eq "ARRAY" @{$_[1];} == grep({ref $_ eq "ARRAY" and &Scalar::Util::looks_like_number($_->[0]);} @{$_[1];})
Truth table0%0/3
A and B
A= ref $_[1][0] eq "ARRAY"B= @{$_[1];} == grep({ref $_ eq "ARRAY" and &Scalar::Util::looks_like_number($_->[0]);} @{$_[1];})
ABresult
0X0
100
111
281
282 # call matrix transform
283 0 &_inv2;
284
285 } else {
286
287 # call structure transform
288 0 &_inv3;
289
290 }
291
292 # if input a list (of numbers)
293 0 } elsif (@_ == $h + 1 + grep {Scalar::Util::looks_like_number($_)} @_) {
294
295 # call list transform
296 0 &_inv0;
297
298 } else {
299
300 # error
301 0 croak('invalid transform input');
302
303 }
304  
305 }
306  
307 # compute Jacobian matrix
308 # parameters: (input_vector, [hash])
309 # returns: (Jacobian_matrix, [output_vector])
310 sub jacobian {
311  
312 # get parameters
313 0 ▶ my ($self, $in, $hash) = @_;
Subroutine jacobian: not called CC=12 SCAR=50.5
Pod undocumented
314  
315 # local variables
316 0 my ($pcsi, $pcso, @t, @d, $jac);
317  
318 # verify 3 channels
319 0 ▶ (@{$in} == 3) || croak('PCS object input not 3 channels');
Branch: 1 branch
BranchTrueFalseTotal
@{$in;} == 3 or croak("PCS object input not 3 channels") 0 0 0
320  
321 # get input PCS
322 0 $pcsi = $self->[1][0][0];
323  
324 # get output PCS
325 0 $pcso = $self->[1][1][0];
326  
327 # convert from input PCS
328 0 @t = _rev($pcsi, @{$in});
329  
330 # compute _rev Jacobian
331 0 $jac = _rev_jac($pcsi, @{$in});
332  
333 # if tone compression is required
334 0 ▶ if ($self->[2][0]) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->[2][0]) { } 0 0 0
elsif ($pcsi <= 5 and $pcso >= 6) { } 0 0 0
elsif ($pcsi >= 6 and $pcso <= 5) { } 0 0 0
335
336 # if input PCS is L*a*b*
337 0 ▶ if ($pcsi <= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pcsi <= 5) 0 0 0
338
339 # apply Lab2xyz Jacobian
340 0 $jac = ICC::Shared::Lab2xyz_jac(@t) * $jac;
341
342 # convert to xyz
343 0 @t = ICC::Shared::_Lab2xyz(@t);
344
345 }
346
347 # compute forward derivatives
348 0 @d = _tc_derv($self, 0, @t);
349
350 # for each output
351 0 for my $i (0 .. 2) {
352
353 # for each input
354 0 for my $j (0 .. 2) {
355
356 # adjust Jacobian
357 0 $jac->[$i][$j] *= $d[$i];
358
359 }
360
361 }
362
363 # compute forward tone compression
364 0 @t = _tc($self, 0, @t);
365
366 # if output PCS is L*a*b*
367 0 ▶ if ($pcso <= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pcso <= 5) 0 0 0
368
369 # apply xyz2Lab Jacobian
370 0 $jac = ICC::Shared::xyz2Lab_jac(@t) * $jac;
371
372 # convert to L*a*b*
373 0 @t = ICC::Shared::_xyz2Lab(@t);
374
375 }
376
377 # if input PCS is L*a*b* and output PCS is xyz
378 ▶ } elsif ($pcsi <= 5 && $pcso >= 6) {
Condition0%0/3
$pcsi <= 5 and $pcso >= 6
!ll&&!rl&&r
000
MC/DC0%0/2
$pcsi <= 5 and $pcso >= 6
$pcsi <= 5 $pcso >= 6
Truth table0%0/3
A and B
A= $pcsi <= 5B= $pcso >= 6
ABresult
0X0
100
111
379
380 # apply Lab2xyz Jacobian
381 0 $jac = ICC::Shared::Lab2xyz_jac(@t) * $jac;
382
383 # convert to xyz
384 0 @t = ICC::Shared::_Lab2xyz(@t);
385
386 # if input PCS is xyz and output PCS is L*a*b*
387 ▶ } elsif ($pcsi >= 6 && $pcso <= 5) {
Condition0%0/3
$pcsi >= 6 and $pcso <= 5
!ll&&!rl&&r
000
MC/DC0%0/2
$pcsi >= 6 and $pcso <= 5
$pcsi >= 6 $pcso <= 5
Truth table0%0/3
A and B
A= $pcsi >= 6B= $pcso <= 5
ABresult
0X0
100
111
388
389 # apply xyz2Lab Jacobian
390 0 $jac = ICC::Shared::xyz2Lab_jac(@t) * $jac;
391
392 # convert to L*a*b*
393 0 @t = ICC::Shared::_xyz2Lab(@t);
394
395 }
396  
397 # apply forward Jacobian
398 0 $jac = _fwd_jac($pcso, @t) * $jac;
399  
400 # if output values wanted
401 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
402
403 # return Jacobian and output values
404 0 return($jac, [_fwd($pcso, $self->[3], @t)]);
405
406 } else {
407
408 # return Jacobian only
409 0 return($jac);
410
411 }
412
413 }
414  
415 # compute tone compression coefficients
416 # parameters: (object_reference)
417 sub tc_pars {
418
419 # get object reference
420 0 ▶ my $self = shift();
Subroutine tc_pars: not called CC=5 SCAR=34.0
Pod undocumented
421
422 # local variables
423 0 my ($lin, $elin);
424
425 # set tc flag (true if tc required)
426 0 ▶ $self->[2][0] = grep {$self->[1][0][1][$_] != $self->[1][1][1][$_] || $self->[1][0][2][$_] != $self->[1][1][2][$_]} (0 .. 2);
Branch: 1 branch
BranchTrueFalseTotal
$self->[1][0][1][$_] != $self->[1][1][1][$_] or $self->[1][0][2][$_] != $self->[1][1][2][$_] 0 0 0
427
428 # if non-linear tone compression
429 0 ▶ if ($lin = $self->[2][1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lin = $self->[2][1]) { } 0 0 0
430
431 # compute value
432 0 $elin = exp($lin);
433
434 # for each xyz
435 0 for my $i (0 .. 2) {
436
437 # compute a = (exp(r) - exp(r * y0/y1))/(exp(r) - exp(r * x0/x1))
438 0 $self->[2][2][$i] = ($elin - exp($lin * $self->[1][1][2][$i]/$self->[1][1][1][$i]))/($elin - exp($lin * $self->[1][0][2][$i]/$self->[1][0][1][$i]));
439
440 # compute b = (1 - a) * exp(r)
441 0 $self->[2][3][$i] = (1 - $self->[2][2][$i]) * $elin;
442
443 }
444
445 # else linear tone compression
446 } else {
447
448 # for each xyz
449 0 for my $i (0 .. 2) {
450
451 # compute a = (y1 - y0)/(x1 - x0)
452 0 $self->[2][2][$i] = ($self->[1][1][1][$i] - $self->[1][1][2][$i])/($self->[1][0][1][$i] - $self->[1][0][2][$i]);
453
454 # compute b = y1 - a * x1
455 0 $self->[2][3][$i] = $self->[1][1][1][$i] - $self->[2][2][$i] * $self->[1][0][1][$i];
456
457 }
458
459 }
460
461 }
462  
463 # print object contents to string
464 # format is an array structure
465 # parameter: ([format])
466 # returns: (string)
467 sub sdump {
468  
469 # get parameters
470 0 ▶ my ($self, $p) = @_;
Subroutine sdump: not called CC=5 SCAR=34.0
Pod documented
471  
472 # local variables
473 0 my ($s, $fmt);
474  
475 # resolve parameter to an array reference
476 0 ▶ $p = defined($p) ? ref($p) eq 'ARRAY' ? $p : [$p] : [];
Branch: 2 branches
BranchTrueFalseTotal
defined $p ? : 0 0 0
ref $p eq 'ARRAY' ? : 0 0 0
477  
478 # get format string
479 0 ▶ $fmt = defined($p->[0]) && ! ref($p->[0]) ? $p->[0] : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $p->[0] && !ref($p->[0]) ? : 0 0 0
Condition0%0/3
defined $p->[0] and not ref $p->[0]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $p->[0] and not ref $p->[0]
defined $p->[0] not ref $p->[0]
Truth table0%0/3
A and not(B)
A= defined $p->[0]B= not ref $p->[0]
ABresult
0X0
100
111
480  
481 # set string to object ID
482 0 $s = sprintf("'%s' object, (0x%x)\n", ref($self), $self);
483  
484 # return
485 0 return($s);
486  
487 }
488  
489 # transform list
490 # parameters: (object_reference, list, [hash])
491 # returns: (list)
492 sub _trans0 {
493  
494 # local variables
495 0 ▶ my ($self, $hash);
Subroutine _trans0: not called CC=2 SCAR=17.9
496  
497 # get object reference
498 0 $self = shift();
499  
500 # get optional hash
501 0 ▶ $hash = pop() if (ref($_[-1]) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "HASH" 0 0 0
502  
503 # transform single value
504 0 return(_transform($self, 0, @_));
505  
506 }
507  
508 # transform vector
509 # parameters: (object_reference, vector, [hash])
510 # returns: (vector)
511 sub _trans1 {
512  
513 # get parameters
514 0 ▶ my ($self, $in, $hash) = @_;
Subroutine _trans1: not called CC=1 SCAR=6.9
515  
516 # transform vector
517 0 return([_transform($self, 0, @{$in})]);
518  
519 }
520  
521 # transform matrix (2-D array -or- Math::Matrix object)
522 # parameters: (object_reference, matrix, [hash])
523 # returns: (matrix)
524 sub _trans2 {
525  
526 # get parameters
527 0 ▶ my ($self, $in, $hash) = @_;
Subroutine _trans2: not called CC=3 SCAR=24.8
528  
529 # local variable
530 0 my ($out);
531  
532 # for each sample
533 0 for my $i (0 .. $#{$in}) {
534
535 # transform sample
536 0 $out->[$i] = [_transform($self, 0, @{$in->[$i]})];
537
538 }
539  
540 # return
541 0 ▶ return(UNIVERSAL::isa($in, 'Math::Matrix') ? bless($out, 'Math::Matrix') : $out);
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($in, 'Math::Matrix') ? : 0 0 0
542  
543 }
544  
545 # transform structure
546 # parameters: (object_reference, structure, [hash])
547 # returns: (structure)
548 sub _trans3 {
549  
550 # get parameters
551 0 ▶ my ($self, $in, $hash) = @_;
Subroutine _trans3: not called CC=1 SCAR=6.9
552  
553 # transform the array structure
554 0 _crawl($self, $in, my $out = [], $hash);
555  
556 # return
557 0 return($out);
558  
559 }
560  
561 # recursive transform
562 # array structure is traversed until scalar arrays are found and transformed
563 # parameters: (ref_to_object, ref_to_input_array, ref_to_output_array, hash)
564 sub _crawl {
565  
566 # get parameters
567 0 ▶ my ($self, $in, $out, $hash) = @_;
Subroutine _crawl: not called CC=4 SCAR=30.0
568  
569 # if input is a vector (reference to a scalar array)
570 0 ▶ if (@{$in} == grep {! ref()} @{$in}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$in;} == grep({not ref $_;} @{$in;})) { } 0 0 0
571
572 # transform input vector and copy to output
573 0 @{$out} = @{_trans1($self, $in, $hash)};
574
575 } else {
576
577 # for each input element
578 0 for my $i (0 .. $#{$in}) {
579
580 # if an array reference
581 0 ▶ if (ref($in->[$i]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $in->[$i] eq 'ARRAY') { } 0 0 0
582
583 # transform next level
584 0 _crawl($self, $in->[$i], $out->[$i] = [], $hash);
585
586 } else {
587
588 # error
589 0 croak('invalid transform input');
590
591 }
592
593 }
594
595 }
596
597 }
598  
599 # invert list
600 # parameters: (object_reference, list, [hash])
601 # returns: (list)
602 sub _inv0 {
603  
604 # local variables
605 0 ▶ my ($self, $hash);
Subroutine _inv0: not called CC=2 SCAR=17.9
606  
607 # get object reference
608 0 $self = shift();
609  
610 # get optional hash
611 0 ▶ $hash = pop() if (ref($_[-1]) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "HASH" 0 0 0
612  
613 # invert single value
614 0 return(_transform($self, 1, @_));
615  
616 }
617  
618 # invert vector
619 # parameters: (object_reference, vector, [hash])
620 # returns: (vector)
621 sub _inv1 {
622  
623 # get parameters
624 0 ▶ my ($self, $in, $hash) = @_;
Subroutine _inv1: not called CC=1 SCAR=6.9
625  
626 # invert vector
627 0 return([_transform($self, 1, @{$in})]);
628  
629 }
630  
631 # invert matrix (2-D array -or- Math::Matrix object)
632 # parameters: (object_reference, matrix, [hash])
633 # returns: (matrix)
634 sub _inv2 {
635  
636 # get parameters
637 0 ▶ my ($self, $in, $hash) = @_;
Subroutine _inv2: not called CC=3 SCAR=24.8
638  
639 # local variable
640 0 my ($out);
641  
642 # for each sample
643 0 for my $i (0 .. $#{$in}) {
644
645 # invert sample
646 0 $out->[$i] = [_transform($self, 1, @{$in->[$i]})];
647
648 }
649  
650 # return
651 0 ▶ return(UNIVERSAL::isa($in, 'Math::Matrix') ? bless($out, 'Math::Matrix') : $out);
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($in, 'Math::Matrix') ? : 0 0 0
652  
653 }
654  
655 # invert structure
656 # parameters: (object_reference, structure, [hash])
657 # returns: (structure)
658 sub _inv3 {
659  
660 # get parameters
661 0 ▶ my ($self, $in, $hash) = @_;
Subroutine _inv3: not called CC=1 SCAR=6.9
662  
663 # invert the array structure
664 0 _crawl2($self, $in, my $out = [], $hash);
665  
666 # return
667 0 return($out);
668  
669 }
670  
671 # recursive transform
672 # array structure is traversed until scalar arrays are found and inverted
673 # parameters: (ref_to_object, ref_to_input_array, ref_to_output_array, hash)
674 sub _crawl2 {
675  
676 # get parameters
677 0 ▶ my ($self, $in, $out, $hash) = @_;
Subroutine _crawl2: not called CC=4 SCAR=30.0
678  
679 # if input is a vector (reference to a scalar array)
680 0 ▶ if (@{$in} == grep {! ref()} @{$in}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$in;} == grep({not ref $_;} @{$in;})) { } 0 0 0
681
682 # invert input vector and copy to output
683 0 @{$out} = @{_inv1($in, $hash)};
684
685 } else {
686
687 # for each input element
688 0 for my $i (0 .. $#{$in}) {
689
690 # if an array reference
691 0 ▶ if (ref($in->[$i]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $in->[$i] eq 'ARRAY') { } 0 0 0
692
693 # invert next level
694 0 _crawl($self, $in->[$i], $out->[$i] = [], $hash);
695
696 } else {
697
698 # error
699 0 croak('invalid inverse input');
700
701 }
702
703 }
704
705 }
706
707 }
708  
709 # transform sample data
710 # direction: 0 - normal, 1 - inverse
711 # parameters: (object_reference, direction, array_of_input_values)
712 # returns: (array_of_output_values)
713 sub _transform {
714  
715 # get parameters
716 0 ▶ my ($self, $dir, @in) = @_;
Subroutine _transform: not called CC=9 SCAR=45.0
717  
718 # local variables
719 0 my ($i, $pcsi, $pcso, @t);
720  
721 # verify 3 input channels
722 0 ▶ (@in == 3) || croak('PCS object input not 3 channels');
Branch: 1 branch
BranchTrueFalseTotal
@in == 3 or croak("PCS object input not 3 channels") 0 0 0
723  
724 # get input PCS
725 0 $pcsi = $self->[1][$dir][0];
726  
727 # get output PCS
728 0 $pcso = $self->[1][1 - $dir][0];
729  
730 # convert from input PCS
731 0 @t = _rev($pcsi, @in);
732  
733 # if tone compression required
734 0 ▶ if ($self->[2][0]) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->[2][0]) { } 0 0 0
elsif ($pcsi <= 5 and $pcso >= 6) { } 0 0 0
elsif ($pcsi >= 6 and $pcso <= 5) { } 0 0 0
735
736 # convert to xyz if input PCS is L*a*b*
737 0 ▶ @t = ICC::Shared::_Lab2xyz(@t) if ($pcsi <= 5);
Branch: 1 branch
BranchTrueFalseTotal
if $pcsi <= 5 0 0 0
738
739 # tone compression
740 0 @t = _tc($self, $dir, @t);
741
742 # convert to L*a*b* if output PCS is L*a*b*
743 0 ▶ @t = ICC::Shared::_xyz2Lab(@t) if ($pcso <= 5);
Branch: 1 branch
BranchTrueFalseTotal
if $pcso <= 5 0 0 0
744
745 # if input PCS is L*a*b* and output PCS is xyz
746 ▶ } elsif ($pcsi <= 5 && $pcso >= 6) {
Condition0%0/3
$pcsi <= 5 and $pcso >= 6
!ll&&!rl&&r
000
MC/DC0%0/2
$pcsi <= 5 and $pcso >= 6
$pcsi <= 5 $pcso >= 6
Truth table0%0/3
A and B
A= $pcsi <= 5B= $pcso >= 6
ABresult
0X0
100
111
747
748 # convert to xyz
749 0 @t = ICC::Shared::_Lab2xyz(@t);
750
751 # if input PCS is xyz and output PCS is L*a*b*
752 ▶ } elsif ($pcsi >= 6 && $pcso <= 5) {
Condition0%0/3
$pcsi >= 6 and $pcso <= 5
!ll&&!rl&&r
000
MC/DC0%0/2
$pcsi >= 6 and $pcso <= 5
$pcsi >= 6 $pcso <= 5
Truth table0%0/3
A and B
A= $pcsi >= 6B= $pcso <= 5
ABresult
0X0
100
111
753
754 # convert to L*a*b*
755 0 @t = ICC::Shared::_xyz2Lab(@t);
756
757 }
758  
759 # convert to output PCS and return
760 0 return(_fwd($pcso, $self->[3], @t));
761  
762 }
763  
764 # convert to output PCS
765 # input values are either L*a*b* or xyz, depending on PCS
766 # parameters: (PCS, clipping_flag, array_of_input_values)
767 # returns: (array_of_output_values)
768 sub _fwd {
769  
770 # get parameters
771 0 ▶ my ($pcs, $clip, @in) = @_;
Subroutine _fwd: not called CC=26 SCAR=65.5
772  
773 # local variable
774 0 my ($denom);
775  
776 # if 8-bit ICC CIELAB or 16-bit ICC CIELAB
777 0 ▶ if ($pcs == 0) {
Branch: 11 branches
BranchTrueFalseTotal
if ($pcs == 0) { } 0 0 0
elsif ($pcs == 1) { } 0 0 0
elsif ($pcs == 2) { } 0 0 0
elsif ($pcs == 3) { } 0 0 0
elsif ($pcs == 4) { } 0 0 0
elsif ($pcs == 5) { } 0 0 0
elsif ($pcs == 6) { } 0 0 0
elsif ($pcs == 7) { } 0 0 0
elsif ($pcs == 8) { } 0 0 0
elsif ($pcs == 9) { } 0 0 0
elsif ($pcs == 10) { } 0 0 0
778
779 # if clipping flag set
780 0 ▶ if ($clip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clip) { } 0 0 0
781
782 # return clipped ICC CIELAB values
783 0 ▶ return(map {$_ < 0 ? 0 : ($_ > 1 ? 1 : $_)} $in[0]/100, ($in[1] + 128)/255, ($in[2] + 128)/255);
Branch: 2 branches
BranchTrueFalseTotal
$_ < 0 ? : 0 0 0
$_ > 1 ? : 0 0 0
784
785 } else {
786
787 # return ICC CIELAB values
788 0 return($in[0]/100, ($in[1] + 128)/255, ($in[2] + 128)/255);
789
790 }
791
792 # if 16-bit ICC legacy L*a*b*
793 } elsif ($pcs == 1) {
794
795 # if clipping flag set
796 0 ▶ if ($clip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clip) { } 0 0 0
797
798 # clip L* value
799 0 ▶ $in[0] = $in[0] > 100 ? 100 : $in[0];
Branch: 1 branch
BranchTrueFalseTotal
$in[0] > 100 ? : 0 0 0
800
801 # return clipped 16-bit ICC legacy L*a*b* values
802 0 ▶ return(map {$_ < 0 ? 0 : ($_ > 1 ? 1 : $_)} $in[0] * 256/25700, ($in[1] + 128) * 256/65535, ($in[2] + 128) * 256/65535);
Branch: 2 branches
BranchTrueFalseTotal
$_ < 0 ? : 0 0 0
$_ > 1 ? : 0 0 0
803
804 } else {
805
806 # return 16-bit ICC legacy L*a*b* values
807 0 return($in[0] * 256/25700, ($in[1] + 128) * 256/65535, ($in[2] + 128) * 256/65535);
808
809 }
810
811 # if 16-bit ICC EFI/Monaco L*a*b*
812 } elsif ($pcs == 2) {
813
814 # if clipping flag set
815 0 ▶ if ($clip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clip) { } 0 0 0
816
817 # return clipped 16-bit ICC EFI/Monaco L*a*b* values
818 0 ▶ return(map {$_ < 0 ? 0 : ($_ > 1 ? 1 : $_)} $in[0]/100, ($in[1] + 128) * 256/65535, ($in[2] + 128) * 256/65535);
Branch: 2 branches
BranchTrueFalseTotal
$_ < 0 ? : 0 0 0
$_ > 1 ? : 0 0 0
819
820 } else {
821
822 # return 16-bit ICC EFI/Monaco L*a*b* values
823 0 return($in[0]/100, ($in[1] + 128) * 256/65535, ($in[2] + 128) * 256/65535);
824
825 }
826
827 # if L*a*b*
828 } elsif ($pcs == 3) {
829
830 # return L*a*b* values
831 0 return(@in);
832
833 # if LxLyLz
834 } elsif ($pcs == 4) {
835
836 # return LxLyLz values
837 0 return($in[0] + 116 * $in[1]/500, $in[0], $in[0] - 116 * $in[2]/200);
838
839 # if unit LxLyLz
840 } elsif ($pcs == 5) {
841
842 # return unit LxLyLz values
843 0 return(map {$_/100} ($in[0] + 116 * $in[1]/500, $in[0], $in[0] - 116 * $in[2]/200));
844
845 # if xyY
846 } elsif ($pcs == 6) {
847
848 # compute denominator (X + Y + Z)
849 0 $denom = (96.42 * $in[0] + 100 * $in[1] + 82.49 * $in[2]);
850
851 # return xyY values
852 0 ▶ return($denom ? (96.42 * $in[0]/$denom, 100 * $in[1]/$denom, 100 * $in[1]) : (0, 0, 0));
Branch: 1 branch
BranchTrueFalseTotal
$denom ? : 0 0 0
853
854 # if 16-bit ICC XYZ
855 } elsif ($pcs == 7) {
856
857 # if clipping flag set
858 0 ▶ if ($clip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clip) { } 0 0 0
859
860 # return clipped 16-bit ICC XYZ values
861 0 ▶ return(map {$_ < 0 ? 0 : ($_ > 1 ? 1 : $_)} $in[0] * 0.482107356374456, $in[1] * 0.500007629510948, $in[2] * 0.412456293583581);
Branch: 2 branches
BranchTrueFalseTotal
$_ < 0 ? : 0 0 0
$_ > 1 ? : 0 0 0
862
863 } else {
864
865 # return 16-bit ICC XYZ values
866 0 return($in[0] * 0.482107356374456, $in[1] * 0.500007629510948, $in[2] * 0.412456293583581);
867
868 }
869
870 # if 32-bit ICC XYZNumber
871 } elsif ($pcs == 8) {
872
873 # return 32-bit ICC XYZNumber
874 0 return($in[0] * 0.9642, $in[1], $in[2] * 0.8249);
875
876 # if xyz
877 } elsif ($pcs == 9) {
878
879 # return xyz values
880 0 return(@in);
881
882 # if XYZ
883 } elsif ($pcs == 10) {
884
885 # return XYZ values
886 0 return($in[0] * 96.42, $in[1] * 100, $in[2] * 82.49);
887
888 } else {
889
890 # error
891 0 croak('unsupported PCS color space');
892
893 }
894
895 }
896  
897 # convert from input PCS
898 # output values are either L*a*b* or xyz, depending on PCS
899 # parameters: (PCS, array_of_input_values)
900 # returns: (array_of_output_values)
901 sub _rev {
902  
903 # get parameters
904 0 ▶ my ($pcs, @in) = @_;
Subroutine _rev: not called CC=13 SCAR=52.0
905  
906 # local variable
907 0 my ($denom);
908  
909 # if 8-bit ICC CIELAB or 16-bit ICC CIELAB
910 0 ▶ if ($pcs == 0) {
Branch: 11 branches
BranchTrueFalseTotal
if ($pcs == 0) { } 0 0 0
elsif ($pcs == 1) { } 0 0 0
elsif ($pcs == 2) { } 0 0 0
elsif ($pcs == 3) { } 0 0 0
elsif ($pcs == 4) { } 0 0 0
elsif ($pcs == 5) { } 0 0 0
elsif ($pcs == 6) { } 0 0 0
elsif ($pcs == 7) { } 0 0 0
elsif ($pcs == 8) { } 0 0 0
elsif ($pcs == 9) { } 0 0 0
elsif ($pcs == 10) { } 0 0 0
911
912 # return L*a*b*
913 0 return($in[0] * 100, $in[1] * 255 - 128, $in[2] * 255 - 128);
914
915 # if 16-bit ICC legacy L*a*b*
916 } elsif ($pcs == 1) {
917
918 # return L*a*b*
919 0 return($in[0] * 25700/256, $in[1] * 65535/256 - 128, $in[2] * 65535/256 - 128);
920
921 # if 16-bit EFI/Monaco L*a*b*
922 } elsif ($pcs == 2) {
923
924 # return L*a*b*
925 0 return($in[0] * 100, $in[1] * 65535/256 - 128, $in[2] * 65535/256 - 128);
926
927 # if L*a*b*
928 } elsif ($pcs == 3) {
929
930 # return L*a*b*
931 0 return(@in);
932
933 # if LxLyLz
934 } elsif ($pcs == 4) {
935
936 # return L*a*b*
937 0 return($in[1], 500 * ($in[0] - $in[1])/116, 200 * ($in[1] - $in[2])/116);
938
939 # if unit LxLyLz
940 } elsif ($pcs == 5) {
941
942 # return L*a*b*
943 0 return(map {$_ * 100} ($in[1], 500 * ($in[0] - $in[1])/116, 200 * ($in[1] - $in[2])/116));
944
945 # if xyY
946 } elsif ($pcs == 6) {
947
948 # compute denominator (X + Y + Z)
949 0 ▶ $denom = $in[1] ? $in[2]/$in[1] : 0;
Branch: 1 branch
BranchTrueFalseTotal
$in[1] ? : 0 0 0
950
951 # return xyz
952 0 return($in[0] * $denom/96.42, $in[1] * $denom/100, (1 - $in[0] - $in[1]) * $denom/82.49);
953
954 # if 16-bit ICC XYZ
955 } elsif ($pcs == 7) {
956
957 # return xyz
958 0 return($in[0]/0.482107356374456, $in[1]/0.500007629510948, $in[2]/0.412456293583581);
959
960 # if ICC XYZNumber
961 } elsif ($pcs == 8) {
962
963 # return xyz
964 0 return($in[0]/0.9642, $in[1], $in[2]/0.8249);
965
966 # if xyz
967 } elsif ($pcs == 9) {
968
969 # return xyz
970 0 return(@in);
971
972 # if XYZ
973 } elsif ($pcs == 10) {
974
975 # return xyz
976 0 return($in[0]/96.42, $in[1]/100, $in[2]/82.49);
977
978 } else {
979
980 # error
981 0 croak('unsupported PCS color space');
982
983 }
984
985 }
986  
987 # compute Jacobian matrix for forward transform
988 # input values are either L*a*b* or xyz, depending on PCS
989 # parameters: (PCS, array_of_input_values)
990 # returns: (Jacobian_matrix)
991 sub _fwd_jac {
992  
993 # get parameters
994 0 ▶ my ($pcs, @in) = @_;
Subroutine _fwd_jac: not called CC=13 SCAR=52.0
995  
996 # local variables
997 0 my ($denom, @out);
998  
999 # if 8-bit ICC CIELAB or 16-bit ICC CIELAB
1000 0 ▶ if ($pcs == 0) {
Branch: 11 branches
BranchTrueFalseTotal
if ($pcs == 0) { } 0 0 0
elsif ($pcs == 1) { } 0 0 0
elsif ($pcs == 2) { } 0 0 0
elsif ($pcs == 3) { } 0 0 0
elsif ($pcs == 4) { } 0 0 0
elsif ($pcs == 5) { } 0 0 0
elsif ($pcs == 6) { } 0 0 0
elsif ($pcs == 7) { } 0 0 0
elsif ($pcs == 8) { } 0 0 0
elsif ($pcs == 9) { } 0 0 0
elsif ($pcs == 10) { } 0 0 0
1001
1002 # return Jacobian matrix
1003 0 return(Math::Matrix->new(
1004 [1/100, 0, 0],
1005 [0, 1/255, 0],
1006 [0, 0, 1/255]
1007 ));
1008
1009 # if 16-bit ICC legacy L*a*b*
1010 } elsif ($pcs == 1) {
1011
1012 # return Jacobian matrix
1013 0 return(Math::Matrix->new(
1014 [256/25700, 0, 0],
1015 [0, 256/65535, 0],
1016 [0, 0, 256/65535]
1017 ));
1018
1019 # if 16-bit ICC EFI/Monaco L*a*b*
1020 } elsif ($pcs == 2) {
1021
1022 # return Jacobian matrix
1023 0 return(Math::Matrix->new(
1024 [1/100, 0, 0],
1025 [0, 256/65535, 0],
1026 [0, 0, 256/65535]
1027 ));
1028
1029 # if L*a*b*
1030 } elsif ($pcs == 3) {
1031
1032 # return Jacobian matrix
1033 0 return(Math::Matrix->new(
1034 [1, 0, 0],
1035 [0, 1, 0],
1036 [0, 0, 1]
1037 ));
1038
1039 # if LxLyLz
1040 } elsif ($pcs == 4) {
1041
1042 # return Jacobian matrix
1043 0 return(Math::Matrix->new(
1044 [1, 116/500, 0],
1045 [1, 0, 0],
1046 [1, 0, -116/200]
1047 ));
1048
1049 # if unit LxLyLz
1050 } elsif ($pcs == 5) {
1051
1052 # return Jacobian matrix
1053 0 return(Math::Matrix->new(
1054 [1/100, 116/50000, 0],
1055 [1/100, 0, 0],
1056 [1/100, 0, -116/20000]
1057 ));
1058
1059 # if xyY
1060 } elsif ($pcs == 6) {
1061
1062 # if denominator (X + Y + Z) is non-zero
1063 0 ▶ if ($denom = (96.42 * $in[0] + 100 * $in[1] + 82.49 * $in[2])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($denom = 96.42 * $in[0] + 100 * $in[1] + 82.49 * $in[2]) { } 0 0 0
1064
1065 # compute output vector
1066 0 @out = (96.42 * $in[0]/$denom, 100 * $in[1]/$denom, 100 * $in[1]);
1067
1068 # return Jacobian matrix
1069 0 return(Math::Matrix->new(
1070 [96.42 * (1 - $out[0])/$denom, -100 * $out[0]/$denom, -82.49 * $out[0]/$denom],
1071 [-96.42 * $out[1]/$denom, 100 * (1 - $out[1])/$denom, -82.49 * $out[1]/$denom],
1072 [0, 100, 0]
1073 ));
1074
1075 } else {
1076
1077 # print warning
1078 0 print "Jacobian matrix overflow!\n";
1079
1080 # return Jacobian matrix
1081 0 return(Math::Matrix->new(
1082 ['inf', '-inf', '-inf'],
1083 ['-inf', 'inf', '-inf'],
1084 [0, 100, 0]
1085 ));
1086
1087 }
1088
1089 # if 16-bit ICC XYZ
1090 } elsif ($pcs == 7) {
1091
1092 # return Jacobian matrix
1093 0 return(Math::Matrix->new(
1094 [0.482107356374456, 0, 0],
1095 [0, 0.500007629510948, 0],
1096 [0, 0, 0.412456293583581]
1097 ));
1098
1099 # if 32-bit ICC XYZNumber
1100 } elsif ($pcs == 8) {
1101
1102 # return Jacobian matrix
1103 0 return(Math::Matrix->new(
1104 [0.9642, 0, 0],
1105 [0, 1, 0],
1106 [0, 0, 0.8249]
1107 ));
1108
1109 # if xyz
1110 } elsif ($pcs == 9) {
1111
1112 # return Jacobian matrix
1113 0 return(Math::Matrix->new(
1114 [1, 0, 0],
1115 [0, 1, 0],
1116 [0, 0, 1]
1117 ));
1118
1119 # if XYZ
1120 } elsif ($pcs == 10) {
1121
1122 # return Jacobian matrix
1123 0 return(Math::Matrix->new(
1124 [96.42, 0, 0],
1125 [0, 100, 0],
1126 [0, 0, 82.49]
1127 ));
1128
1129 } else {
1130
1131 # error
1132 0 croak('unsupported PCS color space');
1133
1134 }
1135
1136 }
1137  
1138 # compute Jacobian matrix for reverse transform
1139 # output values are either L*a*b* or xyz, depending on PCS
1140 # parameters: (PCS, array_of_input_values)
1141 # returns: (Jacobian_matrix)
1142 sub _rev_jac {
1143  
1144 # get parameters
1145 0 ▶ my ($pcs, @in) = @_;
Subroutine _rev_jac: not called CC=13 SCAR=52.0
1146  
1147 # local variables
1148 0 my ($denom, $xr, $zr);
1149  
1150 # if 8-bit ICC CIELAB or 16-bit ICC CIELAB
1151 0 ▶ if ($pcs == 0) {
Branch: 11 branches
BranchTrueFalseTotal
if ($pcs == 0) { } 0 0 0
elsif ($pcs == 1) { } 0 0 0
elsif ($pcs == 2) { } 0 0 0
elsif ($pcs == 3) { } 0 0 0
elsif ($pcs == 4) { } 0 0 0
elsif ($pcs == 5) { } 0 0 0
elsif ($pcs == 6) { } 0 0 0
elsif ($pcs == 7) { } 0 0 0
elsif ($pcs == 8) { } 0 0 0
elsif ($pcs == 9) { } 0 0 0
elsif ($pcs == 10) { } 0 0 0
1152
1153 # return Jacobian matrix
1154 0 return(Math::Matrix->new(
1155 [100, 0, 0],
1156 [0, 255, 0],
1157 [0, 0, 255]
1158 ));
1159
1160 # if 16-bit ICC legacy L*a*b*
1161 } elsif ($pcs == 1) {
1162
1163 # return Jacobian matrix
1164 0 return(Math::Matrix->new(
1165 [25700/256, 0, 0],
1166 [0, 65535/256, 0],
1167 [0, 0, 65535/256]
1168 ));
1169
1170 # if 16-bit EFI/Monaco L*a*b*
1171 } elsif ($pcs == 2) {
1172
1173 # return Jacobian matrix
1174 0 return(Math::Matrix->new(
1175 [100, 0, 0],
1176 [0, 65535/256, 0],
1177 [0, 0, 65535/256]
1178 ));
1179
1180 # if L*a*b*
1181 } elsif ($pcs == 3) {
1182
1183 # return Jacobian matrix
1184 0 return(Math::Matrix->new(
1185 [1, 0, 0],
1186 [0, 1, 0],
1187 [0, 0, 1]
1188 ));
1189
1190 # if LxLyLz
1191 } elsif ($pcs == 4) {
1192
1193 # return Jacobian matrix
1194 0 return(Math::Matrix->new(
1195 [0, 1, 0],
1196 [500/116, -500/116, 0],
1197 [0, 200/116, -200/116]
1198 ));
1199
1200 # if unit LxLyLz
1201 } elsif ($pcs == 5) {
1202
1203 # return Jacobian matrix
1204 0 return(Math::Matrix->new(
1205 [0, 100, 0],
1206 [50000/116, -50000/116, 0],
1207 [0, 20000/116, -20000/116]
1208 ));
1209
1210 # if xyY
1211 } elsif ($pcs == 6) {
1212
1213 # if y not zero
1214 0 ▶ if ($in[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in[1]) { } 0 0 0
1215
1216 # compute denominator (X + Y + Z)
1217 0 $denom = $in[2]/$in[1];
1218
1219 # compute ratios
1220 0 $xr = $in[0]/$in[1];
1221 0 $zr = (1 - $in[0])/$in[1];
1222
1223 # return Jacobian matrix
1224 0 return(Math::Matrix->new(
1225 [$denom/96.42, -$denom * $xr/96.42, $xr/96.42],
1226 [0, 0, 1/100],
1227 [-$denom/82.49, -$denom * $zr/82.49, ($zr - 1)/82.49]
1228 ));
1229
1230 } else {
1231
1232 # print warning
1233 0 print "Jacobian matrix overflow!\n";
1234
1235 # return Jacobian matrix
1236 0 return(Math::Matrix->new(
1237 ['inf', '-inf', 'inf'],
1238 [0, 0, 1/100],
1239 ['-inf', '-inf', 'inf']
1240 ));
1241
1242 }
1243
1244 # if 16-bit ICC XYZ
1245 } elsif ($pcs == 7) {
1246
1247 # return Jacobian matrix
1248 0 return(Math::Matrix->new(
1249 [2.074226801931005, 0, 0],
1250 [0, 1.999969482421875, 0],
1251 [0, 0, 2.424499311943114]
1252 ));
1253
1254 # if ICC XYZNumber
1255 } elsif ($pcs == 8) {
1256
1257 # return Jacobian matrix
1258 0 return(Math::Matrix->new(
1259 [1/0.9642, 0, 0],
1260 [0, 1, 0],
1261 [0, 0, 1/0.8249]
1262 ));
1263
1264 # if xyz
1265 } elsif ($pcs == 9) {
1266
1267 # return Jacobian matrix
1268 0 return(Math::Matrix->new(
1269 [1, 0, 0],
1270 [0, 1, 0],
1271 [0, 0, 1]
1272 ));
1273
1274 # if XYZ
1275 } elsif ($pcs == 10) {
1276
1277 # return Jacobian matrix
1278 0 return(Math::Matrix->new(
1279 [1/96.42, 0, 0],
1280 [0, 1/100, 0],
1281 [0, 0, 1/82.49]
1282 ));
1283
1284 } else {
1285
1286 # error
1287 0 croak('unsupported PCS color space');
1288
1289 }
1290
1291 }
1292  
1293 # forward tone compression derivative
1294 # input and output values are xyz
1295 # parameters: (object_reference, direction, array_of_input_values)
1296 # returns: (array_of_output values)
1297 sub _tc_derv {
1298
1299 # get parameters
1300 0 ▶ my ($self, $dir, @in) = @_;
Subroutine _tc_derv: not called CC=11 SCAR=48.8
1301
1302 # local variables
1303 0 my ($lin, @out, $t, $u);
1304
1305 # if non-linear tone compression
1306 0 ▶ if ($lin = $self->[2][1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lin = $self->[2][1]) { } 0 0 0
1307
1308 # if reverse direction
1309 0 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) { } 0 0 0
1310
1311 # for each xyz
1312 0 for my $i (0 .. 2) {
1313
1314 # compute t = (exp(r * y/y1) - b)/a
1315 0 $t = (exp($lin * $in[$i]/$self->[1][1][1][$i]) - $self->[2][3][$i])/$self->[2][2][$i];
1316
1317 # compute u = x1/y1
1318 0 $u = $self->[1][0][1][$i]/$self->[1][1][1][$i];
1319
1320 # compute x = u * a * exp(r * y/y1)/exp(r * x/x1)
1321 0 ▶ $out[$i] = $t == 0 ? 1E99 : $u * $self->[2][2][$i] * exp($lin * $in[$i]/$self->[1][1][1][$i])/$t;
Branch: 1 branch
BranchTrueFalseTotal
$t == 0 ? : 0 0 0
1322
1323 }
1324
1325 } else {
1326
1327 # for each xyz
1328 0 for my $i (0 .. 2) {
1329
1330 # compute t = exp(r * x/x1) * a + b
1331 0 $t = exp($lin * $in[$i]/$self->[1][0][1][$i]) * $self->[2][2][$i] + $self->[2][3][$i];
1332
1333 # compute u = y1/x1
1334 0 $u = $self->[1][1][1][$i]/$self->[1][0][1][$i];
1335
1336 # compute x = u * a * (exp(r * x/x1)/exp(r * y/y1))
1337 0 ▶ $out[$i] = $t == 0 ? 1E99 : $u * $self->[2][2][$i] * exp($lin * $in[$i]/$self->[1][0][1][$i])/$t;
Branch: 1 branch
BranchTrueFalseTotal
$t == 0 ? : 0 0 0
1338
1339 }
1340
1341 }
1342
1343 # if linear tone compression
1344 } else {
1345
1346 # if reverse direction
1347 0 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) { } 0 0 0
1348
1349 # for each xyz
1350 0 for my $i (0 .. 2) {
1351
1352 # compute y = 1/a
1353 0 ▶ $out[$i] = $self->[2][2][$i] == 0 ? 1E99 : 1/$self->[2][2][$i];
Branch: 1 branch
BranchTrueFalseTotal
$self->[2][2][$i] == 0 ? : 0 0 0
1354
1355 }
1356
1357 } else {
1358
1359 # for each xyz
1360 0 for my $i (0 .. 2) {
1361
1362 # compute y = a
1363 0 $out[$i] = $self->[2][2][$i];
1364
1365 }
1366
1367 }
1368
1369 }
1370  
1371 # return
1372 0 return(@out);
1373
1374 }
1375  
1376 # tone compression transform
1377 # input and output values are xyz
1378 # parameters: (object_reference, direction, array_of_input_values)
1379 # returns: (array_of_output values)
1380 sub _tc {
1381
1382 # get parameters
1383 0 ▶ my ($self, $dir, @in) = @_;
Subroutine _tc: not called CC=11 SCAR=48.8
1384
1385 # local variables
1386 0 my ($lin, @out, $t);
1387
1388 # if non-linear tone compression
1389 0 ▶ if ($lin = $self->[2][1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lin = $self->[2][1]) { } 0 0 0
1390
1391 # if reverse direction
1392 0 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) { } 0 0 0
1393
1394 # for each xyz
1395 0 for my $i (0 .. 2) {
1396
1397 # compute t = (exp(r * y/y1) - b)/a
1398 0 $t = (exp($lin * $in[$i]/$self->[1][1][1][$i]) - $self->[2][3][$i])/$self->[2][2][$i];
1399
1400 # compute x = ln(t) * x1/r
1401 0 ▶ $out[$i] = $t > 0 ? log($t) * $self->[1][0][1][$i]/$lin : -1E99;
Branch: 1 branch
BranchTrueFalseTotal
$t > 0 ? : 0 0 0
1402
1403 }
1404
1405 } else {
1406
1407 # for each xyz
1408 0 for my $i (0 .. 2) {
1409
1410 # compute t = exp(r * x/x1) * a + b
1411 0 $t = exp($lin * $in[$i]/$self->[1][0][1][$i]) * $self->[2][2][$i] + $self->[2][3][$i];
1412
1413 # compute y = ln(t) * y1/r
1414 0 ▶ $out[$i] = $t > 0 ? log($t) * $self->[1][1][1][$i]/$lin : -1E99;
Branch: 1 branch
BranchTrueFalseTotal
$t > 0 ? : 0 0 0
1415
1416 }
1417
1418 }
1419
1420 # else linear tone compression
1421 } else {
1422
1423 # if reverse direction
1424 0 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) { } 0 0 0
1425
1426 # for each xyz
1427 0 for my $i (0 .. 2) {
1428
1429 # compute y = (x - b)/a
1430 0 ▶ $out[$i] = $self->[2][2][$i] == 0 ? 1E99 : ($in[$i] - $self->[2][3][$i])/$self->[2][2][$i];
Branch: 1 branch
BranchTrueFalseTotal
$self->[2][2][$i] == 0 ? : 0 0 0
1431
1432 }
1433
1434 } else {
1435
1436 # for each xyz
1437 0 for my $i (0 .. 2) {
1438
1439 # compute y = ax + b
1440 0 $out[$i] = $in[$i] * $self->[2][2][$i] + $self->[2][3][$i];
1441
1442 }
1443
1444 }
1445
1446 }
1447
1448 # return
1449 0 return(@out);
1450
1451 }
1452  
1453 # make PCS connection object from parameters
1454 # structure of the input/output parameter arrays is: (pcs_connection_space, [white_point, [black_point]])
1455 # parameters: (object_reference, ref_to_input_parameter_array, ref_to_output_parameter_array, [tone_compression_linearity])
1456 sub _new_pcs {
1457  
1458 # get object reference
1459 0 ▶ my ($self) = shift();
Subroutine _new_pcs: not called CC=13 SCAR=52.0
1460  
1461 # local variables
1462 0 my (@cs, @io);
1463  
1464 # list of supported connection spaces
1465 0 @cs = (0 .. 10);
1466  
1467 # message labels
1468 0 @io = qw(input output);
1469  
1470 # for input and output parameters
1471 0 for my $i (0 .. 1) {
1472
1473 # verify parameter is an array reference
1474 0 ▶ (ref($_[$i]) eq 'ARRAY') || croak("$io[$i] parameter not an array reference");
Branch: 1 branch
BranchTrueFalseTotal
ref $_[$i] eq "ARRAY" or croak("$io[$i] parameter not an array reference") 0 0 0
1475
1476 # verify number of array parameters
1477 0 ▶ (@{$_[$i]} >= 1 || @{$_[$i]} <= 3) || croak("$io[$i] array has wrong number parameters");
Branch: 1 branch
BranchTrueFalseTotal
@{$_[$i];} >= 1 or @{$_[$i];} <= 3 or croak("$io[$i] array has wrong number parameters") 0 0 0
Condition0%0/3
@{$_[$i];} >= 1 or @{$_[$i];} <= 3
l!l&&r!l&&!r
000
MC/DC0%0/2
@{$_[$i];} >= 1 or @{$_[$i];} <= 3
@{$_[$i];} >= 1 @{$_[$i];} <= 3
Truth table0%0/3
A or B
A= @{$_[$i];} >= 1B= @{$_[$i];} <= 3
ABresult
1X1
011
000
1478
1479 # verify color space
1480 0 ▶ (grep {$_[$i][0] == $_} @cs) || croak("$io[$i] color space not supported");
Branch: 1 branch
BranchTrueFalseTotal
grep {$_[$i][0] == $_;} @cs or croak("$io[$i] color space not supported") 0 0 0
1481
1482 # copy color space
1483 0 $self->[1][$i][0] = $_[$i][0];
1484
1485 # if white point is defined
1486 0 ▶ if (defined($_[$i][1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[$i][1]) { } 0 0 0
1487
1488 # verify white point is an array reference
1489 0 ▶ (ref($_[$i][1]) eq 'ARRAY') || croak("$io[$i] white point not an array reference");
Branch: 1 branch
BranchTrueFalseTotal
ref $_[$i][1] eq "ARRAY" or croak("$io[$i] white point not an array reference") 0 0 0
1490
1491 # verify array structure
1492 0 ▶ (3 == grep {! ref()} @{$_[$i][1]}) || croak("$io[$i] white point array has wrong structure");
Branch: 1 branch
BranchTrueFalseTotal
3 == grep({not ref $_;} @{$_[$i][1];}) or croak("$io[$i] white point array has wrong structure") 0 0 0
1493
1494 # copy white point (converting XYZNumber to xyz)
1495 0 $self->[1][$i][1] = [$_[$i][1][0]/ICC::Shared::d50->[0], $_[$i][1][1]/ICC::Shared::d50->[1], $_[$i][1][2]/ICC::Shared::d50->[2]];
1496
1497 } else {
1498
1499 # set white point to perfect white
1500 0 $self->[1][$i][1] = [1, 1, 1];
1501
1502 }
1503
1504 # if black point is defined
1505 0 ▶ if (defined($_[$i][2])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[$i][2]) { } 0 0 0
1506
1507 # verify black point is an array reference
1508 0 ▶ (ref($_[$i][2]) eq 'ARRAY') || croak("$io[$i] black point not an array reference");
Branch: 1 branch
BranchTrueFalseTotal
ref $_[$i][2] eq "ARRAY" or croak("$io[$i] black point not an array reference") 0 0 0
1509
1510 # verify array structure
1511 0 ▶ (3 == grep {! ref()} @{$_[$i][2]}) || croak("$io[$i] black point array has wrong structure");
Branch: 1 branch
BranchTrueFalseTotal
3 == grep({not ref $_;} @{$_[$i][2];}) or croak("$io[$i] black point array has wrong structure") 0 0 0
1512
1513 # copy black point (converting XYZNumber to xyz)
1514 0 $self->[1][$i][2] = [$_[$i][2][0]/ICC::Shared::d50->[0], $_[$i][2][1]/ICC::Shared::d50->[1], $_[$i][2][2]/ICC::Shared::d50->[2]];
1515
1516 } else {
1517
1518 # set black point to perfect black
1519 0 $self->[1][$i][2] = [0, 0, 0];
1520
1521 }
1522
1523 }
1524  
1525 # set tone compression linearity (default = 0)
1526 0 ▶ $self->[2][1] = defined($_[2]) ? $_[2] : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 0 0 0
1527  
1528 # compute tone compression coefficients
1529 0 tc_pars($self);
1530  
1531 }
1532  
1533 2 1;
1534  
« lib/ICC/Support/nNET.pm ↑ Summary blib/lib/ICC/Support/PCS.pm »