lib/Image/Animated/JPEG.pm

filter: Statementstmt 89.6% 181 / 202 Branchbran 69.8% 141 / 202 Conditioncond 29.1% 28 / 96 MC/DCmcdc 0.0% 0 / 63 Subroutinesub 81.8% 9 / 11 Podpod 50.0% 4 / 8 totaltotal 62.3% 363 / 582 SCAR 64.7
CC
137
Cov
70%
process
168.6
encode_ajpeg_data
117.9
decode_ajpeg_data
57.7
CRAP 643.8
« blib/lib/Image/Animated/JPEG.pm ↑ Summary
1 package Image::Animated::JPEG;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = '0.02';
9  
10 sub index {
11 4 ▶ my $io_file = shift;
Subroutine index: called CC=9 SCAR=22.9
Pod documented
12 4 my $args = shift;
13  
14 4 my @frames;
15  
16 4 ▶ print "Parsing file...\n" if $args && $args->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $args and $args->{"debug"} 0 4 4
Condition33%1/3
$args and $args->{"debug"}
!ll&&!rl&&r
400
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table33%1/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
17  
18 # check
19 4 my $soi = my_read($io_file, 2);
20 4 ▶ unless ($soi eq "\xFF\xD8") {
Branch: 1 branch
BranchTrueFalseTotal
unless ($soi eq "\377\330") 1 3 4
21 1 die "Does not look like a JPEG file: SOI missing at start of file";
22 }
23  
24 # first frame
25 3 my $cnt = 1;
26 3 ▶ print " frame $cnt begins at 0\n" if $args && $args->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $args and $args->{"debug"} 0 3 3
Condition33%1/3
$args and $args->{"debug"}
!ll&&!rl&&r
300
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table33%1/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
27 3 push(@frames, {
28 offset => 0,
29 io_file => $io_file, # hand over, per frame (might be multiple files)
30 });
31  
32 # subsequent frames
33 3 local $/ = "\xFF\xD9\xFF\xD8"; # we look for EOI+SOI marker to avoid false-positives with embedded thumbs
34 3 ▶ while(my $chunk = <$io_file>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $chunk = readline $io_file)) 5 3 8
35 5 my $pos = tell($io_file) - 2; # -2: compensate for inclusion of begin-marker
36 5 ▶ print " frame $cnt ends at $pos\n" if $args && $args->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $args and $args->{"debug"} 0 5 5
Condition33%1/3
$args and $args->{"debug"}
!ll&&!rl&&r
500
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table33%1/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
37 5 push(@frames, {
38 offset => $pos,
39 io_file => $io_file, # a quirk for playajpeg: hand over the fh, per frame, as playajpeg may play a sequence of jpegs as animation
40 });
41 5 $frames[$cnt - 1]->{length} = $pos - $frames[$cnt - 1]->{offset};
42 5 $cnt++;
43 }
44 3 seek($io_file, 0,0); # rewind fh
45 3 pop(@frames); # last boundary is not beginning of a new frame
46 3 $frames[-1]->{length} += 2; # last frame won't end with EOI+SOI
47  
48 3 return \@frames;
49 }
50  
51 sub process { # mostly from Image::Info
52 5 ▶ my $args = $_[1];
Subroutine process: called CC=36 SCAR=51.3
Pod documented
53  
54 5 my $fh;
55 5 ▶ if(ref($_[0]) eq 'SCALAR'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'SCALAR') { } 2 3 5
56 2 require IO::String;
57 2 ▶ $fh = IO::String->new($_[0]) or die "Error using scalar-ref for reading: $!";
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::String"->new($_[0]) or die "Error using scalar-ref for reading: $!" 0 2 2
58 }else{
59 3 ▶ open($fh, "<", $_[0]) or die "Error opening file for reading: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $_[0] or die "Error opening file for reading: $!" 1 2 3
60 2 binmode($fh);
61 }
62  
63 4 ▶ my $soi = my_read($fh, 2, ($args && $args->{debug} ? 1 : undef));
Branch: 1 branch
BranchTrueFalseTotal
$args && $args->{'debug'} ? : 0 4 4
Condition33%1/3
$args and $args->{"debug"}
!ll&&!rl&&r
400
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table33%1/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
64 4 ▶ unless ($soi eq "\xFF\xD8") {
Branch: 1 branch
BranchTrueFalseTotal
unless ($soi eq "\377\330") 1 3 4
65 1 my $offset = tell($fh) - 2;
66 1 die "Does not look like a JPEG file: SOI missing at offset $offset";
67 }
68  
69 3 my %markers;
70 my @warnings;
71 3 while (1) {
72 9 ▶ my($ff, $mark) = unpack("CC", my_read($fh, 2, ($args && $args->{debug} ? 1 : undef)));
Branch: 1 branch
BranchTrueFalseTotal
$args && $args->{'debug'} ? : 0 9 9
Condition33%1/3
$args and $args->{"debug"}
!ll&&!rl&&r
900
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table33%1/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
73 9 ▶ last if !defined $ff;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ff 0 9 9
74  
75 9 ▶ if ($ff != 0xFF) { # enter when processing a chunk
Branch: 1 branch
BranchTrueFalseTotal
if ($ff != 255) 0 9 9
76 0 my $corrupt_bytes = 2;
77 0 while(1) {
78 0 ▶ my($ff) = unpack("C", my_read($fh,1, ($args && $args->{debug} ? 1 : undef)));
Branch: 1 branch
BranchTrueFalseTotal
$args && $args->{'debug'} ? : 0 0 0
Condition0%0/3
$args and $args->{"debug"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table0%0/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
79 0 ▶ return if !defined $ff;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ff 0 0 0
80 0 ▶ last if $ff == 0xFF;
Branch: 1 branch
BranchTrueFalseTotal
if $ff == 255 0 0 0
81 0 $corrupt_bytes++;
82 }
83 0 ▶ $mark = unpack("C", my_read($fh,1, ($args && $args->{debug} ? 1 : undef)));
Branch: 1 branch
BranchTrueFalseTotal
$args && $args->{'debug'} ? : 0 0 0
Condition0%0/3
$args and $args->{"debug"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table0%0/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
84 0 push(@warnings, sprintf("Corrupt JPEG data, $corrupt_bytes extraneous bytes before marker 0x%02x", $mark));
85 }
86 9 ▶ if ($mark == 0xFF) { # munge FFs (JPEG markers can be padded with unlimited 0xFF's)
Branch: 1 branch
BranchTrueFalseTotal
if ($mark == 255) 0 9 9
87 0 for (;;) {
88 0 ▶ ($mark) = unpack("C", my_read($fh, 1, ($args && $args->{debug} ? 1 : undef)));
Branch: 1 branch
BranchTrueFalseTotal
$args && $args->{'debug'} ? : 0 0 0
Condition0%0/3
$args and $args->{"debug"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table0%0/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
89 0 ▶ last if $mark != 0xFF;
Branch: 1 branch
BranchTrueFalseTotal
if $mark != 255 0 0 0
90 }
91 }
92  
93 9 ▶ last if $mark == 0xDA || $mark == 0xD9; # exit once we reach a SOS marker, or EOI (end of image)
Branch: 1 branch
BranchTrueFalseTotal
if $mark == 218 or $mark == 217 0 9 9
Condition33%1/3
$mark == 218 or $mark == 217
l!l&&r!l&&!r
009
MC/DC0%0/2
$mark == 218 or $mark == 217
$mark == 218 $mark == 217
Truth table33%1/3
A or B
A= $mark == 218B= $mark == 217
ABresult
1X1
011
000
94  
95 9 ▶ print "marker: FF ".sprintf("%#x",$mark)." \n" if $args && $args->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $args and $args->{"debug"} 0 9 9
Condition33%1/3
$args and $args->{"debug"}
!ll&&!rl&&r
900
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table33%1/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
96 9 my $marker_pos = tell($fh) - 2;
97 9 ▶ my($len) = unpack("n", my_read($fh, 2, ($args && $args->{debug} ? 1 : undef))); # we found a marker, read its size
Branch: 1 branch
BranchTrueFalseTotal
$args && $args->{'debug'} ? : 0 9 9
Condition33%1/3
$args and $args->{"debug"}
!ll&&!rl&&r
900
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table33%1/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
98 9 ▶ last if $len < 2; # data-less marker
Branch: 1 branch
BranchTrueFalseTotal
if $len < 2 0 9 9
99  
100 9 ▶ last if $mark < 0xE0; # data-less marker
Branch: 1 branch
BranchTrueFalseTotal
if $mark < 224 3 6 9
101  
102 # process_chunk($info, $img_no, $mark, my_read($fh, $len - 2));
103 6 ▶ if($mark == 0xE0){
Branch: 1 branch
BranchTrueFalseTotal
if ($mark == 224) { } 5 1 6
104 5 ▶ my $data = my_read($fh, $len - 2, ($args && $args->{debug} ? 1 : undef));
Branch: 1 branch
BranchTrueFalseTotal
$args && $args->{'debug'} ? : 0 5 5
Condition33%1/3
$args and $args->{"debug"}
!ll&&!rl&&r
500
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table33%1/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
105 5 ▶ print "APP0 at ". $marker_pos ." len:$len \n" if $args && $args->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $args and $args->{"debug"} 0 5 5
Condition33%1/3
$args and $args->{"debug"}
!ll&&!rl&&r
500
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table33%1/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
106  
107 # get_name($fh);
108 5 my $name;
109 5 my $rel_offset = 0;
110 5 for(0..10){ # app-identifiers may be arbitrarily long, but let's stop after 10 bytes
111 27 my ($value) = unpack("C", read_bytes(\$data, \$rel_offset, 1));
112 27 ▶ last if $value == 0x00;
Branch: 1 branch
BranchTrueFalseTotal
if $value == 0 5 22 27
113 22 $name .= chr($value);
114 }
115  
116 5 $markers{$name} = {
117 type => 'APP0',
118 offset => $marker_pos,
119 length => $len,
120 data_offset => ($marker_pos + 4) + (length($name) + 1),
121 data_length => ($len - 2) - (length($name) + 1)
122 };
123 }else{
124 1 seek($fh,$len - 2,1);
125 }
126  
127 }
128  
129 # print Dumper(\%markers,\@warnings);
130 3 return \%markers;
131 }
132  
133 sub my_read { # from Image::Info
134 31 ▶ my($fh, $len) = @_;
Subroutine my_read: called CC=4 SCAR=14.3
Pod undocumented
135 31 ▶ print " my_read: len:$len \n" if $_[2];
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] 0 31 31
136 31 my $buf;
137 31 my $n = read($fh, $buf, $len);
138 31 ▶ die "read failed: $!" unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 31 31
139 31 ▶ die "short read ($len/$n) at pos " . tell($fh) unless $n == $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $n == $len 0 31 31
140 31 $buf;
141 }
142  
143  
144 sub encode_ajpeg_marker {
145 0 ▶ return "AJPEG\000" . encode_ajpeg_data(@_);
Subroutine encode_ajpeg_marker: not called CC=1 SCAR=6.9
Pod undocumented
146 }
147  
148 sub decode_ajpeg_marker {
149 0 ▶ return decode_ajpeg_data( substr($_[0],6) );
Subroutine decode_ajpeg_marker: not called CC=1 SCAR=6.9
Pod undocumented
150 }
151  
152 # +------------- segment --------------+
153 # | APP0-marker |
154 # | +--"marker" / data ----+
155 # | | AJPEG-marker |
156 # | | |
157 # <marker><length><identifier>\x00<data>
158 # FF E0 00 00 AJPEG 00 ...
159  
160 ## expects a hashref
161 ## encodes hash keys according to the AJPEG schema
162 sub encode_ajpeg_data {
163 4 ▶ my $ref = shift;
Subroutine encode_ajpeg_data: called CC=51 SCAR=47.7
Pod documented
164 4 my $args = shift; # debug, future: version
165  
166 4 ▶ die "encode_ajpeg_data expects a hash-ref" unless $ref && ref($ref) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless $ref and ref $ref eq "HASH" 0 4 4
Condition33%1/3
$ref and ref $ref eq "HASH"
!ll&&!rl&&r
004
MC/DC0%0/2
$ref and ref $ref eq "HASH"
$ref ref $ref eq "HASH"
Truth table33%1/3
A and B
A= $refB= ref $ref eq "HASH"
ABresult
0X0
100
111
167  
168 4 my $binary;
169  
170 4 $binary = pack("C1",0); # version:0
171  
172 4 for my $key (keys %$ref){
173 37 ▶ unless(defined($ref->{$key})){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ref->{$key}) 0 37 37
174 0 ▶ warn "encode_ajpeg_data: $key value is undef and will be ignored!" if $args && $args->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $args and $args->{"debug"} 0 0 0
Condition0%0/3
$args and $args->{"debug"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table0%0/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
175 0 next;
176 }
177 37 ▶ if($key eq 'version'){
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "version") 4 33 37
178 4 ▶ warn "encode_ajpeg_data: Only format version 0 is currenty implemented!" if $args && $args->{debug} && $ref->{$key} != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $args and $args->{"debug"} and $ref->{$key} != 0 0 4 4
Condition33%2/6
$args and $args->{"debug"} and $ref->{$key} != 0
!ll&&!rl&&r
400
$args and $args->{"debug"}
!ll&&!rl&&r
040
MC/DC0%0/3
$args and $args->{"debug"} and $ref->{$key} != 0
$args $args->{"debug"} $ref->{$key} != 0
Truth table25%1/4
A and B and C
A= $argsB= $args->{"debug"}C= $ref->{$key} != 0
ABCresult
0XX0
10X0
1100
1111
179 4 next;
180 }
181 33 ▶ if($key eq 'delay'){
Branch: 9 branches
BranchTrueFalseTotal
if ($key eq 'delay') { } 4 29 33
elsif ($key eq 'repeat') { } 4 25 29
elsif ($key eq 'parse_next') { } 4 21 25
elsif ($key eq 'length') { } 4 17 21
elsif ($key eq 'previous') { } 4 13 17
elsif ($key eq 'x_offset') { } 4 9 13
elsif ($key eq 'y_offset') { } 4 5 9
elsif ($key eq 'dispose_op') { } 4 1 5
elsif ($key eq 'metadata') { } 1 0 1
182 4 ▶ if($ref->{$key} <= 255){
Branch: 2 branches
BranchTrueFalseTotal
if ($ref->{$key} <= 255) { } 2 2 4
elsif ($ref->{$key} <= 65535) { } 1 1 2
183 2 ▶ print "encode_ajpeg_data: delay $ref->{$key} (byte)\n" if $args && $args->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $args and $args->{"debug"} 0 2 2
Condition33%1/3
$args and $args->{"debug"}
!ll&&!rl&&r
020
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table33%1/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
184 2 $binary .= "\x01" . pack("C",$ref->{$key});
185 }elsif($ref->{$key} <= 65535){
186 1 ▶ print "encode_ajpeg_data: delay $ref->{$key} (short)\n" if $args && $args->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $args and $args->{"debug"} 0 1 1
Condition33%1/3
$args and $args->{"debug"}
!ll&&!rl&&r
010
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table33%1/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
187 1 $binary .= "\x02" . pack("n",$ref->{$key});
188 }else{
189 1 ▶ print "encode_ajpeg_data: delay $ref->{$key} (long)\n" if $args && $args->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $args and $args->{"debug"} 0 1 1
Condition33%1/3
$args and $args->{"debug"}
!ll&&!rl&&r
010
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table33%1/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
190 1 $binary .= "\x04" . pack("N",$ref->{$key});
191 }
192 }elsif($key eq 'repeat'){
193 4 ▶ if($ref->{$key} <= 255){
Branch: 2 branches
BranchTrueFalseTotal
if ($ref->{$key} <= 255) { } 2 2 4
elsif ($ref->{$key} <= 65535) { } 2 0 2
194 2 ▶ print "encode_ajpeg_data: repeat $ref->{$key} (byte)\n" if $args && $args->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $args and $args->{"debug"} 0 2 2
Condition33%1/3
$args and $args->{"debug"}
!ll&&!rl&&r
020
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table33%1/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
195 2 $binary .= "\x11" . pack("C",$ref->{$key});
196 }elsif($ref->{$key} <= 65535){
197 2 ▶ print "encode_ajpeg_data: repeat $ref->{$key} (short)\n" if $args && $args->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $args and $args->{"debug"} 0 2 2
Condition33%1/3
$args and $args->{"debug"}
!ll&&!rl&&r
020
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table33%1/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
198 2 $binary .= "\x12" . pack("n",$ref->{$key});
199 }else{
200 0 die "repeat values must be <= 65535";
201 }
202 }elsif($key eq 'parse_next'){
203 4 ▶ if($ref->{$key} <= 255){
Branch: 2 branches
BranchTrueFalseTotal
if ($ref->{$key} <= 255) { } 2 2 4
elsif ($ref->{$key} <= 65535) { } 2 0 2
204 2 $binary .= "\x21" . pack("C",$ref->{$key});
205 }elsif($ref->{$key} <= 65535){
206 2 $binary .= "\x22" . pack("n",$ref->{$key});
207 }else{
208 0 die "parse_next values must be <= 65535";
209 }
210 }elsif($key eq 'length'){
211 4 ▶ if($ref->{$key} <= 255){
Branch: 2 branches
BranchTrueFalseTotal
if ($ref->{$key} <= 255) { } 1 3 4
elsif ($ref->{$key} <= 65535) { } 2 1 3
212 1 $binary .= "\x31" . pack("C",$ref->{$key});
213 }elsif($ref->{$key} <= 65535){
214 2 $binary .= "\x32" . pack("n",$ref->{$key});
215 }else{
216 1 $binary .= "\x34" . pack("N",$ref->{$key});
217 }
218 }elsif($key eq 'previous'){
219 4 ▶ if($ref->{$key} <= 255){
Branch: 2 branches
BranchTrueFalseTotal
if ($ref->{$key} <= 255) { } 1 3 4
elsif ($ref->{$key} <= 65535) { } 2 1 3
220 1 $binary .= "\x41" . pack("C",$ref->{$key});
221 }elsif($ref->{$key} <= 65535){
222 2 $binary .= "\x42" . pack("n",$ref->{$key});
223 }else{
224 1 $binary .= "\x44" . pack("N",$ref->{$key});
225 }
226 }elsif($key eq 'x_offset'){
227 4 ▶ if($ref->{$key} <= 255){
Branch: 2 branches
BranchTrueFalseTotal
if ($ref->{$key} <= 255) { } 2 2 4
elsif ($ref->{$key} <= 65535) { } 2 0 2
228 2 $binary .= "\x51" . pack("C",$ref->{$key});
229 }elsif($ref->{$key} <= 65535){
230 2 $binary .= "\x52" . pack("n",$ref->{$key});
231 }else{
232 0 die "x_offset values must be <= 65535";
233 }
234 }elsif($key eq 'y_offset'){
235 4 ▶ if($ref->{$key} <= 255){
Branch: 2 branches
BranchTrueFalseTotal
if ($ref->{$key} <= 255) { } 2 2 4
elsif ($ref->{$key} <= 65535) { } 2 0 2
236 2 $binary .= "\x61" . pack("C",$ref->{$key});
237 }elsif($ref->{$key} <= 65535){
238 2 $binary .= "\x62" . pack("n",$ref->{$key});
239 }else{
240 0 die "x_offset values must be <= 65535";
241 }
242 }elsif($key eq 'dispose_op'){
243 4 ▶ if($ref->{$key} <= 2){
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{$key} <= 2) { } 4 0 4
244 4 $binary .= "\x71" . pack("C",$ref->{$key});
245 }else{
246 0 die "dispose_op values must be <= 2";
247 }
248 }elsif($key eq 'metadata'){
249 # skip, for later
250 }else{
251 0 warn "encode_ajpeg_data: '$key' is not recognized";
252 }
253 }
254  
255 # specs do not require it, but it may make sense to sort
256 # "potentially longer" byte segments, like metadata, to the end
257 # of the AJPEG segmemt
258 4 ▶ if($ref->{'metadata'}){ # A0
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{"metadata"}) 1 3 4
259 1 ▶ print "encode_ajpeg_data: metadata \n" if $args && $args->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $args and $args->{"debug"} 0 1 1
Condition33%1/3
$args and $args->{"debug"}
!ll&&!rl&&r
010
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table33%1/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
260 1 for my $mkey (keys %{ $ref->{'metadata'} }){
261 4 ▶ print " metadata: ". $mkey .":". $ref->{'metadata'}->{$mkey} ."\n" if $args && $args->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $args and $args->{"debug"} 0 4 4
Condition33%1/3
$args and $args->{"debug"}
!ll&&!rl&&r
040
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table33%1/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
262 4 my $mkey_utf8 = encode('utf-8',$mkey);
263 4 my $mvalue_utf8 = encode('utf-8',$ref->{'metadata'}->{$mkey});
264  
265 4 $binary .= pack("C",160) . pack("C",length($mkey_utf8)) . $mkey_utf8 . pack("C",length($mvalue_utf8)) . $mvalue_utf8;
266 }
267 }
268  
269 4 return $binary;
270 }
271  
272  
273 sub read_bytes {
274 118 ▶ my $binary_ref = shift;
Subroutine read_bytes: called CC=1 SCAR=0.0
Pod undocumented
275 118 my $offset_ref = shift; # quirk: offset as ref, so a read_bytes(), like core::read, advances a pointer/offset
276 118 my $length = shift;
277  
278 # print " read $length bytes at ${$offset_ref} \n";
279 118 my $data = substr(${$binary_ref}, ${$offset_ref}, $length);
280 118 ${$offset_ref} += $length;
281  
282 118 return $data;
283 }
284  
285 ## expects a scalar holding binary data
286 # decodes AJPEG schema binary encoded keys and values
287 sub decode_ajpeg_data {
288 5 ▶ my $binary = shift;
Subroutine decode_ajpeg_data: called CC=41 SCAR=40.5
Pod documented
289 5 my $args = shift; # debug
290  
291 5 ▶ die "decode_ajpeg_data expects a scalar with binary data" unless defined($binary);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $binary 0 5 5
292  
293 5 my $length = length($binary);
294 5 my $offset = 0;
295  
296 5 my %ref;
297  
298 5 ▶ print "decode_ajpeg_data: length:$length \n" if $args && $args->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $args and $args->{"debug"} 0 5 5
Condition33%1/3
$args and $args->{"debug"}
!ll&&!rl&&r
050
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table33%1/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
299 5 $ref{version} = unpack("C", read_bytes(\$binary,\$offset,1));
300 5 ▶ print "decode_ajpeg_data: version:$ref{version} \n" if $args && $args->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $args and $args->{"debug"} 0 5 5
Condition33%1/3
$args and $args->{"debug"}
!ll&&!rl&&r
050
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table33%1/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
301  
302 5 my $cnt;
303 5 for(;;){
304 37 $cnt++;
305  
306 37 my $byte = read_bytes(\$binary,\$offset, 1);
307 37 ▶ if($byte){ # an "empty AJPEG marker" will only hold version, read_bytes beyond that will return undef (thus we skip $key_num/properties decoding)
Branch: 1 branch
BranchTrueFalseTotal
if ($byte) 37 0 37
308 37 my $key_num = unpack("C", $byte);
309 37 ▶ if($key_num == 1){
Branch: 19 branches
BranchTrueFalseTotal
if ($key_num == 1) { } 3 34 37
elsif ($key_num == 2) { } 1 33 34
elsif ($key_num == 4) { } 1 32 33
elsif ($key_num == 17) { } 2 30 32
elsif ($key_num == 18) { } 2 28 30
elsif ($key_num == 33) { } 2 26 28
elsif ($key_num == 34) { } 2 24 26
elsif ($key_num == 49) { } 1 23 24
elsif ($key_num == 50) { } 2 21 23
elsif ($key_num == 52) { } 1 20 21
elsif ($key_num == 65) { } 1 19 20
elsif ($key_num == 66) { } 2 17 19
elsif ($key_num == 68) { } 1 16 17
elsif ($key_num == 81) { } 2 14 16
elsif ($key_num == 82) { } 2 12 14
elsif ($key_num == 97) { } 2 10 12
elsif ($key_num == 98) { } 2 8 10
elsif ($key_num == 113) { } 4 4 8
elsif ($key_num == 160) { } 4 0 4
310 3 $ref{'delay'} = unpack("C", read_bytes(\$binary,\$offset,1));
311 3 ▶ print "decode_ajpeg_data: delay: $ref{'delay'} (byte) \n" if $args && $args->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $args and $args->{"debug"} 0 3 3
Condition33%1/3
$args and $args->{"debug"}
!ll&&!rl&&r
030
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table33%1/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
312 }elsif($key_num == 2){
313 1 $ref{'delay'} = unpack("n", read_bytes(\$binary,\$offset,2));
314 1 ▶ print "decode_ajpeg_data: delay: $ref{'delay'} (short) \n" if $args && $args->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $args and $args->{"debug"} 0 1 1
Condition33%1/3
$args and $args->{"debug"}
!ll&&!rl&&r
010
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table33%1/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
315 }elsif($key_num == 4){
316 1 $ref{'delay'} = unpack("N1", read_bytes(\$binary,\$offset,4));
317 1 ▶ print "decode_ajpeg_data: delay: $ref{'delay'} (long) \n" if $args && $args->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $args and $args->{"debug"} 0 1 1
Condition33%1/3
$args and $args->{"debug"}
!ll&&!rl&&r
010
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table33%1/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
318 }elsif($key_num == 17){
319 2 $ref{'repeat'} = unpack("C", read_bytes(\$binary,\$offset,1));
320 2 ▶ print "decode_ajpeg_data: repeat: $ref{'repeat'} (byte) \n" if $args && $args->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $args and $args->{"debug"} 0 2 2
Condition33%1/3
$args and $args->{"debug"}
!ll&&!rl&&r
020
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table33%1/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
321 }elsif($key_num == 18){
322 2 $ref{'repeat'} = unpack("n", read_bytes(\$binary,\$offset,2));
323 2 ▶ print "decode_ajpeg_data: repeat: $ref{'repeat'} (short) \n" if $args && $args->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $args and $args->{"debug"} 0 2 2
Condition33%1/3
$args and $args->{"debug"}
!ll&&!rl&&r
020
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table33%1/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
324 }elsif($key_num == 33){
325 2 $ref{'parse_next'} = unpack("C", read_bytes(\$binary,\$offset,1));
326 }elsif($key_num == 34){
327 2 $ref{'parse_next'} = unpack("n", read_bytes(\$binary,\$offset,2));
328 }elsif($key_num == 49){
329 1 $ref{'length'} = unpack("C", read_bytes(\$binary,\$offset,1));
330 }elsif($key_num == 50){
331 2 $ref{'length'} = unpack("n", read_bytes(\$binary,\$offset,2));
332 }elsif($key_num == 52){
333 1 $ref{'length'} = unpack("N", read_bytes(\$binary,\$offset,4));
334 }elsif($key_num == 65){
335 1 $ref{'previous'} = unpack("C", read_bytes(\$binary,\$offset,1));
336 }elsif($key_num == 66){
337 2 $ref{'previous'} = unpack("n", read_bytes(\$binary,\$offset,2));
338 }elsif($key_num == 68){
339 1 $ref{'previous'} = unpack("N", read_bytes(\$binary,\$offset,4));
340 }elsif($key_num == 81){
341 2 $ref{'x_offset'} = unpack("C", read_bytes(\$binary,\$offset,1));
342 }elsif($key_num == 82){
343 2 $ref{'x_offset'} = unpack("n", read_bytes(\$binary,\$offset,2));
344 }elsif($key_num == 97){
345 2 $ref{'y_offset'} = unpack("C", read_bytes(\$binary,\$offset,1));
346 }elsif($key_num == 98){
347 2 $ref{'y_offset'} = unpack("n", read_bytes(\$binary,\$offset,2));
348 }elsif($key_num == 113){
349 4 $ref{'dispose_op'} = unpack("C", read_bytes(\$binary,\$offset,1));
350 }elsif($key_num == 160){ # 0xA0
351 4 my $mkey_utf8 = read_bytes(\$binary,\$offset, unpack('C',read_bytes(\$binary,\$offset,1)) );
352 4 my $mvalue_utf8 = read_bytes(\$binary,\$offset, unpack('C',read_bytes(\$binary,\$offset,1)) );
353  
354 4 ▶ my $mkey = decode('utf-8', $mkey_utf8) if defined($mkey_utf8);
Branch: 1 branch
BranchTrueFalseTotal
if defined $mkey_utf8 4 0 4
355 4 ▶ my $mvalue = decode('utf-8', $mvalue_utf8) if defined($mvalue_utf8);
Branch: 1 branch
BranchTrueFalseTotal
if defined $mvalue_utf8 4 0 4
356 4 ▶ print "decode_ajpeg_data: metadata: ". $mkey .":". $mvalue ." \n" if $args && $args->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $args and $args->{"debug"} 0 4 4
Condition33%1/3
$args and $args->{"debug"}
!ll&&!rl&&r
040
MC/DC0%0/2
$args and $args->{"debug"}
$args $args->{"debug"}
Truth table33%1/3
A and B
A= $argsB= $args->{"debug"}
ABresult
0X0
100
111
357 4 $ref{'metadata'}->{$mkey} = $mvalue;
358 }
359 }
360 37 ▶ last if $offset >= $length;
Branch: 1 branch
BranchTrueFalseTotal
if $offset >= $length 5 32 37
361 }
362  
363 5 return \%ref;
364 }
365  
366 3 1;
367  
368 __END__
« blib/lib/Image/Animated/JPEG.pm ↑ Summary