3
8Ad]0>  ã               @   s°   d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	 ddl
mZmZmZmZmZmZmZ ddlmZ dd	lmZ dd
lmZ ddlZdd„ ZG dd„ deƒZi add„ ZdS )zÁ Class to aid in automatically computing the 'slice' points for a specified
    ImageResource and then drawing it that it can be 'stretched' to fit a larger
    region than the original image.
é    )Úabsolute_importN)Ú
rgb_to_hls)ÚreshapeÚ
fromstringÚuint8)ÚHasPrivateTraitsÚInstanceÚIntÚListÚColorÚEnumÚBool)ÚImageResourceé   )ÚWindowColor)Úis_macc             C   sÊ   |j ƒ }t|ddƒ}|dk	rL|jƒ \}}|jƒ \}}|j| | | ||ƒ nz|jƒ \}}tr–t|dƒr–|jr–| j	t
jƒ | jt
jt
jddddƒƒƒ n| jt
j|jƒ ƒƒ | jt
jƒ | jdd||ƒ |S )z_ Recursively paint the parent's background if they have an associated
        image slice.
    Z_image_sliceNÚ_borderr   )Ú	GetParentÚgetattrZGetPositionTupleZGetSizeTupleÚfillZGetClientSizeTupler   Úhasattrr   ZSetBackgroundModeÚwxZTRANSPARENTÚSetBrushÚBrushÚColourZGetBackgroundColourÚSetPenÚTRANSPARENT_PENÚDrawRectangle)ÚdcÚwindowÚparentÚsliceÚxÚyÚdxÚdy© r&   ús/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/traitsui-6.1.2-py3.6.egg/traitsui/wx/image_slice.pyÚpaint_parent7   s    r(   c               @   sÞ   e Zd ZeeƒZedƒZeddƒZ	eddƒZ
eZeZeZeZeZeZeZeZeejƒZeejƒZeZedƒZeejƒZeejƒZeZeZe Z!e Z"eZ#eZ$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,dS )Ú
ImageSliceé
   r   é   Fc             C   sx  t jƒ }|r|j| jƒ n|j| jƒ | j| j }}	| j| j }
}|| j || j	  }}t
|
ƒ}|dkr¤ddtd|d ƒf|d |d fdtd||d  ƒfdg}n˜|dkrâ|
d |
d f|
d td|ƒfd	d
|
d |
d fg}nZ|
d |
d f|
d td|d ƒf|
d |
d f|
d td||d  ƒf|
d |
d fg}t
|ƒ}|dk�rŠddtd|d ƒf|	d |	d fdtd||d  ƒfdg}nš|dk�rÊ|d |d f|d td|ƒfdd|d |d fg}nZ|d |d f|d td|d ƒf|d |d f|d td||d  ƒf|d |d fg}|| ||  }}|d }}x˜|D ]�\}}||k�rZP |dk�rÌ|d }}xT|D ]L\}}||k�rˆP |dk�r¶| j||||||||||ƒ
 ||7 }||7 }�qtW ||7 }||7 }�qFW | j�rt|jt jt jƒƒ |j||| j ||| j ƒ |j||| j d ||| j d ƒ |j|| j ||| j |ƒ |j|| j d ||| j d |ƒ dS )zd 'Stretch fill' the specified region of a device context with the
            sliced image.
        r   r   r+   é   é   N)r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   ÚMemoryDCÚSelectObjectÚtransparent_bitmapÚopaque_bitmapr$   ÚdxsÚdysÚfdxÚfdyÚlenÚmaxÚ_fillÚdebugr   ZPenZREDÚDrawLineÚtopÚbottomÚleftÚright)Úselfr   r"   r#   r$   r%   ÚtransparentÚidcZsdxZsdyr2   r3   ZtdxZtdyÚnZpdxsZpdysÚlast_xÚlast_yÚy0Ziy0ÚidyZwdyÚx0Zix0ÚidxZwdxr&   r&   r'   r   �   sh      "&
 
 "&





zImageSlice.fillc          
   C   sÌ   |j ƒ jƒ  | _}|jƒ  | _}|jƒ  | _}tj||ƒ| _	tj
ƒ }|j| j	ƒ |jtjtƒƒ |jtjƒ |jdd||ƒ tj
ƒ }|j|ƒ |jdd|||dddd� |jtjƒ |jtjƒ | jƒ  dS )z2 Handles the 'image' trait being changed.
        r   T)ÚuseMaskN)Zcreate_imageZConvertToBitmapr0   ÚGetWidthr$   Ú	GetHeightr%   r   ZEmptyBitmapr1   r.   r/   r   r   r   r   r   r   ÚBlitZ
