
    sg                        d dl Z d dlmZmZmZmZmZmZmZ d dl	Z
ddlmZmZ ddlmZmZmZmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZ ddl m!Z!m"Z"m#Z#  e#jH                  e%      Z&dZ' e"       r
d dl(Z(d d	l(m)Z) 	 d'd
e*de*dee*   dee*   dee*e*f   f
dZ+	 d(d
e*de*deee,e*f      dee*e*f   fdZ-	 d(de*deee,ef      dee*e*f   fdZ.dedeee
j^                        fdZ0dee   dee   fdZ1	 d(deee
j^                        deee,ef      dee*   fdZ2	 d(de
j^                  dee*e*f   deee,ef      de
j^                  fdZ3	 	 	 d)de
j^                  dee(jh                  jh                     deee,ef      deee,ef      def
dZ5	 d(de
j^                  d e*d!e*d"e*d#e*deee,ef      de
j^                  fd$Z6 G d% d&e      Z7y)*    N)AnyDictIterableListOptionalTupleUnion   )BaseImageProcessorBatchFeature)PaddingModepadto_channel_dimension_formatto_pil_image)IMAGENET_STANDARD_MEANIMAGENET_STANDARD_STDChannelDimension
ImageInputPILImageResamplingget_image_sizeinfer_channel_dimension_formatis_scaled_imageis_valid_imageto_numpy_arrayvalid_imagesvalidate_preprocess_arguments)
TensorTypeis_vision_availableloggingi   )Imageheightwidthmin_lenmax_lenreturnc                     |t        | |      n|}|| z  }|| k\  r|}t        ||z        } | dz  dk7  r(| dz  } n"| |kD  r|} t        | |z        }|dz  dk7  r|dz  }t        | |      } t        ||      }| |fS )a  
    Get the output size of the image after resizing given a dictionary specifying the max and min sizes.
    Args:
        height (`int`):
            Height of the input image.
        width (`int`):
            Width of the input image.
        min_len (`int`, *optional*, defaults to 1):
            Minimum size of the output image.
        max_len (`int`, *optional*, defaults to the maximum size of the image):
            Maximum size of the output image.
    Returns:
        The output size of the image after resizing.
       r      maxint)r!   r"   r#   r$   aspect_ratios        i/var/www/html/venv/lib/python3.12/site-packages/transformers/models/idefics3/image_processing_idefics3.py&_resize_output_size_rescale_to_max_lenr.   1   s    " %,Oc&% G6>LU\)*A:?aKF	%F\)*19>QJE !FwE5=    c                     |t        | |      n|}|| z  }|| k\  r||kD  r|}t        ||z        } n| |kD  r| |kD  r|} t        | |z        }t        | d      } t        |d      }| |fS )a  
    Get the output size of the image after resizing given a dictionary specifying the max and min sizes.
    Args:
        height (`int`):
            Height of the input image.
        width (`int`):
            Width of the input image.
        max_len (`Dict[str, int]`, *optional*, defaults to the maximum size of the image):
            Defines the maximum dimensions of the image.
    Returns:
        The output size of the image after resizing.
    r(   r)   )r!   r"   r$   r,   s       r-   +_resize_output_size_scale_below_upper_boundr1   V   s     %,Oc&% G6>L57?U\)*	%FW,F\)* ^FqME5=r/   resolution_max_sideinput_data_formatc                 v    t        | |      \  }}t        |||      \  }}t        ||t              \  }}||fS )a'  
    Get the output size of the image after resizing given a dictionary specifying the max and min sizes.
    Args:
        image (`np.ndarray`):
            Image to resize.
        resolution_max_side (`int`):
            The longest edge of the image will be resized to this value. The shortest edge will be resized to keep the
            input aspect ratio.
        input_data_format (`ChannelDimension` or `str`):
            The channel dimension format of the input image.
    Returns:
        The output size of the image after resizing.
    channel_dim)r$   )r   r.   r1   MAX_IMAGE_SIZE)imager2   r3   r!   r"   s        r-   get_resize_output_image_sizer9   u   sG    $ #56GHMFE ;65RefMFE?WefMFE5=r/   imagesc                 l   t        |       r| gg} | S t        | t        t        f      r!t	        |       dkD  rt        | d         r| g} | S t        | t        t        f      rLt	        |       dkD  r>t        | d   t        t        f      r%t	        | d         dkD  rt        | d   d         r	 | S t        d      )z
    Convert a single image or a list of images to a list of numpy arrays.

    Args:
        images (`ImageInput`):
            A single image or a list of images.

    Returns:
        A list of numpy arrays.
    r   z]Invalid input type. Must be a single image, a list of images, or a list of batches of images.)r   
