
    sg                        d dl Z d dlmZ d dl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 ddlmZm Z   e       r
d dl!Z!d d	l!m"Z"  e jF                  e$      Z% ed
      de&deee&e&f      fd       Z'de&de&de&de&de&dee&e&f   fdZ( ed      de&de&de&de&dee&e&f   f
d       Z)de
jT                  de&de&de
jT                  fdZ+deeee&e&f         de&de
jT                  fdZ,deee
jT                        de&dee
jT                  eee&      f   fdZ-d.deeee&e&f         de&de
jT                  fd Z.deeee&e&f         de&de
jT                  fd!Z/	 d/de
jT                  d"eee0f   d#eeee0f      de
jT                  fd$Z1dedefd%Z2d&edeeee
jT                           fd'Z3d&efd(Z4d)ee0e&f   ddfd*Z5d+ Z6 G d, d-e      Z7y)0    N)	lru_cache)DictListOptionalTupleUnion   )BaseImageProcessorBatchFeature)PaddingModeget_image_sizepadresize)
IMAGENET_STANDARD_MEANIMAGENET_STANDARD_STDChannelDimension
ImageInputPILImageResamplinginfer_channel_dimension_formatis_valid_imageis_vision_availableto_numpy_arrayvalidate_preprocess_arguments)
TensorTypelogging)Image
   )maxsizemax_image_tilesreturnc                     g }t        d| dz         D ]2  }t        d| dz         D ]  }||z  | k  s|j                  ||f         4 |S )a  
    Computes all allowed aspect ratios for a given maximum number of input tiles.

    This function calculates all possible arrangements of tiles that can be formed
    within the constraint of the maximum number of tiles. Each arrangement is
    represented by its aspect ratio (width/height) and the corresponding tile configuration.

    Args:
        max_image_tiles (`int`):
            The maximum number of tiles allowed.

    Returns:
        `List[Tuple[int, int]]`: A list of tuples, each tuple representing a valid (width, height)
        configuration in terms of number of tiles.

    Example:
        >>> get_all_supported_aspect_ratios(4)
        [(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (3, 1), (4, 1)]

       )rangeappend)r   aspect_ratioswidthheights       e/var/www/html/venv/lib/python3.12/site-packages/transformers/models/mllama/image_processing_mllama.pyget_all_supported_aspect_ratiosr)   4   se    , Mq/A-. 6A23 	6Fv~0$$eV_5	66     image_heightimage_widthcanvas_heightcanvas_width	tile_sizec                    t        j                  |||      }t        j                  | ||      }|| z  }||z  }||k  r(|}	t        t        j                  | |z        |      }
|
|	fS |}
t        t        j                  ||z        |      }	|
|	fS )a  
    Calculates the new size of an image to fit within a canvas while maintaining aspect ratio.

    This function calculates the optimal size for an image to fit within a canvas defined by
    canvas_height and canvas_width, while ensuring that the image dimensions are not smaller than
    tile_size. If the image is larger than the canvas, the returned size will fit within the canvas.
    If the image already fits within the canvas, the size remains unchanged.
    The aspect ratio of the original image is preserved.

    Args:
        image_height (`int`):
            The height of the original image.
        image_width (`int`):
            The width of the original image.
        canvas_height (`int`):
            The height of the canvas.
        canvas_width (`int`):
            The width of the canvas.
        tile_size (`int`):
            The tile size.

    Returns:
        `Tuple[int, int]`: A tuple containing the new height and width of the image.

    )npclipminmathfloor)r+   r,   r-   r.   r/   target_widthtarget_heightscale_hscale_w	new_width
