File Coverage

blib/lib/ICC/Profile/data.pm
Criterion Covered Total %
statement 12 60 20.0
branch 0 28 0.0
condition 0 3 0.0
subroutine 4 12 33.3
pod 1 6 16.6
total 17 109 15.6


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