Skip to content

Commit

Permalink
Removed unused upsampling setting (#5)
Browse files Browse the repository at this point in the history
* Use filename placeholder in URL

* Removed unused upsampling setting

* Use has_transparency_data

* Removed unnecessary load()

* Test getexif() change

---------

Co-authored-by: Andrew Murray <[email protected]>
  • Loading branch information
radarhere and radarhere authored Nov 12, 2024
1 parent 50b993a commit 671e3c8
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 59 deletions.
20 changes: 4 additions & 16 deletions Tests/test_file_avif.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ def test_exif(self) -> None:
exif = im.getexif()
assert exif[274] == 1

with Image.open("Tests/images/avif/xmp_tags_orientation.avif") as im:
exif = im.getexif()
assert exif[274] == 3

def test_exif_save_default(self, tmp_path: Path) -> None:
with Image.open("Tests/images/avif/exif.avif") as im:
test_file = str(tmp_path / "temp.avif")
Expand Down Expand Up @@ -549,22 +553,6 @@ def test_decoder_codec_available_cannot_decode(self) -> None:
def test_decoder_codec_available_invalid(self) -> None:
assert _avif.decoder_codec_available("foo") is False

@pytest.mark.parametrize("upsampling", ["fastest", "best", "nearest", "bilinear"])
def test_decoder_upsampling(
self, monkeypatch: pytest.MonkeyPatch, upsampling: str
) -> None:
monkeypatch.setattr(AvifImagePlugin, "CHROMA_UPSAMPLING", upsampling)

with Image.open(TEST_AVIF_FILE):
pass

def test_decoder_upsampling_invalid(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(AvifImagePlugin, "CHROMA_UPSAMPLING", "foo")

with pytest.raises(ValueError):
with Image.open(TEST_AVIF_FILE):
pass

def test_p_mode_transparency(self) -> None:
im = Image.new("P", size=(64, 64))
draw = ImageDraw.Draw(im)
Expand Down
14 changes: 1 addition & 13 deletions src/PIL/AvifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# Decoder options as module globals, until there is a way to pass parameters
# to Image.open (see https://github.com/python-pillow/Pillow/issues/569)
DECODE_CODEC_CHOICE = "auto"
CHROMA_UPSAMPLING = "auto"
DEFAULT_MAX_THREADS = 0

_VALID_AVIF_MODES = {"RGB", "RGBA"}
Expand Down Expand Up @@ -76,7 +75,6 @@ def _open(self) -> None:
self._decoder = _avif.AvifDecoder(
self.fp.read(),
DECODE_CODEC_CHOICE,
CHROMA_UPSAMPLING,
_get_default_max_threads(),
)

Expand Down Expand Up @@ -238,22 +236,12 @@ def _save(

for idx in range(nfr):
ims.seek(idx)
ims.load()

# Make sure image mode is supported
frame = ims
rawmode = ims.mode
if ims.mode not in _VALID_AVIF_MODES:
alpha = (
"A" in ims.mode
or "a" in ims.mode
or (ims.mode == "P" and "A" in ims.im.getpalettemode())
or (
ims.mode == "P"
and ims.info.get("transparency", None) is not None
)
)
rawmode = "RGBA" if alpha else "RGB"
rawmode = "RGBA" if ims.has_transparency_data else "RGB"
frame = ims.convert(rawmode)

# Update frame duration
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1548,8 +1548,8 @@ def getexif(self) -> Exif:

# XMP tags
if ExifTags.Base.Orientation not in self._exif:
xmp_tags = self.info.get("XML:com.adobe.xmp") or self.info.get("xmp")
if isinstance(xmp_tags, bytes):
xmp_tags = self.info.get("XML:com.adobe.xmp")
if not xmp_tags and (xmp_tags := self.info.get("xmp")):
xmp_tags = xmp_tags.decode("utf-8")
if xmp_tags:
match = re.search(r'tiff:Orientation(="|>)([0-9])', xmp_tags)
Expand Down
27 changes: 1 addition & 26 deletions src/_avif.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
#include <Python.h>
#include "avif/avif.h"

#if AVIF_VERSION < 80300
#define AVIF_CHROMA_UPSAMPLING_AUTOMATIC AVIF_CHROMA_UPSAMPLING_BILINEAR
#define AVIF_CHROMA_UPSAMPLING_BEST_QUALITY AVIF_CHROMA_UPSAMPLING_BILINEAR
#define AVIF_CHROMA_UPSAMPLING_FASTEST AVIF_CHROMA_UPSAMPLING_NEAREST
#endif

typedef struct {
avifPixelFormat subsampling;
int qmin;
Expand Down Expand Up @@ -671,32 +665,13 @@ AvifDecoderNew(PyObject *self_, PyObject *args) {
PyObject *avif_bytes;
AvifDecoderObject *self = NULL;

char *upsampling_str;
char *codec_str;
avifCodecChoice codec;
avifChromaUpsampling upsampling;
int max_threads;

avifResult result;

if (!PyArg_ParseTuple(
args, "Sssi", &avif_bytes, &codec_str, &upsampling_str, &max_threads
)) {
return NULL;
}

if (!strcmp(upsampling_str, "auto")) {
upsampling = AVIF_CHROMA_UPSAMPLING_AUTOMATIC;
} else if (!strcmp(upsampling_str, "fastest")) {
upsampling = AVIF_CHROMA_UPSAMPLING_FASTEST;
} else if (!strcmp(upsampling_str, "best")) {
upsampling = AVIF_CHROMA_UPSAMPLING_BEST_QUALITY;
} else if (!strcmp(upsampling_str, "nearest")) {
upsampling = AVIF_CHROMA_UPSAMPLING_NEAREST;
} else if (!strcmp(upsampling_str, "bilinear")) {
upsampling = AVIF_CHROMA_UPSAMPLING_BILINEAR;
} else {
PyErr_Format(PyExc_ValueError, "Invalid upsampling option: %s", upsampling_str);
if (!PyArg_ParseTuple(args, "Ssi", &avif_bytes, &codec_str, &max_threads)) {
return NULL;
}

Expand Down
3 changes: 1 addition & 2 deletions winbuild/build_prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,7 @@ def cmd_msbuild(
},
"rav1e": {
"url": (
f"https://github.com/xiph/rav1e/releases/download/v{V['RAV1E']}/"
f"rav1e-{V['RAV1E']}-windows-msvc-generic.zip"
f"https://github.com/xiph/rav1e/releases/download/v{V['RAV1E']}/FILENAME"
),
"filename": f"rav1e-{V['RAV1E']}-windows-msvc-generic.zip",
"dir": "rav1e-windows-msvc-sdk",
Expand Down

0 comments on commit 671e3c8

Please sign in to comment.