# LICENSE HEADER MANAGED BY add-license-header
#
# Copyright 2018 Kornia Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from __future__ import annotations

from typing import Optional

import torch
from torch import nn

from kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_IS_TENSOR, KORNIA_CHECK_SAME_SIZE, KORNIA_CHECK_SHAPE
from kornia.core.utils import is_compiling, is_exporting

from .filter import _pad, _to_floating
from .kernels import _check_kernel_size, _unpack_2d_ks, get_gaussian_kernel2d
from .median import _compute_zero_padding


def _check_sigma_batch(name: str, sigma: torch.Tensor, input: torch.Tensor) -> None:
    """Check that a tensor sigma has a batch of 1, shared by the input, or the input batch."""
    KORNIA_CHECK_SAME_SIZE(sigma, input, allow_one=True, msg=f"{name} must have a batch of 1 or the input batch.")


# Measured on one image, 3 x 3 to 9 x 9 kernels, float32 and float64, with no page faults on either side: the tap loop
# overtakes the unfold path between 20k and 36k values (earlier for 3 x 3 kernels), so 65,536 values leaves a margin.
_BILATERAL_CPU_MIN_NUMEL = 65536
# The eager tap loop works through the batch in chunks of about this many input bytes, so that every tap's temporaries
# stay in cache: on 256 x 256 images, 2 MB chunks took 0.3x to 0.7x the time of one pass over a batch of 8 or 32.
_BILATERAL_CPU_CHUNK_BYTES = 2 * 1024 * 1024
# torch.compile unrolls the tap loop into one graph with a block of operations per tap, so its compile time grows with
# the tap count: a 9 x 9 kernel took 56 s to compile against 2.3 s for the unfold path. Larger kernels keep the unfold
# path under compile.
_BILATERAL_CPU_MAX_COMPILED_TAPS = 25


def _records_gradient(
    input: torch.Tensor,
    guidance: Optional[torch.Tensor],
    sigma_color: float | torch.Tensor,
    sigma_space: tuple[float, float] | torch.Tensor,
) -> bool:
    """Whether autograd records a reverse-mode gradient through the filter."""
    return torch.is_grad_enabled() and (
        input.requires_grad
        or (guidance is not None and guidance.requires_grad)
        or (isinstance(sigma_color, torch.Tensor) and sigma_color.requires_grad)
        or (isinstance(sigma_space, torch.Tensor) and sigma_space.requires_grad)
    )


def _bilateral_blur_cpu_eligible(
    input: torch.Tensor,
    guidance: Optional[torch.Tensor],
    sigma_color: float | torch.Tensor,
    sigma_space: tuple[float, float] | torch.Tensor,
) -> bool:
    """Select large forward-only native-precision CPU inputs, where a tap loop beats unfolding every window."""
    # Keep the unfold path where it is what the caller captured or differentiates: the exporters and the legacy tracer
    # export it, and its reverse mode is one unfold backward instead of a chain of per-tap backwards.
    return (
        input.device.type == "cpu"
        and input.dtype in (torch.float32, torch.float64)
        and (guidance is None or guidance.device.type == "cpu")
        # Before the size check: torch.export would turn it into a guard of the exported program, which then fails at
        # run time for an image on the other side of the threshold although its dynamic shapes allow it.
        and not is_exporting()
        and input.numel() >= _BILATERAL_CPU_MIN_NUMEL
        and not torch.jit.is_tracing()
        and not _records_gradient(input, guidance, sigma_color, sigma_space)
    )


