U
    e»{jª1  ã                   @   st   d Z ddlZddlmZ dgZddd„ZG dd	„ d	ƒZG d
d„ deƒZG dd„ deƒZ	G dd„ de	ƒZ
ddd„ZdS )zí
Combine 3 images to produce a properly-scaled RGB image following Lupton et al. (2004).

The three images must be aligned and have the same pixel scale and size.

For details, see : https://ui.adsabs.harvard.edu/abs/2004PASP..116..133L
é    Né   )ÚZScaleIntervalÚmake_lupton_rgbc                 C   sL   |dks|dkr,|dkr |dks(t dƒ‚| S | | | d }tj|| jd�S )aT  
    Return a naive total intensity from the red, blue, and green intensities.

    Parameters
    ----------
    image_r : ndarray
        Intensity of image to be mapped to red; or total intensity if ``image_g``
        and ``image_b`` are None.
    image_g : ndarray, optional
        Intensity of image to be mapped to green.
    image_b : ndarray, optional
        Intensity of image to be mapped to blue.

    Returns
    -------
    intensity : ndarray
        Total intensity from the red, blue and green intensities, or ``image_r``
        if green and blue images are not provided.
    NzDplease specify either a single image or red, green, and blue images.g      @)Údtype)Ú
ValueErrorÚnpÚasarrayr   )Úimage_rÚimage_gÚimage_bÚ	intensity© r   ú_/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/visualization/lupton_rgb.pyÚcompute_intensity   s    ÿr   c                   @   s:   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚMappingaA  
    Baseclass to map red, blue, green intensities into uint8 values.

    Parameters
    ----------
    minimum : float or sequence(3)
        Intensity that should be mapped to black (a scalar or array for R, G, B).
    image : ndarray, optional
        An image used to calculate some parameters of some mappings.
    Nc                 C   sj   t t tj¡jƒ| _zt|ƒ W n tk
r>   d|g }Y nX t|ƒdkrTtdƒ‚|| _	t 
|¡| _d S )Né   z)please provide 1 or 3 values for minimum.)Úfloatr   ZiinfoÚuint8ÚmaxÚ	_uint8MaxÚlenÚ	TypeErrorr   Úminimumr   Ú_image)Úselfr   Úimager   r   r   Ú__init__>   s    zMapping.__init__c                 C   sn   t  |¡}t  |¡}t  |¡}|j|jks6|j|jkrRd}t| |j|j|j¡ƒ‚t  |  |||¡¡ t j¡S )aÄ  
        Convert 3 arrays, image_r, image_g, and image_b into an 8-bit RGB image.

        Parameters
        ----------
        image_r : ndarray
            Image to map to red.
        image_g : ndarray
            Image to map to green.
        image_b : ndarray
            Image to map to blue.

        Returns
        -------
        RGBimage : ndarray
            RGB (integer, 8-bits per channel) color image as an NxNx3 numpy array.
        z/The image shapes must match. r: {}, g: {} b: {})	r   r   Úshaper   ÚformatZdstackÚ_convert_images_to_uint8Úastyper   )r   r	   r
   r   Úmsgr   r   r   Úmake_rgb_imageK   s    


