U
    ڲg,                     @  s4  d dl mZ d dlZd dlmZ d dlmZmZ ddlm	Z	m
Z
 ddlmZ G dd	 d	Zd	d	d
ddZd	d	d
ddZd	dd	dddZd	dd	dddZd	dd	dddZd	dd	dddZd	dd	dddZeeeeeeedZi fddd d d!d"d#Zi fddd d d!d$d%Zi fddd d d&d'd(ZdS ))    )annotationsN)CodeType)AnyCallable   )Image_imagingmath)	deprecatec                   @  s  e Zd ZdZddddZddddd	ZdRddddd dddZddddZd dddZd dddZ	d dddZ
dd dddZdd dddZdd dd d!Zdd dd"d#Zdd dd$d%Zdd dd&d'Zdd dd(d)Zdd dd*d+Zdd dd,d-Zdd dd.d/Zdd dd0d1Zdd dd2d3Zd dd4d5Zdd dd6d7Zdd dd8d9Zdd dd:d;Zdd dd<d=Zdd dd>d?Zdd dd@dAZdd ddBdCZdd ddDdEZdFdG Z dHdI Z!dd ddJdKZ"dd ddLdMZ#dd ddNdOZ$dd ddPdQZ%d
S )S_Operandz4Wraps an image operand, providing standard operatorszImage.Imageimc                 C  s
   || _ d S Nr   )selfr    r   1/tmp/pip-unpacked-wheel-xela2va7/PIL/ImageMath.py__init__   s    z_Operand.__init__z_Operand | float)im1returnc                 C  s   t |trL|jjdkr"|jdS |jjdkr4|jS d|jj }t|n>t |ttfrx| jjdkrxt	d| jj
|S t	d| jj
|S d S )N)1LI)r   Fzunsupported mode: )r   r   r   r   )
isinstancer
   r   modeconvert
ValueErrorintfloatr   newsize)r   r   msgr   r   r   Z__fixup!   s    

z_Operand.__fixupNstr_Operand | float | Nonez
str | None)opr   im2r   r   c              
   C  s  |  |}|d krt|p|j|jd }|  ztt| d|j }W n8 tk
r } zd| d}t	||W 5 d }~X Y nX t
||jj|jj nF|  |}	|j|	jkr|jdkr|d}|	jdkr|	d}	|j|	jkrJt|jd |	jd t|jd |	jd f}
|j|
kr0|d|
 }|	j|
krJ|	d|
 }	t|pX|j|jd }|  |	  ztt| d|j }W n: tk
r } zd| d}t	||W 5 d }~X Y nX t||jj|jj|	jj t|S )N_zbad operand type for ''r   r   r   )r   r   )_Operand__fixupr   r   r   r   loadgetattrr   AttributeError	TypeErrorZunopr   idr   minZcropZbinopr
   )r   r#   r   r$   r   Zim_1outer    Zim_2r   r   r   r   apply3   sD    





z_Operand.applybool)r   c                 C  s   | j  d k	S r   )r   Zgetbboxr   r   r   r   __bool__d   s    z_Operand.__bool__c                 C  s   |  d| S )Nabsr0   r2   r   r   r   __abs__h   s    z_Operand.__abs__c                 C  s   | S r   r   r2   r   r   r   __pos__k   s    z_Operand.__pos__c                 C  s   |  d| S )Nnegr5   r2   r   r   r   __neg__n   s    z_Operand.__neg__)otherr   c                 C  s   |  d| |S Naddr5   r   r:   r   r   r   __add__r   s    z_Operand.__add__c                 C  s   |  d|| S r;   r5   r=   r   r   r   __radd__u   s    z_Operand.__radd__c                 C  s   |  d| |S Nsubr5   r=   r   r   r   __sub__x   s    z_Operand.__sub__c                 C  s   |  d|| S r@   r5   r=   r   r   r   __rsub__{   s    z_Operand.__rsub__c                 C  s   |  d| |S Nmulr5   r=   r   r   r   __mul__~   s    z_Operand.__mul__c                 C  s   |  d|| S rD   r5   r=   r   r   r   __rmul__   s    z_Operand.__rmul__c                 C  s   |  d| |S Ndivr5   r=   r   r   r   __truediv__   s    z_Operand.__truediv__c                 C  s   |  d|| S rH   r5   r=   r   r   r   __rtruediv__   s    z_Operand.__rtruediv__c                 C  s   |  d| |S Nmodr5   r=   r   r   r   __mod__   s    z_Operand.__mod__c                 C  s   |  d|| S rL   r5   r=   r   r   r   __rmod__   s    z_Operand.__rmod__c                 C  s   |  d| |S Npowr5   r=   r   r   r   __pow__   s    z_Operand.__pow__c                 C  s   |  d|| S rP   r5   r=   r   r   r   __rpow__   s    z_Operand.__rpow__c                 C  s   |  d| S )Ninvertr5   r2   r   r   r   
__invert__   s    z_Operand.__invert__c                 C  s   |  d| |S Nandr5   r=   r   r   r   __and__   s    z_Operand.__and__c                 C  s   |  d|| S rV   r5   r=   r   r   r   __rand__   s    z_Operand.__rand__c                 C  s   |  d| |S Norr5   r=   r   r   r   __or__   s    z_Operand.__or__c                 C  s   |  d|| S rZ   r5   r=   r   r   r   __ror__   s    z_Operand.__ror__c                 C  s   |  d| |S Nxorr5   r=   r   r   r   __xor__   s    z_Operand.__xor__c                 C  s   |  d|| S r^   r5   r=   r   r   r   __rxor__   s    z_Operand.__rxor__c                 C  s   |  d| |S )Nlshiftr5   r=   r   r   r   
__lshift__   s    z_Operand.__lshift__c                 C  s   |  d| |S )Nrshiftr5   r=   r   r   r   
__rshift__   s    z_Operand.__rshift__c                 C  s   |  d| |S )Neqr5   r=   r   r   r   __eq__   s    z_Operand.__eq__c                 C  s   |  d| |S )Nner5   r=   r   r   r   __ne__   s    z_Operand.__ne__c                 C  s   |  d| |S )Nltr5   r=   r   r   r   __lt__   s    z_Operand.__lt__c                 C  s   |  d| |S )Nler5   r=   r   r   r   __le__   s    z_Operand.__le__c                 C  s   |  d| |S )Ngtr5   r=   r   r   r   __gt__   s    z_Operand.__gt__c                 C  s   |  d| |S )Nger5   r=   r   r   r   __ge__   s    z_Operand.__ge__)NN)&__name__
__module____qualname____doc__r   r'   r0   r3   r6   r7   r9   r>   r?   rB   rC   rF   rG   rJ   rK   rN   rO   rR   rS   rU   rX   rY   r\   r]   r`   ra   rc   re   rg   ri   rk   rm   ro   rq   r   r   r   r   r
      sJ     1r
   )r   r   c                 C  s   t | jdS )Nr   r
   r   r   r2   r   r   r   imagemath_int   s    rw   c                 C  s   t | jdS )Nr   rv   r2   r   r   r   imagemath_float   s    rx   r"   )r   r:   r   c                 C  s   | j d| |ddS )Nrf   r   r   r5   r=   r   r   r   imagemath_equal   s    rz   c                 C  s   | j d| |ddS )Nrh   r   ry   r5   r=   r   r   r   imagemath_notequal   s    r{   c                 C  s   |  d| |S )Nr-   r5   r=   r   r   r   imagemath_min   s    r|   c                 C  s   |  d| |S )Nmaxr5   r=   r   r   r   imagemath_max   s    r~   r!   )r   r   r   c                 C  s   t | j|S r   rv   )r   r   r   r   r   imagemath_convert   s    r   )r   r   equalZnotequalr-   r}   r   zCallable[[dict[str, Any]], Any]zdict[str, Any]r   )