isinstancelisttuplelen
ValueError)r:   s    r-   make_list_of_imagesrA      s     f(" M 
FT5M	*s6{Q>RXYZR[C\ M 	6D%=)K!Ovay4-0q	NQ6!9Q<(
 M k
 	
r/   valuesc                 J    t        |  D cg c]  }t        |       c}S c c}w )zO
    Return the maximum value across all indices of an iterable of values.
    )zipr*   )rB   values_is     r-   max_across_indicesrF      s      +.v,7hCM777s    images_listc                     |t        | d   d   d      }t        d      x}}| D ]1  }|D ]*  }t        ||      \  }}t        ||      }t        ||      }, 3 ||fS )zH
    Get the maximum height and width across all images in a batch.
    r   r(   r
      num_channelsz-infr5   )r   floatr   r*   )rG   r3   
max_height	max_widthr:   r8   r!   r"   s           r-   get_max_height_widthrP      s      :;q>!;L[de"6]*J . 	.E*5>OPMFEVZ0JE9-I	..
 	""r/   r8   output_sizec                     t        | |      \  }}t        j                  |t        j                        }d|d|d|f<   |S )a  
    Make a pixel mask for the image, where 1 indicates a valid pixel and 0 indicates padding.
    Args:
        image (`np.ndarray`):
            Image to make the pixel mask for.
        output_size (`Tuple[int, int]`):
            Output size of the mask.
    r5   dtyper(   N)r   npzerosint64)r8   rQ   r3   input_heightinput_widthmasks         r-   make_pixel_maskr[      sF     !/uBS TL+88Krxx0D()D,	$%Kr/   palettedata_formatc                    |t        | d      }||n|}|dnd}t        | ||      } | j                  dk(  r|| j                  |       | j	                  d      }t        j                  d|j                  d      }t        j                  ||      }|j	                  d      }t        j                  |      }t        ||t        j                  	      }|S )
a  
    Converts an image to RGB format.
    Args:
        image (`np.ndarray`):
            The image to convert.
        palette (List[int], *optional*):
            The palette to use if given.
        data_format (ChannelDimension or str, *optional*):
            The channel dimension format for the output image. If not provided, it will be the same as the input image.
        input_data_format (ChannelDimension or str, *optional*):
            The channel dimension format of the input image.
    NrI   rK   P
