
    Vpf5                       d dl mZ d dlmZmZ d dlmZ d dlZd dlZ	d dl
mZ d dl
mZ d dl
mZ d dlmZ d d	lmZ d d
lmZ d Zd Zd Zd)dZd*dZ G d dej                  Zej        eej        d ej        d ej        eiZdej         j!        fd+d#Z"d,d$Z# eed%&          d-d'            Z$dej         j!        fd-d(Z%dS ).    )annotations)CallableSequence)partialN)jit)lax)numpy)core)canonicalize_axis)promote_dtypes_inexactc                x   | t          j        t          j        |z            z  t          j        t          j        |z  | z            z  }t          j        |dk    t          j        |t          j        |dk    t          j        dz  |dz  z  d                    d          }t          j        || k    d|          S )NgMbP?r                 )jnpsinnppiwheredivide)radiusxyouts       T/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/_src/image/scale.py_fill_lanczos_kernelr      s    swruqy!!!CGBEAI,>$?$??!	!d(CJq#)AFBE1Hq!tOQ*O*OPPRSTT#	1v:r3	'	''    c                    d| z  dz
  | z  | z  dz   }t          j        | dk    d| z  dz   | z  dz
  | z  dz   |          }t          j        | dk    d|          S )Ng      ?g      @      ?      g      @g       @r   )r   r   )r   r   s     r   _fill_keys_cubic_kernelr!   &   sk    
 'C-1	!B&#	!r'TAX^q025:R?EE#	17B	$	$$r   c                V    t          j        ddt          j        |           z
            S )Nr   r   )r   maximumabsr   s    r   _fill_triangle_kernelr&   0   s     	QCGAJJ	'	''r   
input_sizecore.DimSizeoutput_sizekernelr   	antialiasboolc                j   t          j        ||          }d|z  }|rt          j        |d          nd}t          j        ||          dz   |z  ||z  z
  dz
  }	t          j        |	t           j        d d f         t          j        | |          d d t           j        f         z
            |z  }
 ||
          }t          j        |dd          }t          j        t          j        |          dt          t          j
        t          j                  j                  z  k    t          j        |t          j        |dk    |d                    d          }t          j        |           dz
  }t          j        t          j        |	d	k    |	|k              t           j        d d f         |d          S )
Nr   dtype      ?r   T)axiskeepdimsg     @@r   r    )r   result_typer#   aranger$   newaxissumr   floatr   finfofloat32epsr   r
   dimension_as_valuelogical_and)r'   r)   scaletranslationr*   r+   r/   	inv_scalekernel_scalesample_fr   weightstotal_weight_suminput_size_minus_0_5s                 r   compute_weight_matrE   4   s    /%
-
-%5j) 09@Y+++b,z+U333c9YFI%&(+,( 
ghs{AAA~&j5111!!!S[.AB C C  F1II'WW1t<<<I	g%%0D0D0H*I*I"II	j#)$4$9<LaPPQQ	 	' 0<<sB		oh$&"668 88;QQQHIPRS
U 
U Ur   output_shape
core.Shapespatial_dimsSequence[int]c           	        | j         }t          |          t          |          k    sJ t          |          t          |          k    sJ t          |          t          |          k    sJ t          |          dk    r| S g }	t          t          t          |                              }
t          t          t          |                              }t	          |          D ]\  }}t          || j                  }||         }||         }t          ||||         ||         ||                              | j	                  }|	
                    |           |	
                    |t          |          |z   g           t          |          |z   ||<   |	
                    |           t          j        | |
