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

filter: Statementstmt 0.0% 0 / 293 Branchbran 0.0% 0 / 98 Conditioncond 0.0% 0 / 73 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 24 totaltotal 0.0% 0 / 517 CC 50 SCAR 33.1
CC
50
Cov
0%
sdump
90.0
transform
56.0
jacobian
30.0
CRAP 27.5
1 package ICC::Profile::mpet;
2  
3 use strict;
4 use Carp;
5  
6 our $VERSION = 0.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 mpet object
22 # array contains processing element objects
23 # objects must have '_transform' and 'jacobian' methods
24 # parameters: ([array_ref])
25 # returns: (ref_to_object)
26 sub new {
27  
28 # get object class
29 my $class = shift();
30  
31 # create empty mpet object
32 my $self = [
33 {}, # object header
34 [], # processing elements
35 0x00 # transform mask
36 ];
37  
38 # if there are parameters
39 if (@_) {
40
41 # if one parameter, an array reference
42 if (@_ == 1 && ref($_[0]) eq 'ARRAY') {
43
44 # make new mpet tag
45 _new_from_array($self, @_);
46
47 } else {
48
49 # error
50 croak('parameter must be an array reference');
51
52 }
53
54 }
55  
56 # bless object
57 bless($self, $class);
58  
59 # return object reference
60 return($self);
61  
62 }
63  
64 # get/set reference to header hash
65 # parameters: ([ref_to_new_hash])
66 # returns: (ref_to_hash)
67 sub header {
68  
69 # get object reference
70 my $self = shift();
71  
72 # if there are parameters
73 if (@_) {
74
75 # if one parameter, a hash reference
76 if (@_ == 1 && ref($_[0]) eq 'HASH') {
77
78 # set header to new hash
79 $self->[0] = shift();
80
81 } else {
82
83 # error
84 croak('parameter must be a hash reference');
85
86 }
87
88 }
89  
90 # return reference
91 return($self->[0]);
92  
93 }
94  
95 # get/set processing element array reference
96 # parameters: ([ref_to_array])
97 # returns: (ref_to_array)
98 sub array {
99  
100 # get object reference
101 my $self = shift();
102  
103 # if parameter
104 if (@_) {
105
106 # verify array reference
107 (ref($_[0]) eq 'ARRAY') || croak('not an array reference');
108
109 # for each processing element
110 for my $i (0 .. $#{$_[0]}) {
111
112 # verify object has processing methods
113 ($_[0][$i]->can('_transform') && $_[0][$i]->can('jacobian')) || croak('processing element lacks \'transform\' or \'jacobian\' method');
114
115 # add processing element
116 $self->[1][$i] = $_[0][$i];
117
118 }
119
120 }
121  
122 # return array reference
123 return($self->[1]);
124  
125 }
126  
127 # get/set transform mask
128 # bits ... 3-2-1-0 correpsond to ... PE3-PE2-PE1-PE0
129 # parameters: ([new_mask_value])
130 # returns: (mask_value)
131 sub mask {
132  
133 # get object reference
134 my $self = shift();
135  
136 # if there are parameters
137 if (@_) {
138
139 # if one parameter
140 if (@_ == 1) {
141
142 # set object transform mask value
143 $self->[2] = shift();
144
145 } else {
146
147 # error
148 croak('more than one parameter');
149
150 }
151
152 }
153  
154 # return transform mask value
155 return($self->[2]);
156  
157 }
158  
159 # create mpet tag object from ICC profile
160 # parameters: (ref_to_parent_object, file_handle, ref_to_tag_table_entry)
161 # returns: (ref_to_object)
162 sub new_fh {
163  
164 # get object class
165 my $class = shift();
166  
167 # create empty mpet object
168 my $self = [
169 {}, # object header
170 [], # processing elements
171 0x00 # transform mask
172 ];
173  
174 # verify 3 parameters
175 (@_ == 3) || croak('wrong number of parameters');
176  
177 # read mpet data from profile
178 _readICCmpet($self, @_);
179  
180 # bless object
181 bless($self, $class);
182  
183 # return object reference
184 return($self);
185  
186 }
187  
188 # writes mpet tag object to ICC profile
189 # parameters: (ref_to_parent_object, file_handle, ref_to_tag_table_entry)
190 sub write_fh {
191  
192 # get tag reference
193 my $self = shift();
194  
195 # verify 3 parameters
196 (@_ == 3) || croak('wrong number of parameters');
197  
198 # write mpet data to profile
199 _writeICCmpet($self, @_);
200  
201 }
202  
203 # get tag size (for writing to profile)
204 # returns: (tag_size)
205 sub size {
206  
207 # get parameters
208 my ($self) = @_;
209  
210 # local variables
211 my ($size);
212  
213 # set header size
214 $size = 16 + 8 * @{$self->[1]};
215  
216 # for each processing element
217 for my $pel (@{$self->[1]}) {
218
219 # add size
220 $size += $pel->size();
221
222 # adjust to 4-byte boundary
223 $size += -$size % 4;
224
225 }
226  
227 # return size
228 return($size);
229  
230 }
231  
232 # get number of input channels
233 # returns: (number)
234 sub cin {
235  
236 # get object reference
237 my $self = shift();
238  
239 # return
240 return($self->[1][0]->cin());
241  
242 }
243  
244 # get number of output channels
245 # returns: (number)
246 sub cout {
247  
248 # get object reference
249 my $self = shift();
250  
251 # return
252 return($self->[1][-1]->cout());
253  
254 }
255  
256 # transform data
257 # transform mask enables/disables individual tag elements
258 # clipping mask enables/disables individual tag output clipping
259 # supported input types:
260 # parameters: (list, [hash])
261 # parameters: (vector, [hash])
262 # parameters: (matrix, [hash])
263 # parameters: (Math::Matrix_object, [hash])
264 # parameters: (structure, [hash])
265 # returns: (same_type_as_input)
266 sub transform {
267  
268 # set hash value (0 or 1)
269 my $h = ref($_[-1]) eq 'HASH' ? 1 : 0;
270  
271 # if input a 'Math::Matrix' object
272 if (@_ == $h + 2 && UNIVERSAL::isa($_[1], 'Math::Matrix')) {
273
274 # call matrix transform
275 &_trans2;
276
277 # if input an array reference
278 } elsif (@_ == $h + 2 && ref($_[1]) eq 'ARRAY') {
279
280 # if array contains numbers (vector)
281 if (! ref($_[1][0]) && @{$_[1]} == grep {Scalar::Util::looks_like_number($_)} @{$_[1]}) {
282
283 # call vector transform
284 &_trans1;
285
286 # if array contains vectors (2-D array)
287 } elsif (ref($_[1][0]) eq 'ARRAY' && @{$_[1]} == grep {ref($_) eq 'ARRAY' && Scalar::Util::looks_like_number($_->[0])} @{$_[1]}) {
288
289 # call matrix transform
290 &_trans2;
291
292 } else {
293
294 # call structure transform
295 &_trans3;
296
297 }
298
299 # if input a list (of numbers)
300 } elsif (@_ == $h + 1 + grep {Scalar::Util::looks_like_number($_)} @_) {
301
302 # call list transform
303 &_trans0;
304
305 } else {
306
307 # error
308 croak('invalid transform input');
309
310 }
311  
312 }
313  
314 # inverse transform
315 # note: number of undefined output values must equal number of defined input values
316 # note: the input and output vectors contain the final solution on return
317 # hash key 'init' specifies initial value vector
318 # hash key 'ubox' enables unit box extrapolation
319 # parameters: (input_vector, output_vector, [hash])
320 # returns: (RMS_error_value)
321 sub inverse {
322  
323 # get parameters
324 my ($self, $in, $out, $hash) = @_;
325  
326 # local variables
327 my ($i, $j, @si, @so, $init);
328 my ($int, $jac, $mat, $delta);
329 my ($max, $elim, $dlim, $accum, $error);
330  
331 # initialize indices
332 $i = $j = -1;
333  
334 # build slice arrays while validating input and output arrays
335 ((grep {$i++; defined() && push(@si, $i)} @{$in}) == (grep {$j++; ! defined() && push(@so, $j)} @{$out})) || croak('wrong number of undefined values');
336  
337 # get init array
338 $init = $hash->{'init'};
339  
340 # for each undefined output value
341 for my $i (@so) {
342
343 # set to supplied initial value or 0.5
344 $out->[$i] = defined($init->[$i]) ? $init->[$i] : 0.5;
345
346 }
347  
348 # set maximum loop count
349 $max = $hash->{'inv_max'} || 10;
350  
351 # loop error limit
352 $elim = $hash->{'inv_elim'} || 1E-6;
353  
354 # set delta limit ('mpet' tags use floating point PCS, so L*a*b* values need greater limit)
355 $dlim = $hash->{'inv_dlim'} || ($self->[0]{'input_cs'} eq 'Lab ') ? 50.0 : 0.5;
356  
357 # create empty solution matrix
358 $mat = Math::Matrix->new([]);
359  
360 # compute initial transform values
361 ($jac, $int) = jacobian($self, $out, $hash);
362  
363 # solution loop
364 for (1 .. $max) {
365
366 # for each input
367 for my $i (0 .. $#si) {
368
369 # for each output
370 for my $j (0 .. $#so) {
371
372 # copy Jacobian value to solution matrix
373 $mat->[$i][$j] = $jac->[$si[$i]][$so[$j]];
374
375 }
376
377 # save residual value to solution matrix
378 $mat->[$i][$#si + 1] = $in->[$si[$i]] - $int->[$si[$i]];
379
380 }
381
382 # solve for delta values
383 $delta = $mat->solve;
384
385 # for each output value
386 for my $i (0 .. $#so) {
387
388 # add delta (limited using hyperbolic tangent)
389 $out->[$so[$i]] += POSIX::tanh($delta->[$i][0]/$dlim) * $dlim;
390
391 }
392
393 # compute updated transform values
394 ($jac, $int) = jacobian($self, $out, $hash);
395
396 # initialize error accumulator
397 $accum = 0;
398
399 # for each input
400 for my $i (0 .. $#si) {
401
402 # accumulate delta squared
403 $accum += ($in->[$si[$i]] - $int->[$si[$i]])**2;
404
405 }
406
407 # compute RMS error
408 $error = sqrt($accum/@si);
409
410 # if error less than limit
411 last if ($error < $elim);
412
413 }
414  
415 # update input vector with final values
416 @{$in} = @{$int};
417  
418 # return
419 return($error);
420  
421 }
422  
423 # compute Jacobian matrix
424 # transform mask enables/disables individual tag elements
425 # parameters: (input_vector, [hash])
426 # returns: (Jacobian_matrix, [output_vector])
427 sub jacobian {
428  
429 # get parameters
430 my ($self, $data, $hash) = @_;
431  
432 # local variables
433 my ($jac, $jaci);
434  
435 # for each processing element
436 for my $i (0 .. $#{$self->[1]}) {
437
438 # if processing element defined, and transform mask bit set
439 if (defined($self->[1][$i]) && $self->[2] & 0x01 << $i) {
440
441 # compute Jacobian matrix and transform data
442 ($jaci, $data) = $self->[1][$i]->jacobian($data, $hash);
443
444 # multiply Jacobian matrices
445 $jac = defined($jac) ? $jaci * $jac : $jaci;
446
447 }
448
449 }
450  
451 # if Jacobian matrix is undefined, use identity matrix
452 $jac = Math::Matrix->diagonal((1) x @{$data}) if (! defined($jac));
453  
454 # if output values wanted
455 if (wantarray) {
456
457 # return Jacobian and output values
458 return($jac, $data);
459
460 } else {
461
462 # return Jacobian only
463 return($jac);
464
465 }
466
467 }
468  
469 # get/set PCS encoding
470 # for use with ICC::Support::PCS objects
471 # parameters: ([PCS_encoding])
472 # returns: (PCS_encoding)
473 sub pcs {
474  
475 # get parameters
476 my ($self, $pcs) = @_;
477  
478 # if PCS parameter is supplied
479 if (defined($pcs)) {
480
481 # if a valid PCS encoding
482 if (grep {$pcs == $_} (3, 8)) {
483
484 # copy to tag header hash
485 $self->[0]{'pcs_encoding'} = $pcs;
486
487 # return PCS encoding
488 return($pcs);
489
490 } else {
491
492 # error
493 croak('invalid PCS encoding');
494
495 }
496
497 } else {
498
499 # if PCS is defined in tag header
500 if (defined($self->[0]{'pcs_encoding'})) {
501
502 # return PCS encoding
503 return($self->[0]{'pcs_encoding'});
504
505 } else {
506
507 # error
508 croak('can\'t determine PCS encoding');
509
510 }
511
512 }
513
514 }
515  
516 # get/set white point
517 # parameters: ([white_point])
518 # returns: (white_point)
519 sub wtpt {
520  
521 # get parameters
522 my ($self, $wtpt) = @_;
523  
524 # if white point parameter is supplied
525 if (defined($wtpt)) {
526
527 # if an array of three scalars
528 if (@{$wtpt} == 3 && 3 == grep {! ref()} @{$wtpt}) {
529
530 # copy to tag header hash
531 $self->[0]{'wtpt'} = $wtpt;
532
533 # return white point
534 return($wtpt);
535
536 } else {
537
538 # error
539 croak('invalid white point');
540
541 }
542
543 } else {
544
545 # if white point is defined in tag header
546 if (defined($self->[0]{'wtpt'})) {
547
548 # return return white point
549 return($self->[0]{'wtpt'});
550
551 } else {
552
553 # error
554 croak('can\'t determine white point');
555
556 }
557
558 }
559
560 }
561  
562 # print object contents to string
563 # format is an array structure
564 # parameter: ([format])
565 # returns: (string)
566 sub sdump {
567  
568 # get parameters
569 my ($self, $p) = @_;
570  
571 # local variables
572 my ($element, $fmt, $s, $pt, $st);
573  
574 # resolve parameter to an array reference
575 $p = defined($p) ? ref($p) eq 'ARRAY' ? $p : [$p] : [];
576  
577 # get format string
578 $fmt = defined($p->[0]) && ! ref($p->[0]) ? $p->[0] : 's';
579  
580 # set string to object ID
581 $s = sprintf("'%s' object, (0x%x)\n", ref($self), $self);
582  
583 # if format contains 'o'
584 if ($fmt =~ m/s/) {
585
586 # get default parameter
587 $pt = $p->[-1];
588
589 # for each processing element
590 for my $i (0 .. $#{$self->[1]}) {
591
592 # get element reference
593 $element = $self->[1][$i];
594
595 # if processing element is undefined
596 if (! defined($element)) {
597
598 # append message
599 $s .= "\tprocessing element is undefined\n";
600
601 # if processing element is not a blessed object
602 } elsif (! Scalar::Util::blessed($element)) {
603
604 # append message
605 $s .= "\tprocessing element is not a blessed object\n";
606
607 # if processing element has an 'sdump' method
608 } elsif ($element->can('sdump')) {
609
610 # get 'sdump' string
611 $st = $element->sdump(defined($p->[$i + 1]) ? $p->[$i + 1] : $pt);
612
613 # prepend tabs to each line
614 $st =~ s/^/\t/mg;
615
616 # append 'sdump' string
617 $s .= $st;
618
619 # processing element is object without an 'sdump' method
620 } else {
621
622 # append object info
623 $s .= sprintf("\t'%s' object, (0x%x)\n", ref($element), $element);
624
625 }
626
627 }
628
629 }
630  
631 # return
632 return($s);
633  
634 }
635  
636 # transform list
637 # parameters: (object_reference, list, [hash])
638 # returns: (list)
639 sub _trans0 {
640  
641 # local variables
642 my ($self, $hash, $data);
643  
644 # get object reference
645 $self = shift();
646  
647 # get optional hash
648 $hash = pop() if (ref($_[-1]) eq 'HASH');
649  
650 # process data
651 $data = _trans1($self, [@_], $hash);
652  
653 # return list
654 return(@{$data});
655  
656 }
657  
658 # transform vector
659 # parameters: (object_reference, vector, [hash])
660 # returns: (vector)
661 sub _trans1 {
662  
663 # get parameters
664 my ($self, $data, $hash) = @_;
665  
666 # for each processing element
667 for my $i (0 .. $#{$self->[1]}) {
668
669 # if processing element defined, and transform mask bit set
670 if (defined($self->[1][$i]) && $self->[2] & 0x01 << $i) {
671
672 # transform data
673 $data = $self->[1][$i]->_trans1($data, $hash);
674
675 }
676
677 }
678
679 # return
680 return($data);
681
682 }
683  
684 # transform matrix (2-D array -or- Math::Matrix object)
685 # parameters: (object_reference, matrix, [hash])
686 # returns: (matrix)
687 sub _trans2 {
688  
689 # get parameters
690 my ($self, $data, $hash) = @_;
691  
692 # for each processing element
693 for my $i (0 .. $#{$self->[1]}) {
694
695 # if processing element defined, and transform mask bit set
696 if (defined($self->[1][$i]) && $self->[2] & 0x01 << $i) {
697
698 # transform data
699 $data = $self->[1][$i]->_trans2($data, $hash);
700
701 }
702
703 }
704  
705 # return
706 return($data);
707  
708 }
709  
710 # transform structure
711 # parameters: (object_reference, structure, [hash])
712 # returns: (structure)
713 sub _trans3 {
714  
715 # get parameters
716 my ($self, $in, $hash) = @_;
717  
718 # transform the array structure
719 _crawl($self, $in, my $out = [], $hash);
720  
721 # return
722 return($out);
723  
724 }
725  
726 # recursive transform
727 # array structure is traversed until scalar arrays are found and transformed
728 # parameters: (ref_to_object, input_array_reference, output_array_reference, hash)
729 sub _crawl {
730  
731 # get parameters
732 my ($self, $in, $out, $hash) = @_;
733  
734 # if input is a vector (reference to a scalar array)
735 if (@{$in} == grep {! ref()} @{$in}) {
736
737 # transform input vector and copy to output
738 @{$out} = @{_trans1($self, $in, $hash)};
739
740 } else {
741
742 # for each input element
743 for my $i (0 .. $#{$in}) {
744
745 # if an array reference
746 if (ref($in->[$i]) eq 'ARRAY') {
747
748 # transform next level
749 _crawl($self, $in->[$i], $out->[$i] = [], $hash);
750
751 } else {
752
753 # error
754 croak('invalid transform input');
755
756 }
757
758 }
759
760 }
761
762 }
763  
764 # check object structure
765 # parameter: (ref_to_object)
766 # returns: (number_input_channels, number_output_channels)
767 sub _check {
768  
769 # get object reference
770 my $self = shift();
771  
772 # local variables
773 my ($ci, $co);
774  
775 # for each processing element
776 for my $i (0 .. $#{$self->[1]}) {
777
778 # if element has 'cin' method
779 if ($self->[1][$i]->can('cin')) {
780
781 # if number of input channels is undefined
782 if (! defined($ci)) {
783
784 # set number of input channels
785 $ci = $self->[1][$i]->cin();
786
787 }
788
789 # if number of output channels is defined
790 if (defined($co)) {
791
792 # verify input channels of this element match output channels of previous element
793 ($self->[1][$i]->cin() == $co) || croak('\'mpet\' processing element has wrong number of channels');
794
795 }
796
797 }
798
799 # if element has 'cout' method
800 if ($self->[1][$i]->can('cout')) {
801
802 # set number of output channels
803 $co = $self->[1][$i]->cout();
804
805 }
806
807 }
808  
809 # return
810 return($ci, $co);
811  
812 }
813  
814 # make new mpet tag from array
815 # array contains processing element objects
816 # objects must have '_trans1', '_trans2', and 'jacobian' methods
817 # parameters: (ref_to_object, ref_to_array)
818 sub _new_from_array {
819  
820 # get parameters
821 my ($self, $array) = @_;
822  
823 # for each processing element
824 for my $i (0 .. $#{$array}) {
825
826 # verify object has required processing methods
827 ($array->[$i]->can('_trans1')) || croak('processing element lacks \'_trans1\'method');
828 ($array->[$i]->can('_trans2')) || croak('processing element lacks \'_trans2\'method');
829 ($array->[$i]->can('jacobian')) || croak('processing element lacks\'jacobian\' method');
830
831 # add processing element
832 $self->[1][$i] = $array->[$i];
833
834 }
835  
836 # check object structure
837 _check($self);
838  
839 }
840  
841 # read mpet tag from ICC profile
842 # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
843 sub _readICCmpet {
844  
845 # get parameters
846 my ($self, $parent, $fh, $tag) = @_;
847  
848 # local variables
849 my ($buf, @mft, $table, $tag2, $type, $class, %hash);
850  
851 # set tag signature
852 $self->[0]{'signature'} = $tag->[0];
853  
854 # if 'D2Bx' tag
855 if ($tag->[0] =~ m|^D2B[0-2]$|) {
856
857 # set input colorspace
858 $self->[0]{'input_cs'} = $parent->[1][4];
859
860 # set output colorspace
861 $self->[0]{'output_cs'} = $parent->[1][5];
862
863 # if 'B2Dx' tag
864 } elsif ($tag->[0] =~ m|^B2D[0-2]$|) {
865
866 # set input colorspace
867 $self->[0]{'input_cs'} = $parent->[1][5];
868
869 # set output colorspace
870 $self->[0]{'output_cs'} = $parent->[1][4];
871
872 }
873  
874 # seek start of tag
875 seek($fh, $tag->[1], 0);
876  
877 # read tag header
878 read($fh, $buf, 16);
879  
880 # unpack header
881 @mft = unpack('a4 x4 n2 N', $buf);
882  
883 # verify tag signature
884 ($mft[0] eq 'mpet') or croak('wrong tag type');
885  
886 # for each processing element
887 for my $i (0 .. $mft[3] - 1) {
888
889 # read positionNumber
890 read($fh, $buf, 8);
891
892 # unpack to processing element tag table
893 $table->[$i] = ['mpet', unpack('N2', $buf)];
894
895 }
896  
897 # clear transform mask
898 $self->[2] = 0;
899  
900 # for each processing element
901 for my $i (0 .. $mft[3] - 1) {
902
903 # get tag table entry
904 $tag2 = $table->[$i];
905
906 # make offset absolute
907 $tag2->[1] += $tag->[1];
908
909 # if a duplicate tag
910 if (exists($hash{$tag2->[1]})) {
911
912 # use original tag
913 $self->[1][$i] = $hash{$tag2->[1]};
914
915 } else {
916
917 # seek to start of tag
918 seek($fh, $tag2->[1], 0);
919
920 # read tag type signature
921 read($fh, $type, 4);
922
923 # convert non-word characters to underscores
924 $type =~ s|\W|_|g;
925
926 # form class specifier
927 $class = "ICC::Profile::$type";
928
929 # if 'class->new_fh' method exists
930 if ($class->can('new_fh')) {
931
932 # create specific tag object
933 $self->[1][$i] = $class->new_fh($self, $fh, $tag2);
934
935 } else {
936
937 # create generic tag object
938 $self->[1][$i] = ICC::Profile::Generic->new_fh($self, $fh, $tag2);
939
940 # print warning
941 print "processing element $type opened as generic\n";
942
943 }
944
945 # save tag in hash
946 $hash{$tag2->[1]} = $self->[1][$i];
947
948 }
949
950 # set mask bit
951 $self->[2] |= 0x01 << $i;
952
953 }
954  
955 }
956  
957 # write mpet tag to ICC profile
958 # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
959 sub _writeICCmpet {
960  
961 # get parameters
962 my ($self, $parent, $fh, $tag) = @_;
963  
964 # local variables
965 my ($ci, $co, $n, $offset, $size, @pept, %hash);
966  
967 # check object structure
968 ($ci, $co) = _check($self);
969  
970 # set number of processing elements
971 $n = @{$self->[1]};
972  
973 # seek start of tag
974 seek($fh, $tag->[1], 0);
975  
976 # write 'mpet' header
977 print $fh pack('a4 x4 n2 N', 'mpet', $ci, $co, $n);
978  
979 # set tag offset
980 $offset = 16 + 8 * $n;
981  
982 # for each processing element
983 for my $i (0 .. $#{$self->[1]}) {
984
985 # verify processing element allowed in 'mpet' tag
986 (ref($self->[1][$i]) =~ m/^ICC::Profile::(cvst|matf|clut|Generic)$/) || croak('processing element not allowed in \'mpet\' tag');
987
988 # if tag not in hash
989 if (! exists($hash{$self->[1][$i]})) {
990
991 # get size
992 $size = $self->[1][$i]->size();
993
994 # set table entry and add to hash
995 $pept[$i] = $hash{$self->[1][$i]} = [$offset, $size];
996
997 # update offset
998 $offset += $size;
999
1000 # adjust to 4-byte boundary
1001 $offset += -$offset % 4;
1002
1003 } else {
1004
1005 # set table entry
1006 $pept[$i] = $hash{$self->[1][$i]};
1007
1008 }
1009
1010 # write processing element position entry
1011 print $fh pack('N2', @{$pept[$i]});
1012
1013 }
1014  
1015 # initialize hash
1016 %hash = ();
1017  
1018 # for each processing element
1019 for my $i (0 .. $#{$self->[1]}) {
1020
1021 # if tag not in hash
1022 if (! exists($hash{$self->[1][$i]})) {
1023
1024 # make offset absolute
1025 $pept[$i][0] += $tag->[1];
1026
1027 # write tag
1028 $self->[1][$i]->write_fh($self, $fh, ['mpet', $pept[$i][0], $pept[$i][1]]);
1029
1030 # add key to hash
1031 $hash{$self->[1][$i]}++;
1032
1033 }
1034
1035 }
1036
1037 }
1038  
1039 1;