Skip to content

Utils

utils

Additional utility functions.

Functions:

Attributes:

CUDA_AVAILABLE module-attribute

CUDA_AVAILABLE = True

Whether cuda is available through cupy.

apply_lut_tetrahedral_int

apply_lut_tetrahedral_int(
    image: ndarray, lut, bit_depth=16, out_bit_depth=8
) -> ndarray

Apply a 3D LUT using tetrahedral interpolation.

The input image is expected to have dtype uint16 with values in the range [0, 65535]. The output image has dtype uint8 with values in the range [0, 255].

Parameters:

  • image

    (ndarray) –

    Input image of shape (H, W, 3), dtype uint16.

  • lut

    3D lookup table of shape (size, size, size, 3), dtype uint8.

Returns:

  • ndarray

    Output image of shape (H, W, 3), dtype uint8.

apply_lut_tetrahedral_int_cuda

apply_lut_tetrahedral_int_cuda(image, lut, out, size, scale, scale_out)

CUDA kernel: Apply a 3D LUT with tetrahedral interpolation. Input : uint16 image in [0, 65535] Output: uint8 image in [0, 255]

apply_per_pixel

apply_per_pixel(rgb, function)

Convert a 3D (H, W, 3) or 4D (N, H, W, 3) RGB array to HSV. RGB values are expected in range [0, 1]. Returns array of same shape with HSV values.

H in [0, 1], S in [0, 1], V in [0, 1].

construct_spectral_density

construct_spectral_density(ref_density: SpectralDistribution, sigma=25) -> ndarray

Split single density curve into separate layers using local extrema.

create_lut

create_lut(
    negative_film,
    print_film=None,
    lut_size=33,
    name="test",
    cube=True,
    verbose=False,
    **kwargs,
)

Creates a cube LUT from using .film_spectral.FilmSpectral.generate_conversion.

generate_all_summing_to_one

generate_all_summing_to_one(steps)

Generate all (a, b, c) such that a + b + c = 1 and a, b, c ∈ [0, 1] with a specified number of steps (granularity).

Parameters:

  • steps

    (int) –

    The number of divisions of the interval [0, 1].

Returns:

  • np.ndarray: Array of shape (n, 3) with all combinations summing to 1.

multi_channel_interp

multi_channel_interp(
    x,
    xps,
    fps,
    num_bins=1024,
    interpolate=False,
    left_extrapolate=False,
    right_extrapolate=False,
)

Resamples each (xp, fp) pair to a uniform grid for fast lookup.

Returns:

  • xp_common

    np.ndarray, shape (num_bins,)

  • fp_uniform

    np.ndarray, shape (n_channels, num_bins)

plot_chromaticity

plot_chromaticity(chromaticity, label=None)

Plot a chromaticity value.

plot_chromaticties

plot_chromaticties(chromaticies, labels=None)

Plot a chromaticity values.

plot_gamut

plot_gamut(rgb_to_xyz: ndarray, label=None)

Plots the gamut of a CST matrix.

plot_gamuts

plot_gamuts(rgb_to_xyz: list[ndarray], labels=None)

Plots the gamut of CST matrices.

run_lut_cuda

run_lut_cuda(image: ndarray, lut: ndarray, exponent=16, out_exponent=8) -> ndarray

Wrapper: runs the CUDA kernel on an image + LUT.

uniform_multi_channel_interp

uniform_multi_channel_interp(
    x: ndarray,
    xp_common: ndarray,
    fp_uniform: ndarray,
    interpolate=True,
    left_extrapolate=False,
    right_extrapolate=False,
) -> ndarray

Interpolate values in an N-D array over the last dimension.

Interpolates values in an N-dimensional array x across the last dimension (channels) using a precomputed uniform grid defined by xp_common and fp_uniform. Optionally performs linear interpolation or nearest-neighbor selection, and supports linear extrapolation on either end of the grid.

Parameters:

  • x

    Input array of shape (..., channels).

  • xp_common

    (ndarray) –

    Monotonically increasing 1D array of shape (num_bins,) representing the shared grid points.

  • fp_uniform

    (ndarray) –

    Array of shape (channels, num_bins) containing function values at each grid point for every channel.

  • interpolate

    If True, perform linear interpolation. If False, use nearest-neighbor selection.

  • left_extrapolate

    If True, apply linear extrapolation for values less than xp_common[0].

  • right_extrapolate

    If True, apply linear extrapolation for values greater than xp_common[-1].

Returns:

  • ndarray

    Interpolated array with the same shape as x.

wavelength_argmax

wavelength_argmax(distribution: SpectralDistribution, low=None, high=None) -> int

Gets the argmax of a spectral distribution.

wavelength_argmin

wavelength_argmin(distribution: SpectralDistribution, low=None, high=None) -> int

Gets the argmax of a spectral distribution.