image_moder3   RGBA)   rc   rc   RGBinput_channel_dim)r   r   mode
putpaletteconvertr    newsizealpha_compositerU   arrayr   r   LAST)	r8   r\   r]   r3   rg   
image_rgba
backgroundrl   output_arrays	            r-   convert_to_rgbrr      s    $  :5yY (3':#K%34D4CTUEzzSW0!v&J6:??ODJ++J
CO%--e4O88O,L.|[\l\q\qrLr/   w1h1w2h2c                     |t        | d      }|t        j                  k(  r| d d ||||f   } | S |t        j                  k(  r| ||||d d f   } | S t	        d      )NrI   rK   !Invalid channel dimension format.)r   r   FIRSTrn   r@   )r8   rs   rt   ru   rv   r]   s         r-   _croprz     s     4US&,,,aB2o& L 
(--	-beRUAo& L <==r/   c            &       B    e Zd ZdZdgZdddej                  ddddddddfdededee	e
f   d	ed
edee	e
f   dedededeeeee   f      deeeee   f      deddf fdZej                  ddfdej"                  dee	e
f   d	edeee	ef      deee	ef      dej"                  fdZej                  ddfdee	e
f   d	edeee	ef      deee	ef      fdZej                  ddfdej"                  de
d	edeee	ef      deee	ef      f
dZ	 	 	 d%dej"                  dee
e
f   deeee   f   dee   deee	ef      dej"                  fdZ	 	 	 	 	 d&deej"                     deeee   f   ded eee	ef      dee   deee	ef      defd!Zdddddddddddddd"ej8                  dfdedee   dee   deee	e
f      d	ed
ee   dee   deee	e
f      dee   dee   deeeee   f      deeeee   f      dee   d eee	ef      d#edee   deee	ef      f"d$Z xZS )'Idefics3ImageProcessora  
    Constructs a Idefics3 image processor.
    Args:
        do_convert_rgb (`bool`, *optional*, defaults to `True`):
            Whether to convert the image to RGB. This is useful if the input image is of a different format e.g. RGBA.
            Only has an effect if the input image is in the PIL format.
        do_resize (`bool`, *optional*, defaults to `True`):
            Whether to resize the image. The longest edge of the image is resized to  be <= `size["longest_edge"]`, with the
            shortest edge resized to keep the input aspect ratio.
        size (`Dict`, *optional*, defaults to `{"longest_edge": 4 * 364}`):
            Controls the size of the output image. This is a dictionary containing the key "longest_edge".
            The image will be resized such that the longest edge is <= `size["longest_edge"]` and the shortest edge is resized
            to keep the input aspect ratio.
        resample (`Resampling`, *optional*, defaults to `Resampling.LANCZOS`):
            Resampling filter to use when resizing the image.
        do_image_splitting (`bool`, *optional*, defaults to `True`):
            Whether to split the image into sub-images concatenated with the original image. They are split into patches
            such that each patch has a size of `max_image_size["height"]` x `max_image_size["width"]`.
        max_image_size (`Dict`, *optional*, defaults to `{"longest_edge": 364}`):
            Maximum resolution of the patches of images accepted by the model. This is a dictionary containing the key "longest_edge".
        do_rescale (`bool`, *optional*, defaults to `True`):
            Whether to rescale the image. If set to `True`, the image is rescaled to have pixel values between 0 and 1.
        rescale_factor (`float`, *optional*, defaults to `1/255`):
            Rescale factor to rescale the image by if `do_rescale` is set to `True`.
        do_normalize (`bool`, *optional*, defaults to `True`):
            Whether to normalize the image. If set to `True`, the image is normalized to have a mean of `image_mean` and
            a standard deviation of `image_std`.
        image_mean (`float` or `List[float]`, *optional*, defaults to `IDEFICS_STANDARD_MEAN`):
            Mean to use if normalizing the image. This is a float or list of floats the length of the number of
            channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. Can be
            overridden by the `image_mean` parameter in the `preprocess` method.
        image_std (`float` or `List[float]`, *optional*, defaults to `IDEFICS_STANDARD_STD`):
            Standard deviation to use if normalizing the image. This is a float or list of floats the length of the
            number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method.
            Can be overridden by the `image_std` parameter in the `preprocess` method.
        do_pad (`bool`, *optional*, defaults to `True`):
            Whether or not to pad the images to the largest height and width in the batch and number of images per
            sample in the batch, such that the returned tensor is of shape (batch_size, max_num_images, num_channels, max_height, max_width).
    pixel_valuesTNgp?do_convert_rgb	do_resizerk   resampledo_image_splittingmax_image_size
do_rescalerescale_factordo_normalize
image_mean	image_stddo_padr%   c                    t        |   di | || _        || _        ||nddi| _        || _        || _        ||nddi| _        || _        || _	        |	| _
        |
|
nt        | _        ||nt        | _        || _        y )Nlongest_edgei  il   )super__init__r~   r   rk   r   r   r   r   r   r   r   r   r   r   r   )selfr~   r   rk   r   r   r   r   r   r   r   r   r   kwargs	__class__s                 r-   r   zIdefics3ImageProcessor.__init__H  s      	"6"," ,D>72K	 "40>0JnQ_adPe$,((2(>*DZ&/&;AVr/   r8   r]   r3   c                    |t        |d      }||n|}d|v rt        ||d   |      }nd|v rd|v r|d   |d   f}nt        d      d}|j                  d	k(  s|j                  d
   dk(  rd}t        |||      }|j                  |d   |d   f|      }t        j                  |      }|j                  d	k(  rt        j                  |d
      n|}t        ||t        j                        }|S )a  
        Resize an image. The longest edge of the image is resized to size["longest_edge"], with the shortest edge
        resized to keep the input aspect ratio. Can also be used with size["height"] and size["width"].
        Args:
            image (`np.ndarray`):
                Image to resize.
            size (`Dict[str, int]`):
                Size of the output image.
            resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.LANCZOS`):
                Resampling filter to use when resizing the image.
            data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format of the output image. If not provided, it will be the same as the input image.
            input_data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format of the input image. If not provided, it will be inferred.
        NrI   rK   r   )r2   r3   r!   r"   zJsize must be a dictionary with key 'longest_edge' or 'height' and 'width'.r'   r(   r_   r`   r   )r   axisre   )r   r9   r@   ndimshaper   resizerU   rm   expand_dimsr   r   rn   )	r   r8   rk   r   r]   r3   r   ra   resized_images	            r-   r   zIdefics3ImageProcessor.resizef  s   0 $ >uS\ ] ,7+>'KT!/4+?SdD 'T/NDM2Dijj
