blib/lib/ICC/Profile/gbd_.pm

filter: Statementstmt 6.7% 20 / 295 Branchbran 0.8% 1 / 120 Conditioncond 0.0% 0 / 81 MC/DCmcdc 0.0% 0 / 48 Subroutinesub 25.0% 5 / 20 Podpod 9.0% 1 / 11 totaltotal 4.7% 27 / 575 CC 105 SCAR 53.6
CC
105
Cov
4%
_new_from_hash
702.0
test
210.0
intersect
156.0
CRAP 213.4
« blib/lib/ICC/Shared.pm ↑ Summary lib/ICC/Support/rbf.pm »
1 package ICC::Profile::gbd_;
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.12;
7  
8 # revised 2016-05-17
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 # create new gbd_ object
19 # hash keys are: ('vertex', 'pcs', 'device')
20 # 'vertex', 'pcs' and 'device' values are 2D array references -or- Math::Matrix objects
21 # each 'vertex' row contains an array of 3 indices defining a gamut face
22 # these indices address the 'pcs' and optional 'device' coordinate arrays
23 # parameters: ([ref_to_attribute_hash])
24 # returns: (ref_to_object)
25 sub new {
26  
27 # get object class
28 1 ▶ my $class = shift();
Subroutine new: called CC=4 SCAR=19.2
Pod undocumented
29  
30 # create empty gbd_ object
31 # index 4 reserved for cache
32 # index 5 reserved for index
33 1 my $self = [
34 {}, # header
35 [], # face vertex IDs
36 [], # pcs coordinates
37 [] # device coordinates
38 ];
39
40 # local parameter
41 1 my ($info);
42  
43 # if there are parameters
44 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1 1
45
46 # if one parameter, a hash reference
47 0 ▶ if (@_ == 1 && ref($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 0 0 0
Condition0%0/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
48
49 # make new gbd_ object from attribute hash
50 0 _new_from_hash($self, shift());
51
52 } else {
53
54 # error
55 0 croak('\'gbd_\' invalid parameter(s)');
56
57 }
58
59 }
60  
61 # bless object
62 1 bless($self, $class);
63  
64 # return object reference
65 1 return($self);
66  
67 }
68  
69 # get/set reference to header hash
70 # parameters: ([ref_to_new_hash])
71 # returns: (ref_to_hash)
72 sub header {
73
74 # get object reference
75 0 ▶ my $self = shift();
Subroutine header: not called CC=4 SCAR=30.0
Pod undocumented
76
77 # if there are parameters
78 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
79
80 # if one parameter, a hash reference
81 0 ▶ if (@_ == 1 && ref($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 0 0 0
Condition0%0/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
82
83 # set header to new hash
84 0 $self->[0] = {%{shift()}};
85
86 } else {
87
88 # error
89 0 croak('\'gbd_\' header attribute must be a hash reference');
90
91 }
92
93 }
94
95 # return header reference
96 0 return($self->[0]);
97
98 }
99  
100 # get/set reference to vertex array
101 # parameters: ([ref_to_new_array])
102 # returns: (ref_to_array)
103 sub vertex {
104  
105 # get object reference
106 0 ▶ my $self = shift();
Subroutine vertex: not called CC=7 SCAR=40.3
Pod undocumented
107  
108 # if there are parameters
109 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
110
111 # if one parameter, a 2-D array reference
112 0 ▶ if (@_ == 1 && ref($_[0]) eq 'ARRAY' && @{$_[0]} == grep {ref() eq 'ARRAY'} @{$_[0]}) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'ARRAY' and @{$_[0];} == grep({ref $_ eq 'ARRAY';} @{$_[0];})) { } 0 0 0
elsif (@_ == 1 and UNIVERSAL::isa($_[0], 'Math::Matrix')) { } 0 0 0
Condition0%0/6
@_ == 1 and ref $_[0] eq "ARRAY" and @{$_[0];} == grep({ref $_ eq "ARRAY";} @{$_[0];})
!ll&&!rl&&r
000
@_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
@_ == 1 and ref $_[0] eq "ARRAY" and @{$_[0];} == grep({ref $_ eq "ARRAY";} @{$_[0];})
@_ == 1 ref $_[0] eq "ARRAY" @{$_[0];} == grep({ref $_ eq "ARRAY";} @{$_[0];})
Truth table0%0/4
A and B and C
A= @_ == 1B= ref $_[0] eq "ARRAY"C= @{$_[0];} == grep({ref $_ eq "ARRAY";} @{$_[0];})
ABCresult
0XX0
10X0
1100
1111
113
114 # set vertex to clone of array
115 0 $self->[1] = bless(Storable::dclone($_[0]), 'Math::Matrix');
116
117 # if one parameter, a Math::Matrix object
118 ▶ } elsif (@_ == 1 && UNIVERSAL::isa($_[0], 'Math::Matrix')) {
Condition0%0/3
@_ == 1 and UNIVERSAL::isa($_[0], "Math::Matrix")
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and UNIVERSAL::isa($_[0], "Math::Matrix")
@_ == 1 UNIVERSAL::isa($_[0], "Math::Matrix")
Truth table0%0/3
A and B
A= @_ == 1B= UNIVERSAL::isa($_[0], "Math::Matrix")
ABresult
0X0
100
111
119
120 # set vertex to object
121 0 $self->[1] = $_[0];
122
123 } else {
124
125 # error
126 0 croak('gbd_ vertex must be a 2-D array reference or Math::Matrix object');
127
128 }
129
130 }
131  
132 # return object reference
133 0 return($self->[1]);
134  
135 }
136  
137 # get/set reference to pcs array
138 # parameters: ([ref_to_new_array])
139 # returns: (ref_to_array)
140 sub pcs {
141  
142 # get object reference
143 0 ▶ my $self = shift();
Subroutine pcs: not called CC=7 SCAR=40.3
Pod undocumented
144  
145 # if there are parameters
146 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
147
148 # if one parameter, a 2-D array reference
149 0 ▶ if (@_ == 1 && ref($_[0]) eq 'ARRAY' && @{$_[0]} == grep {ref() eq 'ARRAY'} @{$_[0]}) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'ARRAY' and @{$_[0];} == grep({ref $_ eq 'ARRAY';} @{$_[0];})) { } 0 0 0
elsif (@_ == 1 and UNIVERSAL::isa($_[0], 'Math::Matrix')) { } 0 0 0
Condition0%0/6
@_ == 1 and ref $_[0] eq "ARRAY" and @{$_[0];} == grep({ref $_ eq "ARRAY";} @{$_[0];})
!ll&&!rl&&r
000
@_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
@_ == 1 and ref $_[0] eq "ARRAY" and @{$_[0];} == grep({ref $_ eq "ARRAY";} @{$_[0];})
@_ == 1 ref $_[0] eq "ARRAY" @{$_[0];} == grep({ref $_ eq "ARRAY";} @{$_[0];})
Truth table0%0/4
A and B and C
A= @_ == 1B= ref $_[0] eq "ARRAY"C= @{$_[0];} == grep({ref $_ eq "ARRAY";} @{$_[0];})
ABCresult
0XX0
10X0
1100
1111
150
151 # set pcs to clone of array
152 0 $self->[2] = bless(Storable::dclone($_[0]), 'Math::Matrix');
153
154 # if one parameter, a Math::Matrix object
155 ▶ } elsif (@_ == 1 && UNIVERSAL::isa($_[0], 'Math::Matrix')) {
Condition0%0/3
@_ == 1 and UNIVERSAL::isa($_[0], "Math::Matrix")
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and UNIVERSAL::isa($_[0], "Math::Matrix")
@_ == 1 UNIVERSAL::isa($_[0], "Math::Matrix")
Truth table0%0/3
A and B
A= @_ == 1B= UNIVERSAL::isa($_[0], "Math::Matrix")
ABresult
0X0
100
111
156
157 # set pcs to object
158 0 $self->[2] = $_[0];
159
160 } else {
161
162 # error
163 0 croak('gbd_ pcs must be a 2-D array reference or Math::Matrix object');
164
165 }
166
167 }
168  
169 # return object reference
170 0 return($self->[2]);
171  
172 }
173  
174 # get/set reference to device array
175 # parameters: ([ref_to_new_array])
176 # returns: (ref_to_array)
177 sub device {
178  
179 # get object reference
180 0 ▶ my $self = shift();
Subroutine device: not called CC=7 SCAR=40.3
Pod undocumented
181  
182 # if there are parameters
183 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
184
185 # if one parameter, a 2-D array reference
186 0 ▶ if (@_ == 1 && ref($_[0]) eq 'ARRAY' && @{$_[0]} == grep {ref() eq 'ARRAY'} @{$_[0]}) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'ARRAY' and @{$_[0];} == grep({ref $_ eq 'ARRAY';} @{$_[0];})) { } 0 0 0
elsif (@_ == 1 and UNIVERSAL::isa($_[0], 'Math::Matrix')) { } 0 0 0
Condition0%0/6
@_ == 1 and ref $_[0] eq "ARRAY" and @{$_[0];} == grep({ref $_ eq "ARRAY";} @{$_[0];})
!ll&&!rl&&r
000
@_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
@_ == 1 and ref $_[0] eq "ARRAY" and @{$_[0];} == grep({ref $_ eq "ARRAY";} @{$_[0];})
@_ == 1 ref $_[0] eq "ARRAY" @{$_[0];} == grep({ref $_ eq "ARRAY";} @{$_[0];})
Truth table0%0/4
A and B and C
A= @_ == 1B= ref $_[0] eq "ARRAY"C= @{$_[0];} == grep({ref $_ eq "ARRAY";} @{$_[0];})
ABCresult
0XX0
10X0
1100
1111
187
188 # set device to clone of array
189 0 $self->[3] = bless(Storable::dclone($_[0]), 'Math::Matrix');
190
191 # if one parameter, a Math::Matrix object
192 ▶ } elsif (@_ == 1 && UNIVERSAL::isa($_[0], 'Math::Matrix')) {
Condition0%0/3
@_ == 1 and UNIVERSAL::isa($_[0], "Math::Matrix")
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and UNIVERSAL::isa($_[0], "Math::Matrix")
@_ == 1 UNIVERSAL::isa($_[0], "Math::Matrix")
Truth table0%0/3
A and B
A= @_ == 1B= UNIVERSAL::isa($_[0], "Math::Matrix")
ABresult
0X0
100
111
193
194 # set device to object
195 0 $self->[3] = $_[0];
196
197 } else {
198
199 # error
200 0 croak('gbd_ device must be a 2-D array reference or Math::Matrix object');
201
202 }
203
204 }
205  
206 # return object reference
207 0 return($self->[3]);
208  
209 }
210  
211 # test an array of samples against gamut
212 # the point inside the gamut my be supplied,
213 # otherwise it is computed from the gamut data
214 # result is an array, [[radius, intersect_point, face_ID], [...]]
215 # if radius == 1, sample is on the gamut surface
216 # if radius > 1, sample is inside the gamut
217 # if radius < 1, sample is out-of-gamut
218 # parameters: (sample_array, [point_inside_gamut])
219 # returns: (result_array)
220 sub test {
221  
222 # get parameters
223 0 ▶ my ($self, $samples, $p0) = @_;
Subroutine test: not called CC=14 SCAR=53.5
Pod undocumented
224  
225 # local variables
226 0 my ($m, $n, $ps, $i, $j, $faces, $info, $r, $px, $result);
227  
228 # if parameter is undefined
229 0 ▶ if (! defined($p0)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $p0) { } 0 0 0
elsif (defined $self->[0]{'p0'} and $self->[0]{'p0'}[0] != $p0->[0] || $self->[0]{'p0'}[1] != $p0->[1] || $self->[0]{'p0'}[2] != $p0->[2]) { } 0 0 0
230
231 # if defined in header
232 0 ▶ if (defined($self->[0]{'p0'}) && defined($self->[5])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->[0]{'p0'} and defined $self->[5]) { } 0 0 0
Condition0%0/3
defined $self->[0]{"p0"} and defined $self->[5]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->[0]{"p0"} and defined $self->[5]
defined $self->[0]{"p0"} defined $self->[5]
Truth table0%0/3
A and B
A= defined $self->[0]{"p0"}B= defined $self->[5]
ABresult
0X0
100
111
233
234 # use header value
235 0 $p0 = $self->[0]{'p0'};
236
237 } else {
238
239 # use mean value of vertices
240 0 $p0 = ICC::Support::Lapack::mean($self->[2]);
241
242 }
243
244 # if parameter is defined, but different from header value
245 ▶ } elsif (defined($self->[0]{'p0'}) && ($self->[0]{'p0'}[0] != $p0->[0] || $self->[0]{'p0'}[1] != $p0->[1] || $self->[0]{'p0'}[2] != $p0->[2])) {
Condition0%0/9
defined $self->[0]{"p0"} and $self->[0]{"p0"}[0] != $p0->[0] || $self->[0]{"p0"}[1] != $p0->[1] || $self->[0]{"p0"}[2] != $p0->[2]
!ll&&!rl&&r
000
$self->[0]{"p0"}[0] != $p0->[0] or $self->[0]{"p0"}[1] != $p0->[1] or $self->[0]{"p0"}[2] != $p0->[2]
l!l&&r!l&&!r
000
$self->[0]{"p0"}[0] != $p0->[0] or $self->[0]{"p0"}[1] != $p0->[1]
l!l&&r!l&&!r
000
MC/DC0%0/4
defined $self->[0]{"p0"} and $self->[0]{"p0"}[0] != $p0->[0] || $self->[0]{"p0"}[1] != $p0->[1] || $self->[0]{"p0"}[2] != $p0->[2]
defined $self->[0]{"p0"} $self->[0]{"p0"}[0] != $p0->[0] $self->[0]{"p0"}[1] != $p0->[1] $self->[0]{"p0"}[2] != $p0->[2]
Truth table0%0/5
A and B or C or D
A= defined $self->[0]{"p0"}B= $self->[0]{"p0"}[0] != $p0->[0]C= $self->[0]{"p0"}[1] != $p0->[1]D= $self->[0]{"p0"}[2] != $p0->[2]
ABCDresult
0XXX0
10000
11XX1
101X1
10011
246
247 # undefine spherical index to force re-calculation
248 0 undef($self->[5]);
249
250 }
251  
252 # if spherical index defined
253 0 ▶ if (defined($self->[5])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->[5]) { } 0 0 0
254
255 # get index array size
256 0 $m = @{$self->[5]};
257 0 $n = @{$self->[5][0]};
258
259 } else {
260
261 # compute index grid size
262 0 $m = $n = int(@{$self->[1]}**(1/3));
263
264 # make spherical index
265 0 _make_index($self, $p0, $m, $n) ;
266
267 }
268  
269 # for each sample
270 0 for my $s (0 .. $#{$samples}) {
271
272 # get sample
273 0 $ps = $samples->[$s];
274
275 # compute spherical indices
276 0 $i = int($m * atan2(sqrt(($ps->[1] - $p0->[1])**2 + ($ps->[2] - $p0->[2])**2), $ps->[0] - $p0->[0])/ICC::Shared::PI);
277 0 $j = int($n * (atan2($ps->[2] - $p0->[2], $ps->[1] - $p0->[1])/ICC::Shared::PI + 1)/2);
278
279 # limit indices
280 0 ▶ $i = $i < $m ? $i : $m - 1;
Branch: 1 branch
BranchTrueFalseTotal
$i < $m ? : 0 0 0
281 0 ▶ $j = $j < $n ? $j : 0;
Branch: 1 branch
BranchTrueFalseTotal
$j < $n ? : 0 0 0
282
283 # get face ID list from spherical index
284 0 $faces = $self->[5][$i][$j];
285
286 # for each gamut face
287 0 for my $f (@{$faces}) {
288
289 # find intersection, if a new face
290 0 ($info, $r, $px) = intersect($self, $f, $p0, $ps);
291
292 # if intersect found
293 0 ▶ if ($info == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info == 0) 0 0 0
294
295 # save result
296 0 $result->[$s] = [$r, $px, $f];
297
298 # quit loop
299 0 last;
300
301 }
302
303 }
304
305 }
306  
307 # return
308 0 return($result);
309  
310 }
311  
312 # compute intersection of line segment with face triangle
313 # the radius is 0 at point_0, and 1 at point_1
314 # parameters: (face_ID, point_0, point_1)
315 # returns: (info, radius, point_intersect)
316 sub intersect {
317  
318 # get parameters
319 0 ▶ my ($self, $fid, $p0, $p1) = @_;
Subroutine intersect: not called CC=12 SCAR=50.5
Pod undocumented
320  
321 # local variables
322 0 my ($v0, $v1, $v2, $u, $v, $n, $dir, $w, $w0, $r, $a, $b);
323 0 my ($px, $uu, $uv, $vv, $wu, $wv, $d, $s, $t);
324  
325 # if face values are cached
326 0 ▶ if (defined($self->[4][$fid])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->[4][$fid]) { } 0 0 0
327
328 # get face vertex
329 0 $v0 = $self->[2][$self->[1][$fid][0]];
330
331 # get face values
332 0 ($u, $v, $n, $uu, $uv, $vv) = @{$self->[4][$fid]};
333
334 } else {
335
336 # get face vertices
337 0 $v0 = $self->[2][$self->[1][$fid][0]];
338 0 $v1 = $self->[2][$self->[1][$fid][1]];
339 0 $v2 = $self->[2][$self->[1][$fid][2]];
340
341 # compute triangle edge vectors
342 0 $u = [$v1->[0] - $v0->[0], $v1->[1] - $v0->[1], $v1->[2] - $v0->[2]];
343 0 $v = [$v2->[0] - $v0->[0], $v2->[1] - $v0->[1], $v2->[2] - $v0->[2]];
344
345 # compute normal vector
346 0 $n = ICC::Shared::crossProduct($u, $v);
347
348 # compute barycentric dot products
349 0 $uu = ICC::Shared::dotProduct($u, $u);
350 0 $uv = ICC::Shared::dotProduct($u, $v);
351 0 $vv = ICC::Shared::dotProduct($v, $v);
352
353 # cache face values
354 0 $self->[4][$fid] = [$u, $v, $n, $uu, $uv, $vv];
355
356 }
357  
358 # check for degenerate triangle
359 0 ▶ return(-1) if ($n->[0] == 0 && $n->[1] == 0 && $n->[2] == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $n->[0] == 0 and $n->[1] == 0 and $n->[2] == 0 0 0 0
Condition0%0/6
$n->[0] == 0 and $n->[1] == 0 and $n->[2] == 0
!ll&&!rl&&r
000
$n->[0] == 0 and $n->[1] == 0
!ll&&!rl&&r
000
MC/DC0%0/3
$n->[0] == 0 and $n->[1] == 0 and $n->[2] == 0
$n->[0] == 0 $n->[1] == 0 $n->[2] == 0
Truth table0%0/4
A and B and C
A= $n->[0] == 0B= $n->[1] == 0C= $n->[2] == 0
ABCresult
0XX0
10X0
1100
1111
360  
361 # compute direction vector
362 0 $dir = [$p1->[0] - $p0->[0], $p1->[1] - $p0->[1], $p1->[2] - $p0->[2]];
363  
364 # compute segment to triangle vector
365 0 $w0 = [$p0->[0] - $v0->[0], $p0->[1] - $v0->[1], $p0->[2] - $v0->[2]];
366  
367 # compute dot products
368 0 $a = -ICC::Shared::dotProduct($n, $w0);
369 0 $b = ICC::Shared::dotProduct($n, $dir);
370  
371 # if b is a very small number
372 0 ▶ if (abs($b) < ICC::Shared::DBL_MIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $b < "2.2250738585072e-308") 0 0 0
373
374 # return (3 - segment lies in plane, 4 - segment disjoint from plane)
375 0 ▶ return($a ? 3 : 4);
Branch: 1 branch
BranchTrueFalseTotal
$a ? : 0 0 0
376
377 }
378  
379 # compute radius
380 0 $r = $a/$b;
381  
382 # check if reverse intersection
383 0 ▶ return(2, $r) if ($r < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $r < 0 0 0 0
384  
385 # compute the intersection point
386 0 $px = [$p0->[0] + $r * $dir->[0], $p0->[1] + $r * $dir->[1], $p0->[2] + $r * $dir->[2]];
387  
388 # compute barycentric dot products
389 0 $w = [$px->[0] - $v0->[0], $px->[1] - $v0->[1], $px->[2] - $v0->[2]];
390 0 $wu = ICC::Shared::dotProduct($w, $u);
391 0 $wv = ICC::Shared::dotProduct($w, $v);
392  
393 # compute common denominator
394 0 $d = $uv * $uv - $uu * $vv;
395  
396 # compute barycentric coordinate
397 0 $s = ($uv * $wv - $vv * $wu) / $d;
398  
399 # return if intersect outside triangle
400 0 ▶ return(1, $r, $px) if ($s < 0 || $s > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $s < 0 or $s > 1 0 0 0
Condition0%0/3
$s < 0 or $s > 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$s < 0 or $s > 1
$s < 0 $s > 1
Truth table0%0/3
A or B
A= $s < 0B= $s > 1
ABresult
1X1
011
000
401  
402 # compute barycentric coordinate
403 0 $t = ($uv * $wu - $uu * $wv) / $d;
404  
405 # return if intersect outside triangle
406 0 ▶ return(1, $r, $px) if ($t < 0 || ($s + $t) > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $t < 0 or $s + $t > 1 0 0 0
Condition0%0/3
$t < 0 or $s + $t > 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$t < 0 or $s + $t > 1
$t < 0 $s + $t > 1
Truth table0%0/3
A or B
A= $t < 0B= $s + $t > 1
ABresult
1X1
011
000
407  
408 # return intersect within triangle
409 0 return(0, $r, $px);
410  
411 }
412  
413 # create gbd_ object from ICC profile
414 # parameters: (ref_to_parent_object, file_handle, ref_to_tag_table_entry)
415 # returns: (ref_to_object)
416 sub new_fh {
417  
418 # get object class
419 0 ▶ my $class = shift();
Subroutine new_fh: not called CC=2 SCAR=17.9
Pod undocumented
420  
421 # create empty gbd_ object
422 0 my $self = [
423 {}, # header
424 [], # matrix
425 [] # offset
426 ];
427  
428 # verify 3 parameters
429 0 ▶ (@_ == 3) || croak('wrong number of parameters');
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or croak("wrong number of parameters") 0 0 0
430  
431 # read gbd_ data from profile
432 0 _readICCgbd_($self, @_);
433  
434 # bless object
435 0 bless($self, $class);
436  
437 # return object reference
438 0 return($self);
439  
440 }
441  
442 # writes gbd_ object to ICC profile
443 # parameters: (ref_to_parent_object, file_handle, ref_to_tag_table_entry)
444 sub write_fh {
445  
446 # get object reference
447 0 ▶ my $self = shift();
Subroutine write_fh: not called CC=2 SCAR=17.9
Pod undocumented
448  
449 # verify 3 parameters
450 0 ▶ (@_ == 3) || croak('wrong number of parameters');
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or croak("wrong number of parameters") 0 0 0
451  
452 # write gbd_ data to profile
453 0 _writeICCgbd_($self, @_);
454  
455 }
456  
457 # get tag size (for writing to profile)
458 # returns: (clut_size)
459 sub size {
460  
461 # get parameter
462 0 ▶ my $self = shift();
Subroutine size: not called CC=2 SCAR=17.9
Pod undocumented
463  
464 # local variables
465 0 my ($p, $q, $size);
466  
467 # get number of pcs channels
468 0 $p = @{$self->[2][0]};
469  
470 # get number of device channels
471 0 ▶ $q = defined($self->[3][0]) ? @{$self->[3][0]} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->[3][0] ? : 0 0 0
472  
473 # set header size
474 0 $size = 20;
475  
476 # add face vertex IDs
477 0 $size += 12 * @{$self->[1]};
478  
479 # add vertex pcs values
480 0 $size += 4 * $p * @{$self->[2]};
481  
482 # add vertex device values (may be 0)
483 0 $size += 4 * $q * @{$self->[3]};
484  
485 # return size
486 0 return($size);
487  
488 }
489  
490 # print object contents to string
491 # format is an array structure
492 # parameter: ([format])
493 # returns: (string)
494 sub sdump {
495  
496 # get parameters
497 0 ▶ my ($self, $p) = @_;
Subroutine sdump: not called CC=5 SCAR=34.0
Pod documented
498  
499 # local variables
500 0 my ($s, $fmt, $f, $v, $e);
501  
502 # resolve parameter to an array reference
503 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
504  
505 # get format string
506 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
507  
508 # set string to object ID
509 0 $s = sprintf("'%s' object, (0x%x)\n", ref($self), $self);
510  
511 # get stats
512 0 ($f, $v, $e) = _check_faces($self);
513  
514 # append stats
515 0 $s .= "faces: $f vertices: $v edges: $e\n";
516  
517 # return
518 0 return($s);
519  
520 }
521  
522 # check gamut faces
523 # parameters: (ref_to_object)
524 # returns: (faces, vertices, edges)
525 sub _check_faces {
526  
527 # get object reference
528 0 ▶ my $self = shift();
Subroutine _check_faces: not called CC=5 SCAR=34.0
529  
530 # local variables
531 0 my (%v, %e, $p0, $p1, $p2);
532  
533 # for each face
534 0 for my $i (0 .. $#{$self->[1]}) {
535
536 # get indices
537 0 $p0 = $self->[1][$i][0];
538 0 $p1 = $self->[1][$i][1];
539 0 $p2 = $self->[1][$i][2];
540
541 # add vertices
542 0 $v{$p0}++;
543 0 $v{$p1}++;
544 0 $v{$p2}++;
545
546 # add edges
547 0 ▶ $e{$p0 > $p1 ? "$p0:$p1" : "$p1:$p0"}++;
Branch: 1 branch
BranchTrueFalseTotal
$p0 > $p1 ? : 0 0 0
548 0 ▶ $e{$p1 > $p2 ? "$p1:$p2" : "$p2:$p1"}++;
Branch: 1 branch
BranchTrueFalseTotal
$p1 > $p2 ? : 0 0 0
549 0 ▶ $e{$p0 > $p2 ? "$p0:$p2" : "$p2:$p0"}++;
Branch: 1 branch
BranchTrueFalseTotal
$p0 > $p2 ? : 0 0 0
550
551 }
552  
553 # return faces, vertices, edges
554 0 return(scalar(@{$self->[1]}), scalar(keys(%v)), scalar(keys(%e)));
555  
556 }
557  
558 # make spherical index
559 # parameters: (object_ref, point_inside_gamut, latitude_steps, longitude_steps)
560 sub _make_index {
561  
562 # get parameters
563 0 ▶ my ($self, $p0, $m, $n) = @_;
Subroutine _make_index: not called CC=9 SCAR=45.0
564  
565 # local variables
566 0 my ($f, $s, $length, $dc, $dot, $dxy);
567  
568 # for each face
569 0 for my $i (0 .. $#{$self->[1]}) {
570
571 # for each coordinate
572 0 for my $j (0 .. 2) {
573
574 # for each vertex
575 0 for my $k (0 .. 2) {
576
577 # add value to face centroid
578 0 $f->[$j][$i] += $self->[2][$self->[1][$i][$k]][$j]/3;
579
580 }
581
582 # subtract internal point value
583 0 $f->[$j][$i] -= $p0->[$j];
584
585 }
586
587 # compute vector length
588 0 $length = sqrt($f->[0][$i]**2 + $f->[1][$i]**2 + $f->[2][$i]**2);
589
590 # for each coordinate
591 0 for my $j (0 .. 2) {
592
593 # normalize
594 0 $f->[$j][$i] /= $length;
595
596 }
597
598 }
599  
600 # for each x
601 0 for my $i (0 .. $m - 1) {
602
603 # for each y
604 0 for my $j (0 .. $n - 1) {
605
606 # compute spherical unit vector for cell[x][y]
607 0 $dc = sin(ICC::Shared::PI * ($i + 0.5)/$m);
608 0 $s->[$n * $i + $j][0] = cos(ICC::Shared::PI * ($i + 0.5)/$m);
609 0 $s->[$n * $i + $j][1] = -$dc * cos(2 * ICC::Shared::PI * (($j + 0.5)/$n));
610 0 $s->[$n * $i + $j][2] = -$dc * sin(2 * ICC::Shared::PI * (($j + 0.5)/$n));
611
612 }
613
614 }
615  
616 # compute dot products [s x 3] * [3 x f] = [s x f]
617 0 $dot = ICC::Support::Lapack::mat_xplus($s, $f);
618  
619 # initialize index
620 0 undef($self->[5]);
621  
622 # for each x
623 0 for my $i (0 .. $m - 1) {
624
625 # for each y
626 0 for my $j (0 .. $n - 1) {
627
628 # get dot product list for cell[x][y]
629 0 $dxy = $dot->[$n * $i + $j];
630
631 # compute face ID list, sorted by dot product
632 0 $self->[5][$i][$j] = [map {$_->[0]} sort {$b->[1] <=> $a->[1]} map {[$_, $dxy->[$_]]} (0 .. $#{$self->[1]})];
633
634 }
635
636 }
637  
638 # save internal point in header hash
639 0 $self->[0]{'p0'} = $p0;
640  
641 }
642  
643 # make new gbd_ object from attribute hash
644 # hash keys are: ('vertex', 'pcs', 'device')
645 # object elements not specified in the hash are unchanged
646 # parameters: (ref_to_object, ref_to_attribute_hash)
647 sub _new_from_hash {
648  
649 # get parameters
650 0 ▶ my ($self, $hash) = @_;
Subroutine _new_from_hash: not called CC=26 SCAR=65.5
651  
652 # local variables
653 0 my ($value, $f, $v, $e);
654  
655 # if 'vertex' key defined
656 0 ▶ if (defined($hash->{'vertex'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash->{"vertex"}) 0 0 0
657
658 # get value
659 0 $value = $hash->{'vertex'};
660
661 # if reference to a 2-D array
662 0 ▶ if (ref($value) eq 'ARRAY' && @{$value} == grep {ref() eq 'ARRAY'} @{$value}) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY' and @{$value;} == grep({ref $_ eq 'ARRAY';} @{$value;})) { } 0 0 0
elsif (UNIVERSAL::isa($value, 'Math::Matrix')) { } 0 0 0
Condition0%0/3
ref $value eq "ARRAY" and @{$value;} == grep({ref $_ eq "ARRAY";} @{$value;})
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value eq "ARRAY" and @{$value;} == grep({ref $_ eq "ARRAY";} @{$value;})
ref $value eq "ARRAY" @{$value;} == grep({ref $_ eq "ARRAY";} @{$value;})
Truth table0%0/3
A and B
A= ref $value eq "ARRAY"B= @{$value;} == grep({ref $_ eq "ARRAY";} @{$value;})
ABresult
0X0
100
111
663
664 # set vertex to clone of array
665 0 $self->[1] = bless(Storable::dclone($value), 'Math::Matrix');
666
667 # if a reference to a Math::Matrix object
668 } elsif (UNIVERSAL::isa($value, 'Math::Matrix')) {
669
670 # set vertex to object
671 0 $self->[1] = $value;
672
673 } else {
674
675 # wrong data type
676 0 croak('wrong \'vertex\' data type');
677
678 }
679
680 # verify number of faces
681 0 ▶ (@{$self->[1]} >= 4) || croak('number of faces < 4');
Branch: 1 branch
BranchTrueFalseTotal
@{$self->[1];} >= 4 or croak("number of faces < 4") 0 0 0
682
683 # verify number of vertices per face
684 0 ▶ (@{$self->[1]} == 3) || croak('number of vertices per face <> 3');
Branch: 1 branch
BranchTrueFalseTotal
@{$self->[1];} == 3 or croak("number of vertices per face <> 3") 0 0 0
685
686 # check gamut faces
687 0 ($f, $v, $e) = _check_faces($self);
688
689 # verify closed shape using Euler's formula
690 0 ▶ ($f + $v - $e == 2) || carp('not a closed shape');
Branch: 1 branch
BranchTrueFalseTotal
$f + $v - $e == 2 or carp("not a closed shape") 0 0 0
691
692 }
693  
694 # if 'pcs' key defined
695 0 ▶ if (defined($hash->{'pcs'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash->{"pcs"}) 0 0 0
696
697 # get value
698 0 $value = $hash->{'pcs'};
699
700 # if reference to a 2-D array
701 0 ▶ if (ref($value) eq 'ARRAY' && @{$value} == grep {ref() eq 'ARRAY'} @{$value}) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY' and @{$value;} == grep({ref $_ eq 'ARRAY';} @{$value;})) { } 0 0 0
elsif (UNIVERSAL::isa($value, 'Math::Matrix')) { } 0 0 0
Condition0%0/3
ref $value eq "ARRAY" and @{$value;} == grep({ref $_ eq "ARRAY";} @{$value;})
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value eq "ARRAY" and @{$value;} == grep({ref $_ eq "ARRAY";} @{$value;})
ref $value eq "ARRAY" @{$value;} == grep({ref $_ eq "ARRAY";} @{$value;})
Truth table0%0/3
A and B
A= ref $value eq "ARRAY"B= @{$value;} == grep({ref $_ eq "ARRAY";} @{$value;})
ABresult
0X0
100
111
702
703 # set pcs to clone of array
704 0 $self->[1] = bless(Storable::dclone($value), 'Math::Matrix');
705
706 # if a reference to a Math::Matrix object
707 } elsif (UNIVERSAL::isa($value, 'Math::Matrix')) {
708
709 # set pcs to object
710 0 $self->[2] = $value;
711
712 } else {
713
714 # wrong data type
715 0 croak('wrong \'pcs\' data type');
716
717 }
718
719 # verify number of vertices
720 0 ▶ (@{$self->[2]} >= 4) || croak('number of vertices < 4');
Branch: 1 branch
BranchTrueFalseTotal
@{$self->[2];} >= 4 or croak("number of vertices < 4") 0 0 0
721
722 # verify number of pcs channels
723 0 ▶ (@{$self->[2][0]} >= 3) || croak('number of pcs channels < 3');
Branch: 1 branch
BranchTrueFalseTotal
@{$self->[2][0];} >= 3 or croak("number of pcs channels < 3") 0 0 0
724
725 }
726  
727 # if 'device' key defined
728 0 ▶ if (defined($hash->{'device'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash->{"device"}) 0 0 0
729
730 # get value
731 0 $value = $hash->{'device'};
732
733 # if reference to a 2-D array
734 0 ▶ if (ref($value) eq 'ARRAY' && @{$value} == grep {ref() eq 'ARRAY'} @{$value}) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY' and @{$value;} == grep({ref $_ eq 'ARRAY';} @{$value;})) { } 0 0 0
elsif (UNIVERSAL::isa($value, 'Math::Matrix')) { } 0 0 0
Condition0%0/3
ref $value eq "ARRAY" and @{$value;} == grep({ref $_ eq "ARRAY";} @{$value;})
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value eq "ARRAY" and @{$value;} == grep({ref $_ eq "ARRAY";} @{$value;})
ref $value eq "ARRAY" @{$value;} == grep({ref $_ eq "ARRAY";} @{$value;})
Truth table0%0/3
A and B
A= ref $value eq "ARRAY"B= @{$value;} == grep({ref $_ eq "ARRAY";} @{$value;})
ABresult
0X0
100
111
735
736 # set device to clone of array
737 0 $self->[1] = bless(Storable::dclone($value), 'Math::Matrix');
738
739 # if a reference to a Math::Matrix object
740 } elsif (UNIVERSAL::isa($value, 'Math::Matrix')) {
741
742 # set device to object
743 0 $self->[3] = $value;
744
745 } else {
746
747 # wrong data type
748 0 croak('wrong \'device\' data type');
749
750 }
751
752 # verify number of vertices
753 0 ▶ (@{$self->[3]} >= 4) || croak('number of vertices < 4');
Branch: 1 branch
BranchTrueFalseTotal
@{$self->[3];} >= 4 or croak("number of vertices < 4") 0 0 0
754
755 # verify number of pcs channels
756 0 ▶ (@{$self->[3][0]} >= 1 && @{$self->[3][0]} <= 16) || croak('number of device channels < 1 or > 16');
Branch: 1 branch
BranchTrueFalseTotal
@{$self->[3][0];} >= 1 and @{$self->[3][0];} <= 16 or croak("number of device channels < 1 or > 16") 0 0 0
Condition0%0/3
@{$self->[3][0];} >= 1 and @{$self->[3][0];} <= 16
!ll&&!rl&&r
000
MC/DC0%0/2
@{$self->[3][0];} >= 1 and @{$self->[3][0];} <= 16
@{$self->[3][0];} >= 1 @{$self->[3][0];} <= 16
Truth table0%0/3
A and B
A= @{$self->[3][0];} >= 1B= @{$self->[3][0];} <= 16
ABresult
0X0
100
111
757
758 }
759
760 # verify pcs array size
761 0 ▶ (@{$self->[2]} == 0 || @{$self->[2]} == $v) || croak('pcs and face arrays have different number of vertices');
Branch: 1 branch
BranchTrueFalseTotal
@{$self->[2];} == 0 or @{$self->[2];} == $v or croak("pcs and face arrays have different number of vertices") 0 0 0
Condition0%0/3
@{$self->[2];} == 0 or @{$self->[2];} == $v
l!l&&r!l&&!r
000
MC/DC0%0/2
@{$self->[2];} == 0 or @{$self->[2];} == $v
@{$self->[2];} == 0 @{$self->[2];} == $v
Truth table0%0/3
A or B
A= @{$self->[2];} == 0B= @{$self->[2];} == $v
ABresult
1X1
011
000
762
763 # if both pcs and device arrays were supplied
764 0 ▶ if (defined($hash->{'pcs'}) && defined($hash->{'device'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash->{"pcs"} and defined $hash->{"device"}) 0 0 0
Condition0%0/3
defined $hash->{"pcs"} and defined $hash->{"device"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $hash->{"pcs"} and defined $hash->{"device"}
defined $hash->{"pcs"} defined $hash->{"device"}
Truth table0%0/3
A and B
A= defined $hash->{"pcs"}B= defined $hash->{"device"}
ABresult
0X0
100
111
765
766 # verify pcs and device arrays have same number of vertices
767 0 ▶ (@{$self->[2]} == @{$self->[3]}) || croak('pcs and device arrays are different sizes');
Branch: 1 branch
BranchTrueFalseTotal
@{$self->[2];} == @{$self->[3];} or croak("pcs and device arrays are different sizes") 0 0 0
768
769 }
770
771 }
772  
773 # read gbd_ tag from ICC profile
774 # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
775 sub _readICCgbd_ {
776  
777 # get parameters
778 0 ▶ my ($self, $parent, $fh, $tag) = @_;
Subroutine _readICCgbd_: not called CC=5 SCAR=34.0
779  
780 # local variables
781 0 my ($buf, $p, $q, $v, $f, $bytes);
782  
783 # save tag signature
784 0 $self->[0]{'signature'} = $tag->[0];
785  
786 # seek start of tag
787 0 seek($fh, $tag->[1], 0);
788  
789 # read tag header
790 0 read($fh, $buf, 20);
791  
792 # unpack header
793 0 ($p, $q, $v, $f) = unpack('x8 n2 N2', $buf);
794  
795 # for each face
796 0 for my $i (0 .. $f - 1) {
797
798 # read vertex IDs
799 0 read($fh, $buf, 12);
800
801 # unpack the values
802 0 $self->[1][$i] = [unpack('N3', $buf)];
803
804 }
805  
806 # bless to Math::Matrix object
807 0 bless($self->[1], 'Math::Matrix');
808  
809 # compute the buffer size
810 0 $bytes = 4 * $p;
811  
812 # for each vertex
813 0 for my $i (0 .. $v - 1) {
814
815 # read vertex PCS values
816 0 read($fh, $buf, $bytes);
817
818 # unpack the values
819 0 $self->[2][$i] = [unpack('f>*', $buf)];
820
821 }
822  
823 # bless to Math::Matrix object
824 0 bless($self->[2], 'Math::Matrix');
825  
826 # if there are device values
827 0 ▶ if ($bytes = 4 * $q) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes = 4 * $q) 0 0 0
828
829 # for each vertex
830 0 for my $i (0 .. $v - 1) {
831
832 # read vertex device values
833 0 read($fh, $buf, $bytes);
834
835 # unpack the values
836 0 $self->[3][$i] = [unpack('f>*', $buf)];
837
838 }
839
840 # bless to Math::Matrix object
841 0 bless($self->[3], 'Math::Matrix');
842
843 }
844
845 }
846  
847 # write gbd_ tag to ICC profile
848 # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
849 sub _writeICCgbd_ {
850  
851 # get parameters
852 0 ▶ my ($self, $parent, $fh, $tag) = @_;
Subroutine _writeICCgbd_: not called CC=9 SCAR=45.0
853  
854 # local variables
855 0 my ($p, $q, $v, $f);
856  
857 # get number of PCS channels
858 0 $p = @{$self->[2][0]};
859  
860 # get number of device channels
861 0 ▶ $q = defined($self->[3][0]) ? @{$self->[3][0]} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->[3][0] ? : 0 0 0
862  
863 # get number of vertices
864 0 $v = @{$self->[2]};
865  
866 # get number of faces
867 0 $f = @{$self->[1]};
868  
869 # validate number PCS channels (3 and up)
870 0 ▶ ($p >= 3) || croak('unsupported number of input channels');
Branch: 1 branch
BranchTrueFalseTotal
$p >= 3 or croak("unsupported number of input channels") 0 0 0
871  
872 # validate number device channels (1 to 15)
873 0 ▶ ($q > 0 && $q < 16) || croak('unsupported number of output channels');
Branch: 1 branch
BranchTrueFalseTotal
$q > 0 and $q < 16 or croak("unsupported number of output channels") 0 0 0
Condition0%0/3
$q > 0 and $q < 16
!ll&&!rl&&r
000
MC/DC0%0/2
$q > 0 and $q < 16
$q > 0 $q < 16
Truth table0%0/3
A and B
A= $q > 0B= $q < 16
ABresult
0X0
100
111
874  
875 # seek start of tag
876 0 seek($fh, $tag->[1], 0);
877  
878 # write tag header
879 0 print $fh pack('a4 x4 n2 N2', 'gbd ', $p, $q, $v, $f);
880  
881 # for each face
882 0 for my $i (0 .. $f - 1) {
883
884 # write face vertex IDs
885 0 print $fh pack('N3', @{$self->[1][$i]});
886
887 }
888  
889 # for each vertex
890 0 for my $i (0 .. $v - 1) {
891
892 # write vertex PCS values
893 0 print $fh pack('f>*', @{$self->[2][$i]});
894
895 }
896  
897 # if there are vertex device values
898 0 ▶ if ($q) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q) 0 0 0
899
900 # for each vertex
901 0 for my $i (0 .. $v - 1) {
902
903 # write vertex device values
904 0 print $fh pack('f>*', @{$self->[3][$i]});
905
906 }
907
908 }
909
910 }
911  
912 2 1;
« blib/lib/ICC/Shared.pm ↑ Summary lib/ICC/Support/rbf.pm »