Skip to content

Commit

Permalink
Fix webp header unpacking
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Feb 17, 2024
1 parent 2837aed commit 8931f9a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
26 changes: 8 additions & 18 deletions panel/pane/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,22 +522,12 @@ class WebP(ImageBase):

@classmethod
def _imgshape(cls, data):
import struct
b = BytesIO(data)
b.read(12) # Skip RIFF header
chunk_header = b.read(4)
if chunk_header == b'VP8 ': # Lossy WebP
b.read(3) # Skip VP8 header
w, h = struct.unpack("<HH", b.read(4)) # Width and height are little-endian
elif chunk_header == b'VP8L': # Lossless WebP
b.read(1) # Skip VP8L header
bits = struct.unpack("<I", b.read(4))[0] # Read width and height bits
w = (bits & 0x3FFF) + 1 # Width is stored in the 14 least significant bits
h = ((bits >> 14) & 0x3FFF) + 1 # Height is stored in the next 14 bits
elif chunk_header == b'VP8X': # Extended WebP
b.read(4) # Skip VP8X header
w = struct.unpack("<I", b.read(3))[0] + 1 # Width is stored in the next 3 bytes
h = struct.unpack("<I", b.read(3))[0] + 1 # Height is stored in the next 3 bytes
else:
raise ValueError("Invalid WebP file")
with BytesIO(data) as b:
b.read(12) # Skip RIFF header
chunk_header = b.read(8)
if chunk_header[:3] != b'VP8':
raise ValueError("Invalid WebP file")
b.read(4)
w = int.from_bytes(b.read(3), 'little') + 1
h = int.from_bytes(b.read(3), 'little') + 1
return int(w), int(h)
4 changes: 2 additions & 2 deletions panel/tests/pane/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def test_svg_pane(document, comm):
b'ACEQMRAD8AA0qs5HvTHQcJdsChioXSbOr/2Q==',
ico = b'AAABAAEAAgEAAAEAIAA0AAAAFgAAACgAAAACAAAAAgAAAAEAIAAAAAAACAAAAHQ' + \
b'SAAB0EgAAAAAAAAAAAAD//////////wAAAAA=',
webp= b'UklGRi4AAABXRUJQVlA4ICIAAACQAQCdASoCAAEAAgA0JZwAAudZuFQA/vXf1fmh' + \
b'/Al4AAAA'
webp= b'UklGRkIAAABXRUJQVlA4WAoAAAAQAAAAAQAAAAAAQUxQSAMAAAAAAAAAVlA4IBg' + \
b'AAAAwAQCdASoCAAEAAUAmJaQAA3AA/v02aAA='
)


Expand Down

0 comments on commit 8931f9a

Please sign in to comment.