ó
 ‰\c           @   sð   d  d l  Z d d l m Z d d l m Z d  d l Z d d l m Z d  d l Z e j	 ƒ  Z
 d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ e ƒ  Z d „  Z d „  Z d e j f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿNi   (   t   _colormixer(   t   _histogramsi   (   t   img_as_ubytet   GuiLockErrorc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   | |  _  d  S(   N(   t   msg(   t   selfR   (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyt   __init__   s    c         C   s   |  j  S(   N(   R   (   R   (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyt   __str__   s    (   t   __name__t
   __module__R   R   (    (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyR      s   	t   WindowManagerc           B   s_   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z RS(
   sq    A class to keep track of spawned windows,
    and make any needed callback once all the windows,
    are closed.c         C   s:   g  |  _  d  |  _ d |  _ i  |  _ t |  _ d |  _ d  S(   Nt    (    (   t   _windowst   Nonet	   _callbackt   _callback_argst   _callback_kwargst   Falset	   _gui_lockt   _guikit(   R   (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyR      s    					c         C   s   |  j  s t d ƒ ‚ n  d  S(   Ns?   Must first acquire the gui lock before using this image manager(   R   R   (   R   (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyt   _check_locked!   s    	c         C   s&   |  j  r" |  j  |  j |  j Ž  n  d  S(   N(   R   R   R   (   R   (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyt   _exec_callback&   s    	c         C   s;   |  j  r t d |  j ƒ ‚ n t |  _  t | ƒ |  _ d  S(   Nsk   The gui lock can only be acquired by one toolkit per session.             The lock is already aquired by %s(   R   R   R   t   Truet   str(   R   t   kit(    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyt   acquire*   s
    		c         C   sh   |  j  ƒ  t | ƒ |  j k rX g  |  _ d  |  _ d |  _ i  |  _ t |  _	 d |  _ n t
 d ƒ ‚ d  S(   NR   s2   Only the toolkit that owns the lock may release it(    (   R   R   R   R   R   R   R   R   R   R   t   RuntimeError(   R   R   (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyt   _release3   s    
					c         C   s   |  j  ƒ  |  j j | ƒ d  S(   N(   R   R   t   append(   R   t   win(    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyt
   add_windowC   s    
c         C   sb   |  j  ƒ  y |  j j | ƒ Wn t k
 r; d GHd GHn# Xt |  j ƒ d k r^ |  j ƒ  n  d  S(   Ns4   Unable to find referenced window in tracked windows.s   Ignoring...i    (   R   R   t   removet
   ValueErrort   lenR   (   R   R   (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyt   remove_windowG   s    
	c         O   s)   |  j  ƒ  | |  _ | |  _ | |  _ d  S(   N(   R   R   R   R   (   R   t   cbt   cbargst   cbkwargs(    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyt   register_callbackR   s    
		c         C   s!   t  |  j ƒ d k r t St Sd  S(   Ni    (   R!   R   R   R   (   R   (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyt   has_windowsX   s    (   R   R	   t   __doc__R   R   R   R   R   R   R"   R&   R'   (    (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyR
      s   									c         C   s¦  |  j  d k  r t d ƒ ‚ n  |  j d } |  j d } t j | | d f d t j ƒ} t |  ƒ }  |  j  d k s– |  j  d k r|  j d d k r|  j | | f ƒ } | | d d … d d … d f <| | d d … d d … d f <| | d d … d d … d f <n— |  j  d k r–|  j d d k s@|  j d d k r‡|  d d … d d … d d … f | d d … d d … d d … f <q¢t d	 ƒ ‚ n t d
 ƒ ‚ | S(   s˜  Convert a 2D or 3D numpy array of any dtype into a
    3D numpy array with dtype uint8. This array will
    be suitable for use in passing to gui toolkits for
    image display purposes.

    Parameters
    ----------
    npy_img : ndarray, 2D or 3D
        The image to convert for display

    Returns
    -------
    out : ndarray, 3D dtype=np.uint8
        The converted image. This is guaranteed to be a contiguous array.

    Notes
    -----
    If the input image is floating point, it is assumed that the data
    is in the range of 0.0 - 1.0. No check is made to assert this
    condition. The image is then scaled to be in the range 0 - 255
    and then cast to np.uint8

    For all other dtypes, the array is simply cast to np.uint8

    If a 2D array is passed, the single channel is replicated
    to the 2nd and 3rd channels.

    If the array contains an alpha channel, this channel is
    ignored.

    i   s   Image must be 2D or 3D arrayi    i   i   t   dtypeNi   s#   Image must have 1, 3, or 4 channelss!   Image must have 2 or 3 dimensions(   t   ndimR    t   shapet   npt   emptyt   uint8R   t   reshape(   t   npy_imgt   heightt   widtht   outt	   npy_plane(    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyt   prepare_for_displaya   s$     !""&Gc         C   s   t  j |  | ƒ S(   s¢  Calculate the channel histograms of the current image.

    Parameters
    ----------
    image : ndarray, ndim=3, dtype=np.uint8
        Input image.
    nbins : int
        The number of bins.

    Returns
    -------
    out : (rcounts, gcounts, bcounts, vcounts)
        The binned histograms of the RGB channels and intensity values.

    This is a NAIVE histogram routine, meant primarily for fast display.

    (   R   t
   histograms(   t   imaget   nbins(    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyR6      s    t	   ImgThreadc           B   s   e  Z d  „  Z d „  Z RS(   c         G   s)   t  t |  ƒ j ƒ  | |  _ | |  _ d  S(   N(   t   superR9   R   t   funct   args(   R   R;   R<   (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyR   ´   s    	c         C   s   |  j  |  j Œ  d  S(   N(   R;   R<   (   R   (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyt   run¹   s    (   R   R	   R   R=   (    (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyR9   ³   s   	t   ThreadDispatchc           B   s   e  Z d  „  Z d „  Z RS(   c         G   s  | j  d } t |  _ g  |  _ g  |  _ |  j d k rP |  j j | | f ƒ nZ|  j d k rò|  j j | d  | d … d  d  … d  d  … f | d  | d … d  d  … d  d  … f f ƒ |  j j | | d | d … d  d  … d  d  … f | | d | d … d  d  … d  d  … f f ƒ |  j j | | d d | d … d  d  … d  d  … f | | d d | d … d  d  … d  d  … f f ƒ |  j j | d | d d  … d  d  … d  d  … f | d | d d  … d  d  … d  d  … f f ƒ n¸ |  j j | d  | d … d  d  … d  d  … f | d  | d … d  d  … d  d  … f f ƒ |  j j | | d d  … d  d  … d  d  … f | | d d  … d  d  … d  d  … f f ƒ xR t t |  j ƒ ƒ D]; } |  j j t | |  j | d |  j | d | Œ ƒ qÀWd  S(   Ni    i   i   i   i   (	   R+   t	   CPU_COUNTt   corest   threadst   chunksR   t   rangeR!   R9   (   R   t   imgt   stateimgR;   R<   R1   t   i(    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyR   ¾   s,    			/-317534/-/-c         C   s@   x |  j  D] } | j ƒ  q
 Wx |  j  D] } | j ƒ  q( Wd  S(   N(   RA   t   startt   join(   R   t   t(    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyR=   Þ   s    (   R   R	   R   R=   (    (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyR>   ½   s   	 t
   ColorMixerc           B   s­   e  Z d  Z d Z d Z d Z e e e g Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sÆ   a class to manage mixing colors in an image.
    The input array must be an RGB uint8 image.

    The mixer maintains an original copy of the image,
    and uses this copy to query the pixel data for operations.
    It also makes a copy for sharing state across operations.
    That is, if you add to a channel, and multiply to same channel,
    the two operations are carried separately and the results
    averaged together.

    it modifies your array in place. This ensures that if you
    bust over a threshold, you can always come back down.

    The passed values to a function are always considered
    absolute. Thus to threshold a channel completely you
    can do mixer.add(RED, 255). Or to double the intensity
    of the blue channel: mixer.multiply(BLUE, 2.)

    To reverse these operations, respectively:
    mixer.add(RED, 0), mixer.multiply(BLUE, 1.)

    The majority of the backend is implemented in Cython,
    so it should be quite quick.
    i    i   i   c         C   s¡   t  | ƒ t j k r$ t d ƒ ‚ n  | j t j k rE t d ƒ ‚ n  | j d k sg | j d d k rv t d ƒ ‚ n  | |  _ | j	 ƒ  |  _
 | j	 ƒ  |  _ d  S(   Ns   Image must be a numpy arrays   Image must have dtype uint8i   i   s   Image must be 3 channel MxNx3(   t   typeR,   t   ndarrayR    R)   R.   R*   R+   RD   t   copyt   origimgRE   (   R   RD   (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyR     s    "	c         C   s   |  j  S(   N(   RE   (   R   (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyt   get_stateimage  s    c         C   s   |  j  |  j (d  S(   N(   RD   RE   (   R   (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyt   commit_changes  s    c         C   s    |  j  |  j (|  j |  j (d  S(   N(   RN   RE   RD   (   R   (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyt   revert  s    c         C   s   |  j  |  j (d  S(   N(   RE   RD   (   R   (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyt   set_to_stateimg  s    c         C   sD   | |  j  k s t ‚ t |  j |  j t j | | ƒ } | j ƒ  d S(   sB  Add the specified ammount to the specified channel.

        Parameters
        ----------
        channel : flag
            the color channel to operate on
            RED, GREED, or BLUE
        ammount : integer
            the ammount of color to add to the channel,
            can be positive or negative.

        N(   t   valid_channelst   AssertionErrorR>   RD   RE   R    t   addR=   (   R   t   channelt   ammountt   pool(    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyRU     s    c         C   sD   | |  j  k s t ‚ t |  j |  j t j | | ƒ } | j ƒ  d S(   sF  Mutliply the indicated channel by the specified value.

         Parameters
        ----------
        channel : flag
            the color channel to operate on
            RED, GREED, or BLUE
        ammount : integer
            the ammount of color to add to the channel,
            can be positive or negative.

        N(   RS   RT   R>   RD   RE   R    t   multiplyR=   (   R   RV   RW   RX   (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyRY   0  s    c         C   s/   t  |  j |  j t j | | ƒ } | j ƒ  d S(   s2  Adjust the brightness off an image with an offset and factor.

        Parameters
        ----------
        offset : integer
            The ammount to add to each channel.
        factor : float
            The factor to multiply each channel by.

        result = clip((pixel + offset)*factor)

        N(   R>   RD   RE   R    t
   brightnessR=   (   R   t   factort   offsetRX   (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyRZ   B  s    c         C   s/   t  |  j |  j t j | | ƒ } | j ƒ  d  S(   N(   R>   RD   RE   R    t   sigmoid_gammaR=   (   R   t   alphat   betaRX   (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyR]   S  s    c         C   s,   t  |  j |  j t j | ƒ } | j ƒ  d  S(   N(   R>   RD   RE   R    t   gammaR=   (   R   R`   RX   (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyR`   X  s    c         C   s2   t  |  j |  j t j | | | ƒ } | j ƒ  d S(   s  Adjust the H, S, V channels of an image by a constant ammount.
        This is similar to the add() mixer function, but operates over the
        entire image at once. Thus all three additive values, H, S, V, must
        be supplied simultaneously.

        Parameters
        ----------
        h_amt : float
            The ammount to add to the hue (-180..180)
        s_amt : float
            The ammount to add to the saturation (-1..1)
        v_amt : float
            The ammount to add to the value (-1..1)

        N(   R>   RD   RE   R    t   hsv_addR=   (   R   t   h_amtt   s_amtt   v_amtRX   (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyRa   ]  s    c         C   s2   t  |  j |  j t j | | | ƒ } | j ƒ  d S(   só  Adjust the H, S, V channels of an image by a constant ammount.
        This is similar to the add() mixer function, but operates over the
        entire image at once. Thus all three additive values, H, S, V, must
        be supplied simultaneously.

        Note that since hue is in degrees, it makes no sense to multiply
        that channel, thus an add operation is performed on the hue. And the
        values given for h_amt, should be the same as for hsv_add

        Parameters
        ----------
        h_amt : float
            The ammount to to add to the hue (-180..180)
        s_amt : float
            The ammount to multiply to the saturation (0..1)
        v_amt : float
            The ammount to multiply to the value (0..1)

        N(   R>   RD   RE   R    t   hsv_multiplyR=   (   R   Rb   Rc   Rd   RX   (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyRe   q  s    c         C   s+   t  j | | | ƒ \ } } } | | | f S(   s  Convert an RGB value to HSV

        Parameters
        ----------
        R : int
            Red value
        G : int
            Green value
        B : int
            Blue value

        Returns
        -------
        out : (H, S, V) Floats
            The HSV values

        (   R    t   py_rgb_2_hsv(   R   t   Rt   Gt   Bt   Ht   St   V(    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyt   rgb_2_hsv_pixel‰  s    c         C   s+   t  j | | | ƒ \ } } } | | | f S(   s*  Convert an HSV value to RGB

        Parameters
        ----------
        H : float
            Hue value
        S : float
            Saturation value
        V : float
            Intensity value

        Returns
        -------
        out : (R, G, B) ints
            The RGB values

        (   R    t   py_hsv_2_rgb(   R   Rj   Rk   Rl   Rg   Rh   Ri   (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyt   hsv_2_rgb_pixelž  s    (   R   R	   R(   t   REDt   GREENt   BLUERS   R   RO   RP   RQ   RR   RU   RY   RZ   R]   R`   Ra   Re   Rm   Ro   (    (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyRJ   å   s&   													(   t   numpyR,   R   R    R   t	   threadingt   utilR   t   multiprocessingt	   cpu_countR?   t	   ExceptionR   t   objectR
   t   window_managerR5   R6   t   ThreadR9   R>   RJ   (    (    (    s7   lib/python2.7/site-packages/skimage/io/_plugins/util.pyt   <module>   s   I		<	
(