
    IR-e1                         d Z ddlZddlmZ dgZddZ G d d          Z G d	 d
e          Z G d de          Z	 G d de	          Z
	 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                     ||||t          d          | S | |z   |z   dz  }t          j        || j                  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	intensitys       @lib/python3.11/site-packages/astropy/visualization/lupton_rgb.pycompute_intensityr      s_    ( '/GOV   7"W,3I :iw}5555    c                   2    e Zd ZdZddZd Zd Zd Z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                 B   t          t          j        t          j                  j                  | _        	 t          |           n# t          $ r	 d|gz  }Y nw xY wt          |          dk    rt          d          || _	        t          j
        |          | _        d S )N   z)please provide 1 or 3 values for minimum.)floatr	   iinfouint8max	_uint8Maxlen	TypeErrorr   minimumr
   _image)selfr   images      r   __init__zMapping.__init__>   s    rx11566	$LLLL 	$ 	$ 	$7)mGGG	$w<<1HIIIj''s   A AAc                    t          j        |          }t          j        |          }t          j        |          }|j        |j        k    s|j        |j        k    r5d}t          |                    |j        |j        |j                            t          j        |                     |||                                        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   formatdstack_convert_images_to_uint8astyper   )r   r   r   r   msgs        r   make_rgb_imagezMapping.make_rgb_imageK   s    $ *W%%*W%%*W%%MW]**0N0NCCSZZw}gmTTUUUy))'7GDD
 

&

	r   c                 $    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   s       r   r   zMapping.intensityi   s    * !'7;;;r   c                     t          j        dd          5  t          j        |d| j                  cddd           S # 1 swxY w Y   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	   errstateclipr   r   Is     r   map_intensity_to_uint8zMapping.map_intensity_to_uint8   s    $ [(;;; 	1 	171a00	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1s   ?AAc                    || j         d         z
  }|| j         d         z
  }|| j         d         z
  }|                     |                     |||                    }|||g}|D ]=}||z  }t          j        d          5  d||dk     <   ddd           n# 1 swxY w Y   >| j        }|\  }}	}
t          j        dd          5  t          |          D ]\  }}t          j        ||	k    t          j        ||
k    t          j        ||k    ||z  |z  |          t          j        |
|k    ||z  |
z  |                    t          j        |	|
k    t          j        |	|k    ||z  |	z  |          t          j        |
|k    ||z  |
z  |                                                  t          j	                  }||||k    <   |||<   	 ddd           n# 1 swxY w Y   |S )zo
        Use the mapping to convert images image_r, image_g, and image_b to a triplet of uint8 images.
        r   r      r,   )r.   Nr-   )
r   r4   r   r	   r0   r   	enumeratewherer'   r   )r   r   r   r   fac	image_rgbcpixmaxr0g0b0is               r   r&   z Mapping._convert_images_to_uint8   s    DLO+DLO+DLO+))$..'7*S*STTgw/	 	 	AHAX...  !a%               
B [(;;; 	! 	!!),, ! !1HGHRvq6zBBBvq6zBBB 
 HRvq6zBBBvq6zBBB   &""  !'!f* 	!!!	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!& s%   >
BB	B	DGGGNN)	__name__
__module____qualname____doc__r!   r)   r   r4   r&    r   r   r   r   2   sn        	 	( ( ( (  << < <.1 1 1*( ( ( ( (r   r   c                        e Zd ZdZddZ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                 >   ||=|t          d          ||                                }||                                }t                              | ||           || _        |	d | _        d S ||k    rt          d          t          ||z
            | _        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   rK   r    s       r   r!   zLinearMapping.__init__   s    ?go} 3   ))++))++we<<<?DKKK'!! !OPPP' 122DKKKr   c                     t          j        dd          5  t          j        |dk    dt          j        || j        k    | j        |z  | j        | j        z                      cd d d            S # 1 swxY w Y   d S Nr,   r-   r   )r	   r0   r8   rL   r   r2   s     r   r4   z$LinearMapping.map_intensity_to_uint8   s    [(;;; 	 	8Q$dnq&8$.4;:V  	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   AA00A47A4)NNNrB   rC   rD   rE   r!   r4   rF   r   r   rH   rH      sA         3 3 3 3,	 	 	 	 	r   rH   c                        e Zd ZdZddZ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                     t                               | |           d}t          |          |k     rd}n
d}||k    r|}d}|| j        z  t	          j        ||z            z  | _        |t          |          z  | _        d S )Ng      >g?g    _B)	r   r!   absr   r	   arcsinh_sloper   _soften)r   r   stretchQepsilonQmaxfracs          r   r!   zAsinhMapping.__init__  s    w''' q66GAAD4xxT^+bj.B.BB5>>)r   c           	          t          j        dd          5  t          j        |dk    dt          j        || j        z            | j        z  |z            cd d d            S # 1 swxY w Y   d S rN   )r	   r0   r8   rU   rW   rV   r2   s     r   r4   z#AsinhMapping.map_intensity_to_uint8  s    [(;;; 	W 	W8AFArz!dl2B'C'Cdk'QTU'UVV	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	Ws   ?A##A'*A'N)rR   rO   rF   r   r   rQ   rQ      sF         &* * * *"W W W W Wr   rQ   c                       e Zd ZdZ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[]
    NrR   c                    ||||t          d          |g}n|||g}|	 t          |           n# t          $ r	 d|gz  }Y nw xY wt          |          dk    rt          d          t          |          }t	          |          D ]\  }}||         dk    r|||         z
  ||<    nt          |          dgz  }t          | }t                                          |          }	t          |	d|i}
|
j	        |
j
        d         z
  }|
j
        }t	          |          D ]\  }}||xx         |z  cc<   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   listr7   r   r   
get_limitsrH   rK   r   rQ   r!   r   )r   image1image2image3rY   pedestalr    r@   imzscale_limitszscalerX   r   levels                 r   r!   zAsinhZScaleMapping.__init__6  s   >V^Nv~ K   HEEVV,E*H * * *z>* 8}}!! !CDDDKKE"5)) 0 02A;#%%!HQK/E!H0 5zzSE)H!5)&((33E::;U;;.6>!#44.!(++ 	  	 HAuAJJJ%JJJJdGWa888s   4 AA)NNrR   N)rB   rC   rD   rE   r!   rF   r   r   r_   r_     s2         2& & & & & &r   r_      rR   c                     t          |||          }|                    | ||          }|r!ddl}	|	j                            ||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)rQ   r)   matplotlib.imager    imsave)
r   r   r   r   rX   rY   filenameasinhMaprgb
matplotlibs
             r   r   r   _  sd    B GWa00H

!
!'7G
<
<C ?#g>>>Jr   rA   )r   rk   rR   N)rE   numpyr	    r   __all__r   r   rH   rQ   r_   r   rF   r   r   <module>rx      s3             
6 6 6 6BK K K K K K K K\. . . . .G . . .b(W (W (W (W (W7 (W (W (WV@ @ @ @ @ @ @ @H DH) ) ) ) ) )r   