o
    f                     @   s   d dl Z ddlmZmZ d Zdd ZG dd dZdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zi Zee  D ]\ZZedd dkrReeedd < q>i fddZdS )    N   )Image_imagingmathc                 C   s   t | ttfS N)
isinstanceintfloat)v r
   //usr/lib/python3/dist-packages/PIL/ImageMath.py_isconstant      r   c                   @   s"  e Zd ZdZdd Zdd ZdGddZd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#dCdD Z$dEdF Z%dS )H_Operandz4Wraps an image operand, providing standard operatorsc                 C   s
   || _ d S r   )im)selfr   r
   r
   r   __init__    s   
z_Operand.__init__c                 C   s~   t |tr#|jjdv r|jdS |jjdv r|jS td|jj t|r6| jjdv r6td| jj	|S td| jj	|S )N)1LI)r   Fzunsupported mode: )r   r   r   r   )
r   r   r   modeconvert
ValueErrorr   r   newsize)r   im1r
   r
   r   __fixup#   s   
z_Operand.__fixupNc              
   C   s  |  |}|d u rIt|p|j|jd }|  ztt|d |j }W n ty9 } z	t	d| d|d }~ww t
||jj|jj t|S |  |}|j|jkrr|jdkr^|d}|jdkrh|d}|j|jkrrtd|j|jkrt|jd |jd t|jd |jd f}|j|kr|d| }|j|kr|d| }t|p|j|d }nt|p|j|jd }|  |  ztt|d |j }W n ty } z	t	d| d|d }~ww t||jj|jj|jj t|S )	N_zbad operand type for ''r   zmode mismatchr   r   )r   r   )_Operand__fixupr   r   r   r   loadgetattrr   AttributeError	TypeErrorunopr   idr   r   mincropbinopr   )r   opr   im2r   outer   r
   r
   r   apply4   sL   





,

z_Operand.applyc                 C   s   | j  d uS r   )r   getbboxr   r
   r
   r   __bool__^   s   z_Operand.__bool__c                 C      |  d| S )Nabsr-   r/   r
   r
   r   __abs__b      z_Operand.__abs__c                 C   s   | S r   r
   r/   r
   r
   r   __pos__e   s   z_Operand.__pos__c                 C   r1   )Nnegr3   r/   r
   r
   r   __neg__h   r5   z_Operand.__neg__c                 C      |  d| |S Naddr3   r   otherr
   r
   r   __add__l   r   z_Operand.__add__c                 C      |  d|| S r:   r3   r<   r
   r
   r   __radd__o   r   z_Operand.__radd__c                 C   r9   Nsubr3   r<   r
   r
   r   __sub__r   r   z_Operand.__sub__c                 C   r?   rA   r3   r<   r
   r
   r   __rsub__u   r   z_Operand.__rsub__c                 C   r9   Nmulr3   r<   r
   r
   r   __mul__x   r   z_Operand.__mul__c                 C   r?   rE   r3   r<   r
   r
   r   __rmul__{   r   z_Operand.__rmul__c                 C   r9   Ndivr3   r<   r
   r
   r   __truediv__~   r   z_Operand.__truediv__c                 C   r?   rI   r3   r<   r
   r
   r   __rtruediv__   r   z_Operand.__rtruediv__c                 C   r9   Nmodr3   r<   r
   r
   r   __mod__   r   z_Operand.__mod__c                 C   r?   rM   r3   r<   r
   r
   r   __rmod__   r   z_Operand.__rmod__c                 C   r9   Npowr3   r<   r
   r
   r   __pow__   r   z_Operand.__pow__c                 C   r?   rQ   r3   r<   r
   r
   r   __rpow__   r   z_Operand.__rpow__c                 C   r1   )Ninvertr3   r/   r
   r
   r   
__invert__   r5   z_Operand.__invert__c                 C   r9   Nandr3   r<   r
   r
   r   __and__   r   z_Operand.__and__c                 C   r?   rW   r3   r<   r
   r
   r   __rand__   r   z_Operand.__rand__c                 C   r9   Norr3   r<   r
   r
   r   __or__   r   z_Operand.__or__c                 C   r?   r[   r3   r<   r
   r
   r   __ror__   r   z_Operand.__ror__c                 C   r9   Nxorr3   r<   r
   r
   r   __xor__   r   z_Operand.__xor__c                 C   r?   r_   r3   r<   r
   r
   r   __rxor__   r   z_Operand.__rxor__c                 C   r9   )Nlshiftr3   r<   r
   r
   r   
__lshift__   r   z_Operand.__lshift__c                 C   r9   )Nrshiftr3   r<   r
   r
   r   
__rshift__   r   z_Operand.__rshift__c                 C   r9   )Neqr3   r<   r
   r
   r   __eq__   r   z_Operand.__eq__c                 C   r9   )Nner3   r<   r
   r
   r   __ne__   r   z_Operand.__ne__c                 C   r9   )Nltr3   r<   r
   r
   r   __lt__   r   z_Operand.__lt__c                 C   r9   )Nler3   r<   r
   r
   r   __le__   r   z_Operand.__le__c                 C   r9   )Ngtr3   r<   r
   r
   r   __gt__   r   z_Operand.__gt__c                 C   r9   )Nger3   r<   r
   r
   r   __ge__   r   z_Operand.__ge__)NN)&__name__
__module____qualname____doc__r   r   r-   r0   r4   r6   r8   r>   r@   rC   rD   rG   rH   rK   rL   rO   rP   rS   rT   rV   rY   rZ   r]   r^   ra   rb   rd   rf   rh   rj   rl   rn   rp   rr   r
   r
   r
   r   r      sH    
*r   c                 C      t | jdS )Nr   r   r   r   r/   r
   r
   r   imagemath_int      ry   c                 C   rw   )Nr   rx   r/   r
   r
   r   imagemath_float   rz   r{   c                 C      | j d| |ddS )Nrg   r   r   r3   r<   r
   r
   r   imagemath_equal      r~   c                 C   r|   )Nri   r   r}   r3   r<   r
   r
   r   imagemath_notequal   r   r   c                 C   r9   )Nr&   r3   r<   r
   r
   r   imagemath_min   r   r   c                 C   r9   )Nmaxr3   r<   r
   r
   r   imagemath_max   r   r   c                 C   s   t | j|S r   rx   )r   r   r
   r
   r   imagemath_convert   rz   r   
   
imagemath_c                    s   t   t| t|  D ]}d|v stt|r%d| d}t|q |  | t  D ]\}}t|drEt	| |< q6t
| dd fdd t| d	d
tii }z|jW S  tyr   | Y S w )a  
    Evaluates an image expression.

    :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>evalc                    sT   | j D ]}t|tkr| q| jD ]}| vr'|dkr'td| dqd S )Nr2   r   r   )	co_conststypeco_namesr   )codeconstnameargscompiled_codescanr
   r   r      s   

zeval.<locals>.scan
__builtinsr2   )opscopylistkeyshasattrbuiltinsr   updateitemsr   compiler   r2   r   r"   )
expression_dictkwkmsgr	   r+   r
   r   r   r      s*   


	r   )r    r   r   VERBOSEr   r   ry   r{   r~   r   r   r   r   r   r   globalsr   r   r	   r   r
   r
   r
   r   <module>   s&    %