new_heights              r(   get_image_size_fit_to_canvasr<   R   s    B 77;	<@LGGL)]CMl*G[(G 	L7$:;]K

 y   #


;#89<H	y  r*   d   c                    t        |      }t        j                  |      |z  }t        j                  |      j                  \  }}|| z  }||z  }	t        j                  |	|kD  ||	      }
|
|
dk\     }t        |      dkD  rt        j                  |      }n|
|
dk     }t        j                  |      }||
|k(     }t        |      dkD  r/|dddf   |dddf   z  }t        j                  |      }||   }|S |d   }|S )a  
    Determines the best canvas based on image and tile size and maximum number of tiles.

    First, calculates possible resolutions based on the maximum number of tiles and tile size.
    For example for max_image_tiles=2, tile_size=100, possible tile arrangements are:
    [(1, 1), (1, 2), (2, 1)] and corresponding canvas sizes are:
    [(100, 100), (100, 200), (200, 100)]

    For each possible resolution, calculates the scaling factors for
    width and height, and selects the smallest one, which is the limiting side.
    E.g. to match the canvas you can upscale height by 2x, and width by 1.5x,
    therefore, the maximum upscaling you can do is min(2, 1.5) = 1.5.

    If upscaling is possible (any of the scaling factors is greater than 1),
    then picks the smallest upscaling factor > 1.

    If upscaling is not possible, then picks the largest scaling factor <= 1, i.e.
    reduce downscaling as much as possible.

    If there are multiple resolutions with the same max scale, we pick the one with the lowest area,
    to minimize padding. E.g., the same image can be upscaled to 224x224 and 224x448, but the latter
    has more padding.

    Example of canvases made from tiles:

    To visualize how the image can fit onto different tile grids, let's try fitting an ASCII cat into the tiles.

    Here's an ASCII cat image you want to fit into the tiles:

       /\_/\
      ( o.o )
       > ^ <

    If `num_tiles=6`, possible tile grids would look like this:

    **2x3 Canvas (2 tiles wide, 3 tiles tall)**: -> total of 6 tiles
    +-------+-------+
    | /\_/\ |   0   |   <- Cat image split across two tiles horizontally
    +-------+-------+
    | > ^ < |   0   |   <- Remaining part of the cat occupies the left tile
    +-------+-------+
    |( o.o )|   0   |
    +-------+-------+

    **3x2 Canvas (3 tiles wide, 2 tiles tall)**: -> total of 6 tiles
    +-------+-------+-------+
    | /\_/\ |( o.o )|   0   |   <- Cat image occupies the first two tiles, 1 tile remains empty
    +-------+-------+-------+
    | > ^ < |   0   |   0   |   <- Remaining part of the cat occupies the left tile
    +-------+-------+-------+

    **1x6 Canvas (1 tile wide, 6 tiles tall)**: -> total of 6 tiles
    +-------+
    | /\_/\ |   <- Top part of the cat
    +-------+
    |( o.o )|   <- Middle part of the cat
    +-------+
    | > ^ < |   <- Bottom part of the cat
    +-------+
    |   0   |
    +-------+
    |   0   |
    +-------+
    |   0   |
    +-------+

    Given that the tiles you get depend on the chosen aspect ratio, you have to add
    embedding in the modeling code to help it know if it got a 3x2 or a 1x6 or a 2x3
    aspect ratio.

    The function tests these arrangements to find the smallest canvas where the image fits.
    If multiple canvases fit, it selects the one where the dimensions are closest to the image size.

    In this case the first canvas is the closest to the original image.

    You then feed all of the tiles to the model:

        +-------+-------+-------+-------+-------+-------+
    -   | /\_/\ |( o.o )| > ^ < |   0   |   0   |   0   |  <- Last canvas
        +-------+-------+-------+-------+-------+-------+

        +-------+-------+-------+-------+-------+-------+
    -   | /\_/\ | 0     |( o.o )|   0   | > ^ < |   0   | <- First canvas
        +-------+-------+-------+-------+-------+-------+

        +-------+-------+-------+-------+-------+-------+
    -   | /\_/\ |( o.o )|   0   | > ^ < |   0   |   0   | <- second canvas
        +-------+-------+-------+-------+-------+-------+

    For each tile, you have num_channels (usually RGB so 3), tile_width, tile_height

    Args:
        image_height (`int`):
            The height of the image.
        image_width (`int`):
            The width of the image.
        max_image_tiles (`int`):
            The maximum number of tiles any image can be split into.
        tile_size (`int`):
            The tile size.

    Returns:
        `Tuple[int, int]`: The best canvas resolution [height, width] for the given image.
    r"   r   N)	r)   r1   arrayTwherelenr3   maxargmin)r+   r,   r   r/   possible_tile_arrangementspossible_canvas_sizestarget_heightstarget_widthsr8   r9   scalesupscaling_optionsselected_scaledownscaling_optionschosen_canvasareasoptimal_idxoptimal_canvass                     r(   get_optimal_tiled_canvasrQ      s-   ^ "A!QHH%?@9L %'HH-B$C$E$E!NM |+Gk)G XXg'':F v{+
