File Coverage

blib/lib/ICC/Profile/data.pm
Criterion Covered Total %
statement 9 57 15.7
branch 0 28 0.0
condition 0 3 0.0
subroutine 3 11 27.2
pod 1 6 16.6
total 13 105 12.3


line stmt bran cond sub pod time code
1             package ICC::Profile::data;
2              
3 2     2   82257 use strict;
  2         10  
  2         48  
4 2     2   10 use Carp;
  2         4  
  2         116  
5              
6             our $VERSION = 0.13;
7              
8             # revised 2019-01-28
9             #
10             # Copyright © 2004-2020 by William B. Birkett
11              
12             # inherit from Shared
13 2     2   371 use parent qw(ICC::Shared);
  2         233  
  2         12  
14              
15             # create new data tag object
16             # parameters: ([data_flag, data_string])
17             # returns: (ref_to_object)
18             sub new {
19              
20             # get object class
21 0     0 0   my $class = shift();
22            
23             # create empty data object
24 0           my $self = [
25             {}, # object header
26             1, # data flag
27             '' # data string
28             ];
29              
30             # if parameter supplied
31 0 0         if (@_) {
32            
33             # save data flag
34 0 0         $self->[1] = (shift() == 0) ? 0 : 1;
35            
36             # save data string
37 0           $self->[2] = shift();
38            
39             }
40              
41             # bless object
42 0           bless($self, $class);
43            
44             # return object reference
45 0           return($self);
46              
47             }
48              
49             # create data tag object from ICC profile
50             # parameters: (ref_to_parent_object, file_handle, ref_to_tag_table_entry)
51             # returns: (ref_to_object)
52             sub new_fh {
53              
54             # get object class
55 0     0 0   my $class = shift();
56              
57             # create empty data object
58 0           my $self = [
59             {}, # object header
60             1, # data flag
61             '' # data string
62             ];
63              
64             # verify 3 parameters
65 0 0         (@_ == 3) or croak('wrong number of parameters');
66              
67             # read data from profile
68 0           _readICCdata($self, @_);
69              
70             # bless object
71 0           bless($self, $class);
72              
73             # return object reference
74 0           return($self);
75              
76             }
77              
78             # writes data tag object to ICC profile
79             # parameters: (ref_to_parent_object, file_handle, ref_to_tag_table_entry)
80             sub write_fh {
81              
82             # verify 4 parameters
83 0 0   0 0   (@_ == 4) or croak('wrong number of parameters');
84              
85             # write data to profile
86 0           goto &_writeICCdata;
87              
88             }
89              
90             # get tag size (for writing to profile)
91             # returns: (tag_size)
92             sub size {
93            
94             # get parameters
95 0     0 0   my ($self) = @_;
96            
97             # return size
98 0 0         return(12 + length($self->[2]) + ($self->[1] == 0 ? 1 : 0));
99            
100             }
101              
102             # get/set data string
103             # parameters: ([data_flag, data_string])
104             # returns: (data_string)
105             sub data {
106              
107             # get object reference
108 0     0 0   my $self = shift();
109            
110             # if parameters supplied
111 0 0         if (@_) {
112            
113             # save data flag
114 0 0         $self->[1] = shift() == 0 ? 0 : 1;
115            
116             # save data string
117 0           $self->[2] = shift();
118            
119             }
120            
121             # return data string
122 0           return($self->[2]);
123              
124             }
125              
126             # print object contents to string
127             # format is an array structure
128             # parameter: ([format])
129             # returns: (string)
130             sub sdump {
131              
132             # get parameters
133 0     0 1   my ($self, $p) = @_;
134              
135             # local variables
136 0           my ($s, $fmt);
137              
138             # resolve parameter to an array reference
139 0 0         $p = defined($p) ? ref($p) eq 'ARRAY' ? $p : [$p] : [];
    0          
140              
141             # get format string
142 0 0 0       $fmt = defined($p->[0]) && ! ref($p->[0]) ? $p->[0] : 'undef';
143              
144             # set string to object ID
145 0           $s = sprintf("'%s' object, (0x%x)\n", ref($self), $self);
146              
147             # return
148 0           return($s);
149              
150             }
151              
152             # read data tag from ICC profile
153             # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
154             sub _readICCdata {
155              
156             # get parameters
157 0     0     my ($self, $parent, $fh, $tag) = @_;
158              
159             # local variables
160 0           my ($buf, $type);
161              
162             # save tag signature
163 0           $self->[0]{'signature'} = $tag->[0];
164              
165             # seek start of tag
166 0           seek($fh, $tag->[1], 0);
167              
168             # read type and data flag
169 0           read($fh, $buf, 12);
170              
171             # unpack data flag
172 0           $self->[1] = $type = unpack('x8 N', $buf);
173              
174             # read remaining data
175 0           read($fh, $buf, $tag->[2] - 12);
176              
177             # if ASCII data
178 0 0         if ($type == 0) {
    0          
179            
180             # unpack ASCII data (zero terminated)
181 0           $self->[2] = unpack('Z*', $buf);
182            
183             # if binary data
184             } elsif ($type == 1) {
185            
186             # unpack binary data
187 0           $self->[2] = unpack('a*', $buf);
188            
189             } else {
190            
191             # print message
192 0           print "unknown data type ($type)\n";
193            
194             # unpack binary data
195 0           $self->[2] = unpack('a*', $buf);
196            
197             }
198            
199             }
200              
201             # write data tag to ICC profile
202             # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
203             sub _writeICCdata {
204              
205             # get parameters
206 0     0     my ($self, $parent, $fh, $tag) = @_;
207              
208             # get data type
209 0           my $type = $self->[1];
210              
211             # seek start of tag
212 0           seek($fh, $tag->[1], 0);
213              
214             # if ASCII data
215 0 0         if ($type == 0) {
    0          
216            
217             # write tag
218 0           print $fh pack('a4 x4 N Z*', 'data', $type, $self->[2]);
219            
220             # if binary data
221             } elsif ($type == 1) {
222            
223             # write tag
224 0           print $fh pack('a4 x4 N a*', 'data', $type, $self->[2]);
225            
226             } else {
227            
228             # print message
229 0           print "unknown data type ($type)\n";
230            
231             # write tag
232 0           print $fh pack('a4 x4 N a*', 'data', $type, $self->[2]);
233            
234             }
235            
236             }
237              
238             1;