Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for Animated WebP Support. #2

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 100 additions & 32 deletions piexif/_webp.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,50 +93,118 @@ def _get_size_from_anmf(chunk):
height = height_minus_one + 1
return (width, height)

def set_vp8x(chunks):

width = None
height = None
flags = [b"0", b"0", b"0", b"0", b"0", b"0", b"0", b"0"] # [0, 0, ICC, Alpha, EXIF, XMP, Anim, 0]
def _get_sub_chunks_from_anmf(anmf_chunk):
"""
Extracts and returns sub-chunks from the ANMF chunk data.

for chunk in chunks:
if chunk["fourcc"] == b"VP8X":
:param anmf_chunk: Dictionary containing the 'fourcc', 'length_bytes', and 'data' for the ANMF chunk.
:return: List of dictionaries representing sub-chunks found within the ANMF chunk.
"""
data = anmf_chunk['data']
sub_chunks = []
offset = 0

# Fixed size for the ANMF header (Frame X, Frame Y, Width, Height, Duration, Reserved, B, D)
anmf_header_size = 16

# Skip the ANMF header to get to the Frame Data
offset += anmf_header_size

data_length = len(data)
while offset < data_length:
# Ensure there's enough data left for a new chunk (4 bytes for fourcc + 4 bytes for length)
if offset + 8 > data_length:
break

# Read the fourcc code (4 bytes)
fourcc = data[offset:offset + 4]
offset += 4

# Read the length of the chunk (4 bytes, little-endian)
length = struct.unpack('<I', data[offset:offset + 4])[0]
offset += 4

# Ensure there's enough data left for the chunk data
if offset + length > data_length:
break

# Read the chunk data
chunk_data = data[offset:offset + length]
offset += length

# Append the sub-chunk to the list
sub_chunks.append({
'fourcc': fourcc,
'length_bytes': struct.pack('<I', length),
'data': chunk_data
})

# Ensure chunks are padded to even length
if length % 2 == 1:
offset += 1

return sub_chunks

def set_vp8x(chunks):
max_width = 0
max_height = 0
flags = [b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0'] # [0, 0, ICC, Alpha, EXIF, XMP, Anim, 0]

def process_chunk(chunk):
nonlocal max_width, max_height
if chunk['fourcc'] == b'VP8X':
width, height = _get_size_from_vp8x(chunk)
elif chunk["fourcc"] == b"VP8 ":
max_width = max(max_width, width)
max_height = max(max_height, height)
elif chunk['fourcc'] == b'VP8 ':
width, height = _get_size_from_vp8(chunk)
elif chunk["fourcc"] == b"VP8L":
is_rgba = _vp8L_contains_alpha(chunk["data"])
max_width = max(max_width, width)
max_height = max(max_height, height)
elif chunk['fourcc'] == b'VP8L':
is_rgba = _vp8L_contains_alpha(chunk['data'])
if is_rgba:
flags[3] = b"1"
flags[3] = b'1'
width, height = _get_size_from_vp8L(chunk)
elif chunk["fourcc"] == b"ANMF":
width, height = _get_size_from_anmf(chunk)
elif chunk["fourcc"] == b"ICCP":
flags[2] = b"1"
elif chunk["fourcc"] == b"ALPH":
flags[3] = b"1"
elif chunk["fourcc"] == b"EXIF":
flags[4] = b"1"
elif chunk["fourcc"] == b"XMP ":
flags[5] = b"1"
elif chunk["fourcc"] == b"ANIM":
flags[6] = b"1"
width_minus_one = width - 1
height_minus_one = height - 1
max_width = max(max_width, width)
max_height = max(max_height, height)
elif chunk['fourcc'] == b'ANMF':
sub_chunks = _get_sub_chunks_from_anmf(chunk)
for sub_chunk in sub_chunks:
process_chunk(sub_chunk)
frame_width, frame_height = _get_size_from_anmf(chunk)
max_width = max(max_width, frame_width)
max_height = max(max_height, frame_height)
elif chunk['fourcc'] == b'ICCP':
flags[2] = b'1'
elif chunk['fourcc'] == b'ALPH':
flags[3] = b'1'
elif chunk['fourcc'] == b'EXIF':
flags[4] = b'1'
elif chunk['fourcc'] == b'XMP ':
flags[5] = b'1'
elif chunk['fourcc'] == b'ANIM':
flags[6] = b'1'

for chunk in chunks:
process_chunk(chunk)

max_width_minus_one = max_width - 1
max_height_minus_one = max_height - 1

if chunks[0]["fourcc"] == b"VP8X":
if chunks[0]['fourcc'] == b'VP8X':
chunks.pop(0)

header_bytes = b"VP8X"
length_bytes = b"\x0a\x00\x00\x00"
flags_bytes = struct.pack("B", int(b"".join(flags), 2))
padding_bytes = b"\x00\x00\x00"
width_bytes = struct.pack("<L", width_minus_one)[:3]
height_bytes = struct.pack("<L", height_minus_one)[:3]
header_bytes = b'VP8X'
length_bytes = b'\x0a\x00\x00\x00'
flags_bytes = struct.pack('B', int(b''.join(flags), 2))
padding_bytes = b'\x00\x00\x00'
width_bytes = struct.pack('<L', max_width_minus_one)[:3]
height_bytes = struct.pack('<L', max_height_minus_one)[:3]

data_bytes = flags_bytes + padding_bytes + width_bytes + height_bytes

vp8x_chunk = {"fourcc":header_bytes, "length_bytes":length_bytes, "data":data_bytes}
vp8x_chunk = {'fourcc': header_bytes, 'length_bytes': length_bytes, 'data': data_bytes}
chunks.insert(0, vp8x_chunk)

return chunks
Expand Down
Binary file added tests/images/pil_animated1.webp
Binary file not shown.
Binary file added tests/images/pil_animated2.webp
Binary file not shown.
Binary file added tests/images/pil_animated3.webp
Binary file not shown.
15 changes: 15 additions & 0 deletions tests/s_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,9 @@ def test_merge_chunks(self):
"pil3.webp",
"pil_rgb.webp",
"pil_rgba.webp",
"pil_animated1.webp",
"pil_animated2.webp",
"pil_animated3.webp",
]

for filename in files:
Expand Down Expand Up @@ -901,6 +904,9 @@ def test_insert_exif(self):
"pil3.webp",
"pil_rgb.webp",
"pil_rgba.webp",
"pil_animated1.webp",
"pil_animated2.webp",
"pil_animated3.webp",
]

exif_dict = {
Expand Down Expand Up @@ -936,6 +942,9 @@ def test_remove_exif(self):
"pil3.webp",
"pil_rgb.webp",
"pil_rgba.webp",
"pil_animated1.webp",
"pil_animated2.webp",
"pil_animated3.webp",
]

for filename in files:
Expand Down Expand Up @@ -999,6 +1008,9 @@ def test_remove(self):
"pil3.webp",
"pil_rgb.webp",
"pil_rgba.webp",
"pil_animated1.webp",
"pil_animated2.webp",
"pil_animated3.webp",
]

for filename in files:
Expand All @@ -1021,6 +1033,9 @@ def test_insert(self):
"pil3.webp",
"pil_rgb.webp",
"pil_rgba.webp",
"pil_animated1.webp",
"pil_animated2.webp",
"pil_animated3.webp",
]

exif_dict = {
Expand Down