ÿþzMapping.make_rgb_imagec                 C   s   t |||ƒS )aÚ  
        Return the total intensity from the red, blue, and green intensities.
        This is a naive computation, and may be overridden by subclasses.

        Parameters
        ----------
        image_r : ndarray
            Intensity of image to be mapped to red; or total intensity if
            ``image_g`` and ``image_b`` are None.
        image_g : ndarray, optional
            Intensity of image to be mapped to green.
        image_b : ndarray, optional
            Intensity of image to be mapped to blue.

        Returns
        -------
        intensity : ndarray
            Total intensity from the red, blue and green intensities, or
            ``image_r`` if green and blue images are not provided.
        )r   )r   r	   r
   r   r   r   r   r   i   s    zMapping.intensityc              
   C   s6   t jddd�� t  |d| j¡W  5 Q R £ S Q R X dS )aÍ  
        Return an array which, when multiplied by an image, returns that image
        mapped to the range of a uint8, [0, 255] (but not converted to uint8).

        The intensity is assumed to have had minimum subtracted (as that can be
        done per-band).

        Parameters
        ----------
        I : ndarray
            Intensity to be mapped.

        Returns
        -------
        mapped_I : ndarray
            ``I`` mapped to uint8
        Úignore©ÚinvalidÚdivider   N)r   ÚerrstateZclipr   ©r   ÚIr   r   r   Úmap_intensity_to_uint8€   s    zMapping.map_intensity_to_uint8c                 C   s`  || j d  }|| j d  }|| j d  }|  |  |||¡¡}|||g}|D ]0}||9 }tjdd�� d||dk < W 5 Q R X qL| j}|\}}	}
tjddd��º t|ƒD ]ª\}}t ||	kt ||
kt ||k|| | |¡t |
|k|| |
 |¡¡t |	|
kt |	|k|| |	 |¡t |
|k|| |
 |¡¡¡ tj	¡}||||k< |||< q¦W 5 Q R X |S )zn
        Use the mapping to convert images image_r, image_g, and image_b to a triplet of uint8 images
        r   r   é   r#   )r%   r$   )
r   r*   r   r   r'   r   Ú	enumerateÚwherer    r   )r   r	   r
   r   ZfacZ	image_rgbÚcZpixmaxZr0Zg0Zb0Úir   r   r   r   •   s>    

ýýùôz Mapping._convert_images_to_uint8)NN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r"   r   r*   r   r   r   r   r   r   2   s   
r   c                   @   s"   e Zd ZdZddd„Zdd„ ZdS )ÚLinearMappinga—  
    A linear map map of red, blue, green intensities into uint8 values.

    A linear stretch from [minimum, maximum].
    If one or both are omitted use image min and/or max to set them.

    Parameters
    ----------
    minimum : float
        Intensity that should be mapped to black (a scalar or array for R, G, B).
    maximum : float
        Intensity that should be mapped to white (a scalar).
    Nc                 C   sˆ   |d ks|d kr@|d kr t dƒ‚|d kr0| ¡ }|d kr@| ¡ }tj| ||d� || _|d krfd | _n||krvt dƒ‚t|| ƒ| _d S )NzCyou must provide an image if you don't set both minimum and maximum)r   r   z,minimum and maximum values must not be equal)r   Úminr   r   r   ÚmaximumÚ_ranger   )r   r   r6   r   r   r   r   r   Ï   s     ÿzLinearMapping.__init__c                 C   sV   t jddd��> t  |dkdt  || jk| j| | j| j ¡¡W  5 Q R £ S Q R X d S ©Nr#   r$   r   )r   r'   r-   r7   r   r(   r   r   r   r*   å   s      
ÿýz$LinearMapping.map_intensity_to_uint8)NNN©r0   r1   r2   r3   r   r*   r   r   r   r   r4   À   s   
r4   c                   @   s"   e Zd ZdZddd„Zdd„ ZdS )	ÚAsinhMappingaè  
    A mapping for an asinh stretch (preserving colours independent of brightness)

    x = asinh(Q (I - minimum)/stretch)/Q

    This reduces to a linear stretch if Q == 0

    See https://ui.adsabs.harvard.edu/abs/2004PASP..116..133L

    Parameters
    ----------

    minimum : float
        Intensity that should be mapped to black (a scalar or array for R, G, B).
    stretch : float
        The linear stretch of the image.
    Q : float
        The asinh softening parameter.
    é   c                 C   sb   t  | |¡ d}t|ƒ|k r"d}nd}||kr2|}d}|| j t || ¡ | _|t|ƒ | _d S )Ng      €>gš™™™™™¹?g    _ B)	r   r   Úabsr   r   ÚarcsinhÚ_sloper   Ú_soften)r   r   ÚstretchÚQÚepsilonZQmaxÚfracr   r   r   r     s    zAsinhMapping.__init__c              
   C   sN   t jddd��6 t  |dkdt  || j ¡| j | ¡W  5 Q R £ S Q R X d S r8   )r   r'   r-   r=   r?   r>   r(   r   r   r   r*     s    z#AsinhMapping.map_intensity_to_uint8N)r;   r9   r   r   r   r   r:   ñ   s   
r:   c                   @   s   e Zd ZdZddd„ZdS )ÚAsinhZScaleMappingaO  
    A mapping for an asinh stretch, estimating the linear stretch by zscale.

    x = asinh(Q (I - z1)/(z2 - z1))/Q

    Parameters
    ----------
    image1 : ndarray or a list of arrays
        The image to analyse, or a list of 3 images to be converted to
        an intensity image.
    image2 : ndarray, optional
        the second image to analyse (must be specified with image3).
    image3 : ndarray, optional
        the third image to analyse (must be specified with image2).
    Q : float, optional
        The asinh softening parameter. Default is 8.
    pedestal : float or sequence(3), optional
        The value, or array of 3 values, to subtract from the images; or None.

    Notes
    -----
    pedestal, if not None, is removed from the images when calculating the
    zscale stretch, and added back into Mapping.minimum[]
    Nr;   c                 C   s>  |d ks|d kr0|d kr |d ks(t dƒ‚|g}n
|||g}|d k	rºzt|ƒ W n tk
rl   d|g }Y nX t|ƒdkr‚t dƒ‚t|ƒ}t|ƒD ]$\}}|| dkr’|||  ||< q’nt|ƒdg }t|Ž }tƒ  |¡}	t|	d|iŽ}
|
j	|
j
d  }|
j
}t|ƒD ]\}}||  |7  < �qt | |||¡ || _d S )Nz5please specify either a single image or three images.r   z please provide 1 or 3 pedestals.g        r   r   )r   r   r   Úlistr,   r   r   Z
get_limitsr4   r6   r   r:   r   r   )r   Zimage1Zimage2Zimage3rA   Zpedestalr   r/   ZimZzscale_limitsZzscaler@   r   Úlevelr   r   r   r   7  s8    ÿ
zAsinhZScaleMapping.__init__)NNr;   N)r0   r1   r2   r3   r   r   r   r   r   rD     s   rD   é   r;   c           
      C   s<   t |||ƒ}| | ||¡}|r8ddl}	|	jj||dd� |S )a™  
    Return a Red/Green/Blue color image from up to 3 images using an asinh stretch.
    The input images can be int or float, and in any range or bit-depth.

    For a more detailed look at the use of this method, see the document
    :ref:`astropy:astropy-visualization-rgb`.

    Parameters
    ----------
    image_r : ndarray
        Image to map to red.
    image_g : ndarray
        Image to map to green.
    image_b : ndarray
        Image to map to blue.
    minimum : float
        Intensity that should be mapped to black (a scalar or array for R, G, B).
    stretch : float
        The linear stretch of the image.
    Q : float
        The asinh softening parameter.
    filename : str
        Write the resulting RGB image to a file (file type determined
        from extension).

    Returns
    -------
    rgb : ndarray
        RGB (integer, 8-bits per channel) color image as an NxNx3 numpy array.
    r   NÚlower)Úorigin)r:   r"   Zmatplotlib.imager   Zimsave)
r	   r
   r   r   r@   rA   ÚfilenameZasinhMapÚrgbZ
matplotlibr   r   r   r   `  s    !)NN)r   rG   r;   N)r3   Únumpyr   Ú r   Ú__all__r   r   r4   r:   rD   r   r   r   r   r   Ú<module>   s   
! 1,D       ÿ