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