File size: 4,406 Bytes
532466c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""Luminance computation, relative-field helpers, and surface inpainting."""

from __future__ import annotations

import cv2
import numpy as np

from config.lighting import (
    SHADE_MAP_MAX,
    SHADE_MAP_MIN,
)


# ---------------------------------------------------------------------------
# Encoding / Decoding
# ---------------------------------------------------------------------------

def encode_relative_map(values: np.ndarray, minimum: float, maximum: float) -> np.ndarray:
    clipped = np.clip(values, minimum, maximum)
    scale = 255.0 / max(maximum - minimum, 1e-6)
    return np.round((clipped - minimum) * scale).clip(0, 255).astype(np.uint8)


def decode_relative_map(encoded: np.ndarray, minimum: float, maximum: float) -> np.ndarray:
    return minimum + (encoded.astype(np.float32) / 255.0) * (maximum - minimum)


# ---------------------------------------------------------------------------
# Luminance
# ---------------------------------------------------------------------------

def image_luminance(img_np: np.ndarray) -> np.ndarray:
    return (
        img_np[:, :, 0].astype(np.float32) * 0.299
        + img_np[:, :, 1].astype(np.float32) * 0.587
        + img_np[:, :, 2].astype(np.float32) * 0.114
    )


def image_linear_luminance(img_np: np.ndarray) -> np.ndarray:
    """Return linear-light luminance in the same 0..255 scale as image_luminance."""
    srgb = np.clip(img_np.astype(np.float32) / 255.0, 0.0, 1.0)
    linear = np.where(
        srgb <= 0.04045,
        srgb / 12.92,
        np.power((srgb + 0.055) / 1.055, 2.4),
    )
    luminance = (
        linear[:, :, 0] * 0.2126
        + linear[:, :, 1] * 0.7152
        + linear[:, :, 2] * 0.0722
    )
    return luminance * 255.0


# ---------------------------------------------------------------------------
# Surface inpainting
# ---------------------------------------------------------------------------

def inpaint_surface_values(values: np.ndarray, mask: np.ndarray, fill_value: float) -> np.ndarray:
    h, w = mask.shape[:2]
    filled = values.astype(np.float32).copy()
    filled[mask == 0] = fill_value
    missing = (mask == 0).astype(np.uint8) * 255
    try:
        source = np.clip(filled, 0, 255).astype(np.uint8)
        filled = cv2.inpaint(
            source,
            missing,
            max(3, min(h, w) // 160),
            cv2.INPAINT_TELEA,
        ).astype(np.float32)
    except cv2.error:
        pass
    return filled


# ---------------------------------------------------------------------------
# Gaussian blur helpers
# ---------------------------------------------------------------------------

def masked_gaussian_blur(
    values: np.ndarray,
    surface_mask: np.ndarray,
    sigma: float,
) -> np.ndarray:
    mask = surface_mask.astype(np.float32)
    numerator = cv2.GaussianBlur(values.astype(np.float32) * mask, (0, 0), sigmaX=sigma, sigmaY=sigma)
    denominator = cv2.GaussianBlur(mask, (0, 0), sigmaX=sigma, sigmaY=sigma)
    return numerator / np.maximum(denominator, 1e-4)


# ---------------------------------------------------------------------------
# Field normalization
# ---------------------------------------------------------------------------

def normalize_relative_field(values: np.ndarray, surface_mask: np.ndarray) -> np.ndarray | None:
    relative = values.astype(np.float32).copy()
    valid = relative[surface_mask > 0]
    valid = valid[np.isfinite(valid) & (valid > 1e-6)]
    if valid.size == 0:
        return None
    median = float(np.median(valid))
    if median < 1e-6:
        return None
    relative /= median
    relative[~np.isfinite(relative)] = 1.0
    relative[surface_mask == 0] = 1.0
    return relative


# ---------------------------------------------------------------------------
# Broad luminance relative
# ---------------------------------------------------------------------------

def broad_luminance_relative(
    img_np: np.ndarray,
    surface_mask: np.ndarray,
) -> np.ndarray | None:
    luminance = image_linear_luminance(img_np) / 255.0
    floor_values = luminance[surface_mask > 0]
    floor_values = floor_values[np.isfinite(floor_values) & (floor_values > 1e-6)]
    if floor_values.size == 0:
        return None
    sigma = max(12.0, min(surface_mask.shape[:2]) / 32.0)
    broad = masked_gaussian_blur(luminance, surface_mask, sigma)
    return normalize_relative_field(broad, surface_mask)