blib/lib/ICC/Profile/mBA_.pm untested

filter: Statementstmt 0.0% 0 / 495 Branchbran 0.0% 0 / 174 Conditioncond 0.0% 0 / 146 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 36 Podpod 0.0% 0 / 31 totaltotal 0.0% 0 / 882 CC 88 SCAR 38.3
CC
88
Cov
0%
_readICCmBA_
156.0
_writeICCmBA_
110.0
sdump
90.0
CRAP 46.0
1 package ICC::Profile::mBA_;
2  
3 use strict;
4 use Carp;
5  
6 our $VERSION = 2.51;
7  
8 # revised 2016-05-17
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 # use POSIX math
19 use POSIX ();
20  
21 # create new mBA_ object
22 # hash may contain pointers to B-curves, matrix, M-curves, CLUT, or A-curves
23 # keys are: ('b_curves', 'matrix', 'm_curves', 'clut', 'a_curves')
24 # tag elements not specified in the hash are left empty
25 # parameters: ()
26 # parameters: (ref_to_attribute_hash)
27 # parameters: (ref_to_matrix-based_profile_object)
28 # returns: (ref_to_object)
29 sub new {
30  
31 # get object class
32 my $class = shift();
33  
34 # create empty mBA_ object
35 my $self = [
36 {}, # object header
37 [], # processing elements
38 0x00, # transform mask
39 0x00 # clipping mask
40 ];
41  
42 # if there are parameters
43 if (@_) {
44
45 # if one parameter, a hash reference
46 if (@_ == 1 && ref($_[0]) eq 'HASH') {
47
48 # make new mBA_ tag from attribute hash
49 _new_from_hash($self, @_);
50
51 # if one parameter, an ICC::Profile object
52 } elsif (@_ == 1 && UNIVERSAL::isa($_[0], 'ICC::Profile')) {
53  
54 # make new mAB_ tag from ICC::Profile object
55 _newICCmatrix($self, @_);
56  
57 } else {
58
59 # error
60 croak('parameter must be a hash reference or a display matrix profile');
61
62 }
63
64 }
65  
66 # bless object
67 bless($self, $class);
68  
69 # return object reference
70 return($self);
71  
72 }
73  
74 # get/set reference to header hash
75 # parameters: ([ref_to_new_hash])
76 # returns: (ref_to_hash)
77 sub header {
78  
79 # get object reference
80 my $self = shift();
81  
82 # if there are parameters
83 if (@_) {
84
85 # if one parameter, a hash reference
86 if (@_ == 1 && ref($_[0]) eq 'HASH') {
87
88 # set header to new hash
89 $self->[0] = shift();
90
91 } else {
92
93 # error
94 croak('parameter must be a hash reference');
95
96 }
97
98 }
99  
100 # return reference
101 return($self->[0]);
102  
103 }
104  
105 # get/set processing element array reference
106 # parameters: ([ref_to_array])
107 # returns: (ref_to_array)
108 sub array {
109  
110 # get object reference
111 my $self = shift();
112  
113 # if parameter
114 if (@_) {
115
116 # verify array reference
117 (ref($_[0]) eq 'ARRAY') || croak('not an array reference');
118
119 # set array reference
120 $self->[1] = [@{shift()}];
121
122 }
123  
124 # return array reference
125 return($self->[1]);
126  
127 }
128  
129 # get/set reference to B-curves 'cvst' object
130 # parameters: ([ref_to_new_object])
131 # returns: (ref_to_object)
132 sub b_curves {
133  
134 # get object reference
135 my $self = shift();
136  
137 # if there are parameters
138 if (@_) {
139
140 # if one parameter, a 'cvst' object
141 if (@_ == 1 && UNIVERSAL::isa($_[0], 'ICC::Profile::cvst')) {
142
143 # set B-curves to new object
144 $self->[1][0] = shift();
145
146 # set transform mask bit
147 $self->[2] |= 0x01;
148
149 } else {
150
151 # error
152 croak('parameter must be a \'cvst\' object');
153
154 }
155
156 }
157  
158 # return object reference
159 return($self->[1][0]);
160  
161 }
162  
163 # get/set reference to matrix 'matf' object
164 # parameters: ([ref_to_new_object])
165 # returns: (ref_to_object)
166 sub matrix {
167  
168 # get object reference
169 my $self = shift();
170  
171 # if there are parameters
172 if (@_) {
173
174 # if one parameter, an 'matf' object
175 if (@_ == 1 && UNIVERSAL::isa($_[0], 'ICC::Profile::matf')) {
176
177 # set matrix to new object
178 $self->[1][1] = shift();
179
180 # set transform mask bit
181 $self->[2] |= 0x02;
182
183 } else {
184
185 # error
186 croak('parameter must be an \'matf\' object');
187
188 }
189
190 }
191  
192 # return object reference
193 return($self->[1][1]);
194  
195 }
196  
197 # get/set reference to M-curves 'cvst' object
198 # parameters: ([ref_to_new_object])
199 # returns: (ref_to_object)
200 sub m_curves {
201  
202 # get object reference
203 my $self = shift();
204  
205 # if there are parameters
206 if (@_) {
207
208 # if one parameter, a 'cvst' object
209 if (@_ == 1 && UNIVERSAL::isa($_[0], 'ICC::Profile::cvst')) {
210
211 # set M-curves to new object
212 $self->[1][2] = shift();
213
214 # set transform mask bit
215 $self->[2] |= 0x04;
216
217 } else {
218
219 # error
220 croak('parameter must be a \'cvst\' object');
221
222 }
223
224 }
225  
226 # return object reference
227 return($self->[1][2]);
228  
229 }
230  
231 # get/set reference to CLUT 'clut' object
232 # parameters: ([ref_to_new_object])
233 # returns: (ref_to_object)
234 sub clut {
235  
236 # get object reference
237 my $self = shift();
238  
239 # if there are parameters
240 if (@_) {
241
242 # if one parameter, a 'clut' object
243 if (@_ == 1 && UNIVERSAL::isa($_[0], 'ICC::Profile::clut')) {
244
245 # set CLUT to new object
246 $self->[1][3] = shift();
247
248 # set transform mask bit
249 $self->[2] |= 0x08;
250
251 } else {
252
253 # error
254 croak('parameter must be a \'clut\' object');
255
256 }
257
258 }
259  
260 # return object reference
261 return($self->[1][3]);
262  
263 }
264  
265 # get/set reference to A-curves 'cvst' object
266 # parameters: ([ref_to_new_object])
267 # returns: (ref_to_object)
268 sub a_curves {
269  
270 # get object reference
271 my $self = shift();
272  
273 # if there are parameters
274 if (@_) {
275
276 # if one parameter, a 'cvst' object
277 if (@_ == 1 && UNIVERSAL::isa($_[0], 'ICC::Profile::cvst')) {
278
279 # set A-curves to new object
280 $self->[1][4] = shift();
281
282 # set transform mask bit
283 $self->[2] |= 0x10;
284
285 } else {
286
287 # error
288 croak('parameter must be a \'cvst\' object');
289
290 }
291
292 }
293  
294 # return object reference
295 return($self->[1][4]);
296  
297 }
298  
299 # get/set transform mask
300 # bits 4-3-2-1-0 correpsond to A-CLUT-M-MATRIX-B
301 # parameters: ([new_mask_value])
302 # returns: (mask_value)
303 sub mask {
304  
305 # get object reference
306 my $self = shift();
307  
308 # if there are parameters
309 if (@_) {
310
311 # if one parameter
312 if (@_ == 1) {
313
314 # set object transform mask value
315 $self->[2] = shift();
316
317 } else {
318
319 # error
320 croak('more than one parameter');
321
322 }
323
324 }
325  
326 # return transform mask value
327 return($self->[2]);
328  
329 }
330  
331 # get/set clipping mask
332 # bits 4-3-2-1-0 correpsond to A-CLUT-M-MATRIX-B
333 # parameters: ([new_mask_value])
334 # returns: (mask_value)
335 sub clip {
336
337 # get object reference
338 my $self = shift();
339
340 # if there are parameters
341 if (@_) {
342
343 # if one parameter
344 if (@_ == 1) {
345
346 # set object clipping mask value
347 $self->[3] = shift();
348
349 } else {
350
351 # error
352 croak('more than one parameter');
353
354 }
355
356 }
357
358 # return clipping mask value
359 return($self->[3]);
360
361 }
362  
363 # create mBA_ tag object from ICC profile
364 # parameters: (ref_to_parent_object, file_handle, ref_to_tag_table_entry)
365 # returns: (ref_to_object)
366 sub new_fh {
367  
368 # get object class
369 my $class = shift();
370  
371 # create empty mBA_ object
372 my $self = [
373 {}, # object header
374 [], # processing elements
375 0x00, # transform mask
376 0x00 # clipping mask
377 ];
378  
379 # verify 3 parameters
380 (@_ == 3) || croak('wrong number of parameters');
381  
382 # read mBA_ data from profile
383 _readICCmBA_($self, @_);
384  
385 # bless object
386 bless($self, $class);
387  
388 # return object reference
389 return($self);
390  
391 }
392  
393 # writes mBA_ tag object to ICC profile
394 # parameters: (ref_to_parent_object, file_handle, ref_to_tag_table_entry)
395 sub write_fh {
396  
397 # get tag reference
398 my $self = shift();
399  
400 # verify 3 parameters
401 (@_ == 3) || croak('wrong number of parameters');
402  
403 # write mBA_ data to profile
404 _writeICCmBA_($self, @_);
405  
406 }
407  
408 # get tag size (for writing to profile)
409 # returns: (tag_clut_size)
410 sub size {
411  
412 # get parameters
413 my ($self) = @_;
414  
415 # local variables
416 my ($size);
417  
418 # set header size
419 $size = 32;
420  
421 # for each B-curve
422 for my $curve (@{$self->[1][0]->array()}) {
423
424 # add curve size
425 $size += $curve->size;
426
427 # pad to 4-bytes
428 $size += (-$size % 4);
429
430 }
431  
432 # if matrix defined
433 if (defined($self->[1][1])) {
434
435 # add matrix size (48 bytes)
436 $size += 48;
437
438 }
439  
440 # if M-curves defined
441 if (defined($self->[1][2])) {
442
443 # for each M-curve
444 for my $curve (@{$self->[1][2]->array()}) {
445
446 # add curve size
447 $size += $curve->size;
448
449 # pad to 4-bytes
450 $size += (-$size % 4);
451
452 }
453
454 }
455  
456 # if CLUT defined
457 if (defined($self->[1][3])) {
458
459 # add CLUT size (nCLUT object)
460 $size += 20 + $self->[1][3]->_clut_size($self->[1][3][0]{'clut_bytes'} || 2);
461
462 # pad to 4-byte boundary
463 $size += (-$size % 4);
464
465 }
466  
467 # if A-curves defined
468 if (defined($self->[1][4])) {
469
470 # for each A-curve
471 for my $curve (@{$self->[1][4]->array()}) {
472
473 # add curve size
474 $size += $curve->size;
475
476 # pad to 4-bytes
477 $size += (-$size % 4);
478
479 }
480
481 }
482  
483 # return size
484 return($size);
485  
486 }
487  
488 # get number of input channels
489 # returns: (number)
490 sub cin {
491  
492 # get object reference
493 my $self = shift();
494  
495 # return
496 return($self->[1][0]->cin());
497  
498 }
499  
500 # get number of output channels
501 # returns: (number)
502 sub cout {
503  
504 # get object reference
505 my $self = shift();
506  
507 # return
508 return($self->[1][-1]->cout());
509  
510 }
511  
512 # transform data
513 # transform mask enables/disables individual tag elements
514 # clipping mask enables/disables individual tag output clipping
515 # supported input types:
516 # parameters: (list, [hash])
517 # parameters: (vector, [hash])
518 # parameters: (matrix, [hash])
519 # parameters: (Math::Matrix_object, [hash])
520 # parameters: (structure, [hash])
521 # returns: (same_type_as_input)
522 sub transform {
523  
524 # set hash value (0 or 1)
525 my $h = ref($_[-1]) eq 'HASH' ? 1 : 0;
526  
527 # if input a 'Math::Matrix' object
528 if (@_ == $h + 2 && UNIVERSAL::isa($_[1], 'Math::Matrix')) {
529
530 # call matrix transform
531 &_trans2;
532
533 # if input an array reference
534 } elsif (@_ == $h + 2 && ref($_[1]) eq 'ARRAY') {
535
536 # if array contains numbers (vector)
537 if (! ref($_[1][0]) && @{$_[1]} == grep {Scalar::Util::looks_like_number($_)} @{$_[1]}) {
538
539 # call vector transform
540 &_trans1;
541
542 # if array contains vectors (2-D array)
543 } elsif (ref($_[1][0]) eq 'ARRAY' && @{$_[1]} == grep {ref($_) eq 'ARRAY' && Scalar::Util::looks_like_number($_->[0])} @{$_[1]}) {
544
545 # call matrix transform
546 &_trans2;
547
548 } else {
549
550 # call structure transform
551 &_trans3;
552
553 }
554
555 # if input a list (of numbers)
556 } elsif (@_ == $h + 1 + grep {Scalar::Util::looks_like_number($_)} @_) {
557
558 # call list transform
559 &_trans0;
560
561 } else {
562
563 # error
564 croak('invalid transform input');
565
566 }
567  
568 }
569  
570 # inverse transform
571 # note: number of undefined output values must equal number of defined input values
572 # note: the input and output vectors contain the final solution on return
573 # hash key 'init' specifies initial value vector
574 # hash key 'ubox' enables unit box extrapolation
575 # parameters: (input_vector, output_vector, [hash])
576 # returns: (RMS_error_value)
577 sub inverse {
578  
579 # get parameters
580 my ($self, $in, $out, $hash) = @_;
581  
582 # local variables
583 my ($i, $j, @si, @so, $init);
584 my ($int, $jac, $mat, $delta);
585 my ($max, $elim, $dlim, $accum, $error);
586  
587 # initialize indices
588 $i = $j = -1;
589  
590 # build slice arrays while validating input and output arrays
591 ((grep {$i++; defined() && push(@si, $i)} @{$in}) == (grep {$j++; ! defined() && push(@so, $j)} @{$out})) || croak('wrong number of undefined values');
592  
593 # get init array
594 $init = $hash->{'init'};
595  
596 # for each undefined output value
597 for my $i (@so) {
598
599 # set to supplied initial value or 0.5
600 $out->[$i] = defined($init->[$i]) ? $init->[$i] : 0.5;
601
602 }
603  
604 # set maximum loop count
605 $max = $hash->{'inv_max'} || 10;
606  
607 # loop error limit
608 $elim = $hash->{'inv_elim'} || 1E-6;
609  
610 # set delta limit
611 $dlim = $hash->{'inv_dlim'} || 0.5;
612  
613 # create empty solution matrix
614 $mat = Math::Matrix->new([]);
615  
616 # compute initial transform values
617 ($jac, $int) = jacobian($self, $out, $hash);
618  
619 # solution loop
620 for (1 .. $max) {
621
622 # for each input
623 for my $i (0 .. $#si) {
624
625 # for each output
626 for my $j (0 .. $#so) {
627
628 # copy Jacobian value to solution matrix
629 $mat->[$i][$j] = $jac->[$si[$i]][$so[$j]];
630
631 }
632
633 # save residual value to solution matrix
634 $mat->[$i][$#si + 1] = $in->[$si[$i]] - $int->[$si[$i]];
635
636 }
637
638 # solve for delta values
639 $delta = $mat->solve;
640
641 # for each output value
642 for my $i (0 .. $#so) {
643
644 # add delta (limited using hyperbolic tangent)
645 $out->[$so[$i]] += POSIX::tanh($delta->[$i][0]/$dlim) * $dlim;
646
647 }
648
649 # compute updated transform values
650 ($jac, $int) = jacobian($self, $out, $hash);
651
652 # initialize error accumulator
653 $accum = 0;
654
655 # for each input
656 for my $i (0 .. $#si) {
657
658 # accumulate delta squared
659 $accum += ($in->[$si[$i]] - $int->[$si[$i]])**2;
660
661 }
662
663 # compute RMS error
664 $error = sqrt($accum/@si);
665
666 # if error less than limit
667 last if ($error < $elim);
668
669 }
670  
671 # update input vector with final values
672 @{$in} = @{$int};
673  
674 # return
675 return($error);
676  
677 }
678  
679 # compute Jacobian matrix
680 # transform mask enables/disables individual tag elements
681 # parameters: (input_vector, [hash])
682 # returns: (Jacobian_matrix, [output_vector])
683 sub jacobian {
684  
685 # get parameters
686 my ($self, $data, $hash) = @_;
687  
688 # local variables
689 my ($jac, $jaci);
690  
691 # for each processing element
692 for my $i (0 .. $#{$self->[1]}) {
693
694 # if processing element defined, and transform mask bit set
695 if (defined($self->[1][$i]) && $self->[2] & 0x01 << $i) {
696
697 # compute Jacobian matrix and transform data
698 ($jaci, $data) = $self->[1][$i]->jacobian($data, $hash);
699
700 # multiply Jacobian matrices
701 $jac = defined($jac) ? $jaci * $jac : $jaci;
702
703 }
704
705 }
706  
707 # if Jacobian matrix is undefined, use identity matrix
708 $jac = Math::Matrix->diagonal((1) x @{$data}) if (! defined($jac));
709  
710 # if output values wanted
711 if (wantarray) {
712
713 # return Jacobian and output values
714 return($jac, $data);
715
716 } else {
717
718 # return Jacobian only
719 return($jac);
720
721 }
722
723 }
724  
725 # get/set PCS encoding
726 # for use with ICC::Support::PCS objects
727 # parameters: ([PCS_encoding])
728 # returns: (PCS_encoding)
729 sub pcs {
730  
731 # get parameters
732 my ($self, $pcs) = @_;
733  
734 # if PCS parameter is supplied
735 if (defined($pcs)) {
736
737 # if a valid PCS encoding
738 if (grep {$pcs == $_} (3, 8)) {
739
740 # copy to tag header hash
741 $self->[0]{'pcs_encoding'} = $pcs;
742
743 # return PCS encoding
744 return($pcs);
745
746 } else {
747
748 # error
749 croak('invalid PCS encoding');
750
751 }
752
753 } else {
754
755 # if PCS is defined in tag header
756 if (defined($self->[0]{'pcs_encoding'})) {
757
758 # return PCS encoding
759 return($self->[0]{'pcs_encoding'});
760
761 } else {
762
763 # error
764 croak('can\'t determine PCS encoding');
765
766 }
767
768 }
769
770 }
771  
772 # get/set white point
773 # parameters: ([white_point])
774 # returns: (white_point)
775 sub wtpt {
776  
777 # get parameters
778 my ($self, $wtpt) = @_;
779  
780 # if white point parameter is supplied
781 if (defined($wtpt)) {
782
783 # if an array of three scalars
784 if (@{$wtpt} == 3 && 3 == grep {! ref()} @{$wtpt}) {
785
786 # copy to tag header hash
787 $self->[0]{'wtpt'} = $wtpt;
788
789 # return white point
790 return($wtpt);
791
792 } else {
793
794 # error
795 croak('invalid white point');
796
797 }
798
799 } else {
800
801 # if white point is defined in tag header
802 if (defined($self->[0]{'wtpt'})) {
803
804 # return return white point
805 return($self->[0]{'wtpt'});
806
807 } else {
808
809 # error
810 croak('can\'t determine white point');
811
812 }
813
814 }
815
816 }
817  
818 # print object contents to string
819 # format is an array structure
820 # parameter: ([format])
821 # returns: (string)
822 sub sdump {
823  
824 # get parameters
825 my ($self, $p) = @_;
826  
827 # local variables
828 my ($element, $fmt, $s, $pt, $st);
829  
830 # resolve parameter to an array reference
831 $p = defined($p) ? ref($p) eq 'ARRAY' ? $p : [$p] : [];
832  
833 # get format string
834 $fmt = defined($p->[0]) && ! ref($p->[0]) ? $p->[0] : 's';
835  
836 # set string to object ID
837 $s = sprintf("'%s' object, (0x%x)\n", ref($self), $self);
838  
839 # if format contains 'o'
840 if ($fmt =~ m/s/) {
841
842 # get default parameter
843 $pt = $p->[-1];
844
845 # for each processing element
846 for my $i (0 .. $#{$self->[1]}) {
847
848 # get element reference
849 $element = $self->[1][$i];
850
851 # if processing element is undefined
852 if (! defined($element)) {
853
854 # append message
855 $s .= "\tprocessing element is undefined\n";
856
857 # if processing element is not a blessed object
858 } elsif (! Scalar::Util::blessed($element)) {
859
860 # append message
861 $s .= "\tprocessing element is not a blessed object\n";
862
863 # if processing element has an 'sdump' method
864 } elsif ($element->can('sdump')) {
865
866 # get 'sdump' string
867 $st = $element->sdump(defined($p->[$i + 1]) ? $p->[$i + 1] : $pt);
868
869 # prepend tabs to each line
870 $st =~ s/^/\t/mg;
871
872 # append 'sdump' string
873 $s .= $st;
874
875 # processing element is object without an 'sdump' method
876 } else {
877
878 # append object info
879 $s .= sprintf("\t'%s' object, (0x%x)\n", ref($element), $element);
880
881 }
882
883 }
884
885 }
886  
887 # return
888 return($s);
889  
890 }
891  
892 # transform list
893 # parameters: (object_reference, list, [hash])
894 # returns: (list)
895 sub _trans0 {
896  
897 # local variables
898 my ($self, $hash, $data);
899  
900 # get object reference
901 $self = shift();
902  
903 # get optional hash
904 $hash = pop() if (ref($_[-1]) eq 'HASH');
905  
906 # process data
907 $data = _trans1($self, [@_], $hash);
908  
909 # return list
910 return(@{$data});
911  
912 }
913  
914 # transform vector
915 # parameters: (object_reference, vector, [hash])
916 # returns: (vector)
917 sub _trans1 {
918  
919 # get parameters
920 my ($self, $data, $hash) = @_;
921  
922 # for each processing element
923 for my $i (0 .. $#{$self->[1]}) {
924
925 # if processing element defined, and transform mask bit set
926 if (defined($self->[1][$i]) && $self->[2] & 0x01 << $i) {
927
928 # transform data
929 $data = $self->[1][$i]->_trans1($data, $hash);
930
931 # clip output values if clipping mask bit set
932 ICC::Shared::clip_struct($data) if ($self->[3] & 0x01 << $i);
933
934 }
935
936 }
937
938 # return
939 return($data);
940
941 }
942  
943 # transform matrix (2-D array -or- Math::Matrix object)
944 # parameters: (object_reference, matrix, [hash])
945 # returns: (matrix)
946 sub _trans2 {
947  
948 # get parameters
949 my ($self, $data, $hash) = @_;
950  
951 # for each processing element
952 for my $i (0 .. $#{$self->[1]}) {
953
954 # if processing element defined, and transform mask bit set
955 if (defined($self->[1][$i]) && $self->[2] & 0x01 << $i) {
956
957 # transform data
958 $data = $self->[1][$i]->_trans2($data, $hash);
959
960 # clip output values if clipping mask bit set
961 ICC::Shared::clip_struct($data) if ($self->[3] & 0x01 << $i);
962
963 }
964
965 }
966  
967 # return
968 return($data);
969  
970 }
971  
972 # transform structure
973 # parameters: (object_reference, structure, [hash])
974 # returns: (structure)
975 sub _trans3 {
976  
977 # get parameters
978 my ($self, $in, $hash) = @_;
979  
980 # transform the array structure
981 _crawl($self, $in, my $out = [], $hash);
982  
983 # return
984 return($out);
985  
986 }
987  
988 # recursive transform
989 # array structure is traversed until scalar arrays are found and transformed
990 # parameters: (ref_to_object, input_array_reference, output_array_reference, hash)
991 sub _crawl {
992  
993 # get parameters
994 my ($self, $in, $out, $hash) = @_;
995  
996 # if input is a vector (reference to a scalar array)
997 if (@{$in} == grep {! ref()} @{$in}) {
998
999 # transform input vector and copy to output
1000 @{$out} = @{_trans1($self, $in, $hash)};
1001
1002 } else {
1003
1004 # for each input element
1005 for my $i (0 .. $#{$in}) {
1006
1007 # if an array reference
1008 if (ref($in->[$i]) eq 'ARRAY') {
1009
1010 # transform next level
1011 _crawl($self, $in->[$i], $out->[$i] = [], $hash);
1012
1013 } else {
1014
1015 # error
1016 croak('invalid transform input');
1017
1018 }
1019
1020 }
1021
1022 }
1023
1024 }
1025  
1026 # check object structure
1027 # parameter: (ref_to_object)
1028 # returns: (number_input_channels, number_output_channels)
1029 sub _check {
1030  
1031 # get object reference
1032 my $self = shift();
1033  
1034 # local variables
1035 my (@class, $ci, $co);
1036  
1037 # make object class array
1038 @class = qw(ICC::Profile::cvst ICC::Profile::matf ICC::Profile::cvst ICC::Profile::clut ICC::Profile::cvst);
1039  
1040 # verify number of processing elements
1041 ($#{$self->[1]} <= 4) || croak('\'mBA_\' object has too many processing elements');
1042  
1043 # for each processing element
1044 for my $i (0 .. $#{$self->[1]}) {
1045
1046 # if element is defined (matrix and M-curves to may be undefined)
1047 if (defined($self->[1][$i])) {
1048
1049 # verify element has correct class
1050 (ref($self->[1][$i]) eq $class[$i]) || croak("'mBA_' processing element $i has wrong class");
1051
1052 # if element has 'cin' method
1053 if ($self->[1][$i]->can('cin')) {
1054
1055 # if number of input channels is undefined
1056 if (! defined($ci)) {
1057
1058 # set number of input channels
1059 $ci = $self->[1][$i]->cin();
1060
1061 }
1062
1063 # if number of output channels is defined
1064 if (defined($co)) {
1065
1066 # verify input channels of this element match output channels of previous element
1067 ($self->[1][$i]->cin() == $co) || croak("'mBA_' processing element $i has wrong number of channels");
1068
1069 }
1070
1071 }
1072
1073 # if element has 'cout' method
1074 if ($self->[1][$i]->can('cout')) {
1075
1076 # set number of output channels
1077 $co = $self->[1][$i]->cout();
1078
1079 }
1080
1081 }
1082
1083 }
1084  
1085 # verify B-curves are defined
1086 (defined($self->[1][0])) || croak('B-curves are required');
1087  
1088 # verify matrix and M-curves are both defined, or neither
1089 (defined($self->[1][1]) xor defined($self->[1][2])) && croak('matrix and M-curves must both be defined, or neither');
1090  
1091 # verify CLUT and A-curves are both defined, or neither
1092 (defined($self->[1][3]) xor defined($self->[1][4])) && croak('CLUT and A-curves must both be defined, or neither');
1093  
1094 # if matrix defined
1095 if (defined($self->[1][1])) {
1096
1097 # verify object has 3 output channels
1098 ($ci == 3) || croak("matrix processing element not permitted with $ci input channels");
1099
1100 # verify matrix size (3x3)
1101 ($self->[1][1]->cin() == 3 && $self->[1][1]->cout() == 3) || croak('matrix processing element matrix wrong size');
1102
1103 # verify offset size (undefined, 0 or 3)
1104 (! defined($self->[1][1][2]) || @{$self->[1][1][2]} == 0 || @{$self->[1][1][2]} == 3) || croak('matrix processing element offset wrong size');
1105
1106 }
1107  
1108 # return
1109 return($ci, $co);
1110  
1111 }
1112  
1113 # set M-curves, matrix, and B-curves
1114 # equivalent to inverse of a matrix-based display profile
1115 # parameters: (ref_to_mAB_object, ref_to_profile_object)
1116 sub _newICCmatrix {
1117  
1118 # get parameters
1119 my ($self, $disp) = @_;
1120  
1121 # local variables
1122 my (@XYZ, @TRC, $wtpt, $bkpt, $id, $mat);
1123  
1124 # verify an RGB/XYZ display profile
1125 (UNIVERSAL::isa($disp, 'ICC::Profile') && $disp->profile_header->[3] eq 'mntr' && $disp->profile_header->[4] eq 'RGB ' && $disp->profile_header->[5] eq 'XYZ ') || croak('not an RGB-XYZ display profile');
1126  
1127 # get primary tags
1128 @XYZ = $disp->tag(qw(rXYZ gXYZ bXYZ));
1129  
1130 # get TRC tags
1131 @TRC = $disp->tag(qw(rTRC gTRC bTRC));
1132  
1133 # verify a matrix profile
1134 (@XYZ == 3 && @TRC == 3) || croak('not a matrix profile');
1135  
1136 # set input colorspace
1137 $self->[0]{'input_cs'} = 'XYZ ';
1138  
1139 # set output colorspace
1140 $self->[0]{'output_cs'} = 'RGB ';
1141  
1142 # set PCS encoding
1143 $self->[0]{'pcs_encoding'} = 7;
1144  
1145 # get white point tag
1146 $wtpt = $disp->tag('wtpt');
1147  
1148 # set white point value
1149 $self->[0]{'wtpt'} = [@{$wtpt->XYZ}] if (defined($wtpt));
1150  
1151 # get black point tag
1152 $bkpt = $disp->tag('bkpt');
1153  
1154 # set black point value
1155 $self->[0]{'bkpt'} = [@{$bkpt->XYZ}] if (defined($bkpt));
1156  
1157 # make identity curve
1158 $id = ICC::Profile::curv->new();
1159  
1160 # make Math::Matrix object from primary tags (inverse)
1161 $mat = Math::Matrix->new(map {$_->XYZ()} @XYZ)->transpose->invert->multiply_scalar(65535/32768);
1162  
1163 # set B-curves
1164 $self->[1][0] = ICC::Profile::cvst->new([$id, $id, $id]);
1165  
1166 # set matrix (inverse)
1167 $self->[1][1] = ICC::Profile::matf->new({'matrix' => $mat});
1168  
1169 # set M-curves (inverse)
1170 $self->[1][2] = ICC::Profile::cvst->new([map {$_->inv()} @TRC]);
1171  
1172 # set transform mask
1173 $self->[2] = 0x07;
1174  
1175 }
1176  
1177 # make new mBA_ tag from attribute hash
1178 # hash may contain pointers to B-curves, matrix, M-curves, CLUT, or A-curves
1179 # keys are: ('b_curves', 'matrix', 'm_curves', 'clut', 'a_curves')
1180 # tag elements not specified in the hash are left empty
1181 # parameters: (ref_to_object, ref_to_attribute_hash)
1182 sub _new_from_hash {
1183  
1184 # get parameters
1185 my ($self, $hash) = @_;
1186  
1187 # set attribute list (key => [reference_type, array_index])
1188 my %list = ('b_curves' => ['ICC::Profile::cvst', 0], 'matrix' => ['ICC::Profile::matf', 1], 'm_curves' => ['ICC::Profile::cvst', 2], 'clut' => ['ICC::Profile::clut', 3], 'a_curves' => ['ICC::Profile::cvst', 4]);
1189  
1190 # for each attribute
1191 for my $attr (keys(%{$hash})) {
1192
1193 # if value defined
1194 if (defined($hash->{$attr})) {
1195
1196 # if correct reference type
1197 if (ref($hash->{$attr}) eq $list{$attr}[0]) {
1198
1199 # set tag element
1200 $self->[1][$list{$attr}[1]] = $hash->{$attr};
1201
1202 # set transform mask bit
1203 $self->[2] |= (0x01 << $list{$attr}[1]);
1204
1205 } else {
1206
1207 # error
1208 croak("wrong object type for $attr key");
1209
1210 }
1211
1212 }
1213
1214 }
1215
1216 }
1217  
1218 # read mBA_ tag from ICC profile
1219 # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
1220 sub _readICCmBA_ {
1221  
1222 # get parameters
1223 my ($self, $parent, $fh, $tag) = @_;
1224  
1225 # local variables
1226 my ($buf, @mft, $pel, $mark, @mat, $bytes, $gsa);
1227  
1228 # set tag signature
1229 $self->[0]{'signature'} = $tag->[0];
1230  
1231 # set input colorspace
1232 $self->[0]{'input_cs'} = $parent->[1][5];
1233  
1234 # set output colorspace
1235 $self->[0]{'output_cs'} = $parent->[1][4];
1236  
1237 # seek start of tag
1238 seek($fh, $tag->[1], 0);
1239  
1240 # read tag header
1241 read($fh, $buf, 32);
1242  
1243 # unpack header
1244 @mft = unpack('a4 x4 C2 x2 N5', $buf);
1245  
1246 # verify tag signature
1247 ($mft[0] eq 'mBA ') or croak('wrong tag type');
1248  
1249 # verify number input channels (1 to 15)
1250 ($mft[1] > 0 && $mft[1] < 16) || croak('unsupported number of input channels');
1251  
1252 # verify number output channels (1 to 15)
1253 ($mft[2] > 0 && $mft[2] < 16) || croak('unsupported number of output channels');
1254  
1255 # if B-curves are defined
1256 if ($mft[3]) {
1257
1258 # make 'cvst' object for B-curves
1259 $pel = ICC::Profile::cvst->new();
1260
1261 # set file pointer to start of first B-curve
1262 $mark = $tag->[1] + $mft[3];
1263
1264 # for each input channel
1265 for my $i (0 .. $mft[1] - 1) {
1266
1267 # adjust file pointer to 4-byte boundary
1268 $mark += (-$mark % 4);
1269
1270 # seek to start of curve
1271 seek($fh, $mark, 0);
1272
1273 # read curve type
1274 read($fh, $buf, 4);
1275
1276 # if 'curv' type
1277 if ($buf eq 'curv') {
1278
1279 # parse 'curv' object
1280 $pel->[1][$i] = ICC::Profile::curv->new_fh($self, $fh, ['cvst', $mark, 0, 0]);
1281
1282 } elsif ($buf eq 'para') {
1283
1284 # parse 'para' object
1285 $pel->[1][$i] = ICC::Profile::para->new_fh($self, $fh, ['cvst', $mark, 0, 0]);
1286
1287 } else {
1288
1289 # error
1290 croak('unsupported curve type or invalid tag structure');
1291
1292 }
1293
1294 # mark current file pointer location
1295 $mark = tell($fh);
1296
1297 }
1298
1299 # set signature
1300 $pel->[0]{'signature'} = 'mBA_';
1301
1302 # add processing element
1303 $self->[1][0] = $pel;
1304
1305 # set transform mask
1306 $self->[2] |= 0x01;
1307
1308 }
1309  
1310 # if matrix is defined
1311 if ($mft[4]) {
1312
1313 # make 'matf' object for matrix
1314 $pel = ICC::Profile::matf->new();
1315
1316 # seek to start of matrix
1317 seek($fh, $tag->[1] + $mft[4], 0);
1318
1319 # read matrix
1320 $pel->_read_matf($fh, 3, 3, 1, 2);
1321
1322 # set signature
1323 $pel->[0]{'signature'} = 'mBA_';
1324
1325 # add processing element
1326 $self->[1][1] = $pel;
1327
1328 # set transform mask
1329 $self->[2] |= 0x02;
1330
1331 }
1332  
1333 # if M-curves are defined
1334 if ($mft[5]) {
1335
1336 # make 'cvst' object for M-curves
1337 $pel = ICC::Profile::cvst->new();
1338
1339 # set file pointer to start of first M-curve
1340 $mark = $tag->[1] + $mft[5];
1341
1342 # for each input channel
1343 for my $i (0 .. $mft[1] - 1) {
1344
1345 # adjust file pointer to 4-byte boundary
1346 $mark += (-$mark % 4);
1347
1348 # seek to start of curve
1349 seek($fh, $mark, 0);
1350
1351 # read curve type
1352 read($fh, $buf, 4);
1353
1354 # if 'curv' type
1355 if ($buf eq 'curv') {
1356
1357 # parse 'curv' object
1358 $pel->[1][$i] = ICC::Profile::curv->new_fh($self, $fh, ['cvst', $mark, 0, 0]);
1359
1360 } elsif ($buf eq 'para') {
1361
1362 # parse 'para' object
1363 $pel->[1][$i] = ICC::Profile::para->new_fh($self, $fh, ['cvst', $mark, 0, 0]);
1364
1365 } else {
1366
1367 # error
1368 croak('unsupported curve type or invalid tag structure');
1369
1370 }
1371
1372 # mark current file pointer location
1373 $mark = tell($fh);
1374
1375 }
1376
1377 # set signature
1378 $pel->[0]{'signature'} = 'mBA_';
1379
1380 # add processing element
1381 $self->[1][2] = $pel;
1382
1383 # set transform mask
1384 $self->[2] |= 0x04;
1385
1386 }
1387  
1388 # if CLUT defined
1389 if ($mft[6]) {
1390
1391 # make 'clut' object for CLUT
1392 $pel = ICC::Profile::clut->new();
1393
1394 # seek to start of CLUT
1395 seek($fh, $tag->[1] + $mft[6], 0);
1396
1397 # read header
1398 read($fh, $buf, 20);
1399
1400 # unpack header
1401 @mat = unpack('C17', $buf);
1402
1403 # get CLUT byte width
1404 $bytes = pop(@mat);
1405
1406 # save CLUT byte width
1407 $pel->[0]{'clut_bytes'} = $bytes;
1408
1409 # set number of input channels
1410 $pel->[0]{'input_channels'} = $mft[1];
1411  
1412 # set number of output channels
1413 $pel->[0]{'output_channels'} = $mft[2];
1414  
1415 # make grid size array
1416 $gsa = [grep {$_} @mat];
1417
1418 # read 'clut' data
1419 $pel->_read_clut($fh, $mft[2], $gsa, $bytes);
1420
1421 # save grid size array
1422 $pel->[2] = [@{$gsa}];
1423
1424 # set signature
1425 $pel->[0]{'signature'} = 'mBA_';
1426
1427 # add processing element
1428 $self->[1][3] = $pel;
1429
1430 # set transform mask
1431 $self->[2] |= 0x08;
1432
1433 }
1434  
1435 # if A-curves are defined
1436 if ($mft[7]) {
1437
1438 # make 'cvst' object for A-curves
1439 $pel = ICC::Profile::cvst->new();
1440
1441 # set file pointer to start of first A-curve
1442 $mark = $tag->[1] + $mft[7];
1443
1444 # for each output channel
1445 for my $i (0 .. $mft[2] - 1) {
1446
1447 # adjust file pointer to 4-byte boundary
1448 $mark += (-$mark % 4);
1449
1450 # seek to start of curve
1451 seek($fh, $mark, 0);
1452
1453 # read curve type
1454 read($fh, $buf, 4);
1455
1456 # if 'curv' type
1457 if ($buf eq 'curv') {
1458
1459 # parse 'curv' object
1460 $pel->[1][$i] = ICC::Profile::curv->new_fh($self, $fh, ['cvst', $mark, 0, 0]);
1461
1462 } elsif ($buf eq 'para') {
1463
1464 # parse 'para' object
1465 $pel->[1][$i] = ICC::Profile::para->new_fh($self, $fh, ['cvst', $mark, 0, 0]);
1466
1467 } else {
1468
1469 # error
1470 croak('unsupported curve type or invalid tag structure');
1471
1472 }
1473
1474 # mark current file pointer location
1475 $mark = tell($fh);
1476
1477 }
1478
1479 # set signature
1480 $pel->[0]{'signature'} = 'mBA_';
1481
1482 # add processing element
1483 $self->[1][4] = $pel;
1484
1485 # set transform mask
1486 $self->[2] |= 0x10;
1487
1488 }
1489  
1490 }
1491  
1492 # write mBA_ tag to ICC profile
1493 # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
1494 sub _writeICCmBA_ {
1495  
1496 # get parameters
1497 my ($self, $parent, $fh, $tag) = @_;
1498  
1499 # local variables
1500 my (@mft, $offset, @mat, $bytes, $size);
1501  
1502 # set tag type
1503 $mft[0] = 'mBA ';
1504  
1505 # check object structure
1506 @mft[1, 2] = _check($self);
1507  
1508 # initialize offset
1509 $offset = 32;
1510  
1511 # set offset to B-curves
1512 $mft[3] = 32;
1513  
1514 # if B-curves defined
1515 if (defined($self->[1][0])) {
1516
1517 # for each B-curve
1518 for my $curve (@{$self->[1][0]->array()}) {
1519
1520 # add curve size
1521 $offset += $curve->size;
1522
1523 # pad to 4-bytes
1524 $offset += (-$offset % 4);
1525
1526 }
1527
1528 } else {
1529
1530 # error
1531 croak('B-curves are required');
1532
1533 }
1534  
1535 # if matrix is defined
1536 if (defined($self->[1][1])) {
1537
1538 # verify output channels
1539 ($mft[2] == 3) || croak('3 output channels required for matrix');
1540
1541 # set offset
1542 $mft[4] = $offset;
1543
1544 # increment offset for matrix size
1545 $offset += 48;
1546
1547 } else {
1548
1549 # set offset
1550 $mft[4] = 0;
1551
1552 }
1553  
1554 # if M-curves defined
1555 if (defined($self->[1][2])) {
1556
1557 # verify output channels
1558 ($mft[2] == 3) || croak('3 output channels required for M-curves');
1559
1560 # set offset
1561 $mft[5] = $offset;
1562
1563 # for each M-curve
1564 for my $curve (@{$self->[1][2]->array()}) {
1565
1566 # add curve size
1567 $offset += $curve->size;
1568
1569 # pad to 4-bytes
1570 $offset += (-$offset % 4);
1571
1572 }
1573
1574 } else {
1575
1576 # set offset
1577 $mft[5] = 0;
1578
1579 }
1580  
1581 # if CLUT defined
1582 if (defined($self->[1][3])) {
1583
1584 # set offset
1585 $mft[6] = $offset;
1586
1587 # get CLUT data size
1588 $bytes = $self->[1][3][0]{'clut_bytes'} || 2;
1589
1590 # add CLUT size to offset
1591 $offset += 20 + $self->[1][3]->_clut_size($bytes);
1592
1593 # pad to 4-byte boundary
1594 $offset += (-$offset % 4);
1595
1596 } else {
1597
1598 # set offset
1599 $mft[6] = 0;
1600
1601 }
1602  
1603 # if A-curves defined
1604 if (defined($self->[1][4])) {
1605
1606 # set offset
1607 $mft[7] = $offset;
1608
1609 } else {
1610
1611 # set offset
1612 $mft[7] = 0;
1613
1614 }
1615  
1616 # seek start of tag
1617 seek($fh, $tag->[1], 0);
1618  
1619 # write header
1620 print $fh pack('a4 x4 C2 x2 N5', @mft);
1621  
1622 # seek start of B-curves
1623 seek($fh, $tag->[1] + $mft[3], 0);
1624  
1625 # for each B-curve
1626 for my $curve (@{$self->[1][0]->array()}) {
1627
1628 # write curve object
1629 $curve->write_fh($self->[1][0], $fh, ['cvst', tell($fh), 0, 0]);
1630
1631 # add padding to 4-byte boundary
1632 seek($fh, (-tell($fh) % 4), 1);
1633
1634 }
1635  
1636 # if matrix is defined
1637 if (defined($self->[1][1])) {
1638
1639 # seek start of matrix
1640 seek($fh, $tag->[1] + $mft[4], 0);
1641
1642 # write matrix
1643 $self->[1][1]->_write_matf($fh, 1, 2);
1644
1645 }
1646  
1647 # if M-curves are defined
1648 if (defined($self->[1][2])) {
1649
1650 # seek start of M-curves
1651 seek($fh, $tag->[1] + $mft[5], 0);
1652
1653 # for each M-curve
1654 for my $curve (@{$self->[1][2]->array()}) {
1655
1656 # write curve object
1657 $curve->write_fh($self->[1][2], $fh, ['cvst', tell($fh), 0, 0]);
1658
1659 # add padding to 4-byte boundary
1660 seek($fh, (-tell($fh) % 4), 1);
1661
1662 }
1663
1664 }
1665  
1666 # if CLUT is defined
1667 if (defined($self->[1][3])) {
1668
1669 # for each possible input channel
1670 for my $i (0 .. 15) {
1671
1672 # set grid size
1673 $mat[$i] = $self->[1][3]->gsa->[$i] || 0;
1674
1675 }
1676
1677 # set CLUT byte width
1678 $mat[16] = $bytes;
1679
1680 # seek start of CLUT
1681 seek($fh, $tag->[1] + $mft[6], 0);
1682
1683 # write CLUT header
1684 print $fh pack('C17 x3', @mat);
1685
1686 # write CLUT
1687 $self->[1][3]->_write_clut($fh, $self->[1][3]->gsa(), $bytes);
1688
1689 }
1690  
1691 # if A-curves are defined
1692 if (defined($self->[1][4])) {
1693
1694 # seek start of A-curves
1695 seek($fh, $tag->[1] + $mft[7], 0);
1696
1697 # for each M-curve
1698 for my $curve (@{$self->[1][4]->array()}) {
1699
1700 # write curve object
1701 $curve->write_fh($self->[1][4], $fh, ['cvst', tell($fh), 0, 0]);
1702
1703 # add padding to 4-byte boundary
1704 seek($fh, (-tell($fh) % 4), 1);
1705
1706 }
1707
1708 }
1709
1710 }
1711  
1712 1;