::?ekk"o2JUzM^_d1gtAw%7(K/ CPBTBTXYBY}2>_l3;:J:O:O
 r/   c           
      `   t        ||      \  }}|d   x}}	g }
||kD  s||	kD  rt        j                  ||z        }t        j                  ||	z        }t        j                  ||z        }t        j                  ||z        }t        |      D ]\  }t        |      D ]L  }||z  }||z  }t	        ||z   |      }t	        ||z   |      }t        ||||||      }|
j                  |       N ^ ||	}}||k7  s||k7  r| j                  |||d||      }nd\  }}|
j                  |       |
||fS )ap  
        Split an image into squares of side max_image_size and the original image resized to max_image_size.
        That means that a single image becomes a sequence of images.
        This is a "trick" to spend more compute on each image with no changes in the vision encoder.
        1) If one side of the original image is larger than `max_image_size`, resize it to `max_image_size` while preserving the aspect ratio.
        2) Divide the resulting image into `ceil(height / max_image_size)` x `ceil(width / max_image_size)`
        sub-images of the same size each (image_size, image_size). Typically, 364x364.
        3) Returns the list of the crops and the original image, in addition to the number of splits for the height and the width.
        Args:
            image (`np.ndarray`):
                Images to split.
            max_image_size (`Dict[str, int]`):
                Maximum size of the output image. If the image is larger than this size, it will be split into
                patches of this size, and the original image will be concatenated with the patches, resized to max_size.
            resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.LANCZOS`):
                Resampling filter to use when resizing the image.
            data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format of the output image. If not provided, it will be the same as the input image.
            input_data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format of the input image. If not provided, it will be inferred.
        r5   r   )r]   r!   r"   r   r3   )r   r   )r   mathceilrangeminrz   appendr   )r   r8   r   r   r]   r3   r!   r"   rN   rO   framesnum_splits_hnum_splits_woptimal_heightoptimal_widthrcstart_xstart_yend_xend_ycropped_imageglobal_image_heightglobal_image_widths                           r-   split_imagez"Idefics3ImageProcessor.split_image  s{   : 'u:KL!/!??
