o
    :ήcJG                     @   s8  d dl Zd dlmZ g dZejejejejej	ej
eejejejejfZdd eD ZedejdejdedejdejdejdejdiZee ee Zd&d	d
Zdd Z d'ddZ!d(ddZ"d)ddZ#d)ddZ$e#j%durwe#j%d e$_%d&ddZ&d&ddZ'd&ddZ(d&ddZ)d&d d!Z*d&d"d#Z+d&d$d%Z,dS )*    N)warn)img_as_float32img_as_float64img_as_float
img_as_intimg_as_uintimg_as_ubyteimg_as_booldtype_limitsc                 C   s&   i | ]}|t |jt |jfqS  )npiinfominmax).0tr   r   9/tmp/pip-target-vg8gfxp4/lib/python/skimage/util/dtype.py
<dictcomp>   s    r   )FT)   Fc                 C   s    t | jj \}}|rd}||fS )a  Return intensity limits, i.e. (min, max) tuple, of the image's dtype.

    Parameters
    ----------
    image : ndarray
        Input image.
    clip_negative : bool, optional
        If True, clip the negative range (i.e. return 0 for min intensity)
        even if the image dtype allows negative values.

    Returns
    -------
    imin, imax : tuple
        Lower and upper intensity limits.
    r   )dtype_rangedtypetype)imageclip_negativeiminimaxr   r   r   r
   &   s   r
   c                    s   t  fdd|D S )a  Return first of `dtypes` with itemsize greater than `itemsize`

    Parameters
    ----------
    itemsize: int
        The data type object element size.

    Other Parameters
    ----------------
    *dtypes:
        Any Object accepted by `np.dtype` to be converted to a data
        type object

    Returns
    -------
    dtype: data type object
        First of `dtypes` with itemsize greater than `itemsize`.

    c                 3   s$    | ]}t |j kr|V  qd S )N)r   r   itemsize)r   dtr   r   r   	<genexpr>P   s   " z"_dtype_itemsize.<locals>.<genexpr>)next)r   dtypesr   r   r   _dtype_itemsize<   s   r#   r   c                    s0   t  fdd|fd D }tt| S )ac  Return dtype of `kind` that can store a `bits` wide unsigned int

    Parameters:
    kind: str
        Data type kind.
    bits: int
        Desired number of bits.
    itemsize: int
        The data type object element size.

    Returns
    -------
    dtype: data type object
        Data type of `kind` that can store a `bits` wide unsigned int

    c                 3   s4    | ]} |d  k s |d  krdkr|V  qdS )   uNr   )r   ibitskindr   r   r    e   s    

z_dtype_bits.<locals>.<genexpr>)      r$   )r!   r   r   str)r)   r(   r   sr   r'   r   _dtype_bitsS   s   r.   Tc           	   	   C   sL  | j j}||krR|  d| k rRtt|d d }||kr%d| }nd| }tt|d d }td| j  d| d|   d| dd	 | t||S ||kr^|r\| 	 S | S ||kr|rt
| jt||}tj| d||  || j d
d |S | d||   } | S || dkr|rt
| jt||}tj| d| d d| d  ||j d |S | jt||| j jdd} | d| d d| d  9 } | S || d | }|rt
| jt||}tj| d| d d| d  ||j d |d||   }|S | jt||| j jdd} | d| d d| d  9 } | d||   } | S )aI  Scale an array of unsigned/positive integers from `n` to `m` bits.

    Numbers can be represented exactly only if `m` is a multiple of `n`.

    Parameters
    ----------
    a : ndarray
        Input image array.
    n : int
        Number of bits currently used to encode the values in `a`.
    m : int
        Desired number of bits to encode the values in `out`.
    copy : bool, optional
        If True, allocates and returns new array. Otherwise, modifies
        `a` in place.

    Returns
    -------
    out : array
        Output image array. Has the same kind as `a`.
    r*   intuintzDowncasting  to z# without scaling because max value z	 fits in    
