blib/lib/ICC/Profile/curv.pm

filter: Statementstmt 38.0% 59 / 155 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.3% 86 / 274 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 2018-08-07
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') or 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) or 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') or 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) or 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 # verify 4 parameters
325 4 ▶ (@_ == 4) or croak('wrong number of parameters');
Subroutine write_fh: called CC=2 SCAR=7.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ == 4 or croak("wrong number of parameters") 0 4 4
326  
327 # write curv data to profile
328 4 goto &_writeICCcurv;
329  
330 }
331  
332 # get tag size (for writing to profile)
333 # returns: (tag_size)
334 sub size {
335  
336 # get parameters
337 12 ▶ my ($self) = @_;
Subroutine size: called CC=1 SCAR=0.0
Pod undocumented
338  
339 # return size
340 12 return(12 + @{$self->[1]} * 2);
341  
342 }
343  
344 # print object contents to string
345 # format is an array structure
346 # parameter: ([format])
347 # returns: (string)
348 sub sdump {
349  
350 # get parameters
351 0 ▶ my ($self, $p) = @_;
Subroutine sdump: not called CC=5 SCAR=34.0
Pod documented
352  
353 # local variables
354 0 my ($s, $fmt);
355  
356 # resolve parameter to an array reference
357 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
358  
359 # get format string
360 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
361  
362 # set string to object ID
363 0 $s = sprintf("'%s' object, (0x%x)\n", ref($self), $self);
364  
365 # return
366 0 return($s);
367  
368 }
369  
370 # directional derivative
371 # nominal domain (0 - 1)
372 # direction: 0 - normal, 1 - inverse
373 # parameters: (object_reference, direction, input_value)
374 # returns: (derivative_value)
375 sub _derivative {
376  
377 # get parameters
378 0 ▶ my ($self, $dir, $in) = @_;
Subroutine _derivative: not called CC=3 SCAR=24.8
379  
380 # if inverse transform
381 0 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) { } 0 0 0
382
383 # compute derivative
384 0 my $d = derivative($self, $in);
385
386 # if non-zero
387 0 ▶ if ($d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d) { } 0 0 0
388
389 # return inverse
390 0 return(1/$d);
391
392 } else {
393
394 # error
395 0 croak('infinite derivative');
396
397 }
398
399 } else {
400
401 # return derivative
402 0 return(derivative($self, $in));
403
404 }
405
406 }
407  
408 # directional transform
409 # nominal domain (0 - 1)
410 # direction: 0 - normal, 1 - inverse
411 # parameters: (object_reference, direction, input_value)
412 # returns: (output_value)
413 sub _transform {
414  
415 # get parameters
416 0 ▶ my ($self, $dir, $in) = @_;
Subroutine _transform: not called CC=2 SCAR=17.9
417  
418 # if inverse transform
419 0 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) { } 0 0 0
420
421 # return inverse
422 0 return(inverse($self, $in));
423
424 } else {
425
426 # return transform
427 0 return(transform($self, $in));
428
429 }
430
431 }
432  
433 # binary search
434 # finds array interval containing value
435 # assumes values are monotonic
436 # parameters: (ref_to_array, value)
437 # returns: (lower_index)
438 sub _binsearch {
439  
440 # get parameters
441 0 ▶ my ($xref, $v) = @_;
Subroutine _binsearch: not called CC=6 SCAR=37.4
442  
443 # local variables
444 0 my ($k, $klo, $khi);
445  
446 # set low and high indices
447 0 $klo = 0;
448 0 $khi = $#{$xref};
449  
450 # if values are increasing
451 0 ▶ if ($xref->[-1] > $xref->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xref->[-1] > $xref->[0]) { } 0 0 0
452
453 # repeat until interval is found
454 0 ▶ while (($khi - $klo) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($khi - $klo > 1) 0 0 0
455
456 # compute the midpoint
457 0 $k = int(($khi + $klo)/2);
458
459 # if midpoint value > value
460 0 ▶ if ($xref->[$k] > $v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xref->[$k] > $v) { } 0 0 0
461
462 # set high index to midpoint
463 0 $khi = $k;
464
465 } else {
466
467 # set low index to midpoint
468 0 $klo = $k;
469
470 }
471
472 }
473
474 # if values are decreasing
475 } else {
476
477 # repeat until interval is found
478 0 ▶ while (($khi - $klo) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($khi - $klo > 1) 0 0 0
479
480 # compute the midpoint
481 0 $k = int(($khi + $klo)/2);
482
483 # if midpoint value < value
484 0 ▶ if ($xref->[$k] < $v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xref->[$k] < $v) { } 0 0 0
485
486 # set high index to midpoint
487 0 $khi = $k;
488
489 } else {
490
491 # set low index to midpoint
492 0 $klo = $k;
493
494 }
495
496 }
497
498 }
499  
500 # return low index
501 0 return ($klo);
502  
503 }
504  
505 # read curv tag from ICC profile
506 # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
507 sub _readICCcurv {
508  
509 # get parameters
510 4 ▶ my ($self, $parent, $fh, $tag) = @_;
Subroutine _readICCcurv: called CC=3 SCAR=11.2
511  
512 # local variables
513 4 my ($buf, $cnt);
514  
515 # save tag signature
516 4 $self->[0]{'signature'} = $tag->[0];
517  
518 # seek start of tag
519 4 seek($fh, $tag->[1], 0);
520  
521 # read tag type signature and count
522 4 read($fh, $buf, 12);
523  
524 # unpack count
525 4 $cnt = unpack('x8 N', $buf);
526  
527 # if count == 1 (gamma)
528 4 ▶ if ($cnt == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cnt == 1) { } 0 4 4
elsif ($cnt > 1) { } 1 3 4
529
530 # read gamma
531 0 read($fh, $buf, 2);
532
533 # unpack gamma
534 0 $self->[1] = [unpack('n', $buf)];
535
536 # if count > 1
537 } elsif ($cnt > 1) {
538
539 # read array values
540 1 read($fh, $buf, 2 * $cnt);
541
542 # unpack array values
543 1 $self->[1] = [map {$_/65535} unpack('n*', $buf)];
544
545 }
546
547 }
548  
549 # write curv tag to ICC profile
550 # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
551 sub _writeICCcurv {
552  
553 # get parameters
554 4 ▶ my ($self, $parent, $fh, $tag) = @_;
Subroutine _writeICCcurv: called CC=5 SCAR=16.6
555  
556 # seek start of tag
557 4 seek($fh, $tag->[1], 0);
558  
559 # write tag type signature and count
560 4 print $fh pack('a4 x4 N', 'curv', scalar(@{$self->[1]}));
561  
562 # if count == 1 (gamma)
563 4 ▶ if (@{$self->[1]} == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$self->[1];} == 1) { } 0 4 4
elsif (@{$self->[1];} > 1) { } 1 3 4
564
565 # write gamma
566 0 print $fh pack('n*', $self->[1][0]);
567
568 # if count > 1
569 4 } elsif (@{$self->[1]} > 1){
570
571 # write array, limiting values and adding 0.5 to round
572 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
573
574 }
575
576 }
577  
578 7 1;
« blib/lib/ICC/Support/ratfunc.pm ↑ Summary blib/lib/ICC/Support/nNET2.pm »