g|	R d|iS )Nr   	precision)shapelenlistrange	enumerater   ndimrE   astyper/   appendr   einsum)r   rF   rH   r=   r>   r*   r+   rK   input_shapecontractions
in_indicesout_indicesidmnws                    r   _scale_and_translater^   V   s    +	[		S..	.	.	.	.	\		c%jj	(	(	(	(	\		c+..	.	.	.	.!H,E#l++,,--*U3|,,--..+%% + +da!QV$$AAAQA1aq;q>!9	. 	..4fQWoo C--12333&&*KNNk"""	Az	FL	F	F	FI	F	FFr   c                  >    e Zd ZdZdZdZdZdZdZe	dd	            Z
d
S )ResizeMethoda>  Image resize method.

  Possible values are:

  NEAREST:
    Nearest-neighbor interpolation.

  LINEAR:
    `Linear interpolation`_.

  LANCZOS3:
    `Lanczos resampling`_, using a kernel of radius 3.

  LANCZOS5:
    `Lanczos resampling`_, using a kernel of radius 5.

  CUBIC:
    `Cubic interpolation`_, using the Keys cubic kernel.

  .. _Linear interpolation: https://en.wikipedia.org/wiki/Bilinear_interpolation
  .. _Cubic interpolation: https://en.wikipedia.org/wiki/Bicubic_interpolation
  .. _Lanczos resampling: https://en.wikipedia.org/wiki/Lanczos_resampling
  r   r   r         sstrc                    | dk    rt           j        S | dv rt           j        S | dk    rt           j        S | dk    rt           j        S | dv rt           j        S t          d|  d          )Nnearest)linearbilinear	trilineartrianglelanczos3lanczos5)cubicbicubictricubiczUnknown resize method "")r`   NEARESTLINEARLANCZOS3LANCZOS5CUBIC
ValueError)rc   s    r   from_stringzResizeMethod.from_string   s|    I~~!!;;;  	
j""	
j""	
.	.	.5555666r   N)rc   rd   )__name__
__module____qualname____doc__rq   rr   rs   rt   ru   staticmethodrw    r   r   r`   r`   o   sY         0 '&((
% 7 7 7 <7 7 7r   r`   c                "    t          d|           S )Ng      @r   r%   s    r   <lambda>r          %9"a%@%@ r   c                "    t          d|           S )Ng      @r   r%   s    r   r   r      r   r   TrL   methodstr | ResizeMethodc           
        t          j        |          }t          |          | j        k    rd| d| j         }t          |          t          |t                    rt          	                    |          }|t          j
        k    rt          d          t          |t                    sJ t          |         }	t          |           \  } t          ||          \  }}t          | |||||	||          S )a	  Apply a scale and translation to an image.

  Generates a new image of shape 'shape' by resampling from the input image
  using the sampling method corresponding to method. For 2D images, this
  operation transforms a location in the input images, (x, y), to a location
  in the output image according to::

    (x * scale[1] + translation[1], y * scale[0] + translation[0])

  (Note the *inverse* warp is used to generate the sample locations.)
  Assumes half-centered pixels, i.e the pixel at integer location ``row, col``
  has coordinates ``y, x = row + 0.5, col + 0.5``, and similarly for other input
  image dimensions.

  If an output location(pixel) maps to an input sample location that is outside
  the input boundaries then the value for the output location will be set to
  zero.

  The ``method`` argument expects one of the following resize methods:

  ``ResizeMethod.LINEAR``, ``"linear"``, ``"bilinear"``, ``"trilinear"``,
    ``"triangle"`` `Linear interpolation`_. If ``antialias`` is ``True``, uses a
    triangular filter when downsampling.

  ``ResizeMethod.CUBIC``, ``"cubic"``, ``"bicubic"``, ``"tricubic"``
    `Cubic interpolation`_, using the Keys cubic kernel.

  ``ResizeMethod.LANCZOS3``, ``"lanczos3"``
    `Lanczos resampling`_, using a kernel of radius 3.

  ``ResizeMethod.LANCZOS5``, ``"lanczos5"``
    `Lanczos resampling`_, using a kernel of radius 5.

  .. _Linear interpolation: https://en.wikipedia.org/wiki/Bilinear_interpolation
  .. _Cubic interpolation: https://en.wikipedia.org/wiki/Bicubic_interpolation
  .. _Lanczos resampling: https://en.wikipedia.org/wiki/Lanczos_resampling

  Args:
    image: a JAX array.
    shape: the output shape, as a sequence of integers with length equal to the
      number of dimensions of `image`.
    spatial_dims: A length K tuple specifying the spatial dimensions that the
      passed scale and translation should be applied to.
    scale: A [K] array with the same number of dimensions as image, containing
      the scale to apply in each dimension.
    translation: A [K] array with the same number of dimensions as image,
      containing the translation to apply in each dimension.
    method: the resizing method to use; either a ``ResizeMethod`` instance or a
      string. Available methods are: LINEAR, LANCZOS3, LANCZOS5, CUBIC.
    antialias: Should an antialiasing filter be used when downsampling? Defaults
      to ``True``. Has no effect when upsampling.

  Returns:
    The scale and translated image.
  @shape must have length equal to the number of dimensions of x;   vs zONearest neighbor resampling is not currently supported for scale_and_translate.)r
   canonicalize_shaperM   rQ   rL   rv   