stacklevelunsafeoutr   castingr   r   )r7   r   Fcopy)r   r)   r   r/   r   ceilr   astyper.   r:   emptyshapefloor_dividemultiplyr   )	anmr:   r)   mnewr   bor   r   r   _scalek   sX   
((rG   c                 C   s  t | } | j}|t ju rt d}nt |}|j}|j}|j}|j}	|j}
|j}t |t |r;|r9| 	 } | S |t
v rC|t
v sNtd| d| d|dv r^t |j}t |j}|	dv rnt |j}t |j}|	dkr~| |t| d d kS |dkr| |}|	d	kr||t| d 9 }|S |d	kr,|	d	kr| |S t | d
k st | dkrtdt||t jt j}|s|	dkrt j| ||d}nt j| || d |d}|d8 }t j||d t j||||d n7|	dkr	t j| |d |d}t j|d||d nt j| || d d |d}t j||d t j||||d ||S |	d	krxt|
|t jt j}|dkrJt j| d| |d} n(|dkrbt j| d| |d} t j| d
| d nt j| d|d} | d||  9 } t | |S |dkr|	dkrt| d|
 d| d } | |S t| d|
 d| S |	dkrt| d|
 d d| } t | j|}t j| d|| jdd |S |
|krt| d|
 d d| d S | td|d } | |8 } t| d|
 d| dd} | |7 } | |S )a  
    Convert an image to the requested data-type.

    Warnings are issued in case of precision loss, or when negative values
    are clipped during conversion to unsigned integer types (sign loss).

    Floating point values are expected to be normalized and will be clipped
    to the range [0.0, 1.0] or [-1.0, 1.0] when converting to unsigned or
    signed integers respectively.

    Numbers are not shifted to the negative side when converting from
    unsigned to signed integer types. Negative values will be clipped when
    converting to unsigned integers.

    Parameters
    ----------
    image : ndarray
        Input image.
    dtype : dtype
        Target data-type.
    force_copy : bool, optional
        Force a copy of the data, irrespective of its current dtype.
    uniform : bool, optional
        Uniformly quantize the floating point range to the integer range.
        By default (uniform=False) floating point values are scaled and
        rounded to the nearest integers, which minimizes back and forth
        conversion errors.

    .. versionchanged:: 0.15
        ``_convert`` no longer warns about possible precision or sign
        information loss. See discussions on these warnings at:
        https://github.com/scikit-image/scikit-image/issues/2602
        https://github.com/scikit-image/scikit-image/issues/543#issuecomment-208202228
        https://github.com/scikit-image/scikit-image/pull/3575

    References
    ----------
    .. [1] DirectX data conversion rules.
           https://msdn.microsoft.com/en-us/library/windows/desktop/dd607323%28v=vs.85%29.aspx
    .. [2] Data Conversions. In "OpenGL ES 2.0 Specification v2.0.25",
           pp 7-8. Khronos Group, 2010.
    .. [3] Proper treatment of pixels as integers. A.W. Paeth.
           In "Graphics Gems I", pp 249-256. Morgan Kaufmann, 1990.
    .. [4] Dirty Pixels. J. Blinn. In "Jim Blinn's corner: Dirty Pixels",
           pp 47-57. Morgan Kaufmann, 1998.

    float64zCannot convert from r1   .uirE   r   r*   fg      g      ?z.Images of type float must be between -1 and 1.r%   )r   g      ?)r7   r   g       @r&   r$   r5   r6   Fr9   )r   asarrayr   floatingr   r)   r   
issubdtype
obj2sctyper:   _supported_types
ValueErrorr   r   r   r   r<   r#   float32rH   r@   rintclipfloormaximumaddrG   viewr=   r>   r.   )r   r   
force_copyuniformdtypeobj_indtypeobj_outdtype_in	dtype_outkind_inkind_outitemsize_initemsize_outimin_inimax_inimin_outimax_outresultcomputation_type	image_outr   r   r   _convert   s   
0

















