o
    €o™i‡  ã                   @   s’   d dl mZmZ d dlmZ d dlmZ d dlmZm	Z	 ddedeee
df  d	efd
d„Zded	efdd„ZG dd„ deƒZG dd„ deƒZdS )é    )ÚOptionalÚTuple)ÚImageModule)ÚTensor)ÚKORNIA_CHECKÚKORNIA_CHECK_SHAPENÚinputÚdim.Úreturnc                 C   s`   t | ddgƒ |du rd}tt|ƒdkdƒ tt|ƒt| jƒkdƒ | }|D ]}| |¡}q&|S )a  Calculate integral of the input tensor.

    The algorithm computes the integral image by summing over the specified dimensions.

    In case dim is specified, the contained dimensions must be unique and sorted in ascending order
    and not exceed the number of dimensions of the input tensor.

    Args:
        input: the input tensor with shape :math:`(*, D)`. Where D is the number of dimensions.
        dim: the dimension to be summed.

    Returns:
        Integral tensor for the input tensor with shape :math:`(*, D)`.

    Examples:
        >>> input = torch.ones(3, 5)
        >>> output = integral_tensor(input, (-2, -1))
        >>> output
        tensor([[ 1.,  2.,  3.,  4.,  5.],
                [ 2.,  4.,  6.,  8., 10.],
                [ 3.,  6.,  9., 12., 15.]])

    Ú*ÚDN)éÿÿÿÿr   zdim must be a non-empty tuple.z-dim must be a tuple of length <= input.shape.)r   r   ÚlenÚshapeÚcumsum)r   r	   ÚoutputÚi© r   úK/home/ubuntu/.local/lib/python3.10/site-packages/kornia/enhance/integral.pyÚintegral_tensor   s   r   Úimagec                 C   s   t | g d¢ƒ t| dƒS )at  Calculate integral of the input image tensor.

    This particular version sums over the last two dimensions.

    Args:
        image: the input image tensor with shape :math:`(*, H, W)`.

    Returns:
        Integral tensor for the input image tensor with shape :math:`(*, H, W)`.

    Examples:
        >>> input = torch.ones(1, 5, 5)
        >>> output = integral_image(input)
        >>> output
        tensor([[[ 1.,  2.,  3.,  4.,  5.],
                 [ 2.,  4.,  6.,  8., 10.],
                 [ 3.,  6.,  9., 12., 15.],
                 [ 4.,  8., 12., 16., 20.],
                 [ 5., 10., 15., 20., 25.]]])

    )r   ÚHÚW)éþÿÿÿr   )r   r   )r   r   r   r   Úintegral_image?   s   
r   c                       sJ   e Zd ZdZddeeedf  ddf‡ fdd„Zdedefd	d
„Z	‡  Z
S )ÚIntegralTensora=  Calculates integral of the input tensor.

    Args:
        image: the input tensor with shape :math:`(B,C,H,W)`.

    Returns:
        Integral tensor for the input tensor with shape :math:`(B,C,H,W)`.

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

    Examples:
        >>> input = torch.ones(3, 5)
        >>> dim = (-2, -1)
        >>> output = IntegralTensor(dim)(input)
        >>> output
        tensor([[ 1.,  2.,  3.,  4.,  5.],
                [ 2.,  4.,  6.,  8., 10.],
                [ 3.,  6.,  9., 12., 15.]])

    Nr	   .r
   c                    s   t ƒ  ¡  || _d S ©N)ÚsuperÚ__init__r	   )Úselfr	   ©Ú	__class__r   r   r   r   s   

zIntegralTensor.__init__r   c                 C   s   t || jƒS r   )r   r	   ©r   r   r   r   r   Úforwardv   s   zIntegralTensor.forwardr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Úintr   r   r#   Ú__classcell__r   r   r    r   r   Z   s    $r   c                   @   s"   e Zd ZdZdedefdd„ZdS )ÚIntegralImageaÏ  Calculates integral of the input image tensor.

    This particular version sums over the last two dimensions.

    Args:
        image: the input image tensor with shape :math:`(B,C,H,W)`.

    Returns:
        Integral tensor for the input image tensor with shape :math:`(B,C,H,W)`.

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

    Examples:
        >>> input = torch.ones(1, 5, 5)
        >>> output = IntegralImage()(input)
        >>> output
        tensor([[[ 1.,  2.,  3.,  4.,  5.],
                 [ 2.,  4.,  6.,  8., 10.],
                 [ 3.,  6.,  9., 12., 15.],
                 [ 4.,  8., 12., 16., 20.],
                 [ 5., 10., 15., 20., 25.]]])

    r   r
   c                 C   s   t |ƒS r   )r   r"   r   r   r   r#   •   s   zIntegralImage.forwardN)r$   r%   r&   r'   r   r#   r   r   r   r   r*   z   s    r*   r   )Útypingr   r   Úkornia.corer   ÚModuler   Úkornia.core.checkr   r   r(   r   r   r   r*   r   r   r   r   Ú<module>   s   $& 