o
    oi$                     @   s  U d dl Z d dlmZmZmZmZmZ d dlZd dlm	Z	 d dl
mZmZmZ d dlmZ d dlmZ g dg dg dgZeee  ed	< g d
g dg d
gZeee  ed< 		d#dedededede	f deej deej de	fddZejddfde	deee	f dede	f dee	 dee	 de	fddZddejdddfdeeeeef deee	f deee	f dede	f d eeeef  deej deej de	fd!d"ZdS )$    N)CallableListOptionalTupleUnion)Tensor)KORNIA_CHECKKORNIA_CHECK_IS_TENSORKORNIA_CHECK_SHAPE)normalize_min_max)filter2d)      ?        r   )r   r   r   default_diamond_kernel)r   r   r   default_square_kernel
replicateswidthheight	random_fn.devicedtypereturnc           
      C   s2  t |dkp|dkd d}|dkrd}||}}t |d dko"|dkd || d||g||d}|| dd|d d g||d|d	d
d
dd
d
df< |d	d
d
dd
ddf |d	d
d
ddd
df  jddd |d	ddd
df< |dkr|d	ddddf |d	ddddf  |d	ddddf  |d	ddddf  d |d	ddddf< |d }|d |d  | |d< |d |d  | |d< |d	ddd
df }	|d	ddddf |d	ddd
df  |	 |d	ddd
df< |d	ddddf |d	ddd
df  |	 |d	ddd
df< |d }|r|dd}|S )a  Generate the diamond square image seee.

    Args:
        replicates: the num of batched replicas for the image.
        width: the expected image width.
        height: the expected image height.
        random_fn: the random function to generate the image seed.
        device: the torch device where to create the image seed.
        dtype: the torch dtype where to create the image seed.

    Return:
        the generated image seed of size Bx1xHxW.

       z#Height or Width must be equal to 3.FT      z+Height must be odd and height bigger than 2r   r   .N)dimg      @r   ).r   r   ).r   r   ).r   r   ).r   r   ).r    ).r   r   ).r   r    g      @)r   sum	transpose)
r   r   r   r   r   r   r"   restmp1tmp2 r&   Q/home/ubuntu/.local/lib/python3.10/site-packages/kornia/contrib/diamond_square.py_diamond_square_seed"   s.   
4PN<<r(   imgrandom_scalediamond_kernelsquare_kernelc                 C   s  t | g d |du rttg| }|du rttg| }| j\}}}}tj|dd|d  d d|d  d g| j| j	d}	| |	ddddddddddf< d}
t
|	}|
|dddddddf< |
|dddddddf< |
|dddddddf< |
|dddddddf< ||	 | j| j	d| }t|	|}|dk| j	}|	d| | |  ||  }	t|	|| }|dk| j	}|	||  d| | |  }	|	S )	a  Doubles the image resolution by applying a single diamond square steps.

    Recursive application of this method creates plasma fractals.

    Attention! The function is differentiable and gradients are computed as well.

    If this function is run in the usual sense, it is more efficient if it is run in a no_grad()

    Args:
        img: a 4D tensor where dimensions are Batch, Channel, Width, Height. Width and Height must both be 2^N+1 and
            Batch and Channels should in the usual case be 1.
        random_scale: a float  number in [0,1] controlling the randomness created pixels get. I the usual case, it is
            halved at every application of this function.
        random_fn: the random function to generate the image seed.
        diamond_kernel: the 3x3 kernel to perform the diamond step.
        square_kernel: the 3x3 kernel to perform the square step.

    Return:
        A tensor on the same device as img with the same channels as img and width, height of 2^(N+1)+1.

    )BCHWNr   r   r   gUUUUUU?r   r    )r
   r   r   tor   shapetorchzerosr   r   	ones_likesizer   )r)   r*   r   r+   r,   batch_sz_r   r   new_imgfactorpad_compencate
random_imgdiamond_regionsdiamond_centerssquare_regionssquare_centersr&   r&   r'   _one_diamond_one_square[   s0   *$

rA   g      ?g      ?output_size	roughnessnormalize_rangec                 C   sH  t t| dkd t|ts*t|gggg||}|| d | d  dddg}n t| |dddd}|| d | d ddg}|g d}t|tskt|gggg||}|| d | d  dddg}n|dddd}|| d | d ddg}|g d}| dd \}}d}	| dd D ]}
|	|
9 }	qd	t	
t	|d  d }d	t	
t	|d  d }ttt	|d d t	|d d }|d d	|  d }|d d	|  d }|t|	||||| }|}t|D ]}|| }t|||}q|d
d|d|f }|| }|dur"|\}}t| ||}|S )a8  Generate Plasma Fractal Images using the diamond square algorithm.

    See: https://en.wikipedia.org/wiki/Diamond-square_algorithm

    Args:
        output_size: a tuple of integers with the BxCxHxW of the image to be generated.
        roughness: the scale value to apply at each recursion step.
        random_scale: the initial value of the scale for recursion.
        random_fn: the callable function to use to sample a random tensor.
        normalize_range: whether to normalize using min-max the output map. In case of a
            range is specified, min-max norm is applied between the provided range.
        device: the torch device to place the output map.
        dtype: the torch dtype to place the output map.

    Returns:
        A tensor with shape :math:`(B,C,H,W)` containing the fractal image.

       zoutput_size must be (B,C,H,W)r   r   r    )r    r   r   r   r   Nr   .)r   len
isinstancer   r1   expandr	   viewreshapemathceillog2intminr(   rangerA   r   
contiguous)rB   rC   r*   r   rD   r   r   r   r   num_samplesxp2_width	p2_heightrecursion_depth
seed_widthseed_heightr)   scaler8   min_valmax_valr&   r&   r'   diamond_square   sD   
 
 
*

r\   )NN)rK   typingr   r   r   r   r   r3   kornia.corer   kornia.core.checkr   r	   r
   kornia.enhancer   kornia.filtersr   r   float__annotations__r   rN   r   r   r(   randrA   r\   r&   r&   r&   r'   <module>   s   
&&

<


F


