a
    d =                     @  s  d Z ddlmZ ddlZddlZddlmZ ddlm	Z	 ddddd	d
Z
ddddddZddddddZddddddZddddddZddddddZddddddZddddddZddddddZddddddZddddddZdddddd Zddddd!d"Zddddd#d$Zddddd%d&Zddddd'd(Zddddd)d*Zddddd+d,Zddddd-d.Zddddd/d0Zddddd1d2Zddddd3d4Zddd5d6d7Z dddd8d9d:Z!ddd5d;d<Z"dddd=d>d?Z#ddddd@dAZ$dddddBdCZ%dddddDdEZ&dddddFdGZ'dddddHdIdJZ(dddddHdKdLZ)dddddHdMdNZ*dddddHdOdPZ+dddQdRdSZ,dddQdTdUZ-dddVddWdXdYZ.dad[d[d\d]d[d^d_d`Z/dS )ba8  Provide blending functions and types.

Adapted from https://github.com/addisonElliott/pypdn/blob/master/pypdn/reader.py
and https://gitlab.com/inklabapp/pyora/-/blob/master/pyora/BlendNonSep.py
MIT License Copyright (c) 2020 FredHappyface

Credits to:

MIT License Copyright (c) 2019 Paul Jewell
For implementing blending from the Open Raster Image Spec

MIT License Copyright (c) 2018 Addison Elliott
For implementing blending from Paint.NET

MIT License Copyright (c) 2017 pashango
For implementing a number of blending functions used by other popular image
editors
    )annotationsN)Image   )	BlendTypez
np.ndarray)
background
foregroundreturnc                 C  s   ~ |S )zBlendType.NORMAL. r   r   r	   r	   Y/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/blendmodes/blend.pynormal   s    r   c                 C  s   t ||  ddS )zBlendType.MULTIPLY.              ?npclipr
   r	   r	   r   multiply$   s    r   c                 C  s   t | | dS )zBlendType.ADDITIVE.r   r   minimumr
   r	   r	   r   additive)   s    r   c              	   C  sV   t jdd6 t |dkt dd|  |  ddW  d   S 1 sH0    Y  dS )zBlendType.COLOURBURN.ignoredivider   r   N)r   errstatewheremaximumr
   r	   r	   r   
colourburn.   s    r   c              	   C  sR   t jdd2 t |dkt | d|  ddW  d   S 1 sD0    Y  dS )zBlendType.COLOURDODGE.r   r   r   Nr   r   r   r   r
   r	   r	   r   colourdodge6   s    r   c              	   C  sV   t jdd6 t |dkt | d d|  ddW  d   S 1 sH0    Y  dS )zBlendType.REFLECT.r   r   r      Nr   r
   r	   r	   r   reflect<   s    r    c              	   C  sV   t jdd6 t | dkt |d d|   ddW  d   S 1 sH0    Y  dS )zBlendType.GLOW.r   r   r   r   Nr   r
   r	   r	   r   glowD   s    r!   c                 C  s.   t | dk d|  | ddd|   d|   S )zBlendType.OVERLAY.      ?r   r   )r   r   r
   r	   r	   r   overlayL   s
    
r#   c                 C  s   t | | S )zBlendType.DIFFERENCE.)r   absr
   r	   r	   r   
differenceU   s    r%   c                 C  s   t | | dS )zBlendType.NEGATION.r   r   r   r
   r	   r	   r   negationZ   s    r'   c                 C  s   t | |S )zBlendType.LIGHTEN.r&   r
   r	   r	   r   lighten_   s    r(   c                 C  s   t | |S )zBlendType.DARKEN.r   r
   r	   r	   r   darkend   s    r)   c                 C  s   | | | |  S )zBlendType.SCREEN.r	   r
   r	   r	   r   screeni   s    r*   c                 C  sJ   t  . t d tt| t|A W  d   S 1 s<0    Y  dS )zBlendType.XOR.r   N)warningscatch_warningssimplefilterimageIntToFloatimageFloatToIntr
   r	   r	   r   xorn   s    

r0   c                 C  s(   d|  |  | | dd|  d|     S )zBlendType.SOFTLIGHT.r   r	   r
   r	   r	   r   	softlightv   s    r1   c                 C  sB   t |dk t | d | dt dd|  d|d d    dS )zBlendType.HARDLIGHT.r"   r   r          @)r   r   r   r
   r	   r	   r   	hardlight}   s
    "r3   c                 C  s   t | | d ddS )zBlendType.GRAINEXTRACT.r"   r   r   r   r
   r	   r	   r   grainextract   s    r4   c                 C  s   t | | d ddS )zBlendType.GRAINMERGE.r"   r   r   r   r
   r	   r	   r   