! 12 %VaZ0 34 *&N*BCM =Aad#mAqD&99ii&&{3  'q)r*   imagenum_tiles_heightnum_tiles_widthc                     | j                   \  }}}||z  }||z  }| j                  |||||      } | j                  ddddd      } | j                  ||z  |||      } t        j                  |       S )a6  
    Split an image into a specified number of tiles along its width and height dimensions.

    Args:
        image (`np.ndarray`):
            Input image with shape (num_channels, height, width).
        num_tiles_height (`int`):
            Number of tiles to split the image into along its height.
        num_tiles_width (`int`):
            Number of tiles to split the image into along its width.

    Returns:
        `np.ndarray`:
            Array of image tiles with shape (num_tiles_width * num_tiles_height, num_channels, tile_height, tile_width).
    r"   r	   r         )shapereshape	transposer1   ascontiguousarray)rR   rS   rT   num_channelsr'   r&   tile_height
tile_widths           r(   split_to_tilesr_     s      #(++L&%,,K/)JMM,(8+XbcE OOAq!Q*E MM/,<<lKYcdE&&r*   r%   c           	      >   t        |       }t        | D cg c]  }t        |       c}      }t        j                  |||ft        j                        }d|dddddf<   t        |       D ](  \  }}t        |      D ]  \  }\  }	}
d|||d|	|
z  f<    * |S c c}w )aP  
    Builds a mask for the aspect ratios of the images.

    Args:
        aspect_ratios (`List[List[Tuple[int, int]]]`):
            A list of lists containing aspect ratios for each image in the batch.
            Each aspect ratio is represented as a tuple of (width, height) in terms of number of tiles.
        max_image_tiles (`int`):
            The maximum number of tiles any image can be split into.

    Returns:
        `np.ndarray`: A 3D numpy array of shape (batch_size, max_num_images, max_image_tiles).
            The mask contains 1s for valid tiles and 0s for padding.
    dtyper"   Nr   )rB   rC   r1   zerosint64	enumerate)r%   r   
batch_sizerowmax_num_imagesaspect_ratio_maskisample_aspect_ratiosjnum_tiles_wnum_tiles_hs              r(   build_aspect_ratio_maskro   8  s     ]#Jm<s#c(<=N*no!NVXV^V^_
 "#aAg $-]#; E-67K-L 	E)A)[CDa$?kK&?$??@	EE  =s   Bbatch_imagesc                    t        |       }t        | D cg c]  }t        |       c}      }| D cg c]  }|D ]  }|j                    }}}|d   \  }}}	}