YJ%)"399Vj%89L99UY%67L!YYv'<=N IIel&:;M <( 1|, 1A-/G.0G  - 7?E. 8&AE %*$/%M MM-0%11, 7A)!3,,9K0K2=OP%&1	 $  *.&L,e|\11r/   vision_encoder_max_sizec                    t        ||      \  }}||z  }||k\  rEt        j                  ||z        |z  }t        ||z        }t        j                  ||z        |z  }nI||kD  rDt        j                  ||z        |z  }t        ||z        }t        j                  ||z        |z  }||d}	| j	                  ||	|||      S )a  
        Resize images to be multiples of `vision_encoder_max_size` while preserving the aspect ratio.
        Args:
            image (`np.ndarray`):
                Images to resize.
            vision_encoder_max_size (`int`):
                Maximum size of the output image. If the image is larger than this size, it will be split into
                patches of this size, and the original image will be concatenated with the patches, resized to max_size.
            resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.LANCZOS`):
                Resampling filter to use when resizing the image.
            data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format of the output image. If not provided, it will be the same as the input image.
            input_data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format of the input image. If not provided, it will be inferred
        r5   r   )rk   r   r3   r]   )r   r   r   r+   r   )
r   r8   r   r   r]   r3   r!   r"   r,   new_sizes
             r-   resize_for_vision_encoderz0Idefics3ImageProcessor.resize_for_vision_encoder  s    . 'u:KLv~F?IIe&==>AXXE-.FYYv(??@CZZFe^YYv(??@CZZF-.EIIe&==>AXXE$u5{{8GXfq  
 	
r/   rQ   constant_valuesc                     t        ||      \  }}|\  }}	||z
  }
|	|z
  }d|
fd|ff}t        ||t        j                  |||      }|S )z<
        Pad an image with zeros to the given size.
        r5   r   )rg   r   r]   r3   )r   r   r   CONSTANT)r   r8   rQ   r   r]   r3   rX   rY   output_heightoutput_width
pad_bottom	pad_rightpaddingpadded_images                 r-   
_pad_imagez!Idefics3ImageProcessor._pad_image  sp     %35FW$X!k&1#|"\1
 ;.	z?Q	N3%%+#/
 r/   r:   return_pixel_maskreturn_tensorsc           
      .   t        ||      }t        |      }t        d |D              }	|t        |d   d   d      n|}||n|}|t        j
                  k(  r|d   d   j                  d   n4|t        j                  k(  r|d   d   j                  d   nt        d      fd	}
t        |      D cg c]"  }t        |	      D cg c]  } |
||       c}$ }}t        |      D cg c].  }t        |	      D cg c]  }t        j                  |       c}0 }}t        |      D ]H  }t        ||         D ]5  \  }}| j                  |||||
      ||   |<   t        |||      ||   |<   7 J |r|nd}||fS c c}w c c}w c c}w c c}w )a~  
        For a list of images, for each images, pads a batch of images to the bottom and right of the image with zeros to the size of largest height and width.
        For each sample in the batch, pads the sample with empty images to the max_number of images per sample in the batch. Optionally returns a pixel mask.
        Args:
            images (`List[np.ndarray]`):
                List of list of images to pad. Pads to the largest height and width in the batch.
            constant_values (`float` or `Iterable[float]`, *optional*):
                The value to use for the padding if `mode` is `"constant"`.
            return_pixel_mask (`bool`, *optional*, defaults to `True`):
                Whether to return a pixel mask.
            return_tensors (`str` or `TensorType`, *optional*):
                The type of tensors to return. Can be one of:
                    - Unset: Return a list of `np.ndarray`.
                    - `TensorType.TENSORFLOW` or `'tf'`: Return a batch of type `tf.Tensor`.
                    - `TensorType.PYTORCH` or `'pt'`: Return a batch of type `torch.Tensor`.
                    - `TensorType.NUMPY` or `'np'`: Return a batch of type `np.ndarray`.
                    - `TensorType.JAX` or `'jax'`: Return a batch of type `jax.numpy.ndarray`.
            data_format (`str` or `ChannelDimension`, *optional*):
                The channel dimension format of the image. If not provided, it will be the same as the input image.
            input_data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format of the input image. If not provided, it will be inferred.
        r3   c              3   2   K   | ]  }t        |        y wN)r?   ).0images_s     r-   	<genexpr>z-Idefics3ImageProcessor.pad.<locals>.<genexpr>R  s     @gS\@s   Nr   rI   rK   r   rx   c                     |t         j                  k(  r)t        j                  g| t        j                        S |t         j
                  k(  r*t        j                  g | t        j                        S y )NrS   )r   ry   rU   rV   uint8rn   )rk   r3   
n_channelss     r-   empty_imagez/Idefics3ImageProcessor.pad.<locals>.empty_imagea  sc     $4$:$::xx 3d 3288DD"&6&;&;;xx 3$ 3
 3288DD <r/   )r   r]   r3   )rQ   r3   )rP   r?   r*   r   r   ry   r   rn   r@   r   rU   rV   	enumerater   r[   )r   r:   r   r   r   r]   r3   pad_size
batch_sizemax_num_imagesr   _padded_images_listpadded_masks	batch_idx
sample_idxr8   r   s                    @r-   r   zIdefics3ImageProcessor.pad0  s   > (BST[
@@@ !( +6!9Q<iP" 	
 ,7+>'K 0 6 661++A.J"2"7"771++B/J@AA	E Z__iYj
TU~9NOA[;/O
 
 V[[eUfgPQU>5JK(+Kggz* 	I%.vi/@%A 
!
E<@OO$3 +&7 =L ="9-j9 7FxCT7Y'
3
	 (9|d!<//% P
 Lgs0   8F
F
F.F F
FFFFreturn_row_col_infoc                    ||n| j                   }||n| j                  }||n| j                  }||n| j                  }|	|	n| j                  }	||n| j
                  }||n| j                  }|
|
n| j                  }
||n| j                  }||n| j                  }||n| j                  }||n| j                  }t        |      }t        |d         st        d      t        ||	|
|||||       |D cg c]K  }|D cg c]=  }t!        |t"        j"                        r|j$                  dk(  r|j'                         nd? c}M }}}|D cg c]  }|D cg c]  }t)        |       c} }}}|t*        j,                  dfv rF|D cg c]8  }|D cg c]*  }|j.                  dk(  rt1        j2                  |d      n|, c}: }}}nX|t*        j4                  k(  rE|D cg c]8  }|D cg c]*  }|j.                  dk(  rt1        j2                  |d      n|, c}: }}}t7        |d   d         r|rt8        j;                  d	       |t=        |d   d   d
      }|r2|D cg c]%  }|D cg c]  }| j?                  ||||       c}' }}}|r|D cg c](  }|D cg c]  }| jA                  ||d   ||       c}* }}}g }g }g }g }tC        ||      D ]  \  }}g }g }g }g }tC        ||      D ]n  \  }} | jE                  |||      \  }!}"}#|jG                  |!       |jG                  | gtI        |!      z         |jK                  |"       |jK                  |#       p |jK                  |       |jK                  |       |jK                  |       |jK                  |        |}|}ns|D cg c].  }|D cg c]   }| j?                  ||d   |d   d||      " c}0 }}}|D cg c]  }dgtI        |      z   }}|D cg c]  }dgtI        |      z   }}|rItC        ||      D  cg c].  \  }}tC        ||      D  cg c]  \  }} tM        ||        c} }0 }}}}} |r1|D cg c]$  }|D cg c]  }| jO                  ||	|       c}& }}}|
r2|D cg c]%  }|D cg c]  }| jQ                  ||||       c}' }}}d}$|r| jS                  |d||      \  }}$|+|D cg c]  }|D cg c]  }tU        |||       c}  }}}d|r|t1        jV                  |      n|i}%|$|r|t1        jV                  |$      n|$|%d<   tY        |%|      }&|r
||&d<   ||&d<   |&S c c}w c c}}w c c}w c c}}w c c}w c c}}w c c}w c c}}w c c}w c c}}w c c}w c c}}w c c}w c c}}w c c}w c c}w c c} }w c c} }}}w c c}w c c}}w c c}w c c}}w c c}w c c}}w )a  
        Preprocess a batch of images.
        Args:
            images (`ImageInput`):
                A list of images to preprocess.
            do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`):
                Whether to convert the image to RGB.
            do_resize (`bool`, *optional*, defaults to `self.do_resize`):
                Whether to resize the image.
            size (`Dict[str, int]`, *optional*, defaults to `self.size`):
                Size of the image after resizing. With the longest edge resized to keep the input aspect ratio.
            resample (`int`, *optional*, defaults to `self.resample`):
                Resampling filter to use if resizing the image. This can be one of the enum `PILImageResampling`. Only
                has an effect if `do_resize` is set to `True`.
            do_image_splitting (`bool`, *optional*, defaults to `self.do_image_splitting`):
                Whether to split the image into sub-images concatenated with the original image. They are split into patches
                such that each patch has a size of `max_image_size["height"]` x `max_image_size["width"]`.
            max_image_size (`Dict`, *optional*, defaults to `self.max_image_size`):
                Maximum resolution of the images. If the image is larger than this size, the image is split into patches.
            do_rescale (`bool`, *optional*, defaults to `self.do_rescale`):
                Whether to rescale the image.
            rescale_factor (`float`, *optional*, defaults to `self.rescale_factor`):
                Rescale factor to rescale the image by if `do_rescale` is set to `True`.
            do_normalize (`bool`, *optional*, defaults to `self.do_normalize`):
                Whether to normalize the image.
            image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`):
                Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`.
            image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`):
                Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to
                `True`.
            do_pad (`bool`, *optional*, defaults to `self.do_pad`):
                Whether or not to pad the images to the largest height and width in the batch.
            return_tensors (`str` or `TensorType`, *optional*):
                The type of tensors to return. Can be one of:
                - Unset: Return a list of `np.ndarray`.
                - `TensorType.TENSORFLOW` or `'tf'`: Return a batch of type `tf.Tensor`.
                - `TensorType.PYTORCH` or `'pt'`: Return a batch of type `torch.Tensor`.
                - `TensorType.NUMPY` or `'np'`: Return a batch of type `np.ndarray`.
                - `TensorType.JAX` or `'jax'`: Return a batch of type `jax.numpy.ndarray`.
            return_row_col_info (`bool`, *optional*, default to `False`):
                Whether to return the number of rows and columns of the split images. This is used for the
                `Idefics3Processor` to generate prompt strings based on the number of rows and columns.
            data_format (`ChannelDimension` or `str`, *optional*, defaults to `ChannelDimension.FIRST`):
                The channel dimension format for the output image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
                - Unset: Use the channel dimension format of the input image.
            input_data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format for the input image. If unset, the channel dimension format is inferred
                from the input image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
                - `"none"` or `ChannelDimension.NONE`: image in (height, width) format.
        Nr   zkInvalid image type. Must be of type PIL.Image.Image, numpy.ndarray, torch.Tensor, tf.Tensor or jax.ndarray.)r   r   r   r   r   r   rk   r   r_   r'   r   r   zIt looks like you are trying to rescale already rescaled images. If the input images have pixel values between 0 and 1, set `do_rescale=False` to avoid rescaling them again.rI   rK   )r8   rk   r   r3   r   r   )r   r3   r   r   )r8   meanstdr3   T)r   r   r3   re   r}   pixel_attention_mask)datatensor_typerowscols)-r   rk   r   r   r   r   r   r   r   r   r~   r   rA   r   r@   r   r<   r    rg   
getpaletter   r   rn   r   rU   r   ry   r   loggerwarning_oncer   r   r   rD   r   extendr?   r   rr   rescale	normalizer   r   rm   r   )'r   r:   r~   r   rk   r   r   r   r   r   r   r   r   r   r   r   r]   r3   rG   impalettes_listr8   imgimages_list_split_arrayspalettes_list_split_arraysimages_list_rowsimages_list_colspalettessplit_image_arrayssplit_palettes_arrays
image_rows
image_colsr\   split_image_arrayr   r   r   r   encodings'                                          r-   
preprocessz!Idefics3ImageProcessor.preprocess|  s&   T "+!6IDNN	'tTYY'38#-#9Zt
+9+E4K^K^3E3Q/W[WnWn+9+E4K^K^'3'?|TEVEV#-#9Zt
!*!6IDNN	+9+E4K^K^!-4;;)&1KN+: 
 	&!)%!		
 &
 djj]_
2u{{ ;3R]]_TXXj
 
 R]]v6B%u-B]] !1!6!6 ==it_eSYZCQ"-CGZK  "2"8"88hs^dRXY3A!,3FYK  ;q>!,-*s $ >{1~a?P_h i *
  "( KKe$]nKoK   *  "(	  22~n=du 3 K  (*$)+&!!$']$C 4 %'"(*%

&)&(&; 	,NE7484D4D'5*; 5E 51%tT
 '--.?@)00'SAR=S1ST%%d+%%d+	, )//0BC*112GH ''
3 ''
3%4& 3K6M *  "(  KK#(6~(FQ_`nQop!)*;	   K  ALLfc&k 1LL@KLfc&k 1LL ),K(G $FH CFfhBWX,#wW-XK 
  * hnn^ce^GXYnK 
  *
  "( NNZYbsNtK   $04tNfw 19 1-K- " *
  "( 0{VghK  ND^ 5dop+28^=W-.]q '(  T~F /HV/HV[ k
 C]
 [
 Z J  ML Y o s  =	VAVV	VV0V	V/V	V*	V)3/V$"V).	V47V/V4#	V?,V:V?	W
