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

Removed alpha_premultiplied #23

Closed
wants to merge 1 commit 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
14 changes: 5 additions & 9 deletions Tests/test_file_avif.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,18 +662,14 @@ def test_heif_raises_unidentified_image_error(self) -> None:
with Image.open("Tests/images/avif/rgba10.heif"):
pass

@pytest.mark.parametrize("alpha_premultiplied", [False, True])
def test_alpha_premultiplied(
self, tmp_path: Path, alpha_premultiplied: bool
) -> None:
def test_alpha_premultiplied(self, tmp_path: Path) -> None:
temp_file = str(tmp_path / "temp.avif")
color = (200, 200, 200, 1)
im = Image.new("RGBA", (1, 1), color)
im.save(temp_file, alpha_premultiplied=alpha_premultiplied)
im = Image.new("RGBa", (1, 1), (200, 200, 200, 1))
im.save(temp_file)

expected = (255, 255, 255, 1) if alpha_premultiplied else color
with Image.open(temp_file) as reloaded:
assert reloaded.getpixel((0, 0)) == expected
assert reloaded.mode == "RGBA"
assert reloaded.getpixel((0, 0)) == (255, 255, 255, 1)

def test_timestamp_and_duration(self, tmp_path: Path) -> None:
"""
Expand Down
4 changes: 0 additions & 4 deletions docs/handbook/image-file-formats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ The :py:meth:`~PIL.Image.Image.save` method supports the following options:
and "tile_cols" both have their default values of zero. Requires libavif version
**0.11.0** or greater.

**alpha_premultiplied**
Encode the image with premultiplied alpha. Defaults to ``False``. Requires libavif
version **0.9.0** or greater.

**advanced**
Codec specific options. Requires libavif version **0.8.2** or greater.

Expand Down
17 changes: 13 additions & 4 deletions src/PIL/AvifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from io import BytesIO
from typing import IO

from . import ExifTags, Image, ImageFile
from . import ExifTags, Image, ImageFile, features

try:
from . import _avif
Expand Down Expand Up @@ -164,7 +164,6 @@ def _save(
range_ = info.get("range", "full")
tile_rows_log2 = info.get("tile_rows", 0)
tile_cols_log2 = info.get("tile_cols", 0)
alpha_premultiplied = bool(info.get("alpha_premultiplied", False))
autotiling = bool(info.get("autotiling", tile_rows_log2 == tile_cols_log2 == 0))

icc_profile = info.get("icc_profile", im.info.get("icc_profile"))
Expand Down Expand Up @@ -214,7 +213,6 @@ def _save(
range_,
tile_rows_log2,
tile_cols_log2,
alpha_premultiplied,
autotiling,
icc_profile or b"",
exif or b"",
Expand All @@ -228,6 +226,17 @@ def _save(
frame_duration = 0
cur_idx = im.tell()
is_single_frame = total == 1
supported_modes = {"RGB", "RGBA"}

try:
from packaging.version import parse as parse_version
except ImportError:
pass
else:
version = features.version_module("avif")
assert version is not None
if parse_version(version) > parse_version("0.9.0"):
supported_modes.add("RGBa")
try:
for ims in [im] + append_images:
# Get # of frames in this image
Expand All @@ -239,7 +248,7 @@ def _save(
# Make sure image mode is supported
frame = ims
rawmode = ims.mode
if ims.mode not in {"RGB", "RGBA"}:
if ims.mode not in supported_modes:
rawmode = "RGBA" if ims.has_transparency_data else "RGB"
frame = ims.convert(rawmode)

Expand Down
21 changes: 9 additions & 12 deletions src/_avif.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ AvifEncoderNew(PyObject *self_, PyObject *args) {
Py_buffer icc_buffer;
Py_buffer exif_buffer;
Py_buffer xmp_buffer;
int alpha_premultiplied;
int autotiling;
int tile_rows_log2;
int tile_cols_log2;
Expand All @@ -254,7 +253,7 @@ AvifEncoderNew(PyObject *self_, PyObject *args) {

if (!PyArg_ParseTuple(
args,
"(II)siiissiippy*y*iy*O",
"(II)siiissiipy*y*iy*O",
&width,
&height,
&subsampling,
Expand All @@ -265,7 +264,6 @@ AvifEncoderNew(PyObject *self_, PyObject *args) {
&range,
&tile_rows_log2,
&tile_cols_log2,
&alpha_premultiplied,
&autotiling,
&icc_buffer,
&exif_buffer,
Expand Down Expand Up @@ -318,9 +316,6 @@ AvifEncoderNew(PyObject *self_, PyObject *args) {
image->height = height;

image->depth = 8;
#if AVIF_VERSION >= 90000
image->alphaPremultiplied = alpha_premultiplied ? AVIF_TRUE : AVIF_FALSE;
#endif

encoder = avifEncoderCreate();
if (!encoder) {
Expand Down Expand Up @@ -537,17 +532,19 @@ _encoder_add(AvifEncoderObject *self, PyObject *args) {
frame->yuvRange = image->yuvRange;
frame->yuvFormat = image->yuvFormat;
frame->depth = image->depth;
#if AVIF_VERSION >= 90000
frame->alphaPremultiplied = image->alphaPremultiplied;
#endif
}

avifRGBImageSetDefaults(&rgb, frame);

if (strcmp(mode, "RGBA") == 0) {
rgb.format = AVIF_RGB_FORMAT_RGBA;
} else {
if (strcmp(mode, "RGB") == 0) {
rgb.format = AVIF_RGB_FORMAT_RGB;
} else {
rgb.format = AVIF_RGB_FORMAT_RGBA;
#if AVIF_VERSION >= 90000
if (strcmp(mode, "RGBa") == 0) {
frame->alphaPremultiplied = AVIF_TRUE;
}
#endif
}

result = avifRGBImageAllocatePixels(&rgb);
Expand Down
Loading