t        j                  |||||	|
ft        j
                        }g }t        |       D ]U  \  }}g }t        |      D ]/  \  }}|j                  d   }||||d|f<   |j                  |       1 |j                  |       W ||fS c c}w c c}}w )a  
    Stack a list of lists of images with variable lengths into a numpy array, applying zero padding as needed.
    Each list in the input represents a batch sample, and each image within a list is expected to be
    pre-split into tiles. The resulting array will have a shape of
    (batch_size, max_num_images, max_image_tiles, channels, tile_height, tile_width).

    Args:
        batch_images (`List[List[np.ndarray]]`):
            A list of lists of image tiles. Each inner list represents
            a batch sample containing multiple images, where each image is pre-split into tiles.
            The shape of each tile array is (num_tiles, channels, tile_height, tile_width).
        max_image_tiles (int):
            The maximum number of tiles any image was potantially split.

    Returns:
        `Tuple[np.ndarray, List[List[int]]]`: A tuple containing:
            - stacked_images (`np.ndarray`):
                A numpy array of stacked images with shape
                (batch_size, max_num_images, max_image_tiles, channels, tile_height, tile_width).
            - all_num_tiles (`List[List[int]]`):
                A list of lists containing the number of tiles
                for each image in each batch sample.
    r   ra   N)rB   rC   rX   r1   rc   float32re   r$   )rp   r   rf   imagesrh   rR   shapes_channelsr]   r^   stacked_imagesall_num_tilesrj   num_sample_tilesrl   	num_tiless                    r(   pack_imagesr{   Y  s   : \"JLA&#f+ABN(4If&IekkIkIFI+1!9(Axj XX	^_hZXjjN M|, /	6!&) 	/HAuAI/4N1a)+,##I.	/ 	-./ =(() BIs
   C.C3	pad_valuec                 J   t        |       }t        | D cg c]  }t        |       c}      }t        j                  ||df|t        j                        }t        |       D ]9  \  }}t        |      dkD  st        j                  |      ||dt        |      f<   ; |S c c}w )a  
    Stack a list of aspect ratios into a numpy array.

    Args:
        aspect_ratios (`List[List[Tuple[int, int]]]`):
            A list of aspect ratios.
        pad_value (`int`, *optional*, defaults to 1):
            The value to pad the aspect ratios with.

    Returns:
        `np.ndarray`:
            The aspect ratios stacked into a numpy array with shape (batch_size, max_num_images, 2).
    rV   ra   r   N)rB   rC   r1   fullrd   re   r?   )r%   r|   rf   rg   rh   aspect_ratios_stackedrj   s          r(   pack_aspect_ratiosr     s     ]#Jm<s#c(<=NGGZ$CYVXV^V^_M* A3s8a<3588C=!!Zs3xZ-0A !  =s   B c                 V   t        |       }t        | D cg c]  }t        |       c}      }t        |      }t        j                  ||ft        j
                        }t        |       D ]6  \  }}t        |      D ]#  \  }	\  }
}|j                  |
|f      dz   |||	f<   % 8 |S c c}w )a  
    Convert aspect ratio tuples to unique ids.

    For batch padding we use 0, because there might be different number of images in each batch.
    The aspect ratio ids start from 1, with 1 corresponding to the first supported aspect ratio.

    Args:
        aspect_ratios (`List[List[Tuple[int, int]]]`):
            A list of aspect ratios for each image in the batch.
        max_image_tiles (`int`):
            The maximum number of tiles any image can be split into.

    Returns:
        `np.ndarray`:
            The aspect ratios ids as a numpy array with shape (batch_size, max_num_images).
            Each id corresponds to the index of the aspect ratio in the list of supported aspect ratios,
            offset by 1 (so 0 can be used for padding).
    ra   r"   )rB   rC   r)   r1   rc   rd   re   index)r%   r   rf   rg   rh   supported_aspect_ratiosaspect_ratios_idsrj   rk   rl   rn   rm   s               r(   convert_aspect_ratios_to_idsr     s    ( ]#Jm<s#c(<=N=oN*n!=RXXN#,]#; d-67K-L 	d)A)[&=&C&C[R]D^&_bc&cad#	dd  =s   B&channel_diminput_channel_dimc                 n   t        | t        j                        st        dt	        |              |t        |       }t        |      }||k(  r| S |t        j                  k(  r| j                  d      } | S |t        j                  k(  r| j                  d      } | S t        dj                  |            )a5  
    Converts `image` to the channel dimension format specified by `channel_dim`.

    Args:
        image (`numpy.ndarray`):
            The image to have its channel dimension set.
        channel_dim (`ChannelDimension`):
            The channel dimension format to use.
        input_channel_dim (`ChannelDimension`, *optional*):
            The channel dimension format of the input image. If not provided, it will be inferred from the input image.

    Returns:
        `np.ndarray`:
            The image with the channel dimension set to `channel_dim`.
    z,Input image must be of type np.ndarray, got )rV   r   r"   )r"   rV   r   z(Unsupported channel dimension format: {})