%W0W
<WWW 
W0W 
	W-W(&W-5	W8>W3W8	XW>#XVVV$V)/V4:V?W
W 
(W-3W8>X)r   NN)r   TNNN) __name__
__module____qualname____doc__model_input_namesr   LANCZOSboolr   strr+   rM   r   r	   r   r   rU   ndarrayr   r   r   r   r   r   r   r   r   r   ry   r   r   __classcell__)r   s   @r-   r|   r|     s   &P ((  $#'9'A'A#')- '!:>9=  38n	
 % ! S#X    U5$u+#567 E%e"456  
D (:'A'A>BDH7zz7 38n7 %	7
 eC)9$9:;7 $E#/?*?$@A7 
7z (:'A'A>BDHM2 S#XM2 %	M2
 eC)9$9:;M2 $E#/?*?$@AM2f (:'A'A>BDH%
zz%
 "%%
 %	%

 eC)9$9:;%
 $E#/?*?$@A%
V :;26DHzz 38_ uhuo56	
 ./ $E#/?*?$@A 
< :;"&;?26DHJ0RZZ J0 uhuo56J0  	J0
 !sJ!78J0 ./J0 $E#/?*?$@AJ0 
J0^ *.$()-'+-1%)37*.'+:>9=!%;?$)2B2H2HDH%yy !y D>	y
 tCH~&y %y %TNy TNy !c3h0y !y tny U5$u+#567y E%e"456y y !sJ!78y  "!y" ./#y$ $E#/?*?$@A%yr/   r|   )r(   Nr   )NNN)8r   typingr   r   r   r   r   r   r	   numpyrU   image_processing_utilsr   r   image_transformsr   r   r   r   image_utilsr   r   r   r   r   r   r   r   r   r   r   r   utilsr   r   r   
get_loggerr   r   r7   PILr    r+   r.   r   r1   r9   r   rA   rF   rP   r[   ImagePaletterr   rz   r|   r   r/   r-   <module>r     s     D D D  F [ [    > = 
		H	%  SW"""&.sm"BJ3-"
38_"L BF&.tCH~&>
38_D AE  c+;&; <= 38_	8
 tD4D/E D8x} 8c 8 fj#d2::&'#<DU3P`K`Ea<b#	#Y#( rv::$)#s(OHPQVWZ\lWlQmHnZZ& 8<:>@D	&::&c&&334& %%5 567&  c+;&; <=	&
 &` ;?:: 	 		
 	 %%5 567 ZZ*X	/ X	r/   