
    i                      j    d dl mZmZmZ d dlZd dlZd dlZddl	m
Z
 ddlmZ ddlmZ  G d de      Zy)	    )OptionalTupleUnionN   )register_to_config)VaeImageProcessor)PIL_INTERPOLATIONc                       e Zd ZdZe	 	 	 	 	 	 	 	 	 	 	 ddedededeeef   dededed	ed
edede	e
eeeedf   f      f fd       Zdej                  j                  dededej                  j                  fdZ	 	 dde
ej                  j                  ej"                  ej&                  f   de	e   de	e   deeef   fdZ xZS )WanAnimateImageProcessorat  
    Image processor to preprocess the reference (character) image for the Wan Animate model.

    Args:
        do_resize (`bool`, *optional*, defaults to `True`):
            Whether to downscale the image's (height, width) dimensions to multiples of `vae_scale_factor`. Can accept
            `height` and `width` arguments from [`image_processor.VaeImageProcessor.preprocess`] method.
        vae_scale_factor (`int`, *optional*, defaults to `8`):
            VAE (spatial) scale factor. If `do_resize` is `True`, the image is automatically resized to multiples of
            this factor.
        vae_latent_channels (`int`, *optional*, defaults to `16`):
            VAE latent channels.
        spatial_patch_size (`Tuple[int, int]`, *optional*, defaults to `(2, 2)`):
            The spatial patch size used by the diffusion transformer. For Wan models, this is typically (2, 2).
        resample (`str`, *optional*, defaults to `lanczos`):
            Resampling filter to use when resizing the image.
        do_normalize (`bool`, *optional*, defaults to `True`):
            Whether to normalize the image to [-1,1].
        do_binarize (`bool`, *optional*, defaults to `False`):
            Whether to binarize the image to 0/1.
        do_convert_rgb (`bool`, *optional*, defaults to be `False`):
            Whether to convert the images to RGB format.
        do_convert_grayscale (`bool`, *optional*, defaults to be `False`):
            Whether to convert the images to grayscale format.
        fill_color (`str` or `float` or `Tuple[float, ...]`, *optional*, defaults to `None`):
            An optional fill color when `resize_mode` is set to `"fill"`. This will fill the empty space with that
            color instead of filling with data from the image. Any valid `color` argument to `PIL.Image.new` is valid;
            if `None`, will default to filling with data from `image`.
    	do_resizevae_scale_factorvae_latent_channelsspatial_patch_sizeresamplereducing_gapdo_normalizedo_binarizedo_convert_rgbdo_convert_grayscale
fill_color.c                 D    t         |           |	r|
rt        dd      y y )Nz`do_convert_rgb` and `do_convert_grayscale` can not both be set to `True`, if you intended to convert the image into RGB format, please set `do_convert_grayscale = False`.z` if you intended to convert the image into grayscale format, please set `do_convert_rgb = False`)super__init__
ValueError)selfr   r   r   r   r   r   r   r   r   r   r   	__class__s               q/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/diffusers/pipelines/wan/image_processor.pyr   z!WanAnimateImageProcessor.__init__9   s3     	2tr  3>    imagewidthheightreturnc           	      :   ||z  }|j                   |j                  z  }| j                  j                  du }| j                  j                  xs d}||k  r|n|j                   |z  |j                  z  }||k\  r|n|j                  |z  |j                   z  }	|j	                  ||	ft
        | j                  j                           }
t        j                  j                  d||f|      }|j                  |
|dz  |dz  z
  |dz  |	dz  z
  f       |r||k  r|dz  |	dz  z
  }|dkD  rm|j                  |
j	                  ||fdd|df      d       |j                  |
j	                  ||fd|
j                  ||
j                  f      d||	z   f       |S ||kD  r}|dz  |dz  z
  }|dkD  rm|j                  |
j	                  ||fddd|f      d       |j                  |
j	                  ||f|
j                   d|
j                   |f      ||z   df       |S )	a*  
        Resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center
        the image within the dimensions, filling empty with data from image.

        Args:
            image (`PIL.Image.Image`):
                The image to resize and fill.
            width (`int`):
                The width to resize the image to.
            height (`int`):
                The height to resize the image to.

        Returns:
            `PIL.Image.Image`:
                The resized and filled image.
        Nr   )r   RGB)color   )box)r   r   )r    r!   configr   resizer	   r   PILImagenewpaste)r   r   r    r!   ratio	src_ratiofill_with_image_datar   src_wsrc_hresizedresfill_height
fill_widths                 r   _resize_and_fillz)WanAnimateImageProcessor._resize_and_fillP   s:   . KK%,,.	#{{55=[[++0q
*f0D0T9,%,,2F%++2U,,u~8I$++J^J^8_,`iimmEE6?*mE		'
UaZ 71uPQz9QR	Sy $kEQJ6?IIgnne[-A1eUVGWnX^dIeII{';!W^^UZ\c\j\jAkle 34   
 ""aZ%1*4
>IIgnnj&-A1aQWGXnY_eIfII
F';'--QRT[TaTaciAjk'%/3  
 
r   c                 *   |jt        |t        j                  j                        r|j                  }n9t        |t        j
                        r|j                  d   }n|j                  d   }|jt        |t        j                  j                        r|j                  }n9t        |t        j
                        r|j                  d   }n|j                  d   }||z  }||z  }| j                  j                  | j                  j                  d   z  }| j                  j                  | j                  j                  d   z  }t        t        j                  ||z              |z  |z  }t        t        j                  ||z              |z  |z  }||fS )aH  
        Returns the height and width of the image, downscaled to the next integer multiple of `vae_scale_factor`.

        Args:
            image (`Union[PIL.Image.Image, np.ndarray, torch.Tensor]`):
                The image input, which can be a PIL image, NumPy array, or PyTorch tensor. If it is a NumPy array, it
                should have shape `[batch, height, width]` or `[batch, height, width, channels]`. If it is a PyTorch
                tensor, it should have shape `[batch, channels, height, width]`.
            height (`Optional[int]`, *optional*, defaults to `None`):
                The height of the preprocessed image. If `None`, the height of the `image` input will be used.
            width (`Optional[int]`, *optional*, defaults to `None`):
                The width of the preprocessed image. If `None`, the width of the `image` input will be used.

        Returns:
            `Tuple[int, int]`:
                A tuple containing the height and width, both resized to the nearest integer multiple of
                `vae_scale_factor * spatial_patch_size`.
        r&      r   r   )
isinstancer*   r+   r!   torchTensorshaper    r(   r   r   roundnpsqrt)r   r   r!   r    max_areaaspect_ratiomod_value_hmod_value_ws           r   get_default_height_widthz1WanAnimateImageProcessor.get_default_height_width   s>   2 >%1E5<<0QQ=%1E5<<0AA6>~kk22T[[5S5STU5VVkk22T[[5S5STU5VV rwwx,678KG+Ubggh567;FTu}r   )T      )r&   r&   lanczosNTFFFr   )NN)__name__
__module____qualname____doc__r   boolintr   strr   r   floatr   r*   r+   r7   r?   ndarrayr;   r<   rE   __classcell__)r   s   @r   r   r      sh   <   !#%.4! !!$%*EF  !	
 "#s(O      # U3uUCZ/@#@AB ,5yy5 5 	5
 
5t !%#	2SYY__bjj%,,>?2 2 }	2
 
sCx2r   r   )typingr   r   r   numpyr?   	PIL.Imager*   r;   configuration_utilsr   image_processorr   utilsr	   r    r   r   <module>rZ      s.    * )    5 0 &_0 _r   