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

[pre-commit.ci] pre-commit autoupdate #315

Merged
merged 2 commits into from
Dec 19, 2024
Merged
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ repos:
- id: pyproject-fmt

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.1
rev: v0.8.3
hooks:
- id: ruff

Expand Down
82 changes: 41 additions & 41 deletions libheif/linux_build_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,22 @@ def download_file(url: str, out_path: str) -> bool:

def download_extract_to(url: str, out_path: str, strip: bool = True):
makedirs(out_path, exist_ok=True)
_archive_path = path.join(out_path, "download.tar.gz")
download_file(url, _archive_path)
_tar_cmd = f"tar -xf {_archive_path} -C {out_path}"
archive_path = path.join(out_path, "download.tar.gz")
download_file(url, archive_path)
tar_cmd = f"tar -xf {archive_path} -C {out_path}"
if strip:
_tar_cmd += " --strip-components 1"
run(_tar_cmd.split(), check=True)
remove(_archive_path)
tar_cmd += " --strip-components 1"
run(tar_cmd.split(), check=True)
remove(archive_path)


def tool_check_version(name: str, min_version: str) -> bool:
try:
_ = run([name, "--version"], stdout=PIPE, stderr=DEVNULL, check=True)
except (CalledProcessError, FileNotFoundError):
return False
_regexp = r"version\s*(\d+(\.\d+){2})" if name == "nasm" else r"(\d+(\.\d+){2})$" # cmake
m_groups = search(_regexp, _.stdout.decode("utf-8"), flags=MULTILINE + IGNORECASE)
v_regexp = r"version\s*(\d+(\.\d+){2})" if name == "nasm" else r"(\d+(\.\d+){2})$" # cmake
m_groups = search(v_regexp, _.stdout.decode("utf-8"), flags=MULTILINE + IGNORECASE)
if m_groups is None:
return False
current_version = tuple(map(int, str(m_groups.groups()[0]).split(".")))
Expand All @@ -82,18 +82,18 @@ def check_install_nasm(version: str):
if tool_check_version("nasm", version):
return True
print(f"Can not find `nasm` with version >={version}, installing...")
_tool_path = path.join(BUILD_DIR, "nasm")
if path.isdir(_tool_path):
tool_path = path.join(BUILD_DIR, "nasm")
if path.isdir(tool_path):
print("Cache found for nasm", flush=True)
chdir(_tool_path)
chdir(tool_path)
else:
download_extract_to(f"https://www.nasm.us/pub/nasm/releasebuilds/{version}/nasm-{version}.tar.gz", _tool_path)
chdir(_tool_path)
download_extract_to(f"https://www.nasm.us/pub/nasm/releasebuilds/{version}/nasm-{version}.tar.gz", tool_path)
chdir(tool_path)
run(["./configure"], check=True)
run("make".split(), check=True)
run("make install".split(), check=True)
run("nasm --version".split(), check=True)
run(f"chmod -R 774 {_tool_path}".split(), check=True)
run(f"chmod -R 774 {tool_path}".split(), check=True)
return True


Expand All @@ -105,10 +105,10 @@ def is_musllinux() -> bool:
def is_library_installed(name: str) -> bool:
if name.find("main") != -1 and name.find("reference") != -1:
raise Exception("`name` param can not contain `main` and `reference` substrings.")
_r = run(f"gcc -l{name}".split(), stdout=PIPE, stderr=STDOUT, check=False)
if _r.stdout:
_ = _r.stdout.decode("utf-8")
if _.find("main") != -1 and _.find("reference") != -1:
result = run(f"gcc -l{name}".split(), stdout=PIPE, stderr=STDOUT, check=False)
if result.stdout:
decoded_result = result.stdout.decode("utf-8")
if decoded_result.find("main") != -1 and decoded_result.find("reference") != -1:
return True
return False

Expand All @@ -121,43 +121,43 @@ def run_print_if_error(args) -> None:


def build_lib_linux(url: str, name: str):
_lib_path = path.join(BUILD_DIR, name)
if path.isdir(_lib_path):
lib_path = path.join(BUILD_DIR, name)
if path.isdir(lib_path):
print(f"Cache found for {name}", flush=True)
chdir(path.join(_lib_path, "build")) if name != "x265" else chdir(_lib_path)
chdir(path.join(lib_path, "build")) if name != "x265" else chdir(lib_path)
else:
_hide_build_process = True
_script_dir = path.dirname(path.abspath(__file__))
_linux_dir = path.join(_script_dir, "linux")
hide_build_process = True
script_dir = path.dirname(path.abspath(__file__))
linux_dir = path.join(script_dir, "linux")
if name == "x265":
download_extract_to(url, _lib_path)
chdir(_lib_path)
download_extract_to(url, lib_path)
chdir(lib_path)
else:
_build_path = path.join(_lib_path, "build")
makedirs(_build_path)
build_path = path.join(lib_path, "build")
makedirs(build_path)
if name == "aom":
download_extract_to(url, path.join(_lib_path, "aom"), False)
download_extract_to(url, path.join(lib_path, "aom"), False)
if is_musllinux():
patch_path = path.join(_linux_dir, "aom-musl/fix-stack-size-e53da0b-2.patch")
chdir(path.join(_lib_path, "aom"))
patch_path = path.join(linux_dir, "aom-musl/fix-stack-size-e53da0b-2.patch")
chdir(path.join(lib_path, "aom"))
run(f"patch -p 1 -i {patch_path}".split(), check=True)
else:
download_extract_to(url, _lib_path)
download_extract_to(url, lib_path)
if name == "libde265": # noqa
chdir(_lib_path)
chdir(lib_path)
# for patch in (
# "libde265/CVE-2022-1253.patch",
# ):
# patch_path = path.join(_linux_dir, patch)
# patch_path = path.join(linux_dir, patch)
# run(f"patch -p 1 -i {patch_path}".split(), check=True)
elif name == "libheif":
chdir(_lib_path)
chdir(lib_path)
# for patch in (
# "libheif/001-void-unused-variable.patch",
# ):
# patch_path = path.join(_linux_dir, patch)
# patch_path = path.join(linux_dir, patch)
# run(f"patch -p 1 -i {patch_path}".split(), check=True)
chdir(_build_path)
chdir(build_path)
print(f"Preconfiguring {name}...", flush=True)
if name == "aom":
cmake_args = "-DENABLE_TESTS=0 -DENABLE_TOOLS=0 -DENABLE_EXAMPLES=0 -DENABLE_DOCS=0".split()
Expand Down Expand Up @@ -215,12 +215,12 @@ def build_lib_linux(url: str, name: str):
"-DWITH_EXAMPLES=OFF "
"-DBUILD_TESTING=OFF".split()
)
_hide_build_process = False
hide_build_process = False
if is_musllinux():
cmake_args += [f"-DCMAKE_INSTALL_LIBDIR={INSTALL_DIR_LIBS}/lib"]
run(["cmake", *cmake_args], check=True)
print(f"{name} configured. building...", flush=True)
if _hide_build_process:
if hide_build_process:
run_print_if_error("make -j4".split())
else:
run("make -j4".split(), check=True)
Expand All @@ -233,7 +233,7 @@ def build_lib_linux(url: str, name: str):


def build_libs() -> None:
_original_dir = getcwd()
original_dir = getcwd()
try:
if not tool_check_version("cmake", "3.16.3"):
raise ValueError("Can not find `cmake` with version >=3.16.3")
Expand All @@ -257,7 +257,7 @@ def build_libs() -> None:
print("libde265 already installed.")
build_lib_linux(LIBHEIF_URL, "libheif")
finally:
chdir(_original_dir)
chdir(original_dir)


if __name__ == "__main__":
Expand Down
32 changes: 15 additions & 17 deletions pillow_heif/as_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ def __init__(self, *args, **kwargs):
def _open(self):
try:
# when Pillow starts supporting 16-bit multichannel images change `convert_hdr_to_8bit` to False
_heif_file = HeifFile(self.fp, convert_hdr_to_8bit=True, hdr_to_16bit=True, remove_stride=False)
heif_file = HeifFile(self.fp, convert_hdr_to_8bit=True, hdr_to_16bit=True, remove_stride=False)
except (OSError, ValueError, SyntaxError, RuntimeError, EOFError) as exception:
raise SyntaxError(str(exception)) from None
self.custom_mimetype = _heif_file.mimetype
self._heif_file = _heif_file
self.__frame = _heif_file.primary_index
self.custom_mimetype = heif_file.mimetype
self._heif_file = heif_file
self.__frame = heif_file.primary_index
self._init_from_heif_file(self.__frame)
self.tile = []

Expand Down Expand Up @@ -100,9 +100,9 @@ def seek(self, frame: int):
# https://github.com/python-pillow/Pillow/issues/8439
self.im = Image.core.new(self._mode, self._size) # pylint: disable=too-many-function-args

_exif = getattr(self, "_exif", None) # Pillow 9.2+ do no reload exif between frames.
if _exif is not None and getattr(_exif, "_loaded", None):
_exif._loaded = False # pylint: disable=protected-access
exif = getattr(self, "_exif", None) # Pillow 9.2+ do no reload exif between frames.
if exif is not None and getattr(exif, "_loaded", None):
exif._loaded = False # pylint: disable=protected-access

def tell(self) -> int:
return self.__frame
Expand Down Expand Up @@ -269,15 +269,13 @@ def __save_all(im: Image.Image, fp: IO[bytes], compression_format: HeifCompressi
def _pil_encode_image(ctx: CtxEncode, img: Image.Image, primary: bool, **kwargs) -> None:
if img.size[0] <= 0 or img.size[1] <= 0:
raise ValueError("Empty images are not supported.")
_info = img.info.copy()
_info["exif"] = _exif_from_pillow(img)
_info["xmp"] = _xmp_from_pillow(img)
_info.update(**kwargs)
_info["primary"] = primary
info = img.info.copy()
info["exif"] = _exif_from_pillow(img)
info["xmp"] = _xmp_from_pillow(img)
info.update(**kwargs)
info["primary"] = primary
if img.mode == "YCbCr":
ctx.add_image_ycbcr(img, image_orientation=_get_orientation_for_encoder(_info), **_info)
ctx.add_image_ycbcr(img, image_orientation=_get_orientation_for_encoder(info), **info)
else:
_img = _pil_to_supported_mode(img)
ctx.add_image(
_img.size, _img.mode, _img.tobytes(), image_orientation=_get_orientation_for_encoder(_info), **_info
)
img = _pil_to_supported_mode(img)
ctx.add_image(img.size, img.mode, img.tobytes(), image_orientation=_get_orientation_for_encoder(info), **info)
Loading
Loading