grainmerge   s    r5   c                 C  s   t d|  d|  dS )zBlendType.DIVIDE.g?gp?r   r   r
   r	   r	   r   r      s    r   c                 C  s4   t | d| |dk  t | d|d  |dk  S )zBlendType.PINLIGHT.r   r"   )r   r   r   r
   r	   r	   r   pinlight   s
    r6   c                 C  s0   t | |d |dk  t| d|d  |dk  S )zBlendType.VIVIDLIGHT.r   r"   )r   r   r
   r	   r	   r   
vividlight   s
    r7   c                 C  s   | | d|  |  S )zBlendType.EXCLUSION.r2   r	   r
   r	   r	   r   	exclusion   s    r8   )coloursr   c                 C  sN   | dddddf d | dddddf d  | dddddf d  S )zLuminosity.

	:param colours: x by x by 3 matrix of rgb color components of pixels
	:return: x by x by 3 matrix of luminosity of pixels
	Nr   gA`"?r   gbX9?r   gv/?r	   r9   r	   r	   r   _lum   s    r;   )originalColoursnewLuminosityr   c                 C  sH  |   }t|}|| }|dddddf  |7  < |dddddf  |7  < |dddddf  |7  < t|}tj|dd}tj|dd}t|jd D ]}t|jd D ]}|| | }	|||f }|||f }
|||f }|
dk r||	| | ||
   || |< |dkr||	| d|  ||   || |< qq|S )z3Set a new luminosity value for the matrix of color.Nr   r   r   Zaxis)copyr;   r   minmaxrangeshape)r<   r=   _coloursZ_luminosityZdeltaLumZ_minColoursZ_MaxColoursij_colourZ	minColourZ	maxColourr	   r	   r   _setLum   s0    
rH   c                 C  s   t j| ddt j| dd S )zzSaturation.

	:param colours: x by x by 3 matrix of rgb color components of pixels
	:return: int of saturation of pixels
	r   r>   )r   rA   r@   r:   r	   r	   r   _sat   s    rI   )r<   newSaturationr   c           	      C  s6  |   }t|jd D ]}t|jd D ]}|| | }d}d}d}|| || k rb|| }}|| || k r||| }}|| || k r|| }}|| ||  dk r|| ||  |||f  || ||   || | |< |||f || | |< n d|| | |< d|| | |< d|| | |< q*q|S )a  Set a new saturation value for the matrix of color.

	The current implementation cannot be vectorized in an efficient manner,
	so it is very slow,
	O(m*n) at least. This might be able to be improved with openCL if that is
	the direction that the lib takes.
	:param c: x by x by 3 matrix of rgb color components of pixels
	:param s: int of the new saturation value for the matrix
	:return: x by x by 3 matrix of luminosity of pixels
	r   r   r   r   )r?   rB   rC   )	r<   rJ   rD   rE   rF   rG   ZminIZmidIZmaxIr	   r	   r   _setSat   s,    


rK   c                 C  s   t t|t| t| S )zBlendType.HUE.rH   rK   rI   r;   r
   r	   r	   r   hue   s    rM   c                 C  s   t t| t|t| S )zBlendType.SATURATION.rL   r
   r	   r	   r   
saturation   s    rN   c                 C  s   t |t| S )zBlendType.COLOUR.rH   r;   r
   r	   r	   r   colour  s    rP   c                 C  s   t | t|S )zBlendType.LUMINOSITY.rO   r
   r	   r	   r   
luminosity	  s    rQ   )backgroundAlphaforegroundAlphabackgroundColourforegroundColourc              	   C  s|   ~| | }t jdddL t t | | dddddf ||dddddf }W d   n1 sj0    Y  ||fS )u   'clip' composite mode.

	All parts of 'layer above' which are alpha in 'layer below' will be made
	also alpha in 'layer above'
	(to whatever degree of alpha they were)

	Destination which overlaps the source, replaces the source.

	Fa = 0; Fb = αs
	co = αb x Cb x αs
	αo = αb x αs
	r   r   invalidNr   r   r   r   rR   rS   rT   rU   ZoutAlphaZoutRGBr	   r	   r   destin  s     "rZ   c              	   C  s   ~| d|  }t jdddP t t | d|  dddddf ||dddddf }W d   n1 sr0    Y  ||fS )zReverse 'Clip' composite mode.

	All parts of 'layer below' which are alpha in 'layer above' will be made
	also alpha in 'layer below'
	(to whatever degree of alpha they were)

	r   r   rV   NrX   rY   r	   r	   r   destout*  s    $"r[   c              
   C  s   |d|   | |  }t jdddr t t |d|   dddddf |t | | dddddf | |dddddf }W d   n1 s0    Y  ||fS )zPlace the layer below above the 'layer above' in places where the 'layer above' exists...

	where 'layer below' does not exist, but 'layer above' does, place 'layer-above'

	r   r   rV   NrX   rY   r	   r	   r   destatopA  s    $ "r\   c              
   C  s   ||  | d|   }t jdddr t t ||  dddddf |t | d|  dddddf | |dddddf }W d   n1 s0    Y  ||fS )zWPlace the layer below above the 'layer above' in places where the 'layer above' exists.r   r   rV   NrX   rY   r	   r	   r   srcatopV  s     $"r]   )imager   c                 C  s   | d S )zConvert a numpy array representing an image to an array of floats.

	Args:
		image (np.ndarray): numpy array of ints

	Returns:
		np.ndarray: numpy array of floats
	   r	   r^   r	   r	   r   r.   h  s    	r.   c                 C  s   | d  tjS )zConvert a numpy array representing an image to an array of ints.

	Args:
		image (np.ndarray): numpy array of floats

	Returns:
		np.ndarray: numpy array of ints
	r_   )Zastyper   Zuint8r`   r	   r	   r   r/   t  s    	r/   r   )r   r   	blendTyper   c              0   C  s   t jtt jtt jtt jtt j	t
