a
    dQ                     @   sB   d dl Z d dlZd dlZdd ZdddZddd	ZdddZdS )    Nc                 C   sp   |   } | jdv rZ| jd | jd  }}|| ||  }}| d|| d|| df } ntd| j d| S )zMod crop images, used during testing.

    Args:
        img (ndarray): Input image.
        scale (int): Scale factor.

    Returns:
        ndarray: Result image.
    )      r      N.zWrong img ndim: .)copyndimshape
ValueError)imgscalehwZh_remainderZw_remainder r   `/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/basicsr/data/transforms.pymod_crop   s    

 r   c           
         s   t | ts| g} t |ts |g}t| d r2dnd}|dkrp|d  dd \}}| d  dd \}}	n,|d jdd \}}| d jdd \}}	 | ||| ks|	|| krtd| d|	 d	| d
d| d| d|k s|k r"td| d| d d d| dtd| td| |dkrdfdd|D }nfdd|D }t	| t	|  |dkr fdd| D } n fdd| D } t
| dkr| d } t
|dkr|d }| |fS )ae  Paired random crop. Support Numpy array and Tensor inputs.

    It crops lists of lq and gt images with corresponding locations.

    Args:
        img_gts (list[ndarray] | ndarray | list[Tensor] | Tensor): GT images. Note that all images
            should have the same shape. If the input is an ndarray, it will
            be transformed to a list containing itself.
        img_lqs (list[ndarray] | ndarray): LQ images. Note that all images
            should have the same shape. If the input is an ndarray, it will
            be transformed to a list containing itself.
        gt_patch_size (int): GT patch size.
        scale (int): Scale factor.
        gt_path (str): Path to ground-truth. Default: None.

    Returns:
        list[ndarray] | ndarray: GT images and LQ images. If returned results
            only have one element, just return ndarray.
    r   ZTensorZNumpyNr   zScale mismatches. GT (z, z	) is not zx zmultiplication of LQ (z).zLQ (z) is smaller than patch size (z). Please remove r   c              	      s4   g | ],}|d d d d     f qS Nr   .0vleftlq_patch_sizetopr   r   
<listcomp>M       z&paired_random_crop.<locals>.<listcomp>c                    s*   g | ]"}|    d f qS .r   r   r   r   r   r   O   r   c              	      s4   g | ],}|d d d d     f qS r   r   r   gt_patch_sizeleft_gttop_gtr   r   r   T   r   c                    s*   g | ]"}|    d f qS r   r   r   r   r   r   r   V   r   r   )
isinstancelisttorchZ	is_tensorsizer   r	   randomrandintintlen)
Zimg_gtsZimg_lqsr   r   Zgt_pathZ
input_typeZh_lqZw_lqZh_gtZw_gtr   )r   r   r   r   r   r    r   paired_random_crop   sJ    




r)   TFc                    s   ot   dk |ot   dk |o.t   dk fdd fddt| ts`| g}  fdd| D } t| dkr| d	 } |d
urt|ts|g}fdd|D }t|dkr|d	 }| |fS |r| ffS | S d
S )am  Augment: horizontal flips OR rotate (0, 90, 180, 270 degrees).

    We use vertical flip and transpose for rotation implementation.
    All the images in the list use the same augmentation.

    Args:
        imgs (list[ndarray] | ndarray): Images to be augmented. If the input
            is an ndarray, it will be transformed to a list.
        hflip (bool): Horizontal flip. Default: True.
        rotation (bool): Ratotation. Default: True.
        flows (list[ndarray]: Flows to be augmented. If the input is an
            ndarray, it will be transformed to a list.
            Dimension is (h, w, 2). Default: None.
        return_status (bool): Return the status of flip and rotation.
            Default: False.

    Returns:
        list[ndarray] | ndarray: Augmented images and flows. If returned
            results only have one element, just return ndarray.

    g      ?c                    s:    rt | d|  r$t | d|  r6| ddd} | S )Nr   r   r   cv2ZflipZ	transpose)r
   hfliprot90vflipr   r   _augmentx   s    zaugment.<locals>._augmentc                    s    r0t | d|  | d d d d df  d9  < r`t | d|  | d d d d df  d9  < r| ddd} | d d d d ddgf } | S )Nr   r   r   r*   )flowr,   r   r   _augment_flow   s    zaugment.<locals>._augment_flowc                    s   g | ]} |qS r   r   )r   r
   )r0   r   r   r      r   zaugment.<locals>.<listcomp>r   r   Nc                    s   g | ]} |qS r   r   )r   r2   )r3   r   r   r      r   )r%   r!   r"   r(   )Zimgsr-   ZrotationZflowsZreturn_statusr   )r0   r3   r-   r.   r/   r   augment^   s(    	

r4         ?c                 C   sN   | j dd \}}|du r*|d |d f}t|||}t| |||f}|S )a  Rotate image.

    Args:
        img (ndarray): Image to be rotated.
        angle (float): Rotation angle in degrees. Positive values mean
            counter-clockwise rotation.
        center (tuple[int]): Rotation center. If the center is None,
            initialize it as the center of the image. Default: None.
        scale (float): Isotropic scale factor. Default: 1.0.
    Nr   )r   r+   ZgetRotationMatrix2DZ
warpAffine)r
   Zanglecenterr   r   r   ZmatrixZrotated_imgr   r   r   
img_rotate   s    r7   )N)TTNF)Nr5   )r+   r%   r#   r   r)   r4   r7   r   r   r   r   <module>   s   
D
C