Files
CalamitousFelicitousness 76aa949a26 refactor: integrate sharpfin for high-quality image resize
Vendor sharpfin library (Apache 2.0) and add centralized wrapper
module (images_sharpfin.py) replacing torchvision tensor/PIL
conversion and resize operations throughout the codebase.

- Add modules/sharpfin/ vendored library with MKS2021, Lanczos3,
  Mitchell, Catmull-Rom kernels and optional Triton sparse acceleration
- Add modules/images_sharpfin.py wrapper with to_tensor(), to_pil(),
  pil_to_tensor(), normalize(), resize(), resize_tensor()
- Add resize_quality and resize_linearize_srgb settings
- Add MKS2021 and Lanczos3 upscaler entries
- Replace torchvision.transforms.functional imports across 18 files
- to_pil() auto-detects HWC/BHWC layout, adds .round() before uint8
- Sparse Triton path falls back to dense GPU on compilation failure
- Mixed-axis resize splits into two single-axis scale() calls
- Masks and non-sRGB data always use linearize=False
2026-02-11 09:57:37 +01:00

50 lines
1.2 KiB
Python

"""Sharpfin utility types and color space conversion functions.
Vendored from https://github.com/drhead/sharpfin (Apache 2.0)
"""
from enum import Enum
import torch
def srgb_to_linear(image: torch.Tensor) -> torch.Tensor:
return torch.where(
image <= 0.04045,
image / 12.92,
# Clamping is for protection against NaNs during backwards passes.
((torch.clamp(image, min=0.04045) + 0.055) / 1.055) ** 2.4
)
def linear_to_srgb(image: torch.Tensor) -> torch.Tensor:
return torch.where(
image <= 0.0031308,
image * 12.92,
torch.clamp(1.055 * image ** (1 / 2.4) - 0.055, min=0.0, max=1.0)
)
class ResizeKernel(Enum):
NEAREST = "nearest"
BILINEAR = "bilinear"
CATMULL_ROM = "catmull-rom"
MITCHELL = "mitchell"
B_SPLINE = "b-spline"
LANCZOS2 = "lanczos2"
LANCZOS3 = "lanczos3"
MAGIC_KERNEL = "magic_kernel"
MAGIC_KERNEL_SHARP_2013 = "magic_kernel_sharp_2013"
MAGIC_KERNEL_SHARP_2021 = "magic_kernel_sharp_2021"
class SharpenKernel(Enum):
SHARP_2013 = "sharp_2013"
SHARP_2021 = "sharp_2021"
class QuantHandling(Enum):
TRUNCATE = "truncate"
ROUND = "round"
STOCHASTIC_ROUND = "stochastic_round"
BAYER = "bayer"