isinstancer1   ndarray
ValueErrortyper   r   FIRSTrZ   LASTformat)rR   r   r   target_channel_dims       r(   to_channel_dimension_formatr     s    ( eRZZ(GU}UVV :5A)+6..-333	* L 
/44	4	* L CJJ;WXXr*   c                 &   t        | t        j                  j                        s| S | j                  dk(  r| S | j	                  d      }t        j
                  d|j                  d      }t        j                  ||      }|j	                  d      }|S )z
    Converts an image to RGB format. Only converts if the image is of type PIL.Image.Image, otherwise returns the image
    as is.
    Args:
        image (Image):
            The image to convert.
    RGBRGBA)   r   r   )r   PILr   modeconvertnewsizealpha_composite)rR   
image_rgba
backgroundr   s       r(   convert_to_rgbr     sz     eSYY__- zzUv&J6:??ODJ++J
CO%--e4Or*   rs   c                    t        |       r| gg}|S t        | t        t        f      rt	        |       r| g}|S t        | t        t        f      r(t        d | D              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.
    c              3   H   K   | ]  }t        |t        t        f        y wN)r   listtuple.0images_is     r(   	<genexpr>z&make_list_of_images.<locals>.<genexpr>  s     K
8dE]3Ks    "c              3   2   K   | ]  }t        |        y wr   )is_valid_list_of_imagesr   s     r(   r   z&make_list_of_images.<locals>.<genexpr>  s     Ih'1I   z]Invalid input type. Must be a single image, a list of images, or a list of batches of images.)r   r   r   r   r   allanyr   )rs   output_imagess     r(   make_list_of_imagesr     s     f 
  
FT5M	*/Fv/N  	6D%=)KFKKI&II
  k
 	
r*   c                 .    | xr t        d | D              S )Nc              3   2   K   | ]  }t        |        y wr   )r   )r   rR   s     r(   r   z*is_valid_list_of_images.<locals>.<genexpr>&  s     DE./Dr   )r   )rs   s    r(   r   r   %  s    DcDVDDDr*   r   c                 b    d| v rd| v st        d|        | d   | d   k7  rt        d|        y )Nr'   r&   zJArgument `size` must be a dictionary with keys 'height' and 'width'. Got: z9Argument `size` must have the same height and width, got )r   )r   s    r(   _validate_sizer   )  sO    DefjeklmmH~g&TUYTZ[\\ 'r*   c                 z    |st        d      | st        d      ||dk  rt        d| d      t        |       y )Nz9MllamaImageProcessor doesn't support `do_pad=False` mode.z<MllamaImageProcessor doesn't support `do_resize=False` mode.r   zGMllamaImageProcessor `max_image_tiles` must be a positive integer, got .)r   r   )	do_resizer   do_padr   s       r(   %_validate_mllama_preprocess_argumentsr   0  sM    TUUWXX/Q"6bcrbsstuvv4r*   c                        e Zd ZdZg dZdddej                  dddddddfdeded	ee	e
ef      d
ededededeeeee   f      deeeee   f      dededdf fdZ	 	 	 	 	 	 	 	 	 	 	 	 	 ddedee   dee   d	ee	e
ef      d
ee   dee   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   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f   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j*                  d	e	e
ef   ded
edeee
ef      deee
ef      deej*                  eeef   f   fdZ xZS )MllamaImageProcessora  
    Constructs a Mllama 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.
        size (`Dict[str, int]`, *optional*, defaults to `self.size`):
            Size of the image tile. Should be a dictionary containing 'height' and 'width' keys, both with integer values.
            The height and width values should be equal.
        resample (`int`, *optional*, defaults to `Resampling.BILINEAR`):
            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_rescale (`bool`, *optional*, defaults to `True`):
            Whether to rescale the image.
        rescale_factor (`float`, *optional*, defaults to 0.0):
            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.
        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 `True`):
            Whether or not to pad the images to the largest height and width in the batch.
        max_image_tiles (`int`, *optional*, defaults to 4):
            The maximum number of tiles to split the image into.
    )pixel_valuesrz   aspect_ratio_idsri   TNgp?rW   do_convert_rgbr   r   resample