rj   c                 C   s   t dtdd t| |||dS )NzThe use of this function is discouraged as its behavior may change dramatically in scikit-image 1.0. This function will be removed in scikit-image 1.0.r*   r3   r   r   rY   rZ   )r   FutureWarningrj   rk   r   r   r   convertp  s   rm   z

    Warns
    -----
    FutureWarning:
        .. versionadded:: 0.17

        The use of this function is discouraged as its behavior may change
        dramatically in scikit-image 1.0. This function will be removed
        in scikit-image 1.0.
    c                 C      t | tj|S )aj  Convert an image to single-precision (32-bit) floating point format.

    Parameters
    ----------
    image : ndarray
        Input image.
    force_copy : bool, optional
        Force a copy of the data, irrespective of its current dtype.

    Returns
    -------
    out : ndarray of float32
        Output image.

    Notes
    -----
    The range of a floating point image is [0.0, 1.0] or [-1.0, 1.0] when
    converting from unsigned or signed datatypes, respectively.
    If the input image has a float type, intensity values are not modified
    and can be outside the ranges [0.0, 1.0] or [-1.0, 1.0].

    )rj   r   rR   r   rY   r   r   r   r        r   c                 C   rn   )aj  Convert an image to double-precision (64-bit) floating point format.

    Parameters
    ----------
    image : ndarray
        Input image.
    force_copy : bool, optional
        Force a copy of the data, irrespective of its current dtype.

    Returns
    -------
    out : ndarray of float64
        Output image.

    Notes
    -----
    The range of a floating point image is [0.0, 1.0] or [-1.0, 1.0] when
    converting from unsigned or signed datatypes, respectively.
    If the input image has a float type, intensity values are not modified
    and can be outside the ranges [0.0, 1.0] or [-1.0, 1.0].

    )rj   r   rH   ro   r   r   r   r     rp   r   c                 C   rn   )a  Convert an image to floating point format.

    This function is similar to `img_as_float64`, but will not convert
    lower-precision floating point arrays to `float64`.

    Parameters
    ----------
    image : ndarray
        Input image.
    force_copy : bool, optional
        Force a copy of the data, irrespective of its current dtype.

    Returns
    -------
    out : ndarray of float
        Output image.

    Notes
    -----
    The range of a floating point image is [0.0, 1.0] or [-1.0, 1.0] when
    converting from unsigned or signed datatypes, respectively.
    If the input image has a float type, intensity values are not modified
    and can be outside the ranges [0.0, 1.0] or [-1.0, 1.0].

    )rj   r   rM   ro   r   r   r   r     s   r   c                 C   rn   )a  Convert an image to 16-bit unsigned integer format.

    Parameters
    ----------
    image : ndarray
        Input image.
    force_copy : bool, optional
        Force a copy of the data, irrespective of its current dtype.

    Returns
    -------
    out : ndarray of uint16
        Output image.

    Notes
    -----
    Negative input values will be clipped.
    Positive values are scaled between 0 and 65535.

    )rj   r   uint16ro   r   r   r   r        r   c                 C   rn   )a  Convert an image to 16-bit signed integer format.

    Parameters
    ----------
    image : ndarray
        Input image.
    force_copy : bool, optional
        Force a copy of the data, irrespective of its current dtype.

    Returns
    -------
    out : ndarray of int16
        Output image.

    Notes
    -----
    The values are scaled between -32768 and 32767.
    If the input data-type is positive-only (e.g., uint8), then
    the output image will still only have positive values.

    )rj   r   int16ro   r   r   r   r     s   r   c                 C   rn   )a  Convert an image to 8-bit unsigned integer format.

    Parameters
    ----------
    image : ndarray
        Input image.
    force_copy : bool, optional
        Force a copy of the data, irrespective of its current dtype.

    Returns
    -------
    out : ndarray of ubyte (uint8)
        Output image.

    Notes
    -----
    Negative input values will be clipped.
    Positive values are scaled between 0 and 255.

    )rj   r   uint8ro   r   r   r   r     rr   r   c                 C   s   t | t|S )a  Convert an image to boolean format.

    Parameters
    ----------
    image : ndarray
        Input image.
    force_copy : bool, optional
        Force a copy of the data, irrespective of its current dtype.

    Returns
    -------
    out : ndarray of bool (`bool_`)
        Output image.

    Notes
    -----
    The upper half of the input dtype's positive range is True, and the lower
    half is False. All negative values (if present) are False.

    )rj   boolro   r   r   r   r	      s   r	   )F)r   )T)FF)-numpyr   warningsr   __all__byteubyteshortushortintcuintcr/   int_r0   longlong	ulonglong_integer_types_integer_rangesru   bool_bool8floatfloat_float16rR   rH   r   updatelistkeysrP   r
   r#   r.   rG   rj   rm   __doc__r   r   r   r   r   r   r	   r   r   r   r   <module>   sN    





H 
>