t jtt jtt jtt jtt jtt jtt jtt jtt jtt jtt jt t j!t"t j#t$t j%t&t j't(t j)t*t j+t,t j-t.t j/t0i}||vrt| |S || | |S )a  Blend pixels.

	Args:
		background (np.ndarray): background
		foreground (np.ndarray): foreground
		blendType (BlendType): the blend type

	Returns:
		np.ndarray: new array representing the image

	background: np.ndarray,
	foreground: np.ndarray and the return are in the form

		[[[0. 0. 0.]
		[0. 0. 0.]
		[0. 0. 0.]
		...
		[0. 0. 0.]
		[0. 0. 0.]
		[0. 0. 0.]]

		...

		[[0. 0. 0.]
		[0. 0. 0.]
		[0. 0. 0.]
		...
		[0. 0. 0.]
		[0. 0. 0.]
		[0. 0. 0.]]]
	)1r   NORMALr   ZMULTIPLYr   Z
COLOURBURNr   ZCOLOURDODGEr   ZREFLECTr    ZOVERLAYr#   Z
DIFFERENCEr%   ZLIGHTENr(   ZDARKENr)   ZSCREENr*   Z	SOFTLIGHTr1   Z	HARDLIGHTr3   ZGRAINEXTRACTr4   Z
GRAINMERGEr5   ZDIVIDEr   ZHUErM   Z
SATURATIONrN   ZCOLOURrP   Z
LUMINOSITYrQ   ZXORr0   ZNEGATIONr'   ZPINLIGHTr6   Z
VIVIDLIGHTr7   Z	EXCLUSIONr8   )r   r   ra   ZblendLookupr	   r	   r   blend  s8    !
rc   r   zImage.ImagezBlendType | tuple[str, ...]float)r   r   ra   opacityr   c                 C  st  t t|d}t t| d}|dddddf }|dddddf | }|| }|ddddddf }	|ddddddf }
tjttjttj	t
tjti}||v rtttjt|| |||	|
dddS || dddddf |	 || dddddf |
  |dddddf t|	|
|  }|| | }tttjt||fdddS )a,  Blend layers using numpy array.

	Args:
		background (Image.Image): background layer
		foreground (Image.Image): foreground layer (must be same size as background)
		blendType (BlendType): The blendtype
		opacity (float): The opacity of the foreground image

	Returns:
		Image.Image: combined image
	ZRGBAN   r   r   )Za_minZa_max)r.   r   arrayconvertr   ZDESTINrZ   ZDESTOUTr[   ZSRCATOPr]   ZDESTATOPr\   r   Z	fromarrayr/   r   Zdstackrc   )r   r   ra   re   ZnpForegroundZnpBackgroundrR   rS   ZcombinedAlphaZbackgroundColorZforegroundColorZ	alphaFuncZcolorComponentsZalphaComponentr	   r	   r   blendLayers  sH     ri   )r   )0__doc__
__future__r   r+   numpyr   ZPILr   Z	blendtyper   r   r   r   r   r   r    r!   r#   r%   r'   r(   r)   r*   r0   r1   r3   r4   r5   r   r6   r7   r8   r;   rH   rI   rK   rM   rN   rP   rQ   rZ   r[   r\   r]   r.   r/   rc   ri   r	   r	   r	   r   <module>   sX   				$D 