| 1 | package ICC::Support::Color; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Carp; | ||
| 5 | |||
| 6 | our $VERSION = 0.22; | ||
| 7 | |||
| 8 | # revised 2017-09-08 | ||
| 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 | # support modules | ||
| 19 | use File::Glob; | ||
| 20 | |||
| 21 | # enable static variables | ||
| 22 | use feature 'state'; | ||
| 23 | |||
| 24 | # spectral range hash | ||
| 25 | our $srh = {'31' => [400, 700, 10], '36' => [380, 730, 10], '38' => [360, 730, 10], '43' => [360, 780, 10], '81' => [380, 780, 5]}; | ||
| 26 | |||
| 27 | # create new Color object | ||
| 28 | # hash keys are: 'illuminant', 'observer', 'increment', 'method', 'imethod', 'bandpass', 'ibandpass', 'status', 'range' | ||
| 29 | # the method used is determined by the hash keys. | ||
| 30 | # for ASTM method, | ||
| 31 | # 'illuminant' value is a scalar, 'A', 'C', 'D50', 'D55', 'D65', 'D75', 'F2', 'F7', or 'F11', default is 'D50' | ||
| 32 | # 'observer' value is a scalar, '2' or '10', default is '2' | ||
| 33 | # 'increment' value is a scalar, '10' or '20', default is '10' | ||
| 34 | # 'bandpass' value is a scalar, 'astm', 'triangle', 'trapezoid' or 'six', default is no bandpass correction | ||
| 35 | # for CIE method, | ||
| 36 | # 'illuminant' value is an array reference, [], ['source', 'id'] or ['nm', 'spd'] | ||
| 37 | # an empty array indicates no illuminant, for emissive measurements | ||
| 38 | # 'source' values are 'CIE', 'Philips', or a measurement file path | ||
| 39 | # 'id' values depend on the 'source' | ||
| 40 | # 'CIE' illuminants are 'A', 'C', 'D50', 'D55', 'D65', 'D75', 'FL1' to 'FL12', 'FL3.1' to 'FL3.15', 'HP1' to 'HP5', and 'E' | ||
| 41 | # 'Philips' illuminants are '60_A/W', 'C100S54', 'C100S54C', 'F32T8/TL830', 'F32T8/TL835', 'F32T8/TL841', 'F32T8/TL850', 'F32T8/TL865/PLUS', 'F34/CW/RS/EW', 'F34T12WW/RS/EW', 'F40/C50', 'F40/C75', 'F40/CWX', 'F40/DX', 'F40/DXTP', 'F40/N', 'F34T12/LW/RS/EW', 'H38HT-100', 'H38JA-100/DX', 'MHC100/U/MP/3K', 'MHC100/U/MP/4K', and 'SDW-T_100W/LV' | ||
| 42 | # for a measurement file path, the 'id' is the sample number | ||
| 43 | # 'nm' and 'spd' are vectors, wavelength range and spectral power distribution | ||
| 44 | # 'observer' value is a scalar, '2', '10', '2P' or '10P', default '2' | ||
| 45 | # 'increment' value is a scalar, '1' or '5', default '1' | ||
| 46 | # 'method' value is a scalar, 'linear', 'cspline' or 'lagrange', default is 'cspline' | ||
| 47 | # 'bandpass' value is a scalar, 'astm', 'triangle' or 'trapezoid', default is no bandpass correction | ||
| 48 | # 'imethod' value is a scalar, 'linear', 'cspline' or 'lagrange', default is 'linear' or 'cspline', based on smoothness of the illuminant SPD | ||
| 49 | # 'ibandpass' value is a scalar, 'astm', 'triangle' or 'trapezoid', default is no bandpass correction | ||
| 50 | # for ISO 5-3 method (density), | ||
| 51 | # 'status' value is a scalar, 'A', 'M', 'T', 'E', or 'I', default 'T' | ||
| 52 | # 'increment' value is a scalar, '10' or '20', default '10' | ||
| 53 | # 'range' value is: [start_nm, end_nm, increment], which is added to the spectral range hash ($srh) | ||
| 54 | # parameters: ([ref_to_attribute_hash]) | ||
| 55 | # returns: (object_reference) | ||
| 56 | sub new { | ||
| 57 | |||
| 58 | # get object class | ||
| 59 | my $class = shift(); | ||
| 60 | |||
| 61 | # create empty Color object | ||
| 62 | my $self = [ | ||
| 63 | {}, # object header | ||
| 64 | [], # illuminant (CIE) | ||
| 65 | [], # color-matching functions (CIE) | ||
| 66 | [], # color-weight functions (ASTM and ISO 5-3) | ||
| 67 | [], # color-weight functions (adjusted to input range and cached) | ||
| 68 | [] # white-point | ||
| 69 | ]; | ||
| 70 | |||
| 71 | # if one parameter, a hash reference | ||
| 72 | if (@_ == 1 && ref($_[0]) eq 'HASH') { | ||
| 73 | |||
| 74 | # set object contents from hash | ||
| 75 | _new_from_hash($self, $_[0]); | ||
| 76 | |||
| 77 | } elsif (@_) { | ||
| 78 | |||
| 79 | # error | ||
| 80 | croak('invalid parameter(s)'); | ||
| 81 | |||
| 82 | } | ||
| 83 | |||
| 84 | # return blessed object | ||
| 85 | return(bless($self, $class)); | ||
| 86 | |||
| 87 | } | ||
| 88 | |||
| 89 | # get/set reference to header hash | ||
| 90 | # header contains keys used by 'new' | ||
| 91 | # parameters: ([ref_to_new_hash]) | ||
| 92 | # returns: (ref_to_hash) | ||
| 93 | sub header { | ||
| 94 | |||
| 95 | # get object reference | ||
| 96 | my $self = shift(); | ||
| 97 | |||
| 98 | # if there are parameters | ||
| 99 | if (@_) { | ||
| 100 | |||
| 101 | # if one parameter, a hash reference | ||
| 102 | if (@_ == 1 && ref($_[0]) eq 'HASH') { | ||
| 103 | |||
| 104 | # set header to copy of hash | ||
| 105 | $self->[0] = Storable::dclone(shift()); | ||
| 106 | |||
| 107 | } else { | ||
| 108 | |||
| 109 | # error | ||
| 110 | croak('parameter must be a hash reference'); | ||
| 111 | |||
| 112 | } | ||
| 113 | |||
| 114 | } | ||
| 115 | |||
| 116 | # return reference | ||
| 117 | return($self->[0]); | ||
| 118 | |||
| 119 | } | ||
| 120 | |||
| 121 | # get/set reference to illuminant structure | ||
| 122 | # structure: [[start_nm, end_nm, increment], spd_vector] | ||
| 123 | # note: set updates the color-weight functions | ||
| 124 | # parameters: ([ref_to_new_structure]) | ||
| 125 | # returns: (ref_to_structure) | ||
| 126 | sub illuminant { | ||
| 127 | |||
| 128 | # get object reference | ||
| 129 | my $self = shift(); | ||
| 130 | |||
| 131 | # local variables | ||
| 132 | my ($array, $sx); | ||
| 133 | |||
| 134 | # if there are parameters | ||
| 135 | if (@_) { | ||
| 136 | |||
| 137 | # if one parameter, an array reference | ||
| 138 | if (@_ == 1 && ref($_[0]) eq 'ARRAY') { | ||
| 139 | |||
| 140 | # get array reference | ||
| 141 | $array = shift(); | ||
| 142 | |||
| 143 | # initialize data array | ||
| 144 | $self->[1] = []; | ||
| 145 | |||
| 146 | # if array is not empty | ||
| 147 | if (@{$array}) { | ||
| 148 | |||
| 149 | # verify array size | ||
| 150 | (@{$array} == 2) or croak('invalid illuminant array'); | ||
| 151 | |||
| 152 | # verify wavelength range | ||
| 153 | (ref($array->[0]) eq 'ARRAY' && 3 == @{$array->[0]} && (3 == grep {Scalar::Util::looks_like_number($_)} @{$array->[0]}) && $array->[0][2]) or croak('invalid illuminant wavelength range'); | ||
| 154 | |||
| 155 | # compute upper index of spd array | ||
| 156 | (($sx = ICC::Shared::round(($array->[0][1] - $array->[0][0])/$array->[0][2])) > 0) or croak('inconsistent illuminant wavelength range'); | ||
| 157 | |||
| 158 | # verify spd array | ||
| 159 | (ref($array->[1]) eq 'ARRAY' && $#{$array->[1]} == $sx && @{$array->[1]} == grep {Scalar::Util::looks_like_number($_)} @{$array->[1]}) or croak('invalid illuminant spd array'); | ||
| 160 | |||
| 161 | # copy array contents | ||
| 162 | $self->[1] = Storable::dclone($array); | ||
| 163 | |||
| 164 | # if observer array defined | ||
| 165 | if (defined($self->[2][0])) { | ||
| 166 | |||
| 167 | # update color-weight functions | ||
| 168 | _make_cwf($self); | ||
| 169 | |||
| 170 | } | ||
| 171 | |||
| 172 | } | ||
| 173 | |||
| 174 | } else { | ||
| 175 | |||
| 176 | # error | ||
| 177 | croak('parameter must be an array reference'); | ||
| 178 | |||
| 179 | } | ||
| 180 | |||
| 181 | } | ||
| 182 | |||
| 183 | # return reference | ||
| 184 | return($self->[1]); | ||
| 185 | |||
| 186 | } | ||
| 187 | |||
| 188 | # get/set reference to observer structure | ||
| 189 | # structure: [[start_nm, end_nm, increment], cmf_matrix] | ||
| 190 | # note: set updates the color-weight functions | ||
| 191 | # parameters: ([ref_to_new_structure]) | ||
| 192 | # returns: (ref_to_structure) | ||
| 193 | sub observer { | ||
| 194 | |||
| 195 | # get object reference | ||
| 196 | my $self = shift(); | ||
| 197 | |||
| 198 | # local variables | ||
| 199 | my ($array, $sx); | ||
| 200 | |||
| 201 | # if there are parameters | ||
| 202 | if (@_) { | ||
| 203 | |||
| 204 | # if one parameter, an array reference | ||
| 205 | if (@_ == 1 && ref($_[0]) eq 'ARRAY') { | ||
| 206 | |||
| 207 | # get array reference | ||
| 208 | $array = shift(); | ||
| 209 | |||
| 210 | # initialize data array | ||
| 211 | $self->[2] = []; | ||
| 212 | |||
| 213 | # if array is not empty | ||
| 214 | if (@{$array}) { | ||
| 215 | |||
| 216 | # verify array size | ||
| 217 | (@{$array} == 2) or croak('invalid observer array'); | ||
| 218 | |||
| 219 | # verify wavelength range | ||
| 220 | (ref($array->[0]) eq 'ARRAY' && 3 == @{$array->[0]} && (3 == grep {Scalar::Util::looks_like_number($_)} @{$array->[0]}) && $array->[0][2]) or croak('invalid observer wavelength range'); | ||
| 221 | |||
| 222 | # compute upper index of observer matrix | ||
| 223 | (($sx = ICC::Shared::round(($array->[0][1] - $array->[0][0])/$array->[0][2])) > 0) or croak('inconsistent observer wavelength range'); | ||
| 224 | |||
| 225 | # verify observer matrix | ||
| 226 | ((ref($array->[1]) eq 'ARRAY' || UNIVERSAL::isa($array->[1], 'Math::Matrix')) && ref($array->[1][0]) eq 'ARRAY' && $#{$array->[1][0]} == $sx && @{$array->[1][0]} == grep {Scalar::Util::looks_like_number($_)} @{$array->[1][0]}) or croak('invalid observer cmf matrix'); | ||
| 227 | |||
| 228 | # copy array contents | ||
| 229 | $self->[2] = Storable::dclone($array); | ||
| 230 | |||
| 231 | # if illuminant array defined | ||
| 232 | if (defined($self->[1][0])) { | ||
| 233 | |||
| 234 | # update color-weight functions | ||
| 235 | _make_cwf($self); | ||
| 236 | |||
| 237 | } | ||
| 238 | |||
| 239 | } | ||
| 240 | |||
| 241 | } else { | ||
| 242 | |||
| 243 | # error | ||
| 244 | croak('parameter must be an array reference'); | ||
| 245 | |||
| 246 | } | ||
| 247 | |||
| 248 | } | ||
| 249 | |||
| 250 | # return reference | ||
| 251 | return($self->[2]); | ||
| 252 | |||
| 253 | } | ||
| 254 | |||
| 255 | # get/set reference to color-weight function structure | ||
| 256 | # structure: [[start_nm, end_nm, increment], cwf_matrix] | ||
| 257 | # parameters: ([ref_to_new_structure]) | ||
| 258 | # returns: (ref_to_structure) | ||
| 259 | sub cwf { | ||
| 260 | |||
| 261 | # get object reference | ||
| 262 | my $self = shift(); | ||
| 263 | |||
| 264 | # local variables | ||
| 265 | my ($array, $sx); | ||
| 266 | |||
| 267 | # if there are parameters | ||
| 268 | if (@_) { | ||
| 269 | |||
| 270 | # if one parameter, an array reference | ||
| 271 | if (@_ == 1 && ref($_[0]) eq 'ARRAY') { | ||
| 272 | |||
| 273 | # get array reference | ||
| 274 | $array = shift(); | ||
| 275 | |||
| 276 | # initialize data array | ||
| 277 | $self->[3] = []; | ||
| 278 | |||
| 279 | # if array is not empty | ||
| 280 | if (@{$array}) { | ||
| 281 | |||
| 282 | # verify array size | ||
| 283 | (@{$array} == 2) or croak('invalid cwf array'); | ||
| 284 | |||
| 285 | # verify wavelength range | ||
| 286 | (ref($array->[0]) eq 'ARRAY' && 3 == @{$array->[0]} && (3 == grep {Scalar::Util::looks_like_number($_)} @{$array->[0]}) && $array->[0][2]) or croak('invalid cwf wavelength range'); | ||
| 287 | |||
| 288 | # compute upper index of cwf matrix | ||
| 289 | (($sx = ICC::Shared::round(($array->[0][1] - $array->[0][0])/$array->[0][2])) > 0) or croak('inconsistent cwf wavelength range'); | ||
| 290 | |||
| 291 | # verify cwf matrix | ||
| 292 | ((ref($array->[1]) eq 'ARRAY' || UNIVERSAL::isa($array->[1], 'Math::Matrix')) && ref($array->[1][0]) eq 'ARRAY' && $#{$array->[1][0]} == $sx && @{$array->[1][0]} == grep {Scalar::Util::looks_like_number($_)} @{$array->[1][0]}) or croak('invalid cwf weight matrix'); | ||
| 293 | |||
| 294 | # copy array contents | ||
| 295 | $self->[3] = Storable::dclone($array); | ||
| 296 | |||
| 297 | # for each weight function (XYZ -or- RGBV) | ||
| 298 | for my $i (0 .. $#{$array->[1]}) { | ||
| 299 | |||
| 300 | # update white point array | ||
| 301 | $self->[5][$i] = List::Util::sum(@{$array->[1][$i]}); | ||
| 302 | |||
| 303 | } | ||
| 304 | |||
| 305 | } | ||
| 306 | |||
| 307 | } else { | ||
| 308 | |||
| 309 | # error | ||
| 310 | croak('parameter must be an array reference'); | ||
| 311 | |||
| 312 | } | ||
| 313 | |||
| 314 | } | ||
| 315 | |||
| 316 | # return reference | ||
| 317 | return($self->[3]); | ||
| 318 | |||
| 319 | } | ||
| 320 | |||
| 321 | # get illuminant white point | ||
| 322 | # encoding specified by 'encoding' hash key | ||
| 323 | # 'encoding' values are 'XYZ', 'xyz', 'ICC_XYZ', 'ICC_XYZNumber', and 'density' | ||
| 324 | # parameters: ([hash]) | ||
| 325 | # returns: (XYZ_vector) | ||
| 326 | sub iwtpt { | ||
| 327 | |||
| 328 | # get parameters | ||
| 329 | my ($self, $hash) = @_; | ||
| 330 | |||
| 331 | # local variable | ||
| 332 | my ($code); | ||
| 333 | |||
| 334 | # return white point with optional encoding | ||
| 335 | return(defined($code = _encoding($self, $hash)) ? [&$code(@{$self->[5]})] : $self->[5]); | ||
| 336 | |||
| 337 | } | ||
| 338 | |||
| 339 | # transform data | ||
| 340 | # hash keys are: 'range', and 'encoding' | ||
| 341 | # 'range' value is: [start_nm, end_nm, increment] | ||
| 342 | # 'encoding' values are 'XYZ', 'xyz', 'ICC_XYZ', 'ICC_XYZNumber', 'RGBV', 'rgbv' and 'density' | ||
| 343 | # supported input types: | ||
| 344 | # parameters: (list, [hash]) | ||
| 345 | # parameters: (vector, [hash]) | ||
| 346 | # parameters: (matrix, [hash]) | ||
| 347 | # parameters: (Math::Matrix_object, [hash]) | ||
| 348 | # parameters: (structure, [hash]) | ||
| 349 | # returns: (same_type_as_input) | ||
| 350 | sub transform { | ||
| 351 | |||
| 352 | # set hash value (0 or 1) | ||
| 353 | my $h = ref($_[-1]) eq 'HASH' ? 1 : 0; | ||
| 354 | |||
| 355 | # verify color weight array | ||
| 356 | (defined($_[0]->[3][0]) && defined($_[0]->[3][1])) or croak('color weight array undefined'); | ||
| 357 | |||
| 358 | # if input a 'Math::Matrix' object | ||
| 359 | if (@_ == $h + 2 && UNIVERSAL::isa($_[1], 'Math::Matrix')) { | ||
| 360 | |||
| 361 | # call matrix transform | ||
| 362 | &_trans2; | ||
| 363 | |||
| 364 | # if input an array reference | ||
| 365 | } elsif (@_ == $h + 2 && ref($_[1]) eq 'ARRAY') { | ||
| 366 | |||
| 367 | # if array contains numbers (vector) | ||
| 368 | if (! ref($_[1][0]) && @{$_[1]} == grep {Scalar::Util::looks_like_number($_)} @{$_[1]}) { | ||
| 369 | |||
| 370 | # call vector transform | ||
| 371 | &_trans1; | ||
| 372 | |||
| 373 | # if array contains vectors (2-D array) | ||
| 374 | } elsif (ref($_[1][0]) eq 'ARRAY' && @{$_[1]} == grep {ref($_) eq 'ARRAY' && Scalar::Util::looks_like_number($_->[0])} @{$_[1]}) { | ||
| 375 | |||
| 376 | # call matrix transform | ||
| 377 | &_trans2; | ||
| 378 | |||
| 379 | } else { | ||
| 380 | |||
| 381 | # call structure transform | ||
| 382 | &_trans3; | ||
| 383 | |||
| 384 | } | ||
| 385 | |||
| 386 | # if input a list (of numbers) | ||
| 387 | } elsif (@_ == $h + 1 + grep {Scalar::Util::looks_like_number($_)} @_) { | ||
| 388 | |||
| 389 | # call list transform | ||
| 390 | &_trans0; | ||
| 391 | |||
| 392 | } else { | ||
| 393 | |||
| 394 | # error | ||
| 395 | croak('invalid transform input'); | ||
| 396 | |||
| 397 | } | ||
| 398 | |||
| 399 | } | ||
| 400 | |||
| 401 | # compute Jacobian matrix | ||
| 402 | # hash keys are: 'range', and 'encoding' | ||
| 403 | # 'range' value is: [start_nm, end_nm, increment] | ||
| 404 | # 'encoding' values are 'XYZ', 'xyz', 'ICC_XYZ', 'ICC_XYZNumber', 'RGBV', 'rgbv' and 'density' | ||
| 405 | # parameters: (input_vector, [hash]) | ||
| 406 | # returns: (Jacobian_matrix, [output_vector]) | ||
| 407 | sub jacobian { | ||
| 408 | |||
| 409 | # get parameters | ||
| 410 | my ($self, $in, $hash) = @_; | ||
| 411 | |||
| 412 | # local variables | ||
| 413 | my ($range, $encoding, $jac, $out, $sf); | ||
| 414 | |||
| 415 | # check if ICC::Support::Lapack module is loaded | ||
| 416 | state $lapack = defined($INC{'ICC/Support/Lapack.pm'}); | ||
| 417 | |||
| 418 | # get input spectral range | ||
| 419 | ($range = $hash->{'range'} || $srh->{@{$in}}) or croak('spectral range must be specified'); | ||
| 420 | |||
| 421 | # if data increment == cwf increment | ||
| 422 | if ($range->[2] == $self->[3][0][2]) { | ||
| 423 | |||
| 424 | # use adjusted cwf | ||
| 425 | $jac = Storable::dclone(_adjust_cwf($self, $range)); | ||
| 426 | |||
| 427 | # if data increment > cwf increment | ||
| 428 | } elsif ($range->[2] > $self->[3][0][2]) { | ||
| 429 | |||
| 430 | # use reduced cwf | ||
| 431 | $jac = Storable::dclone(_reduce_cwf($self, $range)); | ||
| 432 | |||
| 433 | } else { | ||
| 434 | |||
| 435 | # error | ||
| 436 | croak('data increment < cwf increment'); | ||
| 437 | |||
| 438 | } | ||
| 439 | |||
| 440 | # if hash contain 'encoding' other than 'XYZ' or 'RGBV' | ||
| 441 | if (defined($encoding = $hash->{'encoding'}) && $encoding ne 'XYZ' && $encoding ne 'RGBV') { | ||
| 442 | |||
| 443 | # if encoding is 'ICC_XYZ' | ||
| 444 | if ($encoding eq 'ICC_XYZ' && @{$jac} == 3) { | ||
| 445 | |||
| 446 | # for each XYZ | ||
| 447 | for my $i (0 .. 2) { | ||
| 448 | |||
| 449 | # for each spectral value | ||
| 450 | for my $j (0 .. $#{$jac->[0]}) { | ||
| 451 | |||
| 452 | # adjust value | ||
| 453 | $jac->[$i][$j] *= 327.68/65535; | ||
| 454 | |||
| 455 | } | ||
| 456 | |||
| 457 | } | ||
| 458 | |||
| 459 | # if encoding is 'ICC_XYZNumber' | ||
| 460 | } elsif ($encoding eq 'ICC_XYZNumber' && @{$jac} == 3) { | ||
| 461 | |||
| 462 | # for each XYZ | ||
| 463 | for my $i (0 .. 2) { | ||
| 464 | |||
| 465 | # for each spectral value | ||
| 466 | for my $j (0 .. $#{$jac->[0]}) { | ||
| 467 | |||
| 468 | # adjust value | ||
| 469 | $jac->[$i][$j] /= 100; | ||
| 470 | |||
| 471 | } | ||
| 472 | |||
| 473 | } | ||
| 474 | |||
| 475 | # if encoding is 'xyz' | ||
| 476 | } elsif ($encoding eq 'xyz' && @{$jac} == 3) { | ||
| 477 | |||
| 478 | # verify white point | ||
| 479 | ($self->[5][0] && $self->[5][1] && $self->[5][2]) or croak('invalid illuminant white point'); | ||
| 480 | |||
| 481 | # for each XYZ | ||
| 482 | for my $i (0 .. 2) { | ||
| 483 | |||
| 484 | # for each spectral value | ||
| 485 | for my $j (0 .. $#{$jac->[0]}) { | ||
| 486 | |||
| 487 | # adjust value | ||
| 488 | $jac->[$i][$j] /= $self->[5][$i]; | ||
| 489 | |||
| 490 | } | ||
| 491 | |||
| 492 | } | ||
| 493 | |||
| 494 | # if encoding is 'unit' | ||
| 495 | } elsif ($encoding eq 'unit' && @{$jac} == 4) { | ||
| 496 | |||
| 497 | # for each RGBV | ||
| 498 | for my $i (0 .. 3) { | ||
| 499 | |||
| 500 | # for each spectral value | ||
| 501 | for my $j (0 .. $#{$jac->[0]}) { | ||
| 502 | |||
| 503 | # adjust value | ||
| 504 | $jac->[$i][$j] /= 100; | ||
| 505 | |||
| 506 | } | ||
| 507 | |||
| 508 | } | ||
| 509 | |||
| 510 | # if encoding is 'density' | ||
| 511 | } elsif ($encoding eq 'density' && @{$jac} == 4) { | ||
| 512 | |||
| 513 | # delete encoding | ||
| 514 | delete($hash->{'encoding'}); | ||
| 515 | |||
| 516 | # get the output values (RGBV) | ||
| 517 | $out = _trans1($self, $in, $hash); | ||
| 518 | |||
| 519 | # verify output | ||
| 520 | ($out->[0] && $out->[1] && $out->[2] && $out->[3]) or croak('invalid density value'); | ||
| 521 | |||
| 522 | # for each RGBV | ||
| 523 | for my $i (0 .. 3) { | ||
| 524 | |||
| 525 | # compute scale factor | ||
| 526 | $sf = - $out->[$i] * ICC::Shared::ln10; | ||
| 527 | |||
| 528 | # for each spectral value | ||
| 529 | for my $j (0 .. $#{$jac->[0]}) { | ||
| 530 | |||
| 531 | # adjust value | ||
| 532 | $jac->[$i][$j] /= $sf; | ||
| 533 | |||
| 534 | } | ||
| 535 | |||
| 536 | } | ||
| 537 | |||
| 538 | # restore encoding | ||
| 539 | $hash->{'encoding'} = 'density'; | ||
| 540 | |||
| 541 | } else { | ||
| 542 | |||
| 543 | # error | ||
| 544 | croak('unsupported XYZ/RGBV encoding'); | ||
| 545 | } | ||
| 546 | |||
| 547 | } | ||
| 548 | |||
| 549 | # if output vector wanted | ||
| 550 | if (wantarray) { | ||
| 551 | |||
| 552 | # return Jacobian and output vector | ||
| 553 | return($jac, _trans1($self, $in, $hash)); | ||
| 554 | |||
| 555 | } else { | ||
| 556 | |||
| 557 | # return Jacobian only | ||
| 558 | return($jac); | ||
| 559 | |||
| 560 | } | ||
| 561 | |||
| 562 | } | ||
| 563 | |||
| 564 | # print object contents to string !!! needs work !!! | ||
| 565 | # format is an array structure | ||
| 566 | # parameter: ([format]) | ||
| 567 | # returns: (string) | ||
| 568 | sub sdump { | ||
| 569 | |||
| 570 | # get parameters | ||
| 571 | my ($self, $p) = @_; | ||
| 572 | |||
| 573 | # local variables | ||
| 574 | my ($s, $fmt); | ||
| 575 | |||
| 576 | # resolve parameter to an array reference | ||
| 577 | $p = defined($p) ? ref($p) eq 'ARRAY' ? $p : [$p] : []; | ||
| 578 | |||
| 579 | # get format string | ||
| 580 | $fmt = defined($p->[0]) && ! ref($p->[0]) ? $p->[0] : 'undef'; | ||
| 581 | |||
| 582 | # set string to object ID | ||
| 583 | $s = sprintf("'%s' object, (0x%x)\n", ref($self), $self); | ||
| 584 | |||
| 585 | # return | ||
| 586 | return($s); | ||
| 587 | |||
| 588 | } | ||
| 589 | |||
| 590 | # transform list | ||
| 591 | # parameters: (object_reference, list, [hash]) | ||
| 592 | # returns: (list) | ||
| 593 | sub _trans0 { | ||
| 594 | |||
| 595 | # local variables | ||
| 596 | my ($self, $hash); | ||
| 597 | |||
| 598 | # get object reference | ||
| 599 | $self = shift(); | ||
| 600 | |||
| 601 | # get optional hash | ||
| 602 | $hash = pop() if (ref($_[-1]) eq 'HASH'); | ||
| 603 | |||
| 604 | # return | ||
| 605 | return(@{_trans1($self, \@_, $hash)}); | ||
| 606 | |||
| 607 | } | ||
| 608 | |||
| 609 | # transform vector | ||
| 610 | # parameters: (object_reference, vector, [hash]) | ||
| 611 | # returns: (vector) | ||
| 612 | sub _trans1 { | ||
| 613 | |||
| 614 | # get parameters | ||
| 615 | my ($self, $in, $hash) = @_; | ||
| 616 | |||
| 617 | # local variables | ||
| 618 | my ($range, $cwf, $out, $code); | ||
| 619 | |||
| 620 | # check if ICC::Support::Lapack module is loaded | ||
| 621 | state $lapack = defined($INC{'ICC/Support/Lapack.pm'}); | ||
| 622 | |||
| 623 | # get input spectral range | ||
| 624 | ($range = $hash->{'range'} || $srh->{@{$in}}) or croak('spectral range must be specified'); | ||
| 625 | |||
| 626 | # if data increment == cwf increment | ||
| 627 | if ($range->[2] == $self->[3][0][2]) { | ||
| 628 | |||
| 629 | # adjust cwf to match data range | ||
| 630 | $cwf = _adjust_cwf($self, $range); | ||
| 631 | |||
| 632 | # if data increment > cwf increment | ||
| 633 | } elsif ($range->[2] > $self->[3][0][2]) { | ||
| 634 | |||
| 635 | # adjust cwf to match data range | ||
| 636 | $cwf = _reduce_cwf($self, $range); | ||
| 637 | |||
| 638 | } else { | ||
| 639 | |||
| 640 | # error | ||
| 641 | croak('data increment < cwf increment'); | ||
| 642 | |||
| 643 | } | ||
| 644 | |||
| 645 | # if ICC::Support::Lapack module is loaded | ||
| 646 | if ($lapack) { | ||
| 647 | |||
| 648 | # compute output matrix using BLAS dgemv function | ||
| 649 | $out = ICC::Support::Lapack::matf_vec_trans($in, $cwf); | ||
| 650 | |||
| 651 | } else { | ||
| 652 | |||
| 653 | # for each XYZ or RGBV | ||
| 654 | for my $i (0 .. $#{$cwf}) { | ||
| 655 | |||
| 656 | # compute dot product | ||
| 657 | $out->[$i] = ICC::Shared::dotProduct($in, $cwf->[$i]); | ||
| 658 | |||
| 659 | } | ||
| 660 | |||
| 661 | } | ||
| 662 | |||
| 663 | # return with optional encoding | ||
| 664 | return(defined($code = _encoding($self, $hash)) ? [&$code(@{$out})] : $out); | ||
| 665 | |||
| 666 | } | ||
| 667 | |||
| 668 | # transform matrix (2-D array -or- Math::Matrix object) | ||
| 669 | # parameters: (object_reference, matrix, [hash]) | ||
| 670 | # returns: (matrix) | ||
| 671 | sub _trans2 { | ||
| 672 | |||
| 673 | # get parameters | ||
| 674 | my ($self, $in, $hash) = @_; | ||
| 675 | |||
| 676 | # local variables | ||
| 677 | my ($range, $cwf, $out, $code); | ||
| 678 | |||
| 679 | # check if ICC::Support::Lapack module is loaded | ||
| 680 | state $lapack = defined($INC{'ICC/Support/Lapack.pm'}); | ||
| 681 | |||
| 682 | # get input spectral range | ||
| 683 | ($range = $hash->{'range'} || $srh->{@{$in->[0]}}) or croak('spectral range must be specified'); | ||
| 684 | |||
| 685 | # if data increment == cwf increment | ||
| 686 | if ($range->[2] == $self->[3][0][2]) { | ||
| 687 | |||
| 688 | # adjust cwf to match data range | ||
| 689 | $cwf = _adjust_cwf($self, $range); | ||
| 690 | |||
| 691 | # if data increment > cwf increment | ||
| 692 | } elsif ($range->[2] > $self->[3][0][2]) { | ||
| 693 | |||
| 694 | # adjust cwf to match data range | ||
| 695 | $cwf = _reduce_cwf($self, $range); | ||
| 696 | |||
| 697 | } else { | ||
| 698 | |||
| 699 | # error | ||
| 700 | croak('data increment < cwf increment'); | ||
| 701 | |||
| 702 | } | ||
| 703 | |||
| 704 | # if ICC::Support::Lapack module is loaded | ||
| 705 | if ($lapack) { | ||
| 706 | |||
| 707 | # compute output matrix using BLAS dgemm function | ||
| 708 | $out = ICC::Support::Lapack::matf_mat_trans($in, $cwf); | ||
| 709 | |||
| 710 | } else { | ||
| 711 | |||
| 712 | # for each sample | ||
| 713 | for my $i (0 .. $#{$in}) { | ||
| 714 | |||
| 715 | # for each XYZ or RGBV | ||
| 716 | for my $j (0 .. $#{$cwf}) { | ||
| 717 | |||
| 718 | # compute dot product | ||
| 719 | $out->[$i][$j] = ICC::Shared::dotProduct($in->[$i], $cwf->[$j]); | ||
| 720 | |||
| 721 | } | ||
| 722 | |||
| 723 | } | ||
| 724 | |||
| 725 | } | ||
| 726 | |||
| 727 | # if encoding enabled | ||
| 728 | if ($code = _encoding($self, $hash)) { | ||
| 729 | |||
| 730 | # for each sample | ||
| 731 | for my $i (0 .. $#{$out}) { | ||
| 732 | |||
| 733 | # apply encoding | ||
| 734 | @{$out->[$i]} = &$code(@{$out->[$i]}); | ||
| 735 | |||
| 736 | } | ||
| 737 | |||
| 738 | } | ||
| 739 | |||
| 740 | # return output (Math::Matrix object or 2-D array) | ||
| 741 | return(UNIVERSAL::isa($in, 'Math::Matrix') ? bless($out, 'Math::Matrix') : $out); | ||
| 742 | |||
| 743 | } | ||
| 744 | |||
| 745 | # transform structure | ||
| 746 | # parameters: (object_reference, structure, [hash]) | ||
| 747 | # returns: (structure) | ||
| 748 | sub _trans3 { | ||
| 749 | |||
| 750 | # get parameters | ||
| 751 | my ($self, $in, $hash) = @_; | ||
| 752 | |||
| 753 | # transform the array structure | ||
| 754 | _crawl($self, \&_trans1, $in, my $out = [], $hash); | ||
| 755 | |||
| 756 | # return output structure | ||
| 757 | return($out); | ||
| 758 | |||
| 759 | } | ||
| 760 | |||
| 761 | # recursive transform | ||
| 762 | # array structure is traversed until all vectors are found and transformed | ||
| 763 | # parameters: (object_reference, subroutine_reference, input_array_reference, output_array_reference, hash) | ||
| 764 | sub _crawl { | ||
| 765 | |||
| 766 | # get parameters | ||
| 767 | my ($self, $sub, $in, $out, $hash) = @_; | ||
| 768 | |||
| 769 | # if input is a vector (reference to a numeric array) | ||
| 770 | if (@{$in} == grep {Scalar::Util::looks_like_number($_)} @{$in}) { | ||
| 771 | |||
| 772 | # transform input vector and copy to output | ||
| 773 | @{$out} = @{$sub->($self, $in, $hash)}; | ||
| 774 | |||
| 775 | } else { | ||
| 776 | |||
| 777 | # for each input element | ||
| 778 | for my $i (0 .. $#{$in}) { | ||
| 779 | |||
| 780 | # if an array reference | ||
| 781 | if (ref($in->[$i]) eq 'ARRAY') { | ||
| 782 | |||
| 783 | # transform next level | ||
| 784 | _crawl($self, $sub, $in->[$i], $out->[$i] = [], $hash); | ||
| 785 | |||
| 786 | } else { | ||
| 787 | |||
| 788 | # error | ||
| 789 | croak('invalid input structure'); | ||
| 790 | |||
| 791 | } | ||
| 792 | |||
| 793 | } | ||
| 794 | |||
| 795 | } | ||
| 796 | |||
| 797 | } | ||
| 798 | |||
| 799 | # reduce cwf matrix to match data range | ||
| 800 | # the reduced cwf and data range is cached | ||
| 801 | # parameters: (object_reference, data_range) | ||
| 802 | # returns: (reduced_cwf) | ||
| 803 | sub _reduce_cwf { | ||
| 804 | |||
| 805 | # get parameters | ||
| 806 | my ($self, $range_data) = @_; | ||
| 807 | |||
| 808 | # local variables | ||
| 809 | my ($method, $bandpass, $range_cwf, $cwf, $mat); | ||
| 810 | |||
| 811 | # if cached CWF matches data range | ||
| 812 | if (defined($self->[4][0]) && $self->[4][0][0] == $range_data->[0] && $self->[4][0][1] == $range_data->[1] && $self->[4][0][2] == $range_data->[2]) { | ||
| 813 | |||
| 814 | # return cached CWF | ||
| 815 | return($self->[4][1]); | ||
| 816 | |||
| 817 | } | ||
| 818 | |||
| 819 | # check if ICC::Support::Lapack module is loaded | ||
| 820 | state $lapack = defined($INC{'ICC/Support/Lapack.pm'}); | ||
| 821 | |||
| 822 | # get interpolation method | ||
| 823 | $method = $self->[0]{'method'} // 'cspline'; | ||
| 824 | |||
| 825 | # get bandpass correction | ||
| 826 | $bandpass = $self->[0]{'bandpass'} // 0; | ||
| 827 | |||
| 828 | # get cwf range | ||
| 829 | $range_cwf = $self->[3][0]; | ||
| 830 | |||
| 831 | # if method is linear | ||
| 832 | if ($method eq 'linear') { | ||
| 833 | |||
| 834 | # compute linear interpolation matrix | ||
| 835 | $mat = ICC::Shared::linear_matrix($range_data, $range_cwf, 'copy'); | ||
| 836 | |||
| 837 | # if method is cubic spline | ||
| 838 | } elsif ($method eq 'cspline') { | ||
| 839 | |||
| 840 | # compute cubic spline interpolation matrix | ||
| 841 | $mat = ICC::Shared::cspline_matrix($range_data, $range_cwf, 'copy'); | ||
| 842 | |||
| 843 | # if method is Lagrange | ||
| 844 | } elsif ($method eq 'lagrange') { | ||
| 845 | |||
| 846 | # compute Lagrange interpolation matrix | ||
| 847 | $mat = ICC::Shared::lagrange_matrix($range_data, $range_cwf, 'copy'); | ||
| 848 | |||
| 849 | } else { | ||
| 850 | |||
| 851 | # error | ||
| 852 | croak('invalid interpolation method'); | ||
| 853 | |||
| 854 | } | ||
| 855 | |||
| 856 | # if ICC::Support::Lapack module is loaded | ||
| 857 | if ($lapack) { | ||
| 858 | |||
| 859 | # compute cwf using BLAS dgemm function | ||
| 860 | $cwf = ICC::Support::Lapack::mat_xplus($self->[3][1], $mat); | ||
| 861 | |||
| 862 | # if bandpass correction is enabled | ||
| 863 | if ($bandpass) { | ||
| 864 | |||
| 865 | # apply correction matrix | ||
| 866 | $cwf = ICC::Support::Lapack::mat_xplus($cwf, _bandpass($range_data, $method, $bandpass)); | ||
| 867 | |||
| 868 | } | ||
| 869 | |||
| 870 | } else { | ||
| 871 | |||
| 872 | # compute cwf using Math::Matrix module | ||
| 873 | $cwf = bless(Storable::dclone($self->[3][1]), 'Math::Matrix') * $mat; | ||
| 874 | |||
| 875 | # if bandpass correction is enabled | ||
| 876 | if ($bandpass) { | ||
| 877 | |||
| 878 | # apply correction matrix | ||
| 879 | $cwf = $cwf * _bandpass($range_data, $method, $bandpass); | ||
| 880 | |||
| 881 | } | ||
| 882 | |||
| 883 | } | ||
| 884 | |||
| 885 | # cache reduced CWF | ||
| 886 | $self->[4][0] = $range_data; | ||
| 887 | $self->[4][1] = $cwf; | ||
| 888 | |||
| 889 | # return | ||
| 890 | return($cwf); | ||
| 891 | |||
| 892 | } | ||
| 893 | |||
| 894 | # make bandpass correction matrix | ||
| 895 | # per ASTM E 2729 or deconvolution | ||
| 896 | # parameters: (data_range, interpolation_method, bandpass_method) | ||
| 897 | # returns: (matrix) | ||
| 898 | sub _bandpass { | ||
| 899 | |||
| 900 | # get parameters | ||
| 901 | my ($range_data, $method, $bandpass) = @_; | ||
| 902 | |||
| 903 | # local variables | ||
| 904 | my ($ix, $jx, $range_mat, $mati, $matc, $bpf, $info, $matrix); | ||
| 905 | |||
| 906 | # compute bandpass matrix upper index | ||
| 907 | $ix = ($range_data->[1] - $range_data->[0])/$range_data->[2]; | ||
| 908 | |||
| 909 | # set bandpass function upper index | ||
| 910 | $jx = 20; | ||
| 911 | |||
| 912 | # if bandpass value if 'astm' | ||
| 913 | if ($bandpass eq 'astm') { | ||
| 914 | |||
| 915 | # return ASTM E 2729 bandpass rectification matrix | ||
| 916 | return(_bandpass_astm($ix)); | ||
| 917 | |||
| 918 | } else { | ||
| 919 | |||
| 920 | # compute interpolation matrix range | ||
| 921 | $range_mat = [$range_data->[0] - $range_data->[2], $range_data->[1] + $range_data->[2], $range_data->[2]/10]; | ||
| 922 | |||
| 923 | # if method is linear | ||
| 924 | if ($method eq 'linear') { | ||
| 925 | |||
| 926 | # compute linear interpolation matrix | ||
| 927 | $mati = ICC::Shared::linear_matrix($range_data, $range_mat, 'linear'); | ||
| 928 | |||
| 929 | # if method is cubic spline | ||
| 930 | } elsif ($method eq 'cspline') { | ||
| 931 | |||
| 932 | # compute cubic spline interpolation matrix | ||
| 933 | $mati = ICC::Shared::cspline_matrix($range_data, $range_mat, 'linear'); | ||
| 934 | |||
| 935 | # if method is Lagrange | ||
| 936 | } elsif ($method eq 'lagrange') { | ||
| 937 | |||
| 938 | # compute Lagrange interpolation matrix | ||
| 939 | $mati = ICC::Shared::lagrange_matrix($range_data, $range_mat, 'linear'); | ||
| 940 | |||
| 941 | } else { | ||
| 942 | |||
| 943 | # error | ||
| 944 | croak('invalid interpolation method'); | ||
| 945 | |||
| 946 | } | ||
| 947 | |||
| 948 | # if bandpass value if 'triangle' | ||
| 949 | if ($bandpass eq 'triangle') { | ||
| 950 | |||
| 951 | # compute bandpass function array | ||
| 952 | $bpf = _bandpass_fn(1, $jx); | ||
| 953 | |||
| 954 | # if bandpass value if 'trapezoid' (combination of three triangular sub-bands) | ||
| 955 | } elsif ($bandpass eq 'trapezoid') { | ||
| 956 | |||
| 957 | # compute bandpass function array | ||
| 958 | $bpf = _bandpass_fn(1/3, $jx); | ||
| 959 | |||
| 960 | # if bandpass value is an array reference | ||
| 961 | } elsif (ref($bandpass) eq 'ARRAY' && $#{$bandpass} == $jx) { | ||
| 962 | |||
| 963 | # set value | ||
| 964 | $bpf = $bandpass; | ||
| 965 | |||
| 966 | } else { | ||
| 967 | |||
| 968 | # error | ||
| 969 | croak('invalid bandpass correction'); | ||
| 970 | |||
| 971 | } | ||
| 972 | |||
| 973 | # compute convolution matrix | ||
| 974 | $matc = _conv_matrix($bpf, $ix); | ||
| 975 | |||
| 976 | # check if ICC::Support::Lapack module is loaded | ||
| 977 | state $lapack = defined($INC{'ICC/Support/Lapack.pm'}); | ||
| 978 | |||
| 979 | # if Lapack module loaded | ||
| 980 | if ($lapack) { | ||
| 981 | |||
| 982 | # multiply matrices and invert using Lapack module | ||
| 983 | ($info, $matrix) = ICC::Support::Lapack::inv(ICC::Support::Lapack::mat_xplus($matc, $mati)); | ||
| 984 | |||
| 985 | # return deconvolution matrix | ||
| 986 | return(bless($matrix, 'Math::Matrix')); | ||
| 987 | |||
| 988 | } else { | ||
| 989 | |||
| 990 | # return deconvolution matrix | ||
| 991 | return(($matc * $mati)->invert()); | ||
| 992 | |||
| 993 | } | ||
| 994 | |||
| 995 | } | ||
| 996 | |||
| 997 | } | ||
| 998 | |||
| 999 | # make ASTM E 2729 bandpass rectification matrix | ||
| 1000 | # parameter: (upper_index) | ||
| 1001 | # returns: (matrix) | ||
| 1002 | sub _bandpass_astm { | ||
| 1003 | |||
| 1004 | # get upper index | ||
| 1005 | my $ix = shift(); | ||
| 1006 | |||
| 1007 | # local variables | ||
| 1008 | my ($matrix, @zeros); | ||
| 1009 | |||
| 1010 | # make array of zeros | ||
| 1011 | @zeros = (0) x ($ix + 1); | ||
| 1012 | |||
| 1013 | # for each matrix row | ||
| 1014 | for my $i (0 .. $ix) { | ||
| 1015 | |||
| 1016 | # set the row to zeros | ||
| 1017 | $matrix->[$i] = [@zeros]; | ||
| 1018 | |||
| 1019 | # for each matrix column | ||
| 1020 | for my $j (0 .. $ix) { | ||
| 1021 | |||
| 1022 | # if main diagonal | ||
| 1023 | if ($i == $j) { | ||
| 1024 | |||
| 1025 | if ($i == 0 || $i == $ix) { | ||
| 1026 | |||
| 1027 | $matrix->[$i][$j] = 1; | ||
| 1028 | |||
| 1029 | } elsif ($i == 1 || $i == $ix - 1) { | ||
| 1030 | |||
| 1031 | $matrix->[$i][$j] = 1.21; | ||
| 1032 | |||
| 1033 | } else { | ||
| 1034 | |||
| 1035 | $matrix->[$i][$j] = 1.22; | ||
| 1036 | |||
| 1037 | } | ||
| 1038 | |||
| 1039 | # if +/- 1 diagonal, and not the first or last rows | ||
| 1040 | } elsif (abs($i - $j) == 1 && $i > 0 && $i < $ix) { | ||
| 1041 | |||
| 1042 | if ($j == 0 || $j == $ix) { | ||
| 1043 | |||
| 1044 | $matrix->[$i][$j] = -0.10; | ||
| 1045 | |||
| 1046 | } else { | ||
| 1047 | |||
| 1048 | $matrix->[$i][$j] = -0.12; | ||
| 1049 | |||
| 1050 | } | ||
| 1051 | |||
| 1052 | # if +/- 2 diagonal, and not the first or last rows | ||
| 1053 | } elsif (abs($i - $j) == 2 && $i > 0 && $i < $ix) { | ||
| 1054 | |||
| 1055 | $matrix->[$i][$j] = 0.01; | ||
| 1056 | |||
| 1057 | } | ||
| 1058 | |||
| 1059 | } | ||
| 1060 | |||
| 1061 | } | ||
| 1062 | |||
| 1063 | # return Math::Matrix object | ||
| 1064 | return(bless($matrix, 'Math::Matrix')); | ||
| 1065 | |||
| 1066 | } | ||
| 1067 | |||
| 1068 | # make simple bandpass function | ||
| 1069 | # shape ranges from 0 to 1 (rectangular to triangular) | ||
| 1070 | # upper index must be divisible by 4 | ||
| 1071 | # parameters: (shape, upper_index) | ||
| 1072 | # returns: (bandpass_array) | ||
| 1073 | sub _bandpass_fn { | ||
| 1074 | |||
| 1075 | # get parameters | ||
| 1076 | my ($shape, $ix) = @_; | ||
| 1077 | |||
| 1078 | # local variables | ||
| 1079 | my ($m, $v, @array, $sum); | ||
| 1080 | |||
| 1081 | # verify shape | ||
| 1082 | ($shape >= 0 && $shape <= 1) or croak('invalid shape parameter'); | ||
| 1083 | |||
| 1084 | # verify upper index | ||
| 1085 | ($ix == int($ix) && ! ($ix % 4)) or croak('invalid upper index'); | ||
| 1086 | |||
| 1087 | # if shape not rectangular | ||
| 1088 | if ($shape) { | ||
| 1089 | |||
| 1090 | # compute slope | ||
| 1091 | $m = 2/($shape * $ix); | ||
| 1092 | |||
| 1093 | } else { | ||
| 1094 | |||
| 1095 | # set slope to big number | ||
| 1096 | $m = 1E100; | ||
| 1097 | |||
| 1098 | } | ||
| 1099 | |||
| 1100 | # for each array element | ||
| 1101 | for my $i (0 .. $ix/2) { | ||
| 1102 | |||
| 1103 | # compute value | ||
| 1104 | $v = ($i - $ix/4) * $m + 0.5; | ||
| 1105 | |||
| 1106 | # set array elements to limited value | ||
| 1107 | $array[$i] = $array[$ix - $i] = $v < 0 ? 0 : $v > 1 ? 1 : $v; | ||
| 1108 | |||
| 1109 | } | ||
| 1110 | |||
| 1111 | # compute array sum | ||
| 1112 | $sum = List::Util::sum(@array); | ||
| 1113 | |||
| 1114 | # return normalized array | ||
| 1115 | return([map {$_/$sum} @array]); | ||
| 1116 | |||
| 1117 | } | ||
| 1118 | |||
| 1119 | # make convolution matrix | ||
| 1120 | # parameters: (bandpass_function_vector, row_upper_index) | ||
| 1121 | # returns: (convolution_matrix) | ||
| 1122 | sub _conv_matrix { | ||
| 1123 | |||
| 1124 | # get parameters | ||
| 1125 | my ($bpf, $ix) = @_; | ||
| 1126 | |||
| 1127 | # local variables | ||
| 1128 | my ($n, $p, @zeros, $mat); | ||
| 1129 | |||
| 1130 | # verify bandpass function | ||
| 1131 | (@{$bpf} % 2) or croak('bandpass function must have odd number of elements'); | ||
| 1132 | |||
| 1133 | # verify row upper index | ||
| 1134 | ($ix == int($ix) && $ix >= 0) or croak('row upper index must be an integer >= 0'); | ||
| 1135 | |||
| 1136 | # compute upper column index | ||
| 1137 | $n = $#{$bpf} * ($ix + 2)/2; | ||
| 1138 | |||
| 1139 | # compute bandpass increment | ||
| 1140 | $p = $#{$bpf}/2; | ||
| 1141 | |||
| 1142 | # make array of zeros | ||
| 1143 | @zeros = (0) x ($n + 1); | ||
| 1144 | |||
| 1145 | # for each row | ||
| 1146 | for my $i (0 .. $ix) { | ||
| 1147 | |||
| 1148 | # add row of zeros | ||
| 1149 | $mat->[$i] = [@zeros]; | ||
| 1150 | |||
| 1151 | # for each bandpass value | ||
| 1152 | for my $j (0 .. $#{$bpf}) { | ||
| 1153 | |||
| 1154 | # copy bandpass value | ||
| 1155 | $mat->[$i][$i * $p + $j] = $bpf->[$j]; | ||
| 1156 | |||
| 1157 | } | ||
| 1158 | |||
| 1159 | } | ||
| 1160 | |||
| 1161 | # return | ||
| 1162 | return(bless($mat, 'Math::Matrix')); | ||
| 1163 | |||
| 1164 | } | ||
| 1165 | |||
| 1166 | # adjust cwf matrix to match data range | ||
| 1167 | # the adjusted cwf and data range is cached | ||
| 1168 | # parameters: (object_reference, data_range) | ||
| 1169 | # returns: (adjusted_cwf) | ||
| 1170 | sub _adjust_cwf { | ||
| 1171 | |||
| 1172 | # get parameters | ||
| 1173 | my ($self, $range_data) = @_; | ||
| 1174 | |||
| 1175 | # local variables | ||
| 1176 | my ($bandpass, $off, $cwf); | ||
| 1177 | |||
| 1178 | # if cached CWF matches data range | ||
| 1179 | if (defined($self->[4][0]) && $self->[4][0][0] == $range_data->[0] && $self->[4][0][1] == $range_data->[1] && $self->[4][0][2] == $range_data->[2]) { | ||
| 1180 | |||
| 1181 | # return cached CWF | ||
| 1182 | return($self->[4][1]); | ||
| 1183 | |||
| 1184 | } | ||
| 1185 | |||
| 1186 | # check if ICC::Support::Lapack module is loaded | ||
| 1187 | state $lapack = defined($INC{'ICC/Support/Lapack.pm'}); | ||
| 1188 | |||
| 1189 | # get bandpass correction | ||
| 1190 | $bandpass = $self->[0]{'bandpass'} // 0; | ||
| 1191 | |||
| 1192 | # compute range offset | ||
| 1193 | $off = abs(($range_data->[0] - $self->[3][0][0])/$range_data->[2]); | ||
| 1194 | |||
| 1195 | # verify offset is an integer | ||
| 1196 | (abs($off - ICC::Shared::round($off)) < 1E-12) or croak('range offset not an integer'); | ||
| 1197 | |||
| 1198 | # adjust CWF matrix | ||
| 1199 | $cwf = _adjust_matrix($self->[3][1], $self->[3][0], $range_data); | ||
| 1200 | |||
| 1201 | # if bandpass correction enabled, and not table 6 | ||
| 1202 | if ($bandpass && $bandpass ne 'six') { | ||
| 1203 | |||
| 1204 | # if ICC::Support::Lapack module is loaded | ||
| 1205 | if ($lapack) { | ||
| 1206 | |||
| 1207 | # apply correction matrix | ||
| 1208 | $cwf = ICC::Support::Lapack::mat_xplus($cwf, _bandpass($range_data, 'lagrange', $bandpass)); | ||
| 1209 | |||
| 1210 | } else { | ||
| 1211 | |||
| 1212 | # apply correction matrix | ||
| 1213 | $cwf = bless($cwf, 'Math::Matrix') * _bandpass($range_data, 'lagrange', $bandpass); | ||
| 1214 | |||
| 1215 | } | ||
| 1216 | |||
| 1217 | } | ||
| 1218 | |||
| 1219 | # cache adjusted CWF | ||
| 1220 | $self->[4][0] = $range_data; | ||
| 1221 | $self->[4][1] = $cwf; | ||
| 1222 | |||
| 1223 | # return | ||
| 1224 | return($cwf); | ||
| 1225 | |||
| 1226 | } | ||
| 1227 | |||
| 1228 | # adjust matrix to match data range | ||
| 1229 | # parameters: (matrix, matrix_range, data_range) | ||
| 1230 | # returns: (adjusted_matrix) | ||
| 1231 | sub _adjust_matrix { | ||
| 1232 | |||
| 1233 | # get parameters | ||
| 1234 | my ($matrix, $range_matrix, $range_data) = @_; | ||
| 1235 | |||
| 1236 | # local variables | ||
| 1237 | my ($adj, $j); | ||
| 1238 | |||
| 1239 | # clone matrix | ||
| 1240 | $adj = Storable::dclone($matrix); | ||
| 1241 | |||
| 1242 | # for each function | ||
| 1243 | for my $i (0 .. $#{$adj}) { | ||
| 1244 | |||
| 1245 | # if cwf start < data start | ||
| 1246 | if ($range_matrix->[0] < $range_data->[0]) { | ||
| 1247 | |||
| 1248 | # compute number of elements to combine | ||
| 1249 | $j = int(($range_data->[0] - $range_matrix->[0])/$range_data->[2] + 1.5); | ||
| 1250 | |||
| 1251 | # combine elements | ||
| 1252 | splice(@{$adj->[$i]}, 0, $j, List::Util::sum(@{$adj->[$i]}[0 .. ($j - 1)])); | ||
| 1253 | |||
| 1254 | # if cwf start > data start | ||
| 1255 | } elsif ($range_matrix->[0] > $range_data->[0]) { | ||
| 1256 | |||
| 1257 | # compute number of zeros to add | ||
| 1258 | $j = int(($range_matrix->[0] - $range_data->[0])/$range_data->[2] + 0.5); | ||
| 1259 | |||
| 1260 | # add zeros | ||
| 1261 | unshift(@{$adj->[$i]}, (0) x $j); | ||
| 1262 | |||
| 1263 | } | ||
| 1264 | |||
| 1265 | # if cwf end > data end | ||
| 1266 | if ($range_matrix->[1] > $range_data->[1]) { | ||
| 1267 | |||
| 1268 | # compute number of elements to combine | ||
| 1269 | $j = int(($range_matrix->[1] - $range_data->[1])/$range_data->[2] + 1.5); | ||
| 1270 | |||
| 1271 | # combine elements | ||
| 1272 | splice(@{$adj->[$i]}, -$j, $j, List::Util::sum(@{$adj->[$i]}[-$j .. -1])); | ||
| 1273 | |||
| 1274 | # if cwf end < data end | ||
| 1275 | } elsif ($range_matrix->[1] < $range_data->[1]) { | ||
| 1276 | |||
| 1277 | # compute number of zeros to add | ||
| 1278 | $j = int(($range_data->[1] - $range_matrix->[1])/$range_data->[2] + 0.5); | ||
| 1279 | |||
| 1280 | # add zeros | ||
| 1281 | push(@{$adj->[$i]}, (0) x $j); | ||
| 1282 | |||
| 1283 | } | ||
| 1284 | |||
| 1285 | } | ||
| 1286 | |||
| 1287 | # return | ||
| 1288 | return($adj); | ||
| 1289 | |||
| 1290 | } | ||
| 1291 | |||
| 1292 | # compute CIE color-weight functions | ||
| 1293 | # parameters: (object_reference) | ||
| 1294 | # results are saved in the object | ||
| 1295 | # cwf cache is cleared | ||
| 1296 | sub _make_cwf { | ||
| 1297 | |||
| 1298 | # get parameters | ||
| 1299 | my $self = shift(); | ||
| 1300 | |||
| 1301 | # local variables | ||
| 1302 | my ($range, $obs, $inc, $smooth, $method, $bandpass, $matc, $illum, $spd, $ks, @k, @Wx, @Wy, @Wz); | ||
| 1303 | |||
| 1304 | # if cwf_range key defined | ||
| 1305 | if (defined($self->[0]{'cwf_range'})) { | ||
| 1306 | |||
| 1307 | # use supplied range | ||
| 1308 | $range = $self->[0]{'cwf_range'}; | ||
| 1309 | |||
| 1310 | # interpolate observer functions, with linear extrapolation | ||
| 1311 | $obs = ICC::Support::Lapack::matf_mat_trans($self->[2][1], ICC::Shared::cspline_matrix($self->[2][0], $range, 'linear')); | ||
| 1312 | |||
| 1313 | # if observer requires interpolation | ||
| 1314 | } elsif ($self->[2][0][2] != ($inc = $self->[0]{'increment'} // 1)) { | ||
| 1315 | |||
| 1316 | # use observer range, changing increment | ||
| 1317 | $range = [$self->[2][0][0], $self->[2][0][1], $inc]; | ||
| 1318 | |||
| 1319 | # interpolate observer functions | ||
| 1320 | $obs = ICC::Support::Lapack::matf_mat_trans($self->[2][1], ICC::Shared::cspline_matrix($self->[2][0], $range)); | ||
| 1321 | |||
| 1322 | } else { | ||
| 1323 | |||
| 1324 | # use observer range | ||
| 1325 | $range = $self->[2][0]; | ||
| 1326 | |||
| 1327 | # get observer functions | ||
| 1328 | $obs = $self->[2][1]; | ||
| 1329 | |||
| 1330 | } | ||
| 1331 | |||
| 1332 | # if observer requires exponentiation | ||
| 1333 | if ($self->[0]{'observer_exp'}) { | ||
| 1334 | |||
| 1335 | # for each spectral value | ||
| 1336 | for my $i (0 .. $#{$obs->[0]}) { | ||
| 1337 | |||
| 1338 | # exponentiate | ||
| 1339 | $obs->[0][$i] = exp($obs->[0][$i]); | ||
| 1340 | $obs->[1][$i] = exp($obs->[1][$i]); | ||
| 1341 | $obs->[2][$i] = exp($obs->[2][$i]); | ||
| 1342 | |||
| 1343 | } | ||
| 1344 | |||
| 1345 | } | ||
| 1346 | |||
| 1347 | # if illuminant is defined | ||
| 1348 | if (defined($self->[1][0])) { | ||
| 1349 | |||
| 1350 | # compute illuminant smoothness | ||
| 1351 | $smooth = _smoothness($self->[1][1]); | ||
| 1352 | |||
| 1353 | # get illuminant interpolation method | ||
| 1354 | $method = $self->[0]{'imethod'} // ($smooth == 0 || $smooth > 0.001) ? 'linear' : 'cspline'; | ||
| 1355 | |||
| 1356 | # if illuminant bandpass correction enabled | ||
| 1357 | if (defined($bandpass = $self->[0]{'ibandpass'})) { | ||
| 1358 | |||
| 1359 | # compute bandpass correction matrix | ||
| 1360 | $matc = _bandpass($self->[1][0], $method, $bandpass); | ||
| 1361 | |||
| 1362 | # apply bandpass correction matrix | ||
| 1363 | $illum = [map {ICC::Shared::dotProduct($self->[1][1], $_)} @{$matc}]; | ||
| 1364 | |||
| 1365 | } else { | ||
| 1366 | |||
| 1367 | # use illuminant as is | ||
| 1368 | $illum = $self->[1][1]; | ||
| 1369 | |||
| 1370 | } | ||
| 1371 | |||
| 1372 | # if method is linear | ||
| 1373 | if ($method eq 'linear') { | ||
| 1374 | |||
| 1375 | # interpolate illuminant data | ||
| 1376 | $spd = ICC::Shared::linear($illum, $self->[1][0], $range, 'copy'); | ||
| 1377 | |||
| 1378 | # if method is cubic spline | ||
| 1379 | } elsif ($method eq 'cspline') { | ||
| 1380 | |||
| 1381 | # interpolate illuminant data | ||
| 1382 | $spd = ICC::Shared::cspline($illum, $self->[1][0], $range, 'copy'); | ||
| 1383 | |||
| 1384 | # if method is Lagrange -or- ASTM E 2022 | ||
| 1385 | } elsif ($method eq 'lagrange') { | ||
| 1386 | |||
| 1387 | # interpolate illuminant data | ||
| 1388 | $spd = ICC::Shared::lagrange($illum, $self->[1][0], $range, 'copy'); | ||
| 1389 | |||
| 1390 | } else { | ||
| 1391 | |||
| 1392 | # error | ||
| 1393 | croak('invalid illuminant interpolation method'); | ||
| 1394 | |||
| 1395 | } | ||
| 1396 | |||
| 1397 | # get or compute summation constant | ||
| 1398 | $ks = $self->[0]{'cwf_ks'} // 100/ICC::Shared::dotProduct($spd, $obs->[1]); | ||
| 1399 | |||
| 1400 | # for each spectral value | ||
| 1401 | for my $i (0 .. $#{$obs->[0]}) { | ||
| 1402 | |||
| 1403 | # compute spectral products (k * illuminant * observer) | ||
| 1404 | $Wx[$i] = $ks * $spd->[$i] * $obs->[0][$i]; | ||
| 1405 | $Wy[$i] = $ks * $spd->[$i] * $obs->[1][$i]; | ||
| 1406 | $Wz[$i] = $ks * $spd->[$i] * $obs->[2][$i]; | ||
| 1407 | |||
| 1408 | } | ||
| 1409 | |||
| 1410 | } else { | ||
| 1411 | |||
| 1412 | # compute summation constants (slightly different for X, Y, Z) | ||
| 1413 | $k[0] = 100/List::Util::sum(@{$obs->[0]}); | ||
| 1414 | $k[1] = 100/List::Util::sum(@{$obs->[1]}); | ||
| 1415 | $k[2] = 100/List::Util::sum(@{$obs->[2]}); | ||
| 1416 | |||
| 1417 | # for each spectral value | ||
| 1418 | for my $i (0 .. $#{$obs->[0]}) { | ||
| 1419 | |||
| 1420 | # compute spectral products (k * observer) | ||
| 1421 | $Wx[$i] = $k[0] * $obs->[0][$i]; | ||
| 1422 | $Wy[$i] = $k[1] * $obs->[1][$i]; | ||
| 1423 | $Wz[$i] = $k[2] * $obs->[2][$i]; | ||
| 1424 | |||
| 1425 | } | ||
| 1426 | |||
| 1427 | } | ||
| 1428 | |||
| 1429 | # set wavelength range (start, end, and increment) | ||
| 1430 | $self->[3][0] = [@{$range}]; | ||
| 1431 | |||
| 1432 | # set color-weight functions | ||
| 1433 | $self->[3][1][0] = [@Wx]; | ||
| 1434 | $self->[3][1][1] = [@Wy]; | ||
| 1435 | $self->[3][1][2] = [@Wz]; | ||
| 1436 | |||
| 1437 | # set illuminant white point (XYZ) | ||
| 1438 | $self->[5][0] = List::Util::sum(@Wx); | ||
| 1439 | $self->[5][1] = List::Util::sum(@Wy); | ||
| 1440 | $self->[5][2] = List::Util::sum(@Wz); | ||
| 1441 | |||
| 1442 | # clear adjusted CWF cache | ||
| 1443 | $self->[4] = []; | ||
| 1444 | |||
| 1445 | } | ||
| 1446 | |||
| 1447 | # read CIE illuminant and color-matching functions | ||
| 1448 | # compute color-weight functions (k * illuminant * observer) and white point | ||
| 1449 | # illuminant, observer and increment specified in hash | ||
| 1450 | # parameters: (object_reference, hash) | ||
| 1451 | # results are saved in the object | ||
| 1452 | sub _cie { | ||
| 1453 | |||
| 1454 | # get parameters | ||
| 1455 | my ($self, $hash) = @_; | ||
| 1456 | |||
| 1457 | # local variables | ||
| 1458 | my ($array, @files, $illum, $s, $w, $sx); | ||
| 1459 | my ($inc, $obs, $cmf, $key); | ||
| 1460 | |||
| 1461 | # get copy of illuminant array | ||
| 1462 | $array = Storable::dclone($hash->{'illuminant'}); | ||
| 1463 | |||
| 1464 | # if empty array (emissive) | ||
| 1465 | if (! @{$array}) { | ||
| 1466 | |||
| 1467 | # clear illuminant | ||
| 1468 | $self->[1] = []; | ||
| 1469 | |||
| 1470 | # if a measured illuminant (file path) | ||
| 1471 | } elsif (@files = grep {-f} File::Glob::bsd_glob($array->[0])) { | ||
| 1472 | |||
| 1473 | # make new chart object | ||
| 1474 | $illum = ICC::Support::Chart->new($files[0]); | ||
| 1475 | |||
| 1476 | # get sample from hash (default is 1) | ||
| 1477 | $s = defined($array->[1]) ? $array->[1] : 1; | ||
| 1478 | |||
| 1479 | # verify sample | ||
| 1480 | (Scalar::Util::looks_like_number($s) && $s == int($s) && $s > 0 && $s <= $illum->size()) or croak('invalid sample number'); | ||
| 1481 | |||
| 1482 | # set wavelength range | ||
| 1483 | $self->[1][0] = $illum->nm(); | ||
| 1484 | |||
| 1485 | # set spectral values | ||
| 1486 | ($self->[1][1] = $illum->spectral([$s])->[0]) or croak('illuminant chart has no spectral data'); | ||
| 1487 | |||
| 1488 | # if a standard illuminant (YAML file in data folder) | ||
| 1489 | } elsif (@files = grep {-f} File::Glob::bsd_glob(ICC::Shared::getICCPath("Data/$array->[0]_illums_*.yml"))) { | ||
| 1490 | |||
| 1491 | # load standard illuminants (YAML format) | ||
| 1492 | $illum = YAML::Tiny->read($files[0])->[0]; | ||
| 1493 | |||
| 1494 | # get wavelength vector | ||
| 1495 | $w = $illum->{'nm'}; | ||
| 1496 | |||
| 1497 | # set wavelength range | ||
| 1498 | $self->[1][0] = [$w->[0], $w->[-1], $w->[1] - $w->[0]]; | ||
| 1499 | |||
| 1500 | # set spectral values | ||
| 1501 | ($self->[1][1] = $illum->{$array->[1]}) or croak('standard illuminant not found'); | ||
| 1502 | |||
| 1503 | # if illuminant is two array references (wavelength range and spd vectors) | ||
| 1504 | } elsif (@{$array} == grep {ref() eq 'ARRAY'} @{$array}) { | ||
| 1505 | |||
| 1506 | # verify wavelength range | ||
| 1507 | (ref($array->[0]) eq 'ARRAY' && 3 == @{$array->[0]} && (3 == grep {Scalar::Util::looks_like_number($_)} @{$array->[0]}) && $array->[0][2]) or croak('invalid illuminant wavelength range'); | ||
| 1508 | |||
| 1509 | # compute upper index of spd array | ||
| 1510 | (($sx = ICC::Shared::round(($array->[0][1] - $array->[0][0])/$array->[0][2])) > 0) or croak('inconsistent illuminant wavelength range'); | ||
| 1511 | |||
| 1512 | # verify spd array | ||
| 1513 | (ref($array->[1]) eq 'ARRAY' && $#{$array->[1]} == $sx && @{$array->[1]} == grep {Scalar::Util::looks_like_number($_)} @{$array->[1]}) or croak('invalid illuminant spd array'); | ||
| 1514 | |||
| 1515 | # copy array contents | ||
| 1516 | $self->[1] = Storable::dclone($array); | ||
| 1517 | |||
| 1518 | } else { | ||
| 1519 | |||
| 1520 | # error | ||
| 1521 | croak("invalid illuminant [@{$array}]"); | ||
| 1522 | |||
| 1523 | } | ||
| 1524 | |||
| 1525 | # get increment from hash (default is 1) | ||
| 1526 | $inc = defined($hash->{'increment'}) ? $hash->{'increment'} : 1; | ||
| 1527 | |||
| 1528 | # if increment is 1 nm | ||
| 1529 | if ($inc == 1) { | ||
| 1530 | |||
| 1531 | # set wavelength range | ||
| 1532 | $self->[2][0] = [360, 830, 1]; | ||
| 1533 | |||
| 1534 | # load CIE color matching functions (YAML format) | ||
| 1535 | $cmf = YAML::Tiny->read(ICC::Shared::getICCPath('Data/CIE_cmfs_360-830_x_1.yml'))->[0]; | ||
| 1536 | |||
| 1537 | # if increment is 5 nm | ||
| 1538 | } elsif ($inc == 5) { | ||
| 1539 | |||
| 1540 | # set wavelength range | ||
| 1541 | $self->[2][0] = [380, 780, 5]; | ||
| 1542 | |||
| 1543 | # load CIE color matching functions (YAML format) | ||
| 1544 | $cmf = YAML::Tiny->read(ICC::Shared::getICCPath('Data/CIE_cmfs_380-780_x_5.yml'))->[0]; | ||
| 1545 | |||
| 1546 | } else { | ||
| 1547 | |||
| 1548 | # error | ||
| 1549 | croak('invalid spectral increment'); | ||
| 1550 | |||
| 1551 | } | ||
| 1552 | |||
| 1553 | # get observer from hash (default is 2) | ||
| 1554 | $obs = defined($hash->{'observer'}) ? $hash->{'observer'} : 2; | ||
| 1555 | |||
| 1556 | # if observer is an array reference | ||
| 1557 | if (ref($obs) eq 'ARRAY') { | ||
| 1558 | |||
| 1559 | # verify array size | ||
| 1560 | (@{$obs} == 2) or croak('invalid observer array'); | ||
| 1561 | |||
| 1562 | # verify range | ||
| 1563 | (ref($obs->[0]) eq 'ARRAY' && (@{$obs->[0]} == grep {Scalar::Util::looks_like_number($_)} @{$obs->[0]}) && | ||
| 1564 | @{$obs->[0]} == 3 && $obs->[0][2] && ! (($obs->[0][1] - $obs->[0][0]) % $obs->[0][2])) or croak('invalid observer wavelength range'); | ||
| 1565 | |||
| 1566 | # compute upper index of observer matrix | ||
| 1567 | (($sx = ICC::Shared::round(($obs->[0][1] - $obs->[0][0])/$obs->[0][2])) > 0) or croak('inconsistent observer wavelength range'); | ||
| 1568 | |||
| 1569 | # verify observer matrix | ||
| 1570 | ((ref($obs->[1]) eq 'ARRAY' || UNIVERSAL::isa($obs->[1], 'Math::Matrix')) && ref($obs->[1][0]) eq 'ARRAY' && | ||
| 1571 | $#{$obs->[1][0]} == $sx && @{$obs->[1][0]} == grep {Scalar::Util::looks_like_number($_)} @{$obs->[1][0]}) or croak('invalid observer cmf matrix'); | ||
| 1572 | |||
| 1573 | # copy array contents | ||
| 1574 | $self->[2] = Storable::dclone($obs); | ||
| 1575 | |||
| 1576 | } else { | ||
| 1577 | |||
| 1578 | # get observer key | ||
| 1579 | ($key = {'2' => 'CIE1931', '10' => 'CIE1964', '2P' => 'CIE2012D2', '10P' => 'CIE2012D10'}->{$obs}) or croak('invalid observer key'); | ||
| 1580 | |||
| 1581 | # set CIE color-matching functions | ||
| 1582 | $self->[2][1][0] = $cmf->{$key . 'x'}; | ||
| 1583 | $self->[2][1][1] = $cmf->{$key . 'y'}; | ||
| 1584 | $self->[2][1][2] = $cmf->{$key . 'z'}; | ||
| 1585 | |||
| 1586 | } | ||
| 1587 | |||
| 1588 | # compute color-weight functions | ||
| 1589 | _make_cwf($self); | ||
| 1590 | |||
| 1591 | # set object type | ||
| 1592 | $self->[0]{'type'} = 'CIE'; | ||
| 1593 | |||
| 1594 | } | ||
| 1595 | |||
| 1596 | # read ASTM color weight functions and white point | ||
| 1597 | # illuminant, observer, increment and bandpass specified in hash | ||
| 1598 | # parameters: (object_reference, hash) | ||
| 1599 | # results are saved in the object | ||
| 1600 | sub _astm { | ||
| 1601 | |||
| 1602 | # get parameters | ||
| 1603 | my ($self, $hash) = @_; | ||
| 1604 | |||
| 1605 | # local variables | ||
| 1606 | my ($table, $id, @bpc, @inc, @obs, @illum, @m, $nm); | ||
| 1607 | |||
| 1608 | # initialize id | ||
| 1609 | $id = 1; | ||
| 1610 | |||
| 1611 | # if increment key in hash | ||
| 1612 | if (defined($hash->{'increment'})) { | ||
| 1613 | |||
| 1614 | # enumerate ASTM increments | ||
| 1615 | @inc = qw(10 20); | ||
| 1616 | |||
| 1617 | # match observer value | ||
| 1618 | (@m = grep {$hash->{'increment'} eq $inc[$_]} (0 .. $#inc)) || warn('invalid increment value - using 10 nm'); | ||
| 1619 | |||
| 1620 | # adjust id for increment | ||
| 1621 | $id += @m ? $m[0] : 0; | ||
| 1622 | |||
| 1623 | } | ||
| 1624 | |||
| 1625 | # if observer key in hash | ||
| 1626 | if (defined($hash->{'observer'})) { | ||
| 1627 | |||
| 1628 | # enumerate ASTM observers | ||
| 1629 | @obs = qw(2 10); | ||
| 1630 | |||
| 1631 | # match observer value | ||
| 1632 | (@m = grep {$hash->{'observer'} eq $obs[$_]} (0 .. $#obs)) || warn('invalid observer value - using 1931 2º'); | ||
| 1633 | |||
| 1634 | # adjust id for observer | ||
| 1635 | $id += @m ? 2 * $m[0] : 0; | ||
| 1636 | |||
| 1637 | } | ||
| 1638 | |||
| 1639 | # if illuminant key in hash | ||
| 1640 | if (defined($hash->{'illuminant'})) { | ||
| 1641 | |||
| 1642 | # enumerate ASTM illuminants | ||
| 1643 | @illum = qw(A C D50 D55 D65 D75 F2 F7 F11); | ||
| 1644 | |||
| 1645 | # match illuminant value | ||
| 1646 | (@m = grep {$hash->{'illuminant'} eq $illum[$_]} (0 .. $#illum)) || warn('invalid illuminant value - using D50'); | ||
| 1647 | |||
| 1648 | # adjust id for illuminant | ||
| 1649 | $id += @m ? 4 * $m[0] : 8; | ||
| 1650 | |||
| 1651 | } else { | ||
| 1652 | |||
| 1653 | # adjust id for D50 illuminant | ||
| 1654 | $id += 8; | ||
| 1655 | |||
| 1656 | } | ||
| 1657 | |||
| 1658 | # set table: 5 (no bpc) -or- 6 (with bpc) | ||
| 1659 | # note: table 6 is deprecated in ASTM E 2729 | ||
| 1660 | $table = (defined($hash->{'bandpass'}) && $hash->{'bandpass'} eq 'six') ? 6 : 5; | ||
| 1661 | |||
| 1662 | # combine table and id | ||
| 1663 | $table .= '.' . $id; | ||
| 1664 | |||
| 1665 | # load ASTM weight functions (YAML format) | ||
| 1666 | state $ASTM = YAML::Tiny->read(ICC::Shared::getICCPath('Data/ASTM_E308_data.yml'))->[0]; | ||
| 1667 | |||
| 1668 | # get wavelength vector | ||
| 1669 | $nm = $ASTM->{$table . 'nm'}; | ||
| 1670 | |||
| 1671 | # set wavelength start, end, and increment | ||
| 1672 | $self->[3][0] = [$nm->[0], $nm->[-1], $nm->[1] - $nm->[0]]; | ||
| 1673 | |||
| 1674 | # set color-weight functions | ||
| 1675 | $self->[3][1][0] = $ASTM->{$table . 'x'}; | ||
| 1676 | $self->[3][1][1] = $ASTM->{$table . 'y'}; | ||
| 1677 | $self->[3][1][2] = $ASTM->{$table . 'z'}; | ||
| 1678 | |||
| 1679 | # set illuminant white point | ||
| 1680 | $self->[5] = $ASTM->{$table . 'wp'}; | ||
| 1681 | |||
| 1682 | # set object type | ||
| 1683 | $self->[0]{'type'} = 'ASTM'; | ||
| 1684 | |||
| 1685 | } | ||
| 1686 | |||
| 1687 | # read ISO 5-3 density weight functions | ||
| 1688 | # status and increment specified in hash | ||
| 1689 | # parameters: (object_reference, hash) | ||
| 1690 | # results are saved in the object | ||
| 1691 | sub _iso { | ||
| 1692 | |||
| 1693 | # get parameters | ||
| 1694 | my ($self, $hash) = @_; | ||
| 1695 | |||
| 1696 | # local variables | ||
| 1697 | my ($status, $inc, @m, $nm); | ||
| 1698 | |||
| 1699 | # initialize table selectors | ||
| 1700 | $status = 'T'; | ||
| 1701 | $inc = '10'; | ||
| 1702 | |||
| 1703 | # if status key in hash | ||
| 1704 | if (defined($hash->{'status'})) { | ||
| 1705 | |||
| 1706 | # match status value | ||
| 1707 | (@m = grep {$hash->{'status'} eq $_} qw(A M T E I)) || warn('invalid status value - using T'); | ||
| 1708 | |||
| 1709 | # set status | ||
| 1710 | $status = @m ? $m[0] : 'T'; | ||
| 1711 | |||
| 1712 | } | ||
| 1713 | |||
| 1714 | # if increment key in hash | ||
| 1715 | if (defined($hash->{'increment'})) { | ||
| 1716 | |||
| 1717 | # match observer value | ||
| 1718 | (@m = grep {$hash->{'increment'} eq $_} qw(10 20)) || warn('invalid increment value - using 10 nm'); | ||
| 1719 | |||
| 1720 | # set increment | ||
| 1721 | $inc = @m ? $m[0] : '10'; | ||
| 1722 | |||
| 1723 | } | ||
| 1724 | |||
| 1725 | # load ISO 5-3 weight functions (YAML format) | ||
| 1726 | state $ISO = YAML::Tiny->read(ICC::Shared::getICCPath('Data/ISO_5-3_data.yml'))->[0]; | ||
| 1727 | |||
| 1728 | # get wavelength vector | ||
| 1729 | $nm = $ISO->{$inc . 'nm'}; | ||
| 1730 | |||
| 1731 | # set wavelength start, end, and increment | ||
| 1732 | $self->[3][0] = [$nm->[0], $nm->[-1], $nm->[1] - $nm->[0]]; | ||
| 1733 | |||
| 1734 | # set density weight functions | ||
| 1735 | $self->[3][1][0] = $ISO->{$inc . $status . 'r'}; | ||
| 1736 | $self->[3][1][1] = $ISO->{$inc . $status . 'g'}; | ||
| 1737 | $self->[3][1][2] = $ISO->{$inc . $status . 'b'}; | ||
| 1738 | $self->[3][1][3] = $ISO->{$inc . 'vis'}; | ||
| 1739 | |||
| 1740 | # set density weight function sums | ||
| 1741 | $self->[5][0] = List::Util::sum(@{$self->[3][1][0]}); | ||
| 1742 | $self->[5][1] = List::Util::sum(@{$self->[3][1][1]}); | ||
| 1743 | $self->[5][2] = List::Util::sum(@{$self->[3][1][2]}); | ||
| 1744 | $self->[5][3] = List::Util::sum(@{$self->[3][1][3]}); | ||
| 1745 | |||
| 1746 | # set object type | ||
| 1747 | $self->[0]{'type'} = 'ISO'; | ||
| 1748 | |||
| 1749 | } | ||
| 1750 | |||
| 1751 | # compute vector smoothness | ||
| 1752 | # (returns 0 if vector is linear) | ||
| 1753 | # parameter: (vector) | ||
| 1754 | # returns: (linearity) | ||
| 1755 | sub _smoothness { | ||
| 1756 | |||
| 1757 | # get parameter | ||
| 1758 | my $v = shift(); | ||
| 1759 | |||
| 1760 | # local variables | ||
| 1761 | my ($d, $s); | ||
| 1762 | |||
| 1763 | # return if < 3 vector elements | ||
| 1764 | return(0) if (@{$v} < 3); | ||
| 1765 | |||
| 1766 | # for each triplet | ||
| 1767 | for my $i (0 .. $#{$v} - 2) { | ||
| 1768 | |||
| 1769 | # add linear deviation | ||
| 1770 | $d += (2 * $v->[$i + 1] - $v->[$i] - $v->[$i + 2])**2; | ||
| 1771 | |||
| 1772 | # add outer magnitudes | ||
| 1773 | $s += abs($v->[$i]) + abs($v->[$i + 2]); | ||
| 1774 | |||
| 1775 | } | ||
| 1776 | |||
| 1777 | # return relative rms value | ||
| 1778 | return($s ? sqrt($d)/$s : 0); | ||
| 1779 | |||
| 1780 | } | ||
| 1781 | |||
| 1782 | # get encoding CODE reference | ||
| 1783 | # parameter(object_reference, hash) | ||
| 1784 | # returns: (CODE_reference) | ||
| 1785 | sub _encoding { | ||
| 1786 | |||
| 1787 | # get parameters | ||
| 1788 | my ($self, $hash) = @_; | ||
| 1789 | |||
| 1790 | # local variables | ||
| 1791 | my ($encoding, $size); | ||
| 1792 | |||
| 1793 | # if hash contain 'encoding' value, but not 'XYZ', 'RGBV' or 'linear' | ||
| 1794 | if (defined($encoding = $hash->{'encoding'}) && $encoding !~ m/^(XYZ|RGBV|linear)$/) { | ||
| 1795 | |||
| 1796 | # get output size | ||
| 1797 | $size = @{$self->[3][1]}; | ||
| 1798 | |||
| 1799 | # if encoding is 'ICC_XYZ' | ||
| 1800 | if ($encoding eq 'ICC_XYZ' && $size == 3) { | ||
| 1801 | |||
| 1802 | # return code reference | ||
| 1803 | return(sub {map {$_ * 327.68/65535} @_}); | ||
| 1804 | |||
| 1805 | # if encoding is 'ICC_XYZNumber' | ||
| 1806 | } elsif ($encoding eq 'ICC_XYZNumber' && $size == 3) { | ||
| 1807 | |||
| 1808 | # return code reference | ||
| 1809 | return(sub {map {$_/100} @_}); | ||
| 1810 | |||
| 1811 | # if encoding is 'xyz' | ||
| 1812 | } elsif ($encoding eq 'xyz' && $size == 3) { | ||
| 1813 | |||
| 1814 | # verify white point | ||
| 1815 | ($self->[5][0] && $self->[5][1] && $self->[5][2]) or croak('invalid illuminant white point'); | ||
| 1816 | |||
| 1817 | # return code reference | ||
| 1818 | return(sub {$_[0]/$self->[5][0], $_[1]/$self->[5][1], $_[2]/$self->[5][2]}); | ||
| 1819 | |||
| 1820 | # if encoding is 'unit' | ||
| 1821 | } elsif ($encoding eq 'unit' && $size == 4) { | ||
| 1822 | |||
| 1823 | # return code reference | ||
| 1824 | return(sub {map {$_/100} @_}); | ||
| 1825 | |||
| 1826 | # if encoding is 'density' | ||
| 1827 | } elsif ($encoding eq 'density' && $size == 4) { | ||
| 1828 | |||
| 1829 | # return code reference | ||
| 1830 | return(sub {map {$_ > 0 ? -POSIX::log10($_/100) : 99} @_}); | ||
| 1831 | |||
| 1832 | } else { | ||
| 1833 | |||
| 1834 | # error | ||
| 1835 | croak('unsupported XYZ/RGBV encoding'); | ||
| 1836 | |||
| 1837 | } | ||
| 1838 | |||
| 1839 | } else { | ||
| 1840 | |||
| 1841 | # return empty | ||
| 1842 | return(); | ||
| 1843 | |||
| 1844 | } | ||
| 1845 | |||
| 1846 | } | ||
| 1847 | |||
| 1848 | # set object contents from parameter hash | ||
| 1849 | # parameters: (object_reference, ref_to_parameter_hash) | ||
| 1850 | sub _new_from_hash { | ||
| 1851 | |||
| 1852 | # get parameters | ||
| 1853 | my ($self, $hash) = @_; | ||
| 1854 | |||
| 1855 | # local variables | ||
| 1856 | my ($range, $ox); | ||
| 1857 | |||
| 1858 | # save colorimetry hash in object header | ||
| 1859 | $self->[0] = Storable::dclone($hash); | ||
| 1860 | |||
| 1861 | # if 'range' is defined | ||
| 1862 | if (defined($range = $hash->{'range'})) { | ||
| 1863 | |||
| 1864 | # verify wavelength range structure | ||
| 1865 | (ref($range) eq 'ARRAY' && 3 == @{$range} && (3 == grep {Scalar::Util::looks_like_number($_)} @{$range})) or croak('invalid wavelength range structure'); | ||
| 1866 | |||
| 1867 | # compute upper index | ||
| 1868 | $ox = ICC::Shared::round(($range->[1] - $range->[0])/$range->[2]); | ||
| 1869 | |||
| 1870 | # verify wavelength range values | ||
| 1871 | ($ox > 0 && abs($ox * $range->[2] - $range->[1] + $range->[0]) < 1E-12 && $range->[2] > 0) or croak('invalid wavelength range values'); | ||
| 1872 | |||
| 1873 | # add range to hash | ||
| 1874 | $srh->{$ox + 1} = $range; | ||
| 1875 | |||
| 1876 | } | ||
| 1877 | |||
| 1878 | # if 'status' is defined, a scalar | ||
| 1879 | if (defined($hash->{'status'}) && ! ref($hash->{'status'})) { | ||
| 1880 | |||
| 1881 | # read ISO 5-3 color-weight functions (density) | ||
| 1882 | _iso($self, $hash); | ||
| 1883 | |||
| 1884 | # if 'illuminant' is defined, an ARRAY ref | ||
| 1885 | } elsif (defined($hash->{'illuminant'}) && ref($hash->{'illuminant'}) eq 'ARRAY') { | ||
| 1886 | |||
| 1887 | # read CIE illuminant and color-matching functions, compute color-weight functions | ||
| 1888 | _cie($self, $hash); | ||
| 1889 | |||
| 1890 | } else { | ||
| 1891 | |||
| 1892 | # read ASTM color-weight functions | ||
| 1893 | _astm($self, $hash); | ||
| 1894 | |||
| 1895 | } | ||
| 1896 | |||
| 1897 | } | ||
| 1898 | |||
| 1899 | 1; | ||
| 1900 |