| 1 | package ICC::Support::Chart; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Carp; | ||
| 5 | |||
| 6 | our $VERSION = 1.71; | ||
| 7 | |||
| 8 | # revised 2018-03-29 | ||
| 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 Config; | ||
| 20 | use Data::Dumper; | ||
| 21 | use Encode; | ||
| 22 | use File::Glob; | ||
| 23 | use POSIX (); | ||
| 24 | use Time::Piece; | ||
| 25 | use XML::LibXML; | ||
| 26 | |||
| 27 | # enable static variables | ||
| 28 | use feature 'state'; | ||
| 29 | |||
| 30 | # create new chart object | ||
| 31 | # parameters: ([hash]) | ||
| 32 | # parameters: (ref_to_data_array, [hash]) | ||
| 33 | # parameters: (path_to_file, [hash]) | ||
| 34 | # parameters: (path_to_folder, [hash]) | ||
| 35 | # returns: (ref_to_chart_object) -or- (ref_to_chart_object, error_string) | ||
| 36 | sub new { | ||
| 37 | |||
| 38 | # get object class | ||
| 39 | my $class = shift(); | ||
| 40 | |||
| 41 | # local variables | ||
| 42 | my ($self, $hash, $array, $format, $offset, $path, $files, $result, $error); | ||
| 43 | |||
| 44 | # create empty chart object | ||
| 45 | $self = [ | ||
| 46 | {}, # object header | ||
| 47 | [[]], # chart data | ||
| 48 | [[]], # colorimetry data | ||
| 49 | [], # header lines | ||
| 50 | {}, # SAMPLE_ID hash | ||
| 51 | ]; | ||
| 52 | |||
| 53 | # get optional hash | ||
| 54 | $hash = pop() if (ref($_[-1]) eq 'HASH'); | ||
| 55 | |||
| 56 | # if there are additional parameters | ||
| 57 | if (@_) { | ||
| 58 | |||
| 59 | # if first parameter is an array or a Math::Matrix object | ||
| 60 | if (ref($_[0]) eq 'ARRAY' || UNIVERSAL::isa($_[0], 'Math::Matrix')) { | ||
| 61 | |||
| 62 | # get array reference | ||
| 63 | $array = shift(); | ||
| 64 | |||
| 65 | # get format header from hash | ||
| 66 | $format = $hash->{'format'}; | ||
| 67 | |||
| 68 | # copy format header to object, if defined | ||
| 69 | $self->[1][0] = [@{$format}] if defined($format); | ||
| 70 | |||
| 71 | # set array offset | ||
| 72 | $offset = defined($format) ? 1 : 0; | ||
| 73 | |||
| 74 | # for each row | ||
| 75 | for my $i (0 .. $#{$array}) { | ||
| 76 | |||
| 77 | # copy array | ||
| 78 | $self->[1][$i + $offset] = [@{$array->[$i]}]; | ||
| 79 | |||
| 80 | } | ||
| 81 | |||
| 82 | # if first parameter is a scalar | ||
| 83 | } elsif (! ref($_[0])) { | ||
| 84 | |||
| 85 | # get path | ||
| 86 | $path = shift(); | ||
| 87 | |||
| 88 | # save path in object header | ||
| 89 | $self->[0]{'file_path'} = $path; | ||
| 90 | |||
| 91 | # get file list | ||
| 92 | $files = _files($path); | ||
| 93 | |||
| 94 | # no files | ||
| 95 | if (@{$files} == 0) { | ||
| 96 | |||
| 97 | # invalid path | ||
| 98 | carp($error = "no files in path: $path\n"); | ||
| 99 | |||
| 100 | # one file | ||
| 101 | } elsif (@{$files} == 1) { | ||
| 102 | |||
| 103 | # read chart | ||
| 104 | ($result = _readChart($self, $files->[0], $hash)) && carp($error = "chart $files->[0] $result\n"); | ||
| 105 | |||
| 106 | # add colorimetric metadata | ||
| 107 | _addColorMeta($self); | ||
| 108 | |||
| 109 | # multiple files | ||
| 110 | } else { | ||
| 111 | |||
| 112 | # if folder handling undefined or 'AVG' | ||
| 113 | if (! defined($hash->{'folder'}) || $hash->{'folder'} eq 'AVG') { | ||
| 114 | |||
| 115 | # read average chart | ||
| 116 | _readChartAvg($self, $files, $hash) || carp($error = "no valid charts in path: $path\n"); | ||
| 117 | |||
| 118 | # if folder handling 'APPEND' | ||
| 119 | } elsif ($hash->{'folder'} eq 'APPEND') { | ||
| 120 | |||
| 121 | # read appended chart | ||
| 122 | _readChartAppend($self, $files, $hash) || carp($error = "no valid charts in path: $path\n"); | ||
| 123 | |||
| 124 | } else { | ||
| 125 | |||
| 126 | # invalid folder handling | ||
| 127 | carp($error = "invalid folder handling: $hash->{'folder'}\n"); | ||
| 128 | |||
| 129 | } | ||
| 130 | |||
| 131 | } | ||
| 132 | |||
| 133 | } else { | ||
| 134 | |||
| 135 | # invalid parameter(s) | ||
| 136 | carp($error = "invalid parameter(s)"); | ||
| 137 | |||
| 138 | } | ||
| 139 | |||
| 140 | # make SAMPLE_ID hash | ||
| 141 | _makeSampleID($self); | ||
| 142 | |||
| 143 | # if hash defined | ||
| 144 | } elsif (defined($hash)) { | ||
| 145 | |||
| 146 | # make patch set | ||
| 147 | ($result = _makePatchSet($self, $hash)) && carp($error = "failed making patch set - $result\n"); | ||
| 148 | |||
| 149 | } | ||
| 150 | |||
| 151 | # bless object | ||
| 152 | bless($self, $class); | ||
| 153 | |||
| 154 | # return | ||
| 155 | return(wantarray() ? ($self, $error) : $self); | ||
| 156 | |||
| 157 | } | ||
| 158 | |||
| 159 | # get/set reference to header hash | ||
| 160 | # parameters: ([ref_to_new_hash]) | ||
| 161 | # returns: (ref_to_hash) | ||
| 162 | sub header { | ||
| 163 | |||
| 164 | # get object reference | ||
| 165 | my $self = shift(); | ||
| 166 | |||
| 167 | # if there are parameters | ||
| 168 | if (@_) { | ||
| 169 | |||
| 170 | # if one parameter, a hash reference | ||
| 171 | if (@_ == 1 && ref($_[0]) eq 'HASH') { | ||
| 172 | |||
| 173 | # set header to copy of hash | ||
| 174 | $self->[0] = {%{shift()}}; | ||
| 175 | |||
| 176 | } else { | ||
| 177 | |||
| 178 | # error | ||
| 179 | croak('parameter must be a hash reference'); | ||
| 180 | |||
| 181 | } | ||
| 182 | |||
| 183 | } | ||
| 184 | |||
| 185 | # return reference | ||
| 186 | return($self->[0]); | ||
| 187 | |||
| 188 | } | ||
| 189 | |||
| 190 | # get/set reference to data array | ||
| 191 | # note: row 0 contains the DATA_FORMAT field names | ||
| 192 | # note: set updates the SAMPLE_ID hash and colorimetry array | ||
| 193 | # parameters: ([ref_to_new_array]) | ||
| 194 | # returns: (ref_to_array) | ||
| 195 | sub array { | ||
| 196 | |||
| 197 | # get object reference | ||
| 198 | my $self = shift(); | ||
| 199 | |||
| 200 | # if there are parameters | ||
| 201 | if (@_) { | ||
| 202 | |||
| 203 | # if one parameter, an array reference | ||
| 204 | if (@_ == 1 && ref($_[0]) eq 'ARRAY') { | ||
| 205 | |||
| 206 | # get array reference | ||
| 207 | my $array = shift(); | ||
| 208 | |||
| 209 | # initialize data array | ||
| 210 | $self->[1] = []; | ||
| 211 | |||
| 212 | # if array is not empty | ||
| 213 | if (@{$array}) { | ||
| 214 | |||
| 215 | # for each row | ||
| 216 | for my $i (0 .. $#{$array}) { | ||
| 217 | |||
| 218 | # copy to object | ||
| 219 | $self->[1][$i] = [@{$array->[$i]}]; | ||
| 220 | |||
| 221 | } | ||
| 222 | |||
| 223 | # make SAMPLE_ID hash | ||
| 224 | _makeSampleID($self); | ||
| 225 | |||
| 226 | # add colorimetric metadata | ||
| 227 | _addColorMeta($self); | ||
| 228 | |||
| 229 | } | ||
| 230 | |||
| 231 | } else { | ||
| 232 | |||
| 233 | # error | ||
| 234 | croak('parameter must be an array reference'); | ||
| 235 | |||
| 236 | } | ||
| 237 | |||
| 238 | } | ||
| 239 | |||
| 240 | # return reference | ||
| 241 | return($self->[1]); | ||
| 242 | |||
| 243 | } | ||
| 244 | |||
| 245 | # get data array size | ||
| 246 | # returns: (number_rows) | ||
| 247 | # returns: (number_rows, number_columns) | ||
| 248 | sub size { | ||
| 249 | |||
| 250 | # get object reference | ||
| 251 | my $self = shift(); | ||
| 252 | |||
| 253 | # return array or scalar | ||
| 254 | return(wantarray ? ($#{$self->[1]}, $#{$self->[1][0]} + 1) : $#{$self->[1]}); | ||
| 255 | |||
| 256 | } | ||
| 257 | |||
| 258 | # get data matrix size | ||
| 259 | # returns: (number_rows) | ||
| 260 | # returns: (number_rows, number_columns) | ||
| 261 | sub matrix_size { | ||
| 262 | |||
| 263 | # get object reference | ||
| 264 | my $self = shift(); | ||
| 265 | |||
| 266 | # get row length from data | ||
| 267 | my $rows = _getRowLength($self); | ||
| 268 | |||
| 269 | # compute columns | ||
| 270 | my $cols = $rows ? POSIX::ceil($#{$self->[1]}/$rows) : 0; | ||
| 271 | |||
| 272 | # return array or scalar | ||
| 273 | wantarray ? return($rows, $cols) : return($rows); | ||
| 274 | |||
| 275 | } | ||
| 276 | |||
| 277 | # get row slice from SAMPLE_ID values | ||
| 278 | # id_keys is a list of scalars and/or array references | ||
| 279 | # row_slice is reference to an array of row indices | ||
| 280 | # note: returns undef if any key is missing | ||
| 281 | # parameters: (id_keys) | ||
| 282 | # returns: (row_slice) | ||
| 283 | sub rows { | ||
| 284 | |||
| 285 | # get object reference | ||
| 286 | my $self = shift(); | ||
| 287 | |||
| 288 | # local variable | ||
| 289 | my (@keys, @rows); | ||
| 290 | |||
| 291 | # flatten id key list | ||
| 292 | @keys = @{ICC::Shared::flatten(@_)}; | ||
| 293 | |||
| 294 | # get row list using SAMPLE_ID hash | ||
| 295 | @rows = @{$self->[4]}{@keys}; | ||
| 296 | |||
| 297 | # return row slice or undef if any rows are missing | ||
| 298 | return((grep {! defined()} @rows) ? undef : \@rows); | ||
| 299 | |||
| 300 | } | ||
| 301 | |||
| 302 | # get column slice from DATA_FORMAT keys | ||
| 303 | # format_keys is a list of scalars and/or array references | ||
| 304 | # column_slice is reference to an array of column indices | ||
| 305 | # note: tries to match ignoring context if exact match fails | ||
| 306 | # note: returns 'undef' if any column is missing | ||
| 307 | # parameters: (format_keys) | ||
| 308 | # returns: (column_slice) | ||
| 309 | sub cols { | ||
| 310 | |||
| 311 | # get object reference | ||
| 312 | my $self = shift(); | ||
| 313 | |||
| 314 | # local variables | ||
| 315 | my (@keys, %fmt, @cols); | ||
| 316 | |||
| 317 | # flatten format key list | ||
| 318 | @keys = @{ICC::Shared::flatten(@_)}; | ||
| 319 | |||
| 320 | # make lookup hash | ||
| 321 | %fmt = map {defined($self->[1][0][$_]) ? ($self->[1][0][$_], $_) : ()} (0 .. $#{$self->[1][0]}); | ||
| 322 | |||
| 323 | # lookup format keys in hash | ||
| 324 | @cols = @fmt{@keys}; | ||
| 325 | |||
| 326 | # if any columns undefined | ||
| 327 | if (grep {! defined()} @cols) { | ||
| 328 | |||
| 329 | # make lookup hash without context prefixes | ||
| 330 | %fmt = map {(defined($self->[1][0][$_]) && $self->[1][0][$_] =~ m/^(.*?)\|?([^\|\n]*)$/) ? ($2, $_) : ()} (0 .. $#{$self->[1][0]}); | ||
| 331 | |||
| 332 | # lookup format keys in hash | ||
| 333 | @cols = @fmt{@keys}; | ||
| 334 | |||
| 335 | } | ||
| 336 | |||
| 337 | # return column slice or undef if any columns undefined | ||
| 338 | return((grep {! defined()} @cols) ? undef : \@cols); | ||
| 339 | |||
| 340 | } | ||
| 341 | |||
| 342 | # get DATA_FORMAT keys from column slice | ||
| 343 | # column_slice is a list of scalars and/or array references | ||
| 344 | # format_keys is an array reference | ||
| 345 | # note: returns 'undef' if any key is missing | ||
| 346 | # parameters: (column_slice) | ||
| 347 | # returns: (format_keys) | ||
| 348 | sub fmt_keys { | ||
| 349 | |||
| 350 | # get object reference | ||
| 351 | my $self = shift(); | ||
| 352 | |||
| 353 | # local variable | ||
| 354 | my (@keys); | ||
| 355 | |||
| 356 | # if column slice an empty array reference ([]) | ||
| 357 | if (@_ == 1 && ref($_[0]) eq 'ARRAY' && @{$_[0]} == 0) { | ||
| 358 | |||
| 359 | # get all format keys | ||
| 360 | @keys = @{$self->[1][0]}; | ||
| 361 | |||
| 362 | } else { | ||
| 363 | |||
| 364 | # get format keys per flattened slice | ||
| 365 | @keys = map {$self->[1][0][$_]} @{ICC::Shared::flatten(@_)}; | ||
| 366 | |||
| 367 | } | ||
| 368 | |||
| 369 | # return format keys or undef if any keys are missing | ||
| 370 | return((grep {! defined()} @keys) ? undef : \@keys); | ||
| 371 | |||
| 372 | } | ||
| 373 | |||
| 374 | # get/set context | ||
| 375 | # 'undef' indicates no context (get or set) | ||
| 376 | # returned context may be a scalar or an array | ||
| 377 | # parameter: (column_slice) => returns: (context) | ||
| 378 | # parameters: (column_slice, context) => returns: (modified_keys) | ||
| 379 | sub context { | ||
| 380 | |||
| 381 | # get object reference | ||
| 382 | my $self = shift(); | ||
| 383 | |||
| 384 | # local variables | ||
| 385 | my ($cols, $context, @cx); | ||
| 386 | |||
| 387 | # return if no parameters supplied | ||
| 388 | return(undef) if (@_ == 0); | ||
| 389 | |||
| 390 | # get column slice | ||
| 391 | $cols = ICC::Shared::flatten(shift()); | ||
| 392 | |||
| 393 | # use all columns if slice is empty | ||
| 394 | $cols = [0 .. $#{$self->[1][0]}] if (@{$cols} == 0); | ||
| 395 | |||
| 396 | # if no context parameter | ||
| 397 | if (@_ == 0) { | ||
| 398 | |||
| 399 | # match contexts | ||
| 400 | @cx = map {$self->[1][0][$_] =~ m/^(.*)\|/ ? $1 : undef} @{$cols}; | ||
| 401 | |||
| 402 | # return array if wanted | ||
| 403 | return(@cx) if (wantarray); | ||
| 404 | |||
| 405 | # warn if columns have different contexts | ||
| 406 | (@cx == grep {(! defined($cx[0]) && ! defined($_)) || ($cx[0] eq $_)} @cx) || warn('columns have different contexts'); | ||
| 407 | |||
| 408 | # return context of first column | ||
| 409 | return($cx[0]); | ||
| 410 | |||
| 411 | } else { | ||
| 412 | |||
| 413 | # get context | ||
| 414 | $context = shift(); | ||
| 415 | |||
| 416 | # for each column | ||
| 417 | for my $i (0 .. $#{$cols}) { | ||
| 418 | |||
| 419 | # if context is defined | ||
| 420 | if (defined($context)) { | ||
| 421 | |||
| 422 | # replace current context | ||
| 423 | $self->[1][0][$cols->[$i]] =~ s/^(?:.*\|)?(.*)$/$context\|$1/; | ||
| 424 | |||
| 425 | # if context is 'undef' | ||
| 426 | } else { | ||
| 427 | |||
| 428 | # remove current context | ||
| 429 | $self->[1][0][$cols->[$i]] =~ s/^.*\|//; | ||
| 430 | |||
| 431 | } | ||
| 432 | |||
| 433 | } | ||
| 434 | |||
| 435 | # return modified keys | ||
| 436 | return([@{$self->[1][0]}[@{$cols}]]); | ||
| 437 | |||
| 438 | } | ||
| 439 | |||
| 440 | } | ||
| 441 | |||
| 442 | # test for a specified data class | ||
| 443 | # returns list of matched indices or count | ||
| 444 | # context parameter of '|' matches fields with no context | ||
| 445 | # parameters: (class, [context]) | ||
| 446 | # returns: (list -or- count) | ||
| 447 | sub test { | ||
| 448 | |||
| 449 | # get parameters | ||
| 450 | my ($self, $class, $context) = @_; | ||
| 451 | |||
| 452 | # local variables | ||
| 453 | my (%regex, @fields); | ||
| 454 | |||
| 455 | # hash of compiled regex | ||
| 456 | %regex = ( | ||
| 457 | 'RGB' => qr/^(?:(.*)\|)?RGB_[RGB]$/, | ||
| 458 | 'CMYK' => qr/^(?:(.*)\|)?CMYK_[CMYK]$/, | ||
| 459 | 'XYZ' => qr/^(?:(.*)\|)?XYZ_[XYZ]$/, | ||
| 460 | 'XYY' => qr/^(?:(.*)\|)?XYY_(?:X|Y|CAPY)$/, | ||
| 461 | 'LAB' => qr/^(?:(.*)\|)?LAB_[LAB]$/, | ||
| 462 | 'LCH' => qr/^(?:(.*)\|)?LAB_[LCH]$/, | ||
| 463 | 'NCLR' => qr/^(?:(.*)\|)?[2-9A-F]CLR_[1-9A-F]$/, | ||
| 464 | 'SPECTRAL' => qr/^(?:(.*)\|)?(?:nm|SPECTRAL_NM_|SPECTRAL_NM|SPECTRAL_|NM_|R_)\d{3}$/, | ||
| 465 | 'SPOT' => qr/^(?:(.*)\|)?SPOT_\d+$/, | ||
| 466 | 'DENSITY' => qr/^(?:(.*)\|)?D_(?:RED|GREEN|BLUE|VIS)$/, | ||
| 467 | 'STDEVXYZ' => qr/^(?:(.*)\|)?STDEV_[XYZ]$/, | ||
| 468 | 'STDEVLAB' => qr/^(?:(.*)\|)?STDEV_[LAB]$/, | ||
| 469 | 'MEAN_DE' => qr/^(?:(.*)\|)?MEAN_DE$/, | ||
| 470 | 'ID' => qr/^(?:(.*)\|)?(?:SAMPLE_ID|SampleID)$/, | ||
| 471 | 'NAME' => qr/^(?:(.*)\|)?SAMPLE_NAME$/, | ||
| 472 | 'DEVICE' => qr/^(?:(.*)\|)?(?:RGB_[RGB]|CMYK_[CMYK]|[2-9A-F]CLR_[1-9A-F])$/, | ||
| 473 | ); | ||
| 474 | |||
| 475 | # verify class | ||
| 476 | (! ref($class) && exists($regex{$class})) || croak('invalid data class'); | ||
| 477 | |||
| 478 | # if context is undefined | ||
| 479 | if (! defined($context)) { | ||
| 480 | |||
| 481 | # match format fields (ignoring context) | ||
| 482 | @fields = grep {$self->[1][0][$_] =~ /$regex{$class}/} (0 .. $#{$self->[1][0]}); | ||
| 483 | |||
| 484 | # if context is '|' | ||
| 485 | } elsif ($context eq '|') { | ||
| 486 | |||
| 487 | # match format fields (no context) | ||
| 488 | @fields = grep {$self->[1][0][$_] =~ /$regex{$class}/ && ! defined($1)} (0 .. $#{$self->[1][0]}); | ||
| 489 | |||
| 490 | } else { | ||
| 491 | |||
| 492 | # match format fields (matching context) | ||
| 493 | @fields = grep {$self->[1][0][$_] =~ /$regex{$class}/ && defined($1) && ($1 eq $context)} (0 .. $#{$self->[1][0]}); | ||
| 494 | |||
| 495 | } | ||
| 496 | |||
| 497 | # return (list -or- count) | ||
| 498 | return(wantarray() ? @fields : scalar(@fields)); | ||
| 499 | |||
| 500 | } | ||
| 501 | |||
| 502 | # get/set keyword value(s) | ||
| 503 | # CGATS ASCII file header lines are stored as an array in the object header | ||
| 504 | # most lines contain a keyword followed by a value, which this methods gets/sets | ||
| 505 | # a keyword may be used more than once, so the value parameter is an array | ||
| 506 | # if the keyword doesn't exist, a new line is added when setting its value | ||
| 507 | # if the keyword is enclosed by angle brackets, existing lines are removed | ||
| 508 | # parameters: () => returns: (file_header_array_reference) | ||
| 509 | # parameters: (keyword) => returns: (value_array) | ||
| 510 | # parameters: (keyword, value_array) => returns: (original_value_array) | ||
| 511 | sub keyword { | ||
| 512 | |||
| 513 | # get parameters | ||
| 514 | my ($self, $key, @values) = @_; | ||
| 515 | |||
| 516 | # local variables | ||
| 517 | my ($del, @ix, @current); | ||
| 518 | |||
| 519 | # if no keyword, return file header array reference | ||
| 520 | (defined($key)) || return($self->[3]); | ||
| 521 | |||
| 522 | # set delete flag, stripping angle brackets (if any) | ||
| 523 | $del = ($key =~ s/^<(.*)>$/$1/); | ||
| 524 | |||
| 525 | # get indices of existing keyword (if any) | ||
| 526 | @ix = grep {$self->[3][$_][0] eq $key} (0 .. $#{$self->[3]}); | ||
| 527 | |||
| 528 | # get current values array (if any) | ||
| 529 | @current = map {$self->[3][$_][1]} @ix; | ||
| 530 | |||
| 531 | # if delete flag set | ||
| 532 | if ($del) { | ||
| 533 | |||
| 534 | # while indices | ||
| 535 | while (@ix) { | ||
| 536 | |||
| 537 | # delete array element | ||
| 538 | splice(@{$self->[3]}, pop(@ix), 1); | ||
| 539 | |||
| 540 | } | ||
| 541 | |||
| 542 | } | ||
| 543 | |||
| 544 | # if there are supplied values | ||
| 545 | if (@values) { | ||
| 546 | |||
| 547 | # for each value | ||
| 548 | for (@values) { | ||
| 549 | |||
| 550 | # if not a number or already quoted | ||
| 551 | if (! m/^([\d.-]+|".*")$/) { | ||
| 552 | |||
| 553 | # remove any quotes | ||
| 554 | s/"//g; | ||
| 555 | |||
| 556 | # enclose in quotes | ||
| 557 | $_ = "\"$_\""; | ||
| 558 | |||
| 559 | } | ||
| 560 | |||
| 561 | } | ||
| 562 | |||
| 563 | # while indices and values | ||
| 564 | while (@ix && @values) { | ||
| 565 | |||
| 566 | # set keyword/value entry | ||
| 567 | $self->[3][shift(@ix)] = [$key, shift(@values)]; | ||
| 568 | |||
| 569 | } | ||
| 570 | |||
| 571 | # for each remaining value (if any) | ||
| 572 | for (@values) { | ||
| 573 | |||
| 574 | # add keyword/value entry | ||
| 575 | push(@{$self->[3]}, [$key, $_]); | ||
| 576 | |||
| 577 | } | ||
| 578 | |||
| 579 | } | ||
| 580 | |||
| 581 | # return current values array, or scalar | ||
| 582 | return(wantarray ? @current : $current[0]); | ||
| 583 | |||
| 584 | } | ||
| 585 | |||
| 586 | # get/set CREATED value | ||
| 587 | # adds CREATED keyword when setting, if none | ||
| 588 | # parameters: () # gets date/time from CREATED value | ||
| 589 | # parameters: (string) # sets/adds CREATED keyword/value | ||
| 590 | # parameters: (Time::Piece_object) # sets/adds CREATED keyword/value | ||
| 591 | # returns: (Time::Piece_object) # default is localtime | ||
| 592 | sub created { | ||
| 593 | |||
| 594 | # get parameters | ||
| 595 | my ($self, $t) = @_; | ||
| 596 | |||
| 597 | # local variables | ||
| 598 | my (@ix, $datetime); | ||
| 599 | |||
| 600 | # get indices of existing CREATED lines (if any) | ||
| 601 | @ix = grep {$self->[3][$_][0] eq 'CREATED'} (0 .. $#{$self->[3]}); | ||
| 602 | |||
| 603 | # print warning if more than one CREATED line | ||
| 604 | print "warning: more than one CREATED keyword\n" if (@ix > 1); | ||
| 605 | |||
| 606 | # if date/time parameter given | ||
| 607 | if (defined($t)) { | ||
| 608 | |||
| 609 | # make Time::Piece object if reference is a scalar | ||
| 610 | $t = _makeTimePiece($t) if (! ref($t)); | ||
| 611 | |||
| 612 | # if not a Time::Piece object | ||
| 613 | if (ref($t) ne 'Time::Piece') { | ||
| 614 | |||
| 615 | # print warning | ||
| 616 | print "warning: invalid date/time parameter, using localtime instead\n"; | ||
| 617 | |||
| 618 | # use local time | ||
| 619 | $t = localtime(); | ||
| 620 | |||
| 621 | } | ||
| 622 | |||
| 623 | # make ISO 8601 datetime string from Time::Piece object | ||
| 624 | $datetime = $t->strftime('%Y-%m-%dT%T%z'); | ||
| 625 | substr($datetime, -2, 0, ':'); | ||
| 626 | |||
| 627 | # if CREATED lines | ||
| 628 | if (@ix) { | ||
| 629 | |||
| 630 | # replace value in first CREATED line | ||
| 631 | $self->[3][$ix[0]][1] = "\"$datetime\""; | ||
| 632 | |||
| 633 | } else { | ||
| 634 | |||
| 635 | # if keyword lines exist | ||
| 636 | if (@{$self->[3]}) { | ||
| 637 | |||
| 638 | # insert CREATED line (as second line) | ||
| 639 | splice(@{$self->[3]}, 1, 0, ['CREATED', "\"$datetime\""]); | ||
| 640 | |||
| 641 | } else { | ||
| 642 | |||
| 643 | # add CREATED line | ||
| 644 | $self->[3][0] = ['CREATED', "\"$datetime\""]; | ||
| 645 | |||
| 646 | } | ||
| 647 | |||
| 648 | } | ||
| 649 | |||
| 650 | # no parameter | ||
| 651 | } else { | ||
| 652 | |||
| 653 | # if CREATED lines | ||
| 654 | if (@ix) { | ||
| 655 | |||
| 656 | # make Time::Piece object from first CREATED value | ||
| 657 | $t = _makeTimePiece($self->[3][$ix[0]][1]); | ||
| 658 | |||
| 659 | } else { | ||
| 660 | |||
| 661 | # print warning | ||
| 662 | print "warning: no CREATED keyword, returning localtime instead\n"; | ||
| 663 | |||
| 664 | # use local time | ||
| 665 | $t = localtime(); | ||
| 666 | |||
| 667 | } | ||
| 668 | |||
| 669 | } | ||
| 670 | |||
| 671 | # return Time::Piece object | ||
| 672 | return($t); | ||
| 673 | |||
| 674 | } | ||
| 675 | |||
| 676 | # get/set data array slice | ||
| 677 | # row_slice and column_slice may be either a scalar or array reference | ||
| 678 | # replacement_data is reference to a 2-D array of replacement values | ||
| 679 | # replacement data dimensions must match size of row_slice and column_slice | ||
| 680 | # data_slice is reference to a 2-D array, selected by row_slice and column_slice | ||
| 681 | # parameters: ([row_slice, [column_slice, [replacement_data]]]) | ||
| 682 | # return: (data_slice) | ||
| 683 | sub slice { | ||
| 684 | |||
| 685 | # get parameters | ||
| 686 | my ($self, $rows, $cols, $data) = @_; | ||
| 687 | |||
| 688 | # select all rows if row slice undefined | ||
| 689 | $rows = [] if (! defined($rows)); | ||
| 690 | |||
| 691 | # select all fields if column slice undefined | ||
| 692 | $cols = [] if (! defined($cols)); | ||
| 693 | |||
| 694 | # call get/set subroutine | ||
| 695 | _getset($self, 1, $rows, $cols, $data); | ||
| 696 | |||
| 697 | } | ||
| 698 | |||
| 699 | # get/set colorimetry array slice | ||
| 700 | # row_slice and column_slice may be either a scalar or array reference | ||
| 701 | # replacement_data is reference to a 2-D array of replacement values | ||
| 702 | # replacement data dimensions must match size of row_slice and column_slice | ||
| 703 | # data_slice is reference to a 2-D array, selected by row_slice and column_slice | ||
| 704 | # parameters: ([row_slice, [column_slice, [replacement_data]]]) | ||
| 705 | # return: (data_slice) | ||
| 706 | sub colorimetry { | ||
| 707 | |||
| 708 | # get parameters | ||
| 709 | my ($self, $rows, $cols, $data) = @_; | ||
| 710 | |||
| 711 | # flatten row slice | ||
| 712 | $rows = defined($rows) ? ICC::Shared::flatten($rows) : []; | ||
| 713 | |||
| 714 | # select all rows if row slice empty | ||
| 715 | $rows = [0 .. $#{$self->[2]}] if (@{$rows} == 0); | ||
| 716 | |||
| 717 | # flatten column slice | ||
| 718 | $cols = defined($cols) ? ICC::Shared::flatten($cols) : []; | ||
| 719 | |||
| 720 | # select all fields if column slice empty | ||
| 721 | $cols = [0 .. $#{$self->[1][0]}] if (@{$cols} == 0); | ||
| 722 | |||
| 723 | # call get/set subroutine | ||
| 724 | _getset($self, 2, $rows, $cols, $data); | ||
| 725 | |||
| 726 | } | ||
| 727 | |||
| 728 | # get/set SAMPLE_ID data | ||
| 729 | # optional hash contains supplementary parameters | ||
| 730 | # row_slice and column_slice are 1-D array references | ||
| 731 | # data_slice is a Math::Matrix object (2-D array) | ||
| 732 | # replacement_data is a Math::Matrix object or 2-D array | ||
| 733 | # parameters: ([hash]) => returns: (column_slice) | ||
| 734 | # parameters: (row_slice, [hash]) => returns: (data_slice) | ||
| 735 | # parameters: (row_slice, replacement_data, [hash]) => returns: (column_slice) | ||
| 736 | sub id { | ||
| 737 | |||
| 738 | # local variables | ||
| 739 | my ($hash, %fmt, $cols); | ||
| 740 | |||
| 741 | # get optional hash | ||
| 742 | $hash = pop() if (ref($_[-1]) eq 'HASH'); | ||
| 743 | |||
| 744 | # get remaining parameters | ||
| 745 | my ($self, $rows, $data) = @_; | ||
| 746 | |||
| 747 | # make lookup hash (context| -or- '||' => column) | ||
| 748 | %fmt = map {($self->[1][0][$_] =~ m/^(.*\|)?(?:SAMPLE_ID|SampleID|ID)$/) ? (defined($1) ? $1 : '||', $_) : ()} (0 .. $#{$self->[1][0]}); | ||
| 749 | |||
| 750 | # if context defined | ||
| 751 | if (defined($hash->{'context'})) { | ||
| 752 | |||
| 753 | # get id column with context | ||
| 754 | $cols = $fmt{"$hash->{'context'}|"}; | ||
| 755 | |||
| 756 | } else { | ||
| 757 | |||
| 758 | # get id column without context | ||
| 759 | $cols = $fmt{'||'}; | ||
| 760 | |||
| 761 | # if id column undefined | ||
| 762 | if (! defined($cols)) { | ||
| 763 | |||
| 764 | # make lookup hash ignoring context ('||' => column) | ||
| 765 | %fmt = map {($self->[1][0][$_] =~ m/^(?:.*\|)?(?:SAMPLE_ID|SampleID|ID)$/) ? ('||', $_) : ()} (0 .. $#{$self->[1][0]}); | ||
| 766 | |||
| 767 | # get id column | ||
| 768 | $cols = $fmt{'||'}; | ||
| 769 | |||
| 770 | } | ||
| 771 | |||
| 772 | } | ||
| 773 | |||
| 774 | # call get/set subroutine | ||
| 775 | _getset($self, 1, $rows, $cols, $data); | ||
| 776 | |||
| 777 | } | ||
| 778 | |||
| 779 | # get/set SAMPLE_NAME data | ||
| 780 | # optional hash contains supplementary parameters | ||
| 781 | # row_slice and column_slice are 1-D array references | ||
| 782 | # data_slice is a Math::Matrix object (2-D array) | ||
| 783 | # replacement_data is a Math::Matrix object or 2-D array | ||
| 784 | # parameters: ([hash]) => returns: (column_slice) | ||
| 785 | # parameters: (row_slice, [hash]) => returns: (data_slice) | ||
| 786 | # parameters: (row_slice, replacement_data, [hash]) => returns: (column_slice) | ||
| 787 | sub name { | ||
| 788 | |||
| 789 | # local variables | ||
| 790 | my ($hash, $cols); | ||
| 791 | |||
| 792 | # get optional hash | ||
| 793 | $hash = pop() if (ref($_[-1]) eq 'HASH'); | ||
| 794 | |||
| 795 | # get remaining parameters | ||
| 796 | my ($self, $rows, $data) = @_; | ||
| 797 | |||
| 798 | # get column slice, adding optional context prefix | ||
| 799 | $cols = cols($self, defined($hash->{'context'}) ? "$hash->{'context'}|SAMPLE_NAME" : 'SAMPLE_NAME'); | ||
| 800 | |||
| 801 | # call get/set subroutine | ||
| 802 | _getset($self, 1, $rows, $cols, $data); | ||
| 803 | |||
| 804 | } | ||
| 805 | |||
| 806 | # get/set RGB data | ||
| 807 | # optional hash contains supplementary parameters | ||
| 808 | # row_slice and column_slice are 1-D array references | ||
| 809 | # data_slice is a Math::Matrix object (2-D array) | ||
| 810 | # replacement_data is a Math::Matrix object or 2-D array | ||
| 811 | # parameters: ([hash]) => returns: (column_slice) | ||
| 812 | # parameters: (row_slice, [hash]) => returns: (data_slice) | ||
| 813 | # parameters: (row_slice, replacement_data, [hash]) => returns: (column_slice) | ||
| 814 | sub rgb { | ||
| 815 | |||
| 816 | # local variables | ||
| 817 | my ($hash, $cols); | ||
| 818 | |||
| 819 | # get optional hash | ||
| 820 | $hash = pop() if (ref($_[-1]) eq 'HASH'); | ||
| 821 | |||
| 822 | # get remaining parameters | ||
| 823 | my ($self, $rows, $data) = @_; | ||
| 824 | |||
| 825 | # get column slice, adding optional context prefix | ||
| 826 | $cols = cols($self, map {defined($hash->{'context'}) ? "$hash->{'context'}|$_" : $_} qw(RGB_R RGB_G RGB_B)); | ||
| 827 | |||
| 828 | # call get/set subroutine | ||
| 829 | _getset($self, 1, $rows, $cols, $data); | ||
| 830 | |||
| 831 | } | ||
| 832 | |||
| 833 | # get/set CMYK data | ||
| 834 | # optional hash contains supplementary parameters | ||
| 835 | # row_slice and column_slice are 1-D array references | ||
| 836 | # data_slice is a Math::Matrix object (2-D array) | ||
| 837 | # replacement_data is a Math::Matrix object or 2-D array | ||
| 838 | # parameters: ([hash]) => returns: (column_slice) | ||
| 839 | # parameters: (row_slice, [hash]) => returns: (data_slice) | ||
| 840 | # parameters: (row_slice, replacement_data, [hash]) => returns: (column_slice) | ||
| 841 | sub cmyk { | ||
| 842 | |||
| 843 | # local variables | ||
| 844 | my ($hash, $cols); | ||
| 845 | |||
| 846 | # get optional hash | ||
| 847 | $hash = pop() if (ref($_[-1]) eq 'HASH'); | ||
| 848 | |||
| 849 | # get remaining parameters | ||
| 850 | my ($self, $rows, $data) = @_; | ||
| 851 | |||
| 852 | # get column slice, adding optional context prefix | ||
| 853 | $cols = cols($self, map {defined($hash->{'context'}) ? "$hash->{'context'}|$_" : $_} qw(CMYK_C CMYK_M CMYK_Y CMYK_K)); | ||
| 854 | |||
| 855 | # call get/set subroutine | ||
| 856 | _getset($self, 1, $rows, $cols, $data); | ||
| 857 | |||
| 858 | } | ||
| 859 | |||
| 860 | # get/set 6CLR data | ||
| 861 | # optional hash contains supplementary parameters | ||
| 862 | # row_slice and column_slice are 1-D array references | ||
| 863 | # data_slice is a Math::Matrix object (2-D array) | ||
| 864 | # replacement_data is a Math::Matrix object or 2-D array | ||
| 865 | # parameters: ([hash]) => returns: (column_slice) | ||
| 866 | # parameters: (row_slice, [hash]) => returns: (data_slice) | ||
| 867 | # parameters: (row_slice, replacement_data, [hash]) => returns: (column_slice) | ||
| 868 | sub hex { | ||
| 869 | |||
| 870 | # local variables | ||
| 871 | my ($hash, $cols); | ||
| 872 | |||
| 873 | # get optional hash | ||
| 874 | $hash = pop() if (ref($_[-1]) eq 'HASH'); | ||
| 875 | |||
| 876 | # get remaining parameters | ||
| 877 | my ($self, $rows, $data) = @_; | ||
| 878 | |||
| 879 | # get column slice, adding optional context prefix | ||
| 880 | $cols = cols($self, map {defined($hash->{'context'}) ? "$hash->{'context'}|$_" : $_} qw(6CLR_1 6CLR_2 6CLR_3 6CLR_4 6CLR_5 6CLR_6)); | ||
| 881 | |||
| 882 | # call get/set subroutine | ||
| 883 | _getset($self, 1, $rows, $cols, $data); | ||
| 884 | |||
| 885 | } | ||
| 886 | |||
| 887 | # get/set nCLR data | ||
| 888 | # optional hash contains supplementary parameters | ||
| 889 | # row_slice and column_slice are 1-D array references | ||
| 890 | # data_slice and replacement_data are 2-D array references | ||
| 891 | # parameters: ([hash]) => returns: (column_slice) | ||
| 892 | # parameters: (row_slice, [hash]) => returns: (data_slice) | ||
| 893 | # parameters: (row_slice, replacement_data, [hash]) => returns: (column_slice) | ||
| 894 | sub nCLR { | ||
| 895 | |||
| 896 | # local variables | ||
| 897 | my ($hash, $context, %fmt, %fmt2, $chan, @cols); | ||
| 898 | |||
| 899 | # get optional hash | ||
| 900 | $hash = pop() if (ref($_[-1]) eq 'HASH'); | ||
| 901 | |||
| 902 | # get remaining parameters | ||
| 903 | my ($self, $rows, $data) = @_; | ||
| 904 | |||
| 905 | # get the context | ||
| 906 | $context = $hash->{'context'}; | ||
| 907 | |||
| 908 | # make lookup hash (key => column) | ||
| 909 | %fmt = map {($self->[1][0][$_] =~ m/^(?:.*\|)?[2-9A-F]CLR_[1-9A-F]$/) ? ($self->[1][0][$_], $_) : ()} (0 .. $#{$self->[1][0]}); | ||
| 910 | |||
| 911 | # make lookup hash (prefix -or- '||' => channels) | ||
| 912 | %fmt2 = map {($self->[1][0][$_] =~ m/^(.*\|)?([2-9A-F])CLR_[1-9A-F]$/) ? (defined($1) ? ($1, $2) : ('||', $2)) : ()} (0 .. $#{$self->[1][0]}); | ||
| 913 | |||
| 914 | # if context defined | ||
| 915 | if (defined($context)) { | ||
| 916 | |||
| 917 | # get the number of channels | ||
| 918 | ($chan = $fmt2{"$context|"}) || return(); | ||
| 919 | |||
| 920 | # append format | ||
| 921 | $chan .= 'CLR_'; | ||
| 922 | |||
| 923 | # get column slice (selected from %fmt columns) | ||
| 924 | @cols = grep {$self->[1][0][$_] =~ m/^$context\|$chan[1-9A-F]$/} values(%fmt); | ||
| 925 | |||
| 926 | } else { | ||
| 927 | |||
| 928 | # if number of channels undefined | ||
| 929 | if (! defined($chan = $fmt2{'||'})) { | ||
| 930 | |||
| 931 | # make lookup hash ignoring prefix (key => column) | ||
| 932 | %fmt = map {($self->[1][0][$_] =~ m/^(?:.*\|)?([2-9A-F]CLR_[1-9A-F])$/) ? ($1, $_) : ()} (0 .. $#{$self->[1][0]}); | ||
| 933 | |||
| 934 | # make lookup hash ('||' => channels) | ||
| 935 | %fmt2 = map {($self->[1][0][$_] =~ m/^(?:.*\|)?([2-9A-F])CLR_[1-9A-F]$/) ? ('||', $1) : ()} (0 .. $#{$self->[1][0]}); | ||
| 936 | |||
| 937 | # get the number of channels | ||
| 938 | ($chan = $fmt2{'||'}) || return(); | ||
| 939 | |||
| 940 | # append format | ||
| 941 | $chan .= 'CLR_'; | ||
| 942 | |||
| 943 | # get column slice (selected from %fmt columns) | ||
| 944 | @cols = grep {$self->[1][0][$_] =~ m/^(?:.*\|)?$chan[1-9A-F]$/} values(%fmt); | ||
| 945 | |||
| 946 | } else { | ||
| 947 | |||
| 948 | # append format | ||
| 949 | $chan .= 'CLR_'; | ||
| 950 | |||
| 951 | # get column slice (selected from %fmt columns) | ||
| 952 | @cols = grep {$self->[1][0][$_] =~ m/^$chan[1-9A-F]$/} values(%fmt); | ||
| 953 | |||
| 954 | } | ||
| 955 | |||
| 956 | } | ||
| 957 | |||
| 958 | # sort by color channel (1-9, A-F) | ||
| 959 | @cols = sort {substr($self->[1][0][$a], -1) cmp substr($self->[1][0][$b], -1)} @cols; | ||
| 960 | |||
| 961 | # match last format key | ||
| 962 | $self->[1][0][$cols[-1]] =~ m/([2-9A-F])CLR_([1-9A-F])$/; | ||
| 963 | |||
| 964 | # verify number of format keys | ||
| 965 | (CORE::hex($1) == @cols && CORE::hex($2) == @cols) || croak('wrong number of nCLR keys'); | ||
| 966 | |||
| 967 | # call get/set subroutine | ||
| 968 | _getset($self, 1, $rows, \@cols, $data); | ||
| 969 | |||
| 970 | } | ||
| 971 | |||
| 972 | # get/set device data | ||
| 973 | # device data is either RGB, CMYK or nCLR | ||
| 974 | # device values have range (0 - 1) | ||
| 975 | # optional hash contains supplementary parameters | ||
| 976 | # row_slice and column_slice are 1-D array references | ||
| 977 | # data_slice is a Math::Matrix object (2-D array) | ||
| 978 | # replacement_data is a Math::Matrix object or 2-D array | ||
| 979 | # parameters: ([hash]) => returns: (column_slice) | ||
| 980 | # parameters: (row_slice, [hash]) => returns: (data_slice) | ||
| 981 | # parameters: (row_slice, replacement_data, [hash]) => returns: (column_slice) | ||
| 982 | sub device { | ||
| 983 | |||
| 984 | # local variables | ||
| 985 | my ($hash, $cols, $mult); | ||
| 986 | |||
| 987 | # get optional hash | ||
| 988 | $hash = pop() if (ref($_[-1]) eq 'HASH'); | ||
| 989 | |||
| 990 | # get remaining parameters | ||
| 991 | my ($self, $rows, $data) = @_; | ||
| 992 | |||
| 993 | # get column slice or return empty | ||
| 994 | $cols = rgb($self, $hash) || cmyk($self, $hash) || nCLR($self, $hash) || return(); | ||
| 995 | |||
| 996 | # set multiplier (255 if RGB, else 100) | ||
| 997 | $mult = ($self->[1][0][$cols->[0]] =~ m/RGB_R$/) ? 255 : 100; | ||
| 998 | |||
| 999 | # call get/set subroutine | ||
| 1000 | _getset($self, 1, $rows, $cols, $data, sub {map {defined($_) ? $_/$mult : $_} @_}, sub {map {defined($_) ? $_ * $mult : $_} @_}); | ||
| 1001 | |||
| 1002 | } | ||
| 1003 | |||
| 1004 | # get/set CTV data | ||
| 1005 | # optional hash contains supplementary parameters | ||
| 1006 | # row_slice and column_slice are 1-D array references | ||
| 1007 | # data_slice is a Math::Matrix object (2-D array) | ||
| 1008 | # replacement_data is a Math::Matrix object or 2-D array | ||
| 1009 | # parameters: ([hash]) => returns: (column_slice) | ||
| 1010 | # parameters: (row_slice, [hash]) => returns: (data_slice) | ||
| 1011 | # parameters: (row_slice, replacement_data, [hash]) => returns: (column_slice) | ||
| 1012 | sub ctv { | ||
| 1013 | |||
| 1014 | # local variables | ||
| 1015 | my ($hash, $cols); | ||
| 1016 | |||
| 1017 | # get optional hash | ||
| 1018 | $hash = pop() if (ref($_[-1]) eq 'HASH'); | ||
| 1019 | |||
| 1020 | # get remaining parameters | ||
| 1021 | my ($self, $rows, $data) = @_; | ||
| 1022 | |||
| 1023 | # get column slice, adding optional context prefix | ||
| 1024 | $cols = cols($self, map {defined($hash->{'context'}) ? "$hash->{'context'}|$_" : $_} qw(CTV)); | ||
| 1025 | |||
| 1026 | # call get/set subroutine | ||
| 1027 | _getset($self, 1, $rows, $cols, $data); | ||
| 1028 | |||
| 1029 | } | ||
| 1030 | |||
| 1031 | # get/set L*a*b* data | ||
| 1032 | # optional hash contains supplementary parameters | ||
| 1033 | # row_slice and column_slice are 1-D array references | ||
| 1034 | # data_slice is a Math::Matrix object (2-D array) | ||
| 1035 | # replacement_data is a Math::Matrix object or 2-D array | ||
| 1036 | # parameters: ([hash]) => returns: (column_slice) | ||
| 1037 | # parameters: (row_slice, [hash]) => returns: (data_slice) | ||
| 1038 | # parameters: (row_slice, replacement_data, [hash]) => returns: (column_slice) | ||
| 1039 | sub lab { | ||
| 1040 | |||
| 1041 | # local variables | ||
| 1042 | my ($hash, $cols); | ||
| 1043 | |||
| 1044 | # get optional hash | ||
| 1045 | $hash = pop() if (ref($_[-1]) eq 'HASH'); | ||
| 1046 | |||
| 1047 | # get remaining parameters | ||
| 1048 | my ($self, $rows, $data) = @_; | ||
| 1049 | |||
| 1050 | # get column slice, adding optional context prefix | ||
| 1051 | $cols = cols($self, map {defined($hash->{'context'}) ? "$hash->{'context'}|$_" : $_} qw(LAB_L LAB_A LAB_B)); | ||
| 1052 | |||
| 1053 | # call get/set subroutine | ||
| 1054 | _getset($self, 1, $rows, $cols, $data, _lab_encoding($self, $hash)); | ||
| 1055 | |||
| 1056 | } | ||
| 1057 | |||
| 1058 | # get/set XYZ data | ||
| 1059 | # optional hash contains supplementary parameters | ||
| 1060 | # row_slice and column_slice are 1-D array references | ||
| 1061 | # data_slice is a Math::Matrix object (2-D array) | ||
| 1062 | # replacement_data is a Math::Matrix object or 2-D array | ||
| 1063 | # parameters: ([hash]) => returns: (column_slice) | ||
| 1064 | # parameters: (row_slice, [hash]) => returns: (data_slice) | ||
| 1065 | # parameters: (row_slice, replacement_data, [hash]) => returns: (column_slice) | ||
| 1066 | sub xyz { | ||
| 1067 | |||
| 1068 | # local variables | ||
| 1069 | my ($hash, $cols); | ||
| 1070 | |||
| 1071 | # get optional hash | ||
| 1072 | $hash = pop() if (ref($_[-1]) eq 'HASH'); | ||
| 1073 | |||
| 1074 | # get remaining parameters | ||
| 1075 | my ($self, $rows, $data) = @_; | ||
| 1076 | |||
| 1077 | # get column slice, adding optional context prefix | ||
| 1078 | $cols = cols($self, map {defined($hash->{'context'}) ? "$hash->{'context'}|$_" : $_} qw(XYZ_X XYZ_Y XYZ_Z)); | ||
| 1079 | |||
| 1080 | # call get/set subroutine | ||
| 1081 | _getset($self, 1, $rows, $cols, $data, _xyz_encoding($self, $cols, $hash)); | ||
| 1082 | |||
| 1083 | } | ||
| 1084 | |||
| 1085 | # get/set density data | ||
| 1086 | # optional hash contains supplementary parameters | ||
| 1087 | # row_slice and column_slice are 1-D array references | ||
| 1088 | # data_slice is a Math::Matrix object (2-D array) | ||
| 1089 | # replacement_data is a Math::Matrix object or 2-D array | ||
| 1090 | # parameters: ([hash]) => returns: (column_slice) | ||
| 1091 | # parameters: (row_slice, [hash]) => returns: (data_slice) | ||
| 1092 | # parameters: (row_slice, replacement_data, [hash]) => returns: (column_slice) | ||
| 1093 | sub density { | ||
| 1094 | |||
| 1095 | # local variables | ||
| 1096 | my ($hash, $cols); | ||
| 1097 | |||
| 1098 | # get optional hash | ||
| 1099 | $hash = pop() if (ref($_[-1]) eq 'HASH'); | ||
| 1100 | |||
| 1101 | # get remaining parameters | ||
| 1102 | my ($self, $rows, $data) = @_; | ||
| 1103 | |||
| 1104 | # get column slice | ||
| 1105 | $cols = cols($self, map {defined($hash->{'context'}) ? "$hash->{'context'}|$_" : $_} qw(D_RED D_GREEN D_BLUE D_VIS)); | ||
| 1106 | |||
| 1107 | # call get/set subroutine | ||
| 1108 | _getset($self, 1, $rows, $cols, $data, _density_encoding($self, $hash)); | ||
| 1109 | |||
| 1110 | } | ||
| 1111 | |||
| 1112 | # get/set reflectance/transmittance data | ||
| 1113 | # optional hash contains supplementary parameters | ||
| 1114 | # row_slice and column_slice are 1-D array references | ||
| 1115 | # data_slice is a Math::Matrix object (2-D array) | ||
| 1116 | # replacement_data is a Math::Matrix object or 2-D array | ||
| 1117 | # parameters: ([hash]) => returns: (column_slice) | ||
| 1118 | # parameters: (row_slice, [hash]) => returns: (data_slice) | ||
| 1119 | # parameters: (row_slice, replacement_data, [hash]) => returns: (column_slice) | ||
| 1120 | sub rgbv { | ||
| 1121 | |||
| 1122 | # local variables | ||
| 1123 | my ($hash, $cols); | ||
| 1124 | |||
| 1125 | # get optional hash | ||
| 1126 | $hash = pop() if (ref($_[-1]) eq 'HASH'); | ||
| 1127 | |||
| 1128 | # get remaining parameters | ||
| 1129 | my ($self, $rows, $data) = @_; | ||
| 1130 | |||
| 1131 | # get column slice | ||
| 1132 | $cols = cols($self, map {defined($hash->{'context'}) ? "$hash->{'context'}|$_" : $_} qw(R_RED R_GREEN R_BLUE R_VIS)); | ||
| 1133 | |||
| 1134 | # call get/set subroutine | ||
| 1135 | _getset($self, 1, $rows, $cols, $data, _rgbv_encoding($self, $hash)); | ||
| 1136 | |||
| 1137 | } | ||
| 1138 | |||
| 1139 | # get/set spectral data | ||
| 1140 | # optional hash contains supplementary parameters | ||
| 1141 | # row_slice and column_slice are 1-D array references | ||
| 1142 | # data_slice is a Math::Matrix object (2-D array) | ||
| 1143 | # replacement_data is a Math::Matrix object or 2-D array | ||
| 1144 | # parameters: ([hash]) => returns: (column_slice) | ||
| 1145 | # parameters: (row_slice, [hash]) => returns: (data_slice) | ||
| 1146 | # parameters: (row_slice, replacement_data, [hash]) => returns: (column_slice) | ||
| 1147 | sub spectral { | ||
| 1148 | |||
| 1149 | # local variables | ||
| 1150 | my ($hash, $fields, $cols); | ||
| 1151 | |||
| 1152 | # get optional hash | ||
| 1153 | $hash = pop() if (ref($_[-1]) eq 'HASH'); | ||
| 1154 | |||
| 1155 | # get remaining parameters | ||
| 1156 | my ($self, $rows, $data) = @_; | ||
| 1157 | |||
| 1158 | # get spectral fields array | ||
| 1159 | $fields = _spectral($self, $hash->{'context'}); | ||
| 1160 | |||
| 1161 | # get column slice from spectral fields array | ||
| 1162 | $cols = defined($fields) ? [map {$_->[0]} @{$fields}] : undef; | ||
| 1163 | |||
| 1164 | # call get/set subroutine | ||
| 1165 | _getset($self, 1, $rows, $cols, $data); | ||
| 1166 | |||
| 1167 | } | ||
| 1168 | |||
| 1169 | # get spectral wavelength array | ||
| 1170 | # array is sorted (low to high) | ||
| 1171 | # parameters: ([hash]) | ||
| 1172 | # returns: (ref_to_wavelength_array) | ||
| 1173 | sub wavelength { | ||
| 1174 | |||
| 1175 | # get parameters | ||
| 1176 | my ($self, $hash) = @_; | ||
| 1177 | |||
| 1178 | # get spectral fields array or return empty | ||
| 1179 | my $fields = _spectral($self, $hash->{'context'}) || return(); | ||
| 1180 | |||
| 1181 | # return | ||
| 1182 | return([map {$_->[1]} @{$fields}]); | ||
| 1183 | |||
| 1184 | } | ||
| 1185 | |||
| 1186 | # get spectral wavelength range | ||
| 1187 | # structure is [start_nm, end_nm, increment] | ||
| 1188 | # parameters: ([hash]) | ||
| 1189 | # returns: (range) | ||
| 1190 | sub nm { | ||
| 1191 | |||
| 1192 | # get parameters | ||
| 1193 | my ($self, $hash) = @_; | ||
| 1194 | |||
| 1195 | # local variables | ||
| 1196 | my ($fields, $inc); | ||
| 1197 | |||
| 1198 | # get spectral fields array or return empty | ||
| 1199 | $fields = _spectral($self, $hash->{'context'}) || return(); | ||
| 1200 | |||
| 1201 | # compute increment | ||
| 1202 | $inc = $fields->[1][1] - $fields->[0][1]; | ||
| 1203 | |||
| 1204 | # verify wavelength increment | ||
| 1205 | ($inc > 0 && abs($#{$fields} * $inc - $fields->[-1][1] + $fields->[0][1]) < 1E-12) || warn('inconsistent wavelength values'); | ||
| 1206 | |||
| 1207 | # return range | ||
| 1208 | return([$fields->[0][1], $fields->[-1][1], $inc]); | ||
| 1209 | |||
| 1210 | } | ||
| 1211 | |||
| 1212 | # get illuminant white point | ||
| 1213 | # parameters: ([hash]) | ||
| 1214 | # returns: (XYZ_vector) | ||
| 1215 | sub iwtpt { | ||
| 1216 | |||
| 1217 | # get parameters | ||
| 1218 | my ($self, $hash) = @_; | ||
| 1219 | |||
| 1220 | # local variables | ||
| 1221 | my ($encode, $cols, $iwtpt, $get); | ||
| 1222 | |||
| 1223 | # extract encoding hash | ||
| 1224 | $encode = {'encoding' => delete($hash->{'encoding'})}; | ||
| 1225 | |||
| 1226 | # get XYZ or L*a*b* column slice | ||
| 1227 | $cols = xyz($self, $hash) || lab($self, $hash) || croak('illuminant white point XYZ or L*a*b* column slice undefined'); | ||
| 1228 | |||
| 1229 | # get illuminant white point | ||
| 1230 | $iwtpt = _illumWP($self, $cols, $hash); | ||
| 1231 | |||
| 1232 | # get code reference | ||
| 1233 | ($get) = _xyz_encoding($self, $cols, $encode); | ||
| 1234 | |||
| 1235 | # return encoded XYZ vector | ||
| 1236 | return([&$get(@{$iwtpt})]); | ||
| 1237 | |||
| 1238 | } | ||
| 1239 | |||
| 1240 | # get media white point | ||
| 1241 | # parameters: ([hash]) | ||
| 1242 | # returns: (XYZ_vector) | ||
| 1243 | sub wtpt { | ||
| 1244 | |||
| 1245 | # get parameters | ||
| 1246 | my ($self, $hash) = @_; | ||
| 1247 | |||
| 1248 | # local variables | ||
| 1249 | my ($encode, $cols, $get); | ||
| 1250 | |||
| 1251 | # extract encoding hash | ||
| 1252 | $encode = {'encoding' => delete($hash->{'encoding'})}; | ||
| 1253 | |||
| 1254 | # get XYZ or L*a*b* column slice | ||
| 1255 | $cols = xyz($self, $hash) || lab($self, $hash) || croak('white point XYZ or L*a*b* column slice undefined'); | ||
| 1256 | |||
| 1257 | # if media white point undefined in colorimetry array | ||
| 1258 | if (! defined($self->[2][3][$cols->[0]])) { | ||
| 1259 | |||
| 1260 | # compute media white point or return undefined | ||
| 1261 | (_mediaWP($self, $cols, $hash)) || return(); | ||
| 1262 | |||
| 1263 | } | ||
| 1264 | |||
| 1265 | # get code reference | ||
| 1266 | ($get) = _xyz_encoding($self, $cols, $encode); | ||
| 1267 | |||
| 1268 | # return encoded XYZ vector | ||
| 1269 | return([&$get(@{$self->[2][3]}[@{$cols}])]); | ||
| 1270 | |||
| 1271 | } | ||
| 1272 | |||
| 1273 | # get media black point | ||
| 1274 | # parameters: ([hash]) | ||
| 1275 | # returns: (XYZ_vector) | ||
| 1276 | sub bkpt { | ||
| 1277 | |||
| 1278 | # get parameters | ||
| 1279 | my ($self, $hash) = @_; | ||
| 1280 | |||
| 1281 | # local variables | ||
| 1282 | my ($encode, $cols, $get); | ||
| 1283 | |||
| 1284 | # extract encoding hash | ||
| 1285 | $encode = {'encoding' => delete($hash->{'encoding'})}; | ||
| 1286 | |||
| 1287 | # get XYZ or L*a*b* column slice | ||
| 1288 | $cols = xyz($self, $hash) || lab($self, $hash) || croak('black point XYZ or L*a*b* column slice undefined'); | ||
| 1289 | |||
| 1290 | # if media black point undefined in colorimetry array | ||
| 1291 | if (! defined($self->[2][4][$cols->[0]])) { | ||
| 1292 | |||
| 1293 | # compute media black point or return undefined | ||
| 1294 | (_mediaBP($self, $cols, $hash)) || return(); | ||
| 1295 | |||
| 1296 | } | ||
| 1297 | |||
| 1298 | # get code reference | ||
| 1299 | ($get) = _xyz_encoding($self, $cols, $encode); | ||
| 1300 | |||
| 1301 | # return encoded XYZ vector | ||
| 1302 | return([&$get(@{$self->[2][4]}[@{$cols}])]); | ||
| 1303 | |||
| 1304 | } | ||
| 1305 | |||
| 1306 | # compute media OBA index | ||
| 1307 | # requires M1 and M2 measurements | ||
| 1308 | # requires device values -or- sample number | ||
| 1309 | # optional hash keys are 'sample', 'device', and 'context' | ||
| 1310 | # parameters: ([hash]) | ||
| 1311 | # returns: (oba_index) | ||
| 1312 | # returns: (M1_XYZ_vector, M2_XYZ_vector) | ||
| 1313 | sub oba_index { | ||
| 1314 | |||
| 1315 | # get parameters | ||
| 1316 | my ($self, $hash) = @_; | ||
| 1317 | |||
| 1318 | # local variables | ||
| 1319 | my ($sample, $dev, $mwv, $wps, $wpdata, $context1, $context2, $m1, $m2, @xyz1, @xyz2, $nm, $color); | ||
| 1320 | |||
| 1321 | # if 'sample' defined | ||
| 1322 | if (defined($hash->{'sample'})) { | ||
| 1323 | |||
| 1324 | # get sample from hash | ||
| 1325 | $sample = $hash->{'sample'}; | ||
| 1326 | |||
| 1327 | # if valid sample number | ||
| 1328 | if (Scalar::Util::looks_like_number($sample) && $sample == int($sample) && $sample > 0 && $sample <= $#{$self->[1]}) { | ||
| 1329 | |||
| 1330 | # get sample data row | ||
| 1331 | $wpdata = $self->[1][$sample]; | ||
| 1332 | |||
| 1333 | } else { | ||
| 1334 | |||
| 1335 | # warn | ||
| 1336 | warn('invalid sample number'); | ||
| 1337 | |||
| 1338 | # return empty | ||
| 1339 | return(); | ||
| 1340 | |||
| 1341 | } | ||
| 1342 | |||
| 1343 | } else { | ||
| 1344 | |||
| 1345 | # if device data (using 'device' context) | ||
| 1346 | if ($dev = device($self, {'context' => $hash->{'device'}})) { | ||
| 1347 | |||
| 1348 | # set media white device value (255 if RGB, 0 otherwise) | ||
| 1349 | $mwv = ($self->[1][0][$dev->[0]] =~ m/RGB_R$/) ? 255 : 0; | ||
| 1350 | |||
| 1351 | # if paper white samples found | ||
| 1352 | if ($wps = find($self, sub {@_ == grep {$_ == $mwv} @_}, [], $dev)) { | ||
| 1353 | |||
| 1354 | # add average paper white sample row | ||
| 1355 | add_avg($self, $wps); | ||
| 1356 | |||
| 1357 | # get sample data row | ||
| 1358 | $wpdata = pop(@{$self->[1]}); | ||
| 1359 | |||
| 1360 | } else { | ||
| 1361 | |||
| 1362 | # warn | ||
| 1363 | warn('no paper white samples'); | ||
| 1364 | |||
| 1365 | # return empty | ||
| 1366 | return(); | ||
| 1367 | |||
| 1368 | } | ||
| 1369 | |||
| 1370 | } else { | ||
| 1371 | |||
| 1372 | # warn | ||
| 1373 | warn('no sample value or device data'); | ||
| 1374 | |||
| 1375 | # return empty | ||
| 1376 | return(); | ||
| 1377 | |||
| 1378 | } | ||
| 1379 | |||
| 1380 | } | ||
| 1381 | |||
| 1382 | # if 'context' defined | ||
| 1383 | if (defined($hash->{'context'})) { | ||
| 1384 | |||
| 1385 | # if context an array reference containing two scalars | ||
| 1386 | if (ref($hash->{'context'}) eq 'ARRAY' && ! ref($hash->{'context'}->[0]) && ! ref($hash->{'context'}->[1])) { | ||
| 1387 | |||
| 1388 | # get specified 'M1' context | ||
| 1389 | $context1 = $hash->{'context'}->[0]; | ||
| 1390 | |||
| 1391 | # get specified 'M2' context | ||
| 1392 | $context2 = $hash->{'context'}->[1]; | ||
| 1393 | |||
| 1394 | } else { | ||
| 1395 | |||
| 1396 | # warn | ||
| 1397 | warn('OBA context is an array reference containing M1 and M2 contexts'); | ||
| 1398 | |||
| 1399 | # return empty | ||
| 1400 | return(); | ||
| 1401 | |||
| 1402 | } | ||
| 1403 | |||
| 1404 | } else { | ||
| 1405 | |||
| 1406 | # use standard 'M1' context | ||
| 1407 | $context1 = 'M1_Measurement'; | ||
| 1408 | |||
| 1409 | # use standard 'M2' context | ||
| 1410 | $context2 = 'M2_Measurement'; | ||
| 1411 | |||
| 1412 | } | ||
| 1413 | |||
| 1414 | # if M1 and M2 spectral data | ||
| 1415 | if (($m1 = spectral($self, {'context' => $context1})) && ($m2 = spectral($self, {'context' => $context2}))) { | ||
| 1416 | |||
| 1417 | # get spectral range | ||
| 1418 | $nm = nm($self, {'context' => $context1}); | ||
| 1419 | |||
| 1420 | # if increment is 10 or 20 nm | ||
| 1421 | if ($nm->[2] == 10 || $nm->[2] == 20) { | ||
| 1422 | |||
| 1423 | # make ASTM color object | ||
| 1424 | $color = ICC::Support::Color->new({'illuminant' => 'D50', 'increment' => $nm->[2]}); | ||
| 1425 | |||
| 1426 | } else { | ||
| 1427 | |||
| 1428 | # make CIE color object | ||
| 1429 | $color = ICC::Support::Color->new({'illuminant' => ['CIE', 'D50'], 'increment' => $nm->[2]}); | ||
| 1430 | |||
| 1431 | } | ||
| 1432 | |||
| 1433 | # compute M1 and M2 XYZ values | ||
| 1434 | @xyz1 = $color->transform(@{$wpdata}[@{$m1}]); | ||
| 1435 | @xyz2 = $color->transform(@{$wpdata}[@{$m2}]); | ||
| 1436 | |||
| 1437 | # if M1 and M2 XYZ data | ||
| 1438 | } elsif (($m1 = xyz($self, {'context' => $context1})) && ($m2 = xyz($self, {'context' => $context2}))) { | ||
| 1439 | |||
| 1440 | # get M1 and M2 XYZ values (assumes D50 illumination) | ||
| 1441 | @xyz1 = @{$wpdata}[@{$m1}]; | ||
| 1442 | @xyz2 = @{$wpdata}[@{$m2}]; | ||
| 1443 | |||
| 1444 | # if M1 and M2 L*a*b* data | ||
| 1445 | } elsif (($m1 = lab($self, {'context' => $context1})) && ($m2 = lab($self, {'context' => $context2}))) { | ||
| 1446 | |||
| 1447 | # compute M1 and M2 XYZ values (D50 illumination) | ||
| 1448 | @xyz1 = ICC::Shared::_Lab2XYZ(@{$wpdata}[@{$m1}], ICC::Shared::D50); | ||
| 1449 | @xyz2 = ICC::Shared::_Lab2XYZ(@{$wpdata}[@{$m2}], ICC::Shared::D50); | ||
| 1450 | |||
| 1451 | } else { | ||
| 1452 | |||
| 1453 | # warn | ||
| 1454 | warn('M1 and M2 data required for OBA index'); | ||
| 1455 | |||
| 1456 | # return empty | ||
| 1457 | return(); | ||
| 1458 | |||
| 1459 | } | ||
| 1460 | |||
| 1461 | # return array (XYZ media white points) or scalar (OBA index) | ||
| 1462 | return(wantarray ? (\@xyz1, \@xyz2) : ($xyz1[2] - $xyz2[2])/82.49); | ||
| 1463 | |||
| 1464 | } | ||
| 1465 | |||
| 1466 | # get chromatic adaptation transform (CAT) object | ||
| 1467 | # a CAT is optionally created when adding XYZ data | ||
| 1468 | # optional hash contains supplementary parameters | ||
| 1469 | # parameters: ([hash]) | ||
| 1470 | # returns: (CAT_object) | ||
| 1471 | sub cat { | ||
| 1472 | |||
| 1473 | # get parameters | ||
| 1474 | my ($self, $hash) = @_; | ||
| 1475 | |||
| 1476 | # local variables | ||
| 1477 | my ($cols, $cat); | ||
| 1478 | |||
| 1479 | # get column slice, adding optional context prefix | ||
| 1480 | $cols = cols($self, map {defined($hash->{'context'}) ? "$hash->{'context'}|$_" : $_} qw(XYZ_X XYZ_Y XYZ_Z)); | ||
| 1481 | |||
| 1482 | # return if slice undefined | ||
| 1483 | return() if (! defined($cols)); | ||
| 1484 | |||
| 1485 | # get CAT or illuminant | ||
| 1486 | $cat = $self->[2][2][$cols->[0]]; | ||
| 1487 | |||
| 1488 | # return CAT if defined | ||
| 1489 | return((defined($cat) && UNIVERSAL::isa($cat, 'ICC::Profile::matf')) ? $cat : ()); | ||
| 1490 | |||
| 1491 | } | ||
| 1492 | |||
| 1493 | # get Color object | ||
| 1494 | # a Color object is created when adding XYZ data from spectral data | ||
| 1495 | # optional hash contains supplementary parameters | ||
| 1496 | # parameters: ([hash]) | ||
| 1497 | # returns: (Color_object) | ||
| 1498 | sub color { | ||
| 1499 | |||
| 1500 | # get parameters | ||
| 1501 | my ($self, $hash) = @_; | ||
| 1502 | |||
| 1503 | # local variables | ||
| 1504 | my ($cols, $color); | ||
| 1505 | |||
| 1506 | # get column slice, adding optional context prefix | ||
| 1507 | $cols = cols($self, map {defined($hash->{'context'}) ? "$hash->{'context'}|$_" : $_} qw(XYZ_X XYZ_Y XYZ_Z)); | ||
| 1508 | |||
| 1509 | # return if slice undefined | ||
| 1510 | return() if (! defined($cols)); | ||
| 1511 | |||
| 1512 | # get CAT or illuminant | ||
| 1513 | $color = $self->[2][1][$cols->[0]]; | ||
| 1514 | |||
| 1515 | # return CAT if defined | ||
| 1516 | return((defined($color) && UNIVERSAL::isa($color, 'ICC::Support::Color')) ? $color : ()); | ||
| 1517 | |||
| 1518 | } | ||
| 1519 | |||
| 1520 | # append rows to data array | ||
| 1521 | # data matrix is the 2-D array of data values to be appended | ||
| 1522 | # column slice is a reference to an array of data matrix column indices | ||
| 1523 | # parameters: (data_matrix, [column_slice]) | ||
| 1524 | # returns: (row_slice) | ||
| 1525 | sub add_rows { | ||
| 1526 | |||
| 1527 | # get parameters | ||
| 1528 | my ($self, $matrix, $cols) = @_; | ||
| 1529 | |||
| 1530 | # set offset to upper index + 1, or 0 if a new object (row 0 is empty) | ||
| 1531 | my $offset = $#{$self->[1]} || @{$self->[1][0]} ? $#{$self->[1]} + 1 : 0; | ||
| 1532 | |||
| 1533 | # call 'splice_rows' | ||
| 1534 | splice_rows($self, $offset, 0, $matrix, $cols); | ||
| 1535 | |||
| 1536 | # return row slice | ||
| 1537 | return([$offset .. ($offset + $#{$matrix})]); | ||
| 1538 | |||
| 1539 | } | ||
| 1540 | |||
| 1541 | # append columns to data array | ||
| 1542 | # data matrix is the 2-D array of data values to be appended | ||
| 1543 | # header is a reference to an array of DATA_FORMAT keywords | ||
| 1544 | # parameters: (data_matrix, [header]) | ||
| 1545 | # returns: (column_slice) | ||
| 1546 | sub add_cols { | ||
| 1547 | |||
| 1548 | # get parameters | ||
| 1549 | my ($self, $matrix, $header) = @_; | ||
| 1550 | |||
| 1551 | # verify matrix a 2-D array or Math::Matrix object | ||
| 1552 | (ref($matrix) eq 'ARRAY' && ref($matrix->[0]) eq 'ARRAY') || (UNIVERSAL::isa($matrix, 'Math::Matrix')) || croak ('invalid matrix parameter'); | ||
| 1553 | |||
| 1554 | # if header supplied | ||
| 1555 | if (defined($header)) { | ||
| 1556 | |||
| 1557 | # verify header is 1-D array of scalars | ||
| 1558 | (ref($header) eq 'ARRAY' && @{$header} == grep {! ref()} @{$header}) || croak('invalid header parameter'); | ||
| 1559 | |||
| 1560 | # verify header and matrix have same number of columns | ||
| 1561 | (@{$header} == @{$matrix->[0]}) || croak('header and matrix have different number of columns'); | ||
| 1562 | |||
| 1563 | # add header to matrix | ||
| 1564 | $matrix = [$header, @{$matrix}]; | ||
| 1565 | |||
| 1566 | } | ||
| 1567 | |||
| 1568 | # warn if matrix and object have different number of rows | ||
| 1569 | (@{$matrix} == @{$self->[1]}) || carp('matrix and object have different number of rows'); | ||
| 1570 | |||
| 1571 | # set offset to upper index + 1 | ||
| 1572 | my $offset = $#{$self->[1][0]} + 1; | ||
| 1573 | |||
| 1574 | # call 'splice_cols' | ||
| 1575 | splice_cols($self, $offset, 0, $matrix); | ||
| 1576 | |||
| 1577 | # return column slice | ||
| 1578 | return([$offset .. ($offset + $#{$matrix->[0]})]); | ||
| 1579 | |||
| 1580 | } | ||
| 1581 | |||
| 1582 | # add average sample | ||
| 1583 | # assumes device values (if any) are same for each sample | ||
| 1584 | # averages measurement values - spectral, XYZ, L*a*b* or density | ||
| 1585 | # L*a*b* values are converted to xyz for averaging, then back to L*a*b* | ||
| 1586 | # density values are converted to reflectance for averaging, then back to density | ||
| 1587 | # returns row slice of the appended average sample | ||
| 1588 | # parameters: (row_slice, [hash]) | ||
| 1589 | # returns: (row_slice) | ||
| 1590 | sub add_avg { | ||
| 1591 | |||
| 1592 | # get parameters | ||
| 1593 | my ($self, $rows, $hash) = @_; | ||
| 1594 | |||
| 1595 | # local variables | ||
| 1596 | my ($c1, $c2, $c3, @id, @name); | ||
| 1597 | |||
| 1598 | # flatten row slice | ||
| 1599 | $rows = ICC::Shared::flatten($rows); | ||
| 1600 | |||
| 1601 | # resolve empty row slice | ||
| 1602 | $rows = [1 .. $#{$self->[1]}] if (@{$rows} == 0); | ||
| 1603 | |||
| 1604 | # get averaging groups | ||
| 1605 | ($c1, $c2, $c3) = _avg_groups($self, $hash); | ||
| 1606 | |||
| 1607 | # for each format field | ||
| 1608 | for my $i (0 .. $#{$self->[1][0]}) { | ||
| 1609 | |||
| 1610 | # add column if SAMPLE_ID field | ||
| 1611 | push(@id, $i) if ($self->[1][0][$i] =~ m/^(?:.*\|)?(?:SAMPLE_ID|SampleID)$/); | ||
| 1612 | |||
| 1613 | # add column if SAMPLE_NAME field | ||
| 1614 | push(@name, $i) if ($self->[1][0][$i] =~ m/^(?:.*\|)?SAMPLE_NAME$/); | ||
| 1615 | |||
| 1616 | } | ||
| 1617 | |||
| 1618 | # return average sample | ||
| 1619 | return([_add_avg($self, $rows, $c1, $c2, $c3, \@id, \@name, $hash)]); | ||
| 1620 | |||
| 1621 | } | ||
| 1622 | |||
| 1623 | # add format keys | ||
| 1624 | # keys are appended to row 0 of the data array | ||
| 1625 | # note: format_keys is a list of scalars and/or array references | ||
| 1626 | # note: format_keys are saved as given, with or without context | ||
| 1627 | # parameters: (format_keys) | ||
| 1628 | # returns: (column_slice) | ||
| 1629 | sub add_fmt { | ||
| 1630 | |||
| 1631 | # get parameters | ||
| 1632 | my $self = shift(); | ||
| 1633 | |||
| 1634 | # local variables | ||
| 1635 | my (@keys, $i, %fmt); | ||
| 1636 | |||
| 1637 | # flatten format key list | ||
| 1638 | @keys = @{ICC::Shared::flatten(@_)}; | ||
| 1639 | |||
| 1640 | # get upper column index | ||
| 1641 | $i = $#{$self->[1][0]}; | ||
| 1642 | |||
| 1643 | # make format lookup hash of existing keys | ||
| 1644 | %fmt = map {$self->[1][0][$_], $_} (0 .. $#{$self->[1][0]}); | ||
| 1645 | |||
| 1646 | # warn if duplicate keys | ||
| 1647 | warn('adding duplicate format key(s)') if (grep {exists($fmt{$_})} @keys); | ||
| 1648 | |||
| 1649 | # push format keys onto format row | ||
| 1650 | push(@{$self->[1][0]}, @keys); | ||
| 1651 | |||
| 1652 | # return slice array reference | ||
| 1653 | return([$i + 1 .. $#{$self->[1][0]}]); | ||
| 1654 | |||
| 1655 | } | ||
| 1656 | |||
| 1657 | # append CTV data to data array | ||
| 1658 | # computed from L*a*b* data, XYZ data, or spectral data | ||
| 1659 | # if CTV data already exists, return those slices | ||
| 1660 | # adds L*a*b* data, and XYZ data if missing | ||
| 1661 | # parameters: ([hash]) | ||
| 1662 | # returns: (column_slice) | ||
| 1663 | sub add_ctv { | ||
| 1664 | |||
| 1665 | # get parameters | ||
| 1666 | my ($self, $hash) = @_; | ||
| 1667 | |||
| 1668 | # local variables | ||
| 1669 | my ($context, $added, $cols, $Lab, $color); | ||
| 1670 | my ($iwtpt, $WPxyz, @wtpt, $dev, $mwv, $coef, @Ls); | ||
| 1671 | my ($den, $a, $b, $c, $d, $e, $f, $mat); | ||
| 1672 | |||
| 1673 | # get base context | ||
| 1674 | $context = $hash->{'context'}; | ||
| 1675 | |||
| 1676 | # get added context | ||
| 1677 | $added = defined($hash->{'added'}) ? $hash->{'added'} : $context; | ||
| 1678 | |||
| 1679 | # return column slice if CTV data already exists | ||
| 1680 | return($cols) if ($cols = _cols($self, map {defined($added) ? "$added|$_" : $_} qw(CTV))); | ||
| 1681 | |||
| 1682 | # if L*a*b* exists, or is added | ||
| 1683 | if ($Lab = (_cols($self, map {defined($context) ? "$context|$_" : $_} qw(LAB_L LAB_A LAB_B)) || add_lab($self, $hash))) { | ||
| 1684 | |||
| 1685 | # get L*a*b* colorimetry hash | ||
| 1686 | $color = $self->[2][6][$Lab->[0]]; | ||
| 1687 | |||
| 1688 | # for each possible colorimetry key | ||
| 1689 | for my $key (qw(illuminant observer increment bandpass cat)) { | ||
| 1690 | |||
| 1691 | # if key is specified | ||
| 1692 | if (defined($hash->{$key})) { | ||
| 1693 | |||
| 1694 | # if YAML strings differ | ||
| 1695 | if (YAML::Tiny::Dump($hash->{$key}) ne YAML::Tiny::Dump($color->{$key})) { | ||
| 1696 | |||
| 1697 | # print warning | ||
| 1698 | warn("$key parameter differs from source"); | ||
| 1699 | |||
| 1700 | } | ||
| 1701 | |||
| 1702 | } | ||
| 1703 | |||
| 1704 | } | ||
| 1705 | |||
| 1706 | # if 'context' and 'added' keys are undefined, and L*a*b* source has context | ||
| 1707 | if (! defined($added) && $self->[1][0][$Lab->[0]] =~ m/^(.*)\|/) { | ||
| 1708 | |||
| 1709 | # set 'added' to L*a*b* context | ||
| 1710 | $added = $1; | ||
| 1711 | |||
| 1712 | } | ||
| 1713 | |||
| 1714 | # add CTV columns slice | ||
| 1715 | $cols = add_fmt($self, map {defined($added) ? "$added|$_" : $_} qw(CTV)); | ||
| 1716 | |||
| 1717 | # get supplied illuminant white point | ||
| 1718 | $iwtpt = $hash->{'iwtpt'}; | ||
| 1719 | |||
| 1720 | # if supplied illuminant white point is valid | ||
| 1721 | if (defined($iwtpt) && (3 == grep {defined() && ! ref() && $_ > 0} @{$iwtpt})) { | ||
| 1722 | |||
| 1723 | # use it | ||
| 1724 | $WPxyz = $iwtpt; | ||
| 1725 | |||
| 1726 | # if XYZ illuminant white point is valid | ||
| 1727 | } elsif (3 == grep {defined() && ! ref() && $_ > 0} @{$self->[2][2]}[@{$Lab}]) { | ||
| 1728 | |||
| 1729 | # use it | ||
| 1730 | $WPxyz = [@{$self->[2][2]}[@{$Lab}]]; | ||
| 1731 | |||
| 1732 | } else { | ||
| 1733 | |||
| 1734 | # use D50 | ||
| 1735 | $WPxyz = ICC::Shared::D50; | ||
| 1736 | |||
| 1737 | } | ||
| 1738 | |||
| 1739 | # if media white point undefined in colorimetry array | ||
| 1740 | if (! defined($self->[2][3][$Lab->[0]])) { | ||
| 1741 | |||
| 1742 | # compute media white point or return undefined | ||
| 1743 | (_mediaWP($self, $Lab, $hash)) || return(); | ||
| 1744 | |||
| 1745 | } | ||
| 1746 | |||
| 1747 | # get media white point (Lx, Ly, Lz) | ||
| 1748 | @wtpt = ICC::Shared::_xyz2Lxyz($self->[2][3][$Lab->[0]]/$WPxyz->[0], $self->[2][3][$Lab->[1]]/$WPxyz->[1], $self->[2][3][$Lab->[2]]/$WPxyz->[2]); | ||
| 1749 | |||
| 1750 | # get device column slice | ||
| 1751 | $dev = device($self, {'context' => $hash->{'device'}}); | ||
| 1752 | |||
| 1753 | # set media white device value (255 if RGB, 0 otherwise) | ||
| 1754 | $mwv = ($self->[1][0][$dev->[0]] =~ m/RGB_R$/) ? 255 : 0; | ||
| 1755 | |||
| 1756 | # set origin | ||
| 1757 | $self->[2][0][$cols->[0]] = $Lab; | ||
| 1758 | |||
| 1759 | # save media white CTV (0) | ||
| 1760 | $self->[2][3][$cols->[0]] = 0; | ||
| 1761 | |||
| 1762 | # save colorimetry hash | ||
| 1763 | @{$self->[2][6]}[$cols->[0]] = $color; | ||
| 1764 | |||
| 1765 | # get coefficient array | ||
| 1766 | $coef = defined($hash->{'coef'}) ? $hash->{'coef'} : [1, 1, 1, 0, 0, 0]; | ||
| 1767 | |||
| 1768 | # compute denominator | ||
| 1769 | $den = $coef->[0]**2 + $coef->[1]**2 + $coef->[2]**2; | ||
| 1770 | |||
| 1771 | # compute matrix elements | ||
| 1772 | $a = ($coef->[0]**2 + $coef->[4]**2 + $coef->[5]**2)/$den; | ||
| 1773 | $b = ($coef->[1]**2 + $coef->[3]**2 + $coef->[5]**2)/$den; | ||
| 1774 | $c = ($coef->[2]**2 + $coef->[3]**2 + $coef->[4]**2)/$den; | ||
| 1775 | $d = -$coef->[5]**2/$den; | ||
| 1776 | $e = -$coef->[4]**2/$den; | ||
| 1777 | $f = -$coef->[3]**2/$den; | ||
| 1778 | |||
| 1779 | # make Mahalanobis matrix | ||
| 1780 | $mat = [ | ||
| 1781 | [$a, $d, $e], | ||
| 1782 | [$d, $b, $f], | ||
| 1783 | [$e, $f, $c] | ||
| 1784 | ]; | ||
| 1785 | |||
| 1786 | # bless the object | ||
| 1787 | bless($mat, 'Math::Matrix'); | ||
| 1788 | |||
| 1789 | # for each sample | ||
| 1790 | for my $i (1 .. $#{$self->[1]}) { | ||
| 1791 | |||
| 1792 | # if all device channels are white | ||
| 1793 | if (@{$dev} == grep {$_ == $mwv} @{$self->[1][$i]}[@{$dev}]) { | ||
| 1794 | |||
| 1795 | # save CTV (0) | ||
| 1796 | $self->[1][$i][$cols->[0]] = 0; | ||
| 1797 | |||
| 1798 | } else { | ||
| 1799 | |||
| 1800 | # compute sample Lx, Ly, Lz values | ||
| 1801 | @Ls = ICC::Shared::_Lab2Lxyz(@{$self->[1][$i]}[@{$Lab}]); | ||
| 1802 | |||
| 1803 | # save CTV (computed as Mahalanobis distance) | ||
| 1804 | $self->[1][$i][$cols->[0]] = _mahal(\@wtpt, \@Ls, $mat); | ||
| 1805 | |||
| 1806 | } | ||
| 1807 | |||
| 1808 | } | ||
| 1809 | |||
| 1810 | } else { | ||
| 1811 | |||
| 1812 | # warn | ||
| 1813 | warn('spectral, XYZ or L*a*b* data is required'); | ||
| 1814 | |||
| 1815 | # return empty | ||
| 1816 | return(); | ||
| 1817 | |||
| 1818 | } | ||
| 1819 | |||
| 1820 | # return column slice | ||
| 1821 | return($cols); | ||
| 1822 | |||
| 1823 | } | ||
| 1824 | |||
| 1825 | # append L*a*b* data to data array | ||
| 1826 | # computed from XYZ data or spectral data | ||
| 1827 | # if L*a*b* data already exists, returns that slice | ||
| 1828 | # adds XYZ data, if only spectral data exists | ||
| 1829 | # parameter: ([hash]) | ||
| 1830 | # returns: (column_slice) | ||
| 1831 | sub add_lab { | ||
| 1832 | |||
| 1833 | # get parameters | ||
| 1834 | my ($self, $hash) = @_; | ||
| 1835 | |||
| 1836 | # local variables | ||
| 1837 | my ($context, $added, $cols, $xyz, $color, $iwtpt, $WPxyz); | ||
| 1838 | |||
| 1839 | # get base context | ||
| 1840 | $context = $hash->{'context'}; | ||
| 1841 | |||
| 1842 | # get added context | ||
| 1843 | $added = defined($hash->{'added'}) ? $hash->{'added'} : $context; | ||
| 1844 | |||
| 1845 | # return column slice if L*a*b* data already exists | ||
| 1846 | return($cols) if ($cols = _cols($self, map {defined($added) ? "$added|$_" : $_} qw(LAB_L LAB_A LAB_B))); | ||
| 1847 | |||
| 1848 | # if XYZ data exists, or is added | ||
| 1849 | if ($xyz = (_cols($self, map {defined($context) ? "$context|$_" : $_} qw(XYZ_X XYZ_Y XYZ_Z)) || add_xyz($self, $hash))) { | ||
| 1850 | |||
| 1851 | # get XYZ colorimetry hash | ||
| 1852 | $color = $self->[2][6][$xyz->[0]]; | ||
| 1853 | |||
| 1854 | # for each possible colorimetry key | ||
| 1855 | for my $key (qw(illuminant observer increment bandpass cat)) { | ||
| 1856 | |||
| 1857 | # if key is specified | ||
| 1858 | if (defined($hash->{$key})) { | ||
| 1859 | |||
| 1860 | # if YAML strings differ | ||
| 1861 | if (YAML::Tiny::Dump($hash->{$key}) ne YAML::Tiny::Dump($color->{$key})) { | ||
| 1862 | |||
| 1863 | # print warning | ||
| 1864 | warn("$key parameter differs from source"); | ||
| 1865 | |||
| 1866 | } | ||
| 1867 | |||
| 1868 | } | ||
| 1869 | |||
| 1870 | } | ||
| 1871 | |||
| 1872 | # if 'context' and 'added' keys are undefined, and XYZ source has context | ||
| 1873 | if (! defined($added) && $self->[1][0][$xyz->[0]] =~ m/^(.*)\|/) { | ||
| 1874 | |||
| 1875 | # set 'added' to XYZ context | ||
| 1876 | $added = $1; | ||
| 1877 | |||
| 1878 | } | ||
| 1879 | |||
| 1880 | # add L*a*b* columns slice | ||
| 1881 | $cols = add_fmt($self, map {defined($added) ? "$added|$_" : $_} qw(LAB_L LAB_A LAB_B)); | ||
| 1882 | |||
| 1883 | # get supplied illuminant white point | ||
| 1884 | $iwtpt = $hash->{'iwtpt'}; | ||
| 1885 | |||
| 1886 | # if supplied illuminant white point is valid | ||
| 1887 | if (defined($iwtpt) && (3 == grep {defined() && ! ref() && $_ > 0} @{$iwtpt})) { | ||
| 1888 | |||
| 1889 | # use it | ||
| 1890 | $WPxyz = $iwtpt; | ||
| 1891 | |||
| 1892 | # if XYZ illuminant white point is valid | ||
| 1893 | } elsif (3 == grep {defined() && ! ref() && $_ > 0} @{$self->[2][2]}[@{$xyz}]) { | ||
| 1894 | |||
| 1895 | # use it | ||
| 1896 | $WPxyz = [@{$self->[2][2]}[@{$xyz}]]; | ||
| 1897 | |||
| 1898 | } else { | ||
| 1899 | |||
| 1900 | # use D50 | ||
| 1901 | $WPxyz = ICC::Shared::D50; | ||
| 1902 | |||
| 1903 | } | ||
| 1904 | |||
| 1905 | # set origin | ||
| 1906 | @{$self->[2][0]}[@{$cols}] = ($xyz) x 3; | ||
| 1907 | |||
| 1908 | # save illuminant white point | ||
| 1909 | @{$self->[2][2]}[@{$cols}] = @{$WPxyz}; | ||
| 1910 | |||
| 1911 | # save colorimetry hash | ||
| 1912 | @{$self->[2][6]}[@{$cols}] = ($color) x 3; | ||
| 1913 | |||
| 1914 | # for each sample | ||
| 1915 | for my $s (1 .. $#{$self->[1]}) { | ||
| 1916 | |||
| 1917 | # compute L*a*b* values from XYZ values | ||
| 1918 | @{$self->[1][$s]}[@{$cols}] = ICC::Shared::_XYZ2Lab(@{$self->[1][$s]}[@{$xyz}], $WPxyz); | ||
| 1919 | |||
| 1920 | } | ||
| 1921 | |||
| 1922 | } else { | ||
| 1923 | |||
| 1924 | # warn | ||
| 1925 | warn('spectral or XYZ data is required'); | ||
| 1926 | |||
| 1927 | # return empty | ||
| 1928 | return(); | ||
| 1929 | |||
| 1930 | } | ||
| 1931 | |||
| 1932 | # return column slice | ||
| 1933 | return($cols); | ||
| 1934 | |||
| 1935 | } | ||
| 1936 | |||
| 1937 | # append XYZ data to data array | ||
| 1938 | # computed from spectral data or L*a*b* data | ||
| 1939 | # if XYZ data already exists, returns that slice | ||
| 1940 | # default colorimetry is D50, 2 degree observer | ||
| 1941 | # parameters: ([hash]) | ||
| 1942 | # returns: (column_slice) | ||
| 1943 | sub add_xyz { | ||
| 1944 | |||
| 1945 | # get parameters | ||
| 1946 | my ($self, $hash) = @_; | ||
| 1947 | |||
| 1948 | # local variables | ||
| 1949 | my ($oba, $spec1, $spec2, $context, $added); | ||
| 1950 | my ($spec, $color, $illum, $specv, $nm, $cols); | ||
| 1951 | my ($cat, $spectral, $xyz, $Lab, @WPlab, $WPxyz); | ||
| 1952 | |||
| 1953 | # if 'oba' defined | ||
| 1954 | if (defined($hash->{'oba'})) { | ||
| 1955 | |||
| 1956 | # get oba factor | ||
| 1957 | $oba = $hash->{'oba'}; | ||
| 1958 | |||
| 1959 | # if 'context' defined | ||
| 1960 | if (defined($hash->{'context'})) { | ||
| 1961 | |||
| 1962 | # if context an array reference containing two scalars | ||
| 1963 | if (ref($hash->{'context'}) eq 'ARRAY' && ! ref($hash->{'context'}->[0]) && ! ref($hash->{'context'}->[1])) { | ||
| 1964 | |||
| 1965 | # get specified 'M1' spectral slice | ||
| 1966 | $spec1 = spectral($self, {'context' => $hash->{'context'}->[0]}); | ||
| 1967 | |||
| 1968 | # get specified 'M2' spectral slice | ||
| 1969 | $spec2 = spectral($self, {'context' => $hash->{'context'}->[1]}); | ||
| 1970 | |||
| 1971 | # use specified 'M2' context | ||
| 1972 | $context = $hash->{'context'}->[1]; | ||
| 1973 | |||
| 1974 | } else { | ||
| 1975 | |||
| 1976 | # warn | ||
| 1977 | warn('OBA context is an array reference containing M1 and M2 contexts'); | ||
| 1978 | |||
| 1979 | # return empty | ||
| 1980 | return(); | ||
| 1981 | |||
| 1982 | } | ||
| 1983 | |||
| 1984 | } else { | ||
| 1985 | |||
| 1986 | # get spectral slice using standard 'M1' context | ||
| 1987 | $spec1 = spectral($self, {'context' => 'M1_Measurement'}); | ||
| 1988 | |||
| 1989 | # get spectral slice using standard 'M2' context | ||
| 1990 | $spec2 = spectral($self, {'context' => 'M2_Measurement'}); | ||
| 1991 | |||
| 1992 | # use standard 'M2' context | ||
| 1993 | $context = 'M2_Measurement'; | ||
| 1994 | |||
| 1995 | } | ||
| 1996 | |||
| 1997 | # verify spectral slices | ||
| 1998 | if (! $spec1 || ! $spec2 || $#{$spec1} != $#{$spec2}) { | ||
| 1999 | |||
| 2000 | # warn | ||
| 2001 | warn('M1 and M2 spectral data required for OBA effect'); | ||
| 2002 | |||
| 2003 | # return empty | ||
| 2004 | return(); | ||
| 2005 | |||
| 2006 | } | ||
| 2007 | |||
| 2008 | # get added context | ||
| 2009 | $added = defined($hash->{'added'}) ? $hash->{'added'} : 'OBA'; | ||
| 2010 | |||
| 2011 | } else { | ||
| 2012 | |||
| 2013 | # get base context | ||
| 2014 | $context = $hash->{'context'}; | ||
| 2015 | |||
| 2016 | # get added context | ||
| 2017 | $added = defined($hash->{'added'}) ? $hash->{'added'} : $context; | ||
| 2018 | |||
| 2019 | } | ||
| 2020 | |||
| 2021 | # return column slice if XYZ data already exists | ||
| 2022 | return($cols) if ($cols = _cols($self, map {defined($added) ? "$added|$_" : $_} qw(XYZ_X XYZ_Y XYZ_Z))); | ||
| 2023 | |||
| 2024 | # if spectral data exists | ||
| 2025 | if (test($self, 'SPECTRAL', $context)) { | ||
| 2026 | |||
| 2027 | # get spectral slice | ||
| 2028 | $spec = spectral($self, {'context' => $context}); | ||
| 2029 | |||
| 2030 | # add chart wavelength range to hash | ||
| 2031 | $hash->{'range'} = nm($self, {'context' => $context}); | ||
| 2032 | |||
| 2033 | # make empty 'Color.pm' object | ||
| 2034 | $color = ICC::Support::Color->new(); | ||
| 2035 | |||
| 2036 | # if illuminant is defined, an array reference | ||
| 2037 | if (defined($hash->{'illuminant'}) && ref($hash->{'illuminant'}) eq 'ARRAY') { | ||
| 2038 | |||
| 2039 | # if illuminant is ['DATA'] (ProfileMaker convention) | ||
| 2040 | if (defined($hash->{'illuminant'}->[0]) && $hash->{'illuminant'}->[0] eq 'DATA') { | ||
| 2041 | |||
| 2042 | # verify chart object contains illuminant data | ||
| 2043 | (defined($self->[0]{'illuminant'}) && ref($self->[0]{'illuminant'}) eq 'ARRAY') || croak('no illuminant data'); | ||
| 2044 | |||
| 2045 | # make new chart object from illuminant data | ||
| 2046 | $illum = ICC::Support::Chart->new($self->[0]{'illuminant'}); | ||
| 2047 | |||
| 2048 | # get spectral values | ||
| 2049 | ($specv = $illum->spectral([1])->[0]) || croak('illuminant chart has no spectral data'); | ||
| 2050 | |||
| 2051 | # get wavelength range | ||
| 2052 | $nm = $illum->nm(); | ||
| 2053 | |||
| 2054 | # update 'illuminant' value in hash | ||
| 2055 | $hash->{'illuminant'} = [$nm, $specv]; | ||
| 2056 | |||
| 2057 | } | ||
| 2058 | |||
| 2059 | # initialize object for CIE method | ||
| 2060 | ICC::Support::Color::_cie($color, $hash); | ||
| 2061 | |||
| 2062 | } else { | ||
| 2063 | |||
| 2064 | # initialize object for ASTM method | ||
| 2065 | ICC::Support::Color::_astm($color, $hash); | ||
| 2066 | |||
| 2067 | } | ||
| 2068 | |||
| 2069 | # if 'context' and 'added' keys are undefined, and spectral source has context | ||
| 2070 | if (! defined($added) && $self->[1][0][$spec->[0]] =~ m/^(.*)\|/) { | ||
| 2071 | |||
| 2072 | # set 'added' to spectral context | ||
| 2073 | $added = $1; | ||
| 2074 | |||
| 2075 | } | ||
| 2076 | |||
| 2077 | # add XYZ columns slice | ||
| 2078 | $cols = add_fmt($self, map {defined($added) ? "$added|$_" : $_} qw(XYZ_X XYZ_Y XYZ_Z)); | ||
| 2079 | |||
| 2080 | # set origin | ||
| 2081 | @{$self->[2][0]}[@{$cols}] = ($spec) x 3; | ||
| 2082 | |||
| 2083 | # save reference to Color.pm object | ||
| 2084 | @{$self->[2][1]}[@{$cols}] = ($color) x 3; | ||
| 2085 | |||
| 2086 | # if chromatic adaptation transform (cat) is specified | ||
| 2087 | if (defined($hash->{'cat'})) { | ||
| 2088 | |||
| 2089 | # if cat is 'matf' object | ||
| 2090 | if (UNIVERSAL::isa($hash->{'cat'}, 'ICC::Profile::matf')) { | ||
| 2091 | |||
| 2092 | # use it | ||
| 2093 | $cat = $hash->{'cat'}; | ||
| 2094 | |||
| 2095 | # if cat is 'bradford' | ||
| 2096 | } elsif ($hash->{'cat'} eq 'bradford') { | ||
| 2097 | |||
| 2098 | # make 'bradford' object | ||
| 2099 | $cat = ICC::Profile::matf->bradford($color->iwtpt()); | ||
| 2100 | |||
| 2101 | # if cat is 'cat02' | ||
| 2102 | } elsif ($hash->{'cat'} eq 'cat02') { | ||
| 2103 | |||
| 2104 | # make 'cat02' object | ||
| 2105 | $cat = ICC::Profile::matf->cat02($color->iwtpt()); | ||
| 2106 | |||
| 2107 | # if cat is 'quasi' | ||
| 2108 | } elsif ($hash->{'cat'} eq 'quasi') { | ||
| 2109 | |||
| 2110 | # make 'quasi' object | ||
| 2111 | $cat = ICC::Profile::matf->quasi($color->iwtpt()); | ||
| 2112 | |||
| 2113 | } else { | ||
| 2114 | |||
| 2115 | # warn | ||
| 2116 | warn('invalid cat type'); | ||
| 2117 | |||
| 2118 | } | ||
| 2119 | |||
| 2120 | } | ||
| 2121 | |||
| 2122 | # if cat defined | ||
| 2123 | if (defined($cat)) { | ||
| 2124 | |||
| 2125 | # save cat reference | ||
| 2126 | @{$self->[2][2]}[@{$cols}] = ($cat) x 3; | ||
| 2127 | |||
| 2128 | } else { | ||
| 2129 | |||
| 2130 | # save white point | ||
| 2131 | @{$self->[2][2]}[@{$cols}] = @{$color->iwtpt()}; | ||
| 2132 | |||
| 2133 | } | ||
| 2134 | |||
| 2135 | # save colorimetry hash | ||
| 2136 | @{$self->[2][6]}[@{$cols}] = ({map {defined($hash->{$_}) ? ($_, $hash->{$_}) : ()} qw(illuminant observer bandpass method ibandpass imethod oba cat increment range encoding)}) x 3; | ||
| 2137 | |||
| 2138 | # for each sample | ||
| 2139 | for my $i (1 .. $#{$self->[1]}) { | ||
| 2140 | |||
| 2141 | # get spectral slice | ||
| 2142 | $spectral->[$i - 1] = [@{$self->[1][$i]}[@{$spec}]]; | ||
| 2143 | |||
| 2144 | } | ||
| 2145 | |||
| 2146 | # transform to XYZ data (hash may contain 'encoding' key) | ||
| 2147 | $xyz = ICC::Support::Color::_trans2($color, $spectral, $hash); | ||
| 2148 | |||
| 2149 | # add OBA effect, if enabled | ||
| 2150 | _add_oba($self, $spec1, $spec2, $xyz, $oba, $hash) if $oba; | ||
| 2151 | |||
| 2152 | # for each sample | ||
| 2153 | for my $i (1 .. $#{$self->[1]}) { | ||
| 2154 | |||
| 2155 | # if cat defined | ||
| 2156 | if (defined($cat)) { | ||
| 2157 | |||
| 2158 | # set XYZ slice with cat | ||
| 2159 | @{$self->[1][$i]}[@{$cols}] = ICC::Profile::matf::_trans0($cat, @{$xyz->[$i - 1]}); | ||
| 2160 | |||
| 2161 | } else { | ||
| 2162 | |||
| 2163 | # set XYZ slice | ||
| 2164 | @{$self->[1][$i]}[@{$cols}] = @{$xyz->[$i - 1]}; | ||
| 2165 | |||
| 2166 | } | ||
| 2167 | |||
| 2168 | } | ||
| 2169 | |||
| 2170 | # if L*a*b* data exists | ||
| 2171 | } elsif (test($self, 'LAB', $context)) { | ||
| 2172 | |||
| 2173 | # warn if illuminant is specified | ||
| 2174 | (! defined($hash->{'illuminant'})) || warn('illuminant specified but no spectral data!'); | ||
| 2175 | |||
| 2176 | # get L*a*b* slice | ||
| 2177 | $Lab = cols($self, map {defined($context) ? "$context|$_" : $_} qw(LAB_L LAB_A LAB_B)); | ||
| 2178 | |||
| 2179 | # if 'context' and 'added' keys are undefined, and L*a*b* source has context | ||
| 2180 | if (! defined($added) && $self->[1][0][$Lab->[0]] =~ m/^(.*)\|/) { | ||
| 2181 | |||
| 2182 | # set 'added' to L*a*b* context | ||
| 2183 | $added = $1; | ||
| 2184 | |||
| 2185 | } | ||
| 2186 | |||
| 2187 | # get L*a*b* white point values | ||
| 2188 | @WPlab = @{$self->[2][2]}[@{$Lab}]; | ||
| 2189 | |||
| 2190 | # use scalar values or D50 | ||
| 2191 | $WPxyz = (3 == grep {defined() && ! ref() && $_ > 0} @WPlab) ? [@WPlab] : ICC::Shared::D50; | ||
| 2192 | |||
| 2193 | # add XYZ columns slice | ||
| 2194 | $cols = add_fmt($self, map {defined($added) ? "$added|$_" : $_} qw(XYZ_X XYZ_Y XYZ_Z)); | ||
| 2195 | |||
| 2196 | # set origin | ||
| 2197 | @{$self->[2][0]}[@{$cols}] = ($Lab) x 3; | ||
| 2198 | |||
| 2199 | # save illuminant white point | ||
| 2200 | @{$self->[2][2]}[@{$cols}] = @{$WPxyz}; | ||
| 2201 | |||
| 2202 | # for each sample | ||
| 2203 | for my $s (1 .. $#{$self->[1]}) { | ||
| 2204 | |||
| 2205 | # compute XYZ values from L*a*b* values | ||
| 2206 | @{$self->[1][$s]}[@{$cols}] = ICC::Shared::_Lab2XYZ(@{$self->[1][$s]}[@{$Lab}], $WPxyz); | ||
| 2207 | |||
| 2208 | } | ||
| 2209 | |||
| 2210 | } else { | ||
| 2211 | |||
| 2212 | # warn | ||
| 2213 | warn('spectral or L*a*b* data is required'); | ||
| 2214 | |||
| 2215 | # return empty | ||
| 2216 | return(); | ||
| 2217 | |||
| 2218 | } | ||
| 2219 | |||
| 2220 | # return column slice | ||
| 2221 | return($cols); | ||
| 2222 | |||
| 2223 | } | ||
| 2224 | |||
| 2225 | # append ISO 5-3 density data to data array | ||
| 2226 | # computed from spectral data only | ||
| 2227 | # if density data already exists, return that slice | ||
| 2228 | # default status is 'T', encoding is 'density' | ||
| 2229 | # parameters: ([hash]) | ||
| 2230 | # returns: (column_slice) | ||
| 2231 | sub add_density { | ||
| 2232 | |||
| 2233 | # get parameters | ||
| 2234 | my ($self, $hash) = @_; | ||
| 2235 | |||
| 2236 | # local variables | ||
| 2237 | my ($context, $added, $encode, $fp, $cols, $spec, $temp, $color, $spectral, $rgbv); | ||
| 2238 | |||
| 2239 | # get base context | ||
| 2240 | $context = $hash->{'context'}; | ||
| 2241 | |||
| 2242 | # get added context | ||
| 2243 | $added = defined($hash->{'added'}) ? $hash->{'added'} : $context; | ||
| 2244 | |||
| 2245 | # get encoding | ||
| 2246 | $encode = $hash->{'encoding'} // 'density'; | ||
| 2247 | |||
| 2248 | # if invalid encoding | ||
| 2249 | if ($encode ne 'density' && $encode ne 'linear') { | ||
| 2250 | |||
| 2251 | # warn | ||
| 2252 | warn('invalid density encoding, using \'density\''); | ||
| 2253 | |||
| 2254 | # set encoding | ||
| 2255 | $encode = 'density'; | ||
| 2256 | |||
| 2257 | } | ||
| 2258 | |||
| 2259 | # set format prefix | ||
| 2260 | $fp = $encode eq 'density' ? 'D' : 'R'; | ||
| 2261 | |||
| 2262 | # return column slice if density/reflectance data already exists | ||
| 2263 | return($cols) if ($cols = cols($self, map {defined($added) ? "$added|$fp$_" : "$fp$_"} qw(_RED _GREEN _BLUE _VIS))); | ||
| 2264 | |||
| 2265 | # if spectral data | ||
| 2266 | if (test($self, 'SPECTRAL', $context)) { | ||
| 2267 | |||
| 2268 | # get spectral slice | ||
| 2269 | $spec = spectral($self, $hash); | ||
| 2270 | |||
| 2271 | # make copy of hash | ||
| 2272 | $temp = Storable::dclone($hash); | ||
| 2273 | |||
| 2274 | # add chart wavelength range to hash | ||
| 2275 | $temp->{'range'} = nm($self, $hash); | ||
| 2276 | |||
| 2277 | # make empty 'Color.pm' object | ||
| 2278 | $color = ICC::Support::Color->new(); | ||
| 2279 | |||
| 2280 | # initialize object for ISO 5-3 method | ||
| 2281 | ICC::Support::Color::_iso($color, $temp); | ||
| 2282 | |||
| 2283 | # if 'context' and 'added' keys are undefined, and spectral source has context | ||
| 2284 | if (! defined($added) && $self->[1][0][$spec->[0]] =~ m/^(.*)\|/) { | ||
| 2285 | |||
| 2286 | # set 'added' to spectral context | ||
| 2287 | $added = $1; | ||
| 2288 | |||
| 2289 | } | ||
| 2290 | |||
| 2291 | # add density/reflectance columns slice | ||
| 2292 | $cols = add_fmt($self, map {defined($added) ? "$added|$fp$_" : "$fp$_"} qw(_RED _GREEN _BLUE _VIS)); | ||
| 2293 | |||
| 2294 | # set origin | ||
| 2295 | @{$self->[2][0]}[@{$cols}] = ($spec) x 4; | ||
| 2296 | |||
| 2297 | # save reference to Color.pm object | ||
| 2298 | @{$self->[2][1]}[@{$cols}] = ($color) x 4; | ||
| 2299 | |||
| 2300 | # save colorimetry hash | ||
| 2301 | @{$self->[2][6]}[@{$cols}] = ({map {defined($temp->{$_}) ? ($_, $temp->{$_}) : ()} qw(status increment range encoding)}) x 4; | ||
| 2302 | |||
| 2303 | # for each sample | ||
| 2304 | for my $i (1 .. $#{$self->[1]}) { | ||
| 2305 | |||
| 2306 | # get spectral slice | ||
| 2307 | $spectral->[$i - 1] = [@{$self->[1][$i]}[@{$spec}]]; | ||
| 2308 | |||
| 2309 | } | ||
| 2310 | |||
| 2311 | # set encoding | ||
| 2312 | $temp->{'encoding'} = $encode; | ||
| 2313 | |||
| 2314 | # transform to density/reflectance data (per encoding) | ||
| 2315 | $rgbv = ICC::Support::Color::_trans2($color, $spectral, $temp); | ||
| 2316 | |||
| 2317 | # for each sample | ||
| 2318 | for my $i (1 .. $#{$self->[1]}) { | ||
| 2319 | |||
| 2320 | # set data values | ||
| 2321 | @{$self->[1][$i]}[@{$cols}] = @{$rgbv->[$i - 1]}; | ||
| 2322 | |||
| 2323 | } | ||
| 2324 | |||
| 2325 | } else { | ||
| 2326 | |||
| 2327 | # warn | ||
| 2328 | warn('spectral data is required'); | ||
| 2329 | |||
| 2330 | # return empty | ||
| 2331 | return(); | ||
| 2332 | |||
| 2333 | } | ||
| 2334 | |||
| 2335 | # return column slice | ||
| 2336 | return($cols); | ||
| 2337 | |||
| 2338 | } | ||
| 2339 | |||
| 2340 | # add computed values to data array | ||
| 2341 | # processing is done by a user-defined function (udf) | ||
| 2342 | # data groups are defined by one or more column slice(s) | ||
| 2343 | # supported hash keys: 'element', 'sample', 'device', 'rows', 'start', 'added' | ||
| 2344 | # either an 'element' udf or a 'sample' udf are required, but not both | ||
| 2345 | # an 'element' udf computes a single value from single slice value(s) | ||
| 2346 | # a 'sample' udf computes all values at once from slice value array(s) | ||
| 2347 | # setting the 'device' flag converts RGB/CMYK/nCLR values to device values | ||
| 2348 | # the 'rows' parameter is the row slice computed, default is all rows | ||
| 2349 | # the 'start' parameter is the first column computed, default is to append | ||
| 2350 | # the 'added' parameter may be a scalar or an array reference | ||
| 2351 | # an 'added' scalar will be used as a context prefix | ||
| 2352 | # an 'added' array must be the same size as the columns added | ||
| 2353 | # parameters: (column_slice_0, column_slice_1, ... hash) | ||
| 2354 | # returns: (added_column_slice) | ||
| 2355 | sub add_udf { | ||
| 2356 | |||
| 2357 | # local variables | ||
| 2358 | my ($self, $hash, @cs, $rows, $m, $n, @div, $udfe, $udfs); | ||
| 2359 | my (@p, @u, @s, $cx, $added); | ||
| 2360 | |||
| 2361 | # get object reference | ||
| 2362 | $self = shift(); | ||
| 2363 | |||
| 2364 | # get parameter hash | ||
| 2365 | $hash = pop(); | ||
| 2366 | |||
| 2367 | # verify a hash reference | ||
| 2368 | (ref($hash) eq 'HASH') || croak('last parameter must be a hash reference'); | ||
| 2369 | |||
| 2370 | # verify number of slices | ||
| 2371 | (@cs = @_) || croak('one or more column slices are required'); | ||
| 2372 | |||
| 2373 | # get row slice, all rows by default | ||
| 2374 | $rows = defined($hash->{'rows'}) ? $hash->{'rows'} : []; | ||
| 2375 | |||
| 2376 | # if row slice an empty array reference | ||
| 2377 | if (ref($rows) eq 'ARRAY' && @{$rows} == 0) { | ||
| 2378 | |||
| 2379 | # use all rows | ||
| 2380 | $rows = [1 .. $#{$self->[1]}]; | ||
| 2381 | |||
| 2382 | } else { | ||
| 2383 | |||
| 2384 | # flatten row slice | ||
| 2385 | $rows = ICC::Shared::flatten($rows); | ||
| 2386 | |||
| 2387 | # verify row slice contents | ||
| 2388 | (@{$rows} == grep {Scalar::Util::looks_like_number($_) && int($_) == $_ && $_ > 0 && $_ <= $#{$self->[1]}} @{$rows}) || croak('invalid row slice'); | ||
| 2389 | |||
| 2390 | } | ||
| 2391 | |||
| 2392 | # get starting column, append by default | ||
| 2393 | $n = defined($hash->{'start'}) ? $hash->{'start'} : $#{$self->[1][0]} + 1; | ||
| 2394 | |||
| 2395 | # if an array reference (slice), use the first value | ||
| 2396 | $n = $n->[0] if (ref($n) eq 'ARRAY'); | ||
| 2397 | |||
| 2398 | # verify starting column | ||
| 2399 | (Scalar::Util::looks_like_number($n) && int($n) eq $n && $n >= 0) || croak('invalid \'start\' parameter'); | ||
| 2400 | |||
| 2401 | # if 'device' flag | ||
| 2402 | if ($hash->{'device'}) { | ||
| 2403 | |||
| 2404 | # for each data format | ||
| 2405 | for my $i (0 .. $#{$self->[1][0]}) { | ||
| 2406 | |||
| 2407 | # set divisor to 255 for RGB data | ||
| 2408 | $div[$i] = 255 if ($self->[1][0][$i] =~ m/RGB_[RGB]$/); | ||
| 2409 | |||
| 2410 | # set divisor to 100 for CMYK data | ||
| 2411 | $div[$i] = 100 if ($self->[1][0][$i] =~ m/CMYK_[CMYK]$/); | ||
| 2412 | |||
| 2413 | # set divisor to 100 for nCLR data | ||
| 2414 | $div[$i] = 100 if ($self->[1][0][$i] =~ m/[2-9A-F]CLR_[1-9A-F]$/); | ||
| 2415 | } | ||
| 2416 | |||
| 2417 | } | ||
| 2418 | |||
| 2419 | # get udf CODE refs | ||
| 2420 | $udfe = $hash->{'element'}; | ||
| 2421 | $udfs = $hash->{'sample'}; | ||
| 2422 | |||
| 2423 | # if both udfs defined | ||
| 2424 | if (defined($udfe) && defined($udfs)) { | ||
| 2425 | |||
| 2426 | # error | ||
| 2427 | croak('both \'element\' and \'sample\' udfs are defined'); | ||
| 2428 | |||
| 2429 | # if 'element' udf defined | ||
| 2430 | } elsif (defined($udfe)) { | ||
| 2431 | |||
| 2432 | # verify udf is a code reference | ||
| 2433 | (ref($udfe) eq 'CODE') || croak('\'element\' udf is not a CODE reference'); | ||
| 2434 | |||
| 2435 | # for each parameter | ||
| 2436 | for my $i (0 .. $#cs) { | ||
| 2437 | |||
| 2438 | # if an array reference | ||
| 2439 | if (ref($cs[$i]) eq 'ARRAY') { | ||
| 2440 | |||
| 2441 | # if first slice | ||
| 2442 | if (! defined($m)) { | ||
| 2443 | |||
| 2444 | # get upper index | ||
| 2445 | $m = $#{$cs[0]}; | ||
| 2446 | |||
| 2447 | # compute added slice | ||
| 2448 | @s = ($n .. $n + $m); | ||
| 2449 | |||
| 2450 | } else { | ||
| 2451 | |||
| 2452 | # verify slice size | ||
| 2453 | ($#{$cs[$i]} == $m) || croak('column slices are different sizes'); | ||
| 2454 | |||
| 2455 | } | ||
| 2456 | |||
| 2457 | # verify a valid column slice | ||
| 2458 | (ref($cs[$i]) eq 'ARRAY' || @{$cs[$i]} == grep {Scalar::Util::looks_like_number($_) && int($_) == $_ && $_ >= 0 && $_ <= $#{$self->[1][0]}} @{$cs[$i]}) || croak('invalid column slice'); | ||
| 2459 | |||
| 2460 | # if a scalar | ||
| 2461 | } elsif (! ref($cs[$i])) { | ||
| 2462 | |||
| 2463 | # verify a valid column index | ||
| 2464 | (Scalar::Util::looks_like_number($cs[$i]) && int($cs[$i]) == $cs[$i] && $cs[$i] >= 0 && $cs[$i] <= $#{$self->[1][0]}) || croak('invalid column index'); | ||
| 2465 | |||
| 2466 | } else { | ||
| 2467 | |||
| 2468 | # error | ||
| 2469 | croak('parameter must be a scalar or an array reference'); | ||
| 2470 | |||
| 2471 | } | ||
| 2472 | |||
| 2473 | } | ||
| 2474 | |||
| 2475 | # verify at least one column slice parameter | ||
| 2476 | (defined($m)) || croak('at least one column slice is required'); | ||
| 2477 | |||
| 2478 | # for each sample | ||
| 2479 | for my $i (@{$rows}) { | ||
| 2480 | |||
| 2481 | # for each column index | ||
| 2482 | for my $j (0 .. $m) { | ||
| 2483 | |||
| 2484 | # for each parameter | ||
| 2485 | for my $k (0 .. $#cs) { | ||
| 2486 | |||
| 2487 | # get column index (slice -or- scalar) | ||
| 2488 | $cx = (ref($cs[$k]) eq 'ARRAY') ? $cs[$k][$j] : $cs[$k]; | ||
| 2489 | |||
| 2490 | # get parameter value | ||
| 2491 | $p[$k] = $self->[1][$i][$cx]; | ||
| 2492 | |||
| 2493 | # adjust device values, if divisor defined | ||
| 2494 | $p[$k] /= $div[$cx] if defined($div[$cx]); | ||
| 2495 | |||
| 2496 | } | ||
| 2497 | |||
| 2498 | # call 'element' udf | ||
| 2499 | $self->[1][$i][$n + $j] = &$udfe(@p, $j); | ||
| 2500 | |||
| 2501 | } | ||
| 2502 | |||
| 2503 | } | ||
| 2504 | |||
| 2505 | # if 'sample' udf defined | ||
| 2506 | } elsif (defined($udfs)) { | ||
| 2507 | |||
| 2508 | # verify udf is a code reference | ||
| 2509 | (ref($udfs) eq 'CODE') || croak('\'sample\' udf is not a CODE reference'); | ||
| 2510 | |||
| 2511 | # for each parameter | ||
| 2512 | for my $i (0 .. $#cs) { | ||
| 2513 | |||
| 2514 | # verify a valid column slice | ||
| 2515 | (ref($cs[$i]) eq 'ARRAY' || @{$cs[$i]} == grep {Scalar::Util::looks_like_number($_) && int($_) == $_ && $_ >= 0 && $_ <= $#{$self->[1][0]}} @{$cs[$i]}) || croak('invalid column slice'); | ||
| 2516 | |||
| 2517 | } | ||
| 2518 | |||
| 2519 | # verify at least one parameter | ||
| 2520 | (@cs) || croak('at least one column slice is required'); | ||
| 2521 | |||
| 2522 | # for each sample | ||
| 2523 | for my $i (@{$rows}) { | ||
| 2524 | |||
| 2525 | # for each column slice | ||
| 2526 | for my $j (0 .. $#cs) { | ||
| 2527 | |||
| 2528 | # for each slice element | ||
| 2529 | for my $k (0 .. $#{$cs[$j]}) { | ||
| 2530 | |||
| 2531 | # get column index | ||
| 2532 | $cx = $cs[$j][$k]; | ||
| 2533 | |||
| 2534 | # get parameter value | ||
| 2535 | $p[$j][$k] = $self->[1][$i][$cx]; | ||
| 2536 | |||
| 2537 | # adjust device values, if divisor defined | ||
| 2538 | $p[$j][$k] /= $div[$cx] if defined($div[$cx]); | ||
| 2539 | |||
| 2540 | } | ||
| 2541 | |||
| 2542 | } | ||
| 2543 | |||
| 2544 | # if first sample | ||
| 2545 | if (! defined($m)) { | ||
| 2546 | |||
| 2547 | # call 'sample' udf | ||
| 2548 | @u = &$udfs(@p); | ||
| 2549 | |||
| 2550 | # get upper index | ||
| 2551 | $m = $#u; | ||
| 2552 | |||
| 2553 | # compute added slice | ||
| 2554 | @s = ($n .. $n + $m); | ||
| 2555 | |||
| 2556 | # copy values to object | ||
| 2557 | @{$self->[1][$i]}[@s] = @u; | ||
| 2558 | |||
| 2559 | } else { | ||
| 2560 | |||
| 2561 | # call 'sample' udf | ||
| 2562 | @{$self->[1][$i]}[@s] = &$udfs(@p); | ||
| 2563 | |||
| 2564 | } | ||
| 2565 | |||
| 2566 | } | ||
| 2567 | |||
| 2568 | } else { | ||
| 2569 | |||
| 2570 | # error | ||
| 2571 | croak('no udf is defined'); | ||
| 2572 | |||
| 2573 | } | ||
| 2574 | |||
| 2575 | # get 'added' parameter, default is 'udf', could be undefined | ||
| 2576 | $added = exists($hash->{'added'}) ? $hash->{'added'} : 'udf'; | ||
| 2577 | |||
| 2578 | # if 'added' is undefined | ||
| 2579 | if (! defined($added)) { | ||
| 2580 | |||
| 2581 | # if 'element' udf -or- size of first column slice equals number of added columns | ||
| 2582 | if (defined($udfe) || @{$cs[0]} == @s) { | ||
| 2583 | |||
| 2584 | # add data format stripping context from first column slice | ||
| 2585 | @{$self->[1][0]}[@s] = map {m/^(?:.*\|)?(.*)$/; $1} @{$self->[1][0]}[@{$cs[0]}]; | ||
| 2586 | |||
| 2587 | } else { | ||
| 2588 | |||
| 2589 | # add data format as 'colxxx' | ||
| 2590 | @{$self->[1][0]}[@s] = map {"col$_"} @s; | ||
| 2591 | |||
| 2592 | } | ||
| 2593 | |||
| 2594 | # if 'added' a scalar | ||
| 2595 | } elsif (! ref($added)) { | ||
| 2596 | |||
| 2597 | # if 'element' udf -or- size of first column slice equals number of added columns | ||
| 2598 | if (defined($udfe) || @{$cs[0]} == @s) { | ||
| 2599 | |||
| 2600 | # add data format using 'added' as context with first column slice format keys | ||
| 2601 | @{$self->[1][0]}[@s] = map {m/^(?:.*\|)?(.*)$/; "$added|$1"} @{$self->[1][0]}[@{$cs[0]}]; | ||
| 2602 | |||
| 2603 | } else { | ||
| 2604 | |||
| 2605 | # add data format using 'added' as context to 'colxxx' | ||
| 2606 | @{$self->[1][0]}[@s] = map {"$added|col$_"} @s; | ||
| 2607 | |||
| 2608 | } | ||
| 2609 | |||
| 2610 | # if 'added' is an array ref and size equals number of added columns | ||
| 2611 | } elsif (ref($added) eq 'ARRAY' && @{$added} == @s) { | ||
| 2612 | |||
| 2613 | # add data format using 'added' as array | ||
| 2614 | @{$self->[1][0]}[@s] = @{$added}; | ||
| 2615 | |||
| 2616 | } else { | ||
| 2617 | |||
| 2618 | # error | ||
| 2619 | croak('invalid \'added\' parameter'); | ||
| 2620 | |||
| 2621 | } | ||
| 2622 | |||
| 2623 | # return added column slice | ||
| 2624 | return([@s]); | ||
| 2625 | |||
| 2626 | } | ||
| 2627 | |||
| 2628 | # append date column to data array | ||
| 2629 | # adds same date/time to each sample | ||
| 2630 | # supported hash keys: 'date', 'format', 'added' | ||
| 2631 | # parameter: ([hash]) | ||
| 2632 | # returns: (column_slice) | ||
| 2633 | sub add_date { | ||
| 2634 | |||
| 2635 | # get parameters | ||
| 2636 | my ($self, $hash) = @_; | ||
| 2637 | |||
| 2638 | # local variables | ||
| 2639 | my ($cols, $added, $date, $fmt, $str); | ||
| 2640 | |||
| 2641 | # get added context | ||
| 2642 | $added = $hash->{'added'}; | ||
| 2643 | |||
| 2644 | # return column slice if date column already exists | ||
| 2645 | return($cols) if ($cols = _cols($self, defined($added) ? "$added|CREATED" : 'CREATED')); | ||
| 2646 | |||
| 2647 | # add date column slice | ||
| 2648 | $cols = add_fmt($self, defined($added) ? "$added|CREATED" : 'CREATED'); | ||
| 2649 | |||
| 2650 | # if date supplied | ||
| 2651 | if (defined($date = $hash->{'date'})) { | ||
| 2652 | |||
| 2653 | # if date is a number | ||
| 2654 | if (Scalar::Util::looks_like_number($date)) { | ||
| 2655 | |||
| 2656 | # make Time::Piece object | ||
| 2657 | $date = localtime($date); | ||
| 2658 | |||
| 2659 | # if date not a Time::Piece object | ||
| 2660 | } elsif (ref($date) ne 'Time::Piece') { | ||
| 2661 | |||
| 2662 | # error | ||
| 2663 | croak('invalid date parameter'); | ||
| 2664 | } | ||
| 2665 | |||
| 2666 | } else { | ||
| 2667 | |||
| 2668 | # use 'CREATED' value from chart | ||
| 2669 | $date = created($self); | ||
| 2670 | |||
| 2671 | } | ||
| 2672 | |||
| 2673 | # compute the date/time string (same for each sample) | ||
| 2674 | $str = defined($fmt = $hash->{'format'}) ? $date->strftime($fmt) : $date->epoch(); | ||
| 2675 | |||
| 2676 | # for each row | ||
| 2677 | for my $i (1 .. $#{$self->[1]}) { | ||
| 2678 | |||
| 2679 | # set the date time string | ||
| 2680 | $self->[1][$i][$cols->[0]] = $str; | ||
| 2681 | |||
| 2682 | } | ||
| 2683 | |||
| 2684 | # return column slice | ||
| 2685 | return($cols); | ||
| 2686 | |||
| 2687 | } | ||
| 2688 | |||
| 2689 | # splice rows into data array | ||
| 2690 | # offset and length are as used by Perl's 'splice' function | ||
| 2691 | # data matrix is the 2-D array of data values to be spliced | ||
| 2692 | # column slice is a reference to an array of data matrix column indices | ||
| 2693 | # parameters: ([offset, [length, [data_matrix, [column_slice]]]]) | ||
| 2694 | # returns: (removed_data_matrix) | ||
| 2695 | sub splice_rows { | ||
| 2696 | |||
| 2697 | # get parameters | ||
| 2698 | my ($self, $offset, $length, $matrix, $cols) = @_; | ||
| 2699 | |||
| 2700 | # local variables | ||
| 2701 | my (@ix, @list, @s, $removed); | ||
| 2702 | |||
| 2703 | # if offset supplied | ||
| 2704 | if (defined($offset)) { | ||
| 2705 | |||
| 2706 | # verify offset a scalar | ||
| 2707 | (! ref($offset) && (int($offset) == $offset)) || croak('invalid offset parameter'); | ||
| 2708 | |||
| 2709 | } | ||
| 2710 | |||
| 2711 | # if length supplied | ||
| 2712 | if (defined($length)) { | ||
| 2713 | |||
| 2714 | # verify length an integer scalar | ||
| 2715 | (! ref($length) && int($length) == $length) || croak('invalid length parameter'); | ||
| 2716 | |||
| 2717 | } | ||
| 2718 | |||
| 2719 | # if matrix supplied | ||
| 2720 | if (defined($matrix)) { | ||
| 2721 | |||
| 2722 | # verify matrix a 2-D array or Math::Matrix object | ||
| 2723 | (ref($matrix) eq 'ARRAY' && ref($matrix->[0]) eq 'ARRAY') || (UNIVERSAL::isa($matrix, 'Math::Matrix')) || croak ('invalid matrix parameter'); | ||
| 2724 | |||
| 2725 | } | ||
| 2726 | |||
| 2727 | # if column slice supplied | ||
| 2728 | if (defined($cols)) { | ||
| 2729 | |||
| 2730 | # verify column slice an array reference | ||
| 2731 | (ref($cols) eq 'ARRAY') || croak('invalid cols parameter'); | ||
| 2732 | |||
| 2733 | # verify length, offset and matrix supplied | ||
| 2734 | (defined($length) && defined($offset) && defined($matrix)) || croak('cols requires length, offset and matrix'); | ||
| 2735 | |||
| 2736 | # flatten column slice | ||
| 2737 | @ix = @{ICC::Shared::flatten($cols)}; | ||
| 2738 | |||
| 2739 | # make splice list using column slice | ||
| 2740 | @list = map {@s[@ix] = @{$_}; [@s]} @{$matrix}; | ||
| 2741 | |||
| 2742 | # splice the data | ||
| 2743 | $removed = [splice(@{$self->[1]}, $offset, $length, @list)]; | ||
| 2744 | |||
| 2745 | } else { | ||
| 2746 | |||
| 2747 | # if matrix supplied | ||
| 2748 | if (defined($matrix)) { | ||
| 2749 | |||
| 2750 | # verify length, offset | ||
| 2751 | (defined($length) && defined($offset)) || croak('matrix requires length and offset'); | ||
| 2752 | |||
| 2753 | # make splice list from full matrix | ||
| 2754 | @list = map {[@{$_}]} @{$matrix}; | ||
| 2755 | |||
| 2756 | # splice the data | ||
| 2757 | $removed = [splice(@{$self->[1]}, $offset, $length, @list)]; | ||
| 2758 | |||
| 2759 | } else { | ||
| 2760 | |||
| 2761 | # if length supplied | ||
| 2762 | if (defined($length)) { | ||
| 2763 | |||
| 2764 | # verify offset supplied | ||
| 2765 | (defined($offset)) || croak('length requires offset'); | ||
| 2766 | |||
| 2767 | # splice the data | ||
| 2768 | $removed = [splice(@{$self->[1]}, $offset, $length)]; | ||
| 2769 | |||
| 2770 | } else { | ||
| 2771 | |||
| 2772 | # if offset supplied | ||
| 2773 | if (defined($offset)) { | ||
| 2774 | |||
| 2775 | # splice the data | ||
| 2776 | $removed = [splice(@{$self->[1]}, $offset)]; | ||
| 2777 | |||
| 2778 | } else { | ||
| 2779 | |||
| 2780 | # get data array reference | ||
| 2781 | $removed = $self->[1]; | ||
| 2782 | |||
| 2783 | # init data array | ||
| 2784 | $self->[1] = [[]]; | ||
| 2785 | |||
| 2786 | # init colorimetry array | ||
| 2787 | $self->[2] = [[]]; | ||
| 2788 | |||
| 2789 | } | ||
| 2790 | |||
| 2791 | } | ||
| 2792 | |||
| 2793 | } | ||
| 2794 | |||
| 2795 | } | ||
| 2796 | |||
| 2797 | # update the SAMPLE_ID hash | ||
| 2798 | _makeSampleID($self); | ||
| 2799 | |||
| 2800 | # return removed data | ||
| 2801 | return(bless($removed, 'Math::Matrix')); | ||
| 2802 | |||
| 2803 | } | ||
| 2804 | |||
| 2805 | # splice columns into data array | ||
| 2806 | # offset and length are as used by Perl's 'splice' function | ||
| 2807 | # data matrix is the 2-D array of data values to be spliced | ||
| 2808 | # row slice is a reference to an array of data matrix row indices | ||
| 2809 | # parameters: ([offset, [length, [data_matrix, [row_slice]]]]) | ||
| 2810 | # returns: (removed_data_matrix) | ||
| 2811 | sub splice_cols { | ||
| 2812 | |||
| 2813 | # get parameters | ||
| 2814 | my ($self, $offset, $length, $matrix, $rows) = @_; | ||
| 2815 | |||
| 2816 | # local variables | ||
| 2817 | my (@ix, @s, @filler, $removed); | ||
| 2818 | |||
| 2819 | # if offset supplied | ||
| 2820 | if (defined($offset)) { | ||
| 2821 | |||
| 2822 | # verify offset a scalar | ||
| 2823 | (! ref($offset) && (int($offset) == $offset)) || croak('invalid offset parameter'); | ||
| 2824 | |||
| 2825 | } | ||
| 2826 | |||
| 2827 | # if length supplied | ||
| 2828 | if (defined($length)) { | ||
| 2829 | |||
| 2830 | # verify length an integer scalar | ||
| 2831 | (! ref($length) && int($length) == $length) || croak('invalid length parameter'); | ||
| 2832 | |||
| 2833 | } | ||
| 2834 | |||
| 2835 | # if matrix supplied | ||
| 2836 | if (defined($matrix)) { | ||
| 2837 | |||
| 2838 | # verify matrix a 2-D array or Math::Matrix object | ||
| 2839 | (ref($matrix) eq 'ARRAY' && ref($matrix->[0]) eq 'ARRAY') || (UNIVERSAL::isa($matrix, 'Math::Matrix')) || croak ('invalid matrix parameter'); | ||
| 2840 | |||
| 2841 | } | ||
| 2842 | |||
| 2843 | # if row slice supplied | ||
| 2844 | if (defined($rows)) { | ||
| 2845 | |||
| 2846 | # verify row slice an array reference | ||
| 2847 | (ref($rows) eq 'ARRAY') || croak('invalid cols parameter'); | ||
| 2848 | |||
| 2849 | # verify length, offset and matrix supplied | ||
| 2850 | (defined($length) && defined($offset) && defined($matrix)) || croak('rows requires length, offset and matrix'); | ||
| 2851 | |||
| 2852 | # flatten row slice | ||
| 2853 | @ix = @{ICC::Shared::flatten($rows)}; | ||
| 2854 | |||
| 2855 | # make list of matrix row refs | ||
| 2856 | @s[@ix] = @{$matrix}; | ||
| 2857 | |||
| 2858 | # make filler data | ||
| 2859 | @filler = (undef) x @{$matrix->[0]}; | ||
| 2860 | |||
| 2861 | # for each data row | ||
| 2862 | for my $i (0 .. $#{$self->[1]}) { | ||
| 2863 | |||
| 2864 | # if matrix data defined | ||
| 2865 | if (defined($s[$i])) { | ||
| 2866 | |||
| 2867 | # splice matrix data | ||
| 2868 | $removed->[$i] = [splice(@{$self->[1][$i]}, $offset, $length, @{$s[$i]})]; | ||
| 2869 | |||
| 2870 | } else { | ||
| 2871 | |||
| 2872 | # splice filler data | ||
| 2873 | $removed->[$i] = [splice(@{$self->[1][$i]}, $offset, $length, @filler)]; | ||
| 2874 | |||
| 2875 | } | ||
| 2876 | |||
| 2877 | } | ||
| 2878 | |||
| 2879 | # for each colorimetry row | ||
| 2880 | for my $i (0 .. $#{$self->[2]}) { | ||
| 2881 | |||
| 2882 | # splice filler data | ||
| 2883 | splice(@{$self->[2][$i]}, $offset, $length, @filler) if (defined($self->[2][$i][$offset])); | ||
| 2884 | |||
| 2885 | } | ||
| 2886 | |||
| 2887 | } else { | ||
| 2888 | |||
| 2889 | # if matrix supplied | ||
| 2890 | if (defined($matrix)) { | ||
| 2891 | |||
| 2892 | # verify length, offset | ||
| 2893 | (defined($length) && defined($offset)) || croak('matrix requires length and offset'); | ||
| 2894 | |||
| 2895 | # make filler data | ||
| 2896 | @filler = (undef) x @{$matrix->[0]}; | ||
| 2897 | |||
| 2898 | # for each data row | ||
| 2899 | for my $i (0 .. $#{$self->[1]}) { | ||
| 2900 | |||
| 2901 | # if matrix data defined | ||
| 2902 | if (defined($matrix->[$i])) { | ||
| 2903 | |||
| 2904 | # splice matrix data | ||
| 2905 | $removed->[$i] = [splice(@{$self->[1][$i]}, $offset, $length, @{$matrix->[$i]})]; | ||
| 2906 | |||
| 2907 | } else { | ||
| 2908 | |||
| 2909 | # splice filler data | ||
| 2910 | $removed->[$i] = [splice(@{$self->[1][$i]}, $offset, $length, @filler)]; | ||
| 2911 | |||
| 2912 | } | ||
| 2913 | |||
| 2914 | } | ||
| 2915 | |||
| 2916 | # for each colorimetry row | ||
| 2917 | for my $i (0 .. $#{$self->[2]}) { | ||
| 2918 | |||
| 2919 | # splice filler data | ||
| 2920 | splice(@{$self->[2][$i]}, $offset, $length, @filler) if (defined($self->[2][$i][$offset])); | ||
| 2921 | |||
| 2922 | } | ||
| 2923 | |||
| 2924 | } else { | ||
| 2925 | |||
| 2926 | # if length supplied | ||
| 2927 | if (defined($length)) { | ||
| 2928 | |||
| 2929 | # verify offset supplied | ||
| 2930 | (defined($offset)) || croak('length requires offset'); | ||
| 2931 | |||
| 2932 | # for each data row | ||
| 2933 | for my $i (0 .. $#{$self->[1]}) { | ||
| 2934 | |||
| 2935 | # splice the data | ||
| 2936 | $removed->[$i] = [splice(@{$self->[1][$i]}, $offset, $length)]; | ||
| 2937 | |||
| 2938 | } | ||
| 2939 | |||
| 2940 | # for each colorimetry row | ||
| 2941 | for my $i (0 .. $#{$self->[2]}) { | ||
| 2942 | |||
| 2943 | # splice filler data | ||
| 2944 | splice(@{$self->[2][$i]}, $offset, $length) if (defined($self->[2][$i][$offset])); | ||
| 2945 | |||
| 2946 | } | ||
| 2947 | |||
| 2948 | } else { | ||
| 2949 | |||
| 2950 | # if offset supplied | ||
| 2951 | if (defined($offset)) { | ||
| 2952 | |||
| 2953 | # for each data row | ||
| 2954 | for my $i (0 .. $#{$self->[1]}) { | ||
| 2955 | |||
| 2956 | # splice the data | ||
| 2957 | $removed->[$i] = [splice(@{$self->[1][$i]}, $offset)]; | ||
| 2958 | |||
| 2959 | } | ||
| 2960 | |||
| 2961 | # for each colorimetry row | ||
| 2962 | for my $i (0 .. $#{$self->[2]}) { | ||
| 2963 | |||
| 2964 | # splice filler data | ||
| 2965 | splice(@{$self->[2][$i]}, $offset) if (defined($self->[2][$i][$offset])); | ||
| 2966 | |||
| 2967 | } | ||
| 2968 | |||
| 2969 | } else { | ||
| 2970 | |||
| 2971 | # get data array reference | ||
| 2972 | $removed = $self->[1]; | ||
| 2973 | |||
| 2974 | # init data array | ||
| 2975 | $self->[1] = [[]]; | ||
| 2976 | |||
| 2977 | # init colorimetry array | ||
| 2978 | $self->[2] = [[]]; | ||
| 2979 | |||
| 2980 | } | ||
| 2981 | |||
| 2982 | } | ||
| 2983 | |||
| 2984 | } | ||
| 2985 | |||
| 2986 | } | ||
| 2987 | |||
| 2988 | # initialize SAMPLE_ID hash if no SAMPLE_ID field | ||
| 2989 | $self->[4] = {} if (0 == test($self, 'ID')); | ||
| 2990 | |||
| 2991 | # return removed data | ||
| 2992 | return(bless($removed, 'Math::Matrix')); | ||
| 2993 | |||
| 2994 | } | ||
| 2995 | |||
| 2996 | # remove rows from data array | ||
| 2997 | # parameters: (row_slice) | ||
| 2998 | # returns: (removed_data_matrix) | ||
| 2999 | sub remove_rows { | ||
| 3000 | |||
| 3001 | # get parameters | ||
| 3002 | my ($self, $rows) = @_; | ||
| 3003 | |||
| 3004 | # local variables | ||
| 3005 | my ($f, $up, @r, @s, $removed); | ||
| 3006 | |||
| 3007 | # return empty matrix if row slice undefined | ||
| 3008 | return(bless([[]], 'Math::Matrix')) if (! defined($rows)); | ||
| 3009 | |||
| 3010 | # flatten row slice | ||
| 3011 | $f = ICC::Shared::flatten($rows); | ||
| 3012 | |||
| 3013 | # if row slice is empty | ||
| 3014 | if (! defined($f->[0])) { | ||
| 3015 | |||
| 3016 | # remove all rows, except row 0 (DATA_FORMAT) | ||
| 3017 | $removed = [splice(@{$self->[1]}, 1)]; | ||
| 3018 | |||
| 3019 | # clear SAMPLE_ID hash | ||
| 3020 | $self->[4] = {}; | ||
| 3021 | |||
| 3022 | # return removed data | ||
| 3023 | return(bless($removed, 'Math::Matrix')); | ||
| 3024 | |||
| 3025 | } | ||
| 3026 | |||
| 3027 | # get upper row index | ||
| 3028 | $up = $#{$self->[1]}; | ||
| 3029 | |||
| 3030 | # verify row slice | ||
| 3031 | (grep {$_ != int($_) || $_ < 1 || $_ > $up} @{$f}) && carp('row slice contains invalid index value(s)'); | ||
| 3032 | |||
| 3033 | # initialize slice (always keep row 0) | ||
| 3034 | @s = (0); | ||
| 3035 | |||
| 3036 | # for each row | ||
| 3037 | for my $i (1 .. $up) { | ||
| 3038 | |||
| 3039 | # if index contained in row slice | ||
| 3040 | if (grep {$i == $_} @{$f}) { | ||
| 3041 | |||
| 3042 | # add to slice (remove) | ||
| 3043 | push(@r, $i); | ||
| 3044 | |||
| 3045 | } else { | ||
| 3046 | |||
| 3047 | # add to slice (keep) | ||
| 3048 | push(@s, $i) | ||
| 3049 | |||
| 3050 | } | ||
| 3051 | |||
| 3052 | } | ||
| 3053 | |||
| 3054 | # if rows to remove | ||
| 3055 | if (@r) { | ||
| 3056 | |||
| 3057 | # set removed data (@r) | ||
| 3058 | $removed = [@{$self->[1]}[@r]]; | ||
| 3059 | |||
| 3060 | # set kept data (@s) | ||
| 3061 | $self->[1] = [@{$self->[1]}[@s]]; | ||
| 3062 | |||
| 3063 | # update the SAMPLE_ID hash | ||
| 3064 | _makeSampleID($self); | ||
| 3065 | |||
| 3066 | } else { | ||
| 3067 | |||
| 3068 | # set removed data (none) | ||
| 3069 | $removed = [[]]; | ||
| 3070 | |||
| 3071 | } | ||
| 3072 | |||
| 3073 | # return removed data | ||
| 3074 | return(bless($removed, 'Math::Matrix')); | ||
| 3075 | |||
| 3076 | } | ||
| 3077 | |||
| 3078 | # remove columns from data array | ||
| 3079 | # parameters: (column_slice) | ||
| 3080 | # returns: (removed_data_matrix) | ||
| 3081 | sub remove_cols { | ||
| 3082 | |||
| 3083 | # get parameters | ||
| 3084 | my ($self, $cols) = @_; | ||
| 3085 | |||
| 3086 | # local variables | ||
| 3087 | my ($f, $up, @r, @s, $removed, $kept, $color); | ||
| 3088 | |||
| 3089 | # return empty matrix if column slice undefined | ||
| 3090 | return(bless([[]], 'Math::Matrix')) if (! defined($cols)); | ||
| 3091 | |||
| 3092 | # flatten column slice | ||
| 3093 | $f = ICC::Shared::flatten($cols); | ||
| 3094 | |||
| 3095 | # if columns slice is empty | ||
| 3096 | if (! defined($f->[0])) { | ||
| 3097 | |||
| 3098 | # copy all rows | ||
| 3099 | $removed = [@{$self->[1]}]; | ||
| 3100 | |||
| 3101 | # clear data array | ||
| 3102 | $self->[1] =[[]]; | ||
| 3103 | |||
| 3104 | # clear colorimetry array | ||
| 3105 | $self->[2] = [[]]; | ||
| 3106 | |||
| 3107 | # clear SAMPLE_ID hash | ||
| 3108 | $self->[4] = {}; | ||
| 3109 | |||
| 3110 | # return removed data | ||
| 3111 | return(bless($removed, 'Math::Matrix')); | ||
| 3112 | |||
| 3113 | } | ||
| 3114 | |||
| 3115 | # get upper column index | ||
| 3116 | $up = $#{$self->[1][0]}; | ||
| 3117 | |||
| 3118 | # verify column slice | ||
| 3119 | (grep {$_ != int($_) || $_ < 0 || $_ > $up} @{$f}) && carp('column slice contains invalid index value(s)'); | ||
| 3120 | |||
| 3121 | # for each column | ||
| 3122 | for my $i (0 .. $up) { | ||
| 3123 | |||
| 3124 | # if index contained in column slice | ||
| 3125 | if (grep {$i == $_} @{$f}) { | ||
| 3126 | |||
| 3127 | # add to slice (remove) | ||
| 3128 | push(@r, $i); | ||
| 3129 | |||
| 3130 | } else { | ||
| 3131 | |||
| 3132 | # add to slice (keep) | ||
| 3133 | push(@s, $i) | ||
| 3134 | |||
| 3135 | } | ||
| 3136 | |||
| 3137 | } | ||
| 3138 | |||
| 3139 | # if columns to remove | ||
| 3140 | if (@r) { | ||
| 3141 | |||
| 3142 | # for each data row | ||
| 3143 | for my $i (0 .. $#{$self->[1]}) { | ||
| 3144 | |||
| 3145 | # set removed data (@r) | ||
| 3146 | $removed->[$i] = [@{$self->[1][$i]}[@r]]; | ||
| 3147 | |||
| 3148 | # set kept data (@s) | ||
| 3149 | $kept->[$i] = [@{$self->[1][$i]}[@s]]; | ||
| 3150 | |||
| 3151 | } | ||
| 3152 | |||
| 3153 | # update object data | ||
| 3154 | $self->[1] = $kept; | ||
| 3155 | |||
| 3156 | # for each colorimetry row | ||
| 3157 | for my $i (0 .. $#{$self->[2]}) { | ||
| 3158 | |||
| 3159 | # set kept colorimetry (@s) | ||
| 3160 | $color->[$i] = [@{$self->[2][$i]}[@s]]; | ||
| 3161 | |||
| 3162 | } | ||
| 3163 | |||
| 3164 | # update colorimetry data | ||
| 3165 | $self->[2] = $color; | ||
| 3166 | |||
| 3167 | # initialize SAMPLE_ID hash if no SAMPLE_ID field | ||
| 3168 | $self->[4] = {} if (0 == test($self, 'ID')); | ||
| 3169 | |||
| 3170 | } else { | ||
| 3171 | |||
| 3172 | # set removed data (none) | ||
| 3173 | $removed = [[]]; | ||
| 3174 | |||
| 3175 | } | ||
| 3176 | |||
| 3177 | # return removed data | ||
| 3178 | return(bless($removed, 'Math::Matrix')); | ||
| 3179 | |||
| 3180 | } | ||
| 3181 | |||
| 3182 | # get sample selection based on 2-D location | ||
| 3183 | # indices are one-based, with origin at the upper left | ||
| 3184 | # row matrix slice may contain indices of undefined rows | ||
| 3185 | # entire chart is used when the row and column indices are omitted | ||
| 3186 | # chart row length is provided as a parameter, or obtained from the data | ||
| 3187 | # parameters: ([upper_row_index, lower_row_index, left_column_index, right_column_index], [chart_row_length]) | ||
| 3188 | # returns: (row_matrix_slice) | ||
| 3189 | sub select_matrix { | ||
| 3190 | |||
| 3191 | # get object reference | ||
| 3192 | my $self = shift(); | ||
| 3193 | |||
| 3194 | # local variables | ||
| 3195 | my ($sn, $cmax, @rows, @cols, $matrix); | ||
| 3196 | my ($row_length, $upper, $lower, $left, $right); | ||
| 3197 | |||
| 3198 | # get number of samples | ||
| 3199 | $sn = $#{$self->[1]}; | ||
| 3200 | |||
| 3201 | # if 0 or 4 parameters | ||
| 3202 | if (@_ == 0 || @_ == 4) { | ||
| 3203 | |||
| 3204 | # get row length from data | ||
| 3205 | $row_length = _getRowLength($self); | ||
| 3206 | |||
| 3207 | # if 1 or 5 parameters | ||
| 3208 | } elsif (@_ == 1 || @_ == 5) { | ||
| 3209 | |||
| 3210 | # get row length | ||
| 3211 | $row_length = pop(); | ||
| 3212 | |||
| 3213 | # verify row length | ||
| 3214 | (Scalar::Util::looks_like_number($row_length) && $row_length == int($row_length) && $row_length > 0) || croak('invalid chart row length'); | ||
| 3215 | |||
| 3216 | } else { | ||
| 3217 | |||
| 3218 | # error | ||
| 3219 | croak('wrong number of parameters'); | ||
| 3220 | |||
| 3221 | } | ||
| 3222 | |||
| 3223 | # if row and column parameters provided | ||
| 3224 | if (@_) { | ||
| 3225 | |||
| 3226 | # get row and column parameters | ||
| 3227 | ($upper, $lower, $left, $right) = @_; | ||
| 3228 | |||
| 3229 | # verify upper and lower indices | ||
| 3230 | (! ref($upper) && $upper == int($upper) && $upper > 0 && $upper <= $row_length) || warn('invalid upper row index'); | ||
| 3231 | (! ref($lower) && $lower == int($lower) && $lower > 0 && $lower <= $row_length) || warn('invalid lower row index'); | ||
| 3232 | |||
| 3233 | # get maximum column index | ||
| 3234 | $cmax = $sn % $row_length ? int($sn/$row_length) + 1 : int($sn/$row_length); | ||
| 3235 | |||
| 3236 | # verify left and right indices | ||
| 3237 | (! ref($left) && $left == int($left) && $left > 0 && $left <= $cmax) || warn('invalid left column index'); | ||
| 3238 | (! ref($right) && $right == int($right) && $right > 0 && $right <= $cmax) || warn('invalid right column index'); | ||
| 3239 | |||
| 3240 | # if upper index < lower index | ||
| 3241 | if ($upper < $lower) { | ||
| 3242 | |||
| 3243 | # make rows array | ||
| 3244 | @rows = ($upper .. $lower); | ||
| 3245 | |||
| 3246 | } else { | ||
| 3247 | |||
| 3248 | # make rows array | ||
| 3249 | @rows = reverse($lower .. $upper); | ||
| 3250 | |||
| 3251 | } | ||
| 3252 | |||
| 3253 | # if left index < right index | ||
| 3254 | if ($left < $right) { | ||
| 3255 | |||
| 3256 | # make columns array | ||
| 3257 | @cols = ($left .. $right); | ||
| 3258 | |||
| 3259 | } else { | ||
| 3260 | |||
| 3261 | # make columns array | ||
| 3262 | @cols = reverse($right .. $left); | ||
| 3263 | |||
| 3264 | } | ||
| 3265 | |||
| 3266 | # use entire chart | ||
| 3267 | } else { | ||
| 3268 | |||
| 3269 | # make rows array | ||
| 3270 | @rows = (1 .. $row_length); | ||
| 3271 | |||
| 3272 | # if chart is rectangular | ||
| 3273 | if ($sn % $row_length == 0) { | ||
| 3274 | |||
| 3275 | # make columns array | ||
| 3276 | @cols = (1 .. $sn/$row_length); | ||
| 3277 | |||
| 3278 | } else { | ||
| 3279 | |||
| 3280 | # warning | ||
| 3281 | warn('chart is not rectangular'); | ||
| 3282 | |||
| 3283 | # make columns array | ||
| 3284 | @cols = (1 .. int($sn/$row_length) + 1); | ||
| 3285 | |||
| 3286 | } | ||
| 3287 | |||
| 3288 | } | ||
| 3289 | |||
| 3290 | # for each row | ||
| 3291 | for my $i (0 .. $#rows) { | ||
| 3292 | |||
| 3293 | # for each column | ||
| 3294 | for my $j (0 .. $#cols) { | ||
| 3295 | |||
| 3296 | # set matrix element | ||
| 3297 | $matrix->[$j][$i] = ($cols[$j] - 1) * $row_length + $rows[$i]; | ||
| 3298 | |||
| 3299 | } | ||
| 3300 | |||
| 3301 | } | ||
| 3302 | |||
| 3303 | # return row matrix slice | ||
| 3304 | return(bless($matrix, 'Math::Matrix')); | ||
| 3305 | |||
| 3306 | } | ||
| 3307 | |||
| 3308 | # get sample selection using template | ||
| 3309 | # samples are matched by their device values | ||
| 3310 | # supported hash keys: 'dups', 'rows', 'context', 'template_context', 'sid_context', 'method', 'copy' | ||
| 3311 | # duplicate handling: 0 - sample average (default), 1 - FIFO, 2 - LIFO, 3 - first sample, 4 - last sample | ||
| 3312 | # parameters: (template_chart_object, [hash]) | ||
| 3313 | # returns: (row_matrix_slice, [sid_matrix_slice]) | ||
| 3314 | sub select_template { | ||
| 3315 | |||
| 3316 | # get parameters | ||
| 3317 | my ($self, $template, $hash) = @_; | ||
| 3318 | |||
| 3319 | # local variables | ||
| 3320 | my ($row_length, $dups, $copys, $copyt); | ||
| 3321 | my ($devcs, $devct, $devs, $devt); | ||
| 3322 | my ($sx, $c1, $c2, $c3, $n, @src, $cmp); | ||
| 3323 | my ($target, $low, $high, $interval, @m, $nomatch); | ||
| 3324 | my ($rows, $avg, $matrix, $devp, $sidt, $sid); | ||
| 3325 | |||
| 3326 | # verify template is a chart object | ||
| 3327 | (UNIVERSAL::isa($template, 'ICC::Support::Chart')) || croak('template not an ICC::Support::Chart object'); | ||
| 3328 | |||
| 3329 | # get template row length | ||
| 3330 | $row_length = _getRowLength($template, $hash); | ||
| 3331 | |||
| 3332 | # set duplicate handling | ||
| 3333 | $dups = defined($hash->{'dups'}) ? $hash->{'dups'} : 0; | ||
| 3334 | |||
| 3335 | # if copy slice is defined | ||
| 3336 | if (defined($hash->{'copy'})) { | ||
| 3337 | |||
| 3338 | # flatten the copy slice | ||
| 3339 | $copys = ICC::Shared::flatten($hash->{'copy'}); | ||
| 3340 | |||
| 3341 | # add copied fields to template | ||
| 3342 | $copyt = add_fmt($template, @{$self->[1][0]}[@{$copys}]); | ||
| 3343 | |||
| 3344 | } | ||
| 3345 | |||
| 3346 | # verify parameters | ||
| 3347 | (! ref($row_length) && $row_length == int($row_length) && $row_length > 0) || croak('invalid chart_row_length parameter'); | ||
| 3348 | ($dups == int($dups) && $dups >= 0 && $dups <= 4) || croak('invalid duplicate_handling parameter'); | ||
| 3349 | |||
| 3350 | # get object device column slice | ||
| 3351 | $devcs = device($self, $hash); | ||
| 3352 | |||
| 3353 | # get template device column slice | ||
| 3354 | $devct = device($template, {'context' => $hash->{'template_context'}}); | ||
| 3355 | |||
| 3356 | # verify object and template column slices | ||
| 3357 | (defined($devcs)) || croak ('object device data missing'); | ||
| 3358 | (defined($devct)) || croak ('template device data missing'); | ||
| 3359 | ($#{$devcs} == $#{$devct}) || croak('object and template have different number of channels'); | ||
| 3360 | |||
| 3361 | # get object device values | ||
| 3362 | $devs = device($self, [], $hash); | ||
| 3363 | |||
| 3364 | # get template device values | ||
| 3365 | $devt = device($template, [], {'context' => $hash->{'template_context'}}); | ||
| 3366 | |||
| 3367 | # get index of next object sample | ||
| 3368 | $sx = $#{$self->[1]} + 1; | ||
| 3369 | |||
| 3370 | # get averaging groups if duplicates are averaged | ||
| 3371 | ($c1, $c2, $c3) = _avg_groups($self, $hash) if ($dups == 0); | ||
| 3372 | |||
| 3373 | # get number of channels | ||
| 3374 | $n = @{$devcs}; | ||
| 3375 | |||
| 3376 | # initialize sample list | ||
| 3377 | @src = (); | ||
| 3378 | |||
| 3379 | # for each sample | ||
| 3380 | for my $i (0 .. $#{$devs}) { | ||
| 3381 | |||
| 3382 | # if all device values defined | ||
| 3383 | if ($n == grep {defined()} @{$devs->[$i]}) { | ||
| 3384 | |||
| 3385 | # add sample to source list | ||
| 3386 | push(@src, [@{$devs->[$i]}, $i + 1]); | ||
| 3387 | |||
| 3388 | } | ||
| 3389 | |||
| 3390 | } | ||
| 3391 | |||
| 3392 | # sort object device values | ||
| 3393 | @src = sort { | ||
| 3394 | |||
| 3395 | # for each channel | ||
| 3396 | for my $i (0 .. $#{$a}) { | ||
| 3397 | |||
| 3398 | # quit loop if device values are unequal | ||
| 3399 | last if ($cmp = $a->[$i] <=> $b->[$i]) | ||
| 3400 | |||
| 3401 | # use last comparison for sort test | ||
| 3402 | } $cmp | ||
| 3403 | |||
| 3404 | } @src; | ||
| 3405 | |||
| 3406 | # for each template sample | ||
| 3407 | for my $i (0 .. $#{$devt}) { | ||
| 3408 | |||
| 3409 | # initialize search indices | ||
| 3410 | $low = 0; | ||
| 3411 | $high = $#src; | ||
| 3412 | |||
| 3413 | # initialize no match flag | ||
| 3414 | $nomatch = 0; | ||
| 3415 | |||
| 3416 | # for each channel | ||
| 3417 | for my $j (0 .. $#{$devt->[0]}) { | ||
| 3418 | |||
| 3419 | # get the target value | ||
| 3420 | $target = $devt->[$i][$j]; | ||
| 3421 | |||
| 3422 | # locate interval containing or bounding the target value | ||
| 3423 | $interval = _bin_search(\@src, $target, $j, $low, $high); | ||
| 3424 | |||
| 3425 | # find indices matching the target value | ||
| 3426 | @m = grep {$src[$_][$j] == $target} @{$interval}; | ||
| 3427 | |||
| 3428 | # if no object values exactly match the target value | ||
| 3429 | if (@m == 0) { | ||
| 3430 | |||
| 3431 | # sort interval indices by distance to target value | ||
| 3432 | @m = sort {$a->[1] <=> $b->[1]} map {[$_, abs($src[$_][$j] - $target)]} @{$interval}; | ||
| 3433 | |||
| 3434 | # if distance to closest object value > 0.00201 | ||
| 3435 | if (abs($target - $src[$m[0][0]][$j]) > 0.00201) { | ||
| 3436 | |||
| 3437 | # print warning | ||
| 3438 | print "no match to template sample $i\n"; | ||
| 3439 | print "device values: @{$devt->[$i]}\n"; | ||
| 3440 | |||
| 3441 | # set no match flag | ||
| 3442 | $nomatch = 1; | ||
| 3443 | |||
| 3444 | # quit channel loop | ||
| 3445 | last; | ||
| 3446 | |||
| 3447 | } | ||
| 3448 | |||
| 3449 | # set target to closest object value | ||
| 3450 | $target = $src[$m[0][0]][$j]; | ||
| 3451 | |||
| 3452 | # locate interval containing the target value | ||
| 3453 | $interval = _bin_search(\@src, $target, $j, $low, $high); | ||
| 3454 | |||
| 3455 | # find indices matching the target value | ||
| 3456 | @m = grep {$src[$_][$j] == $target} @{$interval}; | ||
| 3457 | |||
| 3458 | } | ||
| 3459 | |||
| 3460 | # update interval | ||
| 3461 | $low = $m[0]; | ||
| 3462 | $high = $m[-1]; | ||
| 3463 | |||
| 3464 | } | ||
| 3465 | |||
| 3466 | # if no match found | ||
| 3467 | if ($nomatch) { | ||
| 3468 | |||
| 3469 | # locate nearest object sample(s) using linear search | ||
| 3470 | ($low, $high) = _lin_search(\@src, $devt->[$i]); | ||
| 3471 | |||
| 3472 | # print message | ||
| 3473 | print "closest match is object sample $src[$low][-1]\n"; | ||
| 3474 | print "device values @{$src[$low]}[0 .. $#{$devt->[0]}]\n"; | ||
| 3475 | |||
| 3476 | } | ||
| 3477 | |||
| 3478 | # single sample | ||
| 3479 | if ($low == $high) { | ||
| 3480 | |||
| 3481 | # set matrix element to first row matching object sample | ||
| 3482 | $matrix->[$i/$row_length][$i % $row_length] = $src[$low][-1]; | ||
| 3483 | |||
| 3484 | # duplicate samples | ||
| 3485 | } else { | ||
| 3486 | |||
| 3487 | # duplicates are averaged | ||
| 3488 | if ($dups == 0) { | ||
| 3489 | |||
| 3490 | # for each appended avg sample | ||
| 3491 | for my $j ($sx .. $#{$self->[1]}) { | ||
| 3492 | |||
| 3493 | # set avg | ||
| 3494 | $avg = $j; | ||
| 3495 | |||
| 3496 | # get device values | ||
| 3497 | $devp = $self->device([$j]); | ||
| 3498 | |||
| 3499 | # for each channel | ||
| 3500 | for my $k (0 .. $#{$devp->[0]}) { | ||
| 3501 | |||
| 3502 | # clear avg if device values differ | ||
| 3503 | $avg = 0 if ($devp->[0][$k] != $devt->[$i][$k]); | ||
| 3504 | |||
| 3505 | } | ||
| 3506 | |||
| 3507 | # quit loop if device values match | ||
| 3508 | last if ($avg); | ||
| 3509 | |||
| 3510 | } | ||
| 3511 | |||
| 3512 | # if existing avg sample found | ||
| 3513 | if ($avg) { | ||
| 3514 | |||
| 3515 | # set matrix element to existing avg sample | ||
| 3516 | $matrix->[$i/$row_length][$i % $row_length] = $avg; | ||
| 3517 | |||
| 3518 | } else { | ||
| 3519 | |||
| 3520 | # make row slice of duplicate samples | ||
| 3521 | $rows = [map {$src[$_][-1]} ($low .. $high)]; | ||
| 3522 | |||
| 3523 | # set matrix element to new avg sample | ||
| 3524 | $matrix->[$i/$row_length][$i % $row_length] = _add_avg($self, $rows, $c1, $c2, $c3); | ||
| 3525 | |||
| 3526 | } | ||
| 3527 | |||
| 3528 | # use FIFO sample | ||
| 3529 | } elsif ($dups == 1) { | ||
| 3530 | |||
| 3531 | # from low to high | ||
| 3532 | for my $j ($low .. $high) { | ||
| 3533 | |||
| 3534 | # if index > 0 | ||
| 3535 | if ($src[$j][-1] > 0) { | ||
| 3536 | |||
| 3537 | # set matrix element to object sample index | ||
| 3538 | $matrix->[$i/$row_length][$i % $row_length] = $src[$j][-1]; | ||
| 3539 | |||
| 3540 | # invert sample index to indicate it was used | ||
| 3541 | $src[$j][-1] = - $src[$j][-1]; | ||
| 3542 | |||
| 3543 | # quit loop | ||
| 3544 | last; | ||
| 3545 | |||
| 3546 | } | ||
| 3547 | |||
| 3548 | } | ||
| 3549 | |||
| 3550 | # if matrix element undefined | ||
| 3551 | if (! defined($matrix->[$i/$row_length][$i % $row_length])) { | ||
| 3552 | |||
| 3553 | # print message | ||
| 3554 | print "FIFO stack empty for @{$devt->[$i]}\n"; | ||
| 3555 | print "using last stack sample\n"; | ||
| 3556 | |||
| 3557 | # set matrix element to last row matching object sample | ||
| 3558 | $matrix->[$i/$row_length][$i % $row_length] = - $src[$high][-1]; | ||
| 3559 | |||
| 3560 | } | ||
| 3561 | |||
| 3562 | # use LIFO sample | ||
| 3563 | } elsif ($dups == 2) { | ||
| 3564 | |||
| 3565 | # from high to low | ||
| 3566 | for my $j (reverse($low .. $high)) { | ||
| 3567 | |||
| 3568 | # if index > 0 | ||
| 3569 | if ($src[$j][-1] > 0) { | ||
| 3570 | |||
| 3571 | # set matrix element to object sample index | ||
| 3572 | $matrix->[$i/$row_length][$i % $row_length] = $src[$j][-1]; | ||
| 3573 | |||
| 3574 | # invert sample index to indicate it was used | ||
| 3575 | $src[$j][-1] = - $src[$j][-1]; | ||
| 3576 | |||
| 3577 | # quit loop | ||
| 3578 | last; | ||
| 3579 | |||
| 3580 | } | ||
| 3581 | |||
| 3582 | } | ||
| 3583 | |||
| 3584 | # if matrix element undefined | ||
| 3585 | if (! defined($matrix->[$i/$row_length][$i % $row_length])) { | ||
| 3586 | |||
| 3587 | # print message | ||
| 3588 | print "LIFO stack empty for @{$devt->[$i]}\n"; | ||
| 3589 | print "using last stack sample\n"; | ||
| 3590 | |||
| 3591 | # set matrix element to first row matching object sample | ||
| 3592 | $matrix->[$i/$row_length][$i % $row_length] = - $src[$low][-1]; | ||
| 3593 | |||
| 3594 | } | ||
| 3595 | |||
| 3596 | # use first duplicate sample | ||
| 3597 | } elsif ($dups == 3) { | ||
| 3598 | |||
| 3599 | # set matrix element to first row matching object sample | ||
| 3600 | $matrix->[$i/$row_length][$i % $row_length] = $src[$low][-1]; | ||
| 3601 | |||
| 3602 | # use last duplicate sample | ||
| 3603 | } elsif ($dups == 4) { | ||
| 3604 | |||
| 3605 | # set matrix element to last row matching object sample | ||
| 3606 | $matrix->[$i/$row_length][$i % $row_length] = $src[$high][-1]; | ||
| 3607 | |||
| 3608 | } else { | ||
| 3609 | |||
| 3610 | # error | ||
| 3611 | croak('invalid duplicate handling'); | ||
| 3612 | |||
| 3613 | } | ||
| 3614 | |||
| 3615 | } | ||
| 3616 | |||
| 3617 | # if 'copy' slice defined | ||
| 3618 | if (defined($copys)) { | ||
| 3619 | |||
| 3620 | # get the object row | ||
| 3621 | $n = $matrix->[$i/$row_length][$i % $row_length]; | ||
| 3622 | |||
| 3623 | # copy selected values from object to template | ||
| 3624 | @{$template->[1][$i + 1]}[@{$copyt}] = @{$self->[1][$n]}[@{$copys}]; | ||
| 3625 | |||
| 3626 | # if device values differ | ||
| 3627 | if ($nomatch) { | ||
| 3628 | |||
| 3629 | # copy device values from object to template | ||
| 3630 | @{$template->[1][$i + 1]}[@{$devct}] = @{$self->[1][$n]}[@{$devcs}]; | ||
| 3631 | |||
| 3632 | } | ||
| 3633 | |||
| 3634 | } | ||
| 3635 | |||
| 3636 | } | ||
| 3637 | |||
| 3638 | # if sid-matrix is wanted and template has sid values | ||
| 3639 | if (wantarray() && ($sidt = id($template, [], {'context' => $hash->{'sid_context'}}))) { | ||
| 3640 | |||
| 3641 | # for each template sample | ||
| 3642 | for my $i (0 .. $#{$sidt}) { | ||
| 3643 | |||
| 3644 | # set sid matrix element to sid slice value | ||
| 3645 | $sid->[$i/$row_length][$i % $row_length] = $sidt->[$i][0]; | ||
| 3646 | |||
| 3647 | } | ||
| 3648 | |||
| 3649 | # return row matrix slice and sid matrix slice | ||
| 3650 | return(bless($matrix, 'Math::Matrix'), bless($sid, 'Math::Matrix')); | ||
| 3651 | |||
| 3652 | } else { | ||
| 3653 | |||
| 3654 | # return row matrix slice | ||
| 3655 | return(bless($matrix, 'Math::Matrix')); | ||
| 3656 | |||
| 3657 | } | ||
| 3658 | |||
| 3659 | } | ||
| 3660 | |||
| 3661 | # get sample selection | ||
| 3662 | # array of data values is supplied to code block | ||
| 3663 | # sample is included if code block returns 'true' value | ||
| 3664 | # default row_slice is all samples | ||
| 3665 | # default column_slice is all columns | ||
| 3666 | # parameters: (code_reference, row_slice, column_slice) | ||
| 3667 | # returns: (row_slice) | ||
| 3668 | sub find { | ||
| 3669 | |||
| 3670 | # get parameters | ||
| 3671 | my ($self, $code, $rows, $cols) = @_; | ||
| 3672 | |||
| 3673 | # local variables | ||
| 3674 | my (@s); | ||
| 3675 | |||
| 3676 | # verify code reference | ||
| 3677 | (ref($code) eq 'CODE') || croak('selection parameter must be a code reference'); | ||
| 3678 | |||
| 3679 | # if row slice undefined or empty | ||
| 3680 | if (! defined($rows) || (ref($rows) eq 'ARRAY' && @{$rows} == 0)) { | ||
| 3681 | |||
| 3682 | # use all rows | ||
| 3683 | $rows = [1 .. $#{$self->[1]}]; | ||
| 3684 | |||
| 3685 | } else { | ||
| 3686 | |||
| 3687 | # flatten slice | ||
| 3688 | $rows = ICC::Shared::flatten($rows); | ||
| 3689 | |||
| 3690 | } | ||
| 3691 | |||
| 3692 | # if column slice undefined or empty | ||
| 3693 | if (! defined($cols) || (ref($cols) eq 'ARRAY' && @{$cols} == 0)) { | ||
| 3694 | |||
| 3695 | # use all columns | ||
| 3696 | $cols = [0 .. $#{$self->[1][0]}]; | ||
| 3697 | |||
| 3698 | } else { | ||
| 3699 | |||
| 3700 | # flatten slice | ||
| 3701 | $cols = ICC::Shared::flatten($cols); | ||
| 3702 | |||
| 3703 | } | ||
| 3704 | |||
| 3705 | # select samples | ||
| 3706 | @s = grep {&$code(@{$self->[1][$_]}[@{$cols}])} @{$rows}; | ||
| 3707 | |||
| 3708 | # return selection, or undef if none selected | ||
| 3709 | return(scalar(@s) ? \@s : undef); | ||
| 3710 | |||
| 3711 | } | ||
| 3712 | |||
| 3713 | # get sample selection based on device values | ||
| 3714 | # array of device values is supplied to code block | ||
| 3715 | # sample is included if code block returns 'true' value | ||
| 3716 | # default row_slice is all samples | ||
| 3717 | # context may be specified with parameter hash | ||
| 3718 | # parameters: (code_reference, [row_slice], [hash]) | ||
| 3719 | # returns: (row_slice) | ||
| 3720 | sub ramp { | ||
| 3721 | |||
| 3722 | # local variables | ||
| 3723 | my ($hash, $cols, $mult, @s); | ||
| 3724 | |||
| 3725 | # get optional hash | ||
| 3726 | $hash = pop() if (ref($_[-1]) eq 'HASH'); | ||
| 3727 | |||
| 3728 | # get remaining parameters | ||
| 3729 | my ($self, $code, $rows) = @_; | ||
| 3730 | |||
| 3731 | # verify code reference | ||
| 3732 | (ref($code) eq 'CODE') || croak('selection parameter must be a code reference'); | ||
| 3733 | |||
| 3734 | # if row slice undefined or empty | ||
| 3735 | if (! defined($rows) || (ref($rows) eq 'ARRAY' && @{$rows} == 0)) { | ||
| 3736 | |||
| 3737 | # use all rows | ||
| 3738 | $rows = [1 .. $#{$self->[1]}]; | ||
| 3739 | |||
| 3740 | } else { | ||
| 3741 | |||
| 3742 | # flatten slice | ||
| 3743 | $rows = ICC::Shared::flatten($rows); | ||
| 3744 | |||
| 3745 | } | ||
| 3746 | |||
| 3747 | # get device column slice | ||
| 3748 | (defined($cols = device($self, $hash))) || croak('device values required'); | ||
| 3749 | |||
| 3750 | # set multiplier (255 if RGB, else 100) | ||
| 3751 | $mult = ($self->[1][0][$cols->[0]] =~ m/RGB_R$/) ? 255 : 100; | ||
| 3752 | |||
| 3753 | # select samples | ||
| 3754 | @s = grep {&$code(map {$_/$mult} @{$self->[1][$_]}[@{$cols}])} @{$rows}; | ||
| 3755 | |||
| 3756 | # return selection, or undef if none selected | ||
| 3757 | return(scalar(@s) ? \@s : undef); | ||
| 3758 | |||
| 3759 | } | ||
| 3760 | |||
| 3761 | # get sample selection based on L*a*b* values | ||
| 3762 | # array of L*a*b* values is supplied to code block | ||
| 3763 | # sample is included if code block returns 'true' value | ||
| 3764 | # default row_slice is all samples | ||
| 3765 | # context may be specified with parameter hash | ||
| 3766 | # parameters: (code_reference, [row_slice], [hash]) | ||
| 3767 | # returns: (row_slice) | ||
| 3768 | sub range { | ||
| 3769 | |||
| 3770 | # local variables | ||
| 3771 | my ($hash, $cols, @s); | ||
| 3772 | |||
| 3773 | # get optional hash | ||
| 3774 | $hash = pop() if (ref($_[-1]) eq 'HASH'); | ||
| 3775 | |||
| 3776 | # get remaining parameters | ||
| 3777 | my ($self, $code, $rows) = @_; | ||
| 3778 | |||
| 3779 | # verify code reference | ||
| 3780 | (ref($code) eq 'CODE') || croak('selection parameter must be a code reference'); | ||
| 3781 | |||
| 3782 | # if row slice undefined or empty | ||
| 3783 | if (! defined($rows) || (ref($rows) eq 'ARRAY' && @{$rows} == 0)) { | ||
| 3784 | |||
| 3785 | # use all rows | ||
| 3786 | $rows = [1 .. $#{$self->[1]}]; | ||
| 3787 | |||
| 3788 | } else { | ||
| 3789 | |||
| 3790 | # flatten slice | ||
| 3791 | $rows = ICC::Shared::flatten($rows); | ||
| 3792 | |||
| 3793 | } | ||
| 3794 | |||
| 3795 | # get L*a*b* column slice | ||
| 3796 | (defined($cols = lab($self, $hash))) || croak('L*a*b* values required'); | ||
| 3797 | |||
| 3798 | # select samples | ||
| 3799 | @s = grep {&$code(@{$self->[1][$_]}[@{$cols}])} @{$rows}; | ||
| 3800 | |||
| 3801 | # return selection, or undef if none selected | ||
| 3802 | return(scalar(@s) ? \@s : undef); | ||
| 3803 | |||
| 3804 | } | ||
| 3805 | |||
| 3806 | # generate randomized sample slice | ||
| 3807 | # parameter: ([row_slice]) | ||
| 3808 | # returns: (row_slice) | ||
| 3809 | sub randomize { | ||
| 3810 | |||
| 3811 | # get parameters | ||
| 3812 | my ($self, $rows) = @_; | ||
| 3813 | |||
| 3814 | # if row slice defined | ||
| 3815 | if (defined($rows)) { | ||
| 3816 | |||
| 3817 | # if row slice an empty array reference | ||
| 3818 | if (ref($rows) eq 'ARRAY' && @{$rows} == 0) { | ||
| 3819 | |||
| 3820 | # use all rows | ||
| 3821 | $rows = [1 .. $#{$self->[1]}]; | ||
| 3822 | |||
| 3823 | } else { | ||
| 3824 | |||
| 3825 | # flatten row slice | ||
| 3826 | $rows = ICC::Shared::flatten($rows); | ||
| 3827 | |||
| 3828 | # verify row slice contents | ||
| 3829 | (@{$rows} == grep {! ref() && $_ == int($_) && $_ >= 0} @{$rows}) || croak('invalid row slice'); | ||
| 3830 | |||
| 3831 | } | ||
| 3832 | |||
| 3833 | # return row slice, randomized | ||
| 3834 | return([List::Util::shuffle(@{$rows})]); | ||
| 3835 | |||
| 3836 | } else { | ||
| 3837 | |||
| 3838 | # return all samples, randomized | ||
| 3839 | return([List::Util::shuffle(1 .. $#{$self->[1]})]); | ||
| 3840 | |||
| 3841 | } | ||
| 3842 | |||
| 3843 | } | ||
| 3844 | |||
| 3845 | # analyze chart device values | ||
| 3846 | # creates an array structure with an element for each device channel. | ||
| 3847 | # each element contains a hash, a keys array, and a ramp array. | ||
| 3848 | # hash keys are device values, and hash values are arrays of samples. | ||
| 3849 | # if row-slice is omitted, all samples are used. | ||
| 3850 | # if the dup_flag is false (default), a new sample is added | ||
| 3851 | # containing average measurement values, and the new sample | ||
| 3852 | # is substituted for the anonymous array of duplicates. | ||
| 3853 | # if the dup_flag is true, duplicate samples are included in | ||
| 3854 | # array of samples grouped within anonymous arrays. | ||
| 3855 | # dup_flag and/or device context are specified with parameter hash | ||
| 3856 | # parameters: ([row_slice], [hash]) | ||
| 3857 | # returns: (ref_to_structure) | ||
| 3858 | sub analyze { | ||
| 3859 | |||
| 3860 | # get object reference | ||
| 3861 | my $self = shift(); | ||
| 3862 | |||
| 3863 | # local variables | ||
| 3864 | my ($hash, $rows, $dup, $ramp, $dev, $c1, $c2, $c3, @id, @name, $mult); | ||
| 3865 | my (@d, %dev_hash, $key, $avg, $value, $struct); | ||
| 3866 | |||
| 3867 | # get optional hash | ||
| 3868 | $hash = pop() if (ref($_[-1]) eq 'HASH'); | ||
| 3869 | |||
| 3870 | # get device column slice | ||
| 3871 | ($dev = device($self, $hash)) || croak('chart has no device values'); | ||
| 3872 | |||
| 3873 | # get row slice | ||
| 3874 | $rows = shift() if (ref($_[0]) eq 'ARRAY'); | ||
| 3875 | |||
| 3876 | # flatten row slice | ||
| 3877 | $rows = $rows ? ICC::Shared::flatten($rows) : []; | ||
| 3878 | |||
| 3879 | # use all samples if slice is empty | ||
| 3880 | $rows = [1 .. $#{$self->[1]}] if (@{$rows} == 0); | ||
| 3881 | |||
| 3882 | # get dup flag | ||
| 3883 | $dup = defined($hash->{'dups'}) ? $hash->{'dups'} : 0; | ||
| 3884 | |||
| 3885 | # get ramp value | ||
| 3886 | $ramp = defined($hash->{'ramp'}) ? $hash->{'ramp'} : 0; | ||
| 3887 | |||
| 3888 | # get averaging groups | ||
| 3889 | ($c1, $c2, $c3) = _avg_groups($self, $hash); | ||
| 3890 | |||
| 3891 | # for each column | ||
| 3892 | for my $i (0 .. $#{$self->[1][0]}) { | ||
| 3893 | |||
| 3894 | # add column if SAMPLE_ID field | ||
| 3895 | push(@id, $i) if ($self->[1][0][$i] =~ m/^(?:.*\|)?(?:SAMPLE_ID|SampleID)$/); | ||
| 3896 | |||
| 3897 | # add column if SAMPLE_NAME field | ||
| 3898 | push(@name, $i) if ($self->[1][0][$i] =~ m/^(?:.*\|)?SAMPLE_NAME$/); | ||
| 3899 | |||
| 3900 | } | ||
| 3901 | |||
| 3902 | # set device multiplier (255 for RGB values, otherwise 100) | ||
| 3903 | $mult = ($self->[1][0][$dev->[0]] =~ m/^(?:.*\|)?RGB_[RGB]$/) ? 255 : 100; | ||
| 3904 | |||
| 3905 | # for each sample | ||
| 3906 | for my $i (0 .. $#{$rows}) { | ||
| 3907 | |||
| 3908 | # get device values | ||
| 3909 | @d = @{$self->[1][$rows->[$i]]}[@{$dev}]; | ||
| 3910 | |||
| 3911 | # divide by multiplier (setting -0 to 0) | ||
| 3912 | @d = map {$_ == 0 ? 0 : $_/$mult} @d; | ||
| 3913 | |||
| 3914 | # make device value key | ||
| 3915 | $key = join(':', @d); | ||
| 3916 | |||
| 3917 | # if key exists | ||
| 3918 | if (exists($dev_hash{$key})) { | ||
| 3919 | |||
| 3920 | # add sample to existing hash entry | ||
| 3921 | push(@{$dev_hash{$key}}, $rows->[$i]); | ||
| 3922 | |||
| 3923 | } else { | ||
| 3924 | |||
| 3925 | # add device hash entry | ||
| 3926 | $dev_hash{$key} = [$rows->[$i]]; | ||
| 3927 | |||
| 3928 | } | ||
| 3929 | |||
| 3930 | } | ||
| 3931 | |||
| 3932 | # if dup flag is not set | ||
| 3933 | if (! $dup) { | ||
| 3934 | |||
| 3935 | # for each key | ||
| 3936 | for my $key (keys(%dev_hash)) { | ||
| 3937 | |||
| 3938 | # if duplicate samples | ||
| 3939 | if (@{$dev_hash{$key}} > 1) { | ||
| 3940 | |||
| 3941 | # if measurement data | ||
| 3942 | if (@{$c1} || @{$c2} || @{$c3}) { | ||
| 3943 | |||
| 3944 | # add average sample | ||
| 3945 | $avg = _add_avg($self, $dev_hash{$key}, $c1, $c2, $c3, \@id, \@name); | ||
| 3946 | |||
| 3947 | # update hash to average sample | ||
| 3948 | $dev_hash{$key} = [$avg]; | ||
| 3949 | |||
| 3950 | } else { | ||
| 3951 | |||
| 3952 | # update hash to first sample | ||
| 3953 | $dev_hash{$key} = [$dev_hash{$key}[0]]; | ||
| 3954 | |||
| 3955 | } | ||
| 3956 | |||
| 3957 | } | ||
| 3958 | |||
| 3959 | } | ||
| 3960 | |||
| 3961 | # update the SAMPLE_ID hash | ||
| 3962 | _makeSampleID($self); | ||
| 3963 | |||
| 3964 | } | ||
| 3965 | |||
| 3966 | # make empty structure | ||
| 3967 | $struct = [map {[{}, [], []]} (0 .. $#{$dev})]; | ||
| 3968 | |||
| 3969 | # for each key | ||
| 3970 | for my $key (keys(%dev_hash)) { | ||
| 3971 | |||
| 3972 | # split key to device values | ||
| 3973 | @d = split(/:/, $key); | ||
| 3974 | |||
| 3975 | # get value | ||
| 3976 | $value = $dev_hash{$key}; | ||
| 3977 | |||
| 3978 | # resolve single value to scalar | ||
| 3979 | $value = $value->[0] if (@{$value} == 1); | ||
| 3980 | |||
| 3981 | # for each device channel | ||
| 3982 | for my $i (0 .. $#d) { | ||
| 3983 | |||
| 3984 | # if key exists | ||
| 3985 | if (exists($struct->[$i][0]{$d[$i]})) { | ||
| 3986 | |||
| 3987 | # add sample to hash entry | ||
| 3988 | push(@{$struct->[$i][0]{$d[$i]}}, $value); | ||
| 3989 | |||
| 3990 | } else { | ||
| 3991 | |||
| 3992 | # add hash entry | ||
| 3993 | $struct->[$i][0]{$d[$i]} = [$value]; | ||
| 3994 | |||
| 3995 | # add device value to keys array | ||
| 3996 | push(@{$struct->[$i][1]}, $d[$i]); | ||
| 3997 | |||
| 3998 | } | ||
| 3999 | |||
| 4000 | # if all other device values equal ramp value | ||
| 4001 | if (@d == grep {$_ == $i || $d[$_] == $ramp} (0 .. $#d)) { | ||
| 4002 | |||
| 4003 | # add sample to ramp array | ||
| 4004 | push(@{$struct->[$i][2]}, $value); | ||
| 4005 | |||
| 4006 | } | ||
| 4007 | |||
| 4008 | } | ||
| 4009 | |||
| 4010 | } | ||
| 4011 | |||
| 4012 | # for each device channel | ||
| 4013 | for my $i (0 .. $#{$dev}) { | ||
| 4014 | |||
| 4015 | # sort keys array (decreasing frequency) | ||
| 4016 | $struct->[$i][1] = [sort {@{$struct->[$i][0]{$b}} <=> @{$struct->[$i][0]{$a}}} @{$struct->[$i][1]}]; | ||
| 4017 | |||
| 4018 | # sort ramp array (increasing values) | ||
| 4019 | $struct->[$i][2] = [sort {$self->[1][(! ref($a) ? $a : $a->[0])][$dev->[$i]] <=> $self->[1][(! ref($b) ? $b : $b->[0])][$dev->[$i]]} @{$struct->[$i][2]}]; | ||
| 4020 | |||
| 4021 | } | ||
| 4022 | |||
| 4023 | # return | ||
| 4024 | return($struct); | ||
| 4025 | |||
| 4026 | } | ||
| 4027 | |||
| 4028 | # write chart to ISO 28178 (CGATS.17) ASCII file | ||
| 4029 | # optional slice parameters are either scalars, array references or 'Math::Matrix' objects | ||
| 4030 | # optional hash parameter keys: 'sid', 'append' | ||
| 4031 | # parameters: (path_to_file, [row_slice, [column_slice]], [hash]) | ||
| 4032 | sub write { | ||
| 4033 | |||
| 4034 | # local variables | ||
| 4035 | my ($hash, $row_length, $m, $n, @files, $sid, $fh, $rs, @fields); | ||
| 4036 | my (%cspec, $keyword, $value, $source, $std_key, @s, $sidx, $append); | ||
| 4037 | my ($null, $undef); | ||
| 4038 | |||
| 4039 | # get optional hash parameter | ||
| 4040 | $hash = pop() if (ref($_[-1]) eq 'HASH'); | ||
| 4041 | |||
| 4042 | # get remaining parameters | ||
| 4043 | my ($self, $path, $rows, $cols) = @_; | ||
| 4044 | |||
| 4045 | # if row slice defined | ||
| 4046 | if (defined($rows)) { | ||
| 4047 | |||
| 4048 | # if row slice an empty array reference | ||
| 4049 | if (ref($rows) eq 'ARRAY' && @{$rows} == 0) { | ||
| 4050 | |||
| 4051 | # use all rows | ||
| 4052 | $rows = [1 .. $#{$self->[1]}]; | ||
| 4053 | |||
| 4054 | } else { | ||
| 4055 | |||
| 4056 | # get row length if row slice is Math::Matrix object | ||
| 4057 | $row_length = @{$rows->[0]} if (UNIVERSAL::isa($rows, 'Math::Matrix')); | ||
| 4058 | |||
| 4059 | # flatten row slice | ||
| 4060 | $rows = ICC::Shared::flatten($rows); | ||
| 4061 | |||
| 4062 | } | ||
| 4063 | |||
| 4064 | } else { | ||
| 4065 | |||
| 4066 | # use all rows | ||
| 4067 | $rows = [1 .. $#{$self->[1]}]; | ||
| 4068 | |||
| 4069 | } | ||
| 4070 | |||
| 4071 | # get number of rows | ||
| 4072 | $m = @{$rows}; | ||
| 4073 | |||
| 4074 | # warn if invalid samples | ||
| 4075 | (@{$rows} == grep {$_ == int($_) && $_ != 0 && defined($self->[1][$_])} @{$rows})|| warn('row slice contains invalid samples'); | ||
| 4076 | |||
| 4077 | # if column slice defined | ||
| 4078 | if (defined($cols)) { | ||
| 4079 | |||
| 4080 | # if column slice an empty array reference | ||
| 4081 | if (ref($cols) eq 'ARRAY' && @{$cols} == 0) { | ||
| 4082 | |||
| 4083 | # use all columns | ||
| 4084 | $cols = [0 .. $#{$self->[1][0]}]; | ||
| 4085 | |||
| 4086 | } else { | ||
| 4087 | |||
| 4088 | # flatten column slice | ||
| 4089 | $cols = ICC::Shared::flatten($cols); | ||
| 4090 | |||
| 4091 | } | ||
| 4092 | |||
| 4093 | } else { | ||
| 4094 | |||
| 4095 | # use all columns | ||
| 4096 | $cols = [0 .. $#{$self->[1][0]}]; | ||
| 4097 | |||
| 4098 | } | ||
| 4099 | |||
| 4100 | # get number of columns | ||
| 4101 | $n = @{$cols}; | ||
| 4102 | |||
| 4103 | # filter column slice | ||
| 4104 | @{$cols} = grep {$_ == int($_) && defined($self->[1][0][$_])} @{$cols}; | ||
| 4105 | |||
| 4106 | # warn if invalid fields | ||
| 4107 | ($n == @{$cols}) || warn('column slice contains invalid fields'); | ||
| 4108 | |||
| 4109 | # if 'sid' hash value defined | ||
| 4110 | if (defined($sid = $hash->{'sid'})) { | ||
| 4111 | |||
| 4112 | # if array reference or Math::Matrix object | ||
| 4113 | if (ref($sid) eq 'ARRAY' || UNIVERSAL::isa($sid, 'Math::Matrix')) { | ||
| 4114 | |||
| 4115 | # flatten 'sid' slice | ||
| 4116 | $sid = ICC::Shared::flatten($sid); | ||
| 4117 | |||
| 4118 | # warn row slice and sid slice are different sizes | ||
| 4119 | ($m == @{$sid}) || warn('row slice and sid slice are different sizes'); | ||
| 4120 | |||
| 4121 | } elsif ($sid eq 'row') { | ||
| 4122 | |||
| 4123 | # use sequential row list | ||
| 4124 | $sid = [1 .. $m]; | ||
| 4125 | |||
| 4126 | } else { | ||
| 4127 | |||
| 4128 | # error | ||
| 4129 | croak('invalid \'sid\' hash value'); | ||
| 4130 | |||
| 4131 | } | ||
| 4132 | |||
| 4133 | } | ||
| 4134 | |||
| 4135 | # resolve file list from path | ||
| 4136 | (defined($path) && (! ref($path)) && (@files = File::Glob::bsd_glob($path))) || croak("invalid path: $path, stopped"); | ||
| 4137 | |||
| 4138 | # verify file path is unique | ||
| 4139 | (@files == 1) || warn('file path not unique'); | ||
| 4140 | |||
| 4141 | # open the file | ||
| 4142 | open($fh, '>', $files[0]) || croak("$! when opening $files[0], stopped"); | ||
| 4143 | |||
| 4144 | # get the record separator | ||
| 4145 | $rs = $self->[0]{'write_rs'} || $self->[0]{'read_rs'} || "\n"; | ||
| 4146 | |||
| 4147 | # initialize color specification hash | ||
| 4148 | # so lines with 'FileInformation' source are printed | ||
| 4149 | %cspec = ('FileInformation' => 1); | ||
| 4150 | |||
| 4151 | # add referenced sources to color specification hash | ||
| 4152 | for (@{$self->[2][5]}[@{$cols}]) {$cspec{$_}++ if defined()}; | ||
| 4153 | |||
| 4154 | # for each header line | ||
| 4155 | for (@{$self->[3]}) { | ||
| 4156 | |||
| 4157 | # get keyword, value and source | ||
| 4158 | ($keyword, $value, $source) = @{$_}; | ||
| 4159 | |||
| 4160 | # if keyword defined and length > 0 | ||
| 4161 | if (defined($keyword) && length($keyword)) { | ||
| 4162 | |||
| 4163 | # make uppercase | ||
| 4164 | $keyword = uc($keyword); | ||
| 4165 | |||
| 4166 | # skip certain keywords | ||
| 4167 | next if ($keyword =~ m/KEYWORD|NUMBER_OF_FIELDS|NUMBER_OF_SETS/); | ||
| 4168 | next if (defined($row_length) && $keyword =~ m/LGOROWLENGTH/); | ||
| 4169 | |||
| 4170 | # if no source or referenced source | ||
| 4171 | if (! defined($source) || $cspec{$source}) { | ||
| 4172 | |||
| 4173 | # if value defined and length > 0 | ||
| 4174 | if (defined($value) && length($value)) { | ||
| 4175 | |||
| 4176 | # print keyword/value | ||
| 4177 | print $fh "$keyword\t$value$rs"; | ||
| 4178 | |||
| 4179 | } else { | ||
| 4180 | |||
| 4181 | # print keyword only | ||
| 4182 | print $fh "$keyword$rs"; | ||
| 4183 | |||
| 4184 | } | ||
| 4185 | |||
| 4186 | } | ||
| 4187 | |||
| 4188 | } else { | ||
| 4189 | |||
| 4190 | # print empty line | ||
| 4191 | print $fh "$rs"; | ||
| 4192 | |||
| 4193 | } | ||
| 4194 | |||
| 4195 | } | ||
| 4196 | |||
| 4197 | # get format fields | ||
| 4198 | @fields = @{$self->[1][0]}[@{$cols}]; | ||
| 4199 | |||
| 4200 | # remove any context, trim leading and trailing white space, and replace spaces with underscores | ||
| 4201 | for (@fields) {s/^.*\|//; s/^\s*(.*?)\s*$/$1/; s/ /_/g} | ||
| 4202 | |||
| 4203 | # make standard format keyword regex (per ISO 28178 and common usage) | ||
| 4204 | $std_key = '^(?:' . join('|', qw(SAMPLE_ID SAMPLE_NO STRING RGB_[RGB] CMYK_[CMYK] [2-9A-F]CLR_[1-9A-F] PC\d+_\d+ SPOT_\d+ | ||
| 4205 | (?:nm|SPECTRAL_NM_|SPECTRAL_NM|SPECTRAL_|NM_|R_)\d{3} D_(?:RED|GREEN|BLUE|VIS|MAJOR_FILTER) XYZ_[XYZ] XYY_(?:X|Y|CAPY) | ||
| 4206 | LAB_[LABCH] LAB_DE LAB_DE_94 LAB_DE_CMC LAB_DE_2000 MEAN_DE STDDEV_[XYZ] STDDEV_[LAB] CHI_SQD_PAR)) . ')$'; | ||
| 4207 | |||
| 4208 | # for each format field | ||
| 4209 | for (@fields) { | ||
| 4210 | |||
| 4211 | # if not a standard keyword | ||
| 4212 | if (! /$std_key/) { | ||
| 4213 | |||
| 4214 | # print KEYWORD | ||
| 4215 | printf $fh "KEYWORD\t%s$rs", $_; | ||
| 4216 | |||
| 4217 | } | ||
| 4218 | |||
| 4219 | } | ||
| 4220 | |||
| 4221 | # if 'sid' slice defined | ||
| 4222 | if (defined($sid)) { | ||
| 4223 | |||
| 4224 | # if 'SAMPLE_ID' keyword(s) | ||
| 4225 | if (@s = grep {uc($fields[$_]) eq 'SAMPLE_ID'} (0 .. $#fields)) { | ||
| 4226 | |||
| 4227 | # save index of first match | ||
| 4228 | $sidx = $s[0]; | ||
| 4229 | |||
| 4230 | } else { | ||
| 4231 | |||
| 4232 | # insert 'SAMPLE_ID' keyword | ||
| 4233 | unshift(@fields, 'SAMPLE_ID'); | ||
| 4234 | |||
| 4235 | } | ||
| 4236 | |||
| 4237 | } | ||
| 4238 | |||
| 4239 | # print LGOROWLENGTH (if $row_length defined) | ||
| 4240 | printf $fh "LGOROWLENGTH\t%d$rs", $row_length if (defined($row_length)); | ||
| 4241 | |||
| 4242 | # print NUMBER_OF_FIELDS | ||
| 4243 | printf $fh "NUMBER_OF_FIELDS\t%d$rs", scalar(@fields); | ||
| 4244 | |||
| 4245 | # print BEGIN_DATA_FORMAT | ||
| 4246 | print $fh 'BEGIN_DATA_FORMAT', $rs; | ||
| 4247 | |||
| 4248 | # print format string (if any) | ||
| 4249 | print $fh join("\t", @fields), $rs if (@fields); | ||
| 4250 | |||
| 4251 | # print END_DATA_FORMAT | ||
| 4252 | print $fh 'END_DATA_FORMAT', $rs; | ||
| 4253 | |||
| 4254 | # print NUMBER_OF_SETS | ||
| 4255 | printf $fh "NUMBER_OF_SETS\t%d$rs", scalar(@{$rows}); | ||
| 4256 | |||
| 4257 | # print BEGIN_DATA | ||
| 4258 | print $fh 'BEGIN_DATA', $rs; | ||
| 4259 | |||
| 4260 | # get null replacement value | ||
| 4261 | $null = $hash->{'null'} // 'null'; | ||
| 4262 | |||
| 4263 | # get undef replacement value | ||
| 4264 | $undef = $hash->{'undef'} // 'undef'; | ||
| 4265 | |||
| 4266 | # for each row | ||
| 4267 | for my $i (0 .. $#{$rows}) { | ||
| 4268 | |||
| 4269 | # get data fields, replacing null and undefined values | ||
| 4270 | @fields = map {defined() ? length() ? $_ : $null : $undef} @{$self->[1][$rows->[$i]]}[@{$cols}]; | ||
| 4271 | |||
| 4272 | # trim leading and trailing white space, and replace spaces with underscores | ||
| 4273 | for (@fields) {s/^\s*(.*?)\s*$/$1/; s/ /_/g}; | ||
| 4274 | |||
| 4275 | # if 'sid' slice defined | ||
| 4276 | if (defined($sid)) { | ||
| 4277 | |||
| 4278 | # if 'sid' index defined | ||
| 4279 | if (defined($sidx)) { | ||
| 4280 | |||
| 4281 | # replace 'sid' value | ||
| 4282 | $fields[$sidx] = $sid->[$i]; | ||
| 4283 | |||
| 4284 | } else { | ||
| 4285 | |||
| 4286 | # insert 'sid' value | ||
| 4287 | unshift(@fields, $sid->[$i]); | ||
| 4288 | } | ||
| 4289 | |||
| 4290 | } | ||
| 4291 | |||
| 4292 | # print the data record | ||
| 4293 | print $fh join("\t", @fields), $rs; | ||
| 4294 | |||
| 4295 | } | ||
| 4296 | |||
| 4297 | # print END_DATA | ||
| 4298 | print $fh 'END_DATA', $rs; | ||
| 4299 | |||
| 4300 | # if 'append' hash value defined | ||
| 4301 | if (defined($append = $hash->{'append'})) { | ||
| 4302 | |||
| 4303 | # replace line endings, if any | ||
| 4304 | $append =~ s/\n/$rs/g; | ||
| 4305 | |||
| 4306 | # print appended data | ||
| 4307 | print $fh $append; | ||
| 4308 | |||
| 4309 | } | ||
| 4310 | |||
| 4311 | # close the file | ||
| 4312 | close($fh); | ||
| 4313 | |||
| 4314 | } | ||
| 4315 | |||
| 4316 | # write chart to CxF3 file | ||
| 4317 | # optional slice parameters are either scalars, array references or 'Math::Matrix' objects | ||
| 4318 | # optional hash parameter keys: 'cc:FileInformation' | ||
| 4319 | # parameters: (path_to_file, [row_slice, [column_slice]], [hash]) | ||
| 4320 | sub writeCxF3 { | ||
| 4321 | |||
| 4322 | # local variables | ||
| 4323 | my ($hash, $row_length, $n); | ||
| 4324 | my ($dom, $root, $ns, $nsURI); | ||
| 4325 | my ($datetime, $id, $ops, $objcol); | ||
| 4326 | my ($prefix, $nid, $obj, $xpath, $node); | ||
| 4327 | my (%lookup, @data, @files, $sub, $spot); | ||
| 4328 | |||
| 4329 | # get optional hash parameter | ||
| 4330 | $hash = pop() if (ref($_[-1]) eq 'HASH'); | ||
| 4331 | |||
| 4332 | # get remaining parameters | ||
| 4333 | my ($self, $path, $rows, $cols) = @_; | ||
| 4334 | |||
| 4335 | # if row slice defined | ||
| 4336 | if (defined($rows)) { | ||
| 4337 | |||
| 4338 | # if row slice an empty array reference | ||
| 4339 | if (ref($rows) eq 'ARRAY' && @{$rows} == 0) { | ||
| 4340 | |||
| 4341 | # use all rows | ||
| 4342 | $rows = [1 .. $#{$self->[1]}]; | ||
| 4343 | |||
| 4344 | } else { | ||
| 4345 | |||
| 4346 | # get row length if row slice is Math::Matrix object | ||
| 4347 | $row_length = @{$rows->[0]} if (UNIVERSAL::isa($rows, 'Math::Matrix')); | ||
| 4348 | |||
| 4349 | # flatten row slice | ||
| 4350 | $rows = ICC::Shared::flatten($rows); | ||
| 4351 | |||
| 4352 | } | ||
| 4353 | |||
| 4354 | } else { | ||
| 4355 | |||
| 4356 | # set array reference to all rows | ||
| 4357 | $rows = [1 .. $#{$self->[1]}]; | ||
| 4358 | |||
| 4359 | } | ||
| 4360 | |||
| 4361 | # get number of rows | ||
| 4362 | $n = @{$rows}; | ||
| 4363 | |||
| 4364 | # filter row slice | ||
| 4365 | @{$rows} = grep {$_ == int($_) && $_ != 0 && defined($self->[1][$_])} @{$rows}; | ||
| 4366 | |||
| 4367 | # warn if invalid samples | ||
| 4368 | ($n == @{$rows}) || warn('row slice contains invalid samples'); | ||
| 4369 | |||
| 4370 | # open CxF3 template | ||
| 4371 | eval {$dom = XML::LibXML->load_xml('location' => ICC::Shared::getICCPath('Templates/CxF3_template.xml'))} || croak('can\'t load CxF3 template'); | ||
| 4372 | |||
| 4373 | # get the root element | ||
| 4374 | $root = $dom->documentElement(); | ||
| 4375 | |||
| 4376 | # get the namespace prefix and URI | ||
| 4377 | $ns = $root->prefix(); | ||
| 4378 | $nsURI = $root->namespaceURI(); | ||
| 4379 | |||
| 4380 | # make 'FileInformation' nodes | ||
| 4381 | $datetime = _makeCxF3fileinfo($self, $root, $ns, $nsURI, $hash); | ||
| 4382 | |||
| 4383 | # make write operations array from column slice | ||
| 4384 | # array structure: [[[class, prefix, XPath, [sub_paths], [columns], {attributes}, sort_order], ...], ...] | ||
| 4385 | $ops = _makeCxF3writeops($self, $root, $ns, $cols); | ||
| 4386 | |||
| 4387 | # make 'ColorSpecification' nodes | ||
| 4388 | _makeCxF3colorspec($self, $root, $ns, $nsURI, $ops); | ||
| 4389 | |||
| 4390 | # get the 'ObjectCollection' node | ||
| 4391 | ($objcol) = $root->findnodes("$ns:Resources/$ns:ObjectCollection"); | ||
| 4392 | |||
| 4393 | # init object Id index | ||
| 4394 | $id = 1; | ||
| 4395 | |||
| 4396 | # for each group of operations | ||
| 4397 | for my $i (0 .. $#{$ops}) { | ||
| 4398 | |||
| 4399 | # get prefix (ObjectType) | ||
| 4400 | $prefix = $ops->[$i][0][1]; | ||
| 4401 | |||
| 4402 | # initialize name Id | ||
| 4403 | $nid = 0; | ||
| 4404 | |||
| 4405 | # for each row in slice | ||
| 4406 | for my $j (@{$rows}) { | ||
| 4407 | |||
| 4408 | # increment name Id | ||
| 4409 | $nid++; | ||
| 4410 | |||
| 4411 | # add 'Object' node | ||
| 4412 | $obj = $objcol->appendChild(XML::LibXML::Element->new("$ns:Object")); | ||
| 4413 | $obj->setAttribute('ObjectType', $prefix); | ||
| 4414 | $obj->setAttribute('Name', "$prefix$nid"); | ||
| 4415 | $obj->setAttribute('Id', "c$id"); | ||
| 4416 | $obj->setNamespace($nsURI, $ns); | ||
| 4417 | |||
| 4418 | # add 'CreationDate' node | ||
| 4419 | $node = $obj->appendChild(XML::LibXML::Element->new("$ns:CreationDate")); | ||
| 4420 | $node->appendText($datetime); | ||
| 4421 | $node->setNamespace($nsURI, $ns); | ||
| 4422 | |||
| 4423 | # init XPath node hash | ||
| 4424 | %lookup = (); | ||
| 4425 | |||
| 4426 | # for each operation in the group | ||
| 4427 | for my $k (0 .. $#{$ops->[$i]}) { | ||
| 4428 | |||
| 4429 | # set current node to Object | ||
| 4430 | $node = $obj; | ||
| 4431 | |||
| 4432 | # initialize XPath | ||
| 4433 | $xpath = undef; | ||
| 4434 | |||
| 4435 | # for each XPath segment | ||
| 4436 | for (split(/\//, $ops->[$i][$k][2])) { | ||
| 4437 | |||
| 4438 | # add segment to XPath | ||
| 4439 | $xpath = defined($xpath) ? "$xpath/$_" : $_; | ||
| 4440 | |||
| 4441 | # if segment exists | ||
| 4442 | if (exists($lookup{$xpath})) { | ||
| 4443 | |||
| 4444 | # use node | ||
| 4445 | $node = $lookup{$xpath}; | ||
| 4446 | |||
| 4447 | } else { | ||
| 4448 | |||
| 4449 | # add node | ||
| 4450 | $node = $node->appendChild(XML::LibXML::Element->new($_)); | ||
| 4451 | $node->setNamespace($nsURI, $ns); | ||
| 4452 | |||
| 4453 | # add hash entry (except Tag elements) | ||
| 4454 | $lookup{$xpath} = $node if ($_ ne "$ns:Tag"); | ||
| 4455 | |||
| 4456 | } | ||
| 4457 | |||
| 4458 | } | ||
| 4459 | |||
| 4460 | # for each attribute key (if any) | ||
| 4461 | for (keys(%{$ops->[$i][$k][5]})) { | ||
| 4462 | |||
| 4463 | # set node attribute using either data element or hash value | ||
| 4464 | $node->setAttribute($_, (ref($ops->[$i][$k][5]{$_}) eq 'ARRAY') ? $self->[1][$j][$ops->[$i][$k][5]{$_}[0]] : $ops->[$i][$k][5]{$_}); | ||
| 4465 | |||
| 4466 | } | ||
| 4467 | |||
| 4468 | # get data | ||
| 4469 | @data = @{$self->[1][$j]}[@{$ops->[$i][$k][4]}]; | ||
| 4470 | |||
| 4471 | # warn on undefined data | ||
| 4472 | (@data == grep {defined()} @data) || warn("undefined data in sample $j when writing CxF3 file"); | ||
| 4473 | |||
| 4474 | # if subpaths | ||
| 4475 | if (@{$ops->[$i][$k][3]}) { | ||
| 4476 | |||
| 4477 | # for each subpath | ||
| 4478 | for my $s (0 .. $#{$ops->[$i][$k][3]}) { | ||
| 4479 | |||
| 4480 | # add node | ||
| 4481 | # CxF3 schema requires integer values for RGB data | ||
| 4482 | $sub = $node->appendChild(XML::LibXML::Element->new($ops->[$i][$k][3][$s])); | ||
| 4483 | $sub->appendText($ops->[$i][$k][0] eq 'RGB' ? int($data[$s] + 0.5) : $data[$s]); | ||
| 4484 | $sub->setNamespace($nsURI, $ns); | ||
| 4485 | |||
| 4486 | } | ||
| 4487 | |||
| 4488 | # if NCLR class | ||
| 4489 | if ($ops->[$i][$k][0] eq 'NCLR') { | ||
| 4490 | |||
| 4491 | # for each spot color | ||
| 4492 | for my $s (4 .. $#data) { | ||
| 4493 | |||
| 4494 | # add SpotColor elements | ||
| 4495 | $spot = $node->appendChild(XML::LibXML::Element->new("$ns:SpotColor")); | ||
| 4496 | $spot->setNamespace($nsURI, $ns); | ||
| 4497 | $sub = $spot->appendChild(XML::LibXML::Element->new("$ns:Name")); | ||
| 4498 | $sub->appendText('Spot' . ($s + 1)); | ||
| 4499 | $sub->setNamespace($nsURI, $ns); | ||
| 4500 | $sub = $spot->appendChild(XML::LibXML::Element->new("$ns:Percentage")); | ||
| 4501 | $sub->appendText($data[$s]); | ||
| 4502 | $sub->setNamespace($nsURI, $ns); | ||
| 4503 | |||
| 4504 | } | ||
| 4505 | |||
| 4506 | } | ||
| 4507 | |||
| 4508 | # no subpaths and one data value | ||
| 4509 | } elsif (@data == 1) { | ||
| 4510 | |||
| 4511 | # add data as text content | ||
| 4512 | $node->appendText($data[0]); | ||
| 4513 | |||
| 4514 | # no subpaths and multiple data values | ||
| 4515 | } elsif (@data > 1) { | ||
| 4516 | |||
| 4517 | # if DENSITY class | ||
| 4518 | if ($ops->[$i][$k][0] eq 'DENSITY') { | ||
| 4519 | |||
| 4520 | ##### to be done | ||
| 4521 | |||
| 4522 | } else { | ||
| 4523 | |||
| 4524 | # join data and add as text content | ||
| 4525 | $node->appendText(join(' ', @data)); | ||
| 4526 | |||
| 4527 | } | ||
| 4528 | |||
| 4529 | } | ||
| 4530 | |||
| 4531 | } | ||
| 4532 | |||
| 4533 | # add Name attribute to TagCollection element | ||
| 4534 | $lookup{"$ns:TagCollection"}->setAttribute('Name', 'Location') if exists($lookup{"$ns:TagCollection"}); | ||
| 4535 | |||
| 4536 | # if nothing was added to Object | ||
| 4537 | if ($node->isSameNode($obj)) { | ||
| 4538 | |||
| 4539 | # unbind the node | ||
| 4540 | $node->unbindNode(); | ||
| 4541 | |||
| 4542 | } else { | ||
| 4543 | |||
| 4544 | # increment Object Id | ||
| 4545 | $id++; | ||
| 4546 | |||
| 4547 | } | ||
| 4548 | |||
| 4549 | } | ||
| 4550 | |||
| 4551 | } | ||
| 4552 | |||
| 4553 | # validate the CxF3 document | ||
| 4554 | _validateCxF3($dom) if (defined($hash->{'validate'}) && $hash->{'validate'}); | ||
| 4555 | |||
| 4556 | # resolve file list from path | ||
| 4557 | (@files = File::Glob::bsd_glob($path)) || croak('invalid file path'); | ||
| 4558 | |||
| 4559 | # verify file path is unique | ||
| 4560 | (@files == 1) || warn('file path not unique'); | ||
| 4561 | |||
| 4562 | # write CxF3 file | ||
| 4563 | $dom->toFile($files[0], 1); | ||
| 4564 | |||
| 4565 | } | ||
| 4566 | |||
| 4567 | # write chart data array as delimited ASCII file (for Excel, R, MATLAB, etc.) | ||
| 4568 | # optional slice parameters are either scalars, array references or 'Math::Matrix' objects | ||
| 4569 | # optional hash parameter keys: 'header', 'sep', 'eol', and 'undef' | ||
| 4570 | # parameters: (path_to_file, [row_slice, [column_slice]], [hash]) | ||
| 4571 | sub writeASCII { | ||
| 4572 | |||
| 4573 | # local variables | ||
| 4574 | my ($hash, $row_length, $n, @files, $fh); | ||
| 4575 | my ($fs, $rs, $undef, $hdr, @fields); | ||
| 4576 | |||
| 4577 | # get optional hash parameter | ||
| 4578 | $hash = pop() if (ref($_[-1]) eq 'HASH'); | ||
| 4579 | |||
| 4580 | # get remaining parameters | ||
| 4581 | my ($self, $path, $rows, $cols) = @_; | ||
| 4582 | |||
| 4583 | # if row slice defined | ||
| 4584 | if (defined($rows)) { | ||
| 4585 | |||
| 4586 | # if row slice an empty array reference | ||
| 4587 | if (ref($rows) eq 'ARRAY' && @{$rows} == 0) { | ||
| 4588 | |||
| 4589 | # use all rows | ||
| 4590 | $rows = [1 .. $#{$self->[1]}]; | ||
| 4591 | |||
| 4592 | } else { | ||
| 4593 | |||
| 4594 | # get row length if row slice is Math::Matrix object | ||
| 4595 | $row_length = @{$rows->[0]} if (UNIVERSAL::isa($rows, 'Math::Matrix')); | ||
| 4596 | |||
| 4597 | # flatten row slice | ||
| 4598 | $rows = ICC::Shared::flatten($rows); | ||
| 4599 | |||
| 4600 | } | ||
| 4601 | |||
| 4602 | } else { | ||
| 4603 | |||
| 4604 | # use all rows | ||
| 4605 | $rows = [1 .. $#{$self->[1]}]; | ||
| 4606 | |||
| 4607 | } | ||
| 4608 | |||
| 4609 | # get number of rows | ||
| 4610 | $n = @{$rows}; | ||
| 4611 | |||
| 4612 | # filter row slice | ||
| 4613 | @{$rows} = grep {$_ == int($_) && $_ != 0 && defined($self->[1][$_])} @{$rows}; | ||
| 4614 | |||
| 4615 | # warn if invalid samples | ||
| 4616 | ($n == @{$rows}) || warn('row slice contains invalid samples'); | ||
| 4617 | |||
| 4618 | # if column slice defined | ||
| 4619 | if (defined($cols)) { | ||
| 4620 | |||
| 4621 | # if column slice an empty array reference | ||
| 4622 | if (ref($cols) eq 'ARRAY' && @{$cols} == 0) { | ||
| 4623 | |||
| 4624 | # use all columns | ||
| 4625 | $cols = [0 .. $#{$self->[1][0]}]; | ||
| 4626 | |||
| 4627 | } else { | ||
| 4628 | |||
| 4629 | # flatten column slice | ||
| 4630 | $cols = ICC::Shared::flatten($cols); | ||
| 4631 | |||
| 4632 | } | ||
| 4633 | |||
| 4634 | } else { | ||
| 4635 | |||
| 4636 | # use all columns | ||
| 4637 | $cols = [0 .. $#{$self->[1][0]}]; | ||
| 4638 | |||
| 4639 | } | ||
| 4640 | |||
| 4641 | # get number of columns | ||
| 4642 | $n = @{$cols}; | ||
| 4643 | |||
| 4644 | # filter column slice | ||
| 4645 | @{$cols} = grep {$_ == int($_) && defined($self->[1][0][$_])} @{$cols}; | ||
| 4646 | |||
| 4647 | # warn if invalid fields | ||
| 4648 | ($n == @{$cols}) || warn('column slice contains invalid fields'); | ||
| 4649 | |||
| 4650 | # resolve file list from path | ||
| 4651 | (defined($path) && (! ref($path)) && (@files = File::Glob::bsd_glob($path))) || croak("invalid path: $path, stopped"); | ||
| 4652 | |||
| 4653 | # verify file path is unique | ||
| 4654 | (@files == 1) || warn('file path not unique'); | ||
| 4655 | |||
| 4656 | # open the file | ||
| 4657 | open($fh, '>', $files[0]) || croak("$! when opening $files[0], stopped"); | ||
| 4658 | |||
| 4659 | # get header mode | ||
| 4660 | $hdr = $hash->{'header'} || 1; | ||
| 4661 | |||
| 4662 | # get the field separator | ||
| 4663 | $fs = $hash->{'sep'} || "\t"; | ||
| 4664 | |||
| 4665 | # get the record separator | ||
| 4666 | $rs = $hash->{'eol'} || "\n"; | ||
| 4667 | |||
| 4668 | # get the undefined string | ||
| 4669 | $undef = $hash->{'undef'} || ''; | ||
| 4670 | |||
| 4671 | # if header enabled | ||
| 4672 | if ($hdr) { | ||
| 4673 | |||
| 4674 | # if format fields, replacing undefined values | ||
| 4675 | if (@fields = map {defined() ? $_ : $undef} @{$self->[1][0]}[@{$cols}]) { | ||
| 4676 | |||
| 4677 | # if header mode 2, remove contexts | ||
| 4678 | if ($hdr == 2) {for (@fields) {s/^.*\|//}}; | ||
| 4679 | |||
| 4680 | # trim leading and trailing white space, and replace spaces with underscores | ||
| 4681 | for (@fields) {s/^\s*(.*?)\s*$/$1/; s/ /_/g}; | ||
| 4682 | |||
| 4683 | # print format record | ||
| 4684 | print $fh join($fs, @fields), $rs; | ||
| 4685 | |||
| 4686 | } | ||
| 4687 | |||
| 4688 | } | ||
| 4689 | |||
| 4690 | # for each row | ||
| 4691 | for my $i (@{$rows}) { | ||
| 4692 | |||
| 4693 | # get data fields, replacing undefined values | ||
| 4694 | @fields = map {defined() ? $_ : $undef} @{$self->[1][$i]}[@{$cols}]; | ||
| 4695 | |||
| 4696 | # trim leading and trailing white space, and replace spaces with underscores | ||
| 4697 | for (@fields) {s/^\s*(.*?)\s*$/$1/; s/ /_/g}; | ||
| 4698 | |||
| 4699 | # print the data record | ||
| 4700 | print $fh join($fs, @fields), $rs; | ||
| 4701 | |||
| 4702 | } | ||
| 4703 | |||
| 4704 | # close the file | ||
| 4705 | close($fh); | ||
| 4706 | |||
| 4707 | } | ||
| 4708 | |||
| 4709 | # write TIFF file | ||
| 4710 | # RGB, CMYK, and CIE L*a*b* color spaces supported | ||
| 4711 | # 8-bit, 16-bit or 32-bit, Intel or Motorola byte order supported | ||
| 4712 | # alpha and spot channels in RGB and CMYK files supported | ||
| 4713 | # supported hash keys: 'width', 'height', 'gap', 'left', 'right', 'rows', 'bits', 'dither', 'endian', 'xres', 'yres', 'unit' | ||
| 4714 | # parameters: (path_to_file, [row_slice, [column_slice]], [hash]) | ||
| 4715 | sub writeTIFF { | ||
| 4716 | |||
| 4717 | # local variables | ||
| 4718 | my ($hash, $trows, $tcols, $n, @files, $fh); | ||
| 4719 | my ($base, $cs, %fields, @alpha, $pi, $rcols, $fmt, $mult, $mab, $samples); | ||
| 4720 | my ($width, $height, $gap, $left, $right, $bits, $xres, $yres, $unit); | ||
| 4721 | my ($le, $short, $long, $fp, $max, $minab, $maxab); | ||
| 4722 | my ($tags, $imagewidth, $bytecount, $stripsize); | ||
| 4723 | my ($ifd, $data, @cmyk, @spot); | ||
| 4724 | |||
| 4725 | # get optional hash | ||
| 4726 | $hash = pop() if (ref($_[-1]) eq 'HASH'); | ||
| 4727 | |||
| 4728 | # get remaining parameters | ||
| 4729 | my ($self, $path, $rows, $cols) = @_; | ||
| 4730 | |||
| 4731 | # if row slice defined | ||
| 4732 | if (defined($rows)) { | ||
| 4733 | |||
| 4734 | # if row slice an empty array reference | ||
| 4735 | if (ref($rows) eq 'ARRAY' && @{$rows} == 0) { | ||
| 4736 | |||
| 4737 | # use all rows | ||
| 4738 | $rows = [1 .. $#{$self->[1]}]; | ||
| 4739 | |||
| 4740 | } else { | ||
| 4741 | |||
| 4742 | # get row length if row slice is Math::Matrix object | ||
| 4743 | $trows = @{$rows->[0]} if (UNIVERSAL::isa($rows, 'Math::Matrix')); | ||
| 4744 | |||
| 4745 | # flatten row slice | ||
| 4746 | $rows = ICC::Shared::flatten($rows); | ||
| 4747 | |||
| 4748 | } | ||
| 4749 | |||
| 4750 | } else { | ||
| 4751 | |||
| 4752 | # use all rows | ||
| 4753 | $rows = [1 .. $#{$self->[1]}]; | ||
| 4754 | |||
| 4755 | } | ||
| 4756 | |||
| 4757 | # get number of rows | ||
| 4758 | $n = @{$rows}; | ||
| 4759 | |||
| 4760 | # filter row slice | ||
| 4761 | @{$rows} = grep {$_ == int($_) && $_ != 0 && defined($self->[1][$_])} @{$rows}; | ||
| 4762 | |||
| 4763 | # warn if invalid samples | ||
| 4764 | ($n == @{$rows}) || warn('row slice contains invalid samples'); | ||
| 4765 | |||
| 4766 | # get target row length, if not defined by row matrix | ||
| 4767 | $trows = _getRowLength($self, $hash) if (! defined($trows)); | ||
| 4768 | |||
| 4769 | # limit to number of samples | ||
| 4770 | $trows = $trows > $n ? $n : $trows; | ||
| 4771 | |||
| 4772 | # verify row length | ||
| 4773 | ($trows == int($trows) && $trows > 0) || croak('invalid row length, stopped'); | ||
| 4774 | |||
| 4775 | # compute target columns | ||
| 4776 | $tcols = int($n/$trows) + ($n % $trows ? 1 : 0); | ||
| 4777 | |||
| 4778 | # if column slice defined | ||
| 4779 | if (defined($cols)) { | ||
| 4780 | |||
| 4781 | # if column slice an empty array reference | ||
| 4782 | if (ref($cols) eq 'ARRAY' && @{$cols} == 0) { | ||
| 4783 | |||
| 4784 | # use all columns | ||
| 4785 | $cols = [0 .. $#{$self->[1][0]}]; | ||
| 4786 | |||
| 4787 | } else { | ||
| 4788 | |||
| 4789 | # flatten column slice | ||
| 4790 | $cols = ICC::Shared::flatten($cols); | ||
| 4791 | |||
| 4792 | } | ||
| 4793 | |||
| 4794 | } else { | ||
| 4795 | |||
| 4796 | # use all columns | ||
| 4797 | $cols = [0 .. $#{$self->[1][0]}]; | ||
| 4798 | |||
| 4799 | } | ||
| 4800 | |||
| 4801 | # get number of columns | ||
| 4802 | $n = @{$cols}; | ||
| 4803 | |||
| 4804 | # filter column slice | ||
| 4805 | @{$cols} = grep {$_ == int($_) && defined($self->[1][0][$_])} @{$cols}; | ||
| 4806 | |||
| 4807 | # warn if invalid fields | ||
| 4808 | ($n == @{$cols}) || warn('column slice contains invalid fields'); | ||
| 4809 | |||
| 4810 | # for each column in slice | ||
| 4811 | for (@{$self->[1][0]}[@{$cols}]) { | ||
| 4812 | |||
| 4813 | # if a supported color space | ||
| 4814 | if (m/^((?:.*\|)?(RGB|CMYK|[4-9A-F]CLR|LAB)_)/) { | ||
| 4815 | |||
| 4816 | # set base and color space | ||
| 4817 | $base = $1; | ||
| 4818 | $cs = $2; | ||
| 4819 | |||
| 4820 | # quit loop | ||
| 4821 | last(); | ||
| 4822 | |||
| 4823 | } | ||
| 4824 | |||
| 4825 | } | ||
| 4826 | |||
| 4827 | # verify color space | ||
| 4828 | (defined($cs)) || croak('column slice does not contain a supported color space, stopped'); | ||
| 4829 | |||
| 4830 | # get bits per sample and verify | ||
| 4831 | $bits = defined($hash->{'bits'}) ? $hash->{'bits'} : 16; | ||
| 4832 | ($bits == 8 || $bits == 16 || $bits == 32) || croak('invalid \'bits\' parameter, stopped'); | ||
| 4833 | |||
| 4834 | # set little-endian flag from system config | ||
| 4835 | $le = ($Config{'byteorder'} =~ m/1234/); | ||
| 4836 | |||
| 4837 | # if endian parameter provided | ||
| 4838 | if (defined($hash->{'endian'})) { | ||
| 4839 | |||
| 4840 | # if little-endian | ||
| 4841 | if ($hash->{'endian'} eq 'little') { | ||
| 4842 | |||
| 4843 | # set flag | ||
| 4844 | $le = 1; | ||
| 4845 | |||
| 4846 | # if big-endian | ||
| 4847 | } elsif ($hash->{'endian'} eq 'big') { | ||
| 4848 | |||
| 4849 | # clear flag | ||
| 4850 | $le = 0; | ||
| 4851 | |||
| 4852 | } else { | ||
| 4853 | |||
| 4854 | # warn | ||
| 4855 | warn('invalid \'endian\' parameter'); | ||
| 4856 | |||
| 4857 | } | ||
| 4858 | |||
| 4859 | } | ||
| 4860 | |||
| 4861 | # if little-endian | ||
| 4862 | if ($le) { | ||
| 4863 | |||
| 4864 | # set 'pack' formats | ||
| 4865 | $short = 'v'; | ||
| 4866 | $long = 'V'; | ||
| 4867 | $fp = 'f<'; | ||
| 4868 | |||
| 4869 | } else { | ||
| 4870 | |||
| 4871 | # set 'pack' formats | ||
| 4872 | $short = 'n'; | ||
| 4873 | $long = 'N'; | ||
| 4874 | $fp = 'f>'; | ||
| 4875 | |||
| 4876 | } | ||
| 4877 | |||
| 4878 | # make lookup hash of column slice fields | ||
| 4879 | %fields = map {defined($self->[1][0][$_]) ? ($self->[1][0][$_], $_) : ()} @{$cols}; | ||
| 4880 | |||
| 4881 | # if color space is RGB | ||
| 4882 | if ($cs eq 'RGB') { | ||
| 4883 | |||
| 4884 | # set photometric interpretation | ||
| 4885 | $pi = 2; | ||
| 4886 | |||
| 4887 | # get alpha channels (if any) | ||
| 4888 | @alpha = map {defined($fields{"$base$_"}) ? $fields{"$base$_"} : ()} ('A', 'A0' .. 'A9'); | ||
| 4889 | |||
| 4890 | # get refined column slice (including alpha channels) | ||
| 4891 | $rcols = [(map {$fields{"$base$_"}} qw(R G B)), @alpha]; | ||
| 4892 | |||
| 4893 | # set pack format (8, 16 or 32 bits) | ||
| 4894 | $fmt = ($bits == 8) ? 'C*' : ($bits == 16) ? "$short*" : "$fp*"; | ||
| 4895 | |||
| 4896 | # set multiplier (8, 16 or 32 bits) | ||
| 4897 | $mult = ($bits == 8) ? 1 : ($bits == 16) ? 257 : 1/255; | ||
| 4898 | |||
| 4899 | # if color space is CMYK (8 or 16 bits) | ||
| 4900 | } elsif ($cs eq 'CMYK' && $bits != 32) { | ||
| 4901 | |||
| 4902 | # set photometric interpretation | ||
| 4903 | $pi = 5; | ||
| 4904 | |||
| 4905 | # get refined column slice | ||
| 4906 | $rcols = [map {$fields{"$base$_"}} qw(C M Y K)]; | ||
| 4907 | |||
| 4908 | # set pack format (8 or 16 bits) | ||
| 4909 | $fmt = ($bits == 8) ? 'C*' : "$short*"; | ||
| 4910 | |||
| 4911 | # set multiplier (8 or 16 bits) | ||
| 4912 | $mult = ($bits == 8) ? 2.55 : 655.35; | ||
| 4913 | |||
| 4914 | # if color space is nCLR (8 or 16 bits) | ||
| 4915 | } elsif ($cs =~ m/^([4-9A-F])CLR$/ && $bits != 32) { | ||
| 4916 | |||
| 4917 | # set photometric interpretation | ||
| 4918 | $pi = 5; | ||
| 4919 | |||
| 4920 | # get refined column slice | ||
| 4921 | $rcols = [map {$fields{sprintf('%s%x', $base, $_)}} (1 .. CORE::hex($1))]; | ||
| 4922 | |||
| 4923 | # set pack format (8 or 16 bits) | ||
| 4924 | $fmt = ($bits == 8) ? 'C*' : "$short*"; | ||
| 4925 | |||
| 4926 | # set multiplier (8 or 16 bits) | ||
| 4927 | $mult = ($bits == 8) ? 2.55 : 655.35; | ||
| 4928 | |||
| 4929 | # if color space if L*a*b* (8 or 16 bits) | ||
| 4930 | } elsif ($cs eq 'LAB' && $bits != 32) { | ||
| 4931 | |||
| 4932 | # set photometric interpretation | ||
| 4933 | $pi = 8; | ||
| 4934 | |||
| 4935 | # get refined column slice | ||
| 4936 | $rcols = [map {$fields{"$base$_"}} qw(L A B)]; | ||
| 4937 | |||
| 4938 | # set pack format (8 or 16 bits) | ||
| 4939 | $fmt = ($bits == 8) ? '(Ccc)*' : "$short*"; | ||
| 4940 | |||
| 4941 | # set multipliers (8 or 16 bits) | ||
| 4942 | $mult = ($bits == 8) ? 2.55 : 655.35; # L* | ||
| 4943 | $mab = ($bits == 8) ? 1 : 256; # a* and b* | ||
| 4944 | |||
| 4945 | } else { | ||
| 4946 | |||
| 4947 | # error | ||
| 4948 | croak('invalid TIFF format'); | ||
| 4949 | |||
| 4950 | } | ||
| 4951 | |||
| 4952 | # verify all fields defined | ||
| 4953 | (@{$rcols} == grep {defined()} @{$rcols}) || croak('column slice has missing fields, stopped'); | ||
| 4954 | |||
| 4955 | # set number of samples | ||
| 4956 | $samples = @{$rcols}; | ||
| 4957 | |||
| 4958 | # get the sample patch width and verify | ||
| 4959 | $width = defined($hash->{'width'}) ? $hash->{'width'} : 1; | ||
| 4960 | ($width == int($width) && $width > 0) || croak('invalid \'width\' parameter, stopped'); | ||
| 4961 | |||
| 4962 | # get the sample patch height and verify | ||
| 4963 | $height = defined($hash->{'height'}) ? $hash->{'height'} : 1; | ||
| 4964 | ($height == int($height) && $height > 0) || croak('invalid \'height\' parameter, stopped'); | ||
| 4965 | |||
| 4966 | # get the sample patch gap and verify | ||
| 4967 | $gap = defined($hash->{'gap'}) ? $hash->{'gap'} : 0; | ||
| 4968 | ($gap == int($gap) && $gap >= 0) || croak('invalid \'gap\' parameter, stopped'); | ||
| 4969 | |||
| 4970 | # get the left edge width and verify | ||
| 4971 | $left = defined($hash->{'left'}) ? $hash->{'left'} : 0; | ||
| 4972 | ($left =~ m/^([0-9]+)(?:\.([0-9]+))?$/ && (! defined($2) || $1 >= $2)) || croak('invalid \'left\' parameter, stopped'); | ||
| 4973 | $left = [$1, defined($2) ? $2 : 0]; | ||
| 4974 | |||
| 4975 | # get the right edge width and verify | ||
| 4976 | $right = defined($hash->{'right'}) ? $hash->{'right'} : 0; | ||
| 4977 | ($right =~ m/^([0-9]+)(?:\.([0-9]+))?$/ && (! defined($2) || $1 >= $2)) || croak('invalid \'right\' parameter, stopped'); | ||
| 4978 | $right = [$1, defined($2) ? $2 : 0]; | ||
| 4979 | |||
| 4980 | # get the x-resolution and verify | ||
| 4981 | $xres = defined($hash->{'xres'}) ? $hash->{'xres'} : 72; | ||
| 4982 | ($xres > 0 && $xres <= 4E4) || croak('invalid \'xres\' parameter, stopped'); | ||
| 4983 | |||
| 4984 | # get the y-resolution and verify | ||
| 4985 | $yres = defined($hash->{'yres'}) ? $hash->{'yres'} : 72; | ||
| 4986 | ($yres > 0 && $yres <= 4E4) || croak('invalid \'yres\' parameter, stopped'); | ||
| 4987 | |||
| 4988 | # get the resolution unit and verify | ||
| 4989 | $unit = defined($hash->{'unit'}) ? $hash->{'unit'} : 2; | ||
| 4990 | ($unit == 1 || $unit == 2 || $unit == 3) || croak('invalid \'unit\' parameter, stopped'); | ||
| 4991 | |||
| 4992 | # compute image width | ||
| 4993 | $imagewidth = $tcols * $width + ($tcols - 1) * $gap + $left->[0] - $left->[1] + $right->[0] - $right->[1]; | ||
| 4994 | |||
| 4995 | # compute strip byte count | ||
| 4996 | $bytecount = $imagewidth * $height * $samples * $bits/8; | ||
| 4997 | |||
| 4998 | # compute strip size (strips must begin on word boundary) | ||
| 4999 | $stripsize = $bytecount + $bytecount % 2; | ||
| 5000 | |||
| 5001 | # set image tags [type, data] | ||
| 5002 | $tags->{'256'} = [3, $imagewidth]; # ImageWidth | ||
| 5003 | $tags->{'257'} = [3, $trows * $height]; # ImageLength | ||
| 5004 | $tags->{'258'} = [3, ($bits) x $samples]; # BitsPerSample | ||
| 5005 | $tags->{'259'} = [3, 1]; # Compression | ||
| 5006 | $tags->{'262'} = [3, $pi]; # PhotometricInterpretation | ||
| 5007 | $tags->{'273'} = [4, map {$_ * $stripsize + 8} (0 .. $trows - 1)]; # StripOffsets | ||
| 5008 | $tags->{'277'} = [3, $samples]; # SamplesPerPixel | ||
| 5009 | $tags->{'278'} = [3, $height]; # RowsPerStrip | ||
| 5010 | $tags->{'279'} = [4, ($bytecount) x $trows]; # StripByteCounts | ||
| 5011 | $tags->{'282'} = [5, $xres * 1E4, 1E4]; # XResolution | ||
| 5012 | $tags->{'283'} = [5, $yres * 1E4, 1E4]; # YResolution | ||
| 5013 | $tags->{'296'} = [3, $unit]; # ResolutionUnit | ||
| 5014 | $tags->{'339'} = [3, (3) x $samples] if ($bits == 32); # SampleFormat | ||
| 5015 | |||
| 5016 | # resolve file list from path | ||
| 5017 | (defined($path) && (! ref($path)) && (@files = File::Glob::bsd_glob($path))) || croak("invalid path: $path, stopped"); | ||
| 5018 | |||
| 5019 | # verify file path is unique | ||
| 5020 | (@files == 1) || warn('file path not unique'); | ||
| 5021 | |||
| 5022 | # open the file | ||
| 5023 | open($fh, '>', $files[0]) || croak("$! when opening $files[0], stopped"); | ||
| 5024 | |||
| 5025 | # set binary mode | ||
| 5026 | binmode($fh); | ||
| 5027 | |||
| 5028 | # write TIFF header | ||
| 5029 | print $fh pack("A2$short$long", $le ? 'II' : 'MM', 42, $ifd = $trows * $stripsize + 8); | ||
| 5030 | |||
| 5031 | # set min/max values | ||
| 5032 | $max = ($bits == 8) ? 255 : ($bits == 16) ? 65535 : 1; | ||
| 5033 | $minab = ($bits == 8) ? -128 : -32768; | ||
| 5034 | $maxab = ($bits == 8) ? 127 : 32767; | ||
| 5035 | |||
| 5036 | # for each strip | ||
| 5037 | for my $i (0 .. $trows - 1) { | ||
| 5038 | |||
| 5039 | # for each patch in strip | ||
| 5040 | for my $j (0 .. $tcols - 1) { | ||
| 5041 | |||
| 5042 | # if patch in row slice | ||
| 5043 | if (defined($rows->[$trows * $j + $i])) { | ||
| 5044 | |||
| 5045 | # if L*a*b* data | ||
| 5046 | if ($pi == 8) { | ||
| 5047 | |||
| 5048 | # get the data | ||
| 5049 | $data->[$j][0] = $mult * $self->[1][$rows->[$trows * $j + $i]][$rcols->[0]]; | ||
| 5050 | $data->[$j][1] = $mab * $self->[1][$rows->[$trows * $j + $i]][$rcols->[1]]; | ||
| 5051 | $data->[$j][2] = $mab * $self->[1][$rows->[$trows * $j + $i]][$rcols->[2]]; | ||
| 5052 | |||
| 5053 | # limit the data | ||
| 5054 | $data->[$j][0] = $data->[$j][0] < 0 ? 0 : ($data->[$j][0] > $max ? $max : $data->[$j][0]); | ||
| 5055 | $data->[$j][1] = $data->[$j][1] < $minab ? $minab : ($data->[$j][1] > $maxab ? $maxab : $data->[$j][1]); | ||
| 5056 | $data->[$j][2] = $data->[$j][2] < $minab ? $minab : ($data->[$j][2] > $maxab ? $maxab : $data->[$j][2]); | ||
| 5057 | |||
| 5058 | # if CMYK + spot data | ||
| 5059 | } elsif ($pi == 5 && @{$rcols} > 4) { | ||
| 5060 | |||
| 5061 | # get CMYK values | ||
| 5062 | @cmyk = @{$self->[1][$rows->[$trows * $j + $i]]}[@{$rcols}[0 .. 3]]; | ||
| 5063 | |||
| 5064 | # get spot values | ||
| 5065 | @spot = @{$self->[1][$rows->[$trows * $j + $i]]}[@{$rcols}[4 .. $#{$rcols}]]; | ||
| 5066 | |||
| 5067 | # get the data (spot channels are inverted) | ||
| 5068 | $data->[$j] = [(map {$_ * $mult} @cmyk), (map {(100 - $_) * $mult} @spot)]; | ||
| 5069 | |||
| 5070 | # limit the data | ||
| 5071 | @{$data->[$j]} = map {$_ < 0 ? 0 : ($_ > $max ? $max : $_)} @{$data->[$j]}; | ||
| 5072 | |||
| 5073 | # RGB data | ||
| 5074 | } else { | ||
| 5075 | |||
| 5076 | # get the data | ||
| 5077 | $data->[$j] = [map {$_ * $mult} @{$self->[1][$rows->[$trows * $j + $i]]}[@{$rcols}]]; | ||
| 5078 | |||
| 5079 | # limit the data (8 or 16 bits) | ||
| 5080 | @{$data->[$j]} = map {$_ < 0 ? 0 : ($_ > $max ? $max : $_)} @{$data->[$j]} if ($bits != 32); | ||
| 5081 | |||
| 5082 | } | ||
| 5083 | |||
| 5084 | # patch undefined | ||
| 5085 | } else { | ||
| 5086 | |||
| 5087 | # if L*a*b* data | ||
| 5088 | if ($pi == 8) { | ||
| 5089 | |||
| 5090 | # if last patch | ||
| 5091 | if ($i == ($trows - 1) && $j == ($tcols - 1)) { | ||
| 5092 | |||
| 5093 | # set gray value | ||
| 5094 | $data->[$j] = [$max * 0.7, 0, 0]; | ||
| 5095 | |||
| 5096 | } else { | ||
| 5097 | |||
| 5098 | # set white value | ||
| 5099 | $data->[$j] = [$max, 0, 0]; | ||
| 5100 | |||
| 5101 | } | ||
| 5102 | |||
| 5103 | # if CMYK + spot data | ||
| 5104 | } elsif ($pi == 5) { | ||
| 5105 | |||
| 5106 | # if last patch | ||
| 5107 | if ($i == ($trows - 1) && $j == ($tcols - 1)) { | ||
| 5108 | |||
| 5109 | # set gray value | ||
| 5110 | $data->[$j] = [0, 0, 0, $max * 0.4, ($max) x ($samples - 4)]; | ||
| 5111 | |||
| 5112 | } else { | ||
| 5113 | |||
| 5114 | # set white value | ||
| 5115 | $data->[$j] = [0, 0, 0, 0, ($max) x ($samples - 4)]; | ||
| 5116 | |||
| 5117 | } | ||
| 5118 | |||
| 5119 | # RGB data | ||
| 5120 | } else { | ||
| 5121 | |||
| 5122 | # if last patch | ||
| 5123 | if ($i == ($trows - 1) && $j == ($tcols - 1)) { | ||
| 5124 | |||
| 5125 | # set gray value | ||
| 5126 | $data->[$j] = [($max * 0.7) x $samples]; | ||
| 5127 | |||
| 5128 | } else { | ||
| 5129 | |||
| 5130 | # set white value | ||
| 5131 | $data->[$j] = [($max) x $samples]; | ||
| 5132 | |||
| 5133 | } | ||
| 5134 | |||
| 5135 | } | ||
| 5136 | |||
| 5137 | } | ||
| 5138 | |||
| 5139 | } | ||
| 5140 | |||
| 5141 | # write TIFF strip | ||
| 5142 | _writeTIFFstrip($fh, $tags, $width, $gap, $left, $right, $i, $data, $fmt, $hash->{'dither'}); | ||
| 5143 | |||
| 5144 | } | ||
| 5145 | |||
| 5146 | # write TIFF IFD | ||
| 5147 | _writeTIFFdir($fh, $ifd, $short, $long, $tags); | ||
| 5148 | |||
| 5149 | # close file | ||
| 5150 | close($fh); | ||
| 5151 | |||
| 5152 | } | ||
| 5153 | |||
| 5154 | # write chart to Adobe Swatch Exchange (.ase) file | ||
| 5155 | # column slice must be CMYK, RGB or L*a*b* | ||
| 5156 | # color type: 0 - global, 1 - spot, 2 - normal (default) | ||
| 5157 | # parameters: (path_to_file, row_slice, column_slice, [color_type]) | ||
| 5158 | sub writeASE { | ||
| 5159 | |||
| 5160 | # get parameters | ||
| 5161 | my ($self, $path, $rows, $cols, $type) = @_; | ||
| 5162 | |||
| 5163 | # local variables | ||
| 5164 | my ($n, @fmt, $cs, $le, $sn, @files, $fh); | ||
| 5165 | my ($name, $slen, $blen); | ||
| 5166 | my ($cmyk, $rgb, $Lab, $val); | ||
| 5167 | |||
| 5168 | # verify row_slice and column_slice are supplied | ||
| 5169 | (defined($rows) && defined($cols)) || croak('missing parameters'); | ||
| 5170 | |||
| 5171 | # if row slice an empty array reference | ||
| 5172 | if (ref($rows) eq 'ARRAY' && @{$rows} == 0) { | ||
| 5173 | |||
| 5174 | # use all rows | ||
| 5175 | $rows = [1 .. $#{$self->[1]}]; | ||
| 5176 | |||
| 5177 | } else { | ||
| 5178 | |||
| 5179 | # flatten row slice | ||
| 5180 | $rows = ICC::Shared::flatten($rows); | ||
| 5181 | |||
| 5182 | } | ||
| 5183 | |||
| 5184 | # get number of rows | ||
| 5185 | $n = @{$rows}; | ||
| 5186 | |||
| 5187 | # filter row slice | ||
| 5188 | @{$rows} = grep {$_ == int($_) && $_ != 0 && defined($self->[1][$_])} @{$rows}; | ||
| 5189 | |||
| 5190 | # warn if invalid samples | ||
| 5191 | ($n == @{$rows}) || warn('row slice contains invalid samples'); | ||
| 5192 | |||
| 5193 | # get format array | ||
| 5194 | @fmt = @{$self->[1][0]}[@{$cols}]; | ||
| 5195 | |||
| 5196 | # if column slice is CMYK | ||
| 5197 | if (4 == @fmt && 4 == grep {m/^(?:.*\|)?CMYK_[CMYK]$/} @fmt) { | ||
| 5198 | |||
| 5199 | # set color space | ||
| 5200 | $cs = 'CMYK'; | ||
| 5201 | |||
| 5202 | # if column slice is RGB | ||
| 5203 | } elsif (3 == @fmt && 3 == grep {m/^(?:.*\|)?RGB_[RGB]$/} @fmt) { | ||
| 5204 | |||
| 5205 | # set color space | ||
| 5206 | $cs = 'RGB '; | ||
| 5207 | |||
| 5208 | # if column slice is L*a*b* | ||
| 5209 | } elsif (3 == @fmt && 3 == grep {m/^(?:.*\|)?LAB_[LAB]$/} @fmt) { | ||
| 5210 | |||
| 5211 | # set color space | ||
| 5212 | $cs = 'LAB '; | ||
| 5213 | |||
| 5214 | } else { | ||
| 5215 | |||
| 5216 | # error | ||
| 5217 | croak('invalid column slice'); | ||
| 5218 | |||
| 5219 | } | ||
| 5220 | |||
| 5221 | # if color type is undefined, set default (2 - normal) | ||
| 5222 | $type = 2 if (! defined($type)); | ||
| 5223 | |||
| 5224 | # verify color type | ||
| 5225 | ($type == int($type) && $type >= 0 && $type <= 2) || croak('invalid ASE color type'); | ||
| 5226 | |||
| 5227 | # get little-endian flag | ||
| 5228 | $le = ($Config{'byteorder'} =~ m/1234/); | ||
| 5229 | |||
| 5230 | # get sample name slice (could be undefined) | ||
| 5231 | $sn = $self->name; | ||
| 5232 | |||
| 5233 | # resolve file list from path | ||
| 5234 | (@files = File::Glob::bsd_glob($path)) || croak('invalid file path'); | ||
| 5235 | |||
| 5236 | # verify file path is unique | ||
| 5237 | (@files == 1) || warn('file path not unique'); | ||
| 5238 | |||
| 5239 | # open file | ||
| 5240 | open($fh, '>', $files[0]); | ||
| 5241 | |||
| 5242 | # set binary mode | ||
| 5243 | binmode($fh); | ||
| 5244 | |||
| 5245 | # print header (file signature, version, number of blocks) | ||
| 5246 | print $fh pack('A4nnN', 'ASEF', 1, 0, scalar(@{$rows})); | ||
| 5247 | |||
| 5248 | # for each sample | ||
| 5249 | for my $s (@{$rows}) { | ||
| 5250 | |||
| 5251 | # if color space is CMYK | ||
| 5252 | if ($cs eq 'CMYK') { | ||
| 5253 | |||
| 5254 | # get the CMYK values | ||
| 5255 | $cmyk = $self->slice([$s], $cols); | ||
| 5256 | |||
| 5257 | # if SAMPLE_NAME is defined | ||
| 5258 | if (defined($sn)) { | ||
| 5259 | |||
| 5260 | # get color name | ||
| 5261 | $name = $self->[1][$s][$sn->[0]]; | ||
| 5262 | |||
| 5263 | # replace underscores with spaces | ||
| 5264 | $name =~ s/_/ /g; | ||
| 5265 | |||
| 5266 | } else { | ||
| 5267 | |||
| 5268 | # build color name from CMYK values | ||
| 5269 | $name = sprintf('C=%d M=%d Y=%d K=%d', @{$cmyk->[0]}); | ||
| 5270 | |||
| 5271 | } | ||
| 5272 | |||
| 5273 | # compute string length | ||
| 5274 | $slen = length($name) + 1; | ||
| 5275 | |||
| 5276 | # compute block length | ||
| 5277 | $blen = 2 * $slen + 24; | ||
| 5278 | |||
| 5279 | # print block | ||
| 5280 | print $fh pack('nNn', 1, $blen, $slen); | ||
| 5281 | print $fh encode('UTF-16BE', $name . "\x00"); | ||
| 5282 | print $fh pack('A4', 'CMYK'); | ||
| 5283 | |||
| 5284 | # for each CMYK value | ||
| 5285 | for my $i (0 .. 3) { | ||
| 5286 | |||
| 5287 | # convert to floating point | ||
| 5288 | $val = pack('f', $cmyk->[0][$i]/100); | ||
| 5289 | |||
| 5290 | # reverse if little-endian system | ||
| 5291 | $val = reverse($val) if ($le); | ||
| 5292 | |||
| 5293 | # print value | ||
| 5294 | print $fh $val; | ||
| 5295 | |||
| 5296 | } | ||
| 5297 | |||
| 5298 | # print color type | ||
| 5299 | print $fh pack('n', $type); | ||
| 5300 | |||
| 5301 | # if color space is RGB | ||
| 5302 | } elsif ($cs eq 'RGB ') { | ||
| 5303 | |||
| 5304 | # get the RGB values | ||
| 5305 | $rgb = $self->slice([$s], $cols); | ||
| 5306 | |||
| 5307 | # if SAMPLE_NAME is defined | ||
| 5308 | if (defined($sn)) { | ||
| 5309 | |||
| 5310 | # get color name | ||
| 5311 | $name = $self->[1][$s][$sn->[0]]; | ||
| 5312 | |||
| 5313 | # replace underscores with spaces | ||
| 5314 | $name =~ s/_/ /g; | ||
| 5315 | |||
| 5316 | } else { | ||
| 5317 | |||
| 5318 | # build color name from RGB values | ||
| 5319 | $name = sprintf('R=%d G=%d B=%d', @{$rgb->[0]}); | ||
| 5320 | |||
| 5321 | } | ||
| 5322 | |||
| 5323 | # compute string length | ||
| 5324 | $slen = length($name) + 1; | ||
| 5325 | |||
| 5326 | # compute block length | ||
| 5327 | $blen = 2 * $slen + 20; | ||
| 5328 | |||
| 5329 | # print block | ||
| 5330 | print $fh pack('nNn', 1, $blen, $slen); | ||
| 5331 | print $fh encode('UTF-16BE', $name . "\x00"); | ||
| 5332 | print $fh pack('A4', 'RGB '); | ||
| 5333 | |||
| 5334 | # for each RGB value | ||
| 5335 | for my $i (0 .. 2) { | ||
| 5336 | |||
| 5337 | # convert to floating point | ||
| 5338 | $val = pack('f', $rgb->[0][$i]/255); | ||
| 5339 | |||
| 5340 | # reverse if little-endian system | ||
| 5341 | $val = reverse($val) if ($le); | ||
| 5342 | |||
| 5343 | # print value | ||
| 5344 | print $fh $val; | ||
| 5345 | |||
| 5346 | } | ||
| 5347 | |||
| 5348 | # print color type | ||
| 5349 | print $fh pack('n', $type); | ||
| 5350 | |||
| 5351 | # if color space is L*a*b* | ||
| 5352 | } elsif ($cs eq 'LAB ') { | ||
| 5353 | |||
| 5354 | # get the L*a*b* values | ||
| 5355 | $Lab = $self->slice([$s], $cols); | ||
| 5356 | |||
| 5357 | # if SAMPLE_NAME is defined | ||
| 5358 | if (defined($sn)) { | ||
| 5359 | |||
| 5360 | # get color name | ||
| 5361 | $name = $self->[1][$s][$sn->[0]]; | ||
| 5362 | |||
| 5363 | # replace underscores with spaces | ||
| 5364 | $name =~ s/_/ /g; | ||
| 5365 | |||
| 5366 | } else { | ||
| 5367 | |||
| 5368 | # build color name from L*a*b* values | ||
| 5369 | $name = sprintf('L=%d a=%d b=%d', @{$Lab->[0]}); | ||
| 5370 | |||
| 5371 | } | ||
| 5372 | |||
| 5373 | # compute string length | ||
| 5374 | $slen = length($name) + 1; | ||
| 5375 | |||
| 5376 | # compute block length | ||
| 5377 | $blen = 2 * $slen + 20; | ||
| 5378 | |||
| 5379 | # print block | ||
| 5380 | print $fh pack('nNn', 1, $blen, $slen); | ||
| 5381 | print $fh encode('UTF-16BE', $name . "\x00"); | ||
| 5382 | print $fh pack('A4', 'LAB '); | ||
| 5383 | |||
| 5384 | # modify L* value | ||
| 5385 | $Lab->[0][0] /= 100; | ||
| 5386 | |||
| 5387 | # for each L*a*b* value | ||
| 5388 | for my $i (0 .. 2) { | ||
| 5389 | |||
| 5390 | # convert to floating point | ||
| 5391 | $val = pack('f', $Lab->[0][$i]); | ||
| 5392 | |||
| 5393 | # reverse if little-endian system | ||
| 5394 | $val = reverse($val) if ($le); | ||
| 5395 | |||
| 5396 | # print value | ||
| 5397 | print $fh $val; | ||
| 5398 | |||
| 5399 | } | ||
| 5400 | |||
| 5401 | # print color type | ||
| 5402 | print $fh pack('n', $type); | ||
| 5403 | |||
| 5404 | } | ||
| 5405 | |||
| 5406 | } | ||
| 5407 | |||
| 5408 | # close file | ||
| 5409 | close($fh); | ||
| 5410 | |||
| 5411 | } | ||
| 5412 | |||
| 5413 | # print object contents to string | ||
| 5414 | # format is an array structure | ||
| 5415 | # parameter: ([format]) | ||
| 5416 | # returns: (string) | ||
| 5417 | sub sdump { | ||
| 5418 | |||
| 5419 | # get parameters | ||
| 5420 | my ($self, $p) = @_; | ||
| 5421 | |||
| 5422 | # local variables | ||
| 5423 | my ($s, $fmt); | ||
| 5424 | |||
| 5425 | # resolve parameter to an array reference | ||
| 5426 | $p = defined($p) ? ref($p) eq 'ARRAY' ? $p : [$p] : []; | ||
| 5427 | |||
| 5428 | # get format string | ||
| 5429 | $fmt = defined($p->[0]) && ! ref($p->[0]) ? $p->[0] : 'undef'; | ||
| 5430 | |||
| 5431 | # set string to object ID | ||
| 5432 | $s = sprintf("'%s' object, (0x%x)\n", ref($self), $self); | ||
| 5433 | |||
| 5434 | # return | ||
| 5435 | return($s); | ||
| 5436 | |||
| 5437 | } | ||
| 5438 | |||
| 5439 | # get column slice from DATA_FORMAT keys | ||
| 5440 | # format_keys is a list of keys with optional context | ||
| 5441 | # column_slice is reference to an array of column indices | ||
| 5442 | # note: returns 'undef' if any column is missing | ||
| 5443 | # parameters: (format_keys) | ||
| 5444 | # returns: (column_slice) | ||
| 5445 | sub _cols { | ||
| 5446 | |||
| 5447 | # get object reference | ||
| 5448 | my $self = shift(); | ||
| 5449 | |||
| 5450 | # local variables | ||
| 5451 | my (%fmt, @cols); | ||
| 5452 | |||
| 5453 | # make lookup hash of DATA_FORMAT keys | ||
| 5454 | %fmt = map {defined($self->[1][0][$_]) ? ($self->[1][0][$_], $_) : ()} (0 .. $#{$self->[1][0]}); | ||
| 5455 | |||
| 5456 | # lookup format keys in hash | ||
| 5457 | @cols = @fmt{@_}; | ||
| 5458 | |||
| 5459 | # return column slice or undef if any columns undefined | ||
| 5460 | return((grep {! defined()} @cols) ? undef : \@cols); | ||
| 5461 | |||
| 5462 | } | ||
| 5463 | |||
| 5464 | # get spectral fields array | ||
| 5465 | # array contains column indices and wavelength | ||
| 5466 | # and is sorted by wavelength (low to high) | ||
| 5467 | # parameters: (object_reference, [context]) | ||
| 5468 | # returns: (array_reference) | ||
| 5469 | sub _spectral { | ||
| 5470 | |||
| 5471 | # get parameters | ||
| 5472 | my ($self, $context) = @_; | ||
| 5473 | |||
| 5474 | # local variables | ||
| 5475 | my (%fmt, @fields); | ||
| 5476 | |||
| 5477 | # make lookup hash (context|wavelength -or- wavelength => column) | ||
| 5478 | %fmt = map {($self->[1][0][$_] =~ m/^(.*\|)?(?:nm|SPECTRAL_NM_|SPECTRAL_NM|SPECTRAL_|NM_|R_)(\d{3})$/) ? (defined($1) ? "$1$2" : $2, $_) : ()} (0 .. $#{$self->[1][0]}); | ||
| 5479 | |||
| 5480 | # if context defined | ||
| 5481 | if (defined($context)) { | ||
| 5482 | |||
| 5483 | # make list of matching fields | ||
| 5484 | @fields = map {m/^$context\|(\d{3})$/ ? [$fmt{$_}, $1] : ()} keys(%fmt); | ||
| 5485 | |||
| 5486 | } else { | ||
| 5487 | |||
| 5488 | # make list of matching fields | ||
| 5489 | @fields = map {m/^(\d{3})$/ ? [$fmt{$_}, $1] : ()} keys(%fmt); | ||
| 5490 | |||
| 5491 | # if no matching fields | ||
| 5492 | if (@fields == 0) { | ||
| 5493 | |||
| 5494 | # make lookup hash (wavelength => column) | ||
| 5495 | %fmt = map {($self->[1][0][$_] =~ m/^(?:.*\|)?(?:nm|SPECTRAL_NM_|SPECTRAL_NM|SPECTRAL_|NM_|R_)(\d{3})$/) ? ($1, $_) : ()} (0 .. $#{$self->[1][0]}); | ||
| 5496 | |||
| 5497 | # make list of fields | ||
| 5498 | @fields = map {[$fmt{$_}, $_]} keys(%fmt); | ||
| 5499 | |||
| 5500 | } | ||
| 5501 | |||
| 5502 | } | ||
| 5503 | |||
| 5504 | # return undef if no match | ||
| 5505 | return() if (@fields == 0); | ||
| 5506 | |||
| 5507 | # sort by wavelength | ||
| 5508 | @fields = sort {$a->[1] <=> $b->[1]} @fields; | ||
| 5509 | |||
| 5510 | # return array reference | ||
| 5511 | return(\@fields); | ||
| 5512 | |||
| 5513 | } | ||
| 5514 | |||
| 5515 | # binary search | ||
| 5516 | # locates the interval containing or bounding the target value | ||
| 5517 | # returns an array of four index values, which indicate upper and lower transitions | ||
| 5518 | # parameters: (source_array, target_value, channel_index, low_index, high_index) | ||
| 5519 | # returns: (interval_index_array) | ||
| 5520 | sub _bin_search { | ||
| 5521 | |||
| 5522 | # get parameters | ||
| 5523 | my ($source, $target, $channel, $low, $high) = @_; | ||
| 5524 | |||
| 5525 | # local variables | ||
| 5526 | my ($k, $interval); | ||
| 5527 | |||
| 5528 | # copy low and high indices | ||
| 5529 | $interval->[0] = $low; | ||
| 5530 | $interval->[1] = $high; | ||
| 5531 | |||
| 5532 | # while interval is open | ||
| 5533 | while ($interval->[1] - $interval->[0] > 1) { | ||
| 5534 | |||
| 5535 | # compute the midpoint | ||
| 5536 | $k = int(($interval->[1] + $interval->[0])/2); | ||
| 5537 | |||
| 5538 | # if midpoint value >= target value | ||
| 5539 | if ($source->[$k][$channel] >= $target) { | ||
| 5540 | |||
| 5541 | # set higher index to midpoint | ||
| 5542 | $interval->[1] = $k; | ||
| 5543 | |||
| 5544 | } else { | ||
| 5545 | |||
| 5546 | # set lower index to midpoint | ||
| 5547 | $interval->[0] = $k; | ||
| 5548 | |||
| 5549 | } | ||
| 5550 | |||
| 5551 | } | ||
| 5552 | |||
| 5553 | # copy low and high indices | ||
| 5554 | $interval->[2] = $low; | ||
| 5555 | $interval->[3] = $high; | ||
| 5556 | |||
| 5557 | # while interval is open | ||
| 5558 | while ($interval->[3] - $interval->[2] > 1) { | ||
| 5559 | |||
| 5560 | # compute the midpoint | ||
| 5561 | $k = int(($interval->[3] + $interval->[2])/2); | ||
| 5562 | |||
| 5563 | # if midpoint value > target value | ||
| 5564 | if ($source->[$k][$channel] > $target) { | ||
| 5565 | |||
| 5566 | # set higher index to midpoint | ||
| 5567 | $interval->[3] = $k; | ||
| 5568 | |||
| 5569 | } else { | ||
| 5570 | |||
| 5571 | # set lower index to midpoint | ||
| 5572 | $interval->[2] = $k; | ||
| 5573 | |||
| 5574 | } | ||
| 5575 | |||
| 5576 | } | ||
| 5577 | |||
| 5578 | # return interval array | ||
| 5579 | return($interval); | ||
| 5580 | |||
| 5581 | } | ||
| 5582 | |||
| 5583 | # linear search | ||
| 5584 | # locates the closest source sample based on Manhattan distance | ||
| 5585 | # parameters: (source_array, target_vector) | ||
| 5586 | # returns: (low_index, high_index) | ||
| 5587 | sub _lin_search { | ||
| 5588 | |||
| 5589 | # get parameters | ||
| 5590 | my ($source, $target) = @_; | ||
| 5591 | |||
| 5592 | # local variables | ||
| 5593 | my ($d0, $d1, $d2, $low, $high); | ||
| 5594 | |||
| 5595 | # set initial difference | ||
| 5596 | $d0 = @{$target}; | ||
| 5597 | |||
| 5598 | # for each source sample | ||
| 5599 | for my $i (0 .. $#{$source}) { | ||
| 5600 | |||
| 5601 | # clear differences | ||
| 5602 | $d1 = $d2 = 0; | ||
| 5603 | |||
| 5604 | # for each channel | ||
| 5605 | for my $j (0 .. $#{$target}) { | ||
| 5606 | |||
| 5607 | # add difference to target sample | ||
| 5608 | $d1 += abs($source->[$i][$j] - $target->[$j]); | ||
| 5609 | |||
| 5610 | # add difference to previous sample | ||
| 5611 | $d2 += abs($source->[$i][$j] - $source->[$i - 1][$j]) if ($i > 0); | ||
| 5612 | |||
| 5613 | } | ||
| 5614 | |||
| 5615 | # if new difference less | ||
| 5616 | if ($d1 < $d0) { | ||
| 5617 | |||
| 5618 | # save index | ||
| 5619 | $low = $high = $i; | ||
| 5620 | |||
| 5621 | # update difference | ||
| 5622 | $d0 = $d1; | ||
| 5623 | |||
| 5624 | } | ||
| 5625 | |||
| 5626 | # if duplicate sample | ||
| 5627 | if ($d0 == $d1 && $d2 == 0) { | ||
| 5628 | |||
| 5629 | # save index | ||
| 5630 | $high = $i; | ||
| 5631 | |||
| 5632 | } | ||
| 5633 | |||
| 5634 | } | ||
| 5635 | |||
| 5636 | # return | ||
| 5637 | return($low, $high); | ||
| 5638 | |||
| 5639 | } | ||
| 5640 | |||
| 5641 | # add average sample | ||
| 5642 | # assumes device values (if any) are same for each sample | ||
| 5643 | # averages measurements values - spectral, XYZ, L*a*b*, or density | ||
| 5644 | # L*a*b* values are converted to xyz for averaging, then back to L*a*b* | ||
| 5645 | # density values are converted to reflectance for averaging, then back to density | ||
| 5646 | # parameters: (object_reference, row_slice, linear_slice, L*a*b*_slice, density_slice, id_slice, name_slice, hash) | ||
| 5647 | # returns: (average_sample_index) | ||
| 5648 | sub _add_avg { | ||
| 5649 | |||
| 5650 | # get parameters | ||
| 5651 | my ($self, $rows, $c1, $c2, $c3, $id, $name, $hash) = @_; | ||
| 5652 | |||
| 5653 | # local variables | ||
| 5654 | my ($n, $next, @xyz, $sid, $sn); | ||
| 5655 | |||
| 5656 | # get number of samples | ||
| 5657 | $n = @{$rows}; | ||
| 5658 | |||
| 5659 | # get index of next data row | ||
| 5660 | $next = $#{$self->[1]} + 1; | ||
| 5661 | |||
| 5662 | # copy first sample | ||
| 5663 | $self->[1][$next] = [@{$self->[1][shift(@{$rows})]}]; | ||
| 5664 | |||
| 5665 | # for each group of L*a*b* columns | ||
| 5666 | for (my $j = 0; $j < @{$c2}; $j += 3) { | ||
| 5667 | |||
| 5668 | # convert to L*a*b* values to xyz | ||
| 5669 | @{$self->[1][$next]}[@{$c2}[$j .. $j + 2]] = ICC::Shared::_Lab2xyz(@{$self->[1][$next]}[@{$c2}[$j .. $j + 2]]); | ||
| 5670 | |||
| 5671 | } | ||
| 5672 | |||
| 5673 | # for each density column | ||
| 5674 | for my $j (@{$c3}) { | ||
| 5675 | |||
| 5676 | # convert to density to reflectance | ||
| 5677 | $self->[1][$next][$j] = POSIX::pow(10, -$self->[1][$next][$j]); | ||
| 5678 | |||
| 5679 | } | ||
| 5680 | |||
| 5681 | # for remaining samples | ||
| 5682 | for my $i (@{$rows}) { | ||
| 5683 | |||
| 5684 | # for each linear column | ||
| 5685 | for my $j (@{$c1}) { | ||
| 5686 | |||
| 5687 | # add value | ||
| 5688 | $self->[1][$next][$j] += $self->[1][$i][$j]; | ||
| 5689 | |||
| 5690 | } | ||
| 5691 | |||
| 5692 | # for each group of L*a*b* columns | ||
| 5693 | for (my $j = 0; $j < @{$c2}; $j += 3) { | ||
| 5694 | |||
| 5695 | # get xyz values | ||
| 5696 | @xyz = ICC::Shared::_Lab2xyz(@{$self->[1][$i]}[@{$c2}[$j .. $j + 2]]); | ||
| 5697 | |||
| 5698 | # add to self | ||
| 5699 | $self->[1][$next][$c2->[$j]] += $xyz[0]; | ||
| 5700 | $self->[1][$next][$c2->[$j + 1]] += $xyz[1]; | ||
| 5701 | $self->[1][$next][$c2->[$j + 2]] += $xyz[2]; | ||
| 5702 | |||
| 5703 | } | ||
| 5704 | |||
| 5705 | # for each density column | ||
| 5706 | for my $j (@{$c3}) { | ||
| 5707 | |||
| 5708 | # add temp reflectance | ||
| 5709 | $self->[1][$next][$j] += POSIX::pow(10, -$self->[1][$i][$j]); | ||
| 5710 | |||
| 5711 | } | ||
| 5712 | |||
| 5713 | } | ||
| 5714 | |||
| 5715 | # for each measurement column | ||
| 5716 | for my $j (@{$c1}, @{$c2}, @{$c3}) { | ||
| 5717 | |||
| 5718 | # divide by number of samples | ||
| 5719 | $self->[1][$next][$j] /= $n; | ||
| 5720 | |||
| 5721 | } | ||
| 5722 | |||
| 5723 | # for each group of L*a*b* columns | ||
| 5724 | for (my $j = 0; $j < @{$c2}; $j += 3) { | ||
| 5725 | |||
| 5726 | # convert to xyz values to L*a*b* | ||
| 5727 | @{$self->[1][$next]}[@{$c2}[$j .. $j + 2]] = ICC::Shared::_xyz2Lab(@{$self->[1][$next]}[@{$c2}[$j .. $j + 2]]); | ||
| 5728 | |||
| 5729 | } | ||
| 5730 | |||
| 5731 | # for each density column | ||
| 5732 | for my $j (@{$c3}) { | ||
| 5733 | |||
| 5734 | # convert to reflectance to density | ||
| 5735 | $self->[1][$next][$j] = -POSIX::log10($self->[1][$next][$j]); | ||
| 5736 | |||
| 5737 | } | ||
| 5738 | |||
| 5739 | # get SAMPLE_ID value from hash | ||
| 5740 | $sid = $hash->{'id'}; | ||
| 5741 | |||
| 5742 | # for each SAMPLE_ID column | ||
| 5743 | for my $i (@{$id}) { | ||
| 5744 | |||
| 5745 | # if SAMPLE_ID defined | ||
| 5746 | if (defined($sid)) { | ||
| 5747 | |||
| 5748 | # set to hash value | ||
| 5749 | $self->[1][$next][$i] = $sid; | ||
| 5750 | |||
| 5751 | } else { | ||
| 5752 | |||
| 5753 | # set to row index | ||
| 5754 | $self->[1][$next][$i] = $next; | ||
| 5755 | |||
| 5756 | } | ||
| 5757 | |||
| 5758 | } | ||
| 5759 | |||
| 5760 | # get SAMPLE_NAME value from hash | ||
| 5761 | $sn = $hash->{'name'}; | ||
| 5762 | |||
| 5763 | # for each SAMPLE_NAME column | ||
| 5764 | for my $i (@{$name}) { | ||
| 5765 | |||
| 5766 | # if SAMPLE_NAME defined | ||
| 5767 | if (defined($sn)) { | ||
| 5768 | |||
| 5769 | # set to hash value | ||
| 5770 | $self->[1][$next][$i] = $sn; | ||
| 5771 | |||
| 5772 | } else { | ||
| 5773 | |||
| 5774 | # append '_AVG' to existing value | ||
| 5775 | $self->[1][$next][$i] .= '_AVG'; | ||
| 5776 | |||
| 5777 | } | ||
| 5778 | |||
| 5779 | } | ||
| 5780 | |||
| 5781 | # return row | ||
| 5782 | return($next); | ||
| 5783 | |||
| 5784 | } | ||
| 5785 | |||
| 5786 | # get averaging groups | ||
| 5787 | # returns column slices for each averaging method | ||
| 5788 | # parameters: (object_reference, hash) | ||
| 5789 | # returns: (linear_slice, L*a*b*_slice, density_slice) | ||
| 5790 | sub _avg_groups { | ||
| 5791 | |||
| 5792 | # get parameters | ||
| 5793 | my ($self, $hash) = @_; | ||
| 5794 | |||
| 5795 | # local variables | ||
| 5796 | my (@c1, @c2, @c3, @cs); | ||
| 5797 | |||
| 5798 | # for each format field | ||
| 5799 | for my $i (0 .. $#{$self->[1][0]}) { | ||
| 5800 | |||
| 5801 | # add column if XYZ or spectral field | ||
| 5802 | push(@c1, $i) if ($self->[1][0][$i] =~ m/^(?:.*\|)?(?:XYZ_[XYZ]|(?:nm|SPECTRAL_NM_|SPECTRAL_NM|SPECTRAL_|NM_|R_)\d{3})$/); | ||
| 5803 | |||
| 5804 | # add column if L*a*b* field | ||
| 5805 | push(@c2, $i) if ($self->[1][0][$i] =~ m/^(?:.*\|)?LAB_[LAB]$/); | ||
| 5806 | |||
| 5807 | # add column if density field | ||
| 5808 | push(@c3, $i) if ($self->[1][0][$i] =~ m/^(?:.*\|)?D_(?:RED|GREEN|BLUE|VIS)$/); | ||
| 5809 | |||
| 5810 | } | ||
| 5811 | |||
| 5812 | # linear averaging method (L*a*b* values are converted to xyz, density values are converted to reflectance) | ||
| 5813 | if (! defined($hash->{'method'}) || $hash->{'method'} eq 'LINEAR') { | ||
| 5814 | |||
| 5815 | # verify number of L*a*b* fields | ||
| 5816 | (@c2 % 3 == 0) || croak('wrong number of L*a*b* fields'); | ||
| 5817 | |||
| 5818 | # for each group of L*a*b* columns | ||
| 5819 | for (my $j = 0; $j < @c2; $j += 3) { | ||
| 5820 | |||
| 5821 | # sort by field name | ||
| 5822 | @cs = sort {$self->[1][0][$a] cmp $self->[1][0][$b]} @c2[$j .. $j + 2]; | ||
| 5823 | |||
| 5824 | # verify field consistency | ||
| 5825 | (join('', map {substr($_, -1, 1)} @{$self->[1][0]}[@cs]) eq 'ABL') || croak('L*a*b* field inconsistency'); | ||
| 5826 | |||
| 5827 | # save columns in LAB order | ||
| 5828 | @c2[$j .. $j + 2] = @cs[2, 0, 1]; | ||
| 5829 | |||
| 5830 | } | ||
| 5831 | |||
| 5832 | # if simple averaging method | ||
| 5833 | } elsif (defined($hash->{'method'}) && $hash->{'method'} eq 'SIMPLE') { | ||
| 5834 | |||
| 5835 | # copy L*a*b* and density columns to XYZ or spectral array | ||
| 5836 | push(@c1, @c2, @c3); | ||
| 5837 | |||
| 5838 | # clear L*a*b* and density arrays | ||
| 5839 | @c2 = (); | ||
| 5840 | @c3 = (); | ||
| 5841 | |||
| 5842 | } else { | ||
| 5843 | |||
| 5844 | # error | ||
| 5845 | croak('unsupported averaging method'); | ||
| 5846 | |||
| 5847 | } | ||
| 5848 | |||
| 5849 | # return slices | ||
| 5850 | return(\@c1, \@c2, \@c3); | ||
| 5851 | |||
| 5852 | } | ||
| 5853 | |||
| 5854 | # add OBA effect to XYZ array | ||
| 5855 | # parameters: (chart_object, M1_slice, M2_slice, XYZ_array, oba_factor, hash) | ||
| 5856 | sub _add_oba { | ||
| 5857 | |||
| 5858 | # get parameters | ||
| 5859 | my ($self, $spec1, $spec2, $xyz, $oba, $hash) = @_; | ||
| 5860 | |||
| 5861 | # local variables | ||
| 5862 | my ($color, $illum, @m1, @m2, $spectral, $xyzoba); | ||
| 5863 | |||
| 5864 | # save illuminant | ||
| 5865 | $illum = $hash->{'illuminant'}; | ||
| 5866 | |||
| 5867 | # if illuminant an array reference | ||
| 5868 | if (defined($hash->{'illuminant'}) && ref($hash->{'illuminant'}) eq 'ARRAY') { | ||
| 5869 | |||
| 5870 | # set illuminant to CIE D50 | ||
| 5871 | $hash->{'illuminant'} = ['CIE', 'D50']; | ||
| 5872 | |||
| 5873 | } else { | ||
| 5874 | |||
| 5875 | # set illuminant to ASTM D50 | ||
| 5876 | $hash->{'illuminant'} = 'D50'; | ||
| 5877 | |||
| 5878 | } | ||
| 5879 | |||
| 5880 | # make 'Color.pm' object (D50 illuminant) | ||
| 5881 | $color = ICC::Support::Color->new($hash); | ||
| 5882 | |||
| 5883 | # restore illuminant | ||
| 5884 | $hash->{'illuminant'} = $illum; | ||
| 5885 | |||
| 5886 | # for each sample | ||
| 5887 | for my $i (1 .. $#{$self->[1]}) { | ||
| 5888 | |||
| 5889 | # get M1 spectral values | ||
| 5890 | @m1 = @{$self->[1][$i]}[@{$spec1}]; | ||
| 5891 | |||
| 5892 | # get M2 spectral values | ||
| 5893 | @m2 = @{$self->[1][$i]}[@{$spec2}]; | ||
| 5894 | |||
| 5895 | # compute (M1 - M2) spectral values | ||
| 5896 | $spectral->[$i - 1] = [map {$m1[$_] - $m2[$_]} (0 .. $#m1)]; | ||
| 5897 | |||
| 5898 | } | ||
| 5899 | |||
| 5900 | # transform (M1 - M2) spectral to D50 XYZ (hash may contain 'encoding' key) | ||
| 5901 | $xyzoba = ICC::Support::Color::_trans2($color, $spectral, $hash); | ||
| 5902 | |||
| 5903 | # for each sample | ||
| 5904 | for my $i (0 .. $#{$xyz}) { | ||
| 5905 | |||
| 5906 | # for each XYZ | ||
| 5907 | for my $j (0 .. 2) { | ||
| 5908 | |||
| 5909 | # add scaled OBA effect | ||
| 5910 | $xyz->[$i][$j] += $xyzoba->[$i][$j] * $oba; | ||
| 5911 | |||
| 5912 | } | ||
| 5913 | |||
| 5914 | } | ||
| 5915 | |||
| 5916 | } | ||
| 5917 | |||
| 5918 | # get/set data | ||
| 5919 | # common routine called by get/set methods | ||
| 5920 | # row_slice and column_slice may be either a scalar or array reference | ||
| 5921 | # an empty array reference indicates all samples or fields | ||
| 5922 | # replacement_data is reference to a 2-D array of replacement values | ||
| 5923 | # array dimensions must match size of row_slice and column_slice | ||
| 5924 | # data_slice is Math::Matrix object, defined by row_slice and column_slice | ||
| 5925 | # get_code_ref and set_code_ref transform the data when getting and setting | ||
| 5926 | # parameters: (object_ref, object_index, row_slice, column_slice, replacement_data, get_code_ref, set_code_ref) | ||
| 5927 | # if column_slice undefined, returns: () | ||
| 5928 | # if row_slice undefined, returns: (column_slice) | ||
| 5929 | # if replacement_data undefined, returns: (data_slice) | ||
| 5930 | # otherwise, sets replacement data and returns: (column_slice) | ||
| 5931 | sub _getset { | ||
| 5932 | |||
| 5933 | # get parameters | ||
| 5934 | my ($self, $ix, $rows, $cols, $data, $get, $set) = @_; | ||
| 5935 | |||
| 5936 | # return empty if no column slice | ||
| 5937 | defined($cols) || return(); | ||
| 5938 | |||
| 5939 | # if column slice an empty array reference | ||
| 5940 | if (ref($cols) eq 'ARRAY' && @{$cols} == 0) { | ||
| 5941 | |||
| 5942 | # use all columns | ||
| 5943 | $cols = [0 .. $#{$self->[$ix][0]}]; | ||
| 5944 | |||
| 5945 | } else { | ||
| 5946 | |||
| 5947 | # flatten column slice | ||
| 5948 | $cols = ICC::Shared::flatten($cols); | ||
| 5949 | |||
| 5950 | # verify column slice contents | ||
| 5951 | (@{$cols} == grep {! ref() && $_ == int($_) && $_ >= 0} @{$cols}) || croak('invalid column slice'); | ||
| 5952 | |||
| 5953 | } | ||
| 5954 | |||
| 5955 | # return columns slice if no row slice | ||
| 5956 | defined($rows) || return($cols); | ||
| 5957 | |||
| 5958 | # if row slice an empty array reference | ||
| 5959 | if (ref($rows) eq 'ARRAY' && @{$rows} == 0) { | ||
| 5960 | |||
| 5961 | # use all rows | ||
| 5962 | $rows = [1 .. $#{$self->[$ix]}]; | ||
| 5963 | |||
| 5964 | } else { | ||
| 5965 | |||
| 5966 | # flatten row slice | ||
| 5967 | $rows = ICC::Shared::flatten($rows); | ||
| 5968 | |||
| 5969 | # verify row slice contents | ||
| 5970 | (@{$rows} == grep {! ref() && $_ == int($_) && $_ >= 0} @{$rows}) || croak('invalid row slice'); | ||
| 5971 | |||
| 5972 | } | ||
| 5973 | |||
| 5974 | # no replacement data (get) | ||
| 5975 | if (! defined($data)) { | ||
| 5976 | |||
| 5977 | # verify 'get' code ref, or use identity function | ||
| 5978 | $get = (defined($get) && ref($get) eq 'CODE') ? $get : sub {@_}; | ||
| 5979 | |||
| 5980 | # for each row | ||
| 5981 | for my $i (0 .. $#{$rows}) { | ||
| 5982 | |||
| 5983 | # get transformed data row | ||
| 5984 | @{$data->[$i]} = &$get(@{$self->[$ix][$rows->[$i]]}[@{$cols}]); | ||
| 5985 | |||
| 5986 | } | ||
| 5987 | |||
| 5988 | # return data slice as a Math::Matrix object | ||
| 5989 | return(bless($data, 'Math::Matrix')); | ||
| 5990 | |||
| 5991 | # with replacement data (set) | ||
| 5992 | } else { | ||
| 5993 | |||
| 5994 | # verify replacement data is 2-D array or Math::Matrix object | ||
| 5995 | ((ref($data) eq 'ARRAY' || UNIVERSAL::isa($data, 'Math::Matrix')) && ref($data->[0]) eq 'ARRAY') || croak('replacement data not a 2-D array reference'); | ||
| 5996 | |||
| 5997 | # verify replacement data size | ||
| 5998 | ($#{$data} == $#{$rows} && $#{$data->[0]} == $#{$cols}) || croak('replacement data is wrong sized'); | ||
| 5999 | |||
| 6000 | # verify 'set' code ref, or use identity function | ||
| 6001 | $set = (defined($set) && ref($set) eq 'CODE') ? $set : sub {@_}; | ||
| 6002 | |||
| 6003 | # for each row | ||
| 6004 | for my $i (0 .. $#{$rows}) { | ||
| 6005 | |||
| 6006 | # set transformed data row | ||
| 6007 | @{$self->[$ix][$rows->[$i]]}[@{$cols}] = &$set(@{$data->[$i]}); | ||
| 6008 | |||
| 6009 | } | ||
| 6010 | |||
| 6011 | # return column slice | ||
| 6012 | return($cols); | ||
| 6013 | |||
| 6014 | } | ||
| 6015 | |||
| 6016 | } | ||
| 6017 | |||
| 6018 | # get accumulated sample values | ||
| 6019 | # sample dimensions are in pixels | ||
| 6020 | # used by _readChartTIFF to extract samples from a data stripe | ||
| 6021 | # parameters: (reference_to_data, sample_offset, sample_width, number_channels) | ||
| 6022 | # returns: (accumulated_sample_values) | ||
| 6023 | sub _getSample { | ||
| 6024 | |||
| 6025 | # get parameters | ||
| 6026 | my ($data, $so, $sx, $c) = @_; | ||
| 6027 | |||
| 6028 | # initialize sample values | ||
| 6029 | my @sv = (0) x $c; | ||
| 6030 | |||
| 6031 | # for each row | ||
| 6032 | for my $i (0 .. $#{$data}) { | ||
| 6033 | |||
| 6034 | # for each pixel | ||
| 6035 | for my $j (0 .. $sx - 1) { | ||
| 6036 | |||
| 6037 | # for each channel | ||
| 6038 | for my $k (0 .. $c - 1) { | ||
| 6039 | |||
| 6040 | # accumulate sample value | ||
| 6041 | $sv[$k] += $data->[$i][($so + $j) * $c + $k]; | ||
| 6042 | |||
| 6043 | } | ||
| 6044 | |||
| 6045 | } | ||
| 6046 | |||
| 6047 | } | ||
| 6048 | |||
| 6049 | # return sample values | ||
| 6050 | return(@sv); | ||
| 6051 | |||
| 6052 | } | ||
| 6053 | |||
| 6054 | # get row length | ||
| 6055 | # parameters: (object_reference, hash) | ||
| 6056 | # returns: (row_length) | ||
| 6057 | sub _getRowLength { | ||
| 6058 | |||
| 6059 | # get parameters | ||
| 6060 | my ($self, $hash) = @_; | ||
| 6061 | |||
| 6062 | # local variables | ||
| 6063 | my ($rows, $n, $square); | ||
| 6064 | |||
| 6065 | # if 'rows' hash key is defined | ||
| 6066 | if (defined($hash->{'rows'})) { | ||
| 6067 | |||
| 6068 | # get row length value | ||
| 6069 | $rows = $hash->{'rows'}; | ||
| 6070 | |||
| 6071 | # if valid row length | ||
| 6072 | if (Scalar::Util::looks_like_number($rows) && $rows > 0 && $rows == int($rows)) { | ||
| 6073 | |||
| 6074 | # return | ||
| 6075 | return($rows); | ||
| 6076 | |||
| 6077 | } else { | ||
| 6078 | |||
| 6079 | # warn | ||
| 6080 | warn('invalid \'rows\' parameter'); | ||
| 6081 | |||
| 6082 | } | ||
| 6083 | |||
| 6084 | } | ||
| 6085 | |||
| 6086 | # if LGOROWLENGTH keyword | ||
| 6087 | if ($rows = keyword($self, 'LGOROWLENGTH')) { | ||
| 6088 | |||
| 6089 | # if valid row length | ||
| 6090 | if (Scalar::Util::looks_like_number($rows) && $rows > 0 && $rows == int($rows)) { | ||
| 6091 | |||
| 6092 | # return | ||
| 6093 | return($rows); | ||
| 6094 | |||
| 6095 | } else { | ||
| 6096 | |||
| 6097 | # warn | ||
| 6098 | warn('invalid \'LGOROWLENGTH\' value'); | ||
| 6099 | |||
| 6100 | } | ||
| 6101 | |||
| 6102 | } | ||
| 6103 | |||
| 6104 | # if NUMBER_OF_STRIPS keyword | ||
| 6105 | if ($rows = keyword($self, 'NUMBER_OF_STRIPS')) { | ||
| 6106 | |||
| 6107 | # if valid row length | ||
| 6108 | if (Scalar::Util::looks_like_number($rows) && $rows > 0 && $rows == int($rows)) { | ||
| 6109 | |||
| 6110 | # return | ||
| 6111 | return($rows); | ||
| 6112 | |||
| 6113 | } else { | ||
| 6114 | |||
| 6115 | # warn | ||
| 6116 | warn('invalid \'NUMBER_OF_STRIPS\' value'); | ||
| 6117 | |||
| 6118 | } | ||
| 6119 | |||
| 6120 | } | ||
| 6121 | |||
| 6122 | # if 'NumberPatchRows' key is defined | ||
| 6123 | if (defined($self->[0]{'xrp:CustomAttributes'}{'NumberPatchRows'})) { | ||
| 6124 | |||
| 6125 | # get row length value | ||
| 6126 | $rows = $self->[0]{'xrp:CustomAttributes'}{'NumberPatchRows'}; | ||
| 6127 | |||
| 6128 | # if valid row length | ||
| 6129 | if (Scalar::Util::looks_like_number($rows) && $rows > 0 && $rows == int($rows)) { | ||
| 6130 | |||
| 6131 | # return | ||
| 6132 | return($rows); | ||
| 6133 | |||
| 6134 | } else { | ||
| 6135 | |||
| 6136 | # warn | ||
| 6137 | warn('invalid \'NumberPatchRows\' attribute'); | ||
| 6138 | |||
| 6139 | } | ||
| 6140 | |||
| 6141 | } | ||
| 6142 | |||
| 6143 | # get number of samples | ||
| 6144 | $n = $#{$self->[1]}; | ||
| 6145 | |||
| 6146 | # return if 0 | ||
| 6147 | return(0) if ($n == 0); | ||
| 6148 | |||
| 6149 | # return if 1 or 2 | ||
| 6150 | return(1) if ($n < 3); | ||
| 6151 | |||
| 6152 | # compute size of square chart | ||
| 6153 | $square = POSIX::ceil(sqrt($n)); | ||
| 6154 | |||
| 6155 | # return if chart is square | ||
| 6156 | return($square) if ($n == $square**2); | ||
| 6157 | |||
| 6158 | # set row length one less than square chart | ||
| 6159 | $rows = $square - 1; | ||
| 6160 | |||
| 6161 | # while modulus is non-zero, decrement row length | ||
| 6162 | while ($n % $rows) {$rows--} | ||
| 6163 | |||
| 6164 | # return row length, choosing full rectangle if possible | ||
| 6165 | return($rows > $square/2 ? $rows : $square); | ||
| 6166 | |||
| 6167 | } | ||
| 6168 | |||
| 6169 | # get illuminant white point | ||
| 6170 | # returns XYZ vector from colorimetry array | ||
| 6171 | # returns D50 if CAT or undefined | ||
| 6172 | # parameter: (object_reference, column_slice, [hash]) | ||
| 6173 | # returns: (XYZ_vector) | ||
| 6174 | sub _illumWP { | ||
| 6175 | |||
| 6176 | # get parameters | ||
| 6177 | my ($self, $cols, $hash) = @_; | ||
| 6178 | |||
| 6179 | # if XYZ values are valid | ||
| 6180 | if (3 == grep {defined() && ! ref() && $_ > 0} @{$self->[2][2]}[@{$cols}]) { | ||
| 6181 | |||
| 6182 | # return XYZ vector | ||
| 6183 | return([@{$self->[2][2]}[@{$cols}]]); | ||
| 6184 | |||
| 6185 | } else { | ||
| 6186 | |||
| 6187 | # return D50 vector | ||
| 6188 | return(ICC::Shared::D50); | ||
| 6189 | |||
| 6190 | } | ||
| 6191 | |||
| 6192 | } | ||
| 6193 | |||
| 6194 | # compute media white point | ||
| 6195 | # multiple samples are averaged | ||
| 6196 | # result also stored in colorimetry array | ||
| 6197 | # parameter: (object_reference, column_slice, [hash]) | ||
| 6198 | # returns: (XYZ_vector) | ||
| 6199 | sub _mediaWP { | ||
| 6200 | |||
| 6201 | # get parameters | ||
| 6202 | my ($self, $cols, $hash) = @_; | ||
| 6203 | |||
| 6204 | # local variables | ||
| 6205 | my ($WPxyz, $dev, $mwv, $n, @XYZ, @XYZs); | ||
| 6206 | |||
| 6207 | # if column slice is L*a*b* | ||
| 6208 | if ((3 == grep {$self->[1][0][$_] =~ m/LAB_[LAB]$/} @{$cols})) { | ||
| 6209 | |||
| 6210 | # get illuminant white point | ||
| 6211 | $WPxyz = defined($self->[2][2][$cols->[0]]) ? [@{$self->[2][2]}[@{$cols}]] : ICC::Shared::D50; | ||
| 6212 | |||
| 6213 | # if column slice is not XYZ | ||
| 6214 | } elsif ((3 != grep {$self->[1][0][$_] =~ m/XYZ_[XYZ]$/} @{$cols})) { | ||
| 6215 | |||
| 6216 | # warning | ||
| 6217 | warn('column slice not XYZ or L*a*b* data'); | ||
| 6218 | |||
| 6219 | # return empty | ||
| 6220 | return(); | ||
| 6221 | |||
| 6222 | } | ||
| 6223 | |||
| 6224 | # if no device data (using 'device' context) | ||
| 6225 | if (! ($dev = device($self, {'context' => $hash->{'device'}}))) { | ||
| 6226 | |||
| 6227 | # warning | ||
| 6228 | warn('no device data'); | ||
| 6229 | |||
| 6230 | # return empty | ||
| 6231 | return(); | ||
| 6232 | |||
| 6233 | } | ||
| 6234 | |||
| 6235 | # set media white device value (255 if RGB, 0 otherwise) | ||
| 6236 | $mwv = ($self->[1][0][$dev->[0]] =~ m/RGB_R$/) ? 255 : 0; | ||
| 6237 | |||
| 6238 | # for each sample | ||
| 6239 | for my $i (1 .. $#{$self->[1]}) { | ||
| 6240 | |||
| 6241 | # if all device channels are white | ||
| 6242 | if (@{$dev} == grep {$_ == $mwv} @{$self->[1][$i]}[@{$dev}]) { | ||
| 6243 | |||
| 6244 | # if L*a*b* data | ||
| 6245 | if ($WPxyz) { | ||
| 6246 | |||
| 6247 | # convert L*a*b* values to XYZ | ||
| 6248 | @XYZs = ICC::Shared::_Lab2XYZ(@{$self->[1][$i]}[@{$cols}], $WPxyz); | ||
| 6249 | |||
| 6250 | # accumulate XYZ values | ||
| 6251 | $XYZ[0] += $XYZs[0]; | ||
| 6252 | $XYZ[1] += $XYZs[1]; | ||
| 6253 | $XYZ[2] += $XYZs[2]; | ||
| 6254 | |||
| 6255 | # if XYZ data | ||
| 6256 | } else { | ||
| 6257 | |||
| 6258 | # accumulate XYZ values | ||
| 6259 | $XYZ[0] += $self->[1][$i][$cols->[0]]; | ||
| 6260 | $XYZ[1] += $self->[1][$i][$cols->[1]]; | ||
| 6261 | $XYZ[2] += $self->[1][$i][$cols->[2]]; | ||
| 6262 | |||
| 6263 | } | ||
| 6264 | |||
| 6265 | # increment count | ||
| 6266 | $n++; | ||
| 6267 | |||
| 6268 | } | ||
| 6269 | |||
| 6270 | } | ||
| 6271 | |||
| 6272 | # if media white sample(s) | ||
| 6273 | if ($n) { | ||
| 6274 | |||
| 6275 | # store average XYZ values in colorimetry array, and return XYZ vector | ||
| 6276 | return([@{$self->[2][3]}[@{$cols}] = map {$_/$n} @XYZ]); | ||
| 6277 | |||
| 6278 | } else { | ||
| 6279 | |||
| 6280 | # warning | ||
| 6281 | warn('no media white sample found'); | ||
| 6282 | |||
| 6283 | # return empty | ||
| 6284 | return(); | ||
| 6285 | |||
| 6286 | } | ||
| 6287 | |||
| 6288 | } | ||
| 6289 | |||
| 6290 | # compute media black point | ||
| 6291 | # multiple samples are averaged | ||
| 6292 | # result also stored in colorimetry array | ||
| 6293 | # parameter: (object_reference, column_slice, [hash]) | ||
| 6294 | # returns: (XYZ_vector) | ||
| 6295 | sub _mediaBP { | ||
| 6296 | |||
| 6297 | # get parameters | ||
| 6298 | my ($self, $cols, $hash) = @_; | ||
| 6299 | |||
| 6300 | # local variables | ||
| 6301 | my ($WPxyz, $dev, $mbv, $n, @XYZ, @XYZs); | ||
| 6302 | |||
| 6303 | # if column slice is L*a*b* | ||
| 6304 | if ((3 == grep {$self->[1][0][$_] =~ m/LAB_[LAB]$/} @{$cols})) { | ||
| 6305 | |||
| 6306 | # get illuminant white point | ||
| 6307 | $WPxyz = defined($self->[2][2][$cols->[0]]) ? [@{$self->[2][2]}[@{$cols}]] : ICC::Shared::D50; | ||
| 6308 | |||
| 6309 | # if column slice is not XYZ | ||
| 6310 | } elsif ((3 != grep {$self->[1][0][$_] =~ m/XYZ_[XYZ]$/} @{$cols})) { | ||
| 6311 | |||
| 6312 | # warning | ||
| 6313 | warn('column slice not XYZ or L*a*b* data'); | ||
| 6314 | |||
| 6315 | # return empty | ||
| 6316 | return(); | ||
| 6317 | |||
| 6318 | } | ||
| 6319 | |||
| 6320 | # if no device data (using 'device' context) | ||
| 6321 | if (! ($dev = device($self, {'context' => $hash->{'device'}}))) { | ||
| 6322 | |||
| 6323 | # warning | ||
| 6324 | warn('no device data'); | ||
| 6325 | |||
| 6326 | # return empty | ||
| 6327 | return(); | ||
| 6328 | |||
| 6329 | } | ||
| 6330 | |||
| 6331 | # set media black device value (0 if RGB, 100 otherwise) | ||
| 6332 | $mbv = ($self->[1][0][$dev->[0]] =~ m/RGB_R$/) ? 1 : 100; | ||
| 6333 | |||
| 6334 | # for each sample | ||
| 6335 | for my $i (1 .. $#{$self->[1]}) { | ||
| 6336 | |||
| 6337 | # if all device channels are black | ||
| 6338 | if (@{$dev} == grep {$_ == $mbv} @{$self->[1][$i]}[@{$dev}]) { | ||
| 6339 | |||
| 6340 | # increment count | ||
| 6341 | $n++; | ||
| 6342 | |||
| 6343 | # if L*a*b* data | ||
| 6344 | if ($WPxyz) { | ||
| 6345 | |||
| 6346 | # convert L*a*b* values to XYZ | ||
| 6347 | @XYZs = ICC::Shared::_Lab2XYZ(@{$self->[1][$i]}[@{$cols}], $WPxyz); | ||
| 6348 | |||
| 6349 | # accumulate XYZ values | ||
| 6350 | $XYZ[0] += $XYZs[0]; | ||
| 6351 | $XYZ[1] += $XYZs[1]; | ||
| 6352 | $XYZ[2] += $XYZs[2]; | ||
| 6353 | |||
| 6354 | # if XYZ data | ||
| 6355 | } else { | ||
| 6356 | |||
| 6357 | # accumulate XYZ values | ||
| 6358 | $XYZ[0] += $self->[1][$i][$cols->[0]]; | ||
| 6359 | $XYZ[1] += $self->[1][$i][$cols->[1]]; | ||
| 6360 | $XYZ[2] += $self->[1][$i][$cols->[2]]; | ||
| 6361 | |||
| 6362 | } | ||
| 6363 | |||
| 6364 | } | ||
| 6365 | |||
| 6366 | } | ||
| 6367 | |||
| 6368 | # if media black sample(s) | ||
| 6369 | if ($n) { | ||
| 6370 | |||
| 6371 | # store average XYZ values in colorimetry array, and return XYZ vector | ||
| 6372 | return([@{$self->[2][4]}[@{$cols}] = map {$_/$n} @XYZ]); | ||
| 6373 | |||
| 6374 | } else { | ||
| 6375 | |||
| 6376 | # warning | ||
| 6377 | warn('no media black sample found'); | ||
| 6378 | |||
| 6379 | # return empty | ||
| 6380 | return(); | ||
| 6381 | |||
| 6382 | } | ||
| 6383 | |||
| 6384 | } | ||
| 6385 | |||
| 6386 | # make SAMPLE_ID hash | ||
| 6387 | # if no SAMPLE_ID field, hash is initialized | ||
| 6388 | # parameter: (object_reference) | ||
| 6389 | sub _makeSampleID { | ||
| 6390 | |||
| 6391 | # get object reference | ||
| 6392 | my $self = shift(); | ||
| 6393 | |||
| 6394 | # if SAMPLE_ID column(s) exist | ||
| 6395 | if (my @id = grep {$self->[1][0][$_] =~ m/^(?:.*\|)?(?:SAMPLE_ID|SampleID)$/} (0 .. $#{$self->[1][0]})) { | ||
| 6396 | |||
| 6397 | # make the SAMPLE_ID hash, omitting undefined ID values | ||
| 6398 | $self->[4] = {map {defined($self->[1][$_][$id[0]]) ? ($self->[1][$_][$id[0]], $_) : ()} (1 .. $#{$self->[1]})}; | ||
| 6399 | |||
| 6400 | } else { | ||
| 6401 | |||
| 6402 | # initialize the hash | ||
| 6403 | $self->[4] = {}; | ||
| 6404 | |||
| 6405 | } | ||
| 6406 | |||
| 6407 | } | ||
| 6408 | |||
| 6409 | # add colorimetry metadata | ||
| 6410 | # called when creating a new object | ||
| 6411 | # parameter: (object_reference) | ||
| 6412 | sub _addColorMeta { | ||
| 6413 | |||
| 6414 | # get object reference | ||
| 6415 | my $self = shift(); | ||
| 6416 | |||
| 6417 | # local variables | ||
| 6418 | my (@cols, $hash, $illum, $spec, $nm, $str, $color, $WPxyz, @values); | ||
| 6419 | |||
| 6420 | # if object contains colorimetric data | ||
| 6421 | if (@cols = grep {$self->[1][0][$_] =~ m/^(?:(.*)\|)?(?:LAB_[LAB]|XYZ_[XYZ]|STDEV_[LABXYZ]|MEAN_DE|STDEV_DE|CHI_SQD_PAR)$/} (0 .. $#{$self->[1][0]})) { | ||
| 6422 | |||
| 6423 | # set default hash values | ||
| 6424 | $hash = {'illuminant' => 'D50', 'observer' => '2'}; | ||
| 6425 | |||
| 6426 | # if CxF3 'TristimulusSpec' node | ||
| 6427 | if (defined($self->[0]{'CxF3_dom'}) && 0) { | ||
| 6428 | |||
| 6429 | ##### to be implemented ##### | ||
| 6430 | |||
| 6431 | # if 'WEIGHTING_FUNCTION' keyword(s) | ||
| 6432 | } elsif (@values = keyword($self, 'WEIGHTING_FUNCTION')) { | ||
| 6433 | |||
| 6434 | # join values into string | ||
| 6435 | $str = join(';', @values); | ||
| 6436 | |||
| 6437 | # match illuminant and save in hash | ||
| 6438 | $hash->{'illuminant'} = $1 if ($str =~ m/ILLUMINANT\s*,\s*(\w+)"/); | ||
| 6439 | |||
| 6440 | # match observer and save in hash | ||
| 6441 | $hash->{'observer'} = $1 if ($str =~ m/OBSERVER\s*,\s*(\d+).*"/); | ||
| 6442 | |||
| 6443 | } | ||
| 6444 | |||
| 6445 | # if non-standard illuminant | ||
| 6446 | if ($hash->{'illuminant'} ne 'D50' || $hash->{'observer'} ne '2') { | ||
| 6447 | |||
| 6448 | # make an empty 'Color.pm' object | ||
| 6449 | $color = ICC::Support::Color->new(); | ||
| 6450 | |||
| 6451 | # if illuminant is an ARRAY reference | ||
| 6452 | if (ref($hash->{'illuminant'}) eq 'ARRAY') { | ||
| 6453 | |||
| 6454 | # initialize object for CIE method | ||
| 6455 | ICC::Support::Color::_cie($color, $hash); | ||
| 6456 | |||
| 6457 | } else { | ||
| 6458 | |||
| 6459 | # initialize object for ASTM method | ||
| 6460 | ICC::Support::Color::_astm($color, $hash); | ||
| 6461 | |||
| 6462 | } | ||
| 6463 | |||
| 6464 | # use computed white point | ||
| 6465 | $WPxyz = $color->iwtpt(); | ||
| 6466 | |||
| 6467 | } else { | ||
| 6468 | |||
| 6469 | # use D50 | ||
| 6470 | $WPxyz = ICC::Shared::D50; | ||
| 6471 | |||
| 6472 | } | ||
| 6473 | |||
| 6474 | # for each colorimetric field | ||
| 6475 | for my $i (@cols) { | ||
| 6476 | |||
| 6477 | # if field name ends in L or X | ||
| 6478 | if ($self->[1][0][$i] =~ m/[LX]$/) { | ||
| 6479 | |||
| 6480 | # save WP X-value | ||
| 6481 | $self->[2][2][$i] = $WPxyz->[0]; | ||
| 6482 | |||
| 6483 | # if field name ends in A or Y | ||
| 6484 | } elsif ($self->[1][0][$i] =~ m/[AY]$/) { | ||
| 6485 | |||
| 6486 | # save WP Y-value | ||
| 6487 | $self->[2][2][$i] = $WPxyz->[1]; | ||
| 6488 | |||
| 6489 | # if field name ends in B or Z | ||
| 6490 | } elsif ($self->[1][0][$i] =~ m/[BZ]$/) { | ||
| 6491 | |||
| 6492 | # save WP Z-value | ||
| 6493 | $self->[2][2][$i] = $WPxyz->[2]; | ||
| 6494 | |||
| 6495 | } | ||
| 6496 | |||
| 6497 | } | ||
| 6498 | |||
| 6499 | } | ||
| 6500 | |||
| 6501 | } | ||
| 6502 | |||
| 6503 | # read chart from list of data files | ||
| 6504 | # averages color measurement data (spectral, XYZ, L*a*b* or density) | ||
| 6505 | # files must have identical structure (rows and cols) | ||
| 6506 | # parameters: (object_reference, ref_to_file_list, hash) | ||
| 6507 | # returns: (number_of_files_averaged) | ||
| 6508 | sub _readChartAvg { | ||
| 6509 | |||
| 6510 | # get parameters | ||
| 6511 | my ($self, $list, $hash) = @_; | ||
| 6512 | |||
| 6513 | # local variables | ||
| 6514 | my ($n, $result, $c1, $c2, $c3, $keys, $temp, @xyz); | ||
| 6515 | my ($charts, $fstat, @ctx1, @ctx2, $add_hash); | ||
| 6516 | |||
| 6517 | # initialize file count | ||
| 6518 | $n = 0; | ||
| 6519 | |||
| 6520 | # if hash is defined | ||
| 6521 | if (defined($hash)) { | ||
| 6522 | |||
| 6523 | # for each hash key | ||
| 6524 | for (keys(%{$hash})) { | ||
| 6525 | |||
| 6526 | # if XYZ based stat requested | ||
| 6527 | if (m/^STDEV_XYZ$/) { | ||
| 6528 | |||
| 6529 | # if value is a scalar | ||
| 6530 | if (! ref($hash->{$_})) { | ||
| 6531 | |||
| 6532 | # save XYZ context | ||
| 6533 | push(@ctx1, $hash->{$_}); | ||
| 6534 | |||
| 6535 | } elsif (ref($hash->{$_}) eq 'ARRAY') { | ||
| 6536 | |||
| 6537 | # save XYZ contexts | ||
| 6538 | push(@ctx1, @{$hash->{$_}}); | ||
| 6539 | |||
| 6540 | } | ||
| 6541 | |||
| 6542 | # increment flag | ||
| 6543 | $fstat++; | ||
| 6544 | |||
| 6545 | # if L*a*b* based stat requested | ||
| 6546 | } elsif (m/^(MEAN_DE|STDEV_LAB|CHI_SQD_PAR)$/) { | ||
| 6547 | |||
| 6548 | # if value is a scalar | ||
| 6549 | if (! ref($hash->{$_})) { | ||
| 6550 | |||
| 6551 | # save L*a*b* context | ||
| 6552 | push(@ctx2, $hash->{$_}); | ||
| 6553 | |||
| 6554 | } elsif (ref($hash->{$_}) eq 'ARRAY') { | ||
| 6555 | |||
| 6556 | # save L*a*b* contexts | ||
| 6557 | push(@ctx2, @{$hash->{$_}}); | ||
| 6558 | |||
| 6559 | } | ||
| 6560 | |||
| 6561 | # increment flag | ||
| 6562 | $fstat++; | ||
| 6563 | |||
| 6564 | } | ||
| 6565 | |||
| 6566 | } | ||
| 6567 | |||
| 6568 | } | ||
| 6569 | |||
| 6570 | # for each file | ||
| 6571 | for my $file (@{$list}) { | ||
| 6572 | |||
| 6573 | # if first file | ||
| 6574 | if ($n == 0) { | ||
| 6575 | |||
| 6576 | # if file read successfully | ||
| 6577 | if (! ($result = _readChart($self, $file, $hash))) { | ||
| 6578 | |||
| 6579 | # add colorimetric metadata | ||
| 6580 | _addColorMeta($self); | ||
| 6581 | |||
| 6582 | # make format key string | ||
| 6583 | $keys = join(':', map {defined() ? $_ : '-'} @{$self->[1][0]}); | ||
| 6584 | |||
| 6585 | # for each XYZ context | ||
| 6586 | for my $ctx (@ctx1) { | ||
| 6587 | |||
| 6588 | # copy the hash | ||
| 6589 | $add_hash = Storable::dclone($hash); | ||
| 6590 | |||
| 6591 | # set the context (undef for no context) | ||
| 6592 | $add_hash->{'context'} = defined($ctx) && length($ctx) ? $ctx : undef; | ||
| 6593 | |||
| 6594 | # delete the 'added' context | ||
| 6595 | delete($add_hash->{'added'}); | ||
| 6596 | |||
| 6597 | # add the XYZ values | ||
| 6598 | add_xyz($self, $add_hash); | ||
| 6599 | |||
| 6600 | } | ||
| 6601 | |||
| 6602 | # for each L*a*b* context | ||
| 6603 | for my $ctx (@ctx2) { | ||
| 6604 | |||
| 6605 | # copy the hash | ||
| 6606 | $add_hash = Storable::dclone($hash); | ||
| 6607 | |||
| 6608 | # set the context (undef for no context) | ||
| 6609 | $add_hash->{'context'} = defined($ctx) && length($ctx) ? $ctx : undef; | ||
| 6610 | |||
| 6611 | # delete the 'added' context | ||
| 6612 | delete($add_hash->{'added'}); | ||
| 6613 | |||
| 6614 | # add the L*a*b* values | ||
| 6615 | add_lab($self, $add_hash); | ||
| 6616 | |||
| 6617 | } | ||
| 6618 | |||
| 6619 | # save copy of chart data, if needed for stats | ||
| 6620 | $charts->[0] = Storable::dclone($self->[1]) if ($fstat); | ||
| 6621 | |||
| 6622 | # get averaging groups | ||
| 6623 | ($c1, $c2, $c3) = _avg_groups($self, $hash); | ||
| 6624 | |||
| 6625 | # if there are L*a*b* or density groups | ||
| 6626 | if (@{$c2} || @{$c3}) { | ||
| 6627 | |||
| 6628 | # for each sample | ||
| 6629 | for my $i (1 .. $#{$self->[1]}) { | ||
| 6630 | |||
| 6631 | # for each group of L*a*b* columns | ||
| 6632 | for (my $j = 0; $j < @{$c2}; $j += 3) { | ||
| 6633 | |||
| 6634 | # convert to L*a*b* values to xyz | ||
| 6635 | @{$self->[1][$i]}[@{$c2}[$j .. $j + 2]] = ICC::Shared::_Lab2xyz(@{$self->[1][$i]}[@{$c2}[$j .. $j + 2]]); | ||
| 6636 | |||
| 6637 | } | ||
| 6638 | |||
| 6639 | # for each density column | ||
| 6640 | for my $j (@{$c3}) { | ||
| 6641 | |||
| 6642 | # convert to density to reflectance | ||
| 6643 | $self->[1][$i][$j] = POSIX::pow(10, -$self->[1][$i][$j]); | ||
| 6644 | |||
| 6645 | } | ||
| 6646 | |||
| 6647 | } | ||
| 6648 | |||
| 6649 | } | ||
| 6650 | |||
| 6651 | # increment file count | ||
| 6652 | $n++; | ||
| 6653 | |||
| 6654 | } else { | ||
| 6655 | |||
| 6656 | # print warning | ||
| 6657 | warn("chart $file $result, ignored\n"); | ||
| 6658 | |||
| 6659 | } | ||
| 6660 | |||
| 6661 | } else { | ||
| 6662 | |||
| 6663 | # make temporary Chart object | ||
| 6664 | $temp = ICC::Support::Chart->new(); | ||
| 6665 | |||
| 6666 | # if file read successfully | ||
| 6667 | if (! ($result = _readChart($temp, $file, $hash))) { | ||
| 6668 | |||
| 6669 | # if charts have same structure (rows and cols) | ||
| 6670 | if ($#{$self->[1]} == $#{$temp->[1]} && $keys eq join(':', map {defined() ? $_ : '-'} @{$temp->[1][0]})) { | ||
| 6671 | |||
| 6672 | # for each XYZ context | ||
| 6673 | for my $ctx (@ctx1) { | ||
| 6674 | |||
| 6675 | # copy the hash | ||
| 6676 | $add_hash = Storable::dclone($hash); | ||
| 6677 | |||
| 6678 | # set the context (undef for no context) | ||
| 6679 | $add_hash->{'context'} = defined($ctx) && length($ctx) ? $ctx : undef; | ||
| 6680 | |||
| 6681 | # delete the 'added' context | ||
| 6682 | delete($add_hash->{'added'}); | ||
| 6683 | |||
| 6684 | # add the XYZ values | ||
| 6685 | add_xyz($temp, $add_hash); | ||
| 6686 | |||
| 6687 | } | ||
| 6688 | |||
| 6689 | # for each L*a*b* context | ||
| 6690 | for my $ctx (@ctx2) { | ||
| 6691 | |||
| 6692 | # copy the hash | ||
| 6693 | $add_hash = Storable::dclone($hash); | ||
| 6694 | |||
| 6695 | # set the context (undef for no context) | ||
| 6696 | $add_hash->{'context'} = defined($ctx) && length($ctx) ? $ctx : undef; | ||
| 6697 | |||
| 6698 | # delete the 'added' context | ||
| 6699 | delete($add_hash->{'added'}); | ||
| 6700 | |||
| 6701 | # add the L*a*b* values | ||
| 6702 | add_lab($temp, $add_hash); | ||
| 6703 | |||
| 6704 | } | ||
| 6705 | |||
| 6706 | # save copy of chart data, if needed for stats | ||
| 6707 | $charts->[$n] = $temp->[1] if ($fstat); | ||
| 6708 | |||
| 6709 | # for each sample | ||
| 6710 | for my $i (1 .. $#{$self->[1]}) { | ||
| 6711 | |||
| 6712 | # for each linear column | ||
| 6713 | for my $j (@{$c1}) { | ||
| 6714 | |||
| 6715 | # add temp value | ||
| 6716 | $self->[1][$i][$j] += $temp->[1][$i][$j]; | ||
| 6717 | |||
| 6718 | } | ||
| 6719 | |||
| 6720 | # for each group of L*a*b* columns | ||
| 6721 | for (my $j = 0; $j < @{$c2}; $j += 3) { | ||
| 6722 | |||
| 6723 | # get temp xyz values | ||
| 6724 | @xyz = ICC::Shared::_Lab2xyz(@{$temp->[1][$i]}[@{$c2}[$j .. $j + 2]]); | ||
| 6725 | |||
| 6726 | # add to self | ||
| 6727 | $self->[1][$i][$c2->[$j]] += $xyz[0]; | ||
| 6728 | $self->[1][$i][$c2->[$j + 1]] += $xyz[1]; | ||
| 6729 | $self->[1][$i][$c2->[$j + 2]] += $xyz[2]; | ||
| 6730 | |||
| 6731 | } | ||
| 6732 | |||
| 6733 | # for each density column | ||
| 6734 | for my $j (@{$c3}) { | ||
| 6735 | |||
| 6736 | # add temp reflectance | ||
| 6737 | $self->[1][$i][$j] += POSIX::pow(10, -$temp->[1][$i][$j]); | ||
| 6738 | |||
| 6739 | } | ||
| 6740 | |||
| 6741 | } | ||
| 6742 | |||
| 6743 | # increment file count | ||
| 6744 | $n++; | ||
| 6745 | |||
| 6746 | } else { | ||
| 6747 | |||
| 6748 | # print warning | ||
| 6749 | warn("chart $file has different structure, ignored\n"); | ||
| 6750 | |||
| 6751 | } | ||
| 6752 | |||
| 6753 | } else { | ||
| 6754 | |||
| 6755 | # print warning | ||
| 6756 | warn("chart $file $result, ignored\n"); | ||
| 6757 | |||
| 6758 | } | ||
| 6759 | |||
| 6760 | } | ||
| 6761 | |||
| 6762 | } | ||
| 6763 | |||
| 6764 | # if any files were read | ||
| 6765 | if ($n) { | ||
| 6766 | |||
| 6767 | # if there are measurement values | ||
| 6768 | if (@{$c1} || @{$c2} || @{$c3}) { | ||
| 6769 | |||
| 6770 | # for each sample | ||
| 6771 | for my $i (1 .. $#{$self->[1]}) { | ||
| 6772 | |||
| 6773 | # for each measurement column | ||
| 6774 | for my $j (@{$c1}, @{$c2}, @{$c3}) { | ||
| 6775 | |||
| 6776 | # divide by n | ||
| 6777 | $self->[1][$i][$j] /= $n; | ||
| 6778 | |||
| 6779 | } | ||
| 6780 | |||
| 6781 | # for each group of L*a*b* columns | ||
| 6782 | for (my $j = 0; $j < @{$c2}; $j += 3) { | ||
| 6783 | |||
| 6784 | # convert to xyz values to L*a*b* | ||
| 6785 | @{$self->[1][$i]}[@{$c2}[$j .. $j + 2]] = ICC::Shared::_xyz2Lab(@{$self->[1][$i]}[@{$c2}[$j .. $j + 2]]); | ||
| 6786 | |||
| 6787 | } | ||
| 6788 | |||
| 6789 | # for each density column | ||
| 6790 | for my $j (@{$c3}) { | ||
| 6791 | |||
| 6792 | # convert reflectance to density | ||
| 6793 | $self->[1][$i][$j] = -POSIX::log10($self->[1][$i][$j]); | ||
| 6794 | |||
| 6795 | } | ||
| 6796 | |||
| 6797 | } | ||
| 6798 | |||
| 6799 | } | ||
| 6800 | |||
| 6801 | # add ISO statistics, if requested | ||
| 6802 | _addStats($self, $charts, $hash) if ($fstat); | ||
| 6803 | |||
| 6804 | # print message | ||
| 6805 | print "$n files read in directory $self->[0]{'file_path'}\n\n"; | ||
| 6806 | |||
| 6807 | # save number of files read | ||
| 6808 | $self->[0]{'files_read'} = $n; | ||
| 6809 | |||
| 6810 | } | ||
| 6811 | |||
| 6812 | # return | ||
| 6813 | return($n); | ||
| 6814 | |||
| 6815 | } | ||
| 6816 | |||
| 6817 | # add ISO statistics | ||
| 6818 | # the object_reference contains the mean values | ||
| 6819 | # the individual charts are in the array_of_chart_objects | ||
| 6820 | # parameters: (object_reference, array_of_chart_objects, hash) | ||
| 6821 | sub _addStats { | ||
| 6822 | |||
| 6823 | # get parameters | ||
| 6824 | my ($self, $charts, $hash) = @_; | ||
| 6825 | |||
| 6826 | # local variables | ||
| 6827 | my (@ctx, $cols, $scols); | ||
| 6828 | |||
| 6829 | # for each hash key | ||
| 6830 | for (keys(%{$hash})) { | ||
| 6831 | |||
| 6832 | # if value is a scalar | ||
| 6833 | if (! ref($hash->{$_})) { | ||
| 6834 | |||
| 6835 | # save context value | ||
| 6836 | @ctx = ($hash->{$_}); | ||
| 6837 | |||
| 6838 | } elsif (ref($hash->{$_}) eq 'ARRAY') { | ||
| 6839 | |||
| 6840 | # save context values | ||
| 6841 | @ctx = @{$hash->{$_}}; | ||
| 6842 | |||
| 6843 | } | ||
| 6844 | |||
| 6845 | # if 'STDEV_XYZ' | ||
| 6846 | if (m/^STDEV_XYZ$/) { | ||
| 6847 | |||
| 6848 | # for each context | ||
| 6849 | for my $context (@ctx) { | ||
| 6850 | |||
| 6851 | # resolve context value | ||
| 6852 | $context = defined($context) && length($context) ? $context : undef; | ||
| 6853 | |||
| 6854 | # if no STDEV_XYZ columns with context | ||
| 6855 | if (! test($self, 'STDEVXYZ', $context)) { | ||
| 6856 | |||
| 6857 | # get XYZ columns | ||
| 6858 | $cols = cols($self, map {defined($context) ? "$context|$_" : $_} qw(XYZ_X XYZ_Y XYZ_Z)); | ||
| 6859 | |||
| 6860 | # add STDEV_XYZ columns | ||
| 6861 | $scols = add_fmt($self, map {defined($context) ? "$context|$_" : $_} qw(STDEV_X STDEV_Y STDEV_Z)); | ||
| 6862 | |||
| 6863 | # for each XYZ | ||
| 6864 | for my $i (0 .. 2) { | ||
| 6865 | |||
| 6866 | # add STDEV_XYZ values | ||
| 6867 | _addStdDevCol($self, $charts, $cols->[$i], $scols->[$i]); | ||
| 6868 | |||
| 6869 | } | ||
| 6870 | |||
| 6871 | } | ||
| 6872 | |||
| 6873 | # set origin | ||
| 6874 | @{$self->[2][0]}[@{$scols}] = ($cols) x 3; | ||
| 6875 | |||
| 6876 | # save illuminant white point | ||
| 6877 | @{$self->[2][2]}[@{$scols}] = @{$self->[2][2]}[@{$cols}]; | ||
| 6878 | |||
| 6879 | } | ||
| 6880 | |||
| 6881 | # if 'STDEV_LAB' or 'CHI_SQD_PAR' | ||
| 6882 | } elsif (m/^(STDEV_LAB|CHI_SQD_PAR)$/) { | ||
| 6883 | |||
| 6884 | # for each context | ||
| 6885 | for my $context (@ctx) { | ||
| 6886 | |||
| 6887 | # resolve context value | ||
| 6888 | $context = defined($context) && length($context) ? $context : undef; | ||
| 6889 | |||
| 6890 | # if no STDEV_LAB columns with context | ||
| 6891 | if (! test($self, 'STDEVLAB', $context)) { | ||
| 6892 | |||
| 6893 | # get L*a*b* columns | ||
| 6894 | $cols = cols($self, map {defined($context) ? "$context|$_" : $_} qw(LAB_L LAB_A LAB_B)); | ||
| 6895 | |||
| 6896 | # add STDEV_LAB columns | ||
| 6897 | $scols = add_fmt($self, map {defined($context) ? "$context|$_" : $_} qw(STDEV_L STDEV_A STDEV_B)); | ||
| 6898 | |||
| 6899 | # for each L*a*b* | ||
| 6900 | for my $i (0 .. 2) { | ||
| 6901 | |||
| 6902 | # add STDEV_LAB values | ||
| 6903 | _addStdDevCol($self, $charts, $cols->[$i], $scols->[$i]); | ||
| 6904 | |||
| 6905 | } | ||
| 6906 | |||
| 6907 | # set origin | ||
| 6908 | @{$self->[2][0]}[@{$scols}] = ($cols) x 3; | ||
| 6909 | |||
| 6910 | # save illuminant white point | ||
| 6911 | @{$self->[2][2]}[@{$scols}] = @{$self->[2][2]}[@{$cols}]; | ||
| 6912 | |||
| 6913 | } | ||
| 6914 | |||
| 6915 | # if 'CHI_SQD_PAR' | ||
| 6916 | if ($1 eq 'CHI_SQD_PAR') { | ||
| 6917 | |||
| 6918 | # get STDEV_LAB columns | ||
| 6919 | $cols = cols($self, map {defined($context) ? "$context|$_" : $_} qw(STDEV_L STDEV_A STDEV_B)); | ||
| 6920 | |||
| 6921 | # add CHI_SQD_PAR column | ||
| 6922 | $scols = add_fmt($self, map {defined($context) ? "$context|$_" : $_} qw(CHI_SQD_PAR)); | ||
| 6923 | |||
| 6924 | # for each sample | ||
| 6925 | for my $i (1 .. $#{$self->[1]}) { | ||
| 6926 | |||
| 6927 | # set CHI_SQD_PAR value (average of L*a*b* standard deviations) | ||
| 6928 | $self->[1][$i][$scols->[0]] = List::Util::sum(@{$self->[1][$i]}[@{$cols}])/3; | ||
| 6929 | |||
| 6930 | } | ||
| 6931 | |||
| 6932 | # set origin | ||
| 6933 | $self->[2][0][$scols->[0]] = $cols; | ||
| 6934 | |||
| 6935 | } | ||
| 6936 | |||
| 6937 | } | ||
| 6938 | |||
| 6939 | # if 'MEAN_DE' | ||
| 6940 | } elsif (m/^MEAN_DE$/) { | ||
| 6941 | |||
| 6942 | # for each context | ||
| 6943 | for my $context (@ctx) { | ||
| 6944 | |||
| 6945 | # resolve context value | ||
| 6946 | $context = defined($context) && length($context) ? $context : undef; | ||
| 6947 | |||
| 6948 | # if no MEAN_DE columns with context | ||
| 6949 | if (! test($self, 'MEAN_DE', $context)) { | ||
| 6950 | |||
| 6951 | # get L*a*b* columns | ||
| 6952 | $cols = cols($self, map {defined($context) ? "$context|$_" : $_} qw(LAB_L LAB_A LAB_B)); | ||
| 6953 | |||
| 6954 | # add MEAN_DE column | ||
| 6955 | $scols = add_fmt($self, map {defined($context) ? "$context|$_" : $_} qw(MEAN_DE)); | ||
| 6956 | |||
| 6957 | # add MEAN_DE values | ||
| 6958 | _addMeanDECol($self, $charts, $cols, $scols->[0]); | ||
| 6959 | |||
| 6960 | # set origin | ||
| 6961 | $self->[2][0][$scols->[0]] = $cols; | ||
| 6962 | |||
| 6963 | } | ||
| 6964 | |||
| 6965 | } | ||
| 6966 | |||
| 6967 | } | ||
| 6968 | |||
| 6969 | } | ||
| 6970 | |||
| 6971 | } | ||
| 6972 | |||
| 6973 | # add standard deviation column | ||
| 6974 | # the object_reference contains the mean values | ||
| 6975 | # the individual charts are in the array_of_chart_objects | ||
| 6976 | # parameters: (object_reference, array_of_chart_objects, mean_column, std_dev_column) | ||
| 6977 | sub _addStdDevCol { | ||
| 6978 | |||
| 6979 | # get parameters | ||
| 6980 | my ($self, $charts, $m, $s) = @_; | ||
| 6981 | |||
| 6982 | # local variables | ||
| 6983 | my ($n); | ||
| 6984 | |||
| 6985 | # get number of charts | ||
| 6986 | $n = @{$charts}; | ||
| 6987 | |||
| 6988 | # for each sample | ||
| 6989 | for my $i (1 .. $#{$self->[1]}) { | ||
| 6990 | |||
| 6991 | # initialize value | ||
| 6992 | $self->[1][$i][$s] = 0; | ||
| 6993 | |||
| 6994 | # if number of charts > 0 | ||
| 6995 | if ($n) { | ||
| 6996 | |||
| 6997 | # for each chart | ||
| 6998 | for my $j (0 .. $#{$charts}) { | ||
| 6999 | |||
| 7000 | # add squared difference | ||
| 7001 | $self->[1][$i][$s] += ($charts->[$j][$i][$m] - $self->[1][$i][$m])**2; | ||
| 7002 | |||
| 7003 | } | ||
| 7004 | |||
| 7005 | # complete calculation | ||
| 7006 | $self->[1][$i][$s] = sqrt($self->[1][$i][$s]/$n); | ||
| 7007 | |||
| 7008 | } else { | ||
| 7009 | |||
| 7010 | # error | ||
| 7011 | croak('can\'t compute standard deviation with zero samples'); | ||
| 7012 | |||
| 7013 | } | ||
| 7014 | |||
| 7015 | } | ||
| 7016 | |||
| 7017 | } | ||
| 7018 | |||
| 7019 | # add mean dEab column | ||
| 7020 | # the object_reference contains the mean values | ||
| 7021 | # the individual charts are in the array_of_chart_objects | ||
| 7022 | # parameters: (object_reference, array_of_chart_objects, mean_L*a*b*_columns, mean_dE_column) | ||
| 7023 | sub _addMeanDECol { | ||
| 7024 | |||
| 7025 | # get parameters | ||
| 7026 | my ($self, $charts, $m, $s) = @_; | ||
| 7027 | |||
| 7028 | # local variables | ||
| 7029 | my ($n, $dE); | ||
| 7030 | |||
| 7031 | # get number of charts | ||
| 7032 | $n = @{$charts}; | ||
| 7033 | |||
| 7034 | # for each sample | ||
| 7035 | for my $i (1 .. $#{$self->[1]}) { | ||
| 7036 | |||
| 7037 | # initialize value | ||
| 7038 | $self->[1][$i][$s] = 0; | ||
| 7039 | |||
| 7040 | # if number of charts > 0 | ||
| 7041 | if ($n) { | ||
| 7042 | |||
| 7043 | # for each chart | ||
| 7044 | for my $j (0 .. $#{$charts}) { | ||
| 7045 | |||
| 7046 | # initialize dE | ||
| 7047 | $dE = 0; | ||
| 7048 | |||
| 7049 | # for each L*a*b* | ||
| 7050 | for my $k (0 .. 2) { | ||
| 7051 | |||
| 7052 | # add squared difference | ||
| 7053 | $dE += ($self->[1][$i][$m->[$k]] - $charts->[$j][$i][$m->[$k]])**2; | ||
| 7054 | |||
| 7055 | } | ||
| 7056 | |||
| 7057 | # add dE for this chart | ||
| 7058 | $self->[1][$i][$s] += sqrt($dE); | ||
| 7059 | |||
| 7060 | } | ||
| 7061 | |||
| 7062 | # complete calculation | ||
| 7063 | $self->[1][$i][$s] /= $n; | ||
| 7064 | |||
| 7065 | } else { | ||
| 7066 | |||
| 7067 | # error | ||
| 7068 | croak('can\'t compute mean dE with zero samples'); | ||
| 7069 | |||
| 7070 | } | ||
| 7071 | |||
| 7072 | } | ||
| 7073 | |||
| 7074 | } | ||
| 7075 | |||
| 7076 | # read chart from list of data files | ||
| 7077 | # files must have identical structure (cols) | ||
| 7078 | # reads first chart, then appends other charts | ||
| 7079 | # parameters: (object_reference, ref_to_file_list, hash) | ||
| 7080 | # returns: (number_of_files_appended) | ||
| 7081 | sub _readChartAppend { | ||
| 7082 | |||
| 7083 | # get parameters | ||
| 7084 | my ($self, $list, $hash) = @_; | ||
| 7085 | |||
| 7086 | # local variables | ||
| 7087 | my ($n, $result, $keys, $temp); | ||
| 7088 | |||
| 7089 | # initialize file counter | ||
| 7090 | $n = 0; | ||
| 7091 | |||
| 7092 | # for each file | ||
| 7093 | for my $file (@{$list}) { | ||
| 7094 | |||
| 7095 | # if first file | ||
| 7096 | if ($n == 0) { | ||
| 7097 | |||
| 7098 | # if file read successfully | ||
| 7099 | if (! ($result = _readChart($self, $file, $hash))) { | ||
| 7100 | |||
| 7101 | # add colorimetric metadata | ||
| 7102 | _addColorMeta($self); | ||
| 7103 | |||
| 7104 | # make format key string | ||
| 7105 | $keys = join(':', map {defined() ? $_ : '-'} @{$self->[1][0]}); | ||
| 7106 | |||
| 7107 | # increment counter | ||
| 7108 | $n++; | ||
| 7109 | |||
| 7110 | } else { | ||
| 7111 | |||
| 7112 | # print warning | ||
| 7113 | warn("chart $file $result, ignored\n"); | ||
| 7114 | |||
| 7115 | } | ||
| 7116 | |||
| 7117 | } else { | ||
| 7118 | |||
| 7119 | # make temporary Chart object | ||
| 7120 | $temp = ICC::Support::Chart->new(); | ||
| 7121 | |||
| 7122 | # if file read successfully | ||
| 7123 | if (! ($result = _readChart($temp, $file, $hash))) { | ||
| 7124 | |||
| 7125 | # if charts have same structure (cols) | ||
| 7126 | if ($keys eq join(':', map {defined() ? $_ : '-'} @{$temp->[1][0]})) { | ||
| 7127 | |||
| 7128 | # append temp samples | ||
| 7129 | push(@{$self->[1]}, @{$temp->[1]}[1 .. $#{$temp->[1]}]); | ||
| 7130 | |||
| 7131 | # increment counter | ||
| 7132 | $n++; | ||
| 7133 | |||
| 7134 | } else { | ||
| 7135 | |||
| 7136 | # print warning | ||
| 7137 | warn("chart $file has different structure, ignored\n"); | ||
| 7138 | |||
| 7139 | } | ||
| 7140 | |||
| 7141 | } else { | ||
| 7142 | |||
| 7143 | # print warning | ||
| 7144 | warn("chart $file $result, ignored\n"); | ||
| 7145 | |||
| 7146 | } | ||
| 7147 | |||
| 7148 | } | ||
| 7149 | |||
| 7150 | } | ||
| 7151 | |||
| 7152 | # print message if any files were read | ||
| 7153 | print "$n files read in directory $self->[0]{'file_path'}\n\n" if ($n); | ||
| 7154 | |||
| 7155 | # return | ||
| 7156 | return($n); | ||
| 7157 | |||
| 7158 | } | ||
| 7159 | |||
| 7160 | # read chart | ||
| 7161 | # parameters: (object_reference, path_to_file, hash) | ||
| 7162 | # returns: (result) | ||
| 7163 | sub _readChart { | ||
| 7164 | |||
| 7165 | # get parameters | ||
| 7166 | my ($self, $path, $hash) = @_; | ||
| 7167 | |||
| 7168 | # local variables | ||
| 7169 | my ($fh, $buf, $result); | ||
| 7170 | |||
| 7171 | # open the file (read-only) | ||
| 7172 | open($fh, '<', $path) || return("$! when opening $path"); | ||
| 7173 | |||
| 7174 | # set binary mode | ||
| 7175 | binmode($fh); | ||
| 7176 | |||
| 7177 | # read start of file | ||
| 7178 | read($fh, $buf, 1024); | ||
| 7179 | |||
| 7180 | # reset file pointer | ||
| 7181 | seek($fh, 0, 0); | ||
| 7182 | |||
| 7183 | # if an ASE file | ||
| 7184 | if ($buf =~ m/^ASEF/) { | ||
| 7185 | |||
| 7186 | # save file type | ||
| 7187 | $self->[0]{'file_type'} = 'ASEF'; | ||
| 7188 | |||
| 7189 | # read ASE file | ||
| 7190 | $result = _readChartASE($self, $fh, $hash); | ||
| 7191 | |||
| 7192 | # if a TIFF file | ||
| 7193 | } elsif ($buf =~ m/^(II\*\x00|MM\x00\*)/) { | ||
| 7194 | |||
| 7195 | # save file type | ||
| 7196 | $self->[0]{'file_type'} = 'TIFF'; | ||
| 7197 | |||
| 7198 | # read TIFF file | ||
| 7199 | $result = _readChartTIFF($self, $fh, $hash); | ||
| 7200 | |||
| 7201 | # if an ICC profile | ||
| 7202 | } elsif (substr($buf, 36, 4) eq 'acsp') { | ||
| 7203 | |||
| 7204 | # save file type | ||
| 7205 | $self->[0]{'file_type'} = 'prof'; | ||
| 7206 | |||
| 7207 | # read ICC file | ||
| 7208 | $result = _readChartICC($self, $fh, $hash); | ||
| 7209 | |||
| 7210 | # if an XML file | ||
| 7211 | } elsif ($buf =~ m/<\?xml/) { | ||
| 7212 | |||
| 7213 | # save file type | ||
| 7214 | $self->[0]{'file_type'} = 'CXFX'; | ||
| 7215 | |||
| 7216 | # read CxF3 file | ||
| 7217 | $result = _readChartCxF3($self, $fh, $hash); | ||
| 7218 | |||
| 7219 | # if an SS3 file | ||
| 7220 | } elsif (substr($buf, 0, 4) eq "\x00\x20\x00\x00" || substr($buf, 0, 4) eq "\x00\x32\x00\x00") { | ||
| 7221 | |||
| 7222 | # save file type | ||
| 7223 | $self->[0]{'file_type'} = 'SS3'; | ||
| 7224 | |||
| 7225 | # read SS3 file | ||
| 7226 | $result = _readChartSS3($self, $fh, $hash); | ||
| 7227 | |||
| 7228 | } else { | ||
| 7229 | |||
| 7230 | # check for CR-LF (DOS/Windows) | ||
| 7231 | if ($buf =~ m/\015\012/) { | ||
| 7232 | |||
| 7233 | # set record separator | ||
| 7234 | $self->[0]{'read_rs'} = "\015\012"; | ||
| 7235 | |||
| 7236 | # check for LF (Unix/OSX) | ||
| 7237 | } elsif ($buf =~ m/\012/) { | ||
| 7238 | |||
| 7239 | # set record separator | ||
| 7240 | $self->[0]{'read_rs'} = "\012"; | ||
| 7241 | |||
| 7242 | # check for CR (Mac) | ||
| 7243 | } elsif ($buf =~ m/\015/) { | ||
| 7244 | |||
| 7245 | # set record separator | ||
| 7246 | $self->[0]{'read_rs'} = "\015"; | ||
| 7247 | |||
| 7248 | # not a text file | ||
| 7249 | } else { | ||
| 7250 | |||
| 7251 | # close the file | ||
| 7252 | close($fh); | ||
| 7253 | |||
| 7254 | # return | ||
| 7255 | return('unknown file type'); | ||
| 7256 | |||
| 7257 | } | ||
| 7258 | |||
| 7259 | # save file type | ||
| 7260 | $self->[0]{'file_type'} = 'TEXT'; | ||
| 7261 | |||
| 7262 | # read ASCII file | ||
| 7263 | $result = _readChartASCII($self, $fh, $hash); | ||
| 7264 | |||
| 7265 | } | ||
| 7266 | |||
| 7267 | # close the file | ||
| 7268 | close($fh); | ||
| 7269 | |||
| 7270 | # return | ||
| 7271 | return($result); | ||
| 7272 | |||
| 7273 | } | ||
| 7274 | |||
| 7275 | # read chart from ISO 28178 ASCII data file | ||
| 7276 | # parameters: (object_reference, file_handle, hash) | ||
| 7277 | # returns: (result) | ||
| 7278 | sub _readChartASCII { | ||
| 7279 | |||
| 7280 | # get parameters | ||
| 7281 | my ($self, $fh, $hash) = @_; | ||
| 7282 | |||
| 7283 | # local variables | ||
| 7284 | my ($state, $iflag, $eflag, $index); | ||
| 7285 | my (@fields, $illum, $append); | ||
| 7286 | |||
| 7287 | # localize input record separator | ||
| 7288 | local $/ = $self->[0]{'read_rs'}; | ||
| 7289 | |||
| 7290 | # localize loop variable | ||
| 7291 | local $_; | ||
| 7292 | |||
| 7293 | # initialize variables | ||
| 7294 | $self->[1] = [[]]; | ||
| 7295 | $illum = [[]]; | ||
| 7296 | $index = 1; | ||
| 7297 | $state = 0; | ||
| 7298 | $iflag = 0; | ||
| 7299 | |||
| 7300 | # read the file, line by line | ||
| 7301 | while (<$fh>) { | ||
| 7302 | |||
| 7303 | # add appended text, as is | ||
| 7304 | $append .= $_ if ($state == 4); | ||
| 7305 | |||
| 7306 | # remove leading spaces/tabs and trailing whitespace | ||
| 7307 | s/^[ \t]*(.*?)[\s,]*$/$1/; | ||
| 7308 | |||
| 7309 | # if normal comment line (all comments are removed) | ||
| 7310 | if (s/#[\s]*(.*)// && $state == 0) { | ||
| 7311 | |||
| 7312 | # if remaining line blank | ||
| 7313 | if (length() == 0) { | ||
| 7314 | |||
| 7315 | # add comment to header array | ||
| 7316 | push(@{$self->[3]}, ['#', $1]); | ||
| 7317 | |||
| 7318 | } else { | ||
| 7319 | |||
| 7320 | # restore comment to header line | ||
| 7321 | # preserves time in ProfileMaker 'CREATED' lines | ||
| 7322 | $_ .= "# $1"; | ||
| 7323 | |||
| 7324 | } | ||
| 7325 | |||
| 7326 | } | ||
| 7327 | |||
| 7328 | # skip blank lines | ||
| 7329 | next if (length() == 0); | ||
| 7330 | |||
| 7331 | # begin data format | ||
| 7332 | if (m/^BEGIN_DATA_FORMAT$/) { | ||
| 7333 | |||
| 7334 | # set state | ||
| 7335 | $state = 1; | ||
| 7336 | |||
| 7337 | # end data format | ||
| 7338 | } elsif (m/^END_DATA_FORMAT$/) { | ||
| 7339 | |||
| 7340 | # set state | ||
| 7341 | $state = 2; | ||
| 7342 | |||
| 7343 | # begin data | ||
| 7344 | } elsif (m/^BEGIN_DATA$/) { | ||
| 7345 | |||
| 7346 | # set state | ||
| 7347 | $state = 3; | ||
| 7348 | |||
| 7349 | # end data | ||
| 7350 | } elsif (m/^END_DATA$/) { | ||
| 7351 | |||
| 7352 | # set state | ||
| 7353 | $state = 4; | ||
| 7354 | |||
| 7355 | # begin ProfileMaker illuminant section | ||
| 7356 | } elsif (m/^BEGIN_DATA_EMISSION$/) { | ||
| 7357 | |||
| 7358 | # set illuminant flag | ||
| 7359 | $iflag = 1; | ||
| 7360 | |||
| 7361 | # reset index | ||
| 7362 | $index = 1; | ||
| 7363 | |||
| 7364 | # end ProfileMaker illuminant section | ||
| 7365 | } elsif (m/^END_DATA_EMISSION$/) { | ||
| 7366 | |||
| 7367 | # clear illuminant flag | ||
| 7368 | $iflag = 0; | ||
| 7369 | |||
| 7370 | # reset appended data | ||
| 7371 | $append = ''; | ||
| 7372 | |||
| 7373 | # anything else | ||
| 7374 | } else { | ||
| 7375 | |||
| 7376 | # format | ||
| 7377 | if ($iflag == 0 && $state == 1) { | ||
| 7378 | |||
| 7379 | # change 'SampleID' to 'SAMPLE_ID' | ||
| 7380 | # non-standard notation used by ProfileMaker | ||
| 7381 | s/SampleID/SAMPLE_ID/; | ||
| 7382 | |||
| 7383 | # parse and save format keys | ||
| 7384 | push(@{$self->[1][0]}, split(/[\s,]+/)); | ||
| 7385 | |||
| 7386 | # data | ||
| 7387 | } elsif ($iflag == 0 && $state == 3) { | ||
| 7388 | |||
| 7389 | # if Euro flag not defined | ||
| 7390 | if (! defined($eflag)) { | ||
| 7391 | |||
| 7392 | # split data | ||
| 7393 | @fields = split(/[\s,]+/); | ||
| 7394 | |||
| 7395 | # set flag for Euro decimal notation (e.g. 6,3 becomes 6.3) | ||
| 7396 | $eflag = m/,/ && @fields > (@{$self->[1][0]} || 0); | ||
| 7397 | |||
| 7398 | } | ||
| 7399 | |||
| 7400 | # fix Euro decimal notation (e.g. 6,3 becomes 6.3) | ||
| 7401 | s/(\d),(\d)/$1.$2/g if ($eflag); | ||
| 7402 | |||
| 7403 | # parse and save data | ||
| 7404 | $self->[1][$index++] = [split(/[\s,]+/)]; | ||
| 7405 | |||
| 7406 | # illuminant format | ||
| 7407 | # may be different from data format | ||
| 7408 | } elsif ($iflag == 1 && $state == 1) { | ||
| 7409 | |||
| 7410 | # change 'SampleID' to 'SAMPLE_ID' | ||
| 7411 | # non-standard notation used by ProfileMaker | ||
| 7412 | s/SampleID/SAMPLE_ID/; | ||
| 7413 | |||
| 7414 | # parse and save illuminant format keys | ||
| 7415 | push(@{$illum->[0]}, split(/[\s,]+/)); | ||
| 7416 | |||
| 7417 | # illuminant data | ||
| 7418 | } elsif ($iflag == 1 && $state == 3) { | ||
| 7419 | |||
| 7420 | # fix Euro decimal notation (e.g. 6,3 becomes 6.3) | ||
| 7421 | s/(\d),(\d)/$1.$2/g if ($eflag); | ||
| 7422 | |||
| 7423 | # parse and save illuminant data | ||
| 7424 | $illum->[$index++] = [split(/[\s,]+/)]; | ||
| 7425 | |||
| 7426 | # header lines | ||
| 7427 | } elsif ($iflag == 0 && ($state == 0 || $state == 2)) { | ||
| 7428 | |||
| 7429 | # match keyword/value | ||
| 7430 | m/^([^\s,]*)[\s,]*(.*?)$/; | ||
| 7431 | |||
| 7432 | # add to header array | ||
| 7433 | push(@{$self->[3]}, [$1, $2]) if (length($1)); | ||
| 7434 | |||
| 7435 | } | ||
| 7436 | |||
| 7437 | } | ||
| 7438 | |||
| 7439 | } | ||
| 7440 | |||
| 7441 | # save illuminant data, if any | ||
| 7442 | $self->[0]{'illuminant'} = $illum if defined($illum->[1]); | ||
| 7443 | |||
| 7444 | # save appended data, if any | ||
| 7445 | $self->[0]{'append'} = $append if (defined($append)); | ||
| 7446 | |||
| 7447 | # apply rotation/flip (special keywords) | ||
| 7448 | _rotateChartASCII($self); | ||
| 7449 | |||
| 7450 | # return success flag | ||
| 7451 | return($state == 4 ? () : "ASCII read failed with state $state"); | ||
| 7452 | |||
| 7453 | } | ||
| 7454 | |||
| 7455 | # apply rotation/flip to ASCII chart data | ||
| 7456 | # if LGOROWLENGTH and (DPLGROTATE or DPLGFLIP) keywords are present | ||
| 7457 | # parameter: (object_reference) | ||
| 7458 | sub _rotateChartASCII { | ||
| 7459 | |||
| 7460 | # get object reference | ||
| 7461 | my $self = shift(); | ||
| 7462 | |||
| 7463 | # local variables | ||
| 7464 | my ($rot, $flip, $mat, $rows); | ||
| 7465 | |||
| 7466 | # get the rotation and flip values | ||
| 7467 | $rot = keyword($self, 'DPLGROTATE'); | ||
| 7468 | $flip = keyword($self, 'DPLGFLIP'); | ||
| 7469 | |||
| 7470 | # if LGOROWLENGTH and (DPLGROTATE or DPLGFLIP) keywords | ||
| 7471 | if (keyword($self, 'LGOROWLENGTH') && ($rot || $flip)) { | ||
| 7472 | |||
| 7473 | # get selection matrix | ||
| 7474 | $mat = select_matrix($self)->rotate($rot)->flip($flip); | ||
| 7475 | |||
| 7476 | # flatten matrix | ||
| 7477 | $rows = ICC::Shared::flatten($mat); | ||
| 7478 | |||
| 7479 | # prepend DATA_FORMAT row index (0) | ||
| 7480 | unshift(@{$rows}, 0); | ||
| 7481 | |||
| 7482 | # rearrange chart data | ||
| 7483 | $self->[1] = [@{$self->[1]}[@{$rows}]]; | ||
| 7484 | |||
| 7485 | # update LGOROWLENGTH | ||
| 7486 | keyword($self, 'LGOROWLENGTH', scalar(@{$mat->[0]})); | ||
| 7487 | |||
| 7488 | } | ||
| 7489 | |||
| 7490 | } | ||
| 7491 | |||
| 7492 | # read chart from Adobe Swatch Exchange (.ase) file | ||
| 7493 | # optional hash key: 'colorspace' | ||
| 7494 | # 'colorspace' values: 'CMYK', 'LAB ', 'RGB ' or 'Gray' | ||
| 7495 | # 'Gray' swatches are mapped to CMYK values | ||
| 7496 | # parameters: (object_reference, file_handle, hash) | ||
| 7497 | # returns: (result) | ||
| 7498 | sub _readChartASE { | ||
| 7499 | |||
| 7500 | # get parameters | ||
| 7501 | my ($self, $fh, $hash) = @_; | ||
| 7502 | |||
| 7503 | # local variables | ||
| 7504 | my ($cs, $le, $buf, @header, $sn); | ||
| 7505 | my ($mark, $type, $blen, $slen); | ||
| 7506 | my ($name, $space, $cmyk, $rgb, $Lab, $dev); | ||
| 7507 | |||
| 7508 | # set colorspace selector | ||
| 7509 | $cs = $hash->{'colorspace'} if defined($hash->{'colorspace'}); | ||
| 7510 | |||
| 7511 | # get little-endian flag | ||
| 7512 | $le = ($Config{'byteorder'} =~ m/1234/); | ||
| 7513 | |||
| 7514 | # read header (file signature, version, number of blocks) | ||
| 7515 | read($fh, $buf, 12); | ||
| 7516 | |||
| 7517 | # unpack buffer | ||
| 7518 | @header = unpack('A4nnN', $buf); | ||
| 7519 | |||
| 7520 | # verify file signature | ||
| 7521 | ($header[0] eq 'ASEF') || return('not a valid ASE file'); | ||
| 7522 | |||
| 7523 | # add SAMPLE_NAME field | ||
| 7524 | $sn = add_fmt($self, 'SAMPLE_NAME'); | ||
| 7525 | |||
| 7526 | # set file pointer | ||
| 7527 | $mark = 12; | ||
| 7528 | |||
| 7529 | # for each block | ||
| 7530 | for my $s (1 .. $header[3]) { | ||
| 7531 | |||
| 7532 | # read block type, block length, and string length | ||
| 7533 | read($fh, $buf, 8); | ||
| 7534 | |||
| 7535 | # unpack buffer | ||
| 7536 | ($type, $blen, $slen) = unpack('nNn', $buf); | ||
| 7537 | |||
| 7538 | # if color entry type | ||
| 7539 | if ($type == 1) { | ||
| 7540 | |||
| 7541 | # read color name | ||
| 7542 | read($fh, $buf, 2 * $slen); | ||
| 7543 | |||
| 7544 | # decode color name | ||
| 7545 | $name = decode('UTF-16BE', $buf); | ||
| 7546 | |||
| 7547 | # trim trailing '0' | ||
| 7548 | $name =~ s/\x00$//; | ||
| 7549 | |||
| 7550 | # change spaces to underscores | ||
| 7551 | $name =~ s/\s/_/g; | ||
| 7552 | |||
| 7553 | # read color space | ||
| 7554 | read($fh, $space, 4); | ||
| 7555 | |||
| 7556 | # if colorspace is CMYK | ||
| 7557 | if (($space eq 'CMYK' && (! defined($cs)) || $cs eq 'CMYK')) { | ||
| 7558 | |||
| 7559 | # store color as SAMPLE_NAME | ||
| 7560 | $self->[1][$s][$sn->[0]] = $name; | ||
| 7561 | |||
| 7562 | # init device array | ||
| 7563 | $dev = []; | ||
| 7564 | |||
| 7565 | # for each CMYK value | ||
| 7566 | for my $i (0 .. 3) { | ||
| 7567 | |||
| 7568 | # read 32-bit floating point value | ||
| 7569 | read($fh, $buf, 4); | ||
| 7570 | |||
| 7571 | # reverse bytes if long-endian system | ||
| 7572 | $buf = reverse($buf) if ($le); | ||
| 7573 | |||
| 7574 | # unpack buffer | ||
| 7575 | $dev->[$i] = unpack('f', $buf); | ||
| 7576 | |||
| 7577 | } | ||
| 7578 | |||
| 7579 | # if CMYK slice undefined | ||
| 7580 | if (! defined($cmyk)) { | ||
| 7581 | |||
| 7582 | # add CMYK slice | ||
| 7583 | $cmyk = add_fmt($self, qw(CMYK_C CMYK_M CMYK_Y CMYK_K)); | ||
| 7584 | |||
| 7585 | } | ||
| 7586 | |||
| 7587 | # store CMYK values | ||
| 7588 | @{$self->[1][$s]}[@{$cmyk}] = map {100 * $_} @{$dev}; | ||
| 7589 | |||
| 7590 | # if colorspace is RGB | ||
| 7591 | } elsif (($space eq 'RGB ' && (! defined($cs)) || $cs eq 'RGB ')) { | ||
| 7592 | |||
| 7593 | # store color as SAMPLE_NAME | ||
| 7594 | $self->[1][$s][$sn->[0]] = $name; | ||
| 7595 | |||
| 7596 | # init device array | ||
| 7597 | $dev = []; | ||
| 7598 | |||
| 7599 | # for each RGB value | ||
| 7600 | for my $i (0 .. 2) { | ||
| 7601 | |||
| 7602 | # read 32-bit floating point value | ||
| 7603 | read($fh, $buf, 4); | ||
| 7604 | |||
| 7605 | # reverse bytes if long-endian system | ||
| 7606 | $buf = reverse($buf) if ($le); | ||
| 7607 | |||
| 7608 | # unpack buffer | ||
| 7609 | $dev->[$i] = unpack('f', $buf); | ||
| 7610 | |||
| 7611 | } | ||
| 7612 | |||
| 7613 | # if RGB slice undefined | ||
| 7614 | if (! defined($rgb)) { | ||
| 7615 | |||
| 7616 | # add RGB slice | ||
| 7617 | $rgb = add_fmt($self, qw(RGB_R RGB_G RGB_B)); | ||
| 7618 | |||
| 7619 | } | ||
| 7620 | |||
| 7621 | # store RGB values | ||
| 7622 | @{$self->[1][$s]}[@{$rgb}] = map {255 * $_} @{$dev}; | ||
| 7623 | |||
| 7624 | # if colorspace is L*a*b* | ||
| 7625 | } elsif (($space eq 'LAB ' && (! defined($cs)) || $cs eq 'LAB ')) { | ||
| 7626 | |||
| 7627 | # store color as SAMPLE_NAME | ||
| 7628 | $self->[1][$s][$sn->[0]] = $name; | ||
| 7629 | |||
| 7630 | # init device array | ||
| 7631 | $dev = []; | ||
| 7632 | |||
| 7633 | # for each L*a*b* value | ||
| 7634 | for my $i (0 .. 2) { | ||
| 7635 | |||
| 7636 | # read 32-bit floating point value | ||
| 7637 | read($fh, $buf, 4); | ||
| 7638 | |||
| 7639 | # reverse bytes if long-endian system | ||
| 7640 | $buf = reverse($buf) if ($le); | ||
| 7641 | |||
| 7642 | # unpack buffer | ||
| 7643 | $dev->[$i] = unpack('f', $buf); | ||
| 7644 | |||
| 7645 | } | ||
| 7646 | |||
| 7647 | # if L*a*b* slice undefined | ||
| 7648 | if (! defined($Lab)) { | ||
| 7649 | |||
| 7650 | # add L*a*b* fields | ||
| 7651 | $Lab = add_fmt($self, qw(LAB_L LAB_A LAB_B)); | ||
| 7652 | |||
| 7653 | } | ||
| 7654 | |||
| 7655 | # store L*a*b* values | ||
| 7656 | @{$self->[1][$s]}[@{$Lab}] = (100 * $dev->[0], $dev->[1], $dev->[2]); | ||
| 7657 | |||
| 7658 | # if colorspace is Grayscale | ||
| 7659 | } elsif (($space eq 'Gray' && (! defined($cs)) || $cs eq 'Gray')) { | ||
| 7660 | |||
| 7661 | # store color as SAMPLE_NAME | ||
| 7662 | $self->[1][$s][$sn->[0]] = $name; | ||
| 7663 | |||
| 7664 | # read 32-bit floating point value | ||
| 7665 | read($fh, $buf, 4); | ||
| 7666 | |||
| 7667 | # reverse bytes if long-endian system | ||
| 7668 | $buf = reverse($buf) if ($le); | ||
| 7669 | |||
| 7670 | # unpack buffer | ||
| 7671 | $dev = [unpack('f', $buf)]; | ||
| 7672 | |||
| 7673 | # if CMYK slice is undefined | ||
| 7674 | if (! defined($cmyk)) { | ||
| 7675 | |||
| 7676 | # add CMYK slice | ||
| 7677 | $cmyk = add_fmt($self, qw(CMYK_C CMYK_M CMYK_Y CMYK_K)); | ||
| 7678 | |||
| 7679 | } | ||
| 7680 | |||
| 7681 | # store CMYK values | ||
| 7682 | @{$self->[1][$s]}[@{$cmyk}] = (0, 0, 0, 100 * (1 - $dev->[0])); | ||
| 7683 | |||
| 7684 | } | ||
| 7685 | |||
| 7686 | } | ||
| 7687 | |||
| 7688 | # set file pointer to next block | ||
| 7689 | $mark += $blen + 6; | ||
| 7690 | |||
| 7691 | # seek next block | ||
| 7692 | seek($fh, $mark, 0); | ||
| 7693 | |||
| 7694 | } | ||
| 7695 | |||
| 7696 | # return | ||
| 7697 | return(); | ||
| 7698 | |||
| 7699 | } | ||
| 7700 | |||
| 7701 | # read chart from ICC profile | ||
| 7702 | # some profiles have tags containing chart data | ||
| 7703 | # parameters: (object_reference, file_handle, hash) | ||
| 7704 | # returns: (result) | ||
| 7705 | sub _readChartICC { | ||
| 7706 | |||
| 7707 | # get parameters | ||
| 7708 | my ($self, $fh, $hash) = @_; | ||
| 7709 | |||
| 7710 | # local variables | ||
| 7711 | my (@header, @tagtab, %offset, %tags, $type, $class); | ||
| 7712 | my ($temp, $data, $text, $result); | ||
| 7713 | |||
| 7714 | # load ICC::Profile modules, if not already included | ||
| 7715 | require ICC::Profile; | ||
| 7716 | |||
| 7717 | # read the profile header | ||
| 7718 | ICC::Profile::_readICCheader($fh, \@header) || return('failed reading ICC profile header'); | ||
| 7719 | |||
| 7720 | # read the profile tag table | ||
| 7721 | ICC::Profile::_readICCtagtable($fh, \@tagtab) || return('failed reading ICC profile tag table'); | ||
| 7722 | |||
| 7723 | # for each tag | ||
| 7724 | for my $tag (@tagtab) { | ||
| 7725 | |||
| 7726 | # if tag contains measurement data | ||
| 7727 | if ($tag->[0] =~ m/^(?:CxF |DevD|CIED|DEVD|targ)$/) { | ||
| 7728 | |||
| 7729 | # if a duplicate tag | ||
| 7730 | if (exists($offset{$tag->[1]})) { | ||
| 7731 | |||
| 7732 | # use original tag | ||
| 7733 | $tags{$tag->[0]} = $offset{$tag->[1]}; | ||
| 7734 | |||
| 7735 | } else { | ||
| 7736 | |||
| 7737 | # seek to start of tag | ||
| 7738 | seek($fh, $tag->[1], 0); | ||
| 7739 | |||
| 7740 | # read tag type signature | ||
| 7741 | read($fh, $type, 4); | ||
| 7742 | |||
| 7743 | # convert non-word characters to underscores | ||
| 7744 | $type =~ s|\W|_|g; | ||
| 7745 | |||
| 7746 | # form class specifier | ||
| 7747 | $class = 'ICC::Profile::' . $type; | ||
| 7748 | |||
| 7749 | # if 'class->new_fh' method exists | ||
| 7750 | if ($class->can('new_fh')) { | ||
| 7751 | |||
| 7752 | # create specific tag object | ||
| 7753 | $tags{$tag->[0]} = $class->new_fh($self, $fh, $tag); | ||
| 7754 | |||
| 7755 | } else { | ||
| 7756 | |||
| 7757 | # create generic tag object | ||
| 7758 | $tags{$tag->[0]} = ICC::Profile::Generic->new_fh($self, $fh, $tag); | ||
| 7759 | |||
| 7760 | } | ||
| 7761 | |||
| 7762 | # save tag in hash | ||
| 7763 | $offset{$tag->[1]} = $tags{$tag->[0]}; | ||
| 7764 | |||
| 7765 | } | ||
| 7766 | |||
| 7767 | } | ||
| 7768 | |||
| 7769 | } | ||
| 7770 | |||
| 7771 | # if creator is i1Profiler and 'CxF ' tag exists | ||
| 7772 | if ($header[23] eq 'XRCM' && exists($tags{'CxF '})) { | ||
| 7773 | |||
| 7774 | # close file handle | ||
| 7775 | close($fh); | ||
| 7776 | |||
| 7777 | # open file handle to CxF3 string | ||
| 7778 | open($fh, '<', \$tags{'CxF '}->text()); | ||
| 7779 | |||
| 7780 | # make chart from CxF3 string | ||
| 7781 | return(_readChartCxF3($self, $fh, $hash)); | ||
| 7782 | |||
| 7783 | # if creator is ProfileMaker and 'DevD' / 'CIED' tags exist | ||
| 7784 | } elsif ($header[23] eq 'LOGO' && exists($tags{'DevD'}) && exists($tags{'CIED'})) { | ||
| 7785 | |||
| 7786 | # close file handle | ||
| 7787 | close($fh); | ||
| 7788 | |||
| 7789 | # open file handle to 'DevD' text string | ||
| 7790 | open($fh, '<', \$tags{'DevD'}->text()); | ||
| 7791 | |||
| 7792 | # read chart from text | ||
| 7793 | ($result = _readChartASCII($self, $fh, $hash)) && return("failed reading ICC profile DEVD tag, $result"); | ||
| 7794 | |||
| 7795 | # close file handle | ||
| 7796 | close($fh); | ||
| 7797 | |||
| 7798 | # make temporary chart object | ||
| 7799 | $temp = ICC::Support::Chart->new(); | ||
| 7800 | |||
| 7801 | # open file handle to 'CIED' text string | ||
| 7802 | open($fh, '<', \$tags{'CIED'}->text()); | ||
| 7803 | |||
| 7804 | # read chart from text | ||
| 7805 | ($result = _readChartASCII($temp, $fh, $hash)) && return("failed reading ICC profile CIED tag, $result"); | ||
| 7806 | |||
| 7807 | # get data slice (all rows, spectral, XYZ and L*a*b* columns) | ||
| 7808 | $data = slice($temp, [0 .. $#{$temp->[1]}], [grep {$temp->[1][0][$_] =~ m/^(nm\d{3}|XYZ_(X|Y|Z)|LAB_(L|A|B))$/} (0 .. $#{$temp->[1][0]})]); | ||
| 7809 | |||
| 7810 | # append to chart | ||
| 7811 | add_cols($self, $data); | ||
| 7812 | |||
| 7813 | # for each keyword | ||
| 7814 | for my $key (@{$temp->[3]}) { | ||
| 7815 | |||
| 7816 | # if keyword not in main chart | ||
| 7817 | if (0 == grep {$key->[0] eq $_->[0]} @{$self->[3]}) { | ||
| 7818 | |||
| 7819 | # append keyword/value | ||
| 7820 | push(@{$self->[3]}, $key); | ||
| 7821 | |||
| 7822 | } | ||
| 7823 | |||
| 7824 | } | ||
| 7825 | |||
| 7826 | # return | ||
| 7827 | return(); | ||
| 7828 | |||
| 7829 | # if creator is Monaco and 'DEVD' tag exists (some old profiles are identified by preferred CMM) | ||
| 7830 | } elsif (($header[23] eq 'MONS' || $header[1] eq 'mnco') && exists($tags{'DEVD'})) { | ||
| 7831 | |||
| 7832 | # read chart from Monaco 'DEVD' tag | ||
| 7833 | return(_readMonacoDEVD($self, $tags{'DEVD'}->data(), \@header)); | ||
| 7834 | |||
| 7835 | # if 'targ' tag exists | ||
| 7836 | } elsif (exists($tags{'targ'})) { | ||
| 7837 | |||
| 7838 | # get 'targ' tag text string | ||
| 7839 | $text = $tags{'targ'}->text(); | ||
| 7840 | |||
| 7841 | # if reference to ICC Characterization Data Registry | ||
| 7842 | if ($text =~ m/^ICCHDAT (.*)$/) { | ||
| 7843 | |||
| 7844 | # return | ||
| 7845 | return("profile derived from $1 characterization data, available at www.color.org"); | ||
| 7846 | |||
| 7847 | } else { | ||
| 7848 | |||
| 7849 | # close file handle | ||
| 7850 | close($fh); | ||
| 7851 | |||
| 7852 | # open file handle to text string | ||
| 7853 | open($fh, '<', \$text); | ||
| 7854 | |||
| 7855 | # read chart from text | ||
| 7856 | return(_readChartASCII($self, $fh, $hash)); | ||
| 7857 | |||
| 7858 | } | ||
| 7859 | |||
| 7860 | } | ||
| 7861 | |||
| 7862 | # return | ||
| 7863 | return('failed reading ICC profile characterization data'); | ||
| 7864 | |||
| 7865 | } | ||
| 7866 | |||
| 7867 | # read chart from Monaco 'DEVD' tag | ||
| 7868 | # parameters: (object_reference, tag_data, profile_header) | ||
| 7869 | # returns: (result) | ||
| 7870 | sub _readMonacoDEVD { | ||
| 7871 | |||
| 7872 | # get parameters | ||
| 7873 | my ($self, $data, $header) = @_; | ||
| 7874 | |||
| 7875 | # local variables | ||
| 7876 | my ($big, %cshash, $cs, $nc, $fix, $ix, $tag, $tac, $limit, $mult, $dev, $lab); | ||
| 7877 | my ($ns, $sec, @devfix, @nd, $nt, @devstep, @dev, @cmy, @sum, @temp, $m, @dat); | ||
| 7878 | |||
| 7879 | # get big-endian flag (true if our system is big-endian) | ||
| 7880 | $big = ($Config{'byteorder'} =~ m/4321/); | ||
| 7881 | |||
| 7882 | # colorspace hash (colorspace => number_channels) | ||
| 7883 | %cshash = ('RGB ' => 3, 'CMYK' => 4, '5CLR' => 5, '6CLR' => 6, '7CLR' => 7, '8CLR' => 8); | ||
| 7884 | |||
| 7885 | # initialize fixed value array | ||
| 7886 | @devfix = (); | ||
| 7887 | |||
| 7888 | # get colorspace from profile header | ||
| 7889 | $cs = $header->[4]; | ||
| 7890 | |||
| 7891 | # lookup number of channels | ||
| 7892 | $nc = $cshash{$cs}; | ||
| 7893 | |||
| 7894 | # set number of fixed channels | ||
| 7895 | $fix = $nc - 3; | ||
| 7896 | |||
| 7897 | # set index to start of first tag | ||
| 7898 | $ix = 28; | ||
| 7899 | |||
| 7900 | # set tag value | ||
| 7901 | $tag = pack('N', 0x002D); | ||
| 7902 | |||
| 7903 | # find TAC tag | ||
| 7904 | do {$ix = index($data, $tag, $ix)} while ($ix >= 0 && $ix % 4 && $ix++); | ||
| 7905 | |||
| 7906 | # verify tag found | ||
| 7907 | ($ix >= 0) || return('failed reading TAC from Monaco DEVD tag'); | ||
| 7908 | |||
| 7909 | # get TAC value | ||
| 7910 | $tac = 100 * unpack('d', $big ? substr($data, $ix + 4, 8) : reverse(substr($data, $ix + 4, 8))); | ||
| 7911 | |||
| 7912 | # if RGB colorspace | ||
| 7913 | if ($cs eq 'RGB ') { | ||
| 7914 | |||
| 7915 | # add device fields | ||
| 7916 | $dev = add_fmt($self, qw(RGB_R RGB_G RGB_B)); | ||
| 7917 | |||
| 7918 | # set device multiplier | ||
| 7919 | $mult = 255; | ||
| 7920 | |||
| 7921 | # if CMYK colorspace | ||
| 7922 | } elsif ($cs eq 'CMYK') { | ||
| 7923 | |||
| 7924 | # add device fields | ||
| 7925 | $dev = add_fmt($self, qw(CMYK_C CMYK_M CMYK_Y CMYK_K)); | ||
| 7926 | |||
| 7927 | # set device multiplier | ||
| 7928 | $mult = 100; | ||
| 7929 | |||
| 7930 | } else { | ||
| 7931 | |||
| 7932 | # add device fields | ||
| 7933 | $dev = add_fmt($self, map {"$cs\_$_"} (1 .. $nc)); | ||
| 7934 | |||
| 7935 | # set device multiplier | ||
| 7936 | $mult = 100; | ||
| 7937 | |||
| 7938 | } | ||
| 7939 | |||
| 7940 | # add L*a*b* fields | ||
| 7941 | $lab = add_fmt($self, qw(LAB_L LAB_A LAB_B)); | ||
| 7942 | |||
| 7943 | # advance index | ||
| 7944 | $ix += 12; | ||
| 7945 | |||
| 7946 | # set tag value | ||
| 7947 | $tag = pack('N', 0x0027); | ||
| 7948 | |||
| 7949 | # find data group tag | ||
| 7950 | do {$ix = index($data, $tag, $ix)} while ($ix >= 0 && $ix % 4 && $ix++); | ||
| 7951 | |||
| 7952 | # verify tag found | ||
| 7953 | ($ix >= 0) || return('failed reading data group from Monaco DEVD tag'); | ||
| 7954 | |||
| 7955 | # get number data sections in group | ||
| 7956 | $ns = unpack('N', substr($data, $ix + 4, 4)); | ||
| 7957 | |||
| 7958 | # advance index | ||
| 7959 | $ix += 8; | ||
| 7960 | |||
| 7961 | # for data each section | ||
| 7962 | for my $s (0 .. $ns - 1) { | ||
| 7963 | |||
| 7964 | # verify tag 0x0028 | ||
| 7965 | (substr($data, $ix, 4) eq pack('N', 0x0028)) || return(0); | ||
| 7966 | |||
| 7967 | # get section index | ||
| 7968 | $sec = unpack('N', substr($data, $ix + 4, 4)); | ||
| 7969 | |||
| 7970 | # verify section index is correct | ||
| 7971 | ($sec == $s) || return('failed reading section index from Monaco DEVD tag'); | ||
| 7972 | |||
| 7973 | # advance index | ||
| 7974 | $ix += 8; | ||
| 7975 | |||
| 7976 | # verify tag 0x002A (fixed device values) | ||
| 7977 | (substr($data, $ix, 4) eq pack('N', 0x002A)) || return('failed reading fixed device values from Monaco DEVD tag'); | ||
| 7978 | |||
| 7979 | # if fixed device values (none for RGB) | ||
| 7980 | if ($fix) { | ||
| 7981 | |||
| 7982 | # get fixed device values (black plus any extra colors, e.g. orange or green) | ||
| 7983 | @devfix = unpack("d$fix", $big ? substr($data, $ix + 4, 8 * $fix) : reverse(substr($data, $ix + 4, 8 * $fix))); | ||
| 7984 | |||
| 7985 | # reverse array if little-endian | ||
| 7986 | @devfix = reverse(@devfix) if (! $big); | ||
| 7987 | |||
| 7988 | # apply multiplier | ||
| 7989 | @devfix = map {$_ * $mult} @devfix; | ||
| 7990 | |||
| 7991 | } | ||
| 7992 | |||
| 7993 | # advance index | ||
| 7994 | $ix += 8 * $fix + 4; | ||
| 7995 | |||
| 7996 | # verify tag 0x002B (step counts by color) | ||
| 7997 | (substr($data, $ix, 4) eq pack('N', 0x002B)) || return('failed reading step counts from Monaco DEVD tag'); | ||
| 7998 | |||
| 7999 | # get device step counts | ||
| 8000 | @nd = unpack('N3', substr($data, $ix + 4, 12)); | ||
| 8001 | |||
| 8002 | # get total number of steps | ||
| 8003 | $nt = $nd[0] + $nd[1] + $nd[2]; | ||
| 8004 | |||
| 8005 | # advance index | ||
| 8006 | $ix += 16; | ||
| 8007 | |||
| 8008 | # verify tag 0x002C (step values by color) | ||
| 8009 | (substr($data, $ix, 4) eq pack('N', 0x002C)) || return('failed reading step values from Monaco DEVD tag'); | ||
| 8010 | |||
| 8011 | # get step values | ||
| 8012 | @devstep = unpack("d$nt", $big ? substr($data, $ix + 4, 8 * $nt) : reverse(substr($data, $ix + 4, 8 * $nt))); | ||
| 8013 | |||
| 8014 | # reverse array if little-endian | ||
| 8015 | @devstep = reverse(@devstep) if (! $big); | ||
| 8016 | |||
| 8017 | # apply multiplier | ||
| 8018 | @devstep = map {$_ * $mult} @devstep; | ||
| 8019 | |||
| 8020 | # advance index | ||
| 8021 | $ix += 8 * $nt + 4; | ||
| 8022 | |||
| 8023 | # initialize arrays | ||
| 8024 | @dev = (); | ||
| 8025 | @sum = (); | ||
| 8026 | @temp = (); | ||
| 8027 | |||
| 8028 | # if RGB colorspace | ||
| 8029 | if ($cs eq 'RGB ') { | ||
| 8030 | |||
| 8031 | # for each blue step | ||
| 8032 | for my $i (0 .. $nd[2] - 1) { | ||
| 8033 | |||
| 8034 | # for each green step | ||
| 8035 | for my $j (0 .. $nd[1] - 1) { | ||
| 8036 | |||
| 8037 | # for each red step | ||
| 8038 | for my $k (0 .. $nd[0] - 1) { | ||
| 8039 | |||
| 8040 | # save RGB values | ||
| 8041 | push(@dev, $devstep[$k], $devstep[$j + $nd[0]], $devstep[$i + $nd[0] + $nd[1]]); | ||
| 8042 | |||
| 8043 | } | ||
| 8044 | |||
| 8045 | } | ||
| 8046 | |||
| 8047 | } | ||
| 8048 | |||
| 8049 | # if CMYK or NCLR colorspace | ||
| 8050 | } else { | ||
| 8051 | |||
| 8052 | # for each yellow step | ||
| 8053 | for my $i (0 .. $nd[2] - 1) { | ||
| 8054 | |||
| 8055 | # for each cyan step | ||
| 8056 | for my $j (0 .. $nd[0] - 1) { | ||
| 8057 | |||
| 8058 | # for each magenta step | ||
| 8059 | for my $k (0 .. $nd[1] - 1) { | ||
| 8060 | |||
| 8061 | # get CMY values | ||
| 8062 | @cmy = ($devstep[$j], $devstep[$k + $nd[0]], $devstep[$i + $nd[0] + $nd[1]]); | ||
| 8063 | |||
| 8064 | # save CMY values | ||
| 8065 | push(@temp, [@cmy]); | ||
| 8066 | |||
| 8067 | # save total ink value | ||
| 8068 | push(@sum, List::Util::sum(@cmy, @devfix)); | ||
| 8069 | |||
| 8070 | } | ||
| 8071 | |||
| 8072 | } | ||
| 8073 | |||
| 8074 | } | ||
| 8075 | |||
| 8076 | # initialize actual ink limit | ||
| 8077 | $limit = $nc * 100; | ||
| 8078 | |||
| 8079 | # find actual ink limit (smallest value greater than TAC) | ||
| 8080 | for (@sum) {$limit = $_ if ($_ > $tac && $_ < $limit)}; | ||
| 8081 | |||
| 8082 | # for each sample | ||
| 8083 | for my $i (0 .. $#sum) { | ||
| 8084 | |||
| 8085 | # get cmy values | ||
| 8086 | @cmy = @{$temp[$i]}; | ||
| 8087 | |||
| 8088 | # if sample within ink limit, or a corner point | ||
| 8089 | if ($sum[$i] <= $limit || ((0 < grep {$_ == 0} @cmy) && (0 < grep {$_ == 100} @cmy))) { | ||
| 8090 | |||
| 8091 | # copy cmy values | ||
| 8092 | push(@dev, @cmy); | ||
| 8093 | |||
| 8094 | } | ||
| 8095 | |||
| 8096 | } | ||
| 8097 | |||
| 8098 | } | ||
| 8099 | |||
| 8100 | # verify tag 0x0032 (L*a*b* sample data) | ||
| 8101 | (substr($data, $ix, 4) eq pack('N', 0x0032)) || return('failed reading L*a*b* data from Monaco DEVD tag'); | ||
| 8102 | |||
| 8103 | # get number of values | ||
| 8104 | $m = unpack('N', substr($data, $ix + 4, 4)) * 3; | ||
| 8105 | |||
| 8106 | # get L*a*b* color data | ||
| 8107 | @dat = unpack("d$m", $big ? substr($data, $ix + 8, 8 * $m) : reverse(substr($data, $ix + 8, 8 * $m))); | ||
| 8108 | |||
| 8109 | # reverse array if little-endian | ||
| 8110 | @dat = reverse(@dat) if (! $big); | ||
| 8111 | |||
| 8112 | # advance index | ||
| 8113 | $ix += 8 * $m + 8; | ||
| 8114 | |||
| 8115 | # verify @dev and @dat are same size | ||
| 8116 | (scalar(@dev) == scalar(@dat)) || return('failed comparing data counts of Monaco DEVD tag'); | ||
| 8117 | |||
| 8118 | # for each sample (3 values per sample) | ||
| 8119 | for my $i (0 .. ($m/3 - 1)) { | ||
| 8120 | |||
| 8121 | # add sample data to object | ||
| 8122 | push (@{$self->[1]}, [@dev[($i * 3) .. ($i * 3 + 2)], @devfix, @dat[($i * 3) .. ($i * 3 + 2)]]); | ||
| 8123 | |||
| 8124 | } | ||
| 8125 | |||
| 8126 | # verify tag 0x0029 (end of section) | ||
| 8127 | (substr($data, $ix, 4) eq pack('N', 0x0029)) || return('failed reading section end from Monaco DEVD tag'); | ||
| 8128 | |||
| 8129 | # advance index | ||
| 8130 | $ix += 4; | ||
| 8131 | |||
| 8132 | } | ||
| 8133 | |||
| 8134 | # verify tag 0x0030 (end of data) | ||
| 8135 | (substr($data, $ix, 4) eq pack('N', 0x0030)) || return('failed reading data end from Monaco DEVD tag'); | ||
| 8136 | |||
| 8137 | # add 'CREATED' keyword/value from header date/time | ||
| 8138 | push(@{$self->[3]}, ['CREATED', sprintf('%.4d-%.2d-%.2dT%.2d:%.2d:%.2dZ', @{$header}[6 .. 11])]); | ||
| 8139 | |||
| 8140 | # return | ||
| 8141 | return(); | ||
| 8142 | |||
| 8143 | } | ||
| 8144 | |||
| 8145 | # read chart from SpectraShop (.ss3) file | ||
| 8146 | # parameters: (object_reference, file_handle, hash) | ||
| 8147 | sub _readChartSS3 { | ||
| 8148 | |||
| 8149 | # get parameters | ||
| 8150 | my ($self, $fh, $hash) = @_; | ||
| 8151 | |||
| 8152 | # local variables | ||
| 8153 | my (%fmt, $buf, @data, $notes); | ||
| 8154 | my ($meta, $measure, %tally, @keys, $value, $nm); | ||
| 8155 | |||
| 8156 | # metadata format array (v32) | ||
| 8157 | $fmt{'32'} = [ | ||
| 8158 | [qw(Identifier_1 SAMPLE_NAME P)], | ||
| 8159 | [qw(Identifier_2 SAMPLE_ID2 P)], | ||
| 8160 | [qw(Identifier_3 SAMPLE_ID3 P)], | ||
| 8161 | [qw(Material MATERIAL P)], | ||
| 8162 | [qw(Manufacturer MANUFACTURER P)], | ||
| 8163 | [qw(Model MODEL P)], | ||
| 8164 | [qw(Serial_Number SERIAL_NUMBER P)], | ||
| 8165 | [qw(Production_Date PROD_DATE P)], | ||
| 8166 | [qw(Surface SURFACE P)], | ||
| 8167 | [qw(Originator ORIGINATOR P)], | ||
| 8168 | [qw(Creation_Date CREATED P)], | ||
| 8169 | [qw(Comments NOTE P)], | ||
| 8170 | [qw(Instrument INSTRUMENTATION P)], | ||
| 8171 | [qw(Spectrum_Type SPECTRUM_TYPE n), [qw(Emissive-light Emissive-monitor Observer Reflective Transmissive)]], | ||
| 8172 | [qw(Filter MEASUREMENT_FILTER P)], | ||
| 8173 | [qw(Geometry MEASUREMENT_GEOMETRY P)], | ||
| 8174 | [qw(Aperture MEASUREMENT_APERTURE P)], | ||
| 8175 | [qw(Data_Reference DATA_REFERENCE P)], | ||
| 8176 | [qw(Illuminant MEASUREMENT_SOURCE P)], | ||
| 8177 | [qw(Backing SAMPLE_BACKING P)], | ||
| 8178 | [qw(Measurements NSAMPLES n)], | ||
| 8179 | [qw(Notes ACQUIRE_NOTE P)], | ||
| 8180 | ]; | ||
| 8181 | |||
| 8182 | # metadata format array (v50) | ||
| 8183 | $fmt{'50'} = [ | ||
| 8184 | [qw(Identifier_1 SAMPLE_NAME P)], | ||
| 8185 | [qw(Identifier_2 SAMPLE_ID2 P)], | ||
| 8186 | [qw(Identifier_3 SAMPLE_ID3 P)], | ||
| 8187 | [qw(Material MATERIAL P)], | ||
| 8188 | [qw(Manufacturer MANUFACTURER P)], | ||
| 8189 | [qw(Model MODEL P)], | ||
| 8190 | [qw(Serial_Number SERIAL_NUMBER P)], | ||
| 8191 | [qw(Production_Date PROD_DATE P)], | ||
| 8192 | [qw(Surface SURFACE P)], | ||
| 8193 | [qw(Originator ORIGINATOR P)], | ||
| 8194 | [qw(Creation_Date CREATED P)], | ||
| 8195 | [qw(Comments NOTE P)], | ||
| 8196 | [qw(Instrument INSTRUMENTATION P)], | ||
| 8197 | [qw(Serial_Number INSTRUMENT_SERIAL_NUMBER P)], | ||
| 8198 | [qw(Spectrum_Type SPECTRUM_TYPE n), [qw(Emissive-light Emissive-monitor Observer Reflective Transmissive)]], | ||
| 8199 | [qw(Filter MEASUREMENT_FILTER P)], | ||
| 8200 | [qw(Geometry MEASUREMENT_GEOMETRY P)], | ||
| 8201 | [qw(Aperture MEASUREMENT_APERTURE P)], | ||
| 8202 | [qw(Data_Reference DATA_REFERENCE P)], | ||
| 8203 | [qw(Illuminant MEASUREMENT_SOURCE P)], | ||
| 8204 | [qw(Backing SAMPLE_BACKING P)], | ||
| 8205 | [qw(Measurements NSAMPLES n)], | ||
| 8206 | [qw(Notes ACQUIRE_NOTE P)], | ||
| 8207 | ]; | ||
| 8208 | |||
| 8209 | # read version, samples, Collection Notes length | ||
| 8210 | read($fh, $buf, 7); | ||
| 8211 | |||
| 8212 | # unpack | ||
| 8213 | @data = unpack('nx2nC', $buf); | ||
| 8214 | |||
| 8215 | # read Collection Notes string | ||
| 8216 | read($fh, $notes, $data[2]); | ||
| 8217 | |||
| 8218 | # for each sample | ||
| 8219 | for my $i (0 .. ($data[1] - 1)) { | ||
| 8220 | |||
| 8221 | # for each metadata field | ||
| 8222 | for my $j (0 .. $#{$fmt{$data[0]}}) { | ||
| 8223 | |||
| 8224 | # if a Pascal string | ||
| 8225 | if ($fmt{$data[0]}[$j][2] eq 'P') { | ||
| 8226 | |||
| 8227 | # read string length | ||
| 8228 | read($fh, $buf, 1); | ||
| 8229 | |||
| 8230 | # read string | ||
| 8231 | read($fh, $meta->[$i][$j], unpack('C', $buf)); | ||
| 8232 | |||
| 8233 | # if an unsigned short integer | ||
| 8234 | } elsif ($fmt{$data[0]}[$j][2] eq 'n') { | ||
| 8235 | |||
| 8236 | # read short integer | ||
| 8237 | read($fh, $buf, 2); | ||
| 8238 | |||
| 8239 | # unpack | ||
| 8240 | $meta->[$i][$j] = unpack('n', $buf); | ||
| 8241 | |||
| 8242 | } | ||
| 8243 | |||
| 8244 | } | ||
| 8245 | |||
| 8246 | # read wavelength parameters (start, end, increment, count) | ||
| 8247 | read($fh, $buf, 8); | ||
| 8248 | |||
| 8249 | # unpack (unsigned short integer) | ||
| 8250 | $measure->[$i][0] = [unpack('n4', $buf)]; | ||
| 8251 | |||
| 8252 | # for each wavelength | ||
| 8253 | for my $j (1 .. $measure->[$i][0][3]) { | ||
| 8254 | |||
| 8255 | # read measurements (avg, low, high, std_dev) | ||
| 8256 | read($fh, $buf, 16); | ||
| 8257 | |||
| 8258 | # unpack (32-bit float, big endian) | ||
| 8259 | $measure->[$i][$j] = [unpack('(f4)>', $buf)]; | ||
| 8260 | |||
| 8261 | } | ||
| 8262 | |||
| 8263 | } | ||
| 8264 | |||
| 8265 | # add Collection Notes to header line array, if not null string | ||
| 8266 | push(@{$self->[3]}, ['FILE_DESCRIPTOR', "\"$notes\""]) if (length($notes)); | ||
| 8267 | |||
| 8268 | # for each metadata field | ||
| 8269 | for my $j (0 .. $#{$meta->[0]}) { | ||
| 8270 | |||
| 8271 | # init hash | ||
| 8272 | %tally = (); | ||
| 8273 | |||
| 8274 | # for each sample | ||
| 8275 | for my $i (0 .. $#{$meta}) { | ||
| 8276 | |||
| 8277 | # increment hash value | ||
| 8278 | $tally{$meta->[$i][$j]}++; | ||
| 8279 | |||
| 8280 | } | ||
| 8281 | |||
| 8282 | # get hash keys | ||
| 8283 | @keys = keys(%tally); | ||
| 8284 | |||
| 8285 | # if one hash key | ||
| 8286 | if (@keys == 1) { | ||
| 8287 | |||
| 8288 | # if not the null string | ||
| 8289 | if (length($keys[0])) { | ||
| 8290 | |||
| 8291 | # if value is string | ||
| 8292 | if ($fmt{$data[0]}[$j][2] eq 'P') { | ||
| 8293 | |||
| 8294 | # wrap in quotes | ||
| 8295 | $value = "\"$keys[0]\""; | ||
| 8296 | |||
| 8297 | } else { | ||
| 8298 | |||
| 8299 | # if value is an enumeration | ||
| 8300 | if (defined($fmt{$data[0]}[$j][3])) { | ||
| 8301 | |||
| 8302 | # look up enumerated value and wrap in quotes | ||
| 8303 | $value = "\"$fmt{$data[0]}[$j][3][$keys[0]]\""; | ||
| 8304 | |||
| 8305 | } else { | ||
| 8306 | |||
| 8307 | # use value as-is | ||
| 8308 | $value = $keys[0]; | ||
| 8309 | |||
| 8310 | } | ||
| 8311 | |||
| 8312 | } | ||
| 8313 | |||
| 8314 | # add KEYWORD/VALUE to header line array | ||
| 8315 | push(@{$self->[3]}, [$fmt{$data[0]}[$j][1], $value]); | ||
| 8316 | |||
| 8317 | } | ||
| 8318 | |||
| 8319 | } else { | ||
| 8320 | |||
| 8321 | # add keyword to DATA_FORMAT array | ||
| 8322 | push(@{$self->[1][0]}, $fmt{$data[0]}[$j][1]); | ||
| 8323 | |||
| 8324 | # for each sample | ||
| 8325 | for my $i (0 .. $#{$meta}) { | ||
| 8326 | |||
| 8327 | # if value is an enumeration | ||
| 8328 | if (defined($fmt{$data[0]}[$j][3])) { | ||
| 8329 | |||
| 8330 | # look up enumerated value | ||
| 8331 | $value = $fmt{$data[0]}[$j][3][$meta->[$i][$j]]; | ||
| 8332 | |||
| 8333 | } else { | ||
| 8334 | |||
| 8335 | # use value as-is | ||
| 8336 | $value = $meta->[$i][$j]; | ||
| 8337 | |||
| 8338 | } | ||
| 8339 | |||
| 8340 | # add value to DATA array | ||
| 8341 | push(@{$self->[1][$i + 1]}, $meta->[$i][$j]); | ||
| 8342 | |||
| 8343 | } | ||
| 8344 | |||
| 8345 | } | ||
| 8346 | |||
| 8347 | } | ||
| 8348 | |||
| 8349 | # for each wavelength parameter (start, end, increment, count) | ||
| 8350 | for my $j (0 .. 3) { | ||
| 8351 | |||
| 8352 | # init hash | ||
| 8353 | %tally = (); | ||
| 8354 | |||
| 8355 | # for each sample | ||
| 8356 | for my $i (0 .. $#{$measure}) { | ||
| 8357 | |||
| 8358 | # increment hash value | ||
| 8359 | $tally{$measure->[$i][0][$j]}++; | ||
| 8360 | |||
| 8361 | } | ||
| 8362 | |||
| 8363 | # get hash keys | ||
| 8364 | @keys = keys(%tally); | ||
| 8365 | |||
| 8366 | # verify all samples have same wavelength parameter value | ||
| 8367 | (@keys == 1) || return('samples have varied spectral range'); | ||
| 8368 | |||
| 8369 | } | ||
| 8370 | |||
| 8371 | # for each wavelength | ||
| 8372 | for my $j (0 .. ($#{$measure->[0]} - 1)) { | ||
| 8373 | |||
| 8374 | # compute wavelength from start and increment values | ||
| 8375 | $nm = $measure->[0][0][0] + $j * $measure->[0][0][2]; | ||
| 8376 | |||
| 8377 | # add keyword to DATA_FORMAT array | ||
| 8378 | push(@{$self->[1][0]}, "nm$nm"); | ||
| 8379 | |||
| 8380 | # for each sample | ||
| 8381 | for my $i (0 .. $#{$measure}) { | ||
| 8382 | |||
| 8383 | # add average measurement to DATA array | ||
| 8384 | push(@{$self->[1][$i + 1]}, $measure->[$i][$j + 1][0]); | ||
| 8385 | |||
| 8386 | } | ||
| 8387 | |||
| 8388 | } | ||
| 8389 | |||
| 8390 | # return | ||
| 8391 | return(); | ||
| 8392 | |||
| 8393 | } | ||
| 8394 | |||
| 8395 | # read data from TIFF file | ||
| 8396 | # RGB, CMYK, and CIE L*a*b* color spaces supported | ||
| 8397 | # 8-bit, 16-bit or 32-bit, Intel or Motorola byte order supported | ||
| 8398 | # alpha and spot channels in RGB and CMYK files supported | ||
| 8399 | # optional hash keys: 'rows', 'columns', 'crop', 'ratio', 'aperture', 'udf', 'format' | ||
| 8400 | # default 'rows' and 'columns' are taken from image size, default 'ratio' is 0.5 | ||
| 8401 | # 'crop' is an array containing the left, right, upper and lower crop values in pixels | ||
| 8402 | # 'ratio' is a value between 0 and 1, sample is a single pixel when 'ratio' is 0 | ||
| 8403 | # 'aperture' is in millimeters, and take precedence over 'ratio' | ||
| 8404 | # 'udf' is a code reference to a pixel processing function | ||
| 8405 | # 'format' is an array reference containing the format fields | ||
| 8406 | # parameters: (object_reference, file_handle, hash) | ||
| 8407 | # returns: (result) | ||
| 8408 | sub _readChartTIFF { | ||
| 8409 | |||
| 8410 | # get parameters | ||
| 8411 | my ($self, $fh, $hash) = @_; | ||
| 8412 | |||
| 8413 | # local variables | ||
| 8414 | my ($buf, $short, $long, $fp, @header, $tags); | ||
| 8415 | my ($cols, $rows, $bits, $pi, $samples); | ||
| 8416 | my ($context, $fmt, $upf, $udf, $dev, $div, $dab); | ||
| 8417 | my ($trows, $tcols, $crop, $roff, $coff); | ||
| 8418 | my ($res, $size, $frac, $ratio, $rxo, $cxo, $pixels, $width); | ||
| 8419 | my ($lower, $upper, $left, $right, $band, $pval, @data, @pix); | ||
| 8420 | |||
| 8421 | # read the header | ||
| 8422 | read($fh, $buf, 8); | ||
| 8423 | |||
| 8424 | # if big-endian (Motorola) | ||
| 8425 | if (substr($buf, 0, 2) eq 'MM') { | ||
| 8426 | |||
| 8427 | # set 'unpack' formats | ||
| 8428 | $short = 'n'; | ||
| 8429 | $long = 'N'; | ||
| 8430 | $fp = 'f>'; # might not be IEEE FP on some platforms | ||
| 8431 | |||
| 8432 | # if little-endian (Intel) | ||
| 8433 | } elsif (substr($buf, 0, 2) eq 'II'){ | ||
| 8434 | |||
| 8435 | # set 'unpack' formats | ||
| 8436 | $short = 'v'; | ||
| 8437 | $long = 'V'; | ||
| 8438 | $fp = 'f<'; # might not be IEEE FP on some platforms | ||
| 8439 | |||
| 8440 | } else { | ||
| 8441 | |||
| 8442 | # error | ||
| 8443 | return('TIFF byte order incorrect'); | ||
| 8444 | |||
| 8445 | } | ||
| 8446 | |||
| 8447 | # unpack the header | ||
| 8448 | @header = unpack("A2 $short $long", $buf); | ||
| 8449 | |||
| 8450 | # verify file signature | ||
| 8451 | ($header[1] == 42) || return('TIFF file signature incorrect'); | ||
| 8452 | |||
| 8453 | # read TIFF image file directory (IFD) | ||
| 8454 | $tags = _readTIFFdir($fh, $header[2], $short, $long); | ||
| 8455 | |||
| 8456 | # verify compression (1 = uncompressed) | ||
| 8457 | ($tags->{'259'}[0] == 1) || return('TIFF compression unsupported'); | ||
| 8458 | |||
| 8459 | # verify orientation (1 = normal) | ||
| 8460 | (! exists($tags->{'274'}) || $tags->{'274'}[0] == 1) || warn('TIFF orientation rotated and/or flipped'); | ||
| 8461 | |||
| 8462 | # verify planar configuration (1 = chunky) | ||
| 8463 | (! exists($tags->{'284'}) || $tags->{'284'}[0] == 1) || return('TIFF planar configuration unsupported'); | ||
| 8464 | |||
| 8465 | # verify not tiled | ||
| 8466 | (! exists($tags->{'322'})) || return('TIFF tiled layout unsupported'); | ||
| 8467 | |||
| 8468 | # get TIFF columns (width) | ||
| 8469 | $cols = $tags->{'256'}[0]; | ||
| 8470 | |||
| 8471 | # get TIFF rows (length) | ||
| 8472 | $rows = $tags->{'257'}[0]; | ||
| 8473 | |||
| 8474 | # get TIFF bits per sample | ||
| 8475 | $bits = $tags->{'258'}[0]; | ||
| 8476 | |||
| 8477 | # verify bits per sample | ||
| 8478 | ($bits == 8 || $bits == 16 || $bits == 32) || return('TIFF bits per sample unsupported'); | ||
| 8479 | |||
| 8480 | # get the photometric interpretation | ||
| 8481 | $pi = $tags->{'262'}[0]; | ||
| 8482 | |||
| 8483 | # if 32-bits per sample | ||
| 8484 | if ($bits == 32) { | ||
| 8485 | |||
| 8486 | # verify 32-bit IEEE FP format, RGB image | ||
| 8487 | ($tags->{'339'}[0] == 3 && $pi == 2) || return('TIFF format unsupported'); | ||
| 8488 | |||
| 8489 | } | ||
| 8490 | |||
| 8491 | # get TIFF samples per pixel | ||
| 8492 | $samples = $tags->{'277'}[0]; | ||
| 8493 | |||
| 8494 | # verify bits per sample array | ||
| 8495 | ($samples == grep {$_ == $bits} @{$tags->{'258'}}) || return('TIFF image structure unsupported'); | ||
| 8496 | |||
| 8497 | # get context (if any) | ||
| 8498 | $context = $hash->{'context'}; | ||
| 8499 | |||
| 8500 | # get user defined function (if any) | ||
| 8501 | $udf = $hash->{'udf'}; | ||
| 8502 | |||
| 8503 | # verify UDF is a code reference | ||
| 8504 | (ref($udf) eq 'CODE') || return('UDF not a code reference') if (defined($udf)); | ||
| 8505 | |||
| 8506 | # set device value divisor | ||
| 8507 | $dev = ($bits == 8) ? 255 : 65535; | ||
| 8508 | |||
| 8509 | # add fields for udf (if any) | ||
| 8510 | $fmt = add_fmt($self, map {defined($context) ? "$context|$_" : $_} @{$hash->{'format'}}) if defined($hash->{'format'}); | ||
| 8511 | |||
| 8512 | # if RGB file | ||
| 8513 | if ($pi == 2 && $samples < 13) { | ||
| 8514 | |||
| 8515 | # add RGB and ALPHA fields, if not already defined | ||
| 8516 | $fmt = add_fmt($self, map {defined($context) ? "$context|$_" : $_} (qw(RGB_R RGB_G RGB_B), map {"RGB_A$_"} (1 .. $samples - 3))) if (! defined($fmt)); | ||
| 8517 | |||
| 8518 | # set unpack format (8, 16 or 32 bits) | ||
| 8519 | $upf = ($bits == 8) ? 'C*' : ($bits == 16) ? "$short*" : "$fp*"; | ||
| 8520 | |||
| 8521 | # set divisor (8, 16 or 32 bits) | ||
| 8522 | $div = ($bits == 8) ? 1 : ($bits == 16) ? 257 : 1/255; | ||
| 8523 | |||
| 8524 | # if CMYK file | ||
| 8525 | } elsif ($pi == 5 && $samples == 4) { | ||
| 8526 | |||
| 8527 | # add CMYK fields, if not already defined | ||
| 8528 | $fmt = add_fmt($self, map {defined($context) ? "$context|$_" : $_} qw(CMYK_C CMYK_M CMYK_Y CMYK_K)) if (! defined($fmt)); | ||
| 8529 | |||
| 8530 | # set unpack format (8 or 16 bits) | ||
| 8531 | $upf = ($bits == 8) ? 'C*' : "$short*"; | ||
| 8532 | |||
| 8533 | # set divisor (8 or 16 bits) | ||
| 8534 | $div = ($bits == 8) ? 2.55 : 655.35; | ||
| 8535 | |||
| 8536 | # if nCLR file | ||
| 8537 | } elsif ($pi == 5 && $samples > 4 && $samples < 16) { | ||
| 8538 | |||
| 8539 | # add nCLR fields, if not already defined | ||
| 8540 | $fmt = add_fmt($self, map {defined($context) ? "$context|$_" : $_} map {sprintf('%xCLR_%x', $samples, $_)} (1 .. $samples)) if (! defined($fmt)); | ||
| 8541 | |||
| 8542 | # set unpack format (8 or 16 bits) | ||
| 8543 | $upf = ($bits == 8) ? 'C*' : "$short*"; | ||
| 8544 | |||
| 8545 | # set divisor (8 or 16 bits) | ||
| 8546 | $div = ($bits == 8) ? 2.55 : 655.35; | ||
| 8547 | |||
| 8548 | # if CIE L*a*b* file | ||
| 8549 | } elsif ($pi == 8 && $samples == 3) { | ||
| 8550 | |||
| 8551 | # add L*a*b* fields, if not already defined | ||
| 8552 | $fmt = add_fmt($self, map {defined($context) ? "$context|$_" : $_} qw(LAB_L LAB_A LAB_B)) if (! defined($fmt)); | ||
| 8553 | |||
| 8554 | # set unpack format (8 or 16 bits) | ||
| 8555 | $upf = ($bits == 8) ? '(Ccc)*' : "$short*"; | ||
| 8556 | |||
| 8557 | # set divisors (8 or 16 bits) | ||
| 8558 | $div = ($bits == 8) ? 2.55 : 655.35; # L* | ||
| 8559 | $dab = ($bits == 8) ? 1 : 256; # a* and b* | ||
| 8560 | |||
| 8561 | } else { | ||
| 8562 | |||
| 8563 | # return error | ||
| 8564 | return('TIFF color space unsupported'); | ||
| 8565 | |||
| 8566 | } | ||
| 8567 | |||
| 8568 | # get target rows (could be undefined) | ||
| 8569 | $trows = $hash->{'rows'}; | ||
| 8570 | |||
| 8571 | # get target columns (could be undefined) | ||
| 8572 | $tcols = $hash->{'columns'}; | ||
| 8573 | |||
| 8574 | # if 'crop' parameter is defined | ||
| 8575 | if (defined($hash->{'crop'})) { | ||
| 8576 | |||
| 8577 | # get crop parameter | ||
| 8578 | $crop = $hash->{'crop'}; | ||
| 8579 | |||
| 8580 | # verify array reference | ||
| 8581 | (ref($crop) eq 'ARRAY') || return('TIFF crop parameter not an array reference'); | ||
| 8582 | |||
| 8583 | # verify array contains four non-negative integers | ||
| 8584 | (4 == @{$crop} && 4 == grep {$_ == int($_) && $_ >= 0} @{$crop}) || return('TIFF crop parameter(s) invalid'); | ||
| 8585 | |||
| 8586 | # adjust rows and columns | ||
| 8587 | $rows -= $crop->[2] + $crop->[3]; | ||
| 8588 | $cols -= $crop->[0] + $crop->[1]; | ||
| 8589 | |||
| 8590 | # verify cropped size | ||
| 8591 | ($rows > 0 && $cols > 0) || return('TIFF crop size too small'); | ||
| 8592 | |||
| 8593 | # set offset values | ||
| 8594 | $roff = $crop->[2]; | ||
| 8595 | $coff = $crop->[0]; | ||
| 8596 | |||
| 8597 | } else { | ||
| 8598 | |||
| 8599 | # set offset values | ||
| 8600 | $roff = 0; | ||
| 8601 | $coff = 0; | ||
| 8602 | |||
| 8603 | } | ||
| 8604 | |||
| 8605 | # if aperture is defined in hash | ||
| 8606 | if (defined($hash->{'aperture'})) { | ||
| 8607 | |||
| 8608 | # compute image resolution | ||
| 8609 | $res = $tags->{'283'}[0]/$tags->{'283'}[1]; | ||
| 8610 | |||
| 8611 | # convert to lines/mm if resolution unit is inch | ||
| 8612 | $res /= 25.4 if ($tags->{'296'}[0] == 2); | ||
| 8613 | |||
| 8614 | # convert to lines/mm if resolution unit is cm | ||
| 8615 | $res /= 10 if ($tags->{'296'}[0] == 3); | ||
| 8616 | |||
| 8617 | # if target rows or target columns are defined | ||
| 8618 | if (defined($trows) || defined($tcols)) { | ||
| 8619 | |||
| 8620 | # use image rows if target rows undefined | ||
| 8621 | $trows = $rows if (! defined($trows)); | ||
| 8622 | |||
| 8623 | # use image columns if target columns undefined | ||
| 8624 | $tcols = $cols if (! defined($tcols)); | ||
| 8625 | |||
| 8626 | # compute aperture size (diameter in pixels) | ||
| 8627 | ($frac, $size) = POSIX::modf(sqrt(ICC::Shared::PI/4) * $res * $hash->{'aperture'}); | ||
| 8628 | |||
| 8629 | # if fractional part < 0.25 | ||
| 8630 | if ($frac < 0.25) { | ||
| 8631 | |||
| 8632 | # set row and column index offsets | ||
| 8633 | $rxo = $cxo = $size - 1; | ||
| 8634 | |||
| 8635 | # if fractional part < 0.75 | ||
| 8636 | } elsif ($frac < 0.75) { | ||
| 8637 | |||
| 8638 | # set row index offset | ||
| 8639 | $rxo = $size - 1; | ||
| 8640 | |||
| 8641 | # set column index offset | ||
| 8642 | $cxo = $size; | ||
| 8643 | |||
| 8644 | } else { | ||
| 8645 | |||
| 8646 | # set row and column index offsets | ||
| 8647 | $rxo = $cxo = $size; | ||
| 8648 | |||
| 8649 | } | ||
| 8650 | |||
| 8651 | # verify aperture is within sample area | ||
| 8652 | ($rxo <= $rows/$trows && $cxo <= $cols/$tcols) || croak('TIFF aperture exceeds sample area') | ||
| 8653 | |||
| 8654 | } else { | ||
| 8655 | |||
| 8656 | # compute aperture area (in pixels) | ||
| 8657 | $size = ICC::Shared::PI * ($res * $hash->{'aperture'}/2)**2; | ||
| 8658 | |||
| 8659 | # compute the target rows | ||
| 8660 | $trows = int(sqrt($size * $rows/$cols) + 0.5); | ||
| 8661 | |||
| 8662 | # compute the target columns | ||
| 8663 | $tcols = int($size/$trows + 0.5); | ||
| 8664 | |||
| 8665 | # set row and column indices (single pixel sample) | ||
| 8666 | $rxo = $cxo = 0; | ||
| 8667 | |||
| 8668 | } | ||
| 8669 | |||
| 8670 | } else { | ||
| 8671 | |||
| 8672 | # use image rows if target rows undefined | ||
| 8673 | $trows = $rows if (! defined($trows)); | ||
| 8674 | |||
| 8675 | # use image columns if target columns undefined | ||
| 8676 | $tcols = $cols if (! defined($tcols)); | ||
| 8677 | |||
| 8678 | # get mask ratio (default 0.5) | ||
| 8679 | $ratio = defined($hash->{'ratio'}) ? $hash->{'ratio'} : 0.5; | ||
| 8680 | |||
| 8681 | # verify mask ratio | ||
| 8682 | ($ratio >= 0 && $ratio <= 1) || croak('TIFF mask ratio < 0 or > 1'); | ||
| 8683 | |||
| 8684 | # compute row index offset | ||
| 8685 | $rxo = int($ratio * $rows/$trows - 0.5); | ||
| 8686 | |||
| 8687 | # compute column index offset | ||
| 8688 | $cxo = int($ratio * $cols/$tcols - 0.5); | ||
| 8689 | |||
| 8690 | } | ||
| 8691 | |||
| 8692 | # warn if large target size | ||
| 8693 | ($trows * $tcols <= 10000) || warn('TIFF target size > 10000 samples'); | ||
| 8694 | |||
| 8695 | # compute number of pixels | ||
| 8696 | $pixels = ($rxo + 1) * ($cxo + 1); | ||
| 8697 | |||
| 8698 | # compute row width (bytes) | ||
| 8699 | $width = $tags->{'256'}[0] * List::Util::sum(@{$tags->{'258'}})/8; | ||
| 8700 | |||
| 8701 | # for each target row | ||
| 8702 | for my $i (0 .. $trows - 1) { | ||
| 8703 | |||
| 8704 | # compute sample lower row | ||
| 8705 | $lower = int(($i + 0.5) * $rows/$trows - $rxo/2) + $roff; | ||
| 8706 | |||
| 8707 | # compute sample upper row | ||
| 8708 | $upper = $lower + $rxo; | ||
| 8709 | |||
| 8710 | # get sample band data | ||
| 8711 | $band = _readTIFFband($fh, $tags, $lower, $upper, $width, $upf); | ||
| 8712 | |||
| 8713 | # for each target column | ||
| 8714 | for my $j (0 .. $tcols - 1) { | ||
| 8715 | |||
| 8716 | # compute sample left column | ||
| 8717 | $left = int(($j + 0.5) * $cols/$tcols - $cxo/2) + $coff; | ||
| 8718 | |||
| 8719 | # compute sample right column | ||
| 8720 | $right = $left + $cxo; | ||
| 8721 | |||
| 8722 | # initialize data | ||
| 8723 | @data = (); | ||
| 8724 | |||
| 8725 | # for each row (band) | ||
| 8726 | for my $m (0 .. $#{$band}) { | ||
| 8727 | |||
| 8728 | # for each column | ||
| 8729 | for my $n ($left .. $right) { | ||
| 8730 | |||
| 8731 | # get pixel value (all samples) | ||
| 8732 | @pix = @{$band->[$m]}[$n * $samples .. ($n + 1) * $samples - 1]; | ||
| 8733 | |||
| 8734 | # if 16-bit L*a*b* | ||
| 8735 | if ($pi == 8 && $bits == 16) { | ||
| 8736 | |||
| 8737 | # adjust a* and b* if pixel value negative (signed 16-bit) | ||
| 8738 | $pix[1] += -65536 if ($pix[1] > 32767); | ||
| 8739 | $pix[2] += -65536 if ($pix[2] > 32767); | ||
| 8740 | |||
| 8741 | } | ||
| 8742 | |||
| 8743 | # if user defined function provided | ||
| 8744 | if (defined($udf)) { | ||
| 8745 | |||
| 8746 | # if L*a*b* file | ||
| 8747 | if ($pi == 8) { | ||
| 8748 | |||
| 8749 | # convert values | ||
| 8750 | $pix[0] /= $div; | ||
| 8751 | $pix[1] /= $dab; | ||
| 8752 | $pix[2] /= $dab; | ||
| 8753 | |||
| 8754 | } else { | ||
| 8755 | |||
| 8756 | # convert to device values | ||
| 8757 | @pix = map {$_/$dev} @pix; | ||
| 8758 | |||
| 8759 | # if a CMYK file | ||
| 8760 | if ($pi == 5) { | ||
| 8761 | |||
| 8762 | # for alpha/spot colors (if any) | ||
| 8763 | for my $s (4 .. $samples - 1) { | ||
| 8764 | |||
| 8765 | # invert device value | ||
| 8766 | $pix[$s] = 1 - $pix[$s]; | ||
| 8767 | |||
| 8768 | } | ||
| 8769 | |||
| 8770 | } | ||
| 8771 | |||
| 8772 | } | ||
| 8773 | |||
| 8774 | # call user defined function | ||
| 8775 | @pix = &$udf(@pix); | ||
| 8776 | |||
| 8777 | } | ||
| 8778 | |||
| 8779 | # for each channel (may be different from TIFF samples) | ||
| 8780 | for my $s (0 .. $#pix) { | ||
| 8781 | |||
| 8782 | # accumulate pixel values | ||
| 8783 | $data[$s] += $pix[$s] | ||
| 8784 | |||
| 8785 | } | ||
| 8786 | |||
| 8787 | } | ||
| 8788 | |||
| 8789 | } | ||
| 8790 | |||
| 8791 | # if user defined function provided | ||
| 8792 | if (defined($udf)) { | ||
| 8793 | |||
| 8794 | # save data in object | ||
| 8795 | @{$self->[1][$j * $trows + $i + 1]}[@{$fmt}] = map {$_/$pixels} @data; | ||
| 8796 | |||
| 8797 | # if L*a*b* file | ||
| 8798 | } elsif ($pi == 8) { | ||
| 8799 | |||
| 8800 | # save data in object | ||
| 8801 | $self->[1][$j * $trows + $i + 1][$fmt->[0]] = $data[0]/($pixels * $div); | ||
| 8802 | $self->[1][$j * $trows + $i + 1][$fmt->[1]] = $data[1]/($pixels * $dab); | ||
| 8803 | $self->[1][$j * $trows + $i + 1][$fmt->[2]] = $data[2]/($pixels * $dab); | ||
| 8804 | |||
| 8805 | # all others | ||
| 8806 | } else { | ||
| 8807 | |||
| 8808 | # normalize data values | ||
| 8809 | @data = map {$_/($pixels * $div)} @data; | ||
| 8810 | |||
| 8811 | # if a CMYK file | ||
| 8812 | if ($pi == 5) { | ||
| 8813 | |||
| 8814 | # for alpha/spot colors (if any) | ||
| 8815 | for my $s (4 .. $samples - 1) { | ||
| 8816 | |||
| 8817 | # invert %-dot value | ||
| 8818 | $data[$s] = 100 - $data[$s]; | ||
| 8819 | |||
| 8820 | } | ||
| 8821 | |||
| 8822 | } | ||
| 8823 | |||
| 8824 | # save data in object | ||
| 8825 | @{$self->[1][$j * $trows + $i + 1]}[@{$fmt}] = @data; | ||
| 8826 | |||
| 8827 | } | ||
| 8828 | |||
| 8829 | } | ||
| 8830 | |||
| 8831 | } | ||
| 8832 | |||
| 8833 | # save the tag hash in object header | ||
| 8834 | $self->[0]{'TIFF_tag'} = $tags; | ||
| 8835 | |||
| 8836 | # add LGOROWLENGTH keyword | ||
| 8837 | keyword($self, 'LGOROWLENGTH', $trows); | ||
| 8838 | |||
| 8839 | # return | ||
| 8840 | return(); | ||
| 8841 | |||
| 8842 | } | ||
| 8843 | |||
| 8844 | # read TIFF image file directory (IFD) | ||
| 8845 | # parameters: (file_handle, offset, short_format, long_format) | ||
| 8846 | # returns: (IFD_hash) | ||
| 8847 | sub _readTIFFdir { | ||
| 8848 | |||
| 8849 | # get parameters | ||
| 8850 | my ($fh, $start, $short, $long) = @_; | ||
| 8851 | |||
| 8852 | # local variables | ||
| 8853 | my (@ts, $buf, $id, $type, $count, $size, $mark, $offset, $num, $denom, $tags); | ||
| 8854 | |||
| 8855 | # field type size (in bytes) | ||
| 8856 | @ts = (0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8, 4); | ||
| 8857 | |||
| 8858 | # seek start of IFD | ||
| 8859 | seek($fh, $start, 0); | ||
| 8860 | |||
| 8861 | # read number entries | ||
| 8862 | read($fh, $buf, 2); | ||
| 8863 | |||
| 8864 | # read the directory | ||
| 8865 | for (1 .. unpack($short, $buf)) { | ||
| 8866 | |||
| 8867 | # read first part of IFD entry | ||
| 8868 | read($fh, $buf, 8); | ||
| 8869 | |||
| 8870 | # unpack first three fields (ID, type, count) | ||
| 8871 | ($id, $type, $count) = unpack("$short$short$long", $buf); | ||
| 8872 | |||
| 8873 | # read last part of IFD entry | ||
| 8874 | read($fh, $buf, 4); | ||
| 8875 | |||
| 8876 | # determine value/offset size (size * count) + (1 if ASCII string) | ||
| 8877 | $size = $ts[$type] * $count + (($type == 2) ? 1 : 0); | ||
| 8878 | |||
| 8879 | # if an offset | ||
| 8880 | if ($size > 4) { | ||
| 8881 | |||
| 8882 | # mark file location | ||
| 8883 | $mark = tell($fh); | ||
| 8884 | |||
| 8885 | # unpack offset | ||
| 8886 | $offset = unpack($long, $buf); | ||
| 8887 | |||
| 8888 | # seek values | ||
| 8889 | seek($fh, $offset, 0); | ||
| 8890 | |||
| 8891 | # if binary string | ||
| 8892 | if ($type == 1 || $type == 7) { | ||
| 8893 | |||
| 8894 | # read binary string | ||
| 8895 | read($fh, $buf, $count); | ||
| 8896 | |||
| 8897 | # unpack value | ||
| 8898 | $tags->{$id} = [unpack("a$count", $buf)]; | ||
| 8899 | |||
| 8900 | # if ASCII string | ||
| 8901 | } elsif ($type == 2) { | ||
| 8902 | |||
| 8903 | # read ASCII string | ||
| 8904 | read($fh, $buf, $count); | ||
| 8905 | |||
| 8906 | # unpack null-terminated ASCII string | ||
| 8907 | $tags->{$id} = [unpack("Z$count", $buf)]; | ||
| 8908 | |||
| 8909 | # if short values | ||
| 8910 | } elsif ($type == 3) { | ||
| 8911 | |||
| 8912 | # read values | ||
| 8913 | read($fh, $buf, 2 * $count); | ||
| 8914 | |||
| 8915 | # unpack values | ||
| 8916 | $tags->{$id} = [unpack("$short$count", $buf)]; | ||
| 8917 | |||
| 8918 | # if long values | ||
| 8919 | } elsif ($type == 4) { | ||
| 8920 | |||
| 8921 | # read values | ||
| 8922 | read($fh, $buf, 4 * $count); | ||
| 8923 | |||
| 8924 | # unpack values | ||
| 8925 | $tags->{$id} = [unpack("$long$count", $buf)]; | ||
| 8926 | |||
| 8927 | # if rational values | ||
| 8928 | } elsif ($type == 5) { | ||
| 8929 | |||
| 8930 | # double count (one rational value is two long values) | ||
| 8931 | $count *= 2; | ||
| 8932 | |||
| 8933 | # read values | ||
| 8934 | read($fh, $buf, 4 * $count); | ||
| 8935 | |||
| 8936 | # unpack values | ||
| 8937 | $tags->{$id} = [unpack("$long$count", $buf)]; | ||
| 8938 | |||
| 8939 | } | ||
| 8940 | |||
| 8941 | # reset file pointer | ||
| 8942 | seek($fh, $mark, 0); | ||
| 8943 | |||
| 8944 | # if binary string | ||
| 8945 | } elsif ($type == 1 || $type == 7) { | ||
| 8946 | |||
| 8947 | # unpack binary string | ||
| 8948 | $tags->{$id} = [unpack("a$count", $buf)]; | ||
| 8949 | |||
| 8950 | # if ASCII string | ||
| 8951 | } elsif ($type == 2) { | ||
| 8952 | |||
| 8953 | # unpack null-terminated ASCII string | ||
| 8954 | $tags->{$id} = [unpack("Z$count", $buf)]; | ||
| 8955 | |||
| 8956 | # if short value(s) | ||
| 8957 | } elsif ($type == 3) { | ||
| 8958 | |||
| 8959 | # unpack value(s) | ||
| 8960 | $tags->{$id} = [unpack("$short$count", $buf)]; | ||
| 8961 | |||
| 8962 | # if long value | ||
| 8963 | } elsif ($type == 4) { | ||
| 8964 | |||
| 8965 | # unpack value | ||
| 8966 | $tags->{$id} = [unpack($long, $buf)]; | ||
| 8967 | |||
| 8968 | } else { | ||
| 8969 | |||
| 8970 | # save packed value | ||
| 8971 | $tags->{$id} = [$buf]; | ||
| 8972 | |||
| 8973 | } | ||
| 8974 | |||
| 8975 | } | ||
| 8976 | |||
| 8977 | # return | ||
| 8978 | return($tags); | ||
| 8979 | |||
| 8980 | } | ||
| 8981 | |||
| 8982 | # read TIFF image band | ||
| 8983 | # row zero is top of image | ||
| 8984 | # parameters: (file_handle, IFD_hash, lower_row, upper_row, row_width, unpack_format) | ||
| 8985 | # returns: (2D_array) | ||
| 8986 | sub _readTIFFband { | ||
| 8987 | |||
| 8988 | # get parameters | ||
| 8989 | my ($fh, $tags, $lower, $upper, $width, $upf) = @_; | ||
| 8990 | |||
| 8991 | # local variables | ||
| 8992 | my ($offset, $rows, $buf, $band); | ||
| 8993 | |||
| 8994 | # get strip offset array | ||
| 8995 | $offset = $tags->{'273'}; | ||
| 8996 | |||
| 8997 | # get rows per strip | ||
| 8998 | $rows = $tags->{'278'}[0]; | ||
| 8999 | |||
| 9000 | # for each row | ||
| 9001 | for my $i ($lower .. $upper) { | ||
| 9002 | |||
| 9003 | # set file pointer | ||
| 9004 | seek($fh, $offset->[int($i/$rows)] + ($i % $rows) * $width, 0); | ||
| 9005 | |||
| 9006 | # read row data | ||
| 9007 | read($fh, $buf, $width); | ||
| 9008 | |||
| 9009 | # unpack data | ||
| 9010 | $band->[$i - $lower] = [unpack($upf, $buf)]; | ||
| 9011 | |||
| 9012 | } | ||
| 9013 | |||
| 9014 | # return | ||
| 9015 | return($band); | ||
| 9016 | |||
| 9017 | } | ||
| 9018 | |||
| 9019 | # write TIFF image file directory (IFD) | ||
| 9020 | # parameters: (file_handle, offset, short_format, long_format, IFD_hash) | ||
| 9021 | sub _writeTIFFdir { | ||
| 9022 | |||
| 9023 | # get parameters | ||
| 9024 | my ($fh, $ifd, $short, $long, $tags) = @_; | ||
| 9025 | |||
| 9026 | # local variables | ||
| 9027 | my (@ts, @sid, $mark, $type, $count, $size, $fmt); | ||
| 9028 | |||
| 9029 | # field type size (in bytes) | ||
| 9030 | @ts = (0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8, 4); | ||
| 9031 | |||
| 9032 | # make list of tag ids, sorted numerically | ||
| 9033 | @sid = sort {$a <=> $b} keys(%{$tags}); | ||
| 9034 | |||
| 9035 | # seek start of IFD | ||
| 9036 | seek($fh, $ifd, 0); | ||
| 9037 | |||
| 9038 | # write number of tags | ||
| 9039 | print $fh pack($short, scalar(@sid)); | ||
| 9040 | |||
| 9041 | # set data pointer | ||
| 9042 | $mark = $ifd + 12 * @sid + 6; | ||
| 9043 | |||
| 9044 | # for each tag | ||
| 9045 | for my $id (@sid) { | ||
| 9046 | |||
| 9047 | # get data type | ||
| 9048 | $type = $tags->{$id}[0]; | ||
| 9049 | |||
| 9050 | # if a binary string | ||
| 9051 | if ($type == 1 || $type == 7) { | ||
| 9052 | |||
| 9053 | # set count to string length | ||
| 9054 | $count = length($tags->{$id}[1]); | ||
| 9055 | |||
| 9056 | # if an ASCII string | ||
| 9057 | } elsif ($type == 2) { | ||
| 9058 | |||
| 9059 | # set count to string length + 1 | ||
| 9060 | $count = length($tags->{$id}[1]) + 1; | ||
| 9061 | |||
| 9062 | # if a rational value | ||
| 9063 | } elsif ($type == 5) { | ||
| 9064 | |||
| 9065 | # set count to number of values/2 | ||
| 9066 | $count = $#{$tags->{$id}}/2; | ||
| 9067 | |||
| 9068 | } else { | ||
| 9069 | |||
| 9070 | # set count to number of values | ||
| 9071 | $count = $#{$tags->{$id}}; | ||
| 9072 | |||
| 9073 | } | ||
| 9074 | |||
| 9075 | # if size of value/offset > 4 | ||
| 9076 | if (($size = $count * $ts[$type]) > 4) { | ||
| 9077 | |||
| 9078 | # write directory entry with offset | ||
| 9079 | print $fh pack("$short$short$long$long", $id, $type, $count, $mark); | ||
| 9080 | |||
| 9081 | # increment data pointer | ||
| 9082 | $mark += $size; | ||
| 9083 | |||
| 9084 | # make a word boundary | ||
| 9085 | $mark += $mark % 2; | ||
| 9086 | |||
| 9087 | } else { | ||
| 9088 | |||
| 9089 | # if a binary string | ||
| 9090 | if ($type == 1 || $type == 7) { | ||
| 9091 | |||
| 9092 | # set pack format | ||
| 9093 | $fmt = 'a4'; | ||
| 9094 | |||
| 9095 | # if an ASCII string | ||
| 9096 | } elsif ($type == 2) { | ||
| 9097 | |||
| 9098 | # set pack format | ||
| 9099 | $fmt = 'Z4'; | ||
| 9100 | |||
| 9101 | # if a short value | ||
| 9102 | } elsif ($type == 3) { | ||
| 9103 | |||
| 9104 | # set pack format (one or two values) | ||
| 9105 | $fmt = $count == 1 ? $short . 'x2' : $short . '2'; | ||
| 9106 | |||
| 9107 | # if a long value | ||
| 9108 | } elsif ($type == 4) { | ||
| 9109 | |||
| 9110 | # set pack format | ||
| 9111 | $fmt = $long; | ||
| 9112 | |||
| 9113 | } else { | ||
| 9114 | |||
| 9115 | # error | ||
| 9116 | croak('unsupported TIFF data type, stopped'); | ||
| 9117 | |||
| 9118 | } | ||
| 9119 | |||
| 9120 | # write directory entry (12 bytes) with value(s) | ||
| 9121 | print $fh pack("$short$short$long$fmt", $id, $type, $count, @{$tags->{$id}}[1 .. $#{$tags->{$id}}]); | ||
| 9122 | |||
| 9123 | } | ||
| 9124 | |||
| 9125 | } | ||
| 9126 | |||
| 9127 | # set data pointer | ||
| 9128 | $mark = $ifd + 12 * @sid + 6; | ||
| 9129 | |||
| 9130 | # for each tag | ||
| 9131 | for my $id (@sid) { | ||
| 9132 | |||
| 9133 | # get data type | ||
| 9134 | $type = $tags->{$id}[0]; | ||
| 9135 | |||
| 9136 | # if a binary string | ||
| 9137 | if ($type == 1 || $type == 7) { | ||
| 9138 | |||
| 9139 | # set count to string length | ||
| 9140 | $count = length($tags->{$id}[1]); | ||
| 9141 | |||
| 9142 | # if an ASCII string | ||
| 9143 | } elsif ($type == 2) { | ||
| 9144 | |||
| 9145 | # set count to string length + 1 | ||
| 9146 | $count = length($tags->{$id}[1]) + 1; | ||
| 9147 | |||
| 9148 | # if a rational value | ||
| 9149 | } elsif ($type == 5) { | ||
| 9150 | |||
| 9151 | # set count to number of values/2 | ||
| 9152 | $count = $#{$tags->{$id}}/2; | ||
| 9153 | |||
| 9154 | } else { | ||
| 9155 | |||
| 9156 | # set count to number of values | ||
| 9157 | $count = $#{$tags->{$id}}; | ||
| 9158 | |||
| 9159 | } | ||
| 9160 | |||
| 9161 | # if size of value/offset > 4 | ||
| 9162 | if (($size = $count * $ts[$type]) > 4) { | ||
| 9163 | |||
| 9164 | # if a binary string | ||
| 9165 | if ($type == 1 || $type == 7) { | ||
| 9166 | |||
| 9167 | # set pack format | ||
| 9168 | $fmt = "a$count"; | ||
| 9169 | |||
| 9170 | # if an ASCII string | ||
| 9171 | } elsif ($type == 2) { | ||
| 9172 | |||
| 9173 | # set pack format | ||
| 9174 | $fmt = "Z$count"; | ||
| 9175 | |||
| 9176 | # if a short value | ||
| 9177 | } elsif ($type == 3) { | ||
| 9178 | |||
| 9179 | # set pack format | ||
| 9180 | $fmt = "$short$count"; | ||
| 9181 | |||
| 9182 | # if a long value | ||
| 9183 | } elsif ($type == 4) { | ||
| 9184 | |||
| 9185 | # set pack format | ||
| 9186 | $fmt = "$long$count"; | ||
| 9187 | |||
| 9188 | # if a rational value | ||
| 9189 | } elsif ($type == 5) { | ||
| 9190 | |||
| 9191 | # set pack format | ||
| 9192 | $fmt = "$long$#{$tags->{$id}}"; | ||
| 9193 | |||
| 9194 | } else { | ||
| 9195 | |||
| 9196 | # error | ||
| 9197 | croak('unsupported TIFF data type, stopped'); | ||
| 9198 | } | ||
| 9199 | |||
| 9200 | # set file pointer | ||
| 9201 | seek($fh, $mark, 0); | ||
| 9202 | |||
| 9203 | # write the data value(s) | ||
| 9204 | print $fh pack($fmt, @{$tags->{$id}}[1 .. $#{$tags->{$id}}]); | ||
| 9205 | |||
| 9206 | # increment data pointer | ||
| 9207 | $mark += $size; | ||
| 9208 | |||
| 9209 | # make a word boundary | ||
| 9210 | $mark += $mark % 2; | ||
| 9211 | |||
| 9212 | } | ||
| 9213 | |||
| 9214 | } | ||
| 9215 | |||
| 9216 | } | ||
| 9217 | |||
| 9218 | # write TIFF data strip | ||
| 9219 | # parameters: (file_handle, IFD_hash, patch_width, gap_width, left_edge_width, right_edge_width, strip_index, strip_data_array, pack_format, dither_value) | ||
| 9220 | sub _writeTIFFstrip { | ||
| 9221 | |||
| 9222 | # get parameters | ||
| 9223 | my ($fh, $tags, $width, $gap, $left, $right, $sx, $data, $fmt, $dither) = @_; | ||
| 9224 | |||
| 9225 | # local variables | ||
| 9226 | my ($pi, $samples, $bits, $max, $diff, $edge, $w, @spot, $rms, $gdata, @row, $strip); | ||
| 9227 | |||
| 9228 | # get photometric interpretation | ||
| 9229 | $pi = $tags->{'262'}[1]; | ||
| 9230 | |||
| 9231 | # get number of samples (channels) | ||
| 9232 | $samples = $tags->{'277'}[1]; | ||
| 9233 | |||
| 9234 | # get bits per sample | ||
| 9235 | $bits = $tags->{'258'}[1]; | ||
| 9236 | |||
| 9237 | # max binary value (8, 16 or 32 bits) | ||
| 9238 | $max = ($bits == 8) ? 255 : ($bits == 16) ? 65535 : 1; | ||
| 9239 | |||
| 9240 | # make list of spot channel indices | ||
| 9241 | @spot = (4 .. $tags->{'277'}[1] - 1); | ||
| 9242 | |||
| 9243 | # for each patch | ||
| 9244 | for my $i (0 .. $#{$data}) { | ||
| 9245 | |||
| 9246 | # if RGB data | ||
| 9247 | if ($pi == 2) { | ||
| 9248 | |||
| 9249 | # compute white and black differences | ||
| 9250 | $diff->[$i][0] = sqrt(($max - $data->[$i][0])**2 + ($max - $data->[$i][1])**2 + ($max - $data->[$i][2])**2); | ||
| 9251 | $diff->[$i][1] = sqrt($data->[$i][0]**2 + $data->[$i][1]**2 + $data->[$i][2]**2); | ||
| 9252 | |||
| 9253 | # if CMYK data | ||
| 9254 | } elsif ($pi == 5) { | ||
| 9255 | |||
| 9256 | # compute rms value of CMY + spot channels (CMY weighted and spot channels inverted) | ||
| 9257 | $rms = sqrt(List::Util::sum(1.5 * $data->[$i][0]**2, $data->[$i][1]**2, 0.5 * $data->[$i][2]**2, (map {($max - $data->[$i][$_])**2} @spot))/(3 + @spot)); | ||
| 9258 | |||
| 9259 | # compute white and black differences (black * color) | ||
| 9260 | $diff->[$i][0] = $max - ($max - $data->[$i][3]) * ($max - $rms)/$max; | ||
| 9261 | $diff->[$i][1] = ($max - $data->[$i][3]) * ($max - $rms)/$max; | ||
| 9262 | |||
| 9263 | # L*a*b* data | ||
| 9264 | } else { | ||
| 9265 | |||
| 9266 | # compute white and black differences (approx dEab) | ||
| 9267 | $diff->[$i][0] = sqrt(($max - $data->[$i][0])**2 + 6.55 * $data->[$i][1]**2 + 6.55 * $data->[$i][2]**2); | ||
| 9268 | $diff->[$i][1] = sqrt($data->[$i][0]**2 + 6.55 * $data->[$i][1]**2 + 6.55 * $data->[$i][2]**2); | ||
| 9269 | |||
| 9270 | } | ||
| 9271 | |||
| 9272 | # skip first patch | ||
| 9273 | if ($i > 0) { | ||
| 9274 | |||
| 9275 | # if RGB data | ||
| 9276 | if ($pi == 2) { | ||
| 9277 | |||
| 9278 | # if max white difference > max black difference | ||
| 9279 | if (($diff->[$i - 1][0] > $diff->[$i][0] ? $diff->[$i - 1][0] : $diff->[$i][0]) > ($diff->[$i - 1][1] > $diff->[$i][1] ? $diff->[$i - 1][1] : $diff->[$i][1])) { | ||
| 9280 | |||
| 9281 | # gap is white | ||
| 9282 | $gdata->[$i - 1] = [($max) x 3]; | ||
| 9283 | |||
| 9284 | } else { | ||
| 9285 | |||
| 9286 | # gap is black | ||
| 9287 | $gdata->[$i - 1] = [0, 0, 0]; | ||
| 9288 | |||
| 9289 | } | ||
| 9290 | |||
| 9291 | # if CMYK data | ||
| 9292 | } elsif ($pi == 5) { | ||
| 9293 | |||
| 9294 | # if max white difference > max black difference | ||
| 9295 | if (($diff->[$i - 1][0] > $diff->[$i][0] ? $diff->[$i - 1][0] : $diff->[$i][0]) > ($diff->[$i - 1][1] > $diff->[$i][1] ? $diff->[$i - 1][1] : $diff->[$i][1])) { | ||
| 9296 | |||
| 9297 | # gap is white | ||
| 9298 | $gdata->[$i - 1] = [0, 0, 0, 0, ($max) x ($samples - 4)]; | ||
| 9299 | |||
| 9300 | } else { | ||
| 9301 | |||
| 9302 | # gap is black | ||
| 9303 | $gdata->[$i - 1] = [0, 0, 0, ($max) x ($samples - 3)]; | ||
| 9304 | |||
| 9305 | } | ||
| 9306 | |||
| 9307 | # L*a*b* data | ||
| 9308 | } else { | ||
| 9309 | |||
| 9310 | # if max white difference > max black difference | ||
| 9311 | if (($diff->[$i - 1][0] > $diff->[$i][0] ? $diff->[$i - 1][0] : $diff->[$i][0]) > ($diff->[$i - 1][1] > $diff->[$i][1] ? $diff->[$i - 1][1] : $diff->[$i][1])) { | ||
| 9312 | |||
| 9313 | # gap is white | ||
| 9314 | $gdata->[$i - 1] = [$max, 0, 0]; | ||
| 9315 | |||
| 9316 | } else { | ||
| 9317 | |||
| 9318 | # gap is black | ||
| 9319 | $gdata->[$i - 1] = [0, 0, 0]; | ||
| 9320 | |||
| 9321 | } | ||
| 9322 | |||
| 9323 | } | ||
| 9324 | |||
| 9325 | } | ||
| 9326 | |||
| 9327 | } | ||
| 9328 | |||
| 9329 | # compute edge pixel values (black) | ||
| 9330 | $edge = ($pi == 5) ? [0, 0, 0, ($max) x ($samples - 3)] : [0, 0, 0]; | ||
| 9331 | |||
| 9332 | # for each patch | ||
| 9333 | for my $i (0 .. $#{$data}) { | ||
| 9334 | |||
| 9335 | # if first patch | ||
| 9336 | if ($i == 0) { | ||
| 9337 | |||
| 9338 | # add left edge data | ||
| 9339 | push(@row, (@{$edge}) x $left->[0]); | ||
| 9340 | |||
| 9341 | # set patch width | ||
| 9342 | $w = $width - $left->[1]; | ||
| 9343 | |||
| 9344 | # if last patch | ||
| 9345 | } elsif ($i == $#{$data}) { | ||
| 9346 | |||
| 9347 | # set patch width | ||
| 9348 | $w = $width - $right->[1]; | ||
| 9349 | |||
| 9350 | # others | ||
| 9351 | } else { | ||
| 9352 | |||
| 9353 | # set patch width | ||
| 9354 | $w = $width; | ||
| 9355 | |||
| 9356 | } | ||
| 9357 | |||
| 9358 | # if dither enabled or 32-bits | ||
| 9359 | if (defined($dither) || $bits == 32) { | ||
| 9360 | |||
| 9361 | # add patch data | ||
| 9362 | push(@row, (@{$data->[$i]}) x $w); | ||
| 9363 | |||
| 9364 | } else { | ||
| 9365 | |||
| 9366 | # add patch data, adding/subtracting 0.5 to round to the nearest integer (by 'pack', below) | ||
| 9367 | push(@row, (map {$_ < 0 ? $_ - 0.5 : $_ + 0.5} @{$data->[$i]}) x $w); | ||
| 9368 | |||
| 9369 | } | ||
| 9370 | |||
| 9371 | # if last patch | ||
| 9372 | if ($i == $#{$data}) { | ||
| 9373 | |||
| 9374 | # add right edge data | ||
| 9375 | push(@row, (@{$edge}) x $right->[0]); | ||
| 9376 | |||
| 9377 | } else { | ||
| 9378 | |||
| 9379 | # add gap data | ||
| 9380 | push(@row, (@{$gdata->[$i]}) x $gap); | ||
| 9381 | |||
| 9382 | } | ||
| 9383 | |||
| 9384 | } | ||
| 9385 | |||
| 9386 | # set file pointer to strip offset | ||
| 9387 | seek($fh, $tags->{'273'}[$sx + 1], 0); | ||
| 9388 | |||
| 9389 | # if dither enabled and 8-bit | ||
| 9390 | if (defined($dither) && $bits == 8) { | ||
| 9391 | |||
| 9392 | # for each strip row | ||
| 9393 | for my $i (0 .. $tags->{'278'}[1] - 1) { | ||
| 9394 | |||
| 9395 | # write packed data with dithering | ||
| 9396 | print $fh pack($fmt, map {$_ < 0 ? $_ - rand() : $_ + rand()} @row); | ||
| 9397 | |||
| 9398 | } | ||
| 9399 | |||
| 9400 | } else { | ||
| 9401 | |||
| 9402 | # for each strip row | ||
| 9403 | for my $i (0 .. $tags->{'278'}[1] - 1) { | ||
| 9404 | |||
| 9405 | # write packed data | ||
| 9406 | print $fh pack($fmt, @row); | ||
| 9407 | |||
| 9408 | } | ||
| 9409 | |||
| 9410 | } | ||
| 9411 | |||
| 9412 | } | ||
| 9413 | |||
| 9414 | # read chart from CxF3 data file | ||
| 9415 | # parameters: (object_reference, file_handle, hash) | ||
| 9416 | # returns: (result) | ||
| 9417 | sub _readChartCxF3 { | ||
| 9418 | |||
| 9419 | # get parameters | ||
| 9420 | my ($self, $fh, $hash) = @_; | ||
| 9421 | |||
| 9422 | # local variables | ||
| 9423 | my ($dom, $root, $ns, $uri, %keys, @info, @obj, $ops_hash, $ops); | ||
| 9424 | my ($ix, $type, $name, $value, $node, @data, $xrp, @attr); | ||
| 9425 | |||
| 9426 | # parse CxF3 document | ||
| 9427 | eval{$dom = XML::LibXML->load_xml('IO' => $fh)} || return('failed parsing CxF3 document'); | ||
| 9428 | |||
| 9429 | # validate the CxF3 document | ||
| 9430 | _validateCxF3($dom) if (defined($hash->{'validate'}) && $hash->{'validate'}); | ||
| 9431 | |||
| 9432 | # get root element | ||
| 9433 | $root = $dom->documentElement(); | ||
| 9434 | |||
| 9435 | # get the namespace prefix and URI | ||
| 9436 | $ns = $root->prefix(); | ||
| 9437 | $uri = $root->namespaceURI(); | ||
| 9438 | |||
| 9439 | # verify CxF3 document or return | ||
| 9440 | ($uri eq 'http://colorexchangeformat.com/CxF3-core') || return('CxF3 document has wrong URI'); | ||
| 9441 | |||
| 9442 | # get cc:Object elements | ||
| 9443 | @obj = $root->findnodes("$ns:Resources/$ns:ObjectCollection/$ns:Object"); | ||
| 9444 | |||
| 9445 | # get cc:FileInformation elements (optional) | ||
| 9446 | @info = $root->findnodes("$ns:FileInformation/*"); | ||
| 9447 | |||
| 9448 | # save root element | ||
| 9449 | # this method only reads data from cc:Object nodes | ||
| 9450 | # all other CxF3 info is kept in the dom object | ||
| 9451 | # and accessed as needed by other methods | ||
| 9452 | $self->[0]{'CxF3_dom'} = $root; | ||
| 9453 | |||
| 9454 | # save record separator in header | ||
| 9455 | # note: XML files might not have record separators | ||
| 9456 | # so we use Perl's input record separator instead | ||
| 9457 | $self->[0]{'read_rs'} = $/; | ||
| 9458 | |||
| 9459 | # make CxF3 => ASCII mapping table (from ISO 17972-1, Annex A) | ||
| 9460 | %keys = ('Creator' => 'ORIGINATOR', 'Description' => 'FILE_DESCRIPTOR', 'CreationDate' => 'CREATED', 'Comment' => 'CXF3_COMMENT'); | ||
| 9461 | |||
| 9462 | # for each cc:FileInformation element | ||
| 9463 | for my $s (@info) { | ||
| 9464 | |||
| 9465 | # if cc:Tag node | ||
| 9466 | if ($s->nodeName() eq "$ns:Tag") { | ||
| 9467 | |||
| 9468 | # get name attribute | ||
| 9469 | $name = $s->getAttribute('Name'); | ||
| 9470 | |||
| 9471 | # get value attribute | ||
| 9472 | $value = $s->getAttribute('Value'); | ||
| 9473 | |||
| 9474 | } else { | ||
| 9475 | |||
| 9476 | # get node name | ||
| 9477 | $name = $s->nodeName(); | ||
| 9478 | |||
| 9479 | # remove namespace prefix | ||
| 9480 | $name =~ s/^\w+://; | ||
| 9481 | |||
| 9482 | # lookup name in hash | ||
| 9483 | $name = defined($keys{$name}) ? $keys{$name} : $name; | ||
| 9484 | |||
| 9485 | # get node value | ||
| 9486 | $value = $s->textContent(); | ||
| 9487 | |||
| 9488 | } | ||
| 9489 | |||
| 9490 | # add name/value to header array | ||
| 9491 | push(@{$self->[3]}, [$name, "\"$value\"", 'FileInformation']); | ||
| 9492 | |||
| 9493 | } | ||
| 9494 | |||
| 9495 | # make the operations hash and add format fields | ||
| 9496 | $ops_hash = _makeCxF3readops($self, $root, $ns, \@obj, $hash); | ||
| 9497 | |||
| 9498 | # initialize sample index | ||
| 9499 | $ix = 0; | ||
| 9500 | |||
| 9501 | # for each cc:Object element | ||
| 9502 | for my $s (@obj) { | ||
| 9503 | |||
| 9504 | # get the ObjectType attribute | ||
| 9505 | $type = $s->getAttribute('ObjectType'); | ||
| 9506 | |||
| 9507 | # get the Name attribute | ||
| 9508 | $name = $s->getAttribute('Name'); | ||
| 9509 | |||
| 9510 | # if ObjectType is 'Target' or '...Measurement' | ||
| 9511 | if ($type =~ m/^Target$|Measurement$/) { | ||
| 9512 | |||
| 9513 | # match numeric part of Name attribute | ||
| 9514 | $name =~ m/(\d+)$/; | ||
| 9515 | |||
| 9516 | # set row index | ||
| 9517 | $ix = $1; | ||
| 9518 | |||
| 9519 | } else { | ||
| 9520 | |||
| 9521 | # increment row index | ||
| 9522 | $ix++; | ||
| 9523 | |||
| 9524 | } | ||
| 9525 | |||
| 9526 | # get operation list for this ObjectType | ||
| 9527 | $ops = $ops_hash->{$type}; | ||
| 9528 | |||
| 9529 | # for each operation | ||
| 9530 | for my $i (0 .. $#{$ops}) { | ||
| 9531 | |||
| 9532 | # get main Xpath node | ||
| 9533 | ($node) = $s->findnodes($ops->[$i][1]); | ||
| 9534 | |||
| 9535 | # if subpaths | ||
| 9536 | if (@{$ops->[$i][2]}) { | ||
| 9537 | |||
| 9538 | # if data class is NCLR | ||
| 9539 | if ($ops->[$i][0] eq 'NCLR') { | ||
| 9540 | |||
| 9541 | # get the CMYK values | ||
| 9542 | @data = map {$node->findvalue($_)} @{$ops->[$i][2]}; | ||
| 9543 | |||
| 9544 | # for each SpotColor | ||
| 9545 | for my $spotcolor ($node->findnodes("$ns:SpotColor")) { | ||
| 9546 | |||
| 9547 | # push SpotColor value | ||
| 9548 | push(@data, $spotcolor->findvalue("$ns:Percentage")); | ||
| 9549 | |||
| 9550 | } | ||
| 9551 | |||
| 9552 | # set chart data (CMYK + SPOT values) | ||
| 9553 | @{$self->[1][$ix]}[@{$ops->[$i][3]}] = @data; | ||
| 9554 | |||
| 9555 | } else { | ||
| 9556 | |||
| 9557 | # set chart data using subpaths | ||
| 9558 | @{$self->[1][$ix]}[@{$ops->[$i][3]}] = map {$node->findvalue($_)} @{$ops->[$i][2]}; | ||
| 9559 | |||
| 9560 | } | ||
| 9561 | |||
| 9562 | # if no subpaths and one field | ||
| 9563 | } elsif (@{$ops->[$i][3]} == 1) { | ||
| 9564 | |||
| 9565 | # set chart data to text content | ||
| 9566 | $self->[1][$ix][$ops->[$i][3][0]] = $node->textContent(); | ||
| 9567 | |||
| 9568 | # if no subpaths and multiple fields (e.g. spectral data) | ||
| 9569 | } elsif (@{$ops->[$i][3]} > 1) { | ||
| 9570 | |||
| 9571 | # set chart data splitting text content | ||
| 9572 | @{$self->[1][$ix]}[@{$ops->[$i][3]}] = split(/ /, $node->textContent()); | ||
| 9573 | |||
| 9574 | } | ||
| 9575 | |||
| 9576 | } | ||
| 9577 | |||
| 9578 | } | ||
| 9579 | |||
| 9580 | # read CxF3 ColorSpecification nodes | ||
| 9581 | _readCxF3colorspec($self, $root, $ns); | ||
| 9582 | |||
| 9583 | # make XPathContext object for X-Rite Prism namespace | ||
| 9584 | $xrp = XML::LibXML::XPathContext->new($root); | ||
| 9585 | $xrp->registerNs('xrp', 'http://www.xrite.com/products/prism'); | ||
| 9586 | |||
| 9587 | # get the xrp:CustomAttributes node | ||
| 9588 | if (($node) = $xrp->findnodes("$ns:CustomResources/xrp:Prism/xrp:CustomAttributes")) { | ||
| 9589 | |||
| 9590 | # get the attribute list | ||
| 9591 | @attr = $node->attributes(); | ||
| 9592 | |||
| 9593 | # add xrp:CustomAttributes hash | ||
| 9594 | $self->[0]{'xrp:CustomAttributes'} = {map {$_->nodeName, $_->getValue()} @attr}; | ||
| 9595 | |||
| 9596 | } | ||
| 9597 | |||
| 9598 | # return | ||
| 9599 | return(); | ||
| 9600 | |||
| 9601 | } | ||
| 9602 | |||
| 9603 | # make CxF3 read operations hash | ||
| 9604 | # adds the format fields to object | ||
| 9605 | # parameters: (object_reference, CxF3_root, CxF3_prefix, CxF3_object_array_reference, hash) | ||
| 9606 | # returns: (hash_ref) | ||
| 9607 | sub _makeCxF3readops { | ||
| 9608 | |||
| 9609 | # get parameters | ||
| 9610 | my ($self, $root, $ns, $obj, $hash) = @_; | ||
| 9611 | |||
| 9612 | # local variables | ||
| 9613 | my (@attr, @tags, %keys, $table, $k, $m, $n, $t, $type, $entry, $ops_hash); | ||
| 9614 | my (@format, @nodes, $node, @data, $name, $colorspec, $start, $inc); | ||
| 9615 | |||
| 9616 | # if cc:Object filter parameter provided | ||
| 9617 | if (defined($hash->{'cc:Object'}) && ref($hash->{'cc:Object'}) eq 'ARRAY') { | ||
| 9618 | |||
| 9619 | # for each entry | ||
| 9620 | for (@{$hash->{'cc:Object'}}) { | ||
| 9621 | |||
| 9622 | # match type/attribute | ||
| 9623 | m/^([^\s\/]*?)\/?([^\s\/]*)$/; | ||
| 9624 | |||
| 9625 | # save matched values | ||
| 9626 | $entry = [$1, $2]; | ||
| 9627 | |||
| 9628 | # if a valid attribute (see CxF3_Core.xsd) | ||
| 9629 | if ($2 =~ m/^(?:|ObjectType|Name|Id|GUID|\*)$/) { | ||
| 9630 | |||
| 9631 | # push on array | ||
| 9632 | push(@attr, $entry); | ||
| 9633 | |||
| 9634 | } else { | ||
| 9635 | |||
| 9636 | # print warning | ||
| 9637 | warn('invalid cc:Object attribute'); | ||
| 9638 | |||
| 9639 | } | ||
| 9640 | |||
| 9641 | } | ||
| 9642 | |||
| 9643 | } | ||
| 9644 | |||
| 9645 | # if cc:Tag filter parameter provided | ||
| 9646 | if (defined($hash->{'cc:Tag'}) && ref($hash->{'cc:Tag'}) eq 'ARRAY') { | ||
| 9647 | |||
| 9648 | # for each entry | ||
| 9649 | for (@{$hash->{'cc:Tag'}}) { | ||
| 9650 | |||
| 9651 | # match type/key | ||
| 9652 | m/^([^\s\/]*?)\/?([^\s\/]*)$/; | ||
| 9653 | |||
| 9654 | # push on array | ||
| 9655 | push(@tags, [$1, $2]); | ||
| 9656 | |||
| 9657 | } | ||
| 9658 | |||
| 9659 | } | ||
| 9660 | |||
| 9661 | # make hash for sort order of certain keys | ||
| 9662 | %keys = ('SampleID' => -2, 'SampleName' => -1, 'Id' => -2, 'Name' => -1); | ||
| 9663 | |||
| 9664 | # table [data_class, CxF3_main_path, [CxF3_sub_paths], [CGATS/ASCII field names]] | ||
| 9665 | # some mappings have no sub-paths, which is indicated by an empty sub_path array | ||
| 9666 | # the 'NCLR', 'SPECTRAL' and 'DENSITY' data classes are special cases | ||
| 9667 | $table = [ | ||
| 9668 | ['RGB', "$ns:DeviceColorValues/$ns:ColorRGB", ["$ns:R", "$ns:G", "$ns:B"], [qw(RGB_R RGB_G RGB_B)]], | ||
| 9669 | ['CMYK', "$ns:DeviceColorValues/$ns:ColorCMYK", ["$ns:Cyan", "$ns:Magenta", "$ns:Yellow", "$ns:Black"], [qw(CMYK_C CMYK_M CMYK_Y CMYK_K)]], | ||
| 9670 | ['NCLR', "$ns:DeviceColorValues/$ns:ColorCMYKPlusN", ["$ns:Cyan", "$ns:Magenta", "$ns:Yellow", "$ns:Black"], [qw(nCLR)]], | ||
| 9671 | ['SPECTRAL', "$ns:ColorValues/$ns:ReflectanceSpectrum", [], [qw(nm)]], | ||
| 9672 | ['DENSITY', "$ns:ColorValues/$ns:ColorDensity/$ns:Density", [], [qw(D_RED D_GREEN D_BLUE D_VIS)]], | ||
| 9673 | ['XYZ', "$ns:ColorValues/$ns:ColorCIEXYZ", ["$ns:X", "$ns:Y", "$ns:Z"], [qw(XYZ_X XYZ_Y XYZ_Z)]], | ||
| 9674 | ['XYY', "$ns:ColorValues/$ns:ColorCIExyY", ["$ns:x", "$ns:y", "$ns:Y"], [qw(XYY_X XYY_Y XYY_YCAP)]], | ||
| 9675 | ['LAB', "$ns:ColorValues/$ns:ColorCIELab", ["$ns:L", "$ns:A", "$ns:B"], [qw(LAB_L LAB_A LAB_B)]], | ||
| 9676 | ['LCH', "$ns:ColorValues/$ns:ColorCIELCh", ["$ns:L", "$ns:C", "$ns:H"], [qw(LAB_L LAB_C LAB_H)]], | ||
| 9677 | ['DE', "$ns:ColorDifferenceValues/$ns:DeltaCIELab/$ns:dE", [], [qw(LAB_DE)]], | ||
| 9678 | ['DE94', "$ns:ColorDifferenceValues/$ns:DeltaCIELab/$ns:dE94", [], [qw(LAB_DE94)]], | ||
| 9679 | ['DECMC', "$ns:ColorDifferenceValues/$ns:DeltaCIELab/$ns:dEcmc", [], [qw(LAB_CMC)]], | ||
| 9680 | ['DE2000', "$ns:ColorDifferenceValues/$ns:DeltaCIELab/$ns:dE2000", [], [qw(LAB_2000)]], | ||
| 9681 | ]; | ||
| 9682 | |||
| 9683 | # set next table index | ||
| 9684 | $m = $#{$table} + 1; | ||
| 9685 | |||
| 9686 | # for each CxF3 'Object' | ||
| 9687 | for my $s (@{$obj}) { | ||
| 9688 | |||
| 9689 | # get the 'ObjectType' attribute | ||
| 9690 | $type = $s->getAttribute('ObjectType'); | ||
| 9691 | |||
| 9692 | # if 'ObjectType' not in hash | ||
| 9693 | if (! defined($ops_hash->{$type})) { | ||
| 9694 | |||
| 9695 | # add 'ObjectType' to hash | ||
| 9696 | $ops_hash->{$type} = []; | ||
| 9697 | |||
| 9698 | # if 'Object' attributes are mapped | ||
| 9699 | if (@attr) { | ||
| 9700 | |||
| 9701 | # for each 'Object' attribute (GUID is optional) | ||
| 9702 | for my $t (qw(ObjectType Name Id GUID)) { | ||
| 9703 | |||
| 9704 | # if attribute exists and is mapped | ||
| 9705 | if ($s->exists("\@$t") && grep {($_->[0] eq $type || $_->[0] =~ m/^\*?$/) && ($_->[1] eq $t || $_->[1] =~ m/^\*?$/)} @attr) { | ||
| 9706 | |||
| 9707 | # get sort order | ||
| 9708 | $k = defined($keys{$t}) ? $keys{$t} : $m++; | ||
| 9709 | |||
| 9710 | # push table entry on hash array (note: attribute XPaths begin with @) | ||
| 9711 | push(@{$ops_hash->{$type}}, $entry = ["ATTR:$t", "\@$t", [], [$t], $type, $k]); | ||
| 9712 | |||
| 9713 | # push table entry on format array | ||
| 9714 | push(@format, $entry); | ||
| 9715 | |||
| 9716 | } | ||
| 9717 | |||
| 9718 | } | ||
| 9719 | |||
| 9720 | } else { | ||
| 9721 | |||
| 9722 | # if ObjectType not 'Target' or '...Measurement' | ||
| 9723 | if ($type !~ m/^Target$|Measurement$/) { | ||
| 9724 | |||
| 9725 | # push table entry on hash array (note: attribute XPaths begin with @) | ||
| 9726 | push(@{$ops_hash->{$type}}, $entry = ['NAME', '@Name', [], ['SAMPLE_NAME'], $type, -1]); | ||
| 9727 | |||
| 9728 | # push table entry on format array | ||
| 9729 | push(@format, $entry); | ||
| 9730 | |||
| 9731 | } | ||
| 9732 | |||
| 9733 | } | ||
| 9734 | |||
| 9735 | # for each table entry | ||
| 9736 | for my $i (0 .. $#{$table}) { | ||
| 9737 | |||
| 9738 | # get table entry | ||
| 9739 | $t = $table->[$i]; | ||
| 9740 | |||
| 9741 | # if main XPath exists | ||
| 9742 | if ($s->exists($t->[1])) { | ||
| 9743 | |||
| 9744 | # get ColorSpecification attribute (if any) | ||
| 9745 | $colorspec = $s->findvalue("$t->[1]/\@ColorSpecification"); | ||
| 9746 | |||
| 9747 | # push table entry on hash array | ||
| 9748 | push(@{$ops_hash->{$type}}, $entry = [@{$t}, $type, $i, $colorspec]); | ||
| 9749 | |||
| 9750 | # push table entry on format array | ||
| 9751 | push(@format, $entry); | ||
| 9752 | |||
| 9753 | # if an 'NCLR' entry | ||
| 9754 | if ($entry->[0] eq 'NCLR') { | ||
| 9755 | |||
| 9756 | # get cc:SpotColor nodes | ||
| 9757 | @nodes = $s->findnodes(".//$ns:SpotColor"); | ||
| 9758 | |||
| 9759 | # get number of colors | ||
| 9760 | $n = @nodes + 4; | ||
| 9761 | |||
| 9762 | # add format fields | ||
| 9763 | $entry->[3] = [map {sprintf('%xCLR_%x', $n, $_)} (1 .. $n)]; | ||
| 9764 | |||
| 9765 | # if a 'SPECTRAL' entry | ||
| 9766 | } elsif ($entry->[0] eq 'SPECTRAL') { | ||
| 9767 | |||
| 9768 | # get the ReflectanceSpectrum data | ||
| 9769 | @data = split(/ /, $s->findvalue($t->[1])); | ||
| 9770 | |||
| 9771 | # get the ColorSpecification node (linked by the ColorSpecification attribute) | ||
| 9772 | ($node) = $root->findnodes("$ns:Resources/$ns:ColorSpecificationCollection/$ns:ColorSpecification[\@Id='$colorspec']"); | ||
| 9773 | |||
| 9774 | # get the StartWL attribute | ||
| 9775 | $start = $node->findvalue("$ns:MeasurementSpec/$ns:WavelengthRange/\@StartWL"); | ||
| 9776 | |||
| 9777 | # get the Increment attribute | ||
| 9778 | $inc = $node->findvalue("$ns:MeasurementSpec/$ns:WavelengthRange/\@Increment"); | ||
| 9779 | |||
| 9780 | # add format fields | ||
| 9781 | $entry->[3] = [map {'nm' . ($start + $_ * $inc)} (0 .. $#data)]; | ||
| 9782 | |||
| 9783 | } | ||
| 9784 | |||
| 9785 | } | ||
| 9786 | |||
| 9787 | } | ||
| 9788 | |||
| 9789 | # if Tags are mapped | ||
| 9790 | if (@tags) { | ||
| 9791 | |||
| 9792 | # for each Tag | ||
| 9793 | for my $t ($s->findnodes("$ns:TagCollection/$ns:Tag")) { | ||
| 9794 | |||
| 9795 | # get Tag Name attribute | ||
| 9796 | $name = $t->getAttribute('Name'); | ||
| 9797 | |||
| 9798 | # if this Tag is mapped | ||
| 9799 | if (grep {($_->[0] eq $type || $_->[0] =~ m/^\*?$/) && ($_->[1] eq $name || $_->[1] =~ m/^\*?$/)} @tags) { | ||
| 9800 | |||
| 9801 | # get sort order | ||
| 9802 | $k = defined($keys{$name}) ? $keys{$name} : $m++; | ||
| 9803 | |||
| 9804 | # push table entry on hash array (note: attribute XPaths begin with @) | ||
| 9805 | push(@{$ops_hash->{$type}}, $entry = ["TAG:$name", "$ns:TagCollection/$ns:Tag[\@Name = '$name']/\@Value", [], [$name], $type, $k]); | ||
| 9806 | |||
| 9807 | # push table entry on format array | ||
| 9808 | push(@format, $entry); | ||
| 9809 | |||
| 9810 | } | ||
| 9811 | |||
| 9812 | } | ||
| 9813 | |||
| 9814 | } | ||
| 9815 | |||
| 9816 | } | ||
| 9817 | |||
| 9818 | } | ||
| 9819 | |||
| 9820 | # sort format array by table index | ||
| 9821 | @format = sort {$a->[5] <=> $b->[5]} @format; | ||
| 9822 | |||
| 9823 | # for each format entry | ||
| 9824 | for my $fmt (@format) { | ||
| 9825 | |||
| 9826 | # add format fields to data array and replace keys with column indices | ||
| 9827 | $fmt->[3] = add_fmt($self, map {"$fmt->[4]|$_"} @{$fmt->[3]}); | ||
| 9828 | |||
| 9829 | # if entry has ColorSpecification | ||
| 9830 | if (defined($fmt->[6])) { | ||
| 9831 | |||
| 9832 | # add ColorSpecification attribute to colorimetry array | ||
| 9833 | for (@{$fmt->[3]}) {$self->[2][5][$_] = $fmt->[6]} | ||
| 9834 | |||
| 9835 | } | ||
| 9836 | |||
| 9837 | } | ||
| 9838 | |||
| 9839 | # return | ||
| 9840 | return($ops_hash); | ||
| 9841 | |||
| 9842 | } | ||
| 9843 | |||
| 9844 | # read CxF3 ColorSpecification nodes | ||
| 9845 | # parameters: (object_reference, CxF3_root, CxF3_prefix) | ||
| 9846 | sub _readCxF3colorspec { | ||
| 9847 | |||
| 9848 | # get parameters | ||
| 9849 | my ($self, $root, $ns) = @_; | ||
| 9850 | |||
| 9851 | # local variables | ||
| 9852 | my (@keys, @cspec, $id, $node, $child, $value); | ||
| 9853 | |||
| 9854 | # make CxF3 => ASCII mapping table (from ISO 17972-1, Annex A) | ||
| 9855 | @keys = ( | ||
| 9856 | ["$ns:MeasurementSpec/$ns:GeometryChoice" => 'MEASUREMENT_GEOMETRY'], | ||
| 9857 | ["$ns:MeasurementSpec/$ns:Device/$ns:DeviceFilter" => 'FILTER'], | ||
| 9858 | ["$ns:MeasurementSpec/$ns:Device/$ns:DeviceIllumination" => 'MEASUREMENT_SOURCE'], | ||
| 9859 | ["$ns:MeasurementSpec/$ns:CalibrationStandard" => 'DEVCALSTD'], | ||
| 9860 | ); | ||
| 9861 | |||
| 9862 | # find the ColorSpecification nodes | ||
| 9863 | @cspec = $root->findnodes("$ns:Resources/$ns:ColorSpecificationCollection/$ns:ColorSpecification"); | ||
| 9864 | |||
| 9865 | # for each ColorSpecification node | ||
| 9866 | for my $s (@cspec) { | ||
| 9867 | |||
| 9868 | # get the Id attribute and skip if 'Unknown' | ||
| 9869 | next if (($id = $s->getAttribute('Id')) eq 'Unknown'); | ||
| 9870 | |||
| 9871 | # for each entry in mapping table | ||
| 9872 | for my $i (0 .. $#keys) { | ||
| 9873 | |||
| 9874 | # if XPath is found | ||
| 9875 | if (($node) = $s->findnodes($keys[$i][0])) { | ||
| 9876 | |||
| 9877 | # get the first non-blank child node | ||
| 9878 | if (($child) = $node->nonBlankChildNodes()) { | ||
| 9879 | |||
| 9880 | # if child is an element node | ||
| 9881 | if ($child->nodeType() == 1) { | ||
| 9882 | |||
| 9883 | # serialize node | ||
| 9884 | $value = $node->toString(1); | ||
| 9885 | |||
| 9886 | # remove tabs and endlines | ||
| 9887 | $value =~ s/[\t\n]+//g; | ||
| 9888 | |||
| 9889 | # remove namespace prefix | ||
| 9890 | $value =~ s/([<\/])$ns:/$1/g; | ||
| 9891 | |||
| 9892 | # if child is a text node | ||
| 9893 | } elsif ($child->nodeType() == 3) { | ||
| 9894 | |||
| 9895 | # get the value | ||
| 9896 | $value = $node->textContent(); | ||
| 9897 | |||
| 9898 | } | ||
| 9899 | |||
| 9900 | # save in header line array | ||
| 9901 | push(@{$self->[3]}, [$keys[$i][1], "\"$value\"", $id]); | ||
| 9902 | |||
| 9903 | } | ||
| 9904 | |||
| 9905 | } | ||
| 9906 | |||
| 9907 | } | ||
| 9908 | |||
| 9909 | } | ||
| 9910 | |||
| 9911 | } | ||
| 9912 | |||
| 9913 | # make CxF3 FileInformation nodes | ||
| 9914 | # optional hash parameter contains 'cc:FileInformation' filter array | ||
| 9915 | # parameters: (object_reference, CxF3_root, CxF3_prefix, CxF3_namespace_URI, hash) | ||
| 9916 | # returns: (datetime) | ||
| 9917 | sub _makeCxF3fileinfo { | ||
| 9918 | |||
| 9919 | # get parameters | ||
| 9920 | my ($self, $root, $ns, $nsURI, $hash) = @_; | ||
| 9921 | |||
| 9922 | # local variables | ||
| 9923 | my (@filter, $t, $datetime, $info, %keys); | ||
| 9924 | my ($keyword, $value, $source, $node, $child); | ||
| 9925 | |||
| 9926 | # get filter array (if any) | ||
| 9927 | @filter = @{ICC::Shared::flatten($hash->{'cc:FileInformation'})} if (defined($hash->{'cc:FileInformation'})); | ||
| 9928 | |||
| 9929 | # make Time::Piece object | ||
| 9930 | $t = localtime(); | ||
| 9931 | |||
| 9932 | # get the 'FileInformation' node | ||
| 9933 | ($info) = $root->findnodes("$ns:FileInformation"); | ||
| 9934 | |||
| 9935 | # make ASCII => CxF3 mapping table (from ISO 17972-1, Annex A) | ||
| 9936 | %keys = ('ORIGINATOR' => "$ns:Creator", 'FILE_DESCRIPTOR' => "$ns:Description", 'CXF3_COMMENT' => "$ns:Comment"); | ||
| 9937 | |||
| 9938 | # for each file header line | ||
| 9939 | for (@{$self->[3]}) { | ||
| 9940 | |||
| 9941 | # get keyword, value and source | ||
| 9942 | ($keyword, $value, $source) = @{$_}; | ||
| 9943 | |||
| 9944 | # strip quotes from value | ||
| 9945 | $value =~ s/^\"(.*)\"$/$1/; | ||
| 9946 | |||
| 9947 | # if keyword is 'CREATED' | ||
| 9948 | if ($keyword eq 'CREATED') { | ||
| 9949 | |||
| 9950 | # make Time::Piece object from 'CREATED' value | ||
| 9951 | $t = _makeTimePiece($value); | ||
| 9952 | |||
| 9953 | # if source is 'FileInformation' or keyword is in filter array | ||
| 9954 | } elsif ((defined($source) && $source eq 'FileInformation') || grep {$_ eq $keyword} @filter) { | ||
| 9955 | |||
| 9956 | # if keyword in mapping table | ||
| 9957 | if (exists($keys{$keyword})) { | ||
| 9958 | |||
| 9959 | # if XPath exists in FileInformation element | ||
| 9960 | if (($node) = $info->findnodes($keys{$keyword})) { | ||
| 9961 | |||
| 9962 | # if text content exists | ||
| 9963 | if ((($child) = $node->nonBlankChildNodes) && $child->nodeType == 3) { | ||
| 9964 | |||
| 9965 | # update text content | ||
| 9966 | $child->setData($value); | ||
| 9967 | |||
| 9968 | } | ||
| 9969 | |||
| 9970 | } | ||
| 9971 | |||
| 9972 | # must be a 'Tag' element | ||
| 9973 | } else { | ||
| 9974 | |||
| 9975 | # if XPath exists in FileInformation element | ||
| 9976 | if (($node) = $info->findnodes("$ns:Tag[\@Name='$keyword']")) { | ||
| 9977 | |||
| 9978 | # update the Value attribute | ||
| 9979 | $node->setAttribute('Value', $value); | ||
| 9980 | |||
| 9981 | } else { | ||
| 9982 | |||
| 9983 | # add new Tag element | ||
| 9984 | $node = $info->appendChild(XML::LibXML::Element->new('Tag')); | ||
| 9985 | $node->setAttribute('Name', $keyword); | ||
| 9986 | $node->setAttribute('Value', $value); | ||
| 9987 | $node->setNamespace($nsURI, $ns); | ||
| 9988 | |||
| 9989 | } | ||
| 9990 | |||
| 9991 | } | ||
| 9992 | |||
| 9993 | } | ||
| 9994 | |||
| 9995 | } | ||
| 9996 | |||
| 9997 | # make ISO 8601 datetime string from Time::Piece object | ||
| 9998 | $datetime = $t->strftime('%Y-%m-%dT%T%z'); | ||
| 9999 | substr($datetime, -2, 0, ':'); | ||
| 10000 | |||
| 10001 | # get the 'CreationDate' node | ||
| 10002 | ($node) = $info->findnodes("$ns:CreationDate"); | ||
| 10003 | |||
| 10004 | # if text content exists | ||
| 10005 | if ((($child) = $node->nonBlankChildNodes) && $child->nodeType == 3) { | ||
| 10006 | |||
| 10007 | # update text content | ||
| 10008 | $child->setData($datetime); | ||
| 10009 | |||
| 10010 | } | ||
| 10011 | |||
| 10012 | # return datetime | ||
| 10013 | return($datetime); | ||
| 10014 | |||
| 10015 | } | ||
| 10016 | |||
| 10017 | # make CxF3 write operations array | ||
| 10018 | # parameters: (object_reference, CxF3_root, CxF3_prefix, column_slice) | ||
| 10019 | # returns: (array_ref) | ||
| 10020 | sub _makeCxF3writeops { | ||
| 10021 | |||
| 10022 | # get parameters | ||
| 10023 | my ($self, $root, $ns, $cols) = @_; | ||
| 10024 | |||
| 10025 | # local variables | ||
| 10026 | my ($n, %keys, $table, $class, $prefix, $key, $ops, $groups, $sort); | ||
| 10027 | |||
| 10028 | # if column slice defined | ||
| 10029 | if (defined($cols)) { | ||
| 10030 | |||
| 10031 | # if column slice an empty array reference | ||
| 10032 | if (ref($cols) eq 'ARRAY' && @{$cols} == 0) { | ||
| 10033 | |||
| 10034 | # use all columns | ||
| 10035 | $cols = [0 .. $#{$self->[1][0]}]; | ||
| 10036 | |||
| 10037 | } else { | ||
| 10038 | |||
| 10039 | # flatten column slice | ||
| 10040 | $cols = ICC::Shared::flatten($cols); | ||
| 10041 | |||
| 10042 | } | ||
| 10043 | |||
| 10044 | } else { | ||
| 10045 | |||
| 10046 | # use all columns | ||
| 10047 | $cols = [0 .. $#{$self->[1][0]}]; | ||
| 10048 | |||
| 10049 | } | ||
| 10050 | |||
| 10051 | # get number of fields | ||
| 10052 | $n = @{$cols}; | ||
| 10053 | |||
| 10054 | # remove undefined keys | ||
| 10055 | @{$cols} = grep {defined($self->[1][0][$_])} @{$cols}; | ||
| 10056 | |||
| 10057 | # warn if undefined keys | ||
| 10058 | ($n == @{$cols}) || warn('undefined keys in column slice'); | ||
| 10059 | |||
| 10060 | # get number of fields | ||
| 10061 | $n = @{$cols}; | ||
| 10062 | |||
| 10063 | # remove duplicate keys | ||
| 10064 | @{$cols} = grep {! $keys{$self->[1][0][$_]}++} @{$cols}; | ||
| 10065 | |||
| 10066 | # warn if duplicate keys | ||
| 10067 | ($n == @{$cols}) || warn('duplicate keys in column slice'); | ||
| 10068 | |||
| 10069 | # table structure: [data_class, CxF3_main_path, [CxF3_sub_paths], regex, sort_order] | ||
| 10070 | # some mappings have no sub-paths, which is indicated by an empty sub_path array | ||
| 10071 | # sort_order array contains the last character(s) of the format keys, and is optional | ||
| 10072 | # the 'NCLR', 'SPECTRAL' and 'DENSITY' data classes are special cases | ||
| 10073 | $table = [ | ||
| 10074 | ['RGB', "$ns:DeviceColorValues/$ns:ColorRGB", ["$ns:R", "$ns:G", "$ns:B"], qr/^(?:(.*)\|)?RGB_[RGB]$/, [qw(R G B)]], | ||
| 10075 | ['CMYK', "$ns:DeviceColorValues/$ns:ColorCMYK", ["$ns:Cyan", "$ns:Magenta", "$ns:Yellow", "$ns:Black"], qr/^(?:(.*)\|)?CMYK_[CMYK]$/, [qw(C M Y K)]], | ||
| 10076 | ['NCLR', "$ns:DeviceColorValues/$ns:ColorCMYKPlusN", ["$ns:Cyan", "$ns:Magenta", "$ns:Yellow", "$ns:Black"], qr/^(?:(.*)\|)?[2-9A-F]CLR_[1-9A-F]$/], | ||
| 10077 | ['SPECTRAL', "$ns:ColorValues/$ns:ReflectanceSpectrum", [], qr/^(?:(.*)\|)?(?:nm|SPECTRAL_NM_|SPECTRAL_NM|SPECTRAL_|NM_|R_)\d{3}$/], | ||
| 10078 | ['DENSITY', "$ns:ColorValues/$ns:ColorDensity/$ns:Density", [], qr/^(?:(.*)\|)?D_(?:RED|GREEN|BLUE|VIS)$/, [qw(RED GREEN BLUE VIS)]], | ||
| 10079 | ['XYZ', "$ns:ColorValues/$ns:ColorCIEXYZ", ["$ns:X", "$ns:Y", "$ns:Z"], qr/^(?:(.*)\|)?XYZ_[XYZ]$/, [qw(X Y Z)]], | ||
| 10080 | ['XYY', "$ns:ColorValues/$ns:ColorCIExyY", ["$ns:x", "$ns:y", "$ns:Y"], qr/^(?:(.*)\|)?XYY_(?:X|Y|CAPY)$/, [qw(_X _Y _CAPY)]], | ||
| 10081 | ['LAB', "$ns:ColorValues/$ns:ColorCIELab", ["$ns:L", "$ns:A", "$ns:B"], qr/^(?:(.*)\|)?LAB_[LAB]$/, [qw(L A B)]], | ||
| 10082 | ['LCH', "$ns:ColorValues/$ns:ColorCIELCh", ["$ns:L", "$ns:C", "$ns:H"], qr/^(?:(.*)\|)?LAB_[LCH]$/, [qw(L C H)]], | ||
| 10083 | ['DE', "$ns:ColorDifferenceValues/$ns:DeltaCIELab/$ns:dE", [], qr/^(?:(.*)\|)?LAB_DE$/], | ||
| 10084 | ['DE94', "$ns:ColorDifferenceValues/$ns:DeltaCIELab/$ns:dE94", [], qr/^(?:(.*)\|)?LAB_DE94$/], | ||
| 10085 | ['DECMC', "$ns:ColorDifferenceValues/$ns:DeltaCIELab/$ns:dEcmc", [], qr/^(?:(.*)\|)?LAB_CMC$/], | ||
| 10086 | ['DE2000', "$ns:ColorDifferenceValues/$ns:DeltaCIELab/$ns:dE2000", [], qr/^(?:(.*)\|)?LAB_2000$/], | ||
| 10087 | ]; | ||
| 10088 | |||
| 10089 | # following section builds operations array from column slice | ||
| 10090 | # | ||
| 10091 | # sort keys alphabetically | ||
| 10092 | @{$cols} = sort {$self->[1][0][$a] cmp $self->[1][0][$b]} @{$cols}; | ||
| 10093 | |||
| 10094 | # for each field | ||
| 10095 | for my $i (@{$cols}) { | ||
| 10096 | |||
| 10097 | # if key matches current class and prefix (prefix could be undefined) | ||
| 10098 | if (defined($class) && $self->[1][0][$i] =~ /$table->[$class][3]/ && (defined($prefix) ? $prefix : "\n") eq (defined($1) ? $1 : "\n")) { | ||
| 10099 | |||
| 10100 | # add index to current operation | ||
| 10101 | push(@{$ops->[-1][4]}, $i); | ||
| 10102 | |||
| 10103 | } else { | ||
| 10104 | |||
| 10105 | # for each data class | ||
| 10106 | for my $j (0 .. $#{$table}) { | ||
| 10107 | |||
| 10108 | # if key matches class | ||
| 10109 | if ($self->[1][0][$i] =~ /$table->[$j][3]/) { | ||
| 10110 | |||
| 10111 | # save current prefix | ||
| 10112 | $prefix = $1; | ||
| 10113 | |||
| 10114 | # save current class | ||
| 10115 | $class = $j; | ||
| 10116 | |||
| 10117 | # add new operation | ||
| 10118 | push(@{$ops}, [$table->[$j][0], $prefix, $table->[$j][1], $table->[$j][2], [$i], {}, $j]); | ||
| 10119 | |||
| 10120 | # quit loop | ||
| 10121 | last; | ||
| 10122 | |||
| 10123 | # if no match found in table | ||
| 10124 | } elsif ($j == $#{$table}) { | ||
| 10125 | |||
| 10126 | # match prefix/key | ||
| 10127 | $self->[1][0][$i] =~ m/^(?:(.*)\|)?(.*)/; | ||
| 10128 | |||
| 10129 | # save matched values | ||
| 10130 | $prefix = $1; | ||
| 10131 | $key = $2; | ||
| 10132 | |||
| 10133 | # set current class | ||
| 10134 | $class = undef; | ||
| 10135 | |||
| 10136 | # if prefix defined, and not Target or ...Measurement, and key is SAMPLE_NAME | ||
| 10137 | if (defined($prefix) && $prefix !~ m/^Target$|Measurement$/ && $key =~ m/^SAMPLE_NAME$|^SampleName$/) { | ||
| 10138 | |||
| 10139 | # add special operation to set 'Object' 'Name' attribute to SAMPLE_NAME | ||
| 10140 | push(@{$ops}, ['TAG', $prefix, '', [], [], {'Name' => [$i]}, -1]); | ||
| 10141 | |||
| 10142 | } else { | ||
| 10143 | |||
| 10144 | # add Tag operation | ||
| 10145 | push(@{$ops}, ['TAG', $prefix, "$ns:TagCollection/$ns:Tag", [], [], {'Name' => $key, 'Value' => [$i]}, 100]); | ||
| 10146 | |||
| 10147 | } | ||
| 10148 | |||
| 10149 | } | ||
| 10150 | |||
| 10151 | } | ||
| 10152 | |||
| 10153 | } | ||
| 10154 | |||
| 10155 | } | ||
| 10156 | |||
| 10157 | # following section sorts and verifies column slices, sets default prefixes and checks for multiple elements | ||
| 10158 | # | ||
| 10159 | # init loop variable | ||
| 10160 | %keys = (); | ||
| 10161 | |||
| 10162 | # for each array entry | ||
| 10163 | for my $t (@{$ops}) { | ||
| 10164 | |||
| 10165 | # if sort order is defined | ||
| 10166 | if (defined($table->[$t->[6]][4])) { | ||
| 10167 | |||
| 10168 | # arrange column indices in sort order | ||
| 10169 | @{$t->[4]} = map {my $end = $_; grep {$self->[1][0][$_] =~ m/$end$/} @{$t->[4]}} @{$table->[$t->[6]][4]}; | ||
| 10170 | |||
| 10171 | } | ||
| 10172 | |||
| 10173 | # if class is SPECTRAL | ||
| 10174 | if ($t->[0] eq 'SPECTRAL') { | ||
| 10175 | |||
| 10176 | # verify spectral slice | ||
| 10177 | (@{$t->[4]} == @{_spectral($self, $t->[1])}) || warn("invalid column slice - SPECTRAL class"); | ||
| 10178 | |||
| 10179 | # if class is DENSITY | ||
| 10180 | } elsif ($t->[0] eq 'DENSITY') { | ||
| 10181 | |||
| 10182 | # to be done | ||
| 10183 | |||
| 10184 | # if class is NCLR | ||
| 10185 | } elsif ($t->[0] eq 'NCLR') { | ||
| 10186 | |||
| 10187 | # match first key to get number of channels | ||
| 10188 | $self->[1][0][$t->[4][0]] =~ m/([2-9A-F])CLR_[1-9A-F]$/; | ||
| 10189 | |||
| 10190 | # verify nCLR slice | ||
| 10191 | (@{$t->[4]} == CORE::hex($1)) || warn("invalid column slice - NCLR class"); | ||
| 10192 | |||
| 10193 | # all others | ||
| 10194 | } else { | ||
| 10195 | |||
| 10196 | # verify subpaths match column slice | ||
| 10197 | (@{$t->[4]} == @{$t->[3]} || (@{$t->[4]} == 1 && @{$t->[3]} == 0)) || warn("invalid column slice - $t->[0] class"); | ||
| 10198 | |||
| 10199 | } | ||
| 10200 | |||
| 10201 | # if prefix undefined | ||
| 10202 | if (! defined($t->[1])) { | ||
| 10203 | |||
| 10204 | # if XPath contains 'ColorValues' or 'ColorDifferenceValues' | ||
| 10205 | if ($t->[2] =~ m/^$ns:(?:ColorValues|ColorDifferenceValues)\//) { | ||
| 10206 | |||
| 10207 | # set prefix to M0_Measurement | ||
| 10208 | $t->[1] = 'M0_Measurement'; | ||
| 10209 | |||
| 10210 | # if XPath contains 'DeviceColorValues' | ||
| 10211 | } elsif ($t->[2] =~ m/^$ns:DeviceColorValues\//) { | ||
| 10212 | |||
| 10213 | # set prefix to Target | ||
| 10214 | $t->[1] = 'Target'; | ||
| 10215 | |||
| 10216 | # all others | ||
| 10217 | } else { | ||
| 10218 | |||
| 10219 | # set prefix to '~~' | ||
| 10220 | $t->[1] = '~~'; | ||
| 10221 | |||
| 10222 | } | ||
| 10223 | |||
| 10224 | } | ||
| 10225 | |||
| 10226 | # for 'ColorValues' or 'DeviceColorValues' | ||
| 10227 | if ($t->[2] =~ m/^$ns:(ColorValues|DeviceColorValues)\//) { | ||
| 10228 | |||
| 10229 | # warn on multiple elements (not allowed by i1Profiler) | ||
| 10230 | print "warning: multiple $1 elements in CxF3 $t->[1] object\n" if ($keys{"$1/$t->[1]"}++ == 1); | ||
| 10231 | |||
| 10232 | } | ||
| 10233 | |||
| 10234 | } | ||
| 10235 | |||
| 10236 | # following section groups operations by prefix | ||
| 10237 | # | ||
| 10238 | # sort by prefix, then by table index | ||
| 10239 | @{$ops} = sort {($a->[1] cmp $b->[1]) or ($a->[6] <=> $b->[6])} @{$ops}; | ||
| 10240 | |||
| 10241 | # init loop variable | ||
| 10242 | $prefix = undef; | ||
| 10243 | |||
| 10244 | # for each operation | ||
| 10245 | for my $t (@{$ops}) { | ||
| 10246 | |||
| 10247 | # if same prefix as last operation | ||
| 10248 | if (defined($prefix) && $prefix eq $t->[1]) { | ||
| 10249 | |||
| 10250 | # add operation to last group | ||
| 10251 | push(@{$groups->[-1]}, $t); | ||
| 10252 | |||
| 10253 | # if class is TAG and prefix is '~~' | ||
| 10254 | } elsif ($t->[0] eq 'TAG' && $t->[1] eq '~~') { | ||
| 10255 | |||
| 10256 | # for each group | ||
| 10257 | for my $g (@{$groups}) { | ||
| 10258 | |||
| 10259 | # add operation | ||
| 10260 | push(@{$g}, $t); | ||
| 10261 | |||
| 10262 | } | ||
| 10263 | |||
| 10264 | # set prefix | ||
| 10265 | $prefix = undef; | ||
| 10266 | |||
| 10267 | # others | ||
| 10268 | } else { | ||
| 10269 | |||
| 10270 | # add new group | ||
| 10271 | push(@{$groups}, [$t]); | ||
| 10272 | |||
| 10273 | # set prefix | ||
| 10274 | $prefix = $t->[1]; | ||
| 10275 | |||
| 10276 | } | ||
| 10277 | |||
| 10278 | } | ||
| 10279 | |||
| 10280 | # return | ||
| 10281 | return($groups); | ||
| 10282 | |||
| 10283 | } | ||
| 10284 | |||
| 10285 | # make CxF3 ColorSpecification nodes | ||
| 10286 | # parameters: (object_reference, CxF3_root, CxF3_prefix, CxF3_namespace_URI, operations_array) | ||
| 10287 | sub _makeCxF3colorspec { | ||
| 10288 | |||
| 10289 | # get parameters | ||
| 10290 | my ($self, $root, $ns, $nsURI, $ops) = @_; | ||
| 10291 | |||
| 10292 | # local variables | ||
| 10293 | my (@illum, @filter, $cscol, $template, $unknown); | ||
| 10294 | my (%table, %cspec, %hash, $keyword, $value, $source); | ||
| 10295 | my ($Id, $cs, @nodes, $node, $child, @wav); | ||
| 10296 | my ($parser, $frag, $std, $xpath); | ||
| 10297 | |||
| 10298 | # illumination types | ||
| 10299 | @illum = qw(M0_Incandescent M1_Daylight M2_UVExcluded M3_Polarized); | ||
| 10300 | |||
| 10301 | # filter types | ||
| 10302 | @filter = qw(Filter_None Filter_None Filter_UVExcluded Filter_None); | ||
| 10303 | |||
| 10304 | # get the 'ColorSpecificationCollection' node | ||
| 10305 | ($cscol) = $root->findnodes("$ns:Resources/$ns:ColorSpecificationCollection"); | ||
| 10306 | |||
| 10307 | # get the 'ColorSpecification' node with Id = 'template' | ||
| 10308 | ($template) = $cscol->findnodes("$ns:ColorSpecification[\@Id='template']"); | ||
| 10309 | |||
| 10310 | # get the 'ColorSpecification' node with Id = 'Unknown' | ||
| 10311 | ($unknown) = $cscol->findnodes("$ns:ColorSpecification[\@Id='Unknown']"); | ||
| 10312 | |||
| 10313 | # make ASCII => CxF3 mapping table (from ISO 17972-1, Annex A) | ||
| 10314 | %table = ( | ||
| 10315 | 'MANUFACTURER' => "$ns:MeasurementSpec/$ns:Device/$ns:Manufacturer", | ||
| 10316 | 'MODEL' => "$ns:MeasurementSpec/$ns:Device/$ns:Model", | ||
| 10317 | 'SERIAL_NUMBER' => "$ns:MeasurementSpec/$ns:Device/$ns:SerialNumber", | ||
| 10318 | 'MEASUREMENT_GEOMETRY' => "$ns:MeasurementSpec/$ns:GeometryChoice", | ||
| 10319 | 'MEASUREMENT_SOURCE' => "$ns:MeasurementSpec/$ns:Device/$ns:DeviceIllumination", | ||
| 10320 | 'FILTER' => "$ns:MeasurementSpec/$ns:Device/$ns:DeviceFilter", | ||
| 10321 | 'POLARIZATION' => "$ns:MeasurementSpec/$ns:Device/$ns:DevicePolarization", | ||
| 10322 | 'SAMPLE_BACKING' => "$ns:MeasurementSpec/$ns:Backing", | ||
| 10323 | 'DEVCALSTD' => "$ns:MeasurementSpec/$ns:CalibrationStandard", | ||
| 10324 | ); | ||
| 10325 | |||
| 10326 | # for each group | ||
| 10327 | for my $group (@{$ops}) { | ||
| 10328 | |||
| 10329 | # for each operation | ||
| 10330 | for my $t (@{$group}) { | ||
| 10331 | |||
| 10332 | # if ColorValues (only ColorValues reference a ColorSpecification) | ||
| 10333 | if ($t->[2] =~ m/^$ns:ColorValues\//) { | ||
| 10334 | |||
| 10335 | # set Id to saved value, if defined, or add '_spec' to prefix | ||
| 10336 | # the ColorSpecification Id is saved in the Colorimetry array when reading a CxF3 file | ||
| 10337 | $Id = defined($self->[2][5][$t->[4][0]]) ? $self->[2][5][$t->[4][0]] : "$t->[1]\_spec"; | ||
| 10338 | |||
| 10339 | # set attribute hash | ||
| 10340 | $t->[5]{'ColorSpecification'} = $Id; | ||
| 10341 | |||
| 10342 | # if 'ColorSpecification' undefined | ||
| 10343 | if (! $cspec{$Id}++) { | ||
| 10344 | |||
| 10345 | # initialize keyword hash | ||
| 10346 | %hash = (); | ||
| 10347 | |||
| 10348 | # add cloned 'ColorSpecification' element to 'ColorSpecificationCollection' | ||
| 10349 | $cs = $cscol->appendChild($template->cloneNode(1)); | ||
| 10350 | |||
| 10351 | # set the Id | ||
| 10352 | $cs->setAttribute('Id', $Id); | ||
| 10353 | |||
| 10354 | # if spectral data | ||
| 10355 | # there are three types of spectral data, reflective, transmissive and emissive | ||
| 10356 | # spectral data has a WavelengthRange node which contains the starting wavelength and increment | ||
| 10357 | if ($t->[2] =~ m/(Reflectance|Transmittance|Emissive)Spectrum$/) { | ||
| 10358 | |||
| 10359 | # get the 'MeasurementType' node | ||
| 10360 | ($node) = $cs->findnodes("$ns:MeasurementSpec/$ns:MeasurementType"); | ||
| 10361 | |||
| 10362 | # if text content exists | ||
| 10363 | if ((($child) = $node->nonBlankChildNodes) && $child->nodeType == 3) { | ||
| 10364 | |||
| 10365 | # update text content | ||
| 10366 | $child->setData("Spectrum_$1"); | ||
| 10367 | |||
| 10368 | } | ||
| 10369 | |||
| 10370 | # for first two data columns | ||
| 10371 | for ($t->[4][0], $t->[4][1]) { | ||
| 10372 | |||
| 10373 | # match wavelength in format key | ||
| 10374 | $self->[1][0][$_] =~ m/(\d{3})$/; | ||
| 10375 | |||
| 10376 | # push to array | ||
| 10377 | push(@wav, $1); | ||
| 10378 | |||
| 10379 | } | ||
| 10380 | |||
| 10381 | # find the 'WavelengthRange' node | ||
| 10382 | ($node) = $cs->findnodes("$ns:MeasurementSpec/$ns:WavelengthRange"); | ||
| 10383 | |||
| 10384 | # set the 'StartWL' attribute | ||
| 10385 | $node->setAttribute('StartWL', $wav[0]); | ||
| 10386 | |||
| 10387 | # set the 'Increment' attribute | ||
| 10388 | $node->setAttribute('Increment', $wav[1] - $wav[0]); | ||
| 10389 | |||
| 10390 | # set operation 'StartWL' attribute | ||
| 10391 | $t->[5]{'StartWL'} = $wav[0]; | ||
| 10392 | |||
| 10393 | } else { | ||
| 10394 | |||
| 10395 | # find the 'WavelengthRange' node | ||
| 10396 | ($node) = $cs->findnodes("$ns:MeasurementSpec/$ns:WavelengthRange"); | ||
| 10397 | |||
| 10398 | # unbind the node (used only with spectral data) | ||
| 10399 | $node->unbindNode(); | ||
| 10400 | |||
| 10401 | } | ||
| 10402 | |||
| 10403 | # for each file header entry | ||
| 10404 | for (@{$self->[3]}) { | ||
| 10405 | |||
| 10406 | # get keyword, value and source | ||
| 10407 | ($keyword, $value, $source) = @{$_}; | ||
| 10408 | |||
| 10409 | # strip quotes from value | ||
| 10410 | $value =~ s/^\"(.*)\"$/$1/; | ||
| 10411 | |||
| 10412 | # if source is ColorSpecification Id | ||
| 10413 | if (defined($source) && $source eq $Id) { | ||
| 10414 | |||
| 10415 | # add keyword to hash | ||
| 10416 | $hash{$keyword}++; | ||
| 10417 | |||
| 10418 | # if keyword in table | ||
| 10419 | if (exists($table{$keyword})) { | ||
| 10420 | |||
| 10421 | # if XPath does not exist in ColorSpecification element | ||
| 10422 | if (! (($node) = $cs->findnodes($table{$keyword}))) { | ||
| 10423 | |||
| 10424 | # set node | ||
| 10425 | $node = $cs; | ||
| 10426 | |||
| 10427 | # initialize XPath | ||
| 10428 | $xpath = undef; | ||
| 10429 | |||
| 10430 | # for each segment | ||
| 10431 | for (split(/\//, $table{$keyword})) { | ||
| 10432 | |||
| 10433 | # add segment to XPath | ||
| 10434 | $xpath = defined($xpath) ? "$xpath/$_" : $_; | ||
| 10435 | |||
| 10436 | # if XPath does not exist in ColorSpecification element | ||
| 10437 | if (! (($node) = $cs->findnodes($xpath))) { | ||
| 10438 | |||
| 10439 | # add element for XPath segment | ||
| 10440 | $node = $node->appendChild(XML::LibXML::Element->new($_)); | ||
| 10441 | $node->setNamespace($nsURI, $ns); | ||
| 10442 | |||
| 10443 | } | ||
| 10444 | |||
| 10445 | } | ||
| 10446 | |||
| 10447 | } | ||
| 10448 | |||
| 10449 | # get the first non-blank child node | ||
| 10450 | ($child) = $node->nonBlankChildNodes(); | ||
| 10451 | |||
| 10452 | # make a parser object | ||
| 10453 | $parser = XML::LibXML->new(); | ||
| 10454 | |||
| 10455 | # if value is an XML balanced chunk | ||
| 10456 | if ($value =~ m/</ && eval{$frag = $parser->parse_balanced_chunk($value)}) { | ||
| 10457 | |||
| 10458 | # get all element nodes | ||
| 10459 | @nodes = $frag->findnodes('//*'); | ||
| 10460 | |||
| 10461 | # replace existing node | ||
| 10462 | $node->replaceNode($frag); | ||
| 10463 | |||
| 10464 | # set namespace of each element | ||
| 10465 | for (@nodes) {$_->setNamespace($nsURI, $ns)}; | ||
| 10466 | |||
| 10467 | # if no child node | ||
| 10468 | } elsif (! defined($child)) { | ||
| 10469 | |||
| 10470 | # set text content to value | ||
| 10471 | $node->appendText($value); | ||
| 10472 | |||
| 10473 | # if child node is text type | ||
| 10474 | } elsif ($child->nodeType == 3) { | ||
| 10475 | |||
| 10476 | # modify existing text content | ||
| 10477 | $child->setData($value); | ||
| 10478 | |||
| 10479 | } | ||
| 10480 | |||
| 10481 | } | ||
| 10482 | |||
| 10483 | } | ||
| 10484 | |||
| 10485 | } | ||
| 10486 | |||
| 10487 | # match illumination standard in prefix (M0, M1, M2, M3) | ||
| 10488 | $std = ($t->[1] =~ m/^M([0-3])/) ? $1 : 0; | ||
| 10489 | |||
| 10490 | # if 'MEASUREMENT_SOURCE' not a keyword | ||
| 10491 | if (! exists($hash{'MEASUREMENT_SOURCE'})) { | ||
| 10492 | |||
| 10493 | # find the 'DeviceIllumination' node | ||
| 10494 | ($node) = $cs->findnodes("$ns:MeasurementSpec/$ns:Device/$ns:DeviceIllumination"); | ||
| 10495 | |||
| 10496 | # if text content exists | ||
| 10497 | if ((($child) = $node->nonBlankChildNodes) && $child->nodeType == 3) { | ||
| 10498 | |||
| 10499 | # update text content | ||
| 10500 | $child->setData($illum[$std]); | ||
| 10501 | |||
| 10502 | } | ||
| 10503 | |||
| 10504 | } | ||
| 10505 | |||
| 10506 | # if 'FILTER' not a keyword | ||
| 10507 | if (! exists($hash{'FILTER'})) { | ||
| 10508 | |||
| 10509 | # find the 'DeviceFilter' node | ||
| 10510 | ($node) = $cs->findnodes("$ns:MeasurementSpec/$ns:Device/$ns:DeviceFilter"); | ||
| 10511 | |||
| 10512 | # if text content exists | ||
| 10513 | if ((($child) = $node->nonBlankChildNodes) && $child->nodeType == 3) { | ||
| 10514 | |||
| 10515 | # update text content | ||
| 10516 | $child->setData($filter[$std]); | ||
| 10517 | |||
| 10518 | } | ||
| 10519 | |||
| 10520 | } | ||
| 10521 | |||
| 10522 | # if 'POLARIZATION' not a keyword and M3 standard | ||
| 10523 | if (! exists($hash{'POLARIZATION'}) && $std == 3) { | ||
| 10524 | |||
| 10525 | # get the 'Device' node | ||
| 10526 | ($node) = $cs->findnodes("$ns:MeasurementSpec/$ns:Device"); | ||
| 10527 | |||
| 10528 | # add 'Polarization' node | ||
| 10529 | $node = $node->appendChild(XML::LibXML::Element->new("$ns:Polarization")); | ||
| 10530 | $node->appendText(XML::LibXML::Boolean->True); | ||
| 10531 | $node->setNamespace($nsURI, $ns); | ||
| 10532 | |||
| 10533 | } | ||
| 10534 | |||
| 10535 | } | ||
| 10536 | |||
| 10537 | # if 'DeviceColorValues' | ||
| 10538 | } elsif ($t->[2] =~ m/^$ns:DeviceColorValues\//) { | ||
| 10539 | |||
| 10540 | # set attributes hash | ||
| 10541 | $t->[5]{'ColorSpecification'} = 'Unknown'; | ||
| 10542 | |||
| 10543 | # increment 'ColorSpecification' hash | ||
| 10544 | $cspec{'Unknown'}++; | ||
| 10545 | |||
| 10546 | } | ||
| 10547 | |||
| 10548 | } | ||
| 10549 | |||
| 10550 | } | ||
| 10551 | |||
| 10552 | # unbind 'template' node | ||
| 10553 | $template->unbindNode(); | ||
| 10554 | |||
| 10555 | # unbind 'Unknown' node, if not referenced | ||
| 10556 | $unknown->unbindNode() if (! $cspec{'Unknown'}); | ||
| 10557 | |||
| 10558 | } | ||
| 10559 | |||
| 10560 | # validate CxF3 document | ||
| 10561 | # prints warning and error info | ||
| 10562 | # parameters: (document_reference) | ||
| 10563 | sub _validateCxF3 { | ||
| 10564 | |||
| 10565 | # get document reference | ||
| 10566 | my $doc = shift(); | ||
| 10567 | |||
| 10568 | # load CxF3 schema | ||
| 10569 | state $xmlschema = XML::LibXML::Schema->new('location' => ICC::Shared::getICCPath('Templates/CxF3_Core.xsd')); | ||
| 10570 | |||
| 10571 | # validate the document | ||
| 10572 | if (! defined(eval {$xmlschema->validate($doc)})) { | ||
| 10573 | |||
| 10574 | # print warning on failure | ||
| 10575 | print "warning: invalid CxF3 document\n$@\n"; | ||
| 10576 | |||
| 10577 | } | ||
| 10578 | |||
| 10579 | } | ||
| 10580 | |||
| 10581 | # make patch set | ||
| 10582 | # supported hash keys: 'colorspace', 'template', 'sort', 'limit' | ||
| 10583 | # parameters: (object_reference, hash) | ||
| 10584 | # returns: (result) | ||
| 10585 | sub _makePatchSet { | ||
| 10586 | |||
| 10587 | # get parameters | ||
| 10588 | my ($self, $hash) = @_; | ||
| 10589 | |||
| 10590 | # local variables | ||
| 10591 | my ($cs, $template, $sort, $tac, $n, $data, $eps); | ||
| 10592 | my (@fields, $loop, $limit, @inc, $init, $s, $code); | ||
| 10593 | |||
| 10594 | # get the colorspace parameter | ||
| 10595 | (defined($cs = $hash->{'colorspace'})) || return('colorspace parameter missing'); | ||
| 10596 | |||
| 10597 | # get the template parameter | ||
| 10598 | (defined($template = $hash->{'template'})) || return('template parameter missing'); | ||
| 10599 | |||
| 10600 | # get the sort parameter (optional) | ||
| 10601 | $sort = $hash->{'sort'}; | ||
| 10602 | |||
| 10603 | # get the ink limit parameter (optional) | ||
| 10604 | $tac = $hash->{'limit'}; | ||
| 10605 | |||
| 10606 | # get number of elements in first group | ||
| 10607 | $n = @{$template->[0]}; | ||
| 10608 | |||
| 10609 | # for each group | ||
| 10610 | for my $i (0 .. $#{$template}) { | ||
| 10611 | |||
| 10612 | # verify number of elements | ||
| 10613 | ($n == @{$template->[$i]}) || return("wrong number of elements in template group $i"); | ||
| 10614 | |||
| 10615 | # verify number of array references | ||
| 10616 | ($n == grep {ref() eq 'ARRAY'} @{$template->[$i]}) || return("non-array element(s) in template group $i"); | ||
| 10617 | |||
| 10618 | # for each element | ||
| 10619 | for my $j (0 .. $#{$template->[$i]}) { | ||
| 10620 | |||
| 10621 | # verify element contains only numeric scalars | ||
| 10622 | (@{$template->[$i][$j]} > 0 && @{$template->[$i][$j]} == grep {! ref() && Scalar::Util::looks_like_number($_)} @{$template->[$i][$j]}) || return("non-numeric element in template group $i, $j"); | ||
| 10623 | |||
| 10624 | } | ||
| 10625 | |||
| 10626 | } | ||
| 10627 | |||
| 10628 | # if RGB colorspace | ||
| 10629 | if ($cs eq 'RGB') { | ||
| 10630 | |||
| 10631 | # verify number of channels | ||
| 10632 | ($n == 3) || return('wrong number of template elements for RGB colorspace'); | ||
| 10633 | |||
| 10634 | # set fields | ||
| 10635 | @fields = qw(RGB_R RGB_G RGB_B); | ||
| 10636 | |||
| 10637 | # if CMYK colorspace | ||
| 10638 | } elsif ($cs eq 'CMYK') { | ||
| 10639 | |||
| 10640 | # verify number of channels | ||
| 10641 | ($n == 4) || return('wrong number of template elements for CMYK colorspace'); | ||
| 10642 | |||
| 10643 | # set fields | ||
| 10644 | @fields = qw(CMYK_C CMYK_M CMYK_Y CMYK_K); | ||
| 10645 | |||
| 10646 | # if nCLR colorspace | ||
| 10647 | } elsif ($cs eq 'nCLR') { | ||
| 10648 | |||
| 10649 | # verify number of channels | ||
| 10650 | ($n > 0 && $n < 16) || return('wrong number of template elements for nCLR colorspace'); | ||
| 10651 | |||
| 10652 | # set fields | ||
| 10653 | @fields = map {$n . "CLR_$_"} (1 .. $n); | ||
| 10654 | |||
| 10655 | # if L*a*b* colorspace | ||
| 10656 | } elsif ($cs eq 'Lab') { | ||
| 10657 | |||
| 10658 | # verify number of channels | ||
| 10659 | ($n == 3) || return('wrong number of template elements for L*a*b* colorspace'); | ||
| 10660 | |||
| 10661 | # set fields | ||
| 10662 | @fields = qw(LAB_L LAB_A LAB_B); | ||
| 10663 | |||
| 10664 | } else { | ||
| 10665 | |||
| 10666 | # error | ||
| 10667 | return('invalid colorspace parameter'); | ||
| 10668 | |||
| 10669 | } | ||
| 10670 | |||
| 10671 | # make loop variable list | ||
| 10672 | $loop = join(', ', map {"\$i$_"} (0 .. $n - 1)); | ||
| 10673 | |||
| 10674 | # make initial code fragment | ||
| 10675 | $init = "\$data->[\$s++] = [$loop]"; | ||
| 10676 | |||
| 10677 | # initialize index | ||
| 10678 | $s = 0; | ||
| 10679 | |||
| 10680 | # for each group | ||
| 10681 | for my $i (0 .. $#{$template}) { | ||
| 10682 | |||
| 10683 | # copy initial code fragment | ||
| 10684 | $code = $init; | ||
| 10685 | |||
| 10686 | # for each device channel (in reverse order) | ||
| 10687 | for my $j (reverse(0 .. $#{$template->[$i]})) { | ||
| 10688 | |||
| 10689 | # add loop code to fragment | ||
| 10690 | $code = "for my \$i$j (" . join(', ', @{$template->[$i][$j]}) . ") {$code}"; | ||
| 10691 | |||
| 10692 | } | ||
| 10693 | |||
| 10694 | # evaluate code fragment | ||
| 10695 | eval($code); | ||
| 10696 | |||
| 10697 | } | ||
| 10698 | |||
| 10699 | # if ink limit defined and color space is CMYK or nCLR | ||
| 10700 | if (defined($tac) && ($cs eq 'CMYK' || $cs eq 'nCLR')) { | ||
| 10701 | |||
| 10702 | # compute max comparison error | ||
| 10703 | $eps = 1E-12; | ||
| 10704 | |||
| 10705 | # verify ink limit is a number | ||
| 10706 | if (! ref($tac) && Scalar::Util::looks_like_number($tac)) { | ||
| 10707 | |||
| 10708 | # for each patch | ||
| 10709 | for (@{$data}) { | ||
| 10710 | |||
| 10711 | # add the total ink value | ||
| 10712 | push(@{$_}, List::Util::sum(@{$_})); | ||
| 10713 | |||
| 10714 | } | ||
| 10715 | |||
| 10716 | # make sort code fragment (sorts in ascending order by columns K ... total_ink_value) | ||
| 10717 | $code = '@{$data} = sort {' . join(' || ', map {"\$a->[$_] <=> \$b->[$_]"} (3 .. $n)) . '} @{$data}'; | ||
| 10718 | |||
| 10719 | # sort data | ||
| 10720 | eval($code); | ||
| 10721 | |||
| 10722 | # for each patch | ||
| 10723 | for my $i (0 .. $#{$data}) { | ||
| 10724 | |||
| 10725 | # undefine limit if new group (different black or spot values) | ||
| 10726 | undef($limit) if (grep {$data->[$i][$_] != $data->[$i ? $i - 1 : 0][$_]} (3 .. $n - 1)); | ||
| 10727 | |||
| 10728 | # select patch if limit undefined or total ink <= limit or a CMY corner point | ||
| 10729 | push(@inc, [@{$data->[$i]}[0 .. $n - 1]]) if (! defined($limit) || ($data->[$i][-1] - $limit <= $eps) || ((grep {$data->[$i][$_] == 0} (0 .. 2)) && (grep {$data->[$i][$_] == 100} (0 .. 2)))); | ||
| 10730 | |||
| 10731 | # set limit if undefined and total ink > TAC | ||
| 10732 | $limit = $data->[$i][-1] if (! defined($limit) && $data->[$i][-1] - $tac > $eps); | ||
| 10733 | |||
| 10734 | } | ||
| 10735 | |||
| 10736 | # set data to selected patches | ||
| 10737 | $data = \@inc; | ||
| 10738 | |||
| 10739 | } else { | ||
| 10740 | |||
| 10741 | # display warning | ||
| 10742 | carp("invalid ink limit parameter, ink limiting failed\n"); | ||
| 10743 | |||
| 10744 | } | ||
| 10745 | |||
| 10746 | } | ||
| 10747 | |||
| 10748 | # if sort parameter defined | ||
| 10749 | if (defined($sort)) { | ||
| 10750 | |||
| 10751 | # verify sort parameter is an array of integer scalars | ||
| 10752 | if (ref($sort) eq 'ARRAY' && @{$sort} == grep {! ref() && Scalar::Util::looks_like_number($_) && $_ == int($_) && abs($_) > 0 && abs($_) <= $n} @{$sort}) { | ||
| 10753 | |||
| 10754 | # make sort code fragment | ||
| 10755 | $code = '@{$data} = sort {' . join(' || ', map {my $dir = m/-/; my $col = abs($_) - 1; $dir ? "\$b->[$col] <=> \$a->[$col]" : "\$a->[$col] <=> \$b->[$col]"} @{$sort}) . '} @{$data}'; | ||
| 10756 | |||
| 10757 | # evaluate code fragment | ||
| 10758 | eval($code); | ||
| 10759 | |||
| 10760 | } else { | ||
| 10761 | |||
| 10762 | # display warning | ||
| 10763 | carp("invalid sort parameter, sorting failed\n"); | ||
| 10764 | |||
| 10765 | } | ||
| 10766 | |||
| 10767 | } | ||
| 10768 | |||
| 10769 | # add format fields | ||
| 10770 | unshift(@{$data}, [@fields]); | ||
| 10771 | |||
| 10772 | # set object reference | ||
| 10773 | $self->[1] = $data; | ||
| 10774 | |||
| 10775 | # return | ||
| 10776 | return(); | ||
| 10777 | |||
| 10778 | } | ||
| 10779 | |||
| 10780 | # make Time::Piece object from text string | ||
| 10781 | # parses most common date/time notations | ||
| 10782 | # no object returned if parsing fails | ||
| 10783 | # parameter: (string -or- value) | ||
| 10784 | # returns: (object) | ||
| 10785 | sub _makeTimePiece { | ||
| 10786 | |||
| 10787 | # get parameter | ||
| 10788 | my $str = shift(); | ||
| 10789 | |||
| 10790 | # local variables | ||
| 10791 | my ($parse, $fmt, $hr, $sec, $month); | ||
| 10792 | |||
| 10793 | # if a numeric value (Unix time) | ||
| 10794 | if (Scalar::Util::looks_like_number($str)) { | ||
| 10795 | |||
| 10796 | # return Time::Piece object from Unix time | ||
| 10797 | return(scalar(localtime($str))); | ||
| 10798 | |||
| 10799 | } else { | ||
| 10800 | |||
| 10801 | # if UTC offset matched (time string ends in '+/-hh:mm', '+/-hhmm', or '+/-hh') | ||
| 10802 | if ($str =~ s/(T[\d:]+)([+-]\d{2}):?(\d{2})?/$1/) { | ||
| 10803 | |||
| 10804 | # set UTC offset to matched value | ||
| 10805 | $parse = $2 . (defined($3) ? $3 : '00'); | ||
| 10806 | |||
| 10807 | # set UTC format | ||
| 10808 | $fmt = '%z'; | ||
| 10809 | |||
| 10810 | # if Zulu time (time string ends in 'Z') | ||
| 10811 | } elsif ($str =~ s/(T[\d:]+)Z/$1/) { | ||
| 10812 | |||
| 10813 | # set UTC offset to 0 | ||
| 10814 | $parse = '+0000'; | ||
| 10815 | |||
| 10816 | # set UTC format | ||
| 10817 | $fmt = '%z'; | ||
| 10818 | |||
| 10819 | } else { | ||
| 10820 | |||
| 10821 | # initialize strings | ||
| 10822 | $parse = $fmt = ''; | ||
| 10823 | |||
| 10824 | } | ||
| 10825 | |||
| 10826 | # if time matched (time string 'hh:mm' or 'hh:mm:ss', 'AM' or 'PM' optional) | ||
| 10827 | if ($str =~ s/(\d{1,2})(:\d{1,2})(:\d{1,2})?\s*(AM|PM)?//) { | ||
| 10828 | |||
| 10829 | # if 12 AM | ||
| 10830 | if (defined($4) && $4 eq 'AM' && $1 == 12) { | ||
| 10831 | |||
| 10832 | # set hour | ||
| 10833 | $hr = 0; | ||
| 10834 | |||
| 10835 | # if 1 PM - 11 PM | ||
| 10836 | } elsif (defined($4) && $4 eq 'PM' && $1 > 0 && $1 < 12) { | ||
| 10837 | |||
| 10838 | # set hour | ||
| 10839 | $hr = $1 + 12; | ||
| 10840 | |||
| 10841 | } else { | ||
| 10842 | |||
| 10843 | # set hour | ||
| 10844 | $hr = $1; | ||
| 10845 | |||
| 10846 | } | ||
| 10847 | |||
| 10848 | # set seconds | ||
| 10849 | $sec = defined($3) ? $3 : ':00'; | ||
| 10850 | |||
| 10851 | # add time string | ||
| 10852 | $parse = "T$hr$2$sec$parse"; | ||
| 10853 | |||
| 10854 | # add time format | ||
| 10855 | $fmt = "T%T$fmt"; | ||
| 10856 | |||
| 10857 | } | ||
| 10858 | |||
| 10859 | # if three number date matched | ||
| 10860 | if ($str =~ m/(\d{1,4})[\/-](\d{1,2})[\/-](\d{1,4})/) { | ||
| 10861 | |||
| 10862 | # if first value > 99 | ||
| 10863 | if ($1 > 99) { | ||
| 10864 | |||
| 10865 | # add date string | ||
| 10866 | $parse = "$1-$2-$3$parse"; | ||
| 10867 | |||
| 10868 | # if last value > 99 | ||
| 10869 | } elsif ($3 > 99) { | ||
| 10870 | |||
| 10871 | # add date string | ||
| 10872 | $parse = "$3-$1-$2$parse"; | ||
| 10873 | |||
| 10874 | # last value is two digit year | ||
| 10875 | } else { | ||
| 10876 | |||
| 10877 | # add date string | ||
| 10878 | $parse = ($3 > 68 ? 1900 + $3 : 2000 + $3) . "-$1-$2$parse"; | ||
| 10879 | |||
| 10880 | } | ||
| 10881 | |||
| 10882 | # add date format | ||
| 10883 | $fmt = "%Y-%m-%d$fmt"; | ||
| 10884 | |||
| 10885 | # if text month matched | ||
| 10886 | } elsif (uc($str) =~ m/(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)/) { | ||
| 10887 | |||
| 10888 | # save month | ||
| 10889 | $month = $1; | ||
| 10890 | |||
| 10891 | # if two numbers matched | ||
| 10892 | if ($str =~ m/(\d{1,4})[^\d]+(\d{1,4})/) { | ||
| 10893 | |||
| 10894 | # if first value > 99 | ||
| 10895 | if ($1 > 99) { | ||
| 10896 | |||
| 10897 | # add date string | ||
| 10898 | $parse = "$1-$month-$2$parse"; | ||
| 10899 | |||
| 10900 | # if last value > 99 | ||
| 10901 | } elsif ($2 > 99) { | ||
| 10902 | |||
| 10903 | # add date string | ||
| 10904 | $parse = "$2-$month-$1$parse"; | ||
| 10905 | |||
| 10906 | # last value is two digit year | ||
| 10907 | } else { | ||
| 10908 | |||
| 10909 | # add date string | ||
| 10910 | $parse = ($2 > 68 ? 1900 + $2 : 2000 + $2) . "/$month/$1$parse"; | ||
| 10911 | |||
| 10912 | } | ||
| 10913 | |||
| 10914 | # if one number matched | ||
| 10915 | } elsif ($str =~ m/(\d{1,4})/) { | ||
| 10916 | |||
| 10917 | # if value > 99 | ||
| 10918 | if ($1 > 99) { | ||
| 10919 | |||
| 10920 | # add date string | ||
| 10921 | $parse = "$1-$month-1$parse"; | ||
| 10922 | |||
| 10923 | } else { | ||
| 10924 | |||
| 10925 | # add date string | ||
| 10926 | $parse = ($1 > 68 ? 1900 + $1 : 2000 + $1) . "/$month/1$parse"; | ||
| 10927 | |||
| 10928 | } | ||
| 10929 | |||
| 10930 | } | ||
| 10931 | |||
| 10932 | # add date format | ||
| 10933 | $fmt = "%Y-%b-%d$fmt"; | ||
| 10934 | |||
| 10935 | } | ||
| 10936 | |||
| 10937 | # return Time::Piece object, if string parsed successfully | ||
| 10938 | return(Time::Piece->strptime($parse, $fmt)) if (length($parse)); | ||
| 10939 | |||
| 10940 | } | ||
| 10941 | |||
| 10942 | } | ||
| 10943 | |||
| 10944 | # get file list | ||
| 10945 | # uses Perl 'bsd_glob' function | ||
| 10946 | # parameter: (path) | ||
| 10947 | # returns: (ref_to_file_list) | ||
| 10948 | sub _files { | ||
| 10949 | |||
| 10950 | # get path | ||
| 10951 | my $path = shift(); | ||
| 10952 | |||
| 10953 | # get list of files and/or directories | ||
| 10954 | my @files = grep {-e} File::Glob::bsd_glob($path); | ||
| 10955 | |||
| 10956 | # if list is just one directory | ||
| 10957 | if (@files == 1 && -d $files[0]) { | ||
| 10958 | |||
| 10959 | # get files in that directory | ||
| 10960 | @files = grep {-f} File::Glob::bsd_glob("$path/*"); | ||
| 10961 | |||
| 10962 | } else { | ||
| 10963 | |||
| 10964 | # filter the files | ||
| 10965 | @files = grep {-f} @files; | ||
| 10966 | |||
| 10967 | } | ||
| 10968 | |||
| 10969 | # return file list | ||
| 10970 | return(\@files); | ||
| 10971 | |||
| 10972 | } | ||
| 10973 | |||
| 10974 | # compute Mahalanobis distance | ||
| 10975 | # assumes parameters are valid | ||
| 10976 | # parameters: (vector1, vector2, inverse_covariance_matrix) | ||
| 10977 | # returns: (distance) | ||
| 10978 | sub _mahal { | ||
| 10979 | |||
| 10980 | # get parameters | ||
| 10981 | my ($x, $y, $sinv) = @_; | ||
| 10982 | |||
| 10983 | # local variables | ||
| 10984 | my ($d, $dT); | ||
| 10985 | |||
| 10986 | # for each dimension | ||
| 10987 | for my $i (0 .. $#{$x}) { | ||
| 10988 | |||
| 10989 | # save difference | ||
| 10990 | $d->[0][$i] = $dT->[$i][0] = $x->[$i] - $y->[$i]; | ||
| 10991 | |||
| 10992 | } | ||
| 10993 | |||
| 10994 | # bless matrices | ||
| 10995 | bless($d, 'Math::Matrix'); | ||
| 10996 | bless($dT, 'Math::Matrix'); | ||
| 10997 | |||
| 10998 | # return Mahalanobis distance | ||
| 10999 | return(sqrt(($d * $sinv * $dT)->[0][0])); | ||
| 11000 | |||
| 11001 | } | ||
| 11002 | |||
| 11003 | # get L*a*b* encoding code refs | ||
| 11004 | # parameter: (object_reference, hash) | ||
| 11005 | # returns: (get_code_ref, set_code_ref) | ||
| 11006 | sub _lab_encoding { | ||
| 11007 | |||
| 11008 | # get object reference | ||
| 11009 | my ($self, $hash) = @_; | ||
| 11010 | |||
| 11011 | # local variable | ||
| 11012 | my ($encode); | ||
| 11013 | |||
| 11014 | # get encoding parameter from hash | ||
| 11015 | $encode = $hash->{'encoding'}; | ||
| 11016 | |||
| 11017 | # if encoding parameter undefined | ||
| 11018 | if (! defined($encode)) { | ||
| 11019 | |||
| 11020 | # return code refs (identity) | ||
| 11021 | return(sub {@_}, sub {@_}); | ||
| 11022 | |||
| 11023 | # if encoding is 8/16-bit ICC CIELAB | ||
| 11024 | } elsif ($encode == 0) { | ||
| 11025 | |||
| 11026 | # return code refs | ||
| 11027 | return(sub {defined($_[0]) ? $_[0] / 100 : $_[0], defined($_[1]) ? ($_[1] + 128)/255 : $_[1], defined($_[2]) ? ($_[2] + 128)/255 : $_[2]}, | ||
| 11028 | sub {defined($_[0]) ? $_[0] * 100 : $_[0], defined($_[1]) ? $_[1] * 255 - 128 : $_[1], defined($_[2]) ? $_[2] * 255 - 128 : $_[2]}); | ||
| 11029 | |||
| 11030 | # if encoding is 16-bit ICC legacy L*a*b* | ||
| 11031 | } elsif ($encode == 1) { | ||
| 11032 | |||
| 11033 | # return code refs | ||
| 11034 | return(sub {defined($_[0]) ? $_[0] * 256/25700 : $_[0], defined($_[1]) ? ($_[1] + 128) * 256/65535 : $_[1], defined($_[2]) ? ($_[2] + 128) * 256/65535 : $_[2]}, | ||
| 11035 | sub {defined($_[0]) ? $_[0] * 25700/256 : $_[0], defined($_[1]) ? $_[1] * 65535/256 - 128 : $_[1], defined($_[2]) ? $_[2] * 65535/256 - 128 : $_[2]}); | ||
| 11036 | |||
| 11037 | # if encoding is 16-bit EFI/Monaco L*a*b* | ||
| 11038 | } elsif ($encode == 2) { | ||
| 11039 | |||
| 11040 | # return code refs | ||
| 11041 | return(sub {defined($_[0]) ? $_[0]/100 : $_[0], defined($_[1]) ? ($_[1] + 128) * 256/65535 : $_[1], defined($_[2]) ? ($_[2] + 128) * 256/65535 : $_[2]}, | ||
| 11042 | sub {defined($_[0]) ? $_[0] * 100 : $_[0], defined($_[1]) ? $_[1] * 65535/256 - 128 : $_[1], defined($_[2]) ? $_[2] * 65535/256 - 128 : $_[2]}); | ||
| 11043 | |||
| 11044 | # if encoding is L*a*b* | ||
| 11045 | } elsif ($encode == 3) { | ||
| 11046 | |||
| 11047 | # return code refs (identity) | ||
| 11048 | return(sub {@_}, sub {@_}); | ||
| 11049 | |||
| 11050 | # if encoding is LxLyLz | ||
| 11051 | } elsif ($encode == 4) { | ||
| 11052 | |||
| 11053 | # return code refs | ||
| 11054 | return(sub {if (defined($_[0]) && defined($_[1]) && defined($_[2])) {$_[0] + 116 * $_[1]/500, $_[0], $_[0] - 116 * $_[2]/200} else {@_}}, | ||
| 11055 | sub {if (defined($_[0]) && defined($_[1]) && defined($_[2])) {$_[1], 500 * ($_[0] - $_[1])/116, 200 * ($_[1] - $_[2])/116} else {@_}}); | ||
| 11056 | |||
| 11057 | # if encoding is unit LxLyLz | ||
| 11058 | } elsif ($encode == 5) { | ||
| 11059 | |||
| 11060 | # return code refs | ||
| 11061 | return(sub {if (defined($_[0]) && defined($_[1]) && defined($_[2])) {map {$_/100} ($_[0] + 116 * $_[1]/500, $_[0], $_[0] - 116 * $_[2]/200)} else {@_}}, | ||
| 11062 | sub {if (defined($_[0]) && defined($_[1]) && defined($_[2])) {map {$_ * 100} ($_[1], 500 * ($_[0] - $_[1])/116, 200 * ($_[1] - $_[2])/116)} else {@_}}); | ||
| 11063 | |||
| 11064 | } else { | ||
| 11065 | |||
| 11066 | # error | ||
| 11067 | croak('invalid L*a*b* encoding'); | ||
| 11068 | |||
| 11069 | } | ||
| 11070 | |||
| 11071 | } | ||
| 11072 | |||
| 11073 | # get XYZ encoding code refs | ||
| 11074 | # assumes there are XYZ columns | ||
| 11075 | # parameter: (object_reference, column_slice, [hash]) | ||
| 11076 | # returns: (get_code_ref, set_code_ref) | ||
| 11077 | sub _xyz_encoding { | ||
| 11078 | |||
| 11079 | # get object reference | ||
| 11080 | my ($self, $cols, $hash) = @_; | ||
| 11081 | |||
| 11082 | # local variable | ||
| 11083 | my ($encode, $wtpt); | ||
| 11084 | |||
| 11085 | # get encoding parameter from hash | ||
| 11086 | $encode = $hash->{'encoding'}; | ||
| 11087 | |||
| 11088 | # if encoding parameter undefined | ||
| 11089 | if (! defined($encode)) { | ||
| 11090 | |||
| 11091 | # return code refs (identity) | ||
| 11092 | return(sub {@_}, sub {@_}); | ||
| 11093 | |||
| 11094 | # if encoding is L* | ||
| 11095 | } elsif ($encode eq 'L*' || $encode == 4) { | ||
| 11096 | |||
| 11097 | # get illuminant white point | ||
| 11098 | ($wtpt = _illumWP($self, $cols, $hash)) || croak('illuminant white point required for LxLyLz encoding'); | ||
| 11099 | |||
| 11100 | # return code refs | ||
| 11101 | return(sub {defined($_[0]) ? ICC::Shared::x2L($_[0] / $wtpt->[0]) : $_[0], defined($_[1]) ? ICC::Shared::x2L($_[1] / $wtpt->[1]) : $_[1], defined($_[2]) ? ICC::Shared::x2L($_[2] / $wtpt->[2]) : $_[2]}, | ||
| 11102 | sub {defined($_[0]) ? ICC::Shared::L2x($_[0]) * $wtpt->[0] : $_[0], defined($_[1]) ? ICC::Shared::L2x($_[1]) * $wtpt->[1] : $_[1], defined($_[2]) ? ICC::Shared::L2x($_[2]) * $wtpt->[2] : $_[2]}); | ||
| 11103 | |||
| 11104 | # if encoding is 16-bit ICC XYZ | ||
| 11105 | } elsif ($encode == 7) { | ||
| 11106 | |||
| 11107 | # return code refs | ||
| 11108 | return(sub {map {defined() ? $_ / 199.9969482421875 : $_} @_}, sub {map {defined() ? $_ * 199.9969482421875 : $_} @_}); | ||
| 11109 | |||
| 11110 | # if encoding is 32-bit ICC XYZNumber | ||
| 11111 | } elsif ($encode == 8) { | ||
| 11112 | |||
| 11113 | # return code refs | ||
| 11114 | return(sub {map {defined() ? $_ / 100 : $_} @_}, sub {map {defined() ? $_ * 100 : $_} @_}); | ||
| 11115 | |||
| 11116 | # if encoding is xyz | ||
| 11117 | } elsif ($encode == 9) { | ||
| 11118 | |||
| 11119 | # get illuminant white point | ||
| 11120 | ($wtpt = _illumWP($self, $cols, $hash)) || croak('illuminant white point required for xyz encoding'); | ||
| 11121 | |||
| 11122 | # return code refs | ||
| 11123 | return(sub {defined($_[0]) ? $_[0] / $wtpt->[0] : $_[0], defined($_[1]) ? $_[1] / $wtpt->[1] : $_[1], defined($_[2]) ? $_[2] / $wtpt->[2] : $_[2]}, | ||
| 11124 | sub {defined($_[0]) ? $_[0] * $wtpt->[0] : $_[0], defined($_[1]) ? $_[1] * $wtpt->[1] : $_[1], defined($_[2]) ? $_[2] * $wtpt->[2] : $_[2]}); | ||
| 11125 | |||
| 11126 | # if encoding is XYZ | ||
| 11127 | } elsif ($encode == 10) { | ||
| 11128 | |||
| 11129 | # return code refs (identity) | ||
| 11130 | return(sub {@_}, sub {@_}); | ||
| 11131 | |||
| 11132 | # if encoding is media relative xyz | ||
| 11133 | } elsif ($encode == 11) { | ||
| 11134 | |||
| 11135 | # get media white point | ||
| 11136 | ($wtpt = _mediaWP($self, $cols, $hash)) || croak('media white point required for media relative xyz encoding'); | ||
| 11137 | |||
| 11138 | # return code refs | ||
| 11139 | return(sub {defined($_[0]) ? $_[0] / $wtpt->[0] : $_[0], defined($_[1]) ? $_[1] / $wtpt->[1] : $_[1], defined($_[2]) ? $_[2] / $wtpt->[2] : $_[2]}, | ||
| 11140 | sub {defined($_[0]) ? $_[0] * $wtpt->[0] : $_[0], defined($_[1]) ? $_[1] * $wtpt->[1] : $_[1], defined($_[2]) ? $_[2] * $wtpt->[2] : $_[2]}); | ||
| 11141 | |||
| 11142 | } else { | ||
| 11143 | |||
| 11144 | # error | ||
| 11145 | croak('invalid XYZ encoding'); | ||
| 11146 | |||
| 11147 | } | ||
| 11148 | |||
| 11149 | } | ||
| 11150 | |||
| 11151 | # get density encoding code refs | ||
| 11152 | # parameter: (object_reference, hash) | ||
| 11153 | # returns: (get_code_ref, set_code_ref) | ||
| 11154 | sub _density_encoding { | ||
| 11155 | |||
| 11156 | # get object reference | ||
| 11157 | my ($self, $hash) = @_; | ||
| 11158 | |||
| 11159 | # get encoding parameter from hash | ||
| 11160 | my $encode = $hash->{'encoding'}; | ||
| 11161 | |||
| 11162 | # if encoding parameter undefined or density | ||
| 11163 | if (! defined($encode) || $encode eq 'density') { | ||
| 11164 | |||
| 11165 | # return code refs (identity) | ||
| 11166 | return(sub {@_}, sub {@_}); | ||
| 11167 | |||
| 11168 | # if encoding is linear (RGBV) | ||
| 11169 | } elsif ($encode eq 'linear') { | ||
| 11170 | |||
| 11171 | # return code refs | ||
| 11172 | return(sub {map {defined() ? 100 * POSIX::pow(10, -$_) : $_} @_}, sub {map {if (defined()) {if ($_ > 0) {-POSIX::log10($_/100)} else {warn("log of $_"); 99}} else {$_}} @_}); | ||
| 11173 | |||
| 11174 | # if encoding is unit | ||
| 11175 | } elsif ($encode eq 'unit') { | ||
| 11176 | |||
| 11177 | # return code refs | ||
| 11178 | return(sub {map {defined() ? POSIX::pow(10, -$_) : $_} @_}, sub {map {if (defined()) {if ($_ > 0) {-POSIX::log10($_)} else {warn("log of $_"); 99}} else {$_}} @_}); | ||
| 11179 | |||
| 11180 | # if encoding is L* | ||
| 11181 | } elsif ($encode eq 'L*') { | ||
| 11182 | |||
| 11183 | # return code refs | ||
| 11184 | return(sub {map {defined() ? ICC::Shared::x2L(POSIX::pow(10, -$_)) : $_} @_}, sub {map {if (defined()) {if ($_ > 0) {-POSIX::log10(ICC::Shared::L2x($_))} else {warn("log of $_"); 99}} else {$_}} @_}); | ||
| 11185 | |||
| 11186 | } else { | ||
| 11187 | |||
| 11188 | # error | ||
| 11189 | croak('invalid density encoding'); | ||
| 11190 | |||
| 11191 | } | ||
| 11192 | |||
| 11193 | } | ||
| 11194 | |||
| 11195 | # get rgbv encoding code refs | ||
| 11196 | # parameter: (object_reference, hash) | ||
| 11197 | # returns: (get_code_ref, set_code_ref) | ||
| 11198 | sub _rgbv_encoding { | ||
| 11199 | |||
| 11200 | # get object reference | ||
| 11201 | my ($self, $hash) = @_; | ||
| 11202 | |||
| 11203 | # get encoding parameter from hash | ||
| 11204 | my $encode = $hash->{'encoding'}; | ||
| 11205 | |||
| 11206 | # if encoding parameter undefined or linear | ||
| 11207 | if (! defined($encode) || $encode eq 'linear'|| $encode eq 'RGBV') { | ||
| 11208 | |||
| 11209 | # return code refs (identity) | ||
| 11210 | return(sub {@_}, sub {@_}); | ||
| 11211 | |||
| 11212 | # if encoding is unit | ||
| 11213 | } elsif ($encode eq 'unit') { | ||
| 11214 | |||
| 11215 | # return code refs | ||
| 11216 | return(sub {map {$_/100} @_}, sub {map {$_ * 100} @_}); | ||
| 11217 | |||
| 11218 | # if encoding is density | ||
| 11219 | } elsif ($encode eq 'density') { | ||
| 11220 | |||
| 11221 | # return code refs | ||
| 11222 | return(sub {map {if (defined()) {if ($_ > 0) {-POSIX::log10($_/100)} else {warn("log of $_"); 99}} else {$_}} @_}, sub {map {defined() ? 100 * POSIX::pow(10, -$_) : $_} @_}); | ||
| 11223 | |||
| 11224 | # if encoding is L* | ||
| 11225 | } elsif ($encode eq 'L*') { | ||
| 11226 | |||
| 11227 | # return code refs | ||
| 11228 | return(sub {map {ICC::Shared::x2L($_/100)} @_}, sub {map {ICC::Shared::L2x($_) * 100} @_}); | ||
| 11229 | |||
| 11230 | } else { | ||
| 11231 | |||
| 11232 | # error | ||
| 11233 | croak('invalid rgbv encoding'); | ||
| 11234 | |||
| 11235 | } | ||
| 11236 | |||
| 11237 | } | ||
| 11238 | |||
| 11239 | #--------- additional Math::Matrix methods --------- | ||
| 11240 | |||
| 11241 | package Math::Matrix; | ||
| 11242 | |||
| 11243 | # rotate matrix | ||
| 11244 | # rotation: 0 = None, 1 = 90° CW, 2 = 180°, 3 = 90° CCW | ||
| 11245 | # note: rotation describes appearance in MeasureTool | ||
| 11246 | # parameter: (rotation) | ||
| 11247 | # returns: (rotated_matrix) | ||
| 11248 | sub rotate { | ||
| 11249 | |||
| 11250 | # get parameters | ||
| 11251 | my ($self, $rot) = @_; | ||
| 11252 | |||
| 11253 | # local variables | ||
| 11254 | my ($rows, $cols, $replace); | ||
| 11255 | |||
| 11256 | # return if rotation undefined | ||
| 11257 | return($self) if (! defined($rot)); | ||
| 11258 | |||
| 11259 | # resolve rotation parameter | ||
| 11260 | $rot = int($rot) % 4; | ||
| 11261 | |||
| 11262 | # get upper row index | ||
| 11263 | $rows = $#{$self}; | ||
| 11264 | |||
| 11265 | # get upper column index | ||
| 11266 | $cols = $#{$self->[0]}; | ||
| 11267 | |||
| 11268 | # if rotation = 0 (none) | ||
| 11269 | if ($rot == 0) { | ||
| 11270 | |||
| 11271 | # for each row | ||
| 11272 | for my $i (0 .. $rows) { | ||
| 11273 | |||
| 11274 | # for each column | ||
| 11275 | for my $j (0 .. $cols) { | ||
| 11276 | |||
| 11277 | # copy matrix element | ||
| 11278 | $replace->[$i][$j] = $self->[$i][$j]; | ||
| 11279 | |||
| 11280 | } | ||
| 11281 | |||
| 11282 | } | ||
| 11283 | |||
| 11284 | # if rotation = 1 (90° CW) | ||
| 11285 | } elsif ($rot == 1) { | ||
| 11286 | |||
| 11287 | # for each row | ||
| 11288 | for my $i (0 .. $rows) { | ||
| 11289 | |||
| 11290 | # for each column | ||
| 11291 | for my $j (0 .. $cols) { | ||
| 11292 | |||
| 11293 | # copy matrix element | ||
| 11294 | $replace->[$j][$i] = $self->[$i][$cols - $j]; | ||
| 11295 | |||
| 11296 | } | ||
| 11297 | |||
| 11298 | } | ||
| 11299 | |||
| 11300 | # if rotation = 2 (180°) | ||
| 11301 | } elsif ($rot == 2) { | ||
| 11302 | |||
| 11303 | # for each row | ||
| 11304 | for my $i (0 .. $rows) { | ||
| 11305 | |||
| 11306 | # for each column | ||
| 11307 | for my $j (0 .. $cols) { | ||
| 11308 | |||
| 11309 | # copy matrix element | ||
| 11310 | $replace->[$i][$j] = $self->[$rows - $i][$cols - $j]; | ||
| 11311 | |||
| 11312 | } | ||
| 11313 | |||
| 11314 | } | ||
| 11315 | |||
| 11316 | # if rotation = 3 (90° CCW) | ||
| 11317 | } elsif ($rot == 3) { | ||
| 11318 | |||
| 11319 | # for each row | ||
| 11320 | for my $i (0 .. $rows) { | ||
| 11321 | |||
| 11322 | # for each column | ||
| 11323 | for my $j (0 .. $cols) { | ||
| 11324 | |||
| 11325 | # copy matrix element | ||
| 11326 | $replace->[$j][$i] = $self->[$rows - $i][$j]; | ||
| 11327 | |||
| 11328 | } | ||
| 11329 | |||
| 11330 | } | ||
| 11331 | |||
| 11332 | } | ||
| 11333 | |||
| 11334 | # return new object | ||
| 11335 | return(bless($replace, 'Math::Matrix')); | ||
| 11336 | |||
| 11337 | } | ||
| 11338 | |||
| 11339 | # flip matrix | ||
| 11340 | # flip: 0 = transpose, 1 = horizontal, 2 = cross transpose, 3 = vertical | ||
| 11341 | # note: flip describes appearance in MeasureTool | ||
| 11342 | # parameter: (flip) | ||
| 11343 | # returns: (flipped_matrix) | ||
| 11344 | sub flip { | ||
| 11345 | |||
| 11346 | # get parameters | ||
| 11347 | my ($self, $flip) = @_; | ||
| 11348 | |||
| 11349 | # local variables | ||
| 11350 | my ($rows, $cols, $replace); | ||
| 11351 | |||
| 11352 | # return if flip undefined | ||
| 11353 | return($self) if (! defined($flip)); | ||
| 11354 | |||
| 11355 | # resolve flip parameter | ||
| 11356 | $flip = int($flip) % 4; | ||
| 11357 | |||
| 11358 | # get upper row index | ||
| 11359 | $rows = $#{$self}; | ||
| 11360 | |||
| 11361 | # get upper column index | ||
| 11362 | $cols = $#{$self->[0]}; | ||
| 11363 | |||
| 11364 | # if flip = 0 (transpose) | ||
| 11365 | if ($flip == 0) { | ||
| 11366 | |||
| 11367 | # for each row | ||
| 11368 | for my $i (0 .. $rows) { | ||
| 11369 | |||
| 11370 | # for each column | ||
| 11371 | for my $j (0 .. $cols) { | ||
| 11372 | |||
| 11373 | # copy matrix element | ||
| 11374 | $replace->[$j][$i] = $self->[$i][$j]; | ||
| 11375 | |||
| 11376 | } | ||
| 11377 | |||
| 11378 | } | ||
| 11379 | |||
| 11380 | # if flip = 1 (horizontal) | ||
| 11381 | } elsif ($flip == 1) { | ||
| 11382 | |||
| 11383 | # for each row | ||
| 11384 | for my $i (0 .. $rows) { | ||
| 11385 | |||
| 11386 | # for each column | ||
| 11387 | for my $j (0 .. $cols) { | ||
| 11388 | |||
| 11389 | # copy matrix element | ||
| 11390 | $replace->[$i][$j] = $self->[$rows - $i][$j]; | ||
| 11391 | |||
| 11392 | } | ||
| 11393 | |||
| 11394 | } | ||
| 11395 | |||
| 11396 | # if flip = 2 (cross transpose) | ||
| 11397 | } elsif ($flip == 2) { | ||
| 11398 | |||
| 11399 | # for each row | ||
| 11400 | for my $i (0 .. $rows) { | ||
| 11401 | |||
| 11402 | # for each column | ||
| 11403 | for my $j (0 .. $cols) { | ||
| 11404 | |||
| 11405 | # copy matrix element | ||
| 11406 | $replace->[$j][$i] = $self->[$rows - $i][$cols - $j]; | ||
| 11407 | |||
| 11408 | } | ||
| 11409 | |||
| 11410 | } | ||
| 11411 | |||
| 11412 | # if flip = 3 (vertical) | ||
| 11413 | } elsif ($flip == 3) { | ||
| 11414 | |||
| 11415 | # for each row | ||
| 11416 | for my $i (0 .. $rows) { | ||
| 11417 | |||
| 11418 | # for each column | ||
| 11419 | for my $j (0 .. $cols) { | ||
| 11420 | |||
| 11421 | # copy matrix element | ||
| 11422 | $replace->[$i][$j] = $self->[$i][$cols - $j]; | ||
| 11423 | |||
| 11424 | } | ||
| 11425 | |||
| 11426 | } | ||
| 11427 | |||
| 11428 | } | ||
| 11429 | |||
| 11430 | # return new object | ||
| 11431 | return(bless($replace, 'Math::Matrix')); | ||
| 11432 | |||
| 11433 | } | ||
| 11434 | |||
| 11435 | # randomize matrix | ||
| 11436 | # returns: (randomized_matrix) | ||
| 11437 | sub randomize { | ||
| 11438 | |||
| 11439 | # get object reference | ||
| 11440 | my $self = shift(); | ||
| 11441 | |||
| 11442 | # local variables | ||
| 11443 | my (@ix, $rows, $cols, $replace); | ||
| 11444 | |||
| 11445 | # flatten and randomize matrix | ||
| 11446 | @ix = List::Util::shuffle(@{ICC::Shared::flatten($self)}); | ||
| 11447 | |||
| 11448 | # get upper row index | ||
| 11449 | $rows = $#{$self}; | ||
| 11450 | |||
| 11451 | # get upper column index | ||
| 11452 | $cols = $#{$self->[0]}; | ||
| 11453 | |||
| 11454 | # for each row | ||
| 11455 | for my $i (0 .. $rows) { | ||
| 11456 | |||
| 11457 | # for each column | ||
| 11458 | for my $j (0 .. $cols) { | ||
| 11459 | |||
| 11460 | # set element | ||
| 11461 | $replace->[$i][$j] = $ix[$i * ($cols + 1) + $j]; | ||
| 11462 | |||
| 11463 | } | ||
| 11464 | |||
| 11465 | } | ||
| 11466 | |||
| 11467 | # return new object | ||
| 11468 | return(bless($replace, 'Math::Matrix')); | ||
| 11469 | |||
| 11470 | } | ||
| 11471 | |||
| 11472 | 1; | ||
| 11473 |