expressionoptionskwr   c                 K  sr   t  }|| || | D ]\}}t|dr$t|||< q$| |}z|jW S  tk
rl   | Y S X dS )a  
    Returns the result of an image function.

    :py:mod:`~PIL.ImageMath` only supports single-layer images. To process multi-band
    images, use the :py:meth:`~PIL.Image.Image.split` method or
    :py:func:`~PIL.Image.merge` function.

    :param expression: A function that receives a dictionary.
    :param options: Values to add to the function's dictionary. You
                    can either use a dictionary, or one or more keyword
                    arguments.
    :return: The expression result. This is usually an image object, but can
             also be an integer, a floating point value, or a pixel tuple,
             depending on the expression.
    r   N)opscopyupdateitemshasattrr
   r   r*   )r   r   r   argskvr.   r   r   r   lambda_eval   s    


r   c                   s   t   t| t|  D ]*}d|ks6tt|r d| d}t|q  |  |   D ]\}}t|drht	| |< qht
| ddddd	 fd
d t| ddtii }z|jW S  tk
r   | Y S X dS )a  
    Evaluates an image expression. This uses Python's ``eval()`` function to process
    the expression string, and carries the security risks of doing so. It is not
    recommended to process expressions without considering this.
    :py:meth:`~lambda_eval` is a more secure alternative.

    :py:mod:`~PIL.ImageMath` only supports single-layer images. To process multi-band
    images, use the :py:meth:`~PIL.Image.Image.split` method or
    :py:func:`~PIL.Image.merge` function.

    :param expression: A string containing a Python-style expression.
    :param options: Values to add to the evaluation context.  You
                    can either use a dictionary, or one or more keyword
                    arguments.
    :return: The evaluated expression. This is usually an image object, but can
             also be an integer, a floating point value, or a pixel tuple,
             depending on the expression.
    __r&   ' not allowedr   z<string>evalr   None)coder   c                   sX   | j D ]}t|tkr| q| jD ](}| kr*|dkr*d| d}t|q*d S )Nr4   r&   r   )	co_conststypeco_namesr   )r   constnamer    r   Zcompiled_codescanr   r   r   9  s    


zunsafe_eval.<locals>.scanZ
__builtinsr4   N)r   r   listkeysr   builtinsr   r   r   r
   compiler   r4   r   r*   )r   r   r   r   r    r   r.   r   r   r   unsafe_eval  s$    




r   )r   _dictr   r   c                 K  s   t ddd t| |f|S )a1  
    Evaluates an image expression.

    Deprecated. Use lambda_eval() or unsafe_eval() instead.

    :param expression: A string containing a Python-style expression.
    :param _dict: Values to add to the evaluation context.  You
                  can either use a dictionary, or one or more keyword
                  arguments.
    :return: The evaluated expression. This is usually an image object, but can
             also be an integer, a floating point value, or a pixel tuple,
             depending on the expression.

    ..  deprecated:: 10.3.0
    zImageMath.eval   z.ImageMath.lambda_eval or ImageMath.unsafe_eval)r	   r   )r   r   r   r   r   r   r   K  s    r   )
__future__r   r   typesr   typingr   r    r   r   Z
_deprecater	   r
   rw   rx   rz   r{   r|   r~   r   r   r   r   r   r   r   r   r   <module>   s8    -%;