B
    6d                 @   s   d dl Z ddlmZm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x:ee  D ](\ZZedd dkrzeeedd < qzW i fddZdS )    N   )Image_imagingmathc             C   s   t | ttfS )N)
isinstanceintfloat)v r	   1/tmp/pip-unpacked-wheel-lxgm_btu/PIL/ImageMath.py_isconstant   s    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 )N)im)selfr   r	   r	   r
   __init__   s    z_Operand.__init__c             C   s   t |trL|jjdkr"|jdS |jjdkr4|jS d|jj }t|n8t|rr| jjdkrrtd| jj	|S td| jj	|S d S )N)1LI)r   Fzunsupported mode: )r   r   r   r   )
r   r   r   modeconvert
ValueErrorr   r   newsize)r   im1msgr	   r	   r
   Z__fixup!   s    

z_Operand.__fixupNc       	   
   C   s  |  |}|d krt|p|j|jd }|  ytt|d |j }W n8 tk
r } zd| d}t	||W d d }~X Y nX t
||jj|jj nD|  |}|j|jkr|jdkr|d}|jdkr|d}|j|jkrHt|jd |jd t|jd |jd f}|j|kr.|d| }|j|krH|d| }t|pV|j|jd }|  |  ytt|d |j }W n: tk
r } zd| d}t	||W d 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   opr   Zim2r   outer   r   r	   r	   r
   apply3   s@    





,z_Operand.applyc             C   s   | j  d k	S )N)r   Zgetbbox)r   r	   r	   r
   __bool__[   s    z_Operand.__bool__c             C   s   |  d| S )Nabs)r'   )r   r	   r	   r
   __abs___   s    z_Operand.__abs__c             C   s   | S )Nr	   )r   r	   r	   r
   __pos__b   s    z_Operand.__pos__c             C   s   |  d| S )Nneg)r'   )r   r	   r	   r
   __neg__e   s    z_Operand.__neg__c             C   s   |  d| |S )Nadd)r'   )r   otherr	   r	   r
   __add__i   s    z_Operand.__add__c             C   s   |  d|| S )Nr.   )r'   )r   r/   r	   r	   r
   __radd__l   s    z_Operand.__radd__c             C   s   |  d| |S )Nsub)r'   )r   r/   r	   r	   r
   __sub__o   s    z_Operand.__sub__c             C   s   |  d|| S )Nr2   )r'   )r   r/   r	   r	   r
   __rsub__r   s    z_Operand.__rsub__c             C   s   |  d| |S )Nmul)r'   )r   r/   r	   r	   r
   __mul__u   s    z_Operand.__mul__c             C   s   |  d|| S )Nr5   )r'   )r   r/   r	   r	   r
   __rmul__x   s    z_Operand.__rmul__c             C   s   |  d| |S )Ndiv)r'   )r   r/   r	   r	   r
   __truediv__{   s    z_Operand.__truediv__c             C   s   |  d|| S )Nr8   )r'   )r   r/   r	   r	   r
   __rtruediv__~   s    z_Operand.__rtruediv__c             C   s   |  d| |S )Nmod)r'   )r   r/   r	   r	   r
   __mod__   s    z_Operand.__mod__c             C   s   |  d|| S )Nr;   )r'   )r   r/   r	   r	   r
   __rmod__   s    z_Operand.__rmod__c             C   s   |  d| |S )Npow)r'   )r   r/   r	   r	   r
   __pow__   s    z_Operand.__pow__c             C   s   |  d|| S )Nr>   )r'   )r   r/   r	   r	   r
   __rpow__   s    z_Operand.__rpow__c             C   s   |  d| S )Ninvert)r'   )r   r	   r	   r
   
__invert__   s    z_Operand.__invert__c             C   s   |  d| |S )Nand)r'   )r   r/   r	   r	   r
   __and__   s    z_Operand.__and__c             C   s   |  d|| S )NrC   )r'   )r   r/   r	   r	   r
   __rand__   s    z_Operand.__rand__c             C   s   |  d| |S )Nor)r'   )r   r/   r	   r	   r
   __or__   s    z_Operand.__or__c             C   s   |  d|| S )NrF   )r'   )r   r/   r	   r	   r
   __ror__   s    z_Operand.__ror__c             C   s   |  d| |S )Nxor)r'   )r   r/   r	   r	   r
   __xor__   s    z_Operand.__xor__c             C   s   |  d|| S )NrI   )r'   )r   r/   r	   r	   r
   __rxor__   s    z_Operand.__rxor__c             C   s   |  d| |S )Nlshift)r'   )r   r/   r	   r	   r
   
__lshift__   s    z_Operand.__lshift__c             C   s   |  d| |S )Nrshift)r'   )r   r/   r	   r	   r
   
__rshift__   s    z_Operand.__rshift__c             C   s   |  d| |S )Neq)r'   )r   r/   r	   r	   r
   __eq__   s    z_Operand.__eq__c             C   s   |  d| |S )Nne)r'   )r   r/   r	   r	   r
   __ne__   s    z_Operand.__ne__c             C   s   |  d| |S )Nlt)r'   )r   r/   r	   r	   r
   __lt__   s    z_Operand.__lt__c             C   s   |  d| |S )Nle)r'   )r   r/   r	   r	   r
   __le__   s    z_Operand.__le__c             C   s   |  d| |S )Ngt)r'   )r   r/   r	   r	   r
   __gt__   s    z_Operand.__gt__c             C   s   |  d| |S )Nge)r'   )r   r/   r	   r	   r
   __ge__   s    z_Operand.__ge__)NN)&__name__
__module____qualname____doc__r   r   r'   r(   r*   r+   r-   r0   r1   r3   r4   r6   r7   r9   r:   r<   r=   r?   r@   rB   rD   rE   rG   rH   rJ   rK   rM   rO   rQ   rS   rU   rW   rY   r[   r	   r	   r	   r
   r      sF   
(r   c             C   s   t | jdS )Nr   )r   r   r   )r   r	   r	   r
   imagemath_int   s    r`   c             C   s   t | jdS )Nr   )r   r   r   )r   r	   r	   r
   imagemath_float   s    ra   c             C   s   | j d| |ddS )NrP   r   )r   )r'   )r   r/   r	   r	   r
   imagemath_equal   s    rb   c             C   s   | j d| |ddS )NrR   r   )r   )r'   )r   r/   r	   r	   r
   imagemath_notequal   s    rc   c             C   s   |  d| |S )Nr#   )r'   )r   r/   r	   r	   r
   imagemath_min   s    rd   c             C   s   |  d| |S )Nmax)r'   )r   r/   r	   r	   r
   imagemath_max   s    rf   c             C   s   t | j|S )N)r   r   r   )r   r   r	   r	   r
   imagemath_convert   s    rg   
   Z
imagemath_c                s   t    |  | x.t  D ]\}}t|dr*t| |< q*W t| dd fdd t	| ddt
ii }y|jS  tk
r   |S X dS )	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   z<string>evalc                s`   x&| j D ]}t|tkr| qW x2| jD ](}| kr0|dkr0d| d}t|q0W d S )Nr)   r   z' not allowed)	co_conststypeco_namesr   )codeconstnamer   )argscompiled_codescanr	   r
   rr      s    zeval.<locals>.scanZ
__builtinsr)   N)opscopyupdatelistitemshasattrr   compilebuiltinsri   r)   r   r    )Z
expression_dictkwkr   r%   r	   )rp   rq   rr   r
   ri      s    



ri   )rz    r   r   r   r   r`   ra   rb   rc   rd   rf   rg   rs   rv   globalsrw   r}   r   ri   r	   r	   r	   r
   <module>   s     $