NullBitmapÚ_analyze_bitmap)r?   ÚimageÚbitmapr$   r%   Zmdc2Zmdcr&   r&   r'   Ú_image_changedÛ   s    
zImageSlice._image_changedc             C   s>  | j }| j}| j| j }}|jƒ }tt|jƒ tƒ||dfƒ}g }d|d  }}	d| }
xh||	k r¾|| }x0t	|d |ƒD ]}t
|||  ƒjƒ |
krxP qxW || }||kr¸|j||fƒ |}qXW t|ƒ}|dkrô|dkräd|fg}n|d dfg}n,|| jk�r |jdd„ d	d
� |d| j… }| j||ƒ\| _| _g }d|d  }}	d| }
x‚||	k �rÐ|dd…|f }x<t	|d |ƒD ]*}t
||dd…|f  ƒjƒ |
k�rzP �qzW || }||k�rÈ|j||fƒ |}�qPW t|ƒ}|dk�r
|dk�rúd|fg}n|d dfg}n,|| jk�r6|jdd„ d	d
� |d| j… }| j||ƒ\| _| _t|d | jd ƒ| _t|d | jd ƒ| _t|d | jd ƒ| _t|d | jd ƒ| _| j|ƒ |d |d  }}|||f \}}}d| d|  | | _| j|||ƒ| _| j| j k�r| j||| jd ƒ| _!n| j|||| j d  d ƒ| _!dS )z Analyzes the bitmap.
        r,   r   r   gš™™™™™¹?é2   r+   c             S   s   | d S )Nr   r&   )r"   r&   r&   r'   Ú<lambda>  s    z,ImageSlice._analyze_bitmap.<locals>.<lambda>T)ÚkeyÚreverseNc             S   s   | d S )Nr   r&   )r"   r&   r&   r'   rR   8  s    i   é   éÿÿÿÿrV   )"Ú	thresholdr1   r$   r%   ZConvertToImager   r   ÚGetDatar   ÚrangeÚabsÚsumÚappendr6   Ústretch_rowsÚsortÚ_calculate_dxyr5   r3   Ústretch_columnsr4   r2   Úminr;   r<   r=   r>   Ú_find_best_bordersÚbg_colorÚ_find_best_colorÚcontent_colorÚxtopÚxbottomÚlabel_color)r?   rW   rO   r$   r%   rN   ÚdataÚmatchesr#   ÚlastÚmax_diffZy_dataÚy2rB   r"   Zx_dataÚx2ÚrÚgÚbr&   r&   r'   rM   ø   sx    
"




zImageSlice._analyze_bitmapc          
   C   s|   ||	 ||
  }}xd||k rvt ||| ƒ}|}x<||k rjt ||| ƒ}|j|||||||dd� ||7 }q0W ||7 }qW dS )zo Performs a stretch fill of a region of an image into a region of a
            window device context.
        T)rI   N)ra   rL   )r?   rA   ÚixZiyrH   rF   r   r"   r#   r$   r%   rC   rD   ZddyrG   Zddxr&   r&   r'   r8   V  s    

zImageSlice._fillc             C   st   t |ƒdkr2|d \}}|| |||| | gfS |d \}}|d \}}|| | |||| | ||| | gfS )z\ Calculate the size of all image slices for a specified set of
            matches.
        r   r   )r6   )r?   Údrj   Úd1Úd2Úd3Zd4r&   r&   r'   r_   d  s    zImageSlice._calculate_dxyc             C   sŒ  | j | j }}|dk s|dk r"dS |d  }}|d  }}|d }|d }	d }
 } }}�xâ|sp|sp|
sp|�r>|| d }|| d }|o¬|dko¬| j||d |||d|ƒ}|oÒ||	k oÒ| j||d |||d|ƒ}|
oø|dkoø| j|||d |||dƒ}|�o"||k �o"| j|||d |||dƒ}| �pP| �pP| j||d |d ||ddƒ}| �p~| �p~| j||d |d ||ddƒ}| �p¬| �p¬| j||d |d ||ddƒ}| �pÚ| �pÚ| j||d |d ||ddƒ}|�oê|�oê|}|�oú|�oú|}|�o
|�o
|}
|�o|�o|}||8 }||7 }||
8 }||7 }q^W t| j|ƒ| _t| j|| d ƒ| _t| j|ƒ| _	t| j
|| d ƒ| _dS )zÖ Find the best set of image slice border sizes (e.g. for images with
            rounded corners, there should exist a better set of borders than
            the ones computed by the image slice algorithm.
        é   Nr+   r   Tr   )r$   r%   Ú	_is_equalra   r=   Úxleftr>   Úxrightr;   rf   r<   rg   )r?   ri   r$   r%   r=   r>   r;   r<   rD   rC   Útrq   Úlro   ÚheightÚwidthÚnlÚnrÚntÚnbÚtlÚtrÚblÚbrr&   r&   r'   rb   r  sT    zImageSlice._find_best_bordersc             C   sH   |||f \}}}t |d |d |d ƒ\}}}	tj}
|dk rDtj}
|
S )z\ Find the best contrasting text color for a specified pixel
            coordinate.
        g     ào@g      à?)r   r   ZBLACKZWHITE)r?   ri   r"   r#   ro   rp   rq   Úhr|   ÚsZ
text_colorr&   r&   r'   rd   ¾  s    zImageSlice._find_best_colorc             C   sL   t |||| …||| …f |||| …||| …f  ƒjƒ d| | k S )zš Determines if two identically sized regions of an image array are
            'the same' (i.e. within some slight color variance of each other).
        gš™™™™™¹?)rZ   r[   )r?   ri   rG   rE   Úx1Úy1r$   r%   r&   r&   r'   rx   Ê  s    zImageSlice._is_equalN)F)-Ú__name__Ú
__module__Ú__qualname__r   r   rN   r	   rW   r   r]   r`   r;   r<   r=   r>   rf   rg   ry   rz   r   r   re   rh   r   rc   r   r9   ZBitmapr1   r0   r$   r%   r
   r2   r3   r4   r5   r   rP   rM   r8   r_   rb   rd   rx   r&   r&   r&   r'   r)   S   s@   






K^Lr)   c             C   s(   t j| ƒ}|dkr$t| d� t | < }|S )z- Returns a (possibly cached) ImageSlice.
    N)rN   )Úimage_slice_cacheÚgetr)   )rN   Úresultr&   r&   r'   Úimage_slice_forÙ  s    
r‘   )Ú__doc__Ú
__future__r   r   Zcolorsysr   Únumpyr   r   r   Z
traits.apir   r   r	   r
   r   r   r   Zpyface.image_resourcer   Ú	constantsr   r   Ztraitsui.wx.constantsÚtraitsuir(   r)   rŽ   r‘   r&   r&   r&   r'   Ú<module>   s    $	   