a
    áñh¦.  ã                   @  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dd dœdFdG„Z dd dœ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   úM/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/PIL/ImageMath.pyÚ__init__   s    z_Operand.__init__z_Operand | float)Úim1Úreturnc                 C  sŽ   t |tƒrL|jjdv r"|j d¡S |jjdv r4|jS d|jj› �}t|ƒ‚n>t |ttfƒrx| jjdv 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 u r˜t |p|j|jd ¡}ztt|› d|j› �ƒ}W n: ty| } z"d|› d�}t|ƒ|‚W Y d }~n
d }~0 0 t 	|| 
¡ | 
¡ ¡ �n8|   |¡}	|j|	jkrÖ|jdkrÂ| d¡}|	jdkrÖ|	 d¡}	|j|	jk�rDt|jd |	jd ƒt|jd |	jd ƒf}
|j|
k�r*| d|
 ¡}|	j|
k�rD|	 d|
 ¡}	t |�pR|j|jd ¡}ztt|› d|j› �ƒ}W n< t�y² } z"d|› d�}t|ƒ|‚W Y d }~n
d }~0 0 t || 
¡ | 
¡ |	 
¡ ¡ t|ƒS )NÚ_zbad operand type for 'ú'r   r   r   )r   r   )Ú_Operand__fixupr   r   r   r   Úgetattrr   ÚAttributeErrorÚ	TypeErrorÚunopZgetimr   ÚminZcropÚbinopr
   )r   r#   r   r$   r   Zim_1ÚoutÚer    Zim_2r   r   r   r   Úapply3   s>    
 




þ z_Operand.applyÚbool)r   c                 C  s   | j  ¡ d uS r   )r   Zgetbbox©r   r   r   r   Ú__bool__a   s    z_Operand.__bool__c                 C  s   |   d| ¡S )NÚabs©r0   r2   r   r   r   Ú__abs__e   s    z_Operand.__abs__c                 C  s   | S r   r   r2   r   r   r   Ú__pos__h   s    z_Operand.__pos__c                 C  s   |   d| ¡S )NÚnegr5   r2   r   r   r   Ú__neg__k   s    z_Operand.__neg__)Úotherr   c                 C  s   |   d| |¡S ©NÚaddr5   ©r   r:   r   r   r   Ú__add__o   s    z_Operand.__add__c                 C  s   |   d|| ¡S r;   r5   r=   r   r   r   Ú__radd__r   s    z_Operand.__radd__c                 C  s   |   d| |¡S ©NÚsubr5   r=   r   r   r   Ú__sub__u   s    z_Operand.__sub__c                 C  s   |   d|| ¡S r@   r5   r=   r   r   r   Ú__rsub__x   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     û.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  s‚   |rt dddƒ t ¡ }| |¡ | |¡ | ¡ D ] \}}t|tjƒr4t|ƒ||< q4| |ƒ}z|jW S  t	y|   | Y S 0 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. Deprecated.
                    You can instead use one or more keyword arguments.
    :param **kw: Values to add to the function's dictionary.
    :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.
    zImageMath.lambda_eval optionsé   z'ImageMath.lambda_eval keyword argumentsN)
r	   ÚopsÚcopyÚupdateÚitemsr   r   r
   r   r)   )r�   r‚   rƒ   ÚargsÚkÚvr.   r   r   r   Úlambda_evalì   s"    ý

rŒ   c                   s  |rt dddƒ t ¡ ‰ t| ¡ ƒt| ¡ ƒ D ]*}d|v sFtt|ƒr0d|› d�}t|ƒ‚q0ˆ  |¡ ˆ  |¡ ˆ  	¡ D ] \}}t
|tjƒrxt|ƒˆ |< qxt| ddƒ‰d	d
dœ‡ ‡‡fdd„‰ˆˆƒ t | ddtiiˆ ¡}z|jW S  t� yü   | Y S 0 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. Deprecated.
                    You can instead use one or more keyword arguments.
    :param **kw: Values to add to the evaluation context.
    :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.
    zImageMath.unsafe_eval optionsr„   z'ImageMath.unsafe_eval keyword argumentsÚ__r&   ú' not allowedz<string>Úevalr   ÚNone)Úcoder   c                   sX   | j D ]}t|ƒtˆƒu rˆ|ƒ q| jD ](}|ˆ v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˜   D  s    


zunsafe_eval.<locals>.scanZ
__builtinsr4   N)r	   r…   r†   ÚlistÚkeysÚhasattrÚbuiltinsr   r‡   rˆ   r   r   r
   Úcompiler�   r4   r   r)   )r�   r‚   rƒ   rŠ   r    r‹   r.   r   r—   r   Úunsafe_eval  s0    ý



rž   )r�   Ú_dictrƒ   r   c                 K  s   t dddƒ t| |fi |¤Ž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.evalr„   z.ImageMath.lambda_eval or ImageMath.unsafe_eval)r	   rž   )r�   rŸ   rƒ   r   r   r   r�   V  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    *ùþ,þBþ