lib/ICC/Profile/curf.pm

filter: Statementstmt 10.3% 14 / 135 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 25.0% 4 / 16 Podpod 11.1% 1 / 9 totaltotal 7.7% 19 / 245 CC 42 SCAR 36.2
CC
42
Cov
7%
_new_from_hash
90.0
_readICCcurf
56.0
derivative
42.0
CRAP 37.3
« blib/lib/ICC/Profile/mBA_.pm ↑ Summary blib/lib/ICC/Support/nMIX.pm »
1 package ICC::Profile::curf;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = 0.12;
7  
8 # revised 2018-08-07
9 #
10 # Copyright © 2004-2018 by William B. Birkett
11  
12 # add development directory
13 1 ▶use lib 'lib';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # inherit from Shared
16 1 ▶use parent qw(ICC::Shared);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # create new curf tag object
19 # hash may contain pointers to segments or breakpoints
20 # segments are an array of 'parf' or 'samf' objects
21 # hash keys are: ('segment', 'breakpoint')
22 # parameters: ([ref_to_attribute_hash])
23 # returns: (ref_to_object)
24 sub new {
25  
26 # get object class
27 0 ▶ my $class = shift();
Subroutine new: not called CC=4 SCAR=30.0
Pod undocumented
28  
29 # create empty curf object
30 0 my $self = [
31 {}, # object header
32 [], # segment object array
33 [] # breakpoint array
34 ];
35  
36 # if there are parameters
37 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
38
39 # if one parameter, a hash reference
40 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
41
42 # make new curf object from attribute hash
43 0 _new_from_hash($self, shift());
44
45 } else {
46
47 # error
48 0 croak('\'curf\' parameter must be a hash reference');
49
50 }
51
52 }
53  
54 # bless object
55 0 bless($self, $class);
56  
57 # return object reference
58 0 return($self);
59  
60 }
61  
62 # create curf tag object from ICC profile
63 # parameters: (ref_to_parent_object, file_handle, ref_to_tag_table_entry)
64 # returns: (ref_to_object)
65 sub new_fh {
66  
67 # get object class
68 0 ▶ my $class = shift();
Subroutine new_fh: not called CC=2 SCAR=17.9
Pod undocumented
69  
70 # create empty curf object
71 0 my $self = [
72 {}, # object header
73 [], # segment object array
74 [] # breakpoint array
75 ];
76  
77 # verify 3 parameters
78 0 ▶ (@_ == 3) or croak('wrong number of parameters');
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or croak("wrong number of parameters") 0 0 0
79  
80 # read curf data from profile
81 0 _readICCcurf($self, @_);
82  
83 # bless object
84 0 bless($self, $class);
85  
86 # return object reference
87 0 return($self);
88  
89 }
90  
91 # writes curf tag object to ICC profile
92 # parameters: (ref_to_parent_object, file_handle, ref_to_tag_table_entry)
93 sub write_fh {
94  
95 # verify 4 parameters
96 0 ▶ (@_ == 4) or croak('wrong number of parameters');
Subroutine write_fh: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ == 4 or croak("wrong number of parameters") 0 0 0
97  
98 # write curf data to profile
99 0 goto &_writeICCcurf;
100  
101 }
102  
103 # get tag size (for writing to profile)
104 # returns: (tag_size)
105 sub size {
106  
107 # get parameters
108 0 ▶ my ($self) = @_;
Subroutine size: not called CC=2 SCAR=17.9
Pod undocumented
109  
110 # set size of header and breakpoints
111 0 my $size = 12 + 4 * @{$self->[2]};
112  
113 # for each curve segment
114 0 for my $seg (@{$self->[1]}) {
115
116 # add size
117 0 $size += $seg->size();
118
119 }
120  
121 # return size
122 0 return($size);
123  
124 }
125  
126 # compute curve derivative function
127 # parameters: (input_value)
128 # returns: (output_value)
129 sub derivative {
130  
131 # get parameters
132 0 ▶ my ($self, $in) = @_;
Subroutine derivative: not called CC=6 SCAR=37.4
Pod undocumented
133  
134 # local variables
135 0 my ($ix);
136  
137 # return transformed value, if just one segment
138 0 ▶ return($self->[1][0]->derivative($in)) if ($#{$self->[1]} == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $#{$$self[1];} == 0 0 0 0
139  
140 # initialize index
141 0 $ix = 0;
142  
143 # for each breakpoint
144 0 for my $bp (@{$self->[2]}) {
145
146 # last if breakpoint >= input value
147 0 ▶ last if ($bp >= $in);
Branch: 1 branch
BranchTrueFalseTotal
if $bp >= $in 0 0 0
148
149 # increment index
150 0 $ix++;
151
152 }
153  
154 # if segment is a 'parf' object
155 0 ▶ if (UNIVERSAL::isa($self->[1][$ix], 'ICC::Profile::parf')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($self->[1][$ix], 'ICC::Profile::parf')) { } 0 0 0
elsif (UNIVERSAL::isa($self->[1][$ix], 'ICC::Profile::samf')) { } 0 0 0
156
157 # return transformed value
158 0 return($self->[1][$ix]->derivative($in));
159
160 # if segment is a 'samf' object
161 } elsif (UNIVERSAL::isa($self->[1][$ix], 'ICC::Profile::samf')) {
162
163 # return transformed value
164 0 return($self->[1][$ix]->derivative($in, $self->[2][$ix - 1], $self->[2][$ix], $self->[1][$ix - 1]));
165
166 } else {
167
168 # error
169 0 croak('unsupported object class for \'curf\' segment');
170
171 }
172
173 }
174  
175 # compute curve function
176 # parameters: (input_value)
177 # returns: (output_value)
178 sub transform {
179  
180 # get parameters
181 0 ▶ my ($self, $in) = @_;
Subroutine transform: not called CC=6 SCAR=37.4
Pod undocumented
182  
183 # local variables
184 0 my ($ix);
185  
186 # return transformed value, if just one segment
187 0 ▶ return($self->[1][0]->transform($in)) if ($#{$self->[1]} == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $#{$$self[1];} == 0 0 0 0
188  
189 # initialize index
190 0 $ix = 0;
191  
192 # for each breakpoint
193 0 for my $bp (@{$self->[2]}) {
194
195 # last if breakpoint >= input value
196 0 ▶ last if ($bp >= $in);
Branch: 1 branch
BranchTrueFalseTotal
if $bp >= $in 0 0 0
197
198 # increment index
199 0 $ix++;
200
201 }
202  
203 # if segment is a 'parf' object
204 0 ▶ if (UNIVERSAL::isa($self->[1][$ix], 'ICC::Profile::parf')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($self->[1][$ix], 'ICC::Profile::parf')) { } 0 0 0
elsif (UNIVERSAL::isa($self->[1][$ix], 'ICC::Profile::samf')) { } 0 0 0
205
206 # return transformed value
207 0 return($self->[1][$ix]->transform($in));
208
209 # if segment is a 'samf' object
210 } elsif (UNIVERSAL::isa($self->[1][$ix], 'ICC::Profile::samf')) {
211
212 # return transformed value
213 0 return($self->[1][$ix]->transform($in, $self->[2][$ix - 1], $self->[2][$ix], $self->[1][$ix - 1]));
214
215 } else {
216
217 # error
218 0 croak('unsupported object class for \'curf\' segment');
219
220 }
221
222 }
223  
224 # get/set segment array reference
225 # parameters: ([ref_to_array])
226 # returns: (ref_to_array)
227 sub segment {
228  
229 # get object reference
230 0 ▶ my $self = shift();
Subroutine segment: not called CC=3 SCAR=24.8
Pod undocumented
231  
232 # if parameter
233 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
234
235 # verify array reference
236 0 ▶ (ref($_[0]) eq 'ARRAY') or croak('not an array reference');
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "ARRAY" or croak("not an array reference") 0 0 0
237
238 # set array reference
239 0 $self->[1] = [@{shift()}];
240
241 }
242  
243 # return array reference
244 0 return($self->[1]);
245  
246 }
247  
248 # get/set breakpoint array reference
249 # parameters: ([ref_to_array])
250 # returns: (ref_to_array)
251 sub breakpoint {
252  
253 # get object reference
254 0 ▶ my $self = shift();
Subroutine breakpoint: not called CC=3 SCAR=24.8
Pod undocumented
255  
256 # if parameter
257 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
258
259 # verify array reference
260 0 ▶ (ref($_[0]) eq 'ARRAY') or croak('not an array reference');
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "ARRAY" or croak("not an array reference") 0 0 0
261
262 # set array reference
263 0 $self->[2] = [@{shift()}];
264
265 }
266  
267 # return array reference
268 0 return($self->[2]);
269  
270 }
271  
272 # print object contents to string
273 # format is an array structure
274 # parameter: ([format])
275 # returns: (string)
276 sub sdump {
277  
278 # get parameters
279 0 ▶ my ($self, $p) = @_;
Subroutine sdump: not called CC=5 SCAR=34.0
Pod documented
280  
281 # local variables
282 0 my ($s, $fmt);
283  
284 # resolve parameter to an array reference
285 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
286  
287 # get format string
288 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
289  
290 # set string to object ID
291 0 $s = sprintf("'%s' object, (0x%x)\n", ref($self), $self);
292  
293 # return
294 0 return($s);
295  
296 }
297  
298 # make new curf object from attribute hash
299 # hash may contain pointers to segments, or breakpoints
300 # hash keys are: ('segment', 'breakpoint')
301 # object elements not specified in the hash are unchanged
302 # parameters: (ref_to_object, ref_to_attribute_hash)
303 sub _new_from_hash {
304  
305 # get parameters
306 0 ▶ my ($self, $hash) = @_;
Subroutine _new_from_hash: not called CC=9 SCAR=45.0
307  
308 # for each attribute
309 0 for my $attr (keys(%{$hash})) {
310
311 # if 'segment'
312 0 ▶ if ($attr eq 'segment') {
Branch: 2 branches
BranchTrueFalseTotal
if ($attr eq 'segment') { } 0 0 0
elsif ($attr eq 'breakpoint') { } 0 0 0
313
314 # if reference to an array of 'parf' or 'samf' objects
315 0 ▶ if (ref($hash->{$attr}) eq 'ARRAY' && @{$hash->{$attr}} == grep {UNIVERSAL::isa($_, 'ICC::Profile::parf') || UNIVERSAL::isa($_, 'ICC::Profile::samf')} @{$hash->{$attr}}) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $hash->{$attr} eq 'ARRAY' and @{$hash->{$attr};} == grep({UNIVERSAL::isa($_, 'ICC::Profile::parf') or UNIVERSAL::isa($_, 'ICC::Profile::samf');} @{$$hash{$attr};})) { } 0 0 0
UNIVERSAL::isa($_, "ICC::Profile::parf") or UNIVERSAL::isa($_, "ICC::Profile::samf") 0 0 0
Condition0%0/3
ref $hash->{$attr} eq "ARRAY" and @{$hash->{$attr};} == grep({UNIVERSAL::isa($_, "ICC::Profile::parf") or UNIVERSAL::isa($_, "ICC::Profile::samf");} @{$$hash{$attr};})
!ll&&!rl&&r
000
MC/DC0%0/2
ref $hash->{$attr} eq "ARRAY" and @{$hash->{$attr};} == grep({UNIVERSAL::isa($_, "ICC::Profile::parf") or UNIVERSAL::isa($_, "ICC::Profile::samf");} @{$$hash{$attr};})
ref $hash->{$attr} eq "ARRAY" @{$hash->{$attr};} == grep({UNIVERSAL::isa($_, "ICC::Profile::parf") or UNIVERSAL::isa($_, "ICC::Profile::samf");} @{$$hash{$attr};})
Truth table0%0/3
A and B
A= ref $hash->{$attr} eq "ARRAY"B= @{$hash->{$attr};} == grep({UNIVERSAL::isa($_, "ICC::Profile::parf") or UNIVERSAL::isa($_, "ICC::Profile::samf");} @{$$hash{$attr};})
ABresult
0X0
100
111
316
317 # set object element
318 0 $self->[1] = [@{$hash->{$attr}}];
319
320 } else {
321
322 # wrong data type
323 0 croak('\'curf\' segment attribute must be a reference to an array of \'parf\' or \'samf\' objects');
324
325 }
326
327 # if 'breakpoint'
328 } elsif ($attr eq 'breakpoint') {
329
330 # if reference to an array of scalars
331 0 ▶ if (ref($hash->{$attr}) eq 'ARRAY' && @{$hash->{$attr}} == grep {! ref()} @{$hash->{$attr}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash->{$attr} eq 'ARRAY' and @{$hash->{$attr};} == grep({not ref $_;} @{$$hash{$attr};})) { } 0 0 0
Condition0%0/3
ref $hash->{$attr} eq "ARRAY" and @{$hash->{$attr};} == grep({not ref $_;} @{$$hash{$attr};})
!ll&&!rl&&r
000
MC/DC0%0/2
ref $hash->{$attr} eq "ARRAY" and @{$hash->{$attr};} == grep({not ref $_;} @{$$hash{$attr};})
ref $hash->{$attr} eq "ARRAY" @{$hash->{$attr};} == grep({not ref $_;} @{$$hash{$attr};})
Truth table0%0/3
A and B
A= ref $hash->{$attr} eq "ARRAY"B= @{$hash->{$attr};} == grep({not ref $_;} @{$$hash{$attr};})
ABresult
0X0
100
111
332
333 # set object element
334 0 $self->[2] = [@{$hash->{$attr}}];
335
336 } else {
337
338 # wrong data type
339 0 croak('\'curf\' breakpoint attribute must be a reference to an array of scalars');
340
341 }
342
343 }
344
345 }
346
347 }
348  
349 # read curf tag from ICC profile
350 # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
351 sub _readICCcurf {
352  
353 # get parameters
354 0 ▶ my ($self, $parent, $fh, $tag) = @_;
Subroutine _readICCcurf: not called CC=7 SCAR=40.3
355  
356 # local variables
357 0 my ($buf, $segs, $mark, $class);
358  
359 # save tag signature
360 0 $self->[0]{'signature'} = $tag->[0];
361  
362 # seek start of tag
363 0 seek($fh, $tag->[1], 0);
364  
365 # read tag header
366 0 read($fh, $buf, 12);
367  
368 # unpack number of segments
369 0 $segs = unpack('x8 n x2', $buf);
370  
371 # if one segment
372 0 ▶ if ($segs == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($segs == 1) { } 0 0 0
elsif ($segs > 1) { } 0 0 0
373
374 # mark file offset
375 0 $mark = tell($fh);
376
377 # read segment type signature
378 0 read($fh, $buf, 4);
379
380 # if type is 'parf'
381 0 ▶ if ($buf eq 'parf') {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf eq 'parf') { } 0 0 0
382
383 # create object
384 0 $self->[1][0] = ICC::Profile::parf->new_fh($self, $fh, ['curf', $mark]);
385
386 } else {
387
388 # error
389 0 croak('wrong segment type in \'curf\' tag');
390
391 }
392
393 # if more than one segment
394 } elsif ($segs > 1) {
395
396 # read breakpoint values
397 0 read($fh, $buf, 4 * ($segs - 1));
398
399 # unpack breakpoint values
400 0 $self->[2] = [unpack('f>*', $buf)];
401
402 # for each segment
403 0 for my $i (0 .. $segs - 1) {
404
405 # mark file offset
406 0 $mark = tell($fh);
407
408 # read segment type signature
409 0 read($fh, $buf, 4);
410
411 # if type is 'parf' or 'samf'
412 0 ▶ if ($buf eq 'parf' || $buf eq 'samf') {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf eq 'parf' or $buf eq 'samf') { } 0 0 0
Condition0%0/3
$buf eq "parf" or $buf eq "samf"
l!l&&r!l&&!r
000
MC/DC0%0/2
$buf eq "parf" or $buf eq "samf"
$buf eq "parf" $buf eq "samf"
Truth table0%0/3
A or B
A= $buf eq "parf"B= $buf eq "samf"
ABresult
1X1
011
000
413
414 # form class specifier
415 0 $class = "ICC::Profile::$buf";
416
417 # create specific tag object
418 0 $self->[1][$i] = $class->new_fh($self, $fh, ['curf', $mark]);
419
420 } else {
421
422 # error
423 0 croak('unsupported segment type in \'curf\' tag');
424
425 }
426
427 }
428
429 } else {
430
431 # error
432 0 croak('\'curf\' tag has no segments');
433
434 }
435
436 }
437  
438 # write curf tag to ICC profile
439 # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
440 sub _writeICCcurf {
441  
442 # get parameters
443 0 ▶ my ($self, $parent, $fh, $tag) = @_;
Subroutine _writeICCcurf: not called CC=4 SCAR=30.0
444  
445 # verify segments
446 0 ▶ (@{$self->[1]} > 0) or carp('\'curf\' object must contain at least one segment');
Branch: 1 branch
BranchTrueFalseTotal
@{$self->[1];} > 0 or carp("'curf' object must contain at least one segment") 0 0 0
447  
448 # verify breakpoints
449 0 ▶ (@{$self->[1]} == @{$self->[2]} + 1) or carp('\'curf\' object must contain a breakpoint between each segment');
Branch: 1 branch
BranchTrueFalseTotal
@{$self->[1];} == @{$self->[2];} + 1 or carp("'curf' object must contain a breakpoint between each segment") 0 0 0
450  
451 # seek start of tag
452 0 seek($fh, $tag->[1], 0);
453  
454 # write tag type signature and segment count
455 0 print $fh pack('a4 x4 n x2', 'curf', scalar(@{$self->[1]}));
456  
457 # write breakpoints
458 0 print $fh pack('f>*', @{$self->[2]});
459  
460 # for each segment
461 0 for my $seg (@{$self->[1]}) {
462
463 # write segment data
464 0 $seg->write_fh($self, $fh, ['curf', tell($fh)]);
465
466 }
467
468 }
469  
470 1 1;
« blib/lib/ICC/Profile/mBA_.pm ↑ Summary blib/lib/ICC/Support/nMIX.pm »