Skip to content

Commit

Permalink
Moved codec availability errors to Python
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Dec 2, 2024
1 parent f7e6250 commit 8696eab
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 23 deletions.
8 changes: 8 additions & 0 deletions src/PIL/AvifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ def _open(self) -> None:
)
raise SyntaxError(msg)

if DECODE_CODEC_CHOICE != "auto" and not _avif.decoder_codec_available(
DECODE_CODEC_CHOICE
):
msg = "Invalid opening codec"
raise ValueError(msg)
self._decoder = _avif.AvifDecoder(
self.fp.read(),
DECODE_CODEC_CHOICE,
Expand Down Expand Up @@ -153,6 +158,9 @@ def _save(
speed = info.get("speed", 6)
max_threads = info.get("max_threads", _get_default_max_threads())
codec = info.get("codec", "auto")
if codec != "auto" and not _avif.encoder_codec_available(codec):
msg = "Invalid saving codec"
raise ValueError(msg)
range_ = info.get("range", "full")
tile_rows_log2 = info.get("tile_rows", 0)
tile_cols_log2 = info.get("tile_cols", 0)
Expand Down
23 changes: 0 additions & 23 deletions src/_avif.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,17 +349,6 @@ AvifEncoderNew(PyObject *self_, PyObject *args) {
enc_options.codec = AVIF_CODEC_CHOICE_AUTO;
} else {
enc_options.codec = avifCodecChoiceFromName(codec);
if (enc_options.codec == AVIF_CODEC_CHOICE_AUTO) {
PyErr_Format(PyExc_ValueError, "Invalid codec: %s", codec);
return NULL;
} else {
const char *codec_name =
avifCodecName(enc_options.codec, AVIF_CODEC_FLAG_CAN_ENCODE);
if (codec_name == NULL) {
PyErr_Format(PyExc_ValueError, "AV1 Codec cannot encode: %s", codec);
return NULL;
}
}
}

if (strcmp(range, "full") == 0) {
Expand Down Expand Up @@ -734,18 +723,6 @@ AvifDecoderNew(PyObject *self_, PyObject *args) {
codec = AVIF_CODEC_CHOICE_AUTO;
} else {
codec = avifCodecChoiceFromName(codec_str);
if (codec == AVIF_CODEC_CHOICE_AUTO) {
PyErr_Format(PyExc_ValueError, "Invalid codec: %s", codec_str);
return NULL;
} else {
const char *codec_name = avifCodecName(codec, AVIF_CODEC_FLAG_CAN_DECODE);
if (codec_name == NULL) {
PyErr_Format(
PyExc_ValueError, "AV1 Codec cannot decode: %s", codec_str
);
return NULL;
}
}
}

self = PyObject_New(AvifDecoderObject, &AvifDecoder_Type);
Expand Down

0 comments on commit 8696eab

Please sign in to comment.