def _bilateral_blur_taps(
    image: torch.Tensor,
    guide: Optional[torch.Tensor],
    kernel_size: tuple[int, int],
    color_scale: float | torch.Tensor,
    space_kernel: torch.Tensor,
    border_type: str,
    color_distance_type: str,
    in_place: bool,
    out: Optional[torch.Tensor],
) -> torch.Tensor:
    """Filter one batch chunk tap by tap, reusing its own temporaries ``in_place``, into ``out`` if given."""
    ky, kx = kernel_size
    height, width = image.shape[-2], image.shape[-1]
    pad_y, pad_x = (ky - 1) // 2, (kx - 1) // 2
    # The unfold path returned a contiguous result for any input layout.
    image = image.contiguous()
    padded_image = _pad(image, [pad_x, pad_x, pad_y, pad_y], border_type)
    if guide is None:
        guide, padded_guide = image, padded_image
    else:
        guide = guide.contiguous()
        padded_guide = _pad(guide, [pad_x, pad_x, pad_y, pad_y], border_type)
    if color_distance_type not in ("l1", "l2"):
        raise ValueError("color_distance_type only accepts l1 or l2")

    def tap_weight(tap: int) -> tuple[torch.Tensor, torch.Tensor]:
        # The unfold path's kernel entry for this tap, computed with the same operations in the same order.
        dy, dx = tap // kx, tap % kx
        diff = padded_guide[..., dy : dy + height, dx : dx + width] - guide
        if diff.shape[1] == 1:
            # One channel's sum is the value itself, and |d|^2 == d^2: both distances are the squared difference.
            color_distance_sq = diff.square_() if in_place else diff.square()
        elif color_distance_type == "l1":
            color_distance_sq = (diff.abs_() if in_place else diff.abs()).sum(1, keepdim=True)
            color_distance_sq = color_distance_sq.square_() if in_place else color_distance_sq.square()
        else:
            color_distance_sq = (diff.square_() if in_place else diff.square()).sum(1, keepdim=True)
        space_weight = space_kernel[:, tap : tap + 1]
        if in_place:
            weight = color_distance_sq.mul_(color_scale).exp_().mul_(space_weight)
        else:
            weight = space_weight * (color_scale * color_distance_sq).exp()
        return padded_image[..., dy : dy + height, dx : dx + width], weight

    values, weight = tap_weight(0)
    weighted = values * weight if out is None else out.copy_(values).mul_(weight)
    normalizer = weight
    for tap in range(1, ky * kx):
        values, weight = tap_weight(tap)
        if in_place:
            weighted.addcmul_(values, weight)
            normalizer.add_(weight)
        else:
            weighted = weighted.addcmul(values, weight)
            normalizer = normalizer + weight
    return weighted.div_(normalizer) if in_place else weighted / normalizer