do_rescalerescale_factordo_normalize
image_mean	image_stdr   r   r    c                 X   t        |   di | || _        || _        ||nddd| _        || _        || _        || _        || _        ||nt        | _
        |	|	nt        | _        |
| _        || _        t        | j                  | j                  | j                  | j                         y )N   )r'   r&    )super__init__r   r   r   r   r   r   r   r   r   r   r   r   r   r   )selfr   r   r   r   r   r   r   r   r   r   r   kwargs	__class__s                r(   r   zMllamaImageProcessor.__init__]  s     	"6"," ,DS32O	 $,((2(>*DZ&/&;AV.-dnndiiVZVjVjkr*   rs   input_data_formatreturn_tensorsc           
         ||n| j                   }||n| j                  }||n| j                  }||n| j                  }||n| j                  }||n| j
                  }||n| j                  }|	|	n| j                  }	|
|
n| j                  }
||n| j                  }||n| j                  }t        ||||	|
|||       t        ||||       t        |      }| j                   r(|D cg c]  }|D cg c]  }t        |       c} }}}|D cg c]  }|D cg c]  }t        |       c} }}}g }g }|D ]  }g }g }|D ]  }t         j"                  }t%        |||      }| j'                  ||||||      \  }}| j)                  |||||      }|r| j+                  ||||      }|r| j-                  ||	|
||      }|\  }}t/        |||      }|j1                  |       |j1                  ||f        |j1                  |       |j1                  |        t3        ||      \  }}t5        ||      }t7        ||      }t9        |||d|	      }||d
<   |S 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 tile. Should be a dictionary containing 'height' and 'width' keys, both with integer values.
                The height and width values should be equal.
            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_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.
            max_image_tiles (`int`, *optional*, defaults to `self.max_image_tiles`):
                The maximum number of tiles to split the image into.
            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.
            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`.

        Returns:
            `BatchFeature` of the following structure:
                - **pixel_values** (`TensorType`): The preprocessed pixel values.
                - **aspect_ratio_ids** (`TensorType`): The aspect ratio ids of the images.
                - **num_tiles** (`List[List[int]]`): The number of tiles for each image in the batch.
        )r   r   r   r   r   r   r   r   )r   )rR   r   r   r   r   data_format)rR   r   aspect_ratior   r   )rR   scaler   r   )rR   meanstdr   r   )r   )r   r   ri   )datatensor_typerz   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rescale	normalizer_   r$   r{   r   ro   r   )r   rs   r   r   r   r   r   r   r   r   r   r   r   r   r   images_listrR   rp   batch_aspect_ratiossample_imagesrk   r   r   rS   rT   rz   r   ri   encoded_inputss                                r(   
preprocesszMllamaImageProcessor.preprocess{  s   H ,:+E4K^K^!*!6IDNN	'tTYY'38#-#9Zt
+9+E4K^K^'3'?|TEVEV#-#9Zt
!*!6IDNN	!-4;;-<-H/dNbNb%!)%!		
 	.ivW)&1U`a6vFeN51FaKaQ\]v6B%u-B]]  " 6	=FM#%    .Q /443E;Zkl '+kk%$3&1 + '2 '#| !-&1 + !   LL#,*;$/	 ) E   NN#'%*;$/ + E 5A1 /&u.>P$$U+$++-=,OP].Q` .&&';<m6	=p (oF	78K]lm34GYhi & &$4%6
 '
 '0{#g GaB]s0   .	I*7I%	I*	I5I01I5%I*0I5rR   r   r   c                     t        |       t        ||      \  }}|\  }}	||d   z  }
