#!/usr/bin/env python3
"""Create a smaller Radiance HDR while retaining linear high-range pixels."""

from __future__ import annotations

import sys
from pathlib import Path

import bpy


def main() -> None:
    if "--" not in sys.argv or len(sys.argv[sys.argv.index("--") + 1 :]) != 4:
        raise SystemExit("usage: -- INPUT OUTPUT WIDTH HEIGHT")
    source, destination, width, height = sys.argv[sys.argv.index("--") + 1 :]
    destination_path = Path(destination).resolve()
    destination_path.parent.mkdir(parents=True, exist_ok=True)
    image = bpy.data.images.load(str(Path(source).resolve()), check_existing=False)
    image.scale(int(width), int(height))
    image.filepath_raw = str(destination_path)
    image.file_format = "HDR"
    image.save()
    print(f"HDR written: {destination_path} ({image.size[0]}x{image.size[1]})")


if __name__ == "__main__":
    main()
