a
    §ˆcÿ?  ã                   @   s¤   d 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.
é    N)Ú
rgb_to_hls)ÚreshapeÚ
fromstringÚuint8)ÚHasPrivateTraitsÚInstanceÚIntÚListÚColorÚEnumÚBool)ÚImageResourceé   )ÚWindowColor)Úis_macc              
   C   sÊ   |  ¡ }t|ddƒ}|durL| ¡ \}}| ¡ \}}| | | | ||¡ nz| ¡ \}}tr–t|dƒr–|jr–|  	t
j¡ |  t
 t
 dddd¡¡¡ n|  t
 | ¡ ¡¡ |  t
j¡ |  dd||¡ |S )zZRecursively paint the parent's background if they have an associated
    image slice.
    Z_image_sliceNÚ_borderr   )Z	GetParentÚgetattrZGetPositionZGetSizeÚfillZGetClientSizer   Úhasattrr   ZSetBackgroundModeÚwxZTRANSPARENTÚSetBrushÚBrushÚColourZGetBackgroundColourÚSetPenÚTRANSPARENT_PENÚDrawRectangle)ÚdcZwindowÚparentÚsliceÚxÚyÚdxÚdy© r#   úZ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/wx/image_slice.pyÚpaint_parent'   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  ¡ }|r| | j¡ n| | 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 }}|D ]”\}}||k�r\ �qÚ|dk�rÎ|d }}|D ]P\}}||k�rŒ �qÆ|dk�rº|  ||||||||||¡
 ||7 }||7 }�qt||7 }||7 }�qD| j�rt| t  t j¡¡ | ||| j ||| j ¡ | ||| j d ||| j d ¡ | || j ||| j |¡ | || j d ||| j d |¡ dS )z_'Stretch fill' the specified region of a device context with the
        sliced image.
        r   )r   r   r(   é   r   é   N)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sdyr/   r0   ZtdxZtdyÚnZpdxsZpdysÚlast_xÚlast_yÚy0Ziy0ÚidyZwdyÚx0Zix0ÚidxZwdxr#   r#   r$   r   �   sš    ûû	û	
û
û	û






ÿÿÿzImageSlice.fillc              
   C   sÌ   |  ¡  ¡  | _}| ¡  | _}| ¡  | _}t ||¡| _	t 
¡ }| | j	¡ | t t¡¡ | tj¡ | dd||¡ t 
¡ }| |¡ |jdd|||dddd� | tj¡ | tj¡ |  ¡  dS )z(Handles the 'image' trait being changed.r   T©ZuseMaskN)Zcreate_imageZConvertToBitmapr-   ZGetWidthr!   Z	GetHeightr"   r   ÚBitmapr.   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   s4  | j }| j}| j| j }}| ¡ }t| ¡ ||dfƒ t¡}g }d|d  }}	d| }
||	k rº|| }t	|d |ƒD ] }t
|||  ƒ ¡ |
krt q–qt|| }||kr´| ||f¡ |}qVt|ƒ}|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… }|  ||¡\| _| _g }d|d  }}	d| }
||	k �rÈ|dd…|f }t	|d |ƒD ].}t
||dd…|f  ƒ ¡ |
k�rp �q �qp|| }||k�rÀ| ||f¡ |}�qHt|ƒ}|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… }|  ||¡\| _| _t|d | jd ƒ| _t|d | jd ƒ| _t|d | jd ƒ| _t|d | jd ƒ| _|  |¡ |d |d  }}|||f \}}}d| d|  | | _|  |||¡| _| j| j k�r|  ||| jd ¡| _!n|  |||| j d  d ¡| _!dS )zAnalyzes the bitmap.r)   r   r   çš™™™™™¹?é2   r(   c                 S   s   | d S ©Nr   r#   ©r   r#   r#   r$   Ú<lambda>*  ó    z,ImageSlice._analyze_bitmap.<locals>.<lambda>T)ÚkeyÚreverseNc                 S   s   | d S rN   r#   rO   r#   r#   r$   rP   G  rQ   éÿÿÿÿi   é   )"Ú	thresholdr.   r!   r"   ZConvertToImager   ZGetDataZastyper   ÚrangeÚabsÚsumÚappendr3   Ústretch_rowsÚsortÚ_calculate_dxyr2   r0   Ústretch_columnsr1   r/   Úminr8   r9   r:   r;   Ú_find_best_bordersÚbg_colorÚ_find_best_colorÚcontent_colorÚxtopÚxbottomÚlabel_color)r<   rV   rJ   r!   r"   rI   ÚdataÚmatchesr    ÚlastZmax_diffZy_dataÚy2r>   r   Zx_dataZx2ÚrÚgÚbr#   r#   r$   rH     sz    
"




ÿzImageSlice._analyze_bitmapc              
   C   st   ||	 ||
  }}||k rpt ||| ƒ}|}||k rft ||| ƒ}|j|||||||dd� ||7 }q,||7 }qdS )zjPerforms a stretch fill of a region of an image into a region of a
        window device context.
        TrE   N)r_   rG   )r<   r=   ÚixZiyrD   rB   r   r   r    r!   r"   r?   r@   ZddyrC   Zddxr#   r#   r$   r5   f  s    
