blib/lib/ICC/Profile/curv.pm

filter: Statementstmt 38.4% 60 / 156 Branchbran 16.6% 14 / 84 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 60.0% 12 / 20 Podpod 10.0% 1 / 10 totaltotal 31.6% 87 / 275 CC 45 SCAR 32.2
CC
45
Cov
30%
transform
56.0
derivative
56.0
_binsearch
42.0
CRAP 25.1
« blib/lib/ICC/Support/ratfunc.pm ↑ Summary blib/lib/ICC/Support/nNET2.pm »
1 package ICC::Profile::curv;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 our $VERSION = 2.11;
7  
8 # revised 2016-05-17
9 #
10 # Copyright © 2004-2018 by William B. Birkett
11  
12 # add development directory
13 7 ▶use lib 'lib';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # inherit from Shared
16 7 ▶use parent qw(ICC::Shared);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # use POSIX math
19 7 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # create new 'curv' tag object
22 # with no parameters, 'curv' has identity response
23 # if array has one value, 'curv' has gamma response (256 = gamma 1)
24 # if array has multiple values, 'curv' is a linear piecewise function (range 0 - 1)
25 # parameters: ([ref_to_array])
26 # returns: (ref_to_object)
27 sub new {
28  
29 # get object class
30 12 ▶ my $class = shift();
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
31  
32 # create empty curv object
33 12 my $self = [
34 {}, # object header
35 [] # curve array
36 ];
37  
38 # if parameter supplied
39 12 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 11 1 12
40
41 # verify array reference
42 11 ▶ (ref($_[0]) eq 'ARRAY') || croak('not an array reference');
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "ARRAY" or croak("not an array reference") 0 11 11
43
44 # copy array
45 11 $self->[1] = [@{shift()}];
46
47 }
48  
49 # bless object
50 12 bless($self, $class);
51
52 # return object reference
53 12 return($self);
54  
55 }
56  
57 # create inverse 'curv' object
58 # returns: (ref_to_object)
59 sub inv {
60  
61 # get object
62 0 ▶ my $self = shift();
Subroutine inv: not called CC=5 SCAR=34.0
Pod undocumented
63  
64 # local variable
65 0 my ($array);
66  
67 # if identity curve
68 0 ▶ if (@{$self->array()} == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$self->array;} == 0) { } 0 0 0
elsif (@{$self->array;} == 1) { } 0 0 0
69
70 # return identity curve
71 0 return(ICC::Profile::curv->new());
72
73 # if gamma curve
74 0 } elsif (@{$self->array()} == 1) {
75
76 # verify gamma > 0
77 0 ▶ ($self->array->[0] > 0) || croak('gamma must be > 0');
Branch: 1 branch
BranchTrueFalseTotal
$self->array->[0] > 0 or croak("gamma must be > 0") 0 0 0
78
79 # return inverse gamma curve
80 0 return(ICC::Profile::curv->new([65536/$self->array->[0]]));
81
82 # if LUT curve
83 } else {
84
85 # for each point
86 0 for my $i (0 .. 4095) {
87
88 # compute inverse curve value
89 0 $array->[$i] = $self->inverse($i/4095);
90
91 }
92
93 # return inverse curve
94 0 return(ICC::Profile::curv->new($array));
95
96 }
97
98 }
99  
100 # get/set array reference
101 # parameters: ([ref_to_array])
102 # returns: (ref_to_array)
103 sub array {
104  
105 # get object reference
106 32 ▶ my $self = shift();
Subroutine array: called CC=3 SCAR=15.1
Pod undocumented
107  
108 # if parameter
109 32 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 32 32
110
111 # verify array reference
112 0 ▶ (ref($_[0]) eq 'ARRAY') || croak('not an array reference');
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "ARRAY" or croak("not an array reference") 0 0 0
113
114 # set array reference
115 0 $self->[1] = shift();
116
117 }
118  
119 # return array reference
120 32 return($self->[1]);
121  
122 }
123  
124 # compute curve function
125 # domain/range is (0 - 1)
126 # parameters: (input_value)
127 # returns: (output_value)
128 sub transform {
129  
130 # get parameters
131 0 ▶ my ($self, $in) = @_;
Subroutine transform: not called CC=7 SCAR=40.3
Pod undocumented
132  
133 # local variables
134 0 my ($array, $upper, $ix, $ir);
135  
136 # get array reference
137 0 $array = $self->[1];
138  
139 # get array upper subscript
140 0 $upper = $#{$array};
141  
142 # if array size == 0 (identity)
143 0 ▶ if (@{$array} == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$array;} == 0) { } 0 0 0
elsif (@{$array;} == 1) { } 0 0 0
144
145 # return input value
146 0 return($in);
147
148 # if array size == 1 (gamma function)
149 0 } elsif (@{$array} == 1) {
150
151 # if gamma == 1
152 0 ▶ if ($array->[0] == 256) {
Branch: 1 branch
BranchTrueFalseTotal
if ($array->[0] == 256) { } 0 0 0
153
154 # return input value
155 0 return($in);
156
157 } else {
158
159 # return x^gamma
160 0 ▶ return($in > 0 ? $in**($array->[0]/256) : 0);
Branch: 1 branch
BranchTrueFalseTotal
$in > 0 ? : 0 0 0
161
162 }
163
164 } else {
165
166 # compute lower bound index
167 0 $ix = POSIX::floor($in * $upper);
168
169 # limit lower bound index
170 0 ▶ $ix = $ix < 0 ? 0 : ($ix > ($upper - 1) ? $upper - 1 : $ix);
Branch: 2 branches
BranchTrueFalseTotal
$ix < 0 ? : 0 0 0
$ix > $upper - 1 ? : 0 0 0
171
172 # compute interpolation ratio
173 0 $ir = $in * $upper - $ix;
174
175 # return value (linear interpolation)
176 0 return(((1 - $ir) * $array->[$ix] + $ir * $array->[$ix + 1]));
177
178 }
179
180 }
181  
182 # compute inverse curve function
183 # domain/range is (0 - 1)
184 # parameters: (input_value)
185 # returns: (output_value)
186 sub inverse {
187  
188 # get parameters
189 0 ▶ my ($self, $in) = @_;
Subroutine inverse: not called CC=5 SCAR=34.0
Pod undocumented
190  
191 # local variables
192 0 my ($array, $upper, $ix, $ir);
193  
194 # get array reference
195 0 $array = $self->[1];
196  
197 # get array upper subscript
198 0 $upper = $#{$array};
199  
200 # if array size == 0 (identity)
201 0 ▶ if (@{$array} == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$array;} == 0) { } 0 0 0
elsif (@{$array;} == 1) { } 0 0 0
202
203 # return input value
204 0 return($in);
205
206 # if array size = 1 (gamma function)
207 0 } elsif (@{$array} == 1) {
208
209 # if gamma = 1
210 0 ▶ if ($array->[0] == 256) {
Branch: 1 branch
BranchTrueFalseTotal
if ($array->[0] == 256) { } 0 0 0
211
212 # return input value
213 0 return($in);
214
215 } else {
216
217 # return y^(1/gamma)
218 0 ▶ return($in > 0 ? $in**(256/$array->[0]) : 0);
Branch: 1 branch
BranchTrueFalseTotal
$in > 0 ? : 0 0 0
219
220 }
221
222 } else {
223
224 # find array interval containing input value
225 0 $ix = _binsearch($array, $in);
226
227 # compute array interval ratio
228 0 $ir = ($in - $array->[$ix])/($array->[$ix + 1] - $array->[$ix]);
229
230 # return value
231 0 return(($ix + $ir)/$upper);
232
233 }
234
235 }
236  
237 # compute curve derivative
238 # domain is (0 - 1)
239 # parameters: (input_value)
240 # returns: (derivative_value)
241 sub derivative {
242  
243 # get parameters
244 0 ▶ my ($self, $in) = @_;
Subroutine derivative: not called CC=7 SCAR=40.3
Pod undocumented
245  
246 # local variables
247 0 my ($array, $upper, $ix, $ir);
248  
249 # get array reference
250 0 $array = $self->[1];
251  
252 # get array upper subscript
253 0 $upper = $#{$array};
254  
255 # if array size == 0 (identity)
256 0 ▶ if (@{$array} == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$array;} == 0) { } 0 0 0
elsif (@{$array;} == 1) { } 0 0 0
257
258 # return value
259 0 return(1);
260
261 # if array size == 1 (gamma curve)
262 0 } elsif (@{$array} == 1) {
263
264 # if gamma == 1
265 0 ▶ if ($array->[0] == 256) {
Branch: 1 branch
BranchTrueFalseTotal
if ($array->[0] == 256) { } 0 0 0
266
267 # return 1
268 0 return(1);
269
270 } else {
271
272 # return gamma * x^(gamma - 1)
273 0 ▶ return($in > 0 ? ($array->[0]/256) * $in**($array->[0]/256 - 1) : 0);
Branch: 1 branch
BranchTrueFalseTotal
$in > 0 ? : 0 0 0
274
275 }
276
277 } else {
278
279 # compute lower bound index
280 0 $ix = POSIX::floor($in * $upper);
281
282 # limit lower bound index
283 0 ▶ $ix = $ix < 0 ? 0 : ($ix > ($upper - 1) ? $upper - 1 : $ix);
Branch: 2 branches
BranchTrueFalseTotal
$ix < 0 ? : 0 0 0
$ix > $upper - 1 ? : 0 0 0
284
285 # return value
286 0 return(($array->[$ix + 1] - $array->[$ix]) * $upper);
287
288 }
289
290 }
291  
292 # create curv tag object from ICC profile
293 # parameters: (ref_to_parent_object, file_handle, ref_to_tag_table_entry)
294 # returns: (ref_to_object)
295 sub new_fh {
296  
297 # get object class
298 4 ▶ my $class = shift();
Subroutine new_fh: called CC=2 SCAR=7.0
Pod undocumented
299  
300 # create empty curv object
301 4 my $self = [
302 {}, # object header
303 [] # curve array
304 ];
305  
306 # verify 3 parameters
307 4 ▶ (@_ == 3) || croak('wrong number of parameters');
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or croak("wrong number of parameters") 0 4 4
308  
309 # read curv data from profile
310 4 _readICCcurv($self, @_);
311  
312 # bless object
313 4 bless($self, $class);
314  
315 # return object reference
316 4 return($self);
317  
318 }
319  
320 # writes curv tag object to ICC profile
321 # parameters: (ref_to_parent_object, file_handle, ref_to_tag_table_entry)
322 sub write_fh {
323  
324 # get tag reference
325 4 ▶ my $self = shift();
Subroutine write_fh: called CC=2 SCAR=7.1
Pod undocumented
326  
327 # verify 3 parameters
328 4 ▶ (@_ == 3) || croak('wrong number of parameters');
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or croak("wrong number of parameters") 0 4 4
329  
330 # write curv data to profile
331 4 _writeICCcurv($self, @_);
332  
333 }
334  
335 # get tag size (for writing to profile)
336 # returns: (tag_size)
337 sub size {
338  
339 # get parameters
340 12 ▶ my ($self) = @_;
Subroutine size: called CC=1 SCAR=0.0
Pod undocumented
341  
342 # return size
343 12 return(12 + @{$self->[1]} * 2);
344  
345 }
346  
347 # print object contents to string
348 # format is an array structure
349 # parameter: ([format])
350 # returns: (string)
351 sub sdump {
352  
353 # get parameters
354 0 ▶ my ($self, $p) = @_;
Subroutine sdump: not called CC=5 SCAR=34.0
Pod documented
355  
356 # local variables
357 0 my ($s, $fmt);
358  
359 # resolve parameter to an array reference
360 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
361  
362 # get format string
363 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
364  
365 # set string to object ID
366 0 $s = sprintf("'%s' object, (0x%x)\n", ref($self), $self);
367  
368 # return
369 0 return($s);
370  
371 }
372  
373 # directional derivative
374 # nominal domain (0 - 1)
375 # direction: 0 - normal, 1 - inverse
376 # parameters: (object_reference, direction, input_value)
377 # returns: (derivative_value)
378 sub _derivative {
379  
380 # get parameters
381 0 ▶ my ($self, $dir, $in) = @_;
Subroutine _derivative: not called CC=3 SCAR=24.8
382  
383 # if inverse transform
384 0 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) { } 0 0 0
385
386 # compute derivative
387 0 my $d = derivative($self, $in);
388
389 # if non-zero
390 0 ▶ if ($d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d) { } 0 0 0
391
392 # return inverse
393 0 return(1/$d);
394
395 } else {
396
397 # error
398 0 croak('infinite derivative');
399
400 }
401
402 } else {
403
404 # return derivative
405 0 return(derivative($self, $in));
406
407 }
408
409 }
410  
411 # directional transform
412 # nominal domain (0 - 1)
413 # direction: 0 - normal, 1 - inverse
414 # parameters: (object_reference, direction, input_value)
415 # returns: (output_value)
416 sub _transform {
417  
418 # get parameters
419 0 ▶ my ($self, $dir, $in) = @_;
Subroutine _transform: not called CC=2 SCAR=17.9
420  
421 # if inverse transform
422 0 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) { } 0 0 0
423
424 # return inverse
425 0 return(inverse($self, $in));
426
427 } else {
428
429 # return transform
430 0 return(transform($self, $in));
431
432 }
433
434 }
435  
436 # binary search
437 # finds array interval containing value
438 # assumes values are monotonic
439 # parameters: (ref_to_array, value)
440 # returns: (lower_index)
441 sub _binsearch {
442  
443 # get parameters
444 0 ▶ my ($xref, $v) = @_;
Subroutine _binsearch: not called CC=6 SCAR=37.4
445  
446 # local variables
447 0 my ($k, $klo, $khi);
448  
449 # set low and high indices
450 0 $klo = 0;
451 0 $khi = $#{$xref};
452  
453 # if values are increasing
454 0 ▶ if ($xref->[-1] > $xref->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xref->[-1] > $xref->[0]) { } 0 0 0
455
456 # repeat until interval is found
457 0 ▶ while (($khi - $klo) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($khi - $klo > 1) 0 0 0
458
459 # compute the midpoint
460 0 $k = int(($khi + $klo)/2);
461
462 # if midpoint value > value
463 0 ▶ if ($xref->[$k] > $v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xref->[$k] > $v) { } 0 0 0
464
465 # set high index to midpoint
466 0 $khi = $k;
467
468 } else {
469
470 # set low index to midpoint
471 0 $klo = $k;
472
473 }
474
475 }
476
477 # if values are decreasing
478 } else {
479
480 # repeat until interval is found
481 0 ▶ while (($khi - $klo) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($khi - $klo > 1) 0 0 0
482
483 # compute the midpoint
484 0 $k = int(($khi + $klo)/2);
485
486 # if midpoint value < value
487 0 ▶ if ($xref->[$k] < $v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xref->[$k] < $v) { } 0 0 0
488
489 # set high index to midpoint
490 0 $khi = $k;
491
492 } else {
493
494 # set low index to midpoint
495 0 $klo = $k;
496
497 }
498
499 }
500
501 }
502  
503 # return low index
504 0 return ($klo);
505  
506 }
507  
508 # read curv tag from ICC profile
509 # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
510 sub _readICCcurv {
511  
512 # get parameters
513 4 ▶ my ($self, $parent, $fh, $tag) = @_;
Subroutine _readICCcurv: called CC=3 SCAR=11.2
514  
515 # local variables
516 4 my ($buf, $cnt);
517  
518 # save tag signature
519 4 $self->[0]{'signature'} = $tag->[0];
520  
521 # seek start of tag
522 4 seek($fh, $tag->[1], 0);
523  
524 # read tag type signature and count
525 4 read($fh, $buf, 12);
526  
527 # unpack count
528 4 $cnt = unpack('x8 N', $buf);
529  
530 # if count == 1 (gamma)
531 4 ▶ if ($cnt == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cnt == 1) { } 0 4 4
elsif ($cnt > 1) { } 1 3 4
532
533 # read gamma
534 0 read($fh, $buf, 2);
535
536 # unpack gamma
537 0 $self->[1] = [unpack('n', $buf)];
538
539 # if count > 1
540 } elsif ($cnt > 1) {
541
542 # read array values
543 1 read($fh, $buf, 2 * $cnt);
544
545 # unpack array values
546 1 $self->[1] = [map {$_/65535} unpack('n*', $buf)];
547
548 }
549
550 }
551  
552 # write curv tag to ICC profile
553 # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
554 sub _writeICCcurv {
555  
556 # get parameters
557 4 ▶ my ($self, $parent, $fh, $tag) = @_;
Subroutine _writeICCcurv: called CC=5 SCAR=16.6
558  
559 # seek start of tag
560 4 seek($fh, $tag->[1], 0);
561  
562 # write tag type signature and count
563 4 print $fh pack('a4 x4 N', 'curv', scalar(@{$self->[1]}));
564  
565 # if count == 1 (gamma)
566 4 ▶ if (@{$self->[1]} == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$self->[1];} == 1) { } 0 4 4
elsif (@{$self->[1];} > 1) { } 1 3 4
567
568 # write gamma
569 0 print $fh pack('n*', $self->[1][0]);
570
571 # if count > 1
572 4 } elsif (@{$self->[1]} > 1){
573
574 # write array, limiting values and adding 0.5 to round
575 1 ▶ print $fh pack('n*', map {$_ < 0 ? 0 : ($_ > 1 ? 65535 : $_ * 65535 + 0.5)} @{$self->[1]});
Branch: 2 branches
BranchTrueFalseTotal
$_ < 0 ? : 0 700 700
$_ > 1 ? : 0 700 700
576
577 }
578
579 }
580  
581 7 1;
« blib/lib/ICC/Support/ratfunc.pm ↑ Summary blib/lib/ICC/Support/nNET2.pm »