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

filter: Statementstmt 0.0% 0 / 375 Branchbran 0.0% 0 / 136 Conditioncond 0.0% 0 / 127 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 34 Podpod 0.0% 0 / 29 totaltotal 0.0% 0 / 701 CC 69 SCAR 33.3
CC
69
Cov
0%
sdump
90.0
transform
56.0
_check
42.0
CRAP 28.0
1 package ICC::Profile::mft1;
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 mft1 object
22 # hash may contain pointers to matrix, input curves, CLUT, or output curves
23 # keys are: ('matrix', 'input', 'clut', 'output')
24 # tag elements not specified in the hash are left empty
25 # parameters: ([ref_to_attribute_hash])
26 # returns: (ref_to_object)
27 sub new {
28  
29 # get object class
30 my $class = shift();
31  
32 # create empty mft1 object
33 my $self = [
34 {}, # object header
35 [], # processing elements
36 0x00, # transform mask
37 0x00 # clipping mask
38 ];
39  
40 # if there are parameters
41 if (@_) {
42
43 # if one parameter, a hash reference
44 if (@_ == 1 && ref($_[0]) eq 'HASH') {
45
46 # make new mft1 tag from attribute hash
47 _new_from_hash($self, @_);
48
49 } else {
50
51 # error
52 croak('parameter must be a hash reference');
53
54 }
55
56 }
57  
58 # bless object
59 bless($self, $class);
60  
61 # return object reference
62 return($self);
63  
64 }
65  
66 # get/set reference to header hash
67 # parameters: ([ref_to_new_hash])
68 # returns: (ref_to_hash)
69 sub header {
70  
71 # get object reference
72 my $self = shift();
73  
74 # if there are parameters
75 if (@_) {
76
77 # if one parameter, a hash reference
78 if (@_ == 1 && ref($_[0]) eq 'HASH') {
79
80 # set header to new hash
81 $self->[0] = shift();
82
83 } else {
84
85 # error
86 croak('parameter must be a hash reference');
87
88 }
89
90 }
91  
92 # return reference
93 return($self->[0]);
94  
95 }
96  
97 # get/set processing element array reference
98 # parameters: ([ref_to_array])
99 # returns: (ref_to_array)
100 sub array {
101  
102 # get object reference
103 my $self = shift();
104  
105 # if parameter
106 if (@_) {
107
108 # verify array reference
109 (ref($_[0]) eq 'ARRAY') || croak('not an array reference');
110
111 # set array reference
112 $self->[1] = [@{shift()}];
113
114 }
115  
116 # return array reference
117 return($self->[1]);
118  
119 }
120  
121 # get/set reference to matrix 'matf' object
122 # parameters: ([ref_to_new_object])
123 # returns: (ref_to_object)
124 sub matrix {
125  
126 # get object reference
127 my $self = shift();
128  
129 # if there are parameters
130 if (@_) {
131
132 # if one parameter, an 'matf' object
133 if (@_ == 1 && UNIVERSAL::isa($_[0], 'ICC::Profile::matf')) {
134
135 # warn if 'matf' object has offset values
136 (@{$_[0]->offset()}) || carp('offset values in matrix object are not supported');
137
138 # set matrix to new object
139 $self->[1][0] = shift();
140
141 # set transform mask bit
142 $self->[2] |= 0x01;
143
144 } else {
145
146 # error
147 croak('parameter must be an \'matf\' object');
148
149 }
150
151 }
152  
153 # return object reference
154 return($self->[1][0]);
155  
156 }
157  
158 # get/set reference to input curves 'cvst' object
159 # parameters: ([ref_to_new_object])
160 # returns: (ref_to_object)
161 sub input {
162  
163 # get object reference
164 my $self = shift();
165  
166 # if there are parameters
167 if (@_) {
168
169 # if one parameter, a 'cvst' object
170 if (@_ == 1 && UNIVERSAL::isa($_[0], 'ICC::Profile::cvst')) {
171
172 # set input curves to new object
173 $self->[1][1] = shift();
174
175 # set transform mask bit
176 $self->[2] |= 0x02;
177
178 } else {
179
180 # error
181 croak('parameter must be a \'cvst\' object');
182
183 }
184
185 }
186  
187 # return object reference
188 return($self->[1][1]);
189  
190 }
191  
192 # get/set reference to CLUT 'clut' object
193 # parameters: ([ref_to_new_object])
194 # returns: (ref_to_object)
195 sub clut {
196  
197 # get object reference
198 my $self = shift();
199  
200 # if there are parameters
201 if (@_) {
202
203 # if one parameter, a 'clut' object
204 if (@_ == 1 && UNIVERSAL::isa($_[0], 'ICC::Profile::clut')) {
205
206 # set CLUT to new object
207 $self->[1][2] = shift();
208
209 # set transform mask bit
210 $self->[2] |= 0x04;
211
212 } else {
213
214 # error
215 croak('parameter must be a \'clut\' object');
216
217 }
218
219 }
220  
221 # return object reference
222 return($self->[1][2]);
223  
224 }
225  
226 # get/set reference to output curves 'cvst' object
227 # parameters: ([ref_to_new_object])
228 # returns: (ref_to_object)
229 sub output {
230  
231 # get object reference
232 my $self = shift();
233  
234 # if there are parameters
235 if (@_) {
236
237 # if one parameter, a 'cvst' object
238 if (@_ == 1 && UNIVERSAL::isa($_[0], 'ICC::Profile::cvst')) {
239
240 # set output curves to new object
241 $self->[1][3] = shift();
242
243 # set transform mask bit
244 $self->[2] |= 0x08;
245
246 } else {
247
248 # error
249 croak('parameter must be a \'cvst\' object');
250
251 }
252
253 }
254  
255 # return object reference
256 return($self->[1][3]);
257  
258 }
259  
260 # get/set transform mask
261 # bits 3-2-1-0 correpsond to OUTPUT-CLUT-INPUT-MATRIX
262 # parameters: ([new_mask_value])
263 # returns: (mask_value)
264 sub mask {
265  
266 # get object reference
267 my $self = shift();
268  
269 # if there are parameters
270 if (@_) {
271
272 # if one parameter
273 if (@_ == 1) {
274
275 # set object transform mask value
276 $self->[2] = shift();
277
278 } else {
279
280 # error
281 croak('more than one parameter');
282
283 }
284
285 }
286  
287 # return transform mask value
288 return($self->[2]);
289  
290 }
291  
292 # get/set clipping mask
293 # bits 3-2-1-0 correpsond to OUTPUT-CLUT-INPUT-MATRIX
294 # parameters: ([new_mask_value])
295 # returns: (mask_value)
296 sub clip {
297
298 # get object reference
299 my $self = shift();
300
301 # if there are parameters
302 if (@_) {
303
304 # if one parameter
305 if (@_ == 1) {
306
307 # set object clipping mask value
308 $self->[3] = shift();
309
310 } else {
311
312 # error
313 croak('more than one parameter');
314
315 }
316
317 }
318
319 # return clipping mask value
320 return($self->[3]);
321
322 }
323  
324 # create mft1 tag object from ICC profile
325 # parameters: (ref_to_parent_object, file_handle, ref_to_tag_table_entry)
326 # returns: (ref_to_object)
327 sub new_fh {
328  
329 # get object class
330 my $class = shift();
331  
332 # create empty mft1 object
333 my $self = [
334 {}, # object header
335 [], # processing elements
336 0x00, # transform mask
337 0x00 # clipping mask
338 ];
339  
340 # verify 3 parameters
341 (@_ == 3) || croak('wrong number of parameters');
342  
343 # read mft1 data from profile
344 _readICCmft1($self, @_);
345  
346 # bless object
347 bless($self, $class);
348  
349 # return object reference
350 return($self);
351  
352 }
353  
354 # writes mft1 tag object to ICC profile
355 # parameters: (ref_to_parent_object, file_handle, ref_to_tag_table_entry)
356 sub write_fh {
357  
358 # get tag reference
359 my $self = shift();
360  
361 # verify 3 parameters
362 (@_ == 3) || croak('wrong number of parameters');
363  
364 # write mft1 data to profile
365 _writeICCmft1($self, @_);
366  
367 }
368  
369 # get tag size (for writing to profile)
370 # returns: (tag_clut_size)
371 sub size {
372  
373 # get parameters
374 my ($self) = @_;
375  
376 # set header size
377 my $size = 48;
378  
379 # add size of input tables (assumes 'curv' objects)
380 $size += $self->[1][1]->cin() * 256;
381  
382 # add size of clut
383 $size += $self->[1][2]->_clut_size(1);
384  
385 # add size of output tables (assumes 'curv' objects)
386 $size += $self->[1][3]->cin() * 256;
387  
388 # return size
389 return($size);
390  
391 }
392  
393 # get number of input channels
394 # returns: (number)
395 sub cin {
396  
397 # get object reference
398 my $self = shift();
399  
400 # return
401 return($self->[1][1]->cin());
402  
403 }
404  
405 # get number of output channels
406 # returns: (number)
407 sub cout {
408  
409 # get object reference
410 my $self = shift();
411  
412 # return
413 return($self->[1][3]->cout());
414  
415 }
416  
417 # transform data
418 # transform mask enables/disables individual tag elements
419 # clipping mask enables/disables individual tag output clipping
420 # supported input types:
421 # parameters: (list, [hash])
422 # parameters: (vector, [hash])
423 # parameters: (matrix, [hash])
424 # parameters: (Math::Matrix_object, [hash])
425 # parameters: (structure, [hash])
426 # returns: (same_type_as_input)
427 sub transform {
428  
429 # set hash value (0 or 1)
430 my $h = ref($_[-1]) eq 'HASH' ? 1 : 0;
431  
432 # if input a 'Math::Matrix' object
433 if (@_ == $h + 2 && UNIVERSAL::isa($_[1], 'Math::Matrix')) {
434
435 # call matrix transform
436 &_trans2;
437
438 # if input an array reference
439 } elsif (@_ == $h + 2 && ref($_[1]) eq 'ARRAY') {
440
441 # if array contains numbers (vector)
442 if (! ref($_[1][0]) && @{$_[1]} == grep {Scalar::Util::looks_like_number($_)} @{$_[1]}) {
443
444 # call vector transform
445 &_trans1;
446
447 # if array contains vectors (2-D array)
448 } elsif (ref($_[1][0]) eq 'ARRAY' && @{$_[1]} == grep {ref($_) eq 'ARRAY' && Scalar::Util::looks_like_number($_->[0])} @{$_[1]}) {
449
450 # call matrix transform
451 &_trans2;
452
453 } else {
454
455 # call structure transform
456 &_trans3;
457
458 }
459
460 # if input a list (of numbers)
461 } elsif (@_ == $h + 1 + grep {Scalar::Util::looks_like_number($_)} @_) {
462
463 # call list transform
464 &_trans0;
465
466 } else {
467
468 # error
469 croak('invalid transform input');
470
471 }
472  
473 }
474  
475 # inverse transform
476 # note: number of undefined output values must equal number of defined input values
477 # note: the input and output vectors contain the final solution on return
478 # hash key 'init' specifies initial value vector
479 # hash key 'ubox' enables unit box extrapolation
480 # parameters: (input_vector, output_vector, [hash])
481 # returns: (RMS_error_value)
482 sub inverse {
483  
484 # get parameters
485 my ($self, $in, $out, $hash) = @_;
486  
487 # local variables
488 my ($i, $j, @si, @so, $init);
489 my ($int, $jac, $mat, $delta);
490 my ($max, $elim, $dlim, $accum, $error);
491  
492 # initialize indices
493 $i = $j = -1;
494  
495 # build slice arrays while validating input and output arrays
496 ((grep {$i++; defined() && push(@si, $i)} @{$in}) == (grep {$j++; ! defined() && push(@so, $j)} @{$out})) || croak('wrong number of undefined values');
497  
498 # get init array
499 $init = $hash->{'init'};
500  
501 # for each undefined output value
502 for my $i (@so) {
503
504 # set to supplied initial value or 0.5
505 $out->[$i] = defined($init->[$i]) ? $init->[$i] : 0.5;
506
507 }
508  
509 # set maximum loop count
510 $max = $hash->{'inv_max'} || 10;
511  
512 # loop error limit
513 $elim = $hash->{'inv_elim'} || 1E-6;
514  
515 # set delta limit
516 $dlim = $hash->{'inv_dlim'} || 0.5;
517  
518 # create empty solution matrix
519 $mat = Math::Matrix->new([]);
520  
521 # compute initial transform values
522 ($jac, $int) = jacobian($self, $out, $hash);
523  
524 # solution loop
525 for (1 .. $max) {
526
527 # for each input
528 for my $i (0 .. $#si) {
529
530 # for each output
531 for my $j (0 .. $#so) {
532
533 # copy Jacobian value to solution matrix
534 $mat->[$i][$j] = $jac->[$si[$i]][$so[$j]];
535
536 }
537
538 # save residual value to solution matrix
539 $mat->[$i][$#si + 1] = $in->[$si[$i]] - $int->[$si[$i]];
540
541 }
542
543 # solve for delta values
544 $delta = $mat->solve;
545
546 # for each output value
547 for my $i (0 .. $#so) {
548
549 # add delta (limited using hyperbolic tangent)
550 $out->[$so[$i]] += POSIX::tanh($delta->[$i][0]/$dlim) * $dlim;
551
552 }
553
554 # compute updated transform values
555 ($jac, $int) = jacobian($self, $out, $hash);
556
557 # initialize error accumulator
558 $accum = 0;
559
560 # for each input
561 for my $i (0 .. $#si) {
562
563 # accumulate delta squared
564 $accum += ($in->[$si[$i]] - $int->[$si[$i]])**2;
565
566 }
567
568 # compute RMS error
569 $error = sqrt($accum/@si);
570
571 # if error less than limit
572 last if ($error < $elim);
573
574 }
575  
576 # update input vector with final values
577 @{$in} = @{$int};
578  
579 # return
580 return($error);
581  
582 }
583  
584 # compute Jacobian matrix
585 # transform mask enables/disables individual tag elements
586 # parameters: (input_vector, [hash])
587 # returns: (Jacobian_matrix, [output_vector])
588 sub jacobian {
589  
590 # get parameters
591 my ($self, $data, $hash) = @_;
592  
593 # local variables
594 my ($jac, $jaci);
595  
596 # for each processing element
597 for my $i (0 .. $#{$self->[1]}) {
598
599 # if processing element defined, and transform mask bit set
600 if (defined($self->[1][$i]) && $self->[2] & 0x01 << $i) {
601
602 # compute Jacobian matrix and transform data
603 ($jaci, $data) = $self->[1][$i]->jacobian($data, $hash);
604
605 # multiply Jacobian matrices
606 $jac = defined($jac) ? $jaci * $jac : $jaci;
607
608 }
609
610 }
611  
612 # if Jacobian matrix is undefined, use identity matrix
613 $jac = Math::Matrix->diagonal((1) x @{$data}) if (! defined($jac));
614  
615 # if output values wanted
616 if (wantarray) {
617
618 # return Jacobian and output values
619 return($jac, $data);
620
621 } else {
622
623 # return Jacobian only
624 return($jac);
625
626 }
627
628 }
629  
630 # get/set PCS encoding
631 # for use with ICC::Support::PCS objects
632 # parameters: ([PCS_encoding])
633 # returns: (PCS_encoding)
634 sub pcs {
635  
636 # get parameters
637 my ($self, $pcs) = @_;
638  
639 # if PCS parameter is supplied
640 if (defined($pcs)) {
641
642 # if a valid PCS encoding
643 if ($pcs == 0) {
644
645 # copy to tag header hash
646 $self->[0]{'pcs_encoding'} = $pcs;
647
648 # return PCS encoding
649 return($pcs);
650
651 } else {
652
653 # error
654 croak('invalid PCS encoding');
655
656 }
657
658 } else {
659
660 # if tag PCS is L*a*b*
661 if (($self->[0]{'input_cs'} eq 'Lab ' && $self->[0]{'output_cs'} ne 'XYZ ') || ($self->[0]{'input_cs'} ne 'XYZ ' && $self->[0]{'output_cs'} eq 'Lab ')) {
662
663 # return PCS type (8-bit CIELab)
664 return(0);
665
666 # if tag PCS is XYZ
667 } elsif (($self->[0]{'input_cs'} eq 'XYZ ' && $self->[0]{'output_cs'} ne 'Lab ') || ($self->[0]{'input_cs'} ne 'Lab ' && $self->[0]{'output_cs'} eq 'XYZ ')) {
668
669 # error
670 croak('invalid PCS encoding');
671
672 } else {
673
674 # error
675 croak('can\'t determine PCS encoding');
676
677 }
678
679 }
680
681 }
682  
683 # get/set white point
684 # parameters: ([white_point])
685 # returns: (white_point)
686 sub wtpt {
687  
688 # get parameters
689 my ($self, $wtpt) = @_;
690  
691 # if white point parameter is supplied
692 if (defined($wtpt)) {
693
694 # if an array of three scalars
695 if (@{$wtpt} == 3 && 3 == grep {! ref()} @{$wtpt}) {
696
697 # copy to tag header hash
698 $self->[0]{'wtpt'} = $wtpt;
699
700 # return white point
701 return($wtpt);
702
703 } else {
704
705 # error
706 croak('invalid white point');
707
708 }
709
710 } else {
711
712 # if white point is defined in tag header
713 if (defined($self->[0]{'wtpt'})) {
714
715 # return return white point
716 return($self->[0]{'wtpt'});
717
718 } else {
719
720 # error
721 croak('can\'t determine white point');
722
723 }
724
725 }
726
727 }
728  
729 # print object contents to string
730 # format is an array structure
731 # parameter: ([format])
732 # returns: (string)
733 sub sdump {
734  
735 # get parameters
736 my ($self, $p) = @_;
737  
738 # local variables
739 my ($element, $fmt, $s, $pt, $st);
740  
741 # resolve parameter to an array reference
742 $p = defined($p) ? ref($p) eq 'ARRAY' ? $p : [$p] : [];
743  
744 # get format string
745 $fmt = defined($p->[0]) && ! ref($p->[0]) ? $p->[0] : 's';
746  
747 # set string to object ID
748 $s = sprintf("'%s' object, (0x%x)\n", ref($self), $self);
749  
750 # if format contains 'o'
751 if ($fmt =~ m/s/) {
752
753 # get default parameter
754 $pt = $p->[-1];
755
756 # for each processing element
757 for my $i (0 .. $#{$self->[1]}) {
758
759 # get element reference
760 $element = $self->[1][$i];
761
762 # if processing element is undefined
763 if (! defined($element)) {
764
765 # append message
766 $s .= "\tprocessing element is undefined\n";
767
768 # if processing element is not a blessed object
769 } elsif (! Scalar::Util::blessed($element)) {
770
771 # append message
772 $s .= "\tprocessing element is not a blessed object\n";
773
774 # if processing element has an 'sdump' method
775 } elsif ($element->can('sdump')) {
776
777 # get 'sdump' string
778 $st = $element->sdump(defined($p->[$i + 1]) ? $p->[$i + 1] : $pt);
779
780 # prepend tabs to each line
781 $st =~ s/^/\t/mg;
782
783 # append 'sdump' string
784 $s .= $st;
785
786 # processing element is object without an 'sdump' method
787 } else {
788
789 # append object info
790 $s .= sprintf("\t'%s' object, (0x%x)\n", ref($element), $element);
791
792 }
793
794 }
795
796 }
797  
798 # return
799 return($s);
800  
801 }
802  
803 # transform list
804 # parameters: (object_reference, list, [hash])
805 # returns: (list)
806 sub _trans0 {
807  
808 # local variables
809 my ($self, $hash, $data);
810  
811 # get object reference
812 $self = shift();
813  
814 # get optional hash
815 $hash = pop() if (ref($_[-1]) eq 'HASH');
816  
817 # process data
818 $data = _trans1($self, [@_], $hash);
819  
820 # return list
821 return(@{$data});
822  
823 }
824  
825 # transform vector
826 # parameters: (object_reference, vector, [hash])
827 # returns: (vector)
828 sub _trans1 {
829  
830 # get parameters
831 my ($self, $data, $hash) = @_;
832  
833 # for each processing element
834 for my $i (0 .. $#{$self->[1]}) {
835
836 # if processing element defined, and transform mask bit set
837 if (defined($self->[1][$i]) && $self->[2] & 0x01 << $i) {
838
839 # transform data
840 $data = $self->[1][$i]->_trans1($data, $hash);
841
842 # clip output values if clipping mask bit set
843 ICC::Shared::clip_struct($data) if ($self->[3] & 0x01 << $i);
844
845 }
846
847 }
848
849 # return
850 return($data);
851
852 }
853  
854 # transform matrix (2-D array -or- Math::Matrix object)
855 # parameters: (object_reference, matrix, [hash])
856 # returns: (matrix)
857 sub _trans2 {
858  
859 # get parameters
860 my ($self, $data, $hash) = @_;
861  
862 # for each processing element
863 for my $i (0 .. $#{$self->[1]}) {
864
865 # if processing element defined, and transform mask bit set
866 if (defined($self->[1][$i]) && $self->[2] & 0x01 << $i) {
867
868 # transform data
869 $data = $self->[1][$i]->_trans2($data, $hash);
870
871 # clip output values if clipping mask bit set
872 ICC::Shared::clip_struct($data) if ($self->[3] & 0x01 << $i);
873
874 }
875
876 }
877  
878 # return
879 return($data);
880  
881 }
882  
883 # transform structure
884 # parameters: (object_reference, structure, [hash])
885 # returns: (structure)
886 sub _trans3 {
887  
888 # get parameters
889 my ($self, $in, $hash) = @_;
890  
891 # transform the array structure
892 _crawl($self, $in, my $out = [], $hash);
893  
894 # return
895 return($out);
896  
897 }
898  
899 # recursive transform
900 # array structure is traversed until scalar arrays are found and transformed
901 # parameters: (ref_to_object, input_array_reference, output_array_reference, hash)
902 sub _crawl {
903  
904 # get parameters
905 my ($self, $in, $out, $hash) = @_;
906  
907 # if input is a vector (reference to a scalar array)
908 if (@{$in} == grep {! ref()} @{$in}) {
909
910 # transform input vector and copy to output
911 @{$out} = @{_trans1($self, $in, $hash)};
912
913 } else {
914
915 # for each input element
916 for my $i (0 .. $#{$in}) {
917
918 # if an array reference
919 if (ref($in->[$i]) eq 'ARRAY') {
920
921 # transform next level
922 _crawl($self, $in->[$i], $out->[$i] = [], $hash);
923
924 } else {
925
926 # error
927 croak('invalid transform input');
928
929 }
930
931 }
932
933 }
934
935 }
936  
937 # check object structure
938 # parameter: (ref_to_object)
939 # returns: (number_input_channels, number_output_channels, grid_size)
940 sub _check {
941  
942 # get object reference
943 my $self = shift();
944  
945 # local variables
946 my (@class, $ci, $co, $gsa);
947  
948 # make object class array
949 @class = qw(ICC::Profile::matf ICC::Profile::cvst ICC::Profile::clut ICC::Profile::cvst);
950  
951 # verify number of processing elements
952 ($#{$self->[1]} == 3) || croak('\'mft1\' object has wrong number of processing elements');
953  
954 # for each processing element
955 for my $i (0 .. 3) {
956
957 # if element is defined (matrix may be undefined)
958 if (defined($self->[1][$i])) {
959
960 # verify element has correct class
961 (ref($self->[1][$i]) eq $class[$i]) || croak("'mft1' processing element $i is wrong object type");
962
963 # if not matrix processing element
964 if ($i) {
965
966 # if number of input channels is undefined
967 if (! defined($ci)) {
968
969 # set number of input channels
970 $ci = $self->[1][$i]->cin();
971
972 }
973
974 # if number of output channels is defined
975 if (defined($co)) {
976
977 # verify input channels of this element match output channels of previous element
978 ($self->[1][$i]->cin() == $co) || croak("'mft1' processing element $i has wrong number of input channels");
979
980 }
981
982 # set number of output channels
983 $co = $self->[1][$i]->cout();
984
985 } else {
986
987 # verify matrix has 3 input and 3 output channels
988 ($self->[1][0]->cin() == 3 && $self->[1][0]->cout() == 3) || croak("'mft1' matrix processing element wrong size");
989
990 # warn if matrix has non-zero offset values
991 (defined($self->[1][0]->offset) && grep {$_} @{$self->[1][0]->offset}) && carp("'mft1' matrix processing element has non-zero offset values")
992
993 }
994
995 # if not matrix processing element
996 } elsif ($i) {
997
998 # error
999 croak("'mft1' processing element $i is missing");
1000
1001 }
1002
1003 }
1004  
1005 # get 'clut' grid size array
1006 $gsa = $self->[1][2]->gsa();
1007  
1008 # verify 'clut' grid points are same for each channel
1009 (@{$gsa} == grep {$_ == $gsa->[0]} @{$gsa}) || croak("'mft1' clut processing element grid points vary by channel");
1010  
1011 # verify input 'cvst' elements are 'curv' objects
1012 (@{$self->[1][1]->array} == map {UNIVERSAL::isa($_, 'ICC::Profile::curv')} @{$self->[1][1]->array}) || croak("'mft1' input processing element has wrong curve type");
1013  
1014 # verify input 'curv' objects have 256 entries
1015 (@{$self->[1][1]->array} == grep {@{$_->array} == 256} @{$self->[1][1]->array}) || croak("'mft1' input processing element has wrong number of curve entries");
1016  
1017 # verify output 'cvst' elements are 'curv' objects
1018 (@{$self->[1][3]->array} == map {UNIVERSAL::isa($_, 'ICC::Profile::curv')} @{$self->[1][3]->array}) || croak("'mft1' output processing element has wrong curve type");
1019  
1020 # verify output 'curv' objects have 256 entries
1021 (@{$self->[1][3]->array} == grep {@{$_->array} == 256} @{$self->[1][3]->array}) || croak("'mft1' output processing element has wrong number of curve entries");
1022  
1023 # return
1024 return($ci, $co, $gsa->[0]);
1025  
1026 }
1027  
1028 # make new mft1 tag from attribute hash
1029 # hash may contain pointers to matrix, input curves, CLUT, or output curves
1030 # keys are: ('matrix', 'input', 'clut', 'output')
1031 # tag elements not specified in the hash are left empty
1032 # parameters: (ref_to_object, ref_to_attribute_hash)
1033 sub _new_from_hash {
1034  
1035 # get parameters
1036 my ($self, $hash) = @_;
1037  
1038 # set attribute list (key => [reference_type, array_index])
1039 my %list = ('matrix' => ['ICC::Profile::matf', 0], 'input' => ['ICC::Profile::cvst', 1], 'clut' => ['ICC::Profile::clut', 2], 'output' => ['ICC::Profile::cvst', 3]);
1040  
1041 # for each attribute
1042 for my $attr (keys(%{$hash})) {
1043
1044 # if value defined
1045 if (defined($hash->{$attr})) {
1046
1047 # if correct reference type
1048 if (ref($hash->{$attr}) eq $list{$attr}[0]) {
1049
1050 # set tag element
1051 $self->[1][$list{$attr}[1]] = $hash->{$attr};
1052
1053 # set transform mask bit
1054 $self->[2] |= (0x01 << $list{$attr}[1]);
1055
1056 } else {
1057
1058 # error
1059 croak("wrong object type for $attr key");
1060
1061 }
1062
1063 }
1064
1065 }
1066
1067 }
1068  
1069 # read mft1 tag from ICC profile
1070 # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
1071 sub _readICCmft1 {
1072  
1073 # get parameters
1074 my ($self, $parent, $fh, $tag) = @_;
1075  
1076 # local variables
1077 my ($buf, @mft, $input, $gsa, $output);
1078  
1079 # set tag signature
1080 $self->[0]{'signature'} = $tag->[0];
1081  
1082 # if 'A2Bx' tag
1083 if ($tag->[0] =~ m|^A2B[0-2]$|) {
1084
1085 # set input colorspace
1086 $self->[0]{'input_cs'} = $parent->[1][4];
1087
1088 # set output colorspace
1089 $self->[0]{'output_cs'} = $parent->[1][5];
1090
1091 # if 'B2Ax' tag
1092 } elsif ($tag->[0] =~ m|^B2A[0-2]$|) {
1093
1094 # set input colorspace
1095 $self->[0]{'input_cs'} = $parent->[1][5];
1096
1097 # set output colorspace
1098 $self->[0]{'output_cs'} = $parent->[1][4];
1099
1100 # if 'prex' tag
1101 } elsif ($tag->[0] =~ m|^pre[0-2]$|) {
1102
1103 # set input colorspace
1104 $self->[0]{'input_cs'} = $parent->[1][5];
1105
1106 # set output colorspace
1107 $self->[0]{'output_cs'} = $parent->[1][5];
1108
1109 # if 'gamt' tag
1110 } elsif ($tag->[0] eq 'gamt') {
1111
1112 # set input colorspace
1113 $self->[0]{'input_cs'} = $parent->[1][5];
1114
1115 # set output colorspace
1116 $self->[0]{'output_cs'} = 'gamt';
1117
1118 }
1119  
1120 # seek start of tag
1121 seek($fh, $tag->[1], 0);
1122  
1123 # read tag header
1124 read($fh, $buf, 12);
1125  
1126 # unpack header
1127 @mft = unpack('a4 x4 C3', $buf);
1128  
1129 # verify tag signature
1130 ($mft[0] eq 'mft1') or croak('wrong tag type');
1131  
1132 # verify number input channels (1 to 15)
1133 ($mft[1] > 0 && $mft[1] < 16) || croak('unsupported number of input channels');
1134  
1135 # verify number output channels (1 to 15)
1136 ($mft[2] > 0 && $mft[2] < 16) || croak('unsupported number of output channels');
1137  
1138 # make 'matf' object for matrix
1139 $self->[1][0] = ICC::Profile::matf->new();
1140  
1141 # read matrix
1142 $self->[1][0]->_read_matf($fh, 3, 3, 0, 2);
1143  
1144 # set signature
1145 $self->[1][0][0]{'signature'} = 'mft1';
1146  
1147 # for each input curve
1148 for my $i (0 .. $mft[1] - 1) {
1149
1150 # read curve values
1151 read($fh, $buf, 256);
1152
1153 # make 'curv' object
1154 $input->[$i] = ICC::Profile::curv->new([map {$_/255} unpack('C*', $buf)]);
1155
1156 }
1157  
1158 # make 'cvst' object for input curves
1159 $self->[1][1] = ICC::Profile::cvst->new($input);
1160  
1161 # set signature
1162 $self->[1][1][0]{'signature'} = 'mft1';
1163  
1164 # make gsa array
1165 $gsa = [($mft[3]) x $mft[1]];
1166  
1167 # make 'clut' object for CLUT
1168 $self->[1][2] = ICC::Profile::clut->new();
1169  
1170 # read 'clut' data
1171 $self->[1][2]->_read_clut($fh, $mft[2], $gsa, 1);
1172
1173 # save 'clut' gsa
1174 $self->[1][2][2] = $gsa;
1175  
1176 # save CLUT byte width
1177 $self->[1][2][0]{'clut_bytes'} = 1;
1178  
1179 # set number of input channels
1180 $self->[1][2][0]{'input_channels'} = $mft[1];
1181  
1182 # set number of output channels
1183 $self->[1][2][0]{'output_channels'} = $mft[2];
1184  
1185 # set signature
1186 $self->[1][2][0]{'signature'} = 'mft1';
1187  
1188 # for each output curve
1189 for my $i (0 .. $mft[2] - 1) {
1190
1191 # read curve values
1192 read($fh, $buf, 256);
1193
1194 # make 'curv' object
1195 $output->[$i] = ICC::Profile::curv->new([map {$_/255} unpack('C*', $buf)]);
1196
1197 }
1198  
1199 # make 'cvst' object for output curves
1200 $self->[1][3] = ICC::Profile::cvst->new($output);
1201  
1202 # set signature
1203 $self->[1][3][0]{'signature'} = 'mft1';
1204  
1205 # set transform mask (enabling matrix if input colorspace is XYZ)
1206 $self->[2] = $self->[0]{'input_cs'} eq 'XYZ ' ? 0x0F : 0x0E;
1207  
1208 }
1209  
1210 # write mft1 tag to ICC profile
1211 # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
1212 sub _writeICCmft1 {
1213  
1214 # get parameters
1215 my ($self, $parent, $fh, $tag) = @_;
1216  
1217 # local variables
1218 my (@mft, $offset, $mat, $bytes, $size);
1219 my (@mat);
1220  
1221 # set tag type
1222 $mft[0] = 'mft1';
1223  
1224 # check object structure
1225 @mft[1, 2, 3] = _check($self);
1226  
1227 # if 'matf' object is defined
1228 if (defined($self->[1][0])) {
1229
1230 # get matrix
1231 $mat = $self->[1][0]->matrix();
1232
1233 # copy matrix values
1234 @mft[4 .. 12] = ICC::Shared::v2s15f16(@{$mat->[0]}, @{$mat->[1]}, @{$mat->[2]});
1235
1236 } else {
1237
1238 # copy identity matrix
1239 @mft[4 .. 12] = (65536, 0, 0, 0, 65536, 0, 0, 0, 65536);
1240
1241 }
1242  
1243 # seek start of tag
1244 seek($fh, $tag->[1], 0);
1245  
1246 # write mft tag header
1247 print $fh pack('a4 x4 C3 x N9', @mft);
1248  
1249 # for each input channel
1250 for my $i (0 .. $mft[1] - 1) {
1251
1252 # write table values
1253 print $fh pack('C*', map {$_ < 0 ? 0 : ($_ > 1 ? 255 : $_ * 255 + 0.5)} @{$self->[1][1]->array->[$i]->array});
1254
1255 }
1256  
1257 # write clut
1258 $self->[1][2]->_write_clut($fh, $self->[1][2]->gsa(), 1);
1259  
1260 # for each output channel
1261 for my $i (0 .. $mft[2] - 1) {
1262
1263 # write table values
1264 print $fh pack('C*', map {$_ < 0 ? 0 : ($_ > 1 ? 255 : $_ * 255 + 0.5)} @{$self->[1][3]->array->[$i]->array});
1265
1266 }
1267
1268 }
1269  
1270 1;