zImageSlice._fillc                 C   st   t |ƒdkr2|d \}}|| |||| | gfS |d \}}|d \}}|| | |||| | ||| | gfS )zWCalculate the size of all image slices for a specified set of
        matches.
        r   r   )r3   )r<   Údrh   Úd1Zd2Zd3Zd4r#   r#   r$   r]   t  s    zImageSlice._calculate_dxyc              	   C   s†  | j | j }}|dk s|dk r"dS |d  }}|d  }}|d }|d }	d }
 } }}|sl|sl|
sl|�r:|| d }|| d }|o¨|dko¨|  ||d |||d|¡}|oÎ||	k oÎ|  ||d |||d|¡}|
oô|dkoô|  |||d |||d¡}|�o||k �o|  |||d |||d¡}| �pL| �pL|  ||d |d ||dd¡}| �pz| �pz|  ||d |d ||dd¡}| �p¨| �p¨|  ||d |d ||dd¡}| �pÖ| �pÖ|  ||d |d ||dd¡}|�oæ|�oæ|}|�oö|�oö|}|�o|�o|}
|�o|�o|}||8 }||7 }||
8 }||7 }qZt| 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;   Úxrightr8   rd   r9   re   )r<   rg   r!   r"   r:   r;   r8   r9   r@   r?   Útrm   Úlrk   ÚheightÚwidthÚnlÚnrÚntÚnbÚtlÚtrÚblÚbrr#   r#   r$   r`   ‚  sŠ    ÿýÿýÿýÿÿý
ÿýÿýÿÿý	ÿÿý	
zImageSlice._find_best_bordersc                 C   sT   |||f \}}}t |d |d |d ƒ\}}}	t tj¡}
|dk rPt tj¡}
|
S )zWFind the best contrasting text color for a specified pixel
        coordinate.
        g     ào@g      à?)r   r   r   ÚBLACKÚWHITE)r<   rg   r   r    rk   rl   rm   Úhrv   ÚsZ
text_colorr#   r#   r$   rb   é  s    zImageSlice._find_best_colorc                 C   sL   t |||| …||| …f |||| …||| …f  ƒ ¡ 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).
        rL   )rX   rY   )r<   rg   rC   rA   Úx1Úy1r!   r"   r#   r#   r$   rr   õ  s    ÿÿ
üÿzImageSlice._is_equalN)F)-Ú__name__Ú
__module__Ú__qualname__r   r   rI   r   rV   r   r[   r^   r8   r9   r:   r;   rd   re   rs   rt   r   r   rc   rf   r
   ra   r   r6   rF   r.   r-   r!   r"   r	   r/   r0   r1   r2   r   rK   rH   r5   r]   r`   rb   rr   r#   r#   r#   r$   r&   D   s@   






j^gr&   c                 C   s(   t  | ¡}|du 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__r   Zcolorsysr   Ú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   $	   E