| 1 | package ICC::Support::akima; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Carp; | ||
| 5 | |||
| 6 | our $VERSION = 0.15; | ||
| 7 | |||
| 8 | # revised 2017-03-11 | ||
| 9 | # | ||
| 10 | # Copyright © 2004-2018 by William B. Birkett | ||
| 11 | |||
| 12 | # add development directory | ||
| 13 | use lib 'lib'; | ||
| 14 | |||
| 15 | # inherit from Shared | ||
| 16 | use parent qw(ICC::Shared); | ||
| 17 | |||
| 18 | # enable static variables | ||
| 19 | use feature 'state'; | ||
| 20 | |||
| 21 | # create new akima object | ||
| 22 | # arrays are sorted so that input values are increasing | ||
| 23 | # array structure: [[input_values_array], [output_values_array]] | ||
| 24 | # flag enables setting endpoint derivatives with least-squares fit | ||
| 25 | # parameters: ([ref_to_array, [endpoint_flag]]) | ||
| 26 | # returns: (ref_to_object) | ||
| 27 | sub new { | ||
| 28 | |||
| 29 | # get object class | ||
| 30 | my $class = shift(); | ||
| 31 | |||
| 32 | # create empty akima object | ||
| 33 | my $self = [ | ||
| 34 | {}, # object header | ||
| 35 | [], # x-values | ||
| 36 | [], # y-values | ||
| 37 | [] # derivatives | ||
| 38 | ]; | ||
| 39 | |||
| 40 | # if one or two parameters supplied | ||
| 41 | if (@_ == 1 || @_ == 2) { | ||
| 42 | |||
| 43 | # verify parameters are array references | ||
| 44 | ((ref($_[0]) eq 'ARRAY' || UNIVERSAL::isa($_[0], 'Math::Matrix')) && ref($_[0][0]) eq 'ARRAY' && ref($_[0][1]) eq 'ARRAY') || croak('parameter(s) not an array reference'); | ||
| 45 | |||
| 46 | # verify arrays are same length | ||
| 47 | ($#{$_[0][0]} == $#{$_[0][1]}) || croak('arrays have different lengths'); | ||
| 48 | |||
| 49 | # verify arrays contain two or more points | ||
| 50 | ($#{$_[0][0]} > 0) || croak('arrays must contain two or more points'); | ||
| 51 | |||
| 52 | # make index array | ||
| 53 | my @ix = (0 .. $#{$_[0][0]}); | ||
| 54 | |||
| 55 | # sort index by x-values, in ascending order | ||
| 56 | @ix = sort {$_[0][0][$a] <=> $_[0][0][$b]} @ix; | ||
| 57 | |||
| 58 | # store x-values | ||
| 59 | $self->[1] = [@{$_[0][0]}[@ix]]; | ||
| 60 | |||
| 61 | # store y-values | ||
| 62 | $self->[2] = [@{$_[0][1]}[@ix]]; | ||
| 63 | |||
| 64 | # compute object derivatives | ||
| 65 | _objderv($self, $_[1]); | ||
| 66 | |||
| 67 | # add local min/max values | ||
| 68 | _minmax($self); | ||
| 69 | |||
| 70 | # compute range of y-values | ||
| 71 | _range($self); | ||
| 72 | |||
| 73 | } elsif (@_) { | ||
| 74 | |||
| 75 | # error | ||
| 76 | croak('too many parameters'); | ||
| 77 | |||
| 78 | } | ||
| 79 | |||
| 80 | # bless object | ||
| 81 | bless($self, $class); | ||
| 82 | |||
| 83 | # return object reference | ||
| 84 | return($self); | ||
| 85 | |||
| 86 | } | ||
| 87 | |||
| 88 | # write akima object to ICC profile | ||
| 89 | # note: writes an equivalent curv object | ||
| 90 | # parameters: (ref_to_parent_object, file_handle, ref_to_tag_table_entry) | ||
| 91 | sub write_fh { | ||
| 92 | |||
| 93 | # get tag reference | ||
| 94 | my $self = shift(); | ||
| 95 | |||
| 96 | # verify 3 parameters | ||
| 97 | (@_ == 3) || croak('wrong number of parameters'); | ||
| 98 | |||
| 99 | # write akima data to profile | ||
| 100 | _writeICCakima($self, @_); | ||
| 101 | |||
| 102 | } | ||
| 103 | |||
| 104 | # get tag size (for writing to profile) | ||
| 105 | # note: writes an equivalent curv object | ||
| 106 | # returns: (tag_size) | ||
| 107 | sub size { | ||
| 108 | |||
| 109 | # get parameters | ||
| 110 | my ($self) = @_; | ||
| 111 | |||
| 112 | # get count value (defaults to 4370) | ||
| 113 | my $n = $self->[0]{'curv_points'} || 4370; | ||
| 114 | |||
| 115 | # return size | ||
| 116 | return(12 + $n * 2); | ||
| 117 | |||
| 118 | } | ||
| 119 | |||
| 120 | # compute curve function | ||
| 121 | # parameters: (input_value) | ||
| 122 | # returns: (output_value) | ||
| 123 | sub transform { | ||
| 124 | |||
| 125 | # get parameters | ||
| 126 | my ($self, $in) = @_; | ||
| 127 | |||
| 128 | # local variable | ||
| 129 | my ($low); | ||
| 130 | |||
| 131 | # if input value <= x-min | ||
| 132 | if ($in <= $self->[1][0]) { | ||
| 133 | |||
| 134 | # return extrapolated value | ||
| 135 | return($self->[2][0] + $self->[3][0] * ($in - $self->[1][0])); | ||
| 136 | |||
| 137 | # if input value >= x-max | ||
| 138 | } elsif ($in >= $self->[1][-1]) { | ||
| 139 | |||
| 140 | # return extrapolated value | ||
| 141 | return($self->[2][-1] + $self->[3][-1] * ($in - $self->[1][-1])); | ||
| 142 | |||
| 143 | } else { | ||
| 144 | |||
| 145 | # if x-value not within current interval | ||
| 146 | if (! defined($low = $self->[0]{'low'}) || ($in < $self->[1][$low]) || ($in > $self->[1][$low + 1])) { | ||
| 147 | |||
| 148 | # locate interval with binary search | ||
| 149 | $self->[0]{'low'} = _binsearch($self->[1], $in); | ||
| 150 | |||
| 151 | } | ||
| 152 | |||
| 153 | # return interpolated value | ||
| 154 | return(_fwd($self, $in)); | ||
| 155 | |||
| 156 | } | ||
| 157 | |||
| 158 | } | ||
| 159 | |||
| 160 | # compute inverse curve function | ||
| 161 | # note: there may be multiple solutions | ||
| 162 | # parameters: (input_value) | ||
| 163 | # returns: (output_value -or- array_of_output_values) | ||
| 164 | sub inverse { | ||
| 165 | |||
| 166 | # get parameters | ||
| 167 | my ($self, $in) = @_; | ||
| 168 | |||
| 169 | # local variables | ||
| 170 | my (@sol, @sir, @low); | ||
| 171 | |||
| 172 | # if an extrapolated solution with x < x-min | ||
| 173 | if (($in < $self->[2][0] && $self->[3][0] > 0) || ($in > $self->[2][0] && $self->[3][0] < 0)) { | ||
| 174 | |||
| 175 | # add solution | ||
| 176 | push(@sol, $self->[1][0] - ($self->[2][0] - $in)/$self->[3][0]); | ||
| 177 | |||
| 178 | } | ||
| 179 | |||
| 180 | # locate solution interval(s) with x-min <= x <= x-max | ||
| 181 | @low = _linsearch($self->[2], $in); | ||
| 182 | |||
| 183 | # for each interval | ||
| 184 | for my $i (@low) { | ||
| 185 | |||
| 186 | # set interval | ||
| 187 | $self->[0]{'low'} = $i; | ||
| 188 | |||
| 189 | # add solution | ||
| 190 | push(@sol, _rev($self, $in)); | ||
| 191 | |||
| 192 | # add solution in range | ||
| 193 | push(@sir, $sol[-1]); | ||
| 194 | |||
| 195 | } | ||
| 196 | |||
| 197 | # if an extrapolated solution with x > x-max | ||
| 198 | if (($in > $self->[2][-1] && $self->[3][-1] > 0) || ($in < $self->[2][-1] && $self->[3][-1] < 0)) { | ||
| 199 | |||
| 200 | # add solution | ||
| 201 | push(@sol, $self->[1][-1] - ($self->[2][-1] - $in)/$self->[3][-1]); | ||
| 202 | |||
| 203 | } | ||
| 204 | |||
| 205 | # return result (array or first solution in range, if possible) | ||
| 206 | return(wantarray ? @sol : defined($sir[0]) ? $sir[0] : $sol[0]); | ||
| 207 | |||
| 208 | } | ||
| 209 | |||
| 210 | # compute curve derivative | ||
| 211 | # parameters: (input_value) | ||
| 212 | # returns: (derivative_value) | ||
| 213 | sub derivative { | ||
| 214 | |||
| 215 | # get parameters | ||
| 216 | my ($self, $in) = @_; | ||
| 217 | |||
| 218 | # local variable | ||
| 219 | my ($low); | ||
| 220 | |||
| 221 | # if input value <= x-min | ||
| 222 | if ($in <= $self->[1][0]) { | ||
| 223 | |||
| 224 | # return endpoint derivative | ||
| 225 | return($self->[3][0]); | ||
| 226 | |||
| 227 | # if input value >= x-max | ||
| 228 | } elsif ($in >= $self->[1][-1]) { | ||
| 229 | |||
| 230 | # return endpoint derivative | ||
| 231 | return($self->[3][-1]); | ||
| 232 | |||
| 233 | } else { | ||
| 234 | |||
| 235 | # if x-value not within current interval | ||
| 236 | if (! defined($low = $self->[0]{'low'}) || ($in < $self->[1][$low]) || ($in > $self->[1][$low + 1])) { | ||
| 237 | |||
| 238 | # locate interval with binary search | ||
| 239 | $self->[0]{'low'} = _binsearch($self->[1], $in); | ||
| 240 | |||
| 241 | } | ||
| 242 | |||
| 243 | # return derivative | ||
| 244 | return(_derv($self, $in)); | ||
| 245 | |||
| 246 | } | ||
| 247 | |||
| 248 | } | ||
| 249 | |||
| 250 | # get/set reference to header hash | ||
| 251 | # parameters: ([ref_to_new_hash]) | ||
| 252 | # returns: (ref_to_hash) | ||
| 253 | sub header { | ||
| 254 | |||
| 255 | # get object reference | ||
| 256 | my $self = shift(); | ||
| 257 | |||
| 258 | # if there are parameters | ||
| 259 | if (@_) { | ||
| 260 | |||
| 261 | # if one parameter, a hash reference | ||
| 262 | if (@_ == 1 && ref($_[0]) eq 'HASH') { | ||
| 263 | |||
| 264 | # set header to new hash | ||
| 265 | $self->[0] = shift(); | ||
| 266 | |||
| 267 | } else { | ||
| 268 | |||
| 269 | # error | ||
| 270 | croak('parameter must be a hash reference'); | ||
| 271 | |||
| 272 | } | ||
| 273 | |||
| 274 | } | ||
| 275 | |||
| 276 | # return reference | ||
| 277 | return($self->[0]); | ||
| 278 | |||
| 279 | } | ||
| 280 | |||
| 281 | # get/set array reference | ||
| 282 | # array contains x-values, y-values and derivatives | ||
| 283 | # parameters: ([ref_to_array]) | ||
| 284 | # returns: (ref_to_array) | ||
| 285 | sub array { | ||
| 286 | |||
| 287 | # get object reference | ||
| 288 | my $self = shift(); | ||
| 289 | |||
| 290 | # if one parameter supplied | ||
| 291 | if (@_ == 1) { | ||
| 292 | |||
| 293 | # verify parameter is an array reference | ||
| 294 | (ref($_[0]) eq 'ARRAY') || croak('parameter not an array reference'); | ||
| 295 | |||
| 296 | # verify array contents | ||
| 297 | ((3 == @{$_[0]}) && (3 == grep {ref() eq 'ARRAY'} @{$_[0]})) || croak('invalid array contents'); | ||
| 298 | |||
| 299 | # store x-values | ||
| 300 | $self->[1] = [@{$_[0][0]}]; | ||
| 301 | |||
| 302 | # store y-values | ||
| 303 | $self->[2] = [@{$_[0][1]}]; | ||
| 304 | |||
| 305 | # store derivatives | ||
| 306 | $self->[3] = [@{$_[0][2]}]; | ||
| 307 | |||
| 308 | } elsif (@_) { | ||
| 309 | |||
| 310 | # error | ||
| 311 | croak('too many parameters'); | ||
| 312 | |||
| 313 | } | ||
| 314 | |||
| 315 | # return array reference | ||
| 316 | return([[@{$self->[1]}], [@{$self->[2]}], [@{$self->[3]}]]); | ||
| 317 | |||
| 318 | } | ||
| 319 | |||
| 320 | # check if monotonic | ||
| 321 | # computes number of min/max points (0 if monotonic) | ||
| 322 | # parameters: () | ||
| 323 | # returns: (number) | ||
| 324 | sub monotonic { | ||
| 325 | |||
| 326 | # get object reference | ||
| 327 | my $self = shift(); | ||
| 328 | |||
| 329 | # local variables | ||
| 330 | my ($flag); | ||
| 331 | |||
| 332 | # init flag | ||
| 333 | $flag = 0; | ||
| 334 | |||
| 335 | # for each interval | ||
| 336 | for my $i (0 .. ($#{$self->[1]} - 1)) { | ||
| 337 | |||
| 338 | # set interval | ||
| 339 | $self->[0]{'low'} = $i; | ||
| 340 | |||
| 341 | # increment flag if a min/max point | ||
| 342 | $flag++ if ($self->[3][$i] == 0 && _derv2($self, 0) != 0); | ||
| 343 | |||
| 344 | } | ||
| 345 | |||
| 346 | # increment flag if a min/max point (last knot) | ||
| 347 | $flag++ if ($self->[3][-1] == 0 && _derv2($self, 1) != 0); | ||
| 348 | |||
| 349 | # return | ||
| 350 | return($flag); | ||
| 351 | |||
| 352 | } | ||
| 353 | |||
| 354 | # make table for 'curv' objects | ||
| 355 | # assumes curve domain/range is (0 - 1) | ||
| 356 | # parameters: (number_of_table_entries, [direction]) | ||
| 357 | # returns: (ref_to_table_array) | ||
| 358 | sub table { | ||
| 359 | |||
| 360 | # get parameters | ||
| 361 | my ($self, $n, $dir) = @_; | ||
| 362 | |||
| 363 | # local variables | ||
| 364 | my ($up, $table); | ||
| 365 | |||
| 366 | # validate number of table entries | ||
| 367 | ($n == int($n) && $n >= 2) || carp('invalid number of table entries'); | ||
| 368 | |||
| 369 | # purify direction flag | ||
| 370 | $dir = ($dir) ? 1 : 0; | ||
| 371 | |||
| 372 | # array upper index | ||
| 373 | $up = $n - 1; | ||
| 374 | |||
| 375 | # for each table entry | ||
| 376 | for my $i (0 .. $up) { | ||
| 377 | |||
| 378 | # compute table value | ||
| 379 | $table->[$i] = _transform($self, $dir, $i/$up); | ||
| 380 | |||
| 381 | } | ||
| 382 | |||
| 383 | # return table reference | ||
| 384 | return($table); | ||
| 385 | |||
| 386 | } | ||
| 387 | |||
| 388 | # make 'curv' object | ||
| 389 | # assumes curve domain/range is (0 - 1) | ||
| 390 | # parameters: (number_of_table_entries, [direction]) | ||
| 391 | # returns: (ref_to_curv_object) | ||
| 392 | sub curv { | ||
| 393 | |||
| 394 | # return 'curv' object reference | ||
| 395 | return(ICC::Profile::curv->new(table(@_))); | ||
| 396 | |||
| 397 | } | ||
| 398 | |||
| 399 | # normalize transform | ||
| 400 | # adjusts object values linearly | ||
| 401 | # so that min/max values are 0/1 | ||
| 402 | # parameter: () - adjust both x-values and y-values | ||
| 403 | # parameter: (0) - adjust x-values only | ||
| 404 | # parameter: (1) - adjust y-values only | ||
| 405 | sub normalize { | ||
| 406 | |||
| 407 | # get object reference | ||
| 408 | my $self = shift(); | ||
| 409 | |||
| 410 | # local variables | ||
| 411 | my ($flag, $cx0, $cx1, $cy0, $cy1, $dsf); | ||
| 412 | |||
| 413 | # if no parameters | ||
| 414 | if (@_ == 0) { | ||
| 415 | |||
| 416 | # set flag (adjust x-values and y-values) | ||
| 417 | $flag = 3; | ||
| 418 | |||
| 419 | } elsif (@_ == 1) { | ||
| 420 | |||
| 421 | # if valid parameter | ||
| 422 | if ($_[0] == 0 || $_[0] == 1) { | ||
| 423 | |||
| 424 | # set flag | ||
| 425 | $flag = shift() + 1; | ||
| 426 | |||
| 427 | } else { | ||
| 428 | |||
| 429 | # error | ||
| 430 | croak('invalid parameter'); | ||
| 431 | |||
| 432 | } | ||
| 433 | |||
| 434 | } else { | ||
| 435 | |||
| 436 | # error | ||
| 437 | croak('too many parameters') | ||
| 438 | |||
| 439 | } | ||
| 440 | |||
| 441 | # initialize scale factors | ||
| 442 | $cx1 = $cy1 = 1; | ||
| 443 | |||
| 444 | # if x-values adjusted | ||
| 445 | if ($flag & 0x01) { | ||
| 446 | |||
| 447 | # compute scale factor | ||
| 448 | $cx1 = $self->[1][-1] - $self->[1][0]; | ||
| 449 | |||
| 450 | # compute offset | ||
| 451 | $cx0 = $self->[1][0]/$cx1; | ||
| 452 | |||
| 453 | # for each x-value | ||
| 454 | for my $i (0 .. $#{$self->[1]}) { | ||
| 455 | |||
| 456 | # normalize | ||
| 457 | $self->[1][$i] = $self->[1][$i]/$cx1 - $cx0; | ||
| 458 | |||
| 459 | } | ||
| 460 | |||
| 461 | } | ||
| 462 | |||
| 463 | # if y-values adjusted | ||
| 464 | if ($flag & 0x02) { | ||
| 465 | |||
| 466 | # compute scaling coefficient | ||
| 467 | $cy1 = abs($self->[2][-1] - $self->[2][0]); | ||
| 468 | |||
| 469 | # compute offset | ||
| 470 | $cy0 = ($self->[2][-1] < $self->[2][0] ? $self->[2][-1] : $self->[2][0])/$cy1; | ||
| 471 | |||
| 472 | # for each y-value | ||
| 473 | for my $i (0 .. $#{$self->[2]}) { | ||
| 474 | |||
| 475 | # normalize | ||
| 476 | $self->[2][$i] = $self->[2][$i]/$cy1 - $cy0; | ||
| 477 | |||
| 478 | } | ||
| 479 | |||
| 480 | # set range of y-values | ||
| 481 | $self->[0]{'ymin'} = $self->[0]{'ymin'}/$cy1 - $cy0; | ||
| 482 | $self->[0]{'ymax'} = $self->[0]{'ymax'}/$cy1 - $cy0; | ||
| 483 | |||
| 484 | } | ||
| 485 | |||
| 486 | # compute derivative scale factor | ||
| 487 | $dsf = $cx1/$cy1; | ||
| 488 | |||
| 489 | # for each derivative value | ||
| 490 | for my $i (0 .. $#{$self->[3]}) { | ||
| 491 | |||
| 492 | # normalize | ||
| 493 | $self->[3][$i] *= $dsf; | ||
| 494 | |||
| 495 | } | ||
| 496 | |||
| 497 | } | ||
| 498 | |||
| 499 | # print object contents to string | ||
| 500 | # format is an array structure | ||
| 501 | # parameter: ([format]) | ||
| 502 | # returns: (string) | ||
| 503 | sub sdump { | ||
| 504 | |||
| 505 | # get parameters | ||
| 506 | my ($self, $p) = @_; | ||
| 507 | |||
| 508 | # local variables | ||
| 509 | my ($s, $fmt); | ||
| 510 | |||
| 511 | # resolve parameter to an array reference | ||
| 512 | $p = defined($p) ? ref($p) eq 'ARRAY' ? $p : [$p] : []; | ||
| 513 | |||
| 514 | # get format string | ||
| 515 | $fmt = defined($p->[0]) && ! ref($p->[0]) ? $p->[0] : 'undef'; | ||
| 516 | |||
| 517 | # set string to object ID | ||
| 518 | $s = sprintf("'%s' object, (0x%x)\n", ref($self), $self); | ||
| 519 | |||
| 520 | # return | ||
| 521 | return($s); | ||
| 522 | |||
| 523 | } | ||
| 524 | |||
| 525 | # compute parametric partial derivatives | ||
| 526 | # direction: 0 - normal, 1 - inverse | ||
| 527 | # parameters: (object_reference, direction, input_value) | ||
| 528 | # returns: (partial_derivative_array) | ||
| 529 | sub _parametric { | ||
| 530 | |||
| 531 | # get parameters | ||
| 532 | my ($self, $dir, $in) = @_; | ||
| 533 | |||
| 534 | # unimplemented function error | ||
| 535 | croak('unimplemented function'); | ||
| 536 | |||
| 537 | } | ||
| 538 | |||
| 539 | # combined derivative | ||
| 540 | # direction: 0 - normal, 1 - inverse | ||
| 541 | # parameters: (object_reference, direction, input_value) | ||
| 542 | # returns: (derivative_value) | ||
| 543 | sub _derivative { | ||
| 544 | |||
| 545 | # get parameters | ||
| 546 | my ($self, $dir, $in) = @_; | ||
| 547 | |||
| 548 | # if inverse direction | ||
| 549 | if ($dir) { | ||
| 550 | |||
| 551 | # unimplemented function error | ||
| 552 | croak('unimplemented function'); | ||
| 553 | |||
| 554 | } else { | ||
| 555 | |||
| 556 | # return derivative | ||
| 557 | return($self->derivative($in)); | ||
| 558 | |||
| 559 | } | ||
| 560 | |||
| 561 | } | ||
| 562 | |||
| 563 | # combined transform | ||
| 564 | # direction: 0 - normal, 1 - inverse | ||
| 565 | # parameters: (object_reference, direction, input_value) | ||
| 566 | # returns: (output_value) | ||
| 567 | sub _transform { | ||
| 568 | |||
| 569 | # get parameters | ||
| 570 | my ($self, $dir, $in) = @_; | ||
| 571 | |||
| 572 | # if inverse direction | ||
| 573 | if ($dir) { | ||
| 574 | |||
| 575 | # return inverse | ||
| 576 | return(scalar($self->inverse($in))); | ||
| 577 | |||
| 578 | } else { | ||
| 579 | |||
| 580 | # return transform | ||
| 581 | return($self->transform($in)); | ||
| 582 | |||
| 583 | } | ||
| 584 | |||
| 585 | } | ||
| 586 | |||
| 587 | # compute object derivatives | ||
| 588 | # for spline knots using Akima's method | ||
| 589 | # parameters: (ref_to_object, endpoint_flag) | ||
| 590 | sub _objderv { | ||
| 591 | |||
| 592 | # get parameters | ||
| 593 | my ($self, $flag) = @_; | ||
| 594 | |||
| 595 | # local variables | ||
| 596 | my ($p, @m, $d, $d1, $d2, $dm); | ||
| 597 | |||
| 598 | # get array length | ||
| 599 | $p = $#{$self->[1]}; | ||
| 600 | |||
| 601 | # compute segment slopes | ||
| 602 | for my $i (0 .. ($p - 1)) { | ||
| 603 | |||
| 604 | # compute and test denominator | ||
| 605 | ($d = $self->[1][$i] - $self->[1][$i + 1]) || croak('zero x-value interval in spline data'); | ||
| 606 | |||
| 607 | # now, compute slope | ||
| 608 | $m[$i + 2] = ($self->[2][$i] - $self->[2][$i + 1])/$d; | ||
| 609 | |||
| 610 | } | ||
| 611 | |||
| 612 | # if 2 points | ||
| 613 | if ($p == 1) { | ||
| 614 | |||
| 615 | # linear slopes | ||
| 616 | $m[0] = $m[1] = $m[3] = $m[4] = $m[2]; | ||
| 617 | |||
| 618 | # if 3 or more points | ||
| 619 | } else { | ||
| 620 | |||
| 621 | # quadratic slopes | ||
| 622 | $m[1] = 2 * $m[2] - $m[3]; | ||
| 623 | $m[0] = 2 * $m[1] - $m[2]; | ||
| 624 | $m[$p + 2] = 2 * $m[$p + 1] - $m[$p]; | ||
| 625 | $m[$p + 3] = 2 * $m[$p + 2] - $m[$p + 1]; | ||
| 626 | |||
| 627 | } | ||
| 628 | |||
| 629 | # compute Akima derivative | ||
| 630 | for my $i (0 .. $p) { | ||
| 631 | |||
| 632 | # if denominator not 0 | ||
| 633 | if ($d = abs($m[$i + 3] - $m[$i + 2]) + abs($m[$i + 1] - $m[$i])) { | ||
| 634 | |||
| 635 | # use Akima estimate of slope | ||
| 636 | $self->[3][$i] = (abs($m[$i + 3] - $m[$i + 2]) * $m[$i + 1] + abs($m[$i + 1] - $m[$i]) * $m[$i + 2])/$d; | ||
| 637 | |||
| 638 | } else { | ||
| 639 | |||
| 640 | # otherwise, use average slope of adjoining segments | ||
| 641 | $self->[3][$i] = ($m[$i + 1] + $m[$i + 2])/2; | ||
| 642 | |||
| 643 | } | ||
| 644 | |||
| 645 | } | ||
| 646 | |||
| 647 | # if four or more points and flag set | ||
| 648 | if ($p >= 3 && $flag) { | ||
| 649 | |||
| 650 | # compute endpoint derivatives using weighted least squares | ||
| 651 | $self->[3][0] = _endpoint($self, 0); | ||
| 652 | $self->[3][-1] = _endpoint($self, -1); | ||
| 653 | |||
| 654 | } | ||
| 655 | |||
| 656 | } | ||
| 657 | |||
| 658 | # determine derivative of endpoint | ||
| 659 | # using weighted least squares method | ||
| 660 | # endpoint index is either 0 or -1 | ||
| 661 | # parameters: (ref_to_object, endpoint_index) | ||
| 662 | # returns: (derivative) | ||
| 663 | sub _endpoint { | ||
| 664 | |||
| 665 | # get parameters | ||
| 666 | my ($self, $ix) = @_; | ||
| 667 | |||
| 668 | # local variables | ||
| 669 | my ($x, $y, $v, $r, $w, $info, $c); | ||
| 670 | |||
| 671 | # get x and y references | ||
| 672 | $x = $self->[1]; | ||
| 673 | $y = $self->[2]; | ||
| 674 | |||
| 675 | # for each data point | ||
| 676 | for my $i (0 .. $#{$x}) { | ||
| 677 | |||
| 678 | # for each cubic coefficient | ||
| 679 | for my $j (0 .. 3) { | ||
| 680 | |||
| 681 | # add element to Vandermonde matrix | ||
| 682 | $v->[$i][$j] = $x->[$i]**$j; | ||
| 683 | |||
| 684 | } | ||
| 685 | |||
| 686 | # add element to residual matrix | ||
| 687 | $r->[$i][0] = $y->[$i]; | ||
| 688 | |||
| 689 | # add element to weight matrix | ||
| 690 | $w->[$i][0] = exp(-5 * abs(($x->[$i] - $x->[$ix])/($x->[0] - $x->[-1]))); | ||
| 691 | |||
| 692 | } | ||
| 693 | |||
| 694 | # solve normal equations | ||
| 695 | ($info, $c) = ICC::Support::Lapack::normal($v, $r, $w); | ||
| 696 | |||
| 697 | # return derivative | ||
| 698 | return($c->[1][0] + 2 * $c->[2][0] * $x->[$ix] + 3 * $c->[3][0] * $x->[$ix]**2); | ||
| 699 | |||
| 700 | } | ||
| 701 | |||
| 702 | # compute range of y-values | ||
| 703 | # parameters: (ref_to_object) | ||
| 704 | sub _range { | ||
| 705 | |||
| 706 | # get object reference | ||
| 707 | my $self = shift(); | ||
| 708 | |||
| 709 | # local variables | ||
| 710 | my ($min, $max); | ||
| 711 | |||
| 712 | # initialize min/max values | ||
| 713 | $min = $max = $self->[2][0]; | ||
| 714 | |||
| 715 | # for each y-value | ||
| 716 | for my $i (1 .. $#{$self->[2]}) { | ||
| 717 | |||
| 718 | # update min and max | ||
| 719 | $min = $self->[2][$i] if ($min > $self->[2][$i]); | ||
| 720 | $max = $self->[2][$i] if ($max < $self->[2][$i]); | ||
| 721 | |||
| 722 | } | ||
| 723 | |||
| 724 | # save result | ||
| 725 | $self->[0]{'ymin'} = $min; | ||
| 726 | $self->[0]{'ymax'} = $max; | ||
| 727 | |||
| 728 | } | ||
| 729 | |||
| 730 | # add local min/max values | ||
| 731 | # parameters: (ref_to_object) | ||
| 732 | sub _minmax { | ||
| 733 | |||
| 734 | # get object reference | ||
| 735 | my $self = shift(); | ||
| 736 | |||
| 737 | # local variables | ||
| 738 | my (@s, @ix); | ||
| 739 | |||
| 740 | # for each interval | ||
| 741 | for my $i (0 .. ($#{$self->[1]} - 1)) { | ||
| 742 | |||
| 743 | # if local min/max values | ||
| 744 | if (@s = _local($self, $i)) { | ||
| 745 | |||
| 746 | # set lower index | ||
| 747 | $self->[0]{'low'} = $i; | ||
| 748 | |||
| 749 | # add to x-value array | ||
| 750 | push(@{$self->[1]}, @s); | ||
| 751 | |||
| 752 | # add to y-value array | ||
| 753 | push(@{$self->[2]}, map {_fwd($self, $_)} @s); | ||
| 754 | |||
| 755 | # add to derivative array | ||
| 756 | push(@{$self->[3]}, (0) x @s); | ||
| 757 | |||
| 758 | } | ||
| 759 | |||
| 760 | } | ||
| 761 | |||
| 762 | # make index array | ||
| 763 | @ix = (0 .. $#{$self->[1]}); | ||
| 764 | |||
| 765 | # sort index by x-values, in ascending order | ||
| 766 | @ix = sort {$self->[1][$a] <=> $self->[1][$b]} @ix; | ||
| 767 | |||
| 768 | # reorder x-values | ||
| 769 | $self->[1] = [@{$self->[1]}[@ix]]; | ||
| 770 | |||
| 771 | # reorder y-values | ||
| 772 | $self->[2] = [@{$self->[2]}[@ix]]; | ||
| 773 | |||
| 774 | # reorder derivatives | ||
| 775 | $self->[3] = [@{$self->[3]}[@ix]]; | ||
| 776 | |||
| 777 | } | ||
| 778 | |||
| 779 | # compute local min/max value(s) | ||
| 780 | # note: includes inflection points | ||
| 781 | # parameters: (ref_to_object, lower_index) | ||
| 782 | # returns: (value_array) | ||
| 783 | sub _local { | ||
| 784 | |||
| 785 | # get parameters | ||
| 786 | my ($self, $i) = @_; | ||
| 787 | |||
| 788 | # local variables | ||
| 789 | my ($x1, $x2, $y1, $y2, $m1, $m2); | ||
| 790 | my ($dy, $dx, $t, $a, $b, $c, $d, @s); | ||
| 791 | |||
| 792 | # get endpoint values | ||
| 793 | $x1 = $self->[1][$i]; | ||
| 794 | $x2 = $self->[1][$i + 1]; | ||
| 795 | $y1 = $self->[2][$i]; | ||
| 796 | $y2 = $self->[2][$i + 1]; | ||
| 797 | $m1 = $self->[3][$i]; | ||
| 798 | $m2 = $self->[3][$i + 1]; | ||
| 799 | |||
| 800 | # compute intermediate values | ||
| 801 | ($dx = $x2 - $x1) || croak('zero interval in spline data'); | ||
| 802 | $dy = $y1 - $y2; | ||
| 803 | $a = 6 * $dy/$dx + 3 * ($m1 + $m2); | ||
| 804 | $b = -6 * $dy/$dx - 2 * $m2 - 4 * $m1; | ||
| 805 | $c = $m1; | ||
| 806 | |||
| 807 | # return if constant | ||
| 808 | return() if ($a == 0 && $b == 0); | ||
| 809 | |||
| 810 | # if linear equation | ||
| 811 | if ($a == 0) { | ||
| 812 | |||
| 813 | # compute solution | ||
| 814 | $t = -$c/$b; | ||
| 815 | |||
| 816 | # push if solution within interval (but not endpoints) | ||
| 817 | push(@s, $t * $dx + $x1) if ($t > 0 && $t < 1); | ||
| 818 | |||
| 819 | # if quadratic equation | ||
| 820 | } else { | ||
| 821 | |||
| 822 | # compute discriminant | ||
| 823 | $d = $b**2 - 4 * $a * $c; | ||
| 824 | |||
| 825 | # return if complex solutions | ||
| 826 | return() if ($d < 0); | ||
| 827 | |||
| 828 | # compute first solution | ||
| 829 | $t = (-$b + sqrt($d))/(2 * $a); | ||
| 830 | |||
| 831 | # push if solution within interval (but not endpoints) | ||
| 832 | push(@s, $t * $dx + $x1) if ($t > 0 && $t < 1); | ||
| 833 | |||
| 834 | # if discriminant > 0 (two real solutions) | ||
| 835 | if ($d > 0) { | ||
| 836 | |||
| 837 | # compute second solution | ||
| 838 | $t = (-$b - sqrt($d))/(2 * $a); | ||
| 839 | |||
| 840 | # push if solution within interval (but not endpoints) | ||
| 841 | push(@s, $t * $dx + $x1) if ($t > 0 && $t < 1); | ||
| 842 | |||
| 843 | } | ||
| 844 | |||
| 845 | } | ||
| 846 | |||
| 847 | # return solution(s) | ||
| 848 | return (@s); | ||
| 849 | |||
| 850 | } | ||
| 851 | |||
| 852 | # compute second derivative | ||
| 853 | # parameters: (ref_to_object, x-value) | ||
| 854 | # returns: (second_derivative) | ||
| 855 | sub _derv2 { | ||
| 856 | |||
| 857 | # get parameters | ||
| 858 | my ($self, $in) = @_; | ||
| 859 | |||
| 860 | # local variables | ||
| 861 | my ($i, $x1, $x2, $y1, $y2, $m1, $m2); | ||
| 862 | my ($dy, $dx, $t, $h1, $h2, $h3); | ||
| 863 | |||
| 864 | # get lower index | ||
| 865 | $i = $self->[0]{'low'}; | ||
| 866 | |||
| 867 | # get endpoint values | ||
| 868 | $x1 = $self->[1][$i]; | ||
| 869 | $x2 = $self->[1][$i + 1]; | ||
| 870 | $y1 = $self->[2][$i]; | ||
| 871 | $y2 = $self->[2][$i + 1]; | ||
| 872 | $m1 = $self->[3][$i]; | ||
| 873 | $m2 = $self->[3][$i + 1]; | ||
| 874 | |||
| 875 | # compute intermediate values | ||
| 876 | ($dx = $x2 - $x1) || croak('zero interval in spline data'); | ||
| 877 | $dy = $y1 - $y2; | ||
| 878 | $t = ($in - $x1)/$dx; | ||
| 879 | $h1 = 12 * $t - 6; | ||
| 880 | $h2 = 6 * $t - 2; | ||
| 881 | $h3 = 6 * $t - 4; | ||
| 882 | |||
| 883 | # return second derivative | ||
| 884 | return (($h1 * $dy/$dx + $h2 * $m2 + $h3 * $m1)/$dx); | ||
| 885 | |||
| 886 | } | ||
| 887 | |||
| 888 | # compute derivative | ||
| 889 | # parameters: (ref_to_object, x-value) | ||
| 890 | # returns: (derivative) | ||
| 891 | sub _derv { | ||
| 892 | |||
| 893 | # get parameters | ||
| 894 | my ($self, $in) = @_; | ||
| 895 | |||
| 896 | # local variables | ||
| 897 | my ($i, $x1, $x2, $y1, $y2, $m1, $m2); | ||
| 898 | my ($dy, $dx, $t, $h1, $h2, $h3); | ||
| 899 | |||
| 900 | # get lower index | ||
| 901 | $i = $self->[0]{'low'}; | ||
| 902 | |||
| 903 | # get endpoint values | ||
| 904 | $x1 = $self->[1][$i]; | ||
| 905 | $x2 = $self->[1][$i + 1]; | ||
| 906 | $y1 = $self->[2][$i]; | ||
| 907 | $y2 = $self->[2][$i + 1]; | ||
| 908 | $m1 = $self->[3][$i]; | ||
| 909 | $m2 = $self->[3][$i + 1]; | ||
| 910 | |||
| 911 | # compute intermediate values | ||
| 912 | ($dx = $x2 - $x1) || croak('zero interval in spline data'); | ||
| 913 | $dy = $y1 - $y2; | ||
| 914 | $t = ($in - $x1)/$dx; | ||
| 915 | $h1 = 6 * $t * ($t - 1); | ||
| 916 | $h2 = $t * (3 * $t - 2); | ||
| 917 | $h3 = $h2 - 2 * $t + 1; | ||
| 918 | |||
| 919 | # return derivative | ||
| 920 | return ($h1 * $dy/$dx + $h2 * $m2 + $h3 * $m1); | ||
| 921 | |||
| 922 | } | ||
| 923 | |||
| 924 | # compute transform value | ||
| 925 | # parameters: (ref_to_object, x-value) | ||
| 926 | # returns: (y-value) | ||
| 927 | sub _fwd { | ||
| 928 | |||
| 929 | # get parameters | ||
| 930 | my ($self, $in) = @_; | ||
| 931 | |||
| 932 | # local variables | ||
| 933 | my ($i, $x0, $x1, $y0, $y1, $m0, $m1); | ||
| 934 | my ($dx, $t, $tc, $h00, $h01, $h10, $h11); | ||
| 935 | |||
| 936 | # check if ICC::Support::Lapack module is loaded | ||
| 937 | state $lapack = defined($INC{'ICC/Support/Lapack.pm'}); | ||
| 938 | |||
| 939 | # get lower index | ||
| 940 | $i = $self->[0]{'low'}; | ||
| 941 | |||
| 942 | # get endpoint values | ||
| 943 | $x0 = $self->[1][$i]; | ||
| 944 | $x1 = $self->[1][$i + 1]; | ||
| 945 | $y0 = $self->[2][$i]; | ||
| 946 | $y1 = $self->[2][$i + 1]; | ||
| 947 | $m0 = $self->[3][$i]; | ||
| 948 | $m1 = $self->[3][$i + 1]; | ||
| 949 | |||
| 950 | # compute intermediate values | ||
| 951 | ($dx = $x1 - $x0) || croak('zero interval in spline data'); | ||
| 952 | $t = ($in - $x0)/$dx; | ||
| 953 | |||
| 954 | # if ratio is non-zero | ||
| 955 | if ($t) { | ||
| 956 | |||
| 957 | # if ICC::Support::Lapack module is loaded | ||
| 958 | if ($lapack) { | ||
| 959 | |||
| 960 | # return interpolated value | ||
| 961 | return(ICC::Support::Lapack::hermite($t, $y0, $y1, $m0 * $dx, $m1 * $dx)); | ||
| 962 | |||
| 963 | } else { | ||
| 964 | |||
| 965 | # compute Hermite coefficients | ||
| 966 | $tc = 1 - $t; | ||
| 967 | $h00 = (1 + 2 * $t) * $tc * $tc; | ||
| 968 | $h01 = 1 - $h00; | ||
| 969 | $h10 = $t * $tc * $tc; | ||
| 970 | $h11 = -$t * $t * $tc; | ||
| 971 | |||
| 972 | # return interpolated value | ||
| 973 | return($h00 * $x0 + $h01 * $x1 + $h10 * $m0 * $dx + $h11 * $m1 * $dx); | ||
| 974 | |||
| 975 | } | ||
| 976 | |||
| 977 | } else { | ||
| 978 | |||
| 979 | # use lower source value | ||
| 980 | return($y0); | ||
| 981 | |||
| 982 | } | ||
| 983 | |||
| 984 | } | ||
| 985 | |||
| 986 | # compute inverse value | ||
| 987 | # parameters: (ref_to_object, y-value) | ||
| 988 | # returns: (x-value) | ||
| 989 | sub _rev { | ||
| 990 | |||
| 991 | # get parameters | ||
| 992 | my ($self, $in) = @_; | ||
| 993 | |||
| 994 | # local variables | ||
| 995 | my ($i, $x, $y, $dydx, $dx); | ||
| 996 | |||
| 997 | # get lower index | ||
| 998 | $i = $self->[0]{'low'}; | ||
| 999 | |||
| 1000 | # if input is lower y-value | ||
| 1001 | if ($self->[2][$i] == $in) { | ||
| 1002 | |||
| 1003 | # return lower x-value | ||
| 1004 | return($self->[1][$i]); | ||
| 1005 | |||
| 1006 | } else { | ||
| 1007 | |||
| 1008 | # set initial x-value | ||
| 1009 | $x = ($self->[1][$i] + $self->[1][$i + 1])/2; | ||
| 1010 | |||
| 1011 | # loop | ||
| 1012 | for (0 .. 10) { | ||
| 1013 | |||
| 1014 | # get y-value | ||
| 1015 | $y = _fwd($self, $x); | ||
| 1016 | |||
| 1017 | # get derivative | ||
| 1018 | $dydx = _derv($self, $x); | ||
| 1019 | |||
| 1020 | # compute x-delta | ||
| 1021 | $dx = ($y - $in)/($dydx ? $dydx : rand); | ||
| 1022 | |||
| 1023 | # adjust x | ||
| 1024 | $x -= $dx; | ||
| 1025 | |||
| 1026 | # quit if error < limit | ||
| 1027 | last if (abs($dx) < 1E-12 * ($self->[1][-1] - $self->[1][0])); | ||
| 1028 | |||
| 1029 | } | ||
| 1030 | |||
| 1031 | } | ||
| 1032 | |||
| 1033 | # return | ||
| 1034 | return($x); | ||
| 1035 | |||
| 1036 | } | ||
| 1037 | |||
| 1038 | # binary search | ||
| 1039 | # finds the array interval containing the value | ||
| 1040 | # note: assumes array values are in ascending order | ||
| 1041 | # parameters: (ref_to_array, value) | ||
| 1042 | # returns: (lower_index) | ||
| 1043 | sub _binsearch { | ||
| 1044 | |||
| 1045 | # get parameters | ||
| 1046 | my ($vref, $v) = @_; | ||
| 1047 | |||
| 1048 | # local variables | ||
| 1049 | my ($k, $klo, $khi); | ||
| 1050 | |||
| 1051 | # set low and high indices | ||
| 1052 | $klo = 0; | ||
| 1053 | $khi = $#{$vref}; | ||
| 1054 | |||
| 1055 | # repeat until interval is found | ||
| 1056 | while (($khi - $klo) > 1) { | ||
| 1057 | |||
| 1058 | # compute the midpoint | ||
| 1059 | $k = int(($khi + $klo)/2); | ||
| 1060 | |||
| 1061 | # if midpoint value > value | ||
| 1062 | if ($vref->[$k] > $v) { | ||
| 1063 | |||
| 1064 | # set high index to midpoint | ||
| 1065 | $khi = $k; | ||
| 1066 | |||
| 1067 | } else { | ||
| 1068 | |||
| 1069 | # set low index to midpoint | ||
| 1070 | $klo = $k; | ||
| 1071 | |||
| 1072 | } | ||
| 1073 | |||
| 1074 | } | ||
| 1075 | |||
| 1076 | # return low index | ||
| 1077 | return ($klo); | ||
| 1078 | |||
| 1079 | } | ||
| 1080 | |||
| 1081 | # linear search | ||
| 1082 | # finds the array interval(s) containing the value | ||
| 1083 | # parameters: (ref_to_array, value) | ||
| 1084 | # returns: (lower_index_array) | ||
| 1085 | sub _linsearch { | ||
| 1086 | |||
| 1087 | # get parameters | ||
| 1088 | my ($vref, $v) = @_; | ||
| 1089 | |||
| 1090 | # local variables | ||
| 1091 | my (@low); | ||
| 1092 | |||
| 1093 | # for each point | ||
| 1094 | for my $i (0 .. $#{$vref}) { | ||
| 1095 | |||
| 1096 | # if value equals point | ||
| 1097 | if ($v == $vref->[$i]) { | ||
| 1098 | |||
| 1099 | # push index | ||
| 1100 | push(@low, $i); | ||
| 1101 | |||
| 1102 | # if value inside interval (not including end points) | ||
| 1103 | } elsif ($i < $#{$vref} && (($v > $vref->[$i]) ^ ($v > $vref->[$i + 1])) && (($v < $vref->[$i]) ^ ($v < $vref->[$i + 1]))) { | ||
| 1104 | |||
| 1105 | # push index | ||
| 1106 | push(@low, $i); | ||
| 1107 | |||
| 1108 | } | ||
| 1109 | |||
| 1110 | } | ||
| 1111 | |||
| 1112 | # return | ||
| 1113 | return(@low); | ||
| 1114 | |||
| 1115 | } | ||
| 1116 | |||
| 1117 | # write akima object to ICC profile | ||
| 1118 | # note: writes an equivalent curv object | ||
| 1119 | # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry) | ||
| 1120 | sub _writeICCakima { | ||
| 1121 | |||
| 1122 | # get parameters | ||
| 1123 | my ($self, $parent, $fh, $tag) = @_; | ||
| 1124 | |||
| 1125 | # local variables | ||
| 1126 | my ($n, $up, @table); | ||
| 1127 | |||
| 1128 | # get count value (defaults to 4096, the maximum allowed in an 'mft2' tag) | ||
| 1129 | $n = defined($self->[0]{'curv_points'}) ? $self->[0]{'curv_points'} : 4096; | ||
| 1130 | |||
| 1131 | # validate number of table entries | ||
| 1132 | ($n == int($n) && $n >= 2) || carp('invalid number of table entries'); | ||
| 1133 | |||
| 1134 | # array upper index | ||
| 1135 | $up = $n - 1; | ||
| 1136 | |||
| 1137 | # for each table entry | ||
| 1138 | for my $i (0 .. $up) { | ||
| 1139 | |||
| 1140 | # transform value | ||
| 1141 | $table[$i] = transform($self, $i/$up); | ||
| 1142 | |||
| 1143 | } | ||
| 1144 | |||
| 1145 | # seek start of tag | ||
| 1146 | seek($fh, $tag->[1], 0); | ||
| 1147 | |||
| 1148 | # write tag type signature and count | ||
| 1149 | print $fh pack('a4 x4 N', 'curv', $n); | ||
| 1150 | |||
| 1151 | # write array | ||
| 1152 | print $fh pack('n*', map {$_ < 0 ? 0 : ($_ > 1 ? 65535 : $_ * 65535 + 0.5)} @table); | ||
| 1153 | |||
| 1154 | } | ||
| 1155 | |||
| 1156 | 1; |