|	|d   z  }d|
|z
  fd||z
  ff}t        ||t        j                  d||      }|S )a\  
        Pad an image to the `size` x `aspect_ratio`. For example, if size is {height: 224, width: 224} and aspect ratio is
        (1, 2), the image will be padded to 224x448.

        Args:
            image (`np.ndarray`):
                Image to resize.
            size (`Dict[str, int]`):
                Size of the output image.
            aspect_ratio (`Tuple[int, int]`):
                The aspect ratio of the image.
            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.

        Returns:
            `np.ndarray`: The padded image.
        r   r'   r&   r   )r   constant_valuesr   r   )r   r   r   r   CONSTANT)r   rR   r   r   r   r   r+   r,   rS   rT   padded_heightpadded_widthpad_sizes                r(   r   zMllamaImageProcessor.pad1  s    8 	t$25FW$X!k,8)/(4>9&g645<+;U7VW%%#/
 r*   c                     t        |       t        ||      \  }}|d   }	t        ||||	      \  }
}|
|	z  }||	z  }t        |||
||	      \  }}t	        |||f|||      }|||ffS )a  
        Resizes an image to fit within a tiled canvas while maintaining its aspect ratio.
        The optimal canvas size is calculated based on the maximum number of tiles and the tile size.

        The function first determines the best tile arrangement for the image, then resizes the image
        to fit within this canvas. The resized image and the number of tiles along the height and width
        dimensions are returned.

        Args:
            image (`np.ndarray`):
                Image to resize.
            size (`Dict[str, int]`):
                Size of the output image.
            max_image_tiles (`int`):
                The maximum number of tiles to split the image into.
            resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`):
                Resampling filter to use when resizing the image.
            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.

        Returns:
            `Union[np.ndarray, Tuple[int, int]]`: The resized image and a tuple containing the number of tiles
            along the height and width dimensions.
        r   r'   )r+   r,   r   r/   )r+   r,   r-   r.   r/   )r   r   r   )r   r   rQ   r<   r   )r   rR   r   r   r   r   r   r+   r,   r/   r-   r.   rS   rT   r;   r:   s                   r(   r   zMllamaImageProcessor.resize`  s    H 	t$25FW$X!kN	&>%#+	'
#| )I5&)3 <%#'%!

I ##/
 '999r*   )NNNNNNNNNNNNN)NN)__name__
__module____qualname____doc__model_input_namesr   BILINEARboolr   r   strintfloatr   r   r   r   r   r   r   r1   r   r   r   r   __classcell__)r   s   @r(   r   r   :  sW   @ _  $)-'9'B'B '!:>9= ll l tCH~&	l
 %l l l l U5$u+#567l E%e"456l l l 
lB *.$()-15%)*.'+:>9=!%)-DH;?tt !t D>	t
 tCH~&t -.t TNt !t tnt U5$u+#567t E%e"456t t "#t $E#/?*?$@At !sJ!78tv ?CDH-zz- 38n- CHo	-
 eC)9$9:;- $E#/?*?$@A- 
-h (:'B'B>BDHB:zzB: 38nB: 	B:
 %B: eC)9$9:;B: $E#/?*?$@AB: 
rzz5c?*	+B:r*   r   )r"   r   )8r4   	functoolsr   typingr   r   r   r   r   numpyr1   image_processing_utilsr
   r   image_transformsr   r   r   r   image_utilsr   r   r   r   r   r   r   r   r   r   utilsr   r   r   r   
get_loggerr   loggerr   r)   r<   rQ   r   r_   ro   r{   r   r   r   r   r   r   r   r   r   r   r   r*   r(   <module>r      s      5 5  F    )  
		H	% 2S T%S/=R  :.!.!.! .! 	.!
 .! 38_.!b 3RRR R 	R
 38_R Rj'"** ' 'c 'VXV`V` '>4U38_0E+F Y\ acakak B2)tBJJ'(2)2) 2::tDI&'2)j!d4c3h+@&A !c !Z\ZdZd !0T%S/5J0K ^a fhfpfp D AE%::%',-%  &6&; <=% ZZ	%R*  0
 tD"**9M4N/O >ED E]c3h ]D ]h:- h:r*   