isinstancerd   r`   rw   rq   _kernelsr   r^   )
imagerL   rH   r=   r>   r   r+   rK   msgr*   s
             r   scale_and_translater      s	   z 
!%
(
(%ZZ5:(( (+( (C
S// .%%f--F|###  0 1 1 1	FL	)	))))F&!%((&%-e[AA%	eUL%$i
< 
< <r   c                   | j         t                    t                    k    sJ t          fdt          t                              D                       }|D ]}|         }|         }t	          j        |t          j                  dz   t          j	        |          z  t          j	        |          z  }t	          j
        |                    t          j                                                t          j                  }t          d           gt                    z  }|||<   | t          |                   } | S )Nc              3  ^   K   | ]'}t          j        |         |                   #|V  (d S N)r
   definitely_equal).0rY   rU   rF   s     r   	<genexpr>z"_resize_nearest.<locals>.<genexpr>  sZ       V VQ"3KNLQROTTVq V V V V V Vr   r.   r0   )rL   rM   tuplerO   r   r4   r   r9   r
   r;   floorrR   int32slice)	r   rF   rH   rZ   r[   r\   offsetsindicesrU   s	    `      @r   _resize_nearestr      sG   +	[		S..	.	.	.	. V V V V V%K(8(8"9"9 V V V V V, 	 	aAAQAz!2:...48OPQ8R8RRUYUlmnUoUooG irz2233::28DDGT{{mc+...GGAJ	%..AA	
(r   )r   r   ra   rb   )static_argnumsc           
     N    t                     j        k    rd d j         }t          |          t	          |t
                    rt                              |          }|t          j        k    rt                     S t	          |t                    sJ t          |         }t                     \   t           fdt          t                              D                       } fd|D             }t           ||dgt          |          z  |||          S )Nr   r   c              3  h   K   | ],}t          j        j        |         |                   (|V  -d S r   )r
   r   rL   )r   rY   r   rL   s     r   r   z_resize.<locals>.<genexpr>"  s[       O OQ"3EKNE!HMMOq O O O O O Or   c                    g | ]Y}t          j        |         d           rdn9t          j        |                   t          j        j        |                   z  ZS )r   r   )r
   r   r;   rL   )r   rZ   r   rL   s     r   
<listcomp>z_resize.<locals>.<listcomp>$  s     " " " 'a!44  F33$:QRWXYRZ:[:[^b^uv{  wB  CD  wE  _F  _F  ;F " " "r   r   )rM   rQ   rL   rv   r   rd   r`   rw   rq   r   r   r   r   rO   r^   )	r   rL   r   r+   rK   r   r*   rH   r=   s	   ``       r   _resizer     sm    	ZZ5:(( (+( (C
S// .%%f--F|###5%(((	FL	)	))))F&!%((&%  O O O O O%E

"3"3 O O O O O," " " " " " " "%	eUL#bTC,=,=%=v'
4 
4 4r   c                L    t          | t          j        |          |||          S )a  Image resize.

  The ``method`` argument expects one of the following resize methods:

  ``ResizeMethod.NEAREST``, ``"nearest"``
    `Nearest neighbor interpolation`_. The values of ``antialias`` and
    ``precision`` are ignored.

  ``ResizeMethod.LINEAR``, ``"linear"``, ``"bilinear"``, ``"trilinear"``, ``"triangle"``
    `Linear interpolation`_. If ``antialias`` is ``True``, uses a triangular
    filter when downsampling.

  ``ResizeMethod.CUBIC``, ``"cubic"``, ``"bicubic"``, ``"tricubic"``
    `Cubic interpolation`_, using the Keys cubic kernel.

  ``ResizeMethod.LANCZOS3``, ``"lanczos3"``
    `Lanczos resampling`_, using a kernel of radius 3.

  ``ResizeMethod.LANCZOS5``, ``"lanczos5"``
    `Lanczos resampling`_, using a kernel of radius 5.

  .. _Nearest neighbor interpolation: https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation
  .. _Linear interpolation: https://en.wikipedia.org/wiki/Bilinear_interpolation
  .. _Cubic interpolation: https://en.wikipedia.org/wiki/Bicubic_interpolation
  .. _Lanczos resampling: https://en.wikipedia.org/wiki/Lanczos_resampling

  Args:
    image: a JAX array.
    shape: the output shape, as a sequence of integers with length equal to
      the number of dimensions of `image`. Note that :func:`resize` does not
      distinguish spatial dimensions from batch or channel dimensions, so this
      includes all dimensions of the image. To represent a batch or a channel
      dimension, simply leave that element of the shape unchanged.
    method: the resizing method to use; either a ``ResizeMethod`` instance or a
      string. Available methods are: LINEAR, LANCZOS3, LANCZOS5, CUBIC.
    antialias: should an antialiasing filter be used when downsampling? Defaults
      to ``True``. Has no effect when upsampling.
  Returns:
    The resized image.
  )r   r
   r   )r   rL   r   r+   rK   s        r   resizer   +  s-    V 
/66	
 
 r   )r'   r(   r)   r(   r*   r   r+   r,   )rF   rG   rH   rI   r+   r,   )rL   rG   rH   rI   r   r   r+   r,   )rF   rG   )rL   rG   r   r   r+   r,   )&
__future__r   collections.abcr   r   	functoolsr   enumr	   r   jaxr   r   r   jax._srcr
   jax._src.utilr   jax._src.numpy.utilr   r   r!   r&   rE   r^   Enumr`   rr   rs   rt   ru   r   	PrecisionHIGHESTr   r   r   r   r}   r   r   <module>r      s"   # " " " " " . . . . . . . .                                    + + + + + + 6 6 6 6 6 6( ( (% % %( ( (U U U UDG G G G217 17 17 17 1749 17 17 17h .@@@@/	 +/"%-"7O< O< O< O< O<d   $ 	\***4 4 4 +*46 "},, , , , , , ,r   