3
8ßL]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   )Z	GetParentÚgetattrZGetPositionTupleZGetSizeTupleÚfillZGetClientSizeTupler   Úhasattrr   ZSetBackgroundModeÚwxZTRANSPARENTÚSetBrushÚBrushÚColourZGetBackgroundColourÚSetPenÚTRANSPARENT_PENÚDrawRectangle)ÚdcZwindowÚparentÚsliceÚxÚyÚdxÚdy© r$   ú=/tmp/pip-install-5sebvq2j/traitsui/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PenÚREDZDrawLineÚtopÚbottomÚleftÚright)Úselfr   r    r!   r"   r#   ZtransparentÚidcZsdxZsdyr0   r1   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ConvertToBitmapr.   ZGetWidthr"   Z	GetHeightr#   r   ZEmptyBitmapr/   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%   rM   8  s    i   é   éÿÿÿÿrQ   )"Ú	thresholdr/   r"   r#   ZConvertToImager   r   ZGetDatar   ÚrangeÚabsÚsumÚappendr4   Ústretch_rowsÚsortÚ_calculate_dxyr3   r1   Ústretch_columnsr2   r0   Úminr9   r:   r;   r<   Ú_find_best_bordersÚbg_colorÚ_find_best_colorÚcontent_colorÚxtopÚxbottomÚlabel_color)r=   rR   rJ   r"   r#   rI   ÚdataÚmatchesr!   ÚlastZmax_diffZy_dataÚy2r?   r    Zx_dataZx2ÚrÚgÚbr$   r$   r%   rH   ø   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)rF   N)r[   rG   )r=   r>   ÚixZiyrE   rC   r   r    r!   r"   r#   r@   rA   ZddyrD   Zddxr$   r$   r%   r6   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   )r4   )r=   Údrd   Zd1Zd2Zd3Zd4r$   r$   r%   rY   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_equalr[   r;   Úxleftr<   Úxrightr9   r`   r:   ra   )r=   rc   r"   r#   r;   r<   r9   r:   rA   r@   Útri   Úlrg   ÚheightÚwidthÚnlÚnrÚntÚnbÚtlÚtrÚblÚbrr$   r$   r%   r\   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   ÚBLACKÚWHITE)r=   rc   r    r!   rg   rh   ri   Úhrq   ÚsZ
text_colorr$   r$   r%   r^   ¾  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š™™™™™¹?)rT   rU   )r=   rc   rD   rB   Úx1Úy1r"   r#   r$   r$   r%   rm   Ê  s    zImageSlice._is_equalN)F)-Ú__name__Ú
__module__Ú__qualname__r   r   rI   r	   rR   r   rW   rZ   r9   r:   r;   r<   r`   ra   rn   ro   r   r   r_   rb   r   r]   r   r7   ZBitmapr/   r.   r"   r#   r
   r0   r1   r2   r3   r   rK   rH   r6   rY   r\   r^   rm   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)rI   )Úimage_slice_cacheÚgetr'   )rI   Úresultr$   r$   r%   Úimage_slice_forÙ  s    
rˆ   )Ú__doc__Ú
__future__r   r   Zcolorsysr   Znumpyr   r   r   Z
traits.apir   r   r	   r
   r   r   r   Zpyface.image_resourcer   Ú	constantsr   r   Ztraitsui.wx.constantsZtraitsuir&   r'   r…   rˆ   r$   r$   r$   r%   Ú<module>   s    $	   