def _bilateral_blur_cpu(
    input: torch.Tensor,
    guidance: Optional[torch.Tensor],
    kernel_size: tuple[int, int] | int,
    sigma_color: float | torch.Tensor,
    sigma_space: tuple[float, float] | torch.Tensor,
    border_type: str,
    color_distance_type: str,
) -> torch.Tensor:
    """Bilateral filter as a loop over the kernel taps, without unfolding every window.

    The unfold path materialises several :math:`(B, C, H, k_y, W, k_x)` tensors, :math:`k_y k_x` times the image; on a
    large CPU batch they are fresh memory, faulted in on every call. Here every tap takes shifted slices of the padded
    guidance and input, computes its weights with the unfold path's expression, so they are the same numbers, and adds
    them to the weighted sum and to the normalizer; only the order of the :math:`k_y k_x` terms of those two sums
    differs. Tensor sigmas have the input's dtype and device and a batch of 1 or the input's.
    """
    ky, kx = _unpack_2d_ks(kernel_size)
    space_kernel = get_gaussian_kernel2d(kernel_size, sigma_space, device=input.device, dtype=input.dtype)
    space_kernel = space_kernel.view(-1, ky * kx, 1, 1)
    color_scale = -0.5 / sigma_color**2
    if isinstance(color_scale, torch.Tensor):
        color_scale = color_scale.view(-1, 1, 1, 1)
    # In place and chunk by chunk only where nothing records the arithmetic: autograd needs the temporaries that the
    # in-place operations overwrite, vmap has no batching rule for addcmul_, and compilers fuse the functional loop over
    # the whole batch without these allocations. Every value is computed the same way in both forms.
    in_place = (
        not is_compiling()
        and not torch._C._are_functorch_transforms_active()
        and not _records_gradient(input, guidance, sigma_color, sigma_space)
    )
    if not in_place:
        return _bilateral_blur_taps(
            input, guidance, (ky, kx), color_scale, space_kernel, border_type, color_distance_type, False, None
        )
    batch = input.shape[0]
    channels = input.shape[1] if guidance is None else max(input.shape[1], guidance.shape[1])
    chunk = max(1, _BILATERAL_CPU_CHUNK_BYTES // (channels * input.shape[-2] * input.shape[-1] * input.element_size()))
    if chunk >= batch:
        return _bilateral_blur_taps(
            input, guidance, (ky, kx), color_scale, space_kernel, border_type, color_distance_type, True, None
        )
    output = torch.empty(input.shape, device=input.device, dtype=input.dtype)
    for start in range(0, batch, chunk):
        stop = min(start + chunk, batch)
        scale = color_scale
        if isinstance(scale, torch.Tensor) and scale.shape[0] != 1:
            scale = scale[start:stop]
        space = space_kernel if space_kernel.shape[0] == 1 else space_kernel[start:stop]
        guide = None if guidance is None else guidance[start:stop]
        _bilateral_blur_taps(
            input[start:stop],
            guide,
            (ky, kx),
            scale,
            space,
            border_type,
            color_distance_type,
            True,
            output[start:stop],
        )
    return output


def _bilateral_blur(
    input: torch.Tensor,
    guidance: Optional[torch.Tensor],
    kernel_size: tuple[int, int] | int,
    sigma_color: float | torch.Tensor,
    sigma_space: tuple[float, float] | torch.Tensor,
    border_type: str = "reflect",
    color_distance_type: str = "l1",
) -> torch.Tensor:
    """Single implementation for both Bilateral Filter and Joint Bilateral Filter."""
    KORNIA_CHECK_IS_TENSOR(input)
    KORNIA_CHECK_SHAPE(input, ["B", "C", "H", "W"])
    if guidance is not None:
        # NOTE: allow guidance and input having different number of channels
        KORNIA_CHECK_IS_TENSOR(guidance)
        KORNIA_CHECK_SHAPE(guidance, ["B", "C", "H", "W"])
        KORNIA_CHECK(
            (guidance.shape[0] == input.shape[0]) and (guidance.shape[-2:] == input.shape[-2:]),
            "guidance and input should have the same batch size and spatial dimensions",
        )

    # integer values are differenced and weighted in float32: in their own dtype uint8 differences wrap (#5155)
    input = _to_floating(input)
    if guidance is not None and not guidance.is_floating_point():
        guidance = guidance.to(input.dtype)

    if isinstance(sigma_color, torch.Tensor):
        KORNIA_CHECK_SHAPE(sigma_color, ["B"])
        _check_sigma_batch("sigma_color", sigma_color, input)
        # `bool()` on a tensor is untraceable by dynamo; skip the data-dependent check under compile.
        if not is_compiling() and not bool((sigma_color > 0).all()):
            KORNIA_CHECK(False, f"sigma_color must be positive. Got {sigma_color}")
        sigma_color = sigma_color.to(device=input.device, dtype=input.dtype)
    elif not sigma_color > 0:
        KORNIA_CHECK(False, f"sigma_color must be positive. Got {sigma_color}")

    if isinstance(sigma_space, torch.Tensor):
        KORNIA_CHECK_SHAPE(sigma_space, ["B", "2"])
        _check_sigma_batch("sigma_space", sigma_space, input)
        # the spatial kernel takes the dtype of a tensor sigma, so match a floating input as for sigma_color
        if input.is_floating_point():
            sigma_space = sigma_space.to(device=input.device, dtype=input.dtype)

    ky, kx = _unpack_2d_ks(kernel_size)
    _check_kernel_size((ky, kx))

    # the colour kernel takes the guidance's dtype, so a wider guidance would promote the output
    if guidance is not None and input.is_floating_point() and guidance.is_floating_point():
        guidance = guidance.to(input.dtype)

    if _bilateral_blur_cpu_eligible(input, guidance, sigma_color, sigma_space) and (
        not is_compiling() or ky * kx <= _BILATERAL_CPU_MAX_COMPILED_TAPS
    ):
        return _bilateral_blur_cpu(
            input, guidance, kernel_size, sigma_color, sigma_space, border_type, color_distance_type
        )

    if isinstance(sigma_color, torch.Tensor):
        sigma_color = sigma_color.view(-1, 1, 1, 1, 1, 1)
    pad_y, pad_x = _compute_zero_padding(kernel_size)

    # Keep both patch axes: flattening them would copy every overlapping window. The windows are viewed in the order of
    # their elements in memory, (B, C, H, Ky, W, Kx), so that the arithmetic allocates every temporary contiguous, and,
    # on MPS, every reduction, here and in the backward, runs over one axis. MPS reduces a permuted 6-D tensor about 150
    # times slower than a contiguous one (an M1: 238 ms against 1.4 ms for the colour distance of an 8 x 3 x 256 x 256
    # batch with a 5 x 5 kernel), and two axes that are not adjacent, such as Ky and Kx, about 140 times slower than one
    # at a time, on torch 2.14 and on a 2.16 nightly alike.
    padded_input = _pad(input, [pad_x, pad_x, pad_y, pad_y], border_type)
    unfolded_input = padded_input.unfold(2, ky, 1).unfold(3, kx, 1).permute(0, 1, 2, 4, 3, 5)  # (B, C, H, Ky, W, Kx)

    if guidance is None:
        guidance = input
        unfolded_guidance = unfolded_input
    else:
        padded_guidance = _pad(guidance, [pad_x, pad_x, pad_y, pad_y], border_type)
        unfolded_guidance = padded_guidance.unfold(2, ky, 1).unfold(3, kx, 1).permute(0, 1, 2, 4, 3, 5)
    # After the padding, whose errors come first, as in the tap loop, and before any window arithmetic.
    if color_distance_type not in ("l1", "l2"):
        raise ValueError("color_distance_type only accepts l1 or l2")

    # The centre pixel is broadcast over Kx by an expand and over Ky by the subtraction, so that the backward sums its
    # gradient over one axis at a time.
    centre = guidance.unsqueeze(-1).expand(-1, -1, -1, -1, kx).unsqueeze(3)  # (B, C, H, 1, W, Kx)
    diff = unfolded_guidance - centre
    if diff.shape[1] == 1:
        # One channel's sum is the value itself, so the sum is skipped: on torch 2.14, MPS reduces over an axis of
        # size 1 about 270 times slower than it squares (222 ms against 0.8 ms). l1 keeps its abs(): |d|^2 is d^2
        # only for a real difference, not for a complex one.
        color_distance_sq = diff.abs().square() if color_distance_type == "l1" else diff.square()
    elif color_distance_type == "l1":
        color_distance_sq = diff.abs().sum(1, keepdim=True).square()
    else:
        color_distance_sq = diff.square().sum(1, keepdim=True)
    del diff

    space_kernel = get_gaussian_kernel2d(kernel_size, sigma_space, device=input.device, dtype=input.dtype)
    space_kernel = space_kernel.view(-1, 1, 1, ky, 1, kx)

    # Each window-sized temporary is freed once consumed. Without a gradient that lowers the peak memory by 27-42 % (an
    # 8 x 3 or 8 x 1 x 256 x 256 batch, 5 x 5 kernel), and a fresh CPU process, whose glibc heap still returns a few MB
    # to the system on every free, faults fewer pages back in per call.
    kernel = space_kernel * (-0.5 / sigma_color**2 * color_distance_sq).exp()  # (B, 1, H, Ky, W, Kx)
    del color_distance_sq
    # On MPS over Kx, then over Ky, for the speed above. Elsewhere one reduction over both window axes: the two-step sum
    # rounds every partial sum over Kx to a half dtype, which costs float16 and bfloat16 gradients with respect to
    # tensor sigmas their accuracy.
    if input.device.type == "mps":
        return (unfolded_input * kernel).sum(-1).sum(-2) / kernel.sum(-1).sum(-2)
    return (unfolded_input * kernel).sum((-3, -1)) / kernel.sum((-3, -1))


def bilateral_blur(
    input: torch.Tensor,
    kernel_size: tuple[int, int] | int,
    sigma_color: float | torch.Tensor,
    sigma_space: tuple[float, float] | torch.Tensor,
    border_type: str = "reflect",
    color_distance_type: str = "l1",
) -> torch.Tensor:
    r"""Smooth an image, keeping its strong edges, with a bilateral filter.

    ``out = bilateral_blur(input, kernel_size, sigma_color, sigma_space)`` averages each pixel's window of a
    ``(B, C, H, W)`` image with weights that fall with the distance in space and in colour, so a neighbour across a
    strong edge counts little. The output has the input's shape.

    .. image:: _static/img/bilateral_blur.png

    Convention:
        - A neighbour weighs :math:`g \, \exp(-d^2 / (2 \sigma_{color}^2))`. :math:`g` is the Gaussian of
          :func:`~kornia.filters.gaussian_blur2d` with ``sigma = sigma_space``, :math:`(\sigma_y, \sigma_x)`, over
          the whole ``kernel_size`` rectangle, and :math:`d` is the neighbour's colour distance from the centre
          pixel: the sum of the absolute channel differences for ``'l1'``, their Euclidean norm for ``'l2'``.
          :ref:`Filtering <filtering-conventions>` compares this with OpenCV.
        - ``sigma_color`` is in the units of the input values: an image scaled by ``s > 0``, filtered with
          ``sigma_color * s``, gives the result scaled by ``s``.
        - The border modes are :func:`~kornia.filters.filter2d`'s, but only in lower case; see its Convention block.
        - A floating input keeps its dtype: a tensor ``sigma_color`` or ``sigma_space`` is cast to it and moved to
          its device.
        - An integer or bool input is differenced and averaged in float32 and the output is float32, as in
          :func:`~kornia.filters.filter2d`, so uint8 differences do not wrap.
        - On the CPU, a float32 or float64 input (after that conversion) with at least 65,536 values is filtered tap
          by tap from shifted slices of the padded input instead of from its unfolded windows, which avoids
          temporaries of ``kernel_size`` times the image, unless a reverse-mode gradient is recorded through any
          argument, or ``torch.export`` or ``torch.jit.trace`` captures the call, so an exported program keeps the
          windows for every input size. Under ``torch.compile``, only kernels of up to 25 taps are filtered tap by
          tap, since the compiled loop has a block of operations per tap. Both forms compute the same weights and add
          up the weighted values in a different order, so they agree to rounding.
        - On MPS, each window is summed over its columns and then over its rows, because MPS reduces two non-adjacent
          axes much more slowly than one. In float16 and bfloat16 that rounds every partial sum, so there the
          gradients with respect to a tensor ``sigma_color`` or ``sigma_space`` are less accurate than on the CPU or
          CUDA. Tensor sigmas are cast to the input's dtype, so keeping them in float32 does not help; pass a float32
          input on MPS when those gradients matter.

    Args:
        input: image with shape ``(B, C, H, W)``.
        kernel_size: window ``(kH, kW)`` in pixels, or one integer for both; each a positive odd integer.
        sigma_color: standard deviation of the colour weight, positive; smaller values keep more
          edges. A float or a ``(1,)`` tensor, shared by the batch, or a ``(B,)`` tensor, one value per sample.
        sigma_space: standard deviation ``(sigma_y, sigma_x)`` of the spatial weight in pixels, as ``sigma`` of
          :func:`gaussian_blur2d`. A tuple of floats, or a tensor with shape ``(1, 2)``, shared by the batch, or
          ``(B, 2)``, one row per sample.
        border_type: padding mode, ``'constant'``, ``'reflect'``, ``'replicate'`` or ``'circular'``.
        color_distance_type: ``'l1'`` or ``'l2'`` colour distance.

    Returns:
        the smoothed image with shape ``(B, C, H, W)``.

    Raises:
        BaseError: if an entry of ``kernel_size`` is even or not positive.
        BaseError: if ``sigma_color`` is not positive.
        BaseError: if the batch of a tensor ``sigma_color`` or ``sigma_space`` is neither 1 nor the input batch.

    See Also:
        - :class:`BilateralBlur`: the module form.
        - :func:`joint_bilateral_blur`: the colour distance comes from a second image.
        - :func:`guided_blur`: keeps edges by fitting a linear model to a guidance image, with ``eps`` in place of
          the two sigmas.
        - :func:`gaussian_blur2d`: the spatial weight alone, which blurs across edges.

    Example:
        >>> input = torch.rand(2, 4, 5, 5)
        >>> output = bilateral_blur(input, (3, 3), 0.1, (1.5, 1.5))
        >>> output.shape
        torch.Size([2, 4, 5, 5])

    """
    return _bilateral_blur(input, None, kernel_size, sigma_color, sigma_space, border_type, color_distance_type)


def joint_bilateral_blur(
    input: torch.Tensor,
    guidance: torch.Tensor,
    kernel_size: tuple[int, int] | int,
    sigma_color: float | torch.Tensor,
    sigma_space: tuple[float, float] | torch.Tensor,
    border_type: str = "reflect",
    color_distance_type: str = "l1",
) -> torch.Tensor:
    r"""Smooth an image, keeping the edges found in a second, guidance image.

    ``out = joint_bilateral_blur(input, guidance, kernel_size, sigma_color, sigma_space)`` is :func:`bilateral_blur`
    with the colour distance measured in ``guidance``, a ``(B, C_g, H, W)`` image, while ``input``, a
    ``(B, C, H, W)`` image, is what gets averaged. The output has the shape of ``input``.

    .. image:: _static/img/joint_bilateral_blur.png

    Convention:
        - See the Convention block on :func:`~kornia.filters.bilateral_blur` for the weights, the borders and the
          dtypes.
        - ``input`` comes first and ``guidance`` second, the opposite of :func:`~kornia.filters.guided_blur`.
        - ``guidance`` may have its own channel count; its batch size and ``(H, W)`` must equal ``input``'s.
        - ``guidance`` is cast to the dtype of a floating ``input``, so the output keeps ``input``'s dtype. An integer
          ``input`` is filtered in float32, as by :func:`~kornia.filters.bilateral_blur`, and its ``guidance`` is
          cast to float32.

    Args:
        input: image to average, with shape ``(B, C, H, W)``.
        guidance: image that gives the colour distance, with shape ``(B, C_g, H, W)``.
        kernel_size: window ``(kH, kW)`` in pixels, or one integer for both; each a positive odd integer.
        sigma_color: standard deviation of the colour weight, positive; smaller values keep more
          edges. A float or a ``(1,)`` tensor, shared by the batch, or a ``(B,)`` tensor, one value per sample.
        sigma_space: standard deviation ``(sigma_y, sigma_x)`` of the spatial weight in pixels, as ``sigma`` of
          :func:`gaussian_blur2d`. A tuple of floats, or a tensor with shape ``(1, 2)``, shared by the batch, or
          ``(B, 2)``, one row per sample.
        border_type: padding mode, ``'constant'``, ``'reflect'``, ``'replicate'`` or ``'circular'``.
        color_distance_type: ``'l1'`` or ``'l2'`` colour distance.

    Returns:
        the smoothed image with shape ``(B, C, H, W)``.

    Raises:
        BaseError: if an entry of ``kernel_size`` is even or not positive.
        BaseError: if ``sigma_color`` is not positive.
        BaseError: if the batch of a tensor ``sigma_color`` or ``sigma_space`` is neither 1 nor the input batch.

    See Also:
        - :class:`JointBilateralBlur`: the module form.
        - :func:`bilateral_blur`: the colour distance comes from ``input`` itself.
        - :func:`guided_blur`: takes ``guidance`` first and ``eps`` in place of the two sigmas.

    Example:
        >>> input = torch.rand(2, 4, 5, 5)
        >>> guidance = torch.rand(2, 4, 5, 5)
        >>> output = joint_bilateral_blur(input, guidance, (3, 3), 0.1, (1.5, 1.5))
        >>> output.shape
        torch.Size([2, 4, 5, 5])

    """
    return _bilateral_blur(input, guidance, kernel_size, sigma_color, sigma_space, border_type, color_distance_type)


# trick to make mypy not throw errors about difference in .forward() signatures of subclass and superclass
class _BilateralBlur(nn.Module):
    def __init__(
        self,
        kernel_size: tuple[int, int] | int,
        sigma_color: float | torch.Tensor,
        sigma_space: tuple[float, float] | torch.Tensor,
        border_type: str = "reflect",
        color_distance_type: str = "l1",
    ) -> None:
        super().__init__()
        _check_kernel_size(_unpack_2d_ks(kernel_size))
        self.kernel_size = kernel_size
        self.sigma_color = sigma_color
        self.sigma_space = sigma_space
        self.border_type = border_type
        self.color_distance_type = color_distance_type

    def __repr__(self) -> str:
        return (
            f"{self.__class__.__name__}"
            f"(kernel_size={self.kernel_size}, "
            f"sigma_color={self.sigma_color}, "
            f"sigma_space={self.sigma_space}, "
            f"border_type={self.border_type}, "
            f"color_distance_type={self.color_distance_type})"
        )


class BilateralBlur(_BilateralBlur):
    r"""Smooth an image, keeping its strong edges, with stored bilateral parameters (module form).

    ``blur = BilateralBlur(kernel_size, sigma_color, sigma_space)`` stores its arguments, and ``out = blur(input)``
    is :func:`bilateral_blur` on a ``(B, C, H, W)`` image.

    Convention:
        See the Convention block on :func:`~kornia.filters.bilateral_blur`.

    Args:
        kernel_size: window ``(kH, kW)`` in pixels, or one integer for both.
        sigma_color: standard deviation of the colour weight, as in :func:`bilateral_blur`.
        sigma_space: standard deviation ``(sigma_y, sigma_x)`` of the spatial weight in pixels.
        border_type: padding mode, as in :func:`bilateral_blur`.
        color_distance_type: ``'l1'`` or ``'l2'`` colour distance.

    Shape:
        - Input: ``(B, C, H, W)``
        - Output: ``(B, C, H, W)``

    Raises:
        BaseError: if an entry of ``kernel_size`` is even or not positive; raised from the constructor.
        BaseError: if ``sigma_color`` is not positive; raised from ``forward``.
        BaseError: if the batch of a tensor ``sigma_color`` or ``sigma_space`` is neither 1 nor the input batch;
          raised from ``forward``.

    See Also:
        - :func:`bilateral_blur`: the one-call form.
        - :class:`JointBilateralBlur`: the colour distance comes from a second image.

    Example:
        >>> input = torch.rand(2, 4, 5, 5)
        >>> blur = BilateralBlur((3, 3), 0.1, (1.5, 1.5))
        >>> output = blur(input)
        >>> output.shape
        torch.Size([2, 4, 5, 5])

    """

    def forward(self, input: torch.Tensor) -> torch.Tensor:
        """Smooth an image while keeping strong intensity edges visible.

        Bilateral filtering combines two weights for every pixel in the local
        window: a spatial weight based on distance in the image plane and a
        range weight based on color or intensity similarity. Nearby pixels with
        similar values contribute strongly, while pixels across an edge are
        suppressed even if they are spatially close.

        Args:
            input: Input image tensor with shape ``(B, C, H, W)``,
                where :math:`B` is the batch size, :math:`C` is the number of
                channels, :math:`H` is the image height, and :math:`W` is the
                image width.

        Returns:
            Tensor with shape ``(B, C, H, W)`` containing the
            edge-preserving smoothed image. The output keeps the layout and
            device of ``input``, and the dtype of a floating ``input`` unless a
            tensor ``sigma_space`` of a wider dtype promotes it, while reducing
            small local variations according to the configured kernel size and
            sigma values.
        """
        return bilateral_blur(
            input, self.kernel_size, self.sigma_color, self.sigma_space, self.border_type, self.color_distance_type
        )


class JointBilateralBlur(_BilateralBlur):
    r"""Smooth an image, keeping the edges found in a second image (module form).

    ``blur = JointBilateralBlur(kernel_size, sigma_color, sigma_space)`` stores its arguments, and
    ``out = blur(input, guidance)`` is :func:`joint_bilateral_blur` on a ``(B, C, H, W)`` image and a
    ``(B, C_g, H, W)`` guidance.

    Convention:
        See the Convention block on :func:`~kornia.filters.joint_bilateral_blur`.

    Args:
        kernel_size: window ``(kH, kW)`` in pixels, or one integer for both.
        sigma_color: standard deviation of the colour weight, as in :func:`bilateral_blur`.
        sigma_space: standard deviation ``(sigma_y, sigma_x)`` of the spatial weight in pixels.
        border_type: padding mode, as in :func:`bilateral_blur`.
        color_distance_type: ``'l1'`` or ``'l2'`` colour distance.

    Shape:
        - Input: ``(B, C, H, W)``, ``(B, C_g, H, W)``
        - Output: ``(B, C, H, W)``

    Raises:
        BaseError: if an entry of ``kernel_size`` is even or not positive; raised from the constructor.
        BaseError: if ``sigma_color`` is not positive; raised from ``forward``.
        BaseError: if the batch of a tensor ``sigma_color`` or ``sigma_space`` is neither 1 nor the input batch;
          raised from ``forward``.

    See Also:
        - :func:`joint_bilateral_blur`: the one-call form.
        - :class:`BilateralBlur`: the colour distance comes from the input itself.

    Example:
        >>> input = torch.rand(2, 4, 5, 5)
        >>> guidance = torch.rand(2, 4, 5, 5)
        >>> blur = JointBilateralBlur((3, 3), 0.1, (1.5, 1.5))
        >>> output = blur(input, guidance)
        >>> output.shape
        torch.Size([2, 4, 5, 5])

    """

    def forward(self, input: torch.Tensor, guidance: torch.Tensor) -> torch.Tensor:
        """Smooth an input image using edges measured from a guidance image.

        Joint bilateral filtering is useful when the image being smoothed and
        the image that should define the edges are different tensors. The
        spatial kernel is applied around each location of ``input``, but the
        range similarity term is computed from ``guidance``. This lets a clean
        or higher-quality reference image preserve boundaries in another
        signal, such as a depth map, mask, or noisy feature image.

        Args:
            input: Tensor to smooth with shape ``(B, C, H, W)``, where
                :math:`B` is the batch size, :math:`C` is the number of input
                channels, :math:`H` is the height, and :math:`W` is the width.
            guidance: Tensor used to compute range weights. Its batch and
                spatial dimensions must equal those of ``input``; its
                channel count may differ when the guidance signal uses a
                different representation.

        Returns:
            Tensor with shape ``(B, C, H, W)`` containing the filtered
            ``input`` values. Edges present in ``guidance`` reduce mixing
            across boundaries, while smooth regions are averaged more strongly.
        """
        return joint_bilateral_blur(
            input,
            guidance,
            self.kernel_size,
            self.sigma_color,
            self.sigma_space,
            self.border_type,
            self.color_distance_type,
        )
