| 1 | # -*- perl -*- | ||||||||||||||
| 2 | |||||||||||||||
| 3 | # | ||||||||||||||
| 4 | # Copyright (C) 2019 Preisvergleich Internet Services AG. All rights reserved. | ||||||||||||||
| 5 | # This package is free software; you can redistribute it and/or | ||||||||||||||
| 6 | # modify it under the same terms as Perl itself. | ||||||||||||||
| 7 | # | ||||||||||||||
| 8 | |||||||||||||||
| 9 | # File magic is | ||||||||||||||
| 10 | # R I F F | ||||||||||||||
| 11 | # length (4 bytes) | ||||||||||||||
| 12 | # WEPB | ||||||||||||||
| 13 | |||||||||||||||
| 14 | =begin register | ||||||||||||||
| 15 | |||||||||||||||
| 16 | MAGIC: /^RIFF.{4}WEBP/s | ||||||||||||||
| 17 | |||||||||||||||
| 18 | VP8 (lossy), VP8L (lossless) and VP8X (extended) files are supported. | ||||||||||||||
| 19 | Sets the key C<Animation> to true if the file is an animation. Otherwise | ||||||||||||||
| 20 | sets the key C<Compression> to either C<VP8> or C<Lossless>. | ||||||||||||||
| 21 | |||||||||||||||
| 22 | =end register | ||||||||||||||
| 23 | |||||||||||||||
| 24 | =cut | ||||||||||||||
| 25 | |||||||||||||||
| 26 | package Image::Info::WEBP; | ||||||||||||||
| 27 | |||||||||||||||
| 28 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 29 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 30 | |||||||||||||||
| 31 | 2 | ▶ | use vars qw($VERSION); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 32 | 2 | $VERSION = '0.01'; | |||||||||||||
| 33 | |||||||||||||||
| 34 | sub my_read | ||||||||||||||
| 35 | { | ||||||||||||||
| 36 | 20 | ▶ | my($source, $len) = @_; | ||||||||||||
|
Subroutine
my_read: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||
| 37 | 20 | my $buf; | |||||||||||||
| 38 | 20 | my $n = read($source, $buf, $len); | |||||||||||||
| 39 | 20 | ▶ | die "read failed: $!" unless defined $n; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 40 | 20 | ▶ | die "short read ($len/$n) at pos " . tell($source) unless $n == $len; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 41 | 20 | $buf; | |||||||||||||
| 42 | } | ||||||||||||||
| 43 | |||||||||||||||
| 44 | 2 | my @upscale = (1, 5/4, 5/3, 2); | |||||||||||||
| 45 | |||||||||||||||
| 46 | sub process_file | ||||||||||||||
| 47 | { | ||||||||||||||
| 48 | 10 | ▶ | my($info, $fh) = @_; | ||||||||||||
|
Subroutine
process_file: called CC=18 SCAR=30.4
Pod
undocumented
| |||||||||||||||
| 49 | |||||||||||||||
| 50 | 10 | my $signature = my_read($fh, 16); | |||||||||||||
| 51 | 10 | ▶ | die "Bad WEBP signature" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 52 | unless $signature =~ /\ARIFF....WEBPVP8([ LX])/s; | ||||||||||||||
| 53 | |||||||||||||||
| 54 | 10 | my $type = $1; | |||||||||||||
| 55 | |||||||||||||||
| 56 | 10 | $info->push_info(0, "file_media_type" => "image/webp"); | |||||||||||||
| 57 | 10 | $info->push_info(0, "file_ext" => "webp"); | |||||||||||||
| 58 | |||||||||||||||
| 59 | # This code is (arguably) 4 bytes out of sync with the description in the | ||||||||||||||
| 60 | # spec, because the spec describes ChunkHeader('ABCD') as an 8-byte quantity | ||||||||||||||
| 61 | # and we've processed the first 4 bytes above, but need to handle the second | ||||||||||||||
| 62 | # 4 (the length) here: | ||||||||||||||
| 63 | 10 | ▶ | if ($type eq 'X') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 64 | # 32 bits of length | ||||||||||||||
| 65 | # 8 bits of flags | ||||||||||||||
| 66 | # 24 bits reserved | ||||||||||||||
| 67 | # 24 bits canvas width | ||||||||||||||
| 68 | # 24 bits canvas height | ||||||||||||||
| 69 | # and then chunks... | ||||||||||||||
| 70 | 4 | my ($length, $flags, $raw_width, $raw_height) | |||||||||||||
| 71 | = unpack 'VVVv', my_read($fh, 14); | ||||||||||||||
| 72 | # Of the 14 bytes now read, 10 were included in length: | ||||||||||||||
| 73 | 4 | $length -= 10; | |||||||||||||
| 74 | 4 | ▶ | die sprintf "Bad WEBP VP8X reserved bits 0x%02X", $flags & 0xC1 | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 75 | if $flags & 0xC1; | ||||||||||||||
| 76 | 4 | ▶ | die sprintf "Bad WEBP VP8X reserved bits 0x%06X", $flags >> 8 | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 77 | if $flags >> 8; | ||||||||||||||
| 78 | |||||||||||||||
| 79 | # Shuffle the 24 bit values into shape: | ||||||||||||||
| 80 | 4 | $raw_height = ($raw_height << 8) | ($raw_width >> 24); | |||||||||||||
| 81 | 4 | $raw_width &= 0xFFFFFF; | |||||||||||||
| 82 | # Strictly this is the canvas width/height, not that of the first frame. | ||||||||||||||
| 83 | # But 1 image, that might be animated. Hence it doesn't quite map to the | ||||||||||||||
| 84 | # "$n images in a file" model that Image::Info::GIF provides. | ||||||||||||||
| 85 | |||||||||||||||
| 86 | 4 | $info->push_info(0, "width", 1 + $raw_width); | |||||||||||||
| 87 | 4 | $info->push_info(0, "height", 1 + $raw_height); | |||||||||||||
| 88 | |||||||||||||||
| 89 | 4 | ▶ | if ($flags & 0x02) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 90 | 1 | $info->push_info(0, "Animation", 1); | |||||||||||||
| 91 | } else { | ||||||||||||||
| 92 | # Possibly could also handle EXIF chunks here, although it's unclear | ||||||||||||||
| 93 | # how much code that should share with | ||||||||||||||
| 94 | # Image::Info::JPEG::process_app1_exif(), as that seems to have both | ||||||||||||||
| 95 | # JPEG-specific logic, and more generic EXIF logic. | ||||||||||||||
| 96 | |||||||||||||||
| 97 | 3 | while (1) { | |||||||||||||
| 98 | # Spec says that length is actual length, without accounting for | ||||||||||||||
| 99 | # padding. Odd sizes are padded to the next even size: | ||||||||||||||
| 100 | 3 | ▶ | ++$length | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 101 | if $length & 1; | ||||||||||||||
| 102 | 3 | ▶ | die "seek failed: $!" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 103 | unless seek $fh, $length, 1; | ||||||||||||||
| 104 | 3 | my $buf; | |||||||||||||
| 105 | 3 | my $n = read $fh, $buf, 8; | |||||||||||||
| 106 | 3 | ▶ | die "read failed: $!" unless defined $n; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 107 | 3 | ▶ | die "No VP8 or VP8L chunk found in WEPB Extended File Format" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 108 | if $n == 0; | ||||||||||||||
| 109 | 3 | ▶ | die "short read (8/$n) at pos " . tell $fh | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 110 | unless $n == 8; | ||||||||||||||
| 111 | 3 | (my $chunk, $length) = unpack "a4V", $buf; | |||||||||||||
| 112 | 3 | ▶ | if ($chunk eq 'VP8 ') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 113 | 3 | $info->push_info(0, "Compression", "VP8"); | |||||||||||||
| 114 | 3 | last; | |||||||||||||
| 115 | } elsif ($chunk eq 'VP8L') { | ||||||||||||||
| 116 | 0 | $info->push_info(0, "Compression", "Lossless"); | |||||||||||||
| 117 | 0 | last; | |||||||||||||
| 118 | } | ||||||||||||||
| 119 | } | ||||||||||||||
| 120 | } | ||||||||||||||
| 121 | } elsif ($type eq 'L') { | ||||||||||||||
| 122 | # There doesn't seem to be a better name for this: | ||||||||||||||
| 123 | 3 | $info->push_info(0, "Compression", "Lossless"); | |||||||||||||
| 124 | # Discard the 4 bytes of length; grab the next 5. | ||||||||||||||
| 125 | 3 | my ($sig, $size_and_flags) = unpack "x4CV", my_read($fh, 9); | |||||||||||||
| 126 | 3 | ▶ | die sprintf "Bad WEBP Lossless signature 0x%02X", $sig | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 127 | unless $sig == 0x2f; | ||||||||||||||
| 128 | 3 | my $version = $size_and_flags >> 30; | |||||||||||||
| 129 | 3 | ▶ | die "Bad WEBP Lossless version $sig" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 130 | unless $version == 0; | ||||||||||||||
| 131 | 3 | $info->push_info(0, "width", 1 + $size_and_flags & 0x3FFF); | |||||||||||||
| 132 | 3 | $info->push_info(0, "height", 1 + ($size_and_flags >> 14) & 0x3FFF); | |||||||||||||
| 133 | } else { | ||||||||||||||
| 134 | 3 | $info->push_info(0, "Compression", "VP8"); | |||||||||||||
| 135 | # The fun format for a key frame is | ||||||||||||||
| 136 | # 32 bits of length | ||||||||||||||
| 137 | # 24 bits of frame tag | ||||||||||||||
| 138 | # 3 signature bytes | ||||||||||||||
| 139 | # 2+14 bits of width | ||||||||||||||
| 140 | # 2+14 bits of height | ||||||||||||||
| 141 | # We don't have a pack format for 3 bytes, but the bits we need can be | ||||||||||||||
| 142 | # got by approximating it as 2, 4, 2, 2: | ||||||||||||||
| 143 | 3 | my ($type, $start, $raw_horiz, $raw_vert) | |||||||||||||
| 144 | = unpack "x4vVvv", my_read($fh, 14); | ||||||||||||||
| 145 | 3 | ▶ | die "Bad WEBP VP8 type 1 (ie interframe)" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 146 | if $type & 1; | ||||||||||||||
| 147 | 3 | $start >>= 8; | |||||||||||||
| 148 | 3 | ▶ | die sprintf "Bad WEBP VP8 key frame start signature 0x%06X", $start | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 149 | unless $start == 0x2a019d; | ||||||||||||||
| 150 | |||||||||||||||
| 151 | # The top two bits of the raw width and height values are used as to | ||||||||||||||
| 152 | # flag a ratio to upscale. | ||||||||||||||
| 153 | # However, testing against dwebp and webpmux and then re-checking the | ||||||||||||||
| 154 | # documentation, it seems that these are really intended as information | ||||||||||||||
| 155 | # for the video hardware to render the image, because they don't change | ||||||||||||||
| 156 | # the size of bitmap returned from the decoder library. So return them | ||||||||||||||
| 157 | # as extra information, but don't recalculate the width and height. | ||||||||||||||
| 158 | 3 | $info->push_info(0, "width", ($raw_horiz & 0x3FFF)); | |||||||||||||
| 159 | 3 | $info->push_info(0, "height", ($raw_vert & 0x3FFF)); | |||||||||||||
| 160 | 3 | $info->push_info(0, "Width_Upscale", $upscale[$raw_horiz >> 14]); | |||||||||||||
| 161 | 3 | $info->push_info(0, "Height_Upscale", $upscale[$raw_vert >> 14]); | |||||||||||||
| 162 | |||||||||||||||
| 163 | } | ||||||||||||||
| 164 | } | ||||||||||||||