Adds a function to decode VP8X chunks

This commit is contained in:
Fabien LOISON 2021-04-14 16:45:36 +02:00
parent 23ed3ab201
commit 9f8759e153
No known key found for this signature in database
GPG Key ID: FF90CA148348048E
3 changed files with 103 additions and 1 deletions

View File

@ -2,7 +2,7 @@
import sys
from yoga.image.encoders.webp import get_riff_structure
from yoga.image.encoders.webp import get_riff_structure, get_vp8x_info
def print_riff_info(input_path):
@ -20,6 +20,14 @@ def print_riff_info(input_path):
" +-- %s [offset: %i, size: %i]"
% (chunk["type"], chunk["data_offset"], chunk["size"])
)
if chunk["type"] == "VP8X":
vp8x_info = get_vp8x_info(
image[
chunk["data_offset"] : chunk["data_offset"] + chunk["size"]
]
)
for key, value in vp8x_info.items():
print(" +-- %s: %i" % (key, value))
if __name__ == "__main__":

View File

@ -29,6 +29,70 @@ class Test_get_riff_structure(object):
assert riff["chunks"][4]["type"] == "XMP "
class Test_get_vp8x_info(object):
def test_flag_icc(self):
vp8x_info = webp.get_vp8x_info(
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
assert vp8x_info["has_icc"] is False
vp8x_info = webp.get_vp8x_info(
b"\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
assert vp8x_info["has_icc"] is True
def test_flag_alpha(self):
vp8x_info = webp.get_vp8x_info(
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
assert vp8x_info["has_alpha"] is False
vp8x_info = webp.get_vp8x_info(
b"\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
assert vp8x_info["has_alpha"] is True
def test_flag_exif(self):
vp8x_info = webp.get_vp8x_info(
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
assert vp8x_info["has_exif"] is False
vp8x_info = webp.get_vp8x_info(
b"\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
assert vp8x_info["has_exif"] is True
def test_flag_xmp(self):
vp8x_info = webp.get_vp8x_info(
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
assert vp8x_info["has_xmp"] is False
vp8x_info = webp.get_vp8x_info(
b"\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
assert vp8x_info["has_xmp"] is True
def test_flag_animation(self):
vp8x_info = webp.get_vp8x_info(
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
assert vp8x_info["has_anim"] is False
vp8x_info = webp.get_vp8x_info(
b"\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
assert vp8x_info["has_anim"] is True
def test_canvas_width(self):
vp8x_info = webp.get_vp8x_info(
b"\x00\x00\x00\x00\xAA\xBB\xCC\x00\x00\x00"
)
assert vp8x_info["canvas_width"] == 0xCCBBAA + 1
def test_canvas_height(self):
vp8x_info = webp.get_vp8x_info(
b"\x00\x00\x00\x00\x00\x00\x00\xAA\xBB\xCC"
)
assert vp8x_info["canvas_height"] == 0xCCBBAA + 1
class Test_encode_lossy_webp(object):
@pytest.mark.parametrize(
"image_path",

View File

@ -35,6 +35,36 @@ def get_riff_structure(data):
return result
def get_vp8x_info(data):
# fmt: off
# RRILEXAR
VP8X_FLAG_ICC = 0b00100000 # noqa: E221
VP8X_FLAG_ALPHA = 0b00010000 # noqa: E221
VP8X_FLAG_EXIF = 0b00001000 # noqa: E221
VP8X_FLAG_XMP = 0b00000100 # noqa: E221
VP8X_FLAG_ANIM = 0b00000010 # noqa: E221
# fmt: on
if len(data) != 10:
ValueError("Invaild VP8X data")
return {
"has_icc": bool(data[0] & VP8X_FLAG_ICC),
"has_alpha": bool(data[0] & VP8X_FLAG_ALPHA),
"has_exif": bool(data[0] & VP8X_FLAG_EXIF),
"has_xmp": bool(data[0] & VP8X_FLAG_XMP),
"has_anim": bool(data[0] & VP8X_FLAG_ANIM),
"canvas_width": little_endian_unint32_bytes_to_python_int(
data[4:7] + b"\x00"
)
+ 1,
"canvas_height": little_endian_unint32_bytes_to_python_int(
data[7:10] + b"\x00"
)
+ 1,
}
def is_riff(file_bytes):
"""Whether or not the given bytes represent a RIFF file.