o
    iE                     @   st  d dl Zd dlZd dlZd dlZd dlmZ d dlm	Z	m
Z
 d dlZd dlZd(ddZG dd de
jZG d	d
 d
e
jZG dd de
jZG dd de
jZG dd de
jZG dd de
jZG dd de
jZG dd de
jZG dd de
jZG dd de
jZG dd de
jZG dd de
jZG dd  d e
jZG d!d" d"e
jZG d#d$ d$e	Zd%efd&d'ZdS ))    N)override)ComfyExtensionioTc                 C   sP   |j dd  | dd  krtj|| d | d dd}|r&tj|| d S |S )N   bilinearcenterr   )shapecomfyutilscommon_upscalerepeat_to_batch_size)target_shapelatentrepeat_batch r   7/mnt/c/Users/fbmor/ComfyUI/comfy_extras/nodes_latent.pyreshape_latent_to
   s
   r   c                   @   ,   e Zd Zedd ZedejfddZdS )	LatentAddc                 C   4   t jdddgdt jdt jdgt j gdS )Nr   combine latentszsum latentslatent/advancedsamples1samples2node_idsearch_aliasescategoryinputsoutputsr   SchemaLatentInputOutputclsr   r   r   define_schema      

zLatentAdd.define_schemareturnc                 C   s:   |  }|d }|d }t|j|}|| |d< t|S Nsamplescopyr   r
   r   
NodeOutputr(   r   r   samples_outs1s2r   r   r   execute#      
zLatentAdd.executeN__name__
__module____qualname__classmethodr)   r   r0   r5   r   r   r   r   r      
    
r   c                   @   r   )LatentSubtractc                 C   r   )Nr=   zdifference latentzremove featuresr   r   r   r   r"   r'   r   r   r   r)   /   r*   zLatentSubtract.define_schemar+   c                 C   s:   |  }|d }|d }t|j|}|| |d< t|S r,   r.   r1   r   r   r   r5   >   r6   zLatentSubtract.executeNr7   r   r   r   r   r=   .   r<   r=   c                   @   r   )LatentMultiplyc                 C   s>   t jdg ddt jdt jjddddd	d
gt j gdS )Nr>   )zscale latentzamplify latentzlatent gainr   r-   
multiplier      ?g      $      $@{Gz?defaultminmaxstepr   r   r#   r$   r%   Floatr&   r'   r   r   r   r)   J   s   
zLatentMultiply.define_schemar+   c                 C   s&   |  }|d }|| |d< t|S r,   r/   r   r0   )r(   r-   r?   r2   r3   r   r   r   r5   Y   s   
zLatentMultiply.executeNr7   r   r   r   r   r>   I   r<   r>   c                   @   r   )LatentInterpolatec                 C   sH   t jdg ddt jdt jdt jjddddd	d
gt j gdS )NrK   )zblend latentz
mix latentzlerp latent
transitionr   r   r   ratior@           rB   rC   r   rH   r'   r   r   r   r)   b   s   

zLatentInterpolate.define_schemar+   c                 C   s   |  }|d }|d }t|j|}tjj|dd}tjj|dd}t|| }t|| }|| |d|   }	tjj|	dd}
t|	|
 }||| |d|    |d< t|S )Nr-   r   dimr@   )	r/   r   r
   torchlinalgvector_norm
nan_to_numr   r0   )r(   r   r   rM   r2   r3   r4   m1m2tmtstr   r   r   r5   r   s   
zLatentInterpolate.executeNr7   r   r   r   r   rK   a   
    
rK   c                   @   r   )LatentConcatc              
   C   sF   t jdddgdt jdt jdt jjdg dd	gt j gd
S )Nr[   join latentszstitch latentsr   r   r   rP   )xz-xyz-yrW   z-toptionsr   r   r#   r$   r%   Combor&   r'   r   r   r   r)      s   

zLatentConcat.define_schemar+   c                 C   s   |  }|d }|d }tj||jd }d|v r||f}n||f}d|v r*d}nd|v r1d}nd|v r7d	}tj||d
|d< t|S )Nr-   r   -r]   r   r^   r   rW   rO   )	r/   r   r   r   r
   rQ   catr   r0   )r(   r   r   rP   r2   r3   r4   cr   r   r   r5      s   

zLatentConcat.executeNr7   r   r   r   r   r[      rZ   r[   c                   @   r   )	LatentCutc                 C   sl   t jdg ddt jdt jjdg ddt jjdd	tj tjd
dt jjdd
d
tjd
dgt j gdS )Nrg   )zcrop latentzslice latentzextract regionr   r-   rP   )r]   r^   rW   r_   indexr   r   rC   amountr   	r   r#   r$   r%   rb   IntnodesMAX_RESOLUTIONr&   r'   r   r   r   r)      s   
zLatentCut.define_schemar+   c                 C   s   |  }|d }d|v r|jd }nd|v r|jd }n	d|v r%|jd }|dkr>t||j| d }t|j| | |}nt||j|  }t| |}t|||||d< t|S )	Nr-   r]   r   r^      rW      r   )	r/   ndimrE   r
   rF   rQ   narrowr   r0   )r(   r-   rP   rh   ri   r2   r3   r   r   r   r5      s   

zLatentCut.executeNr7   r   r   r   r   rg      s
    
rg   c                   @   r   )LatentCutToBatchc                 C   sR   t jdg ddt jdt jjdg ddt jjdd	d	tjd	d
gt j gdS )Nrr   )zslice to batchzsplit latentztile latentr   r-   rP   )rW   r]   r^   r_   
slice_sizer   rC   r   rj   r'   r   r   r   r)      s   
zLatentCutToBatch.define_schemar+   c                 C   s   |  }|d }d|v r|jd }nd|v r|jd }n	d|v r%|jd }|dk r.t|S ||d}|jd |k rA|jd }n|jd | dkr^|d d d t|jd | | f }d	|gt|jdd   }|	|d||d< t|S )
Nr-   r]   r   r^   rn   rW   ro   r   r   )
r/   rp   r   r0   movedimr
   mathfloorlistreshape)r(   r-   rP   rs   r2   r3   s	new_shaper   r   r   r5      s$   

(
zLatentCutToBatch.executeNr7   r   r   r   r   rr      rZ   rr   c                   @   r   )LatentBatchc              	   C   s6   t jdg dddt jdt jdgt j gdS )Nr{   )r   zmerge latentsr\   latent/batchTr   r   )r   r   r   is_deprecatedr    r!   r"   r'   r   r   r   r)     s   

zLatentBatch.define_schemar+   c              	   C   s   |  }|d }|d }t|j|dd}tj||fdd}||d< |ddd td|jd D |ddd td|jd D  |d< t|S )	Nr-   F)r   r   rO   batch_indexc                 S   s   g | ]}|qS r   r   ).0r]   r   r   r   
<listcomp>  s    z'LatentBatch.execute.<locals>.<listcomp>)	r/   r   r
   rQ   re   getranger   r0   )r(   r   r   r2   r3   r4   ry   r   r   r   r5     s   H
zLatentBatch.executeNr7   r   r   r   r   r{     rZ   r{   c                   @   r   )LatentBatchSeedBehaviorc              	   C   s8   t jddt jdt jjdddgddgt j gdS )	Nr   r   r-   seed_behaviorrandomfixed)r`   rD   r   r   r    r!   ra   r'   r   r   r   r)   !  s   
z%LatentBatchSeedBehavior.define_schemar+   c                 C   sd   |  }|d }|dkrd|v r|d n|dkr-|ddgd }|g|jd  |d< t|S )Nr-   r   r~   r   r   )r/   popr   r
   r   r0   )r(   r-   r   r2   r   batch_numberr   r   r   r5   /  s   

zLatentBatchSeedBehavior.executeNr7   r   r   r   r   r      s
    
r   c                   @   r   )LatentApplyOperationc              	   C   s4   t jddgddt jdt jdgt j gdS )Nr   ztransform latentlatent/advanced/operationsTr-   	operationr   r   r   is_experimentalr    r!   )r   r#   r$   r%   LatentOperationr&   r'   r   r   r   r)   =  s   

z"LatentApplyOperation.define_schemar+   c                 C   s(   |  }|d }||d|d< t|S )Nr-   r   rJ   )r(   r-   r   r2   r3   r   r   r   r5   M  s   
zLatentApplyOperation.executeNr7   r   r   r   r   r   <  rZ   r   c                   @   r   )LatentApplyOperationCFGc                 C   s0   t jdddt jdt jdgt j gdS )Nr   r   Tmodelr   r   r   r   r    r!   )r   r#   Modelr%   r   r&   r'   r   r   r   r)   V  s   

z%LatentApplyOperationCFG.define_schemar+   c                    s(   |  } fdd}|| t|S )Nc                    sP   | d }t |dkr |d |d  d|d  |d< |S  |d d|d< |S )N	conds_outrn   r   r   r   )len)argsr   r   r   r   pre_cfg_functioni  s   "z9LatentApplyOperationCFG.execute.<locals>.pre_cfg_function)clone"set_model_sampler_pre_cfg_functionr   r0   )r(   r   r   mr   r   r   r   r5   e  s   

zLatentApplyOperationCFG.executeNr7   r   r   r   r   r   U  r<   r   c                   @   r   )LatentOperationTonemapReinhardc                 C   s4   t jddgddt jjddddd	d
gt j gdS )Nr   z
hdr latentr   Tr?   r@   rN   g      Y@rB   rC   r   )r   r#   rI   r%   r   r&   r'   r   r   r   r)   u  s   z,LatentOperationTonemapReinhard.define_schemar+   c                    s    fdd}t |S )Nc           	         s   t jj| ddd d d d f }| | }ttd|j}t j||dd}t j||dd}|d |   }|d| 9 }||d  }||9 }|| S )Nr   rO   g|=T)rP   keepdim   r@   )rQ   rR   rS   rw   r   rp   meanstd)	r   kwargslatent_vector_magnitudenormalized_latentdimsr   r   topnew_magnituder?   r   r   tonemap_reinhard  s    z@LatentOperationTonemapReinhard.execute.<locals>.tonemap_reinhardr   r0   )r(   r?   r   r   r   r   r5     s   
z&LatentOperationTonemapReinhard.executeNr7   r   r   r   r   r   t  r<   r   c                   @   r   )LatentOperationSharpenc                 C   s^   t jdddt jjdddddddt jjd	d
dddddt jjdddddddgt j gdS )Nr   r   Tsharpen_radius	   r      )rD   rE   rF   rG   advancedsigmar@   g?rA   alpharN   g      @rB   r   )r   r#   rk   r%   rI   r   r&   r'   r   r   r   r)     s   z$LatentOperationSharpen.define_schemar+   c                    s    fdd}t |S )Nc           
         s   t jj| ddd d d d f }| | }| jd }d d }tjj||jd}|d }| d 9 }|||f |  d |||f< t j	j
|fd}t j	j
j|||ddd|d |d	d d d d   f }	||	 S )
Nr   rO   gư>rn   )deviceir@   reflect)paddinggroups)rQ   rR   rS   r
   comfy_extrasnodes_post_processinggaussian_kernelr   sumnn
functionalpadconv2drepeat	unsqueeze)
r   r   	luminancer   channelskernel_sizekernelr	   padded_image	sharpenedr   r   r   r   r   sharpen  s    
 Jz/LatentOperationSharpen.execute.<locals>.sharpenr   )r(   r   r   r   r   r   r   r   r5     s   
zLatentOperationSharpen.executeNr7   r   r   r   r   r     rZ   r   c                   @   s.   e Zd Zedd ZeddejfddZdS )ReplaceVideoLatentFramesc                 C   sT   t jddt jjdddt jjdddd	t jjd
dtj tjdddgt j gdS )Nr   r|   destinationz5The destination latent where frames will be replaced.)tooltipsourceTzThe source latent providing frames to insert into the destination latent. If not provided, the destination latent is returned unchanged.)optionalr   rh   r   r   zThe starting latent frame index in the destination latent where the source latent frames will be placed. Negative values count from the end.)rD   rE   rF   rG   r   r   )r   r#   r$   r%   rk   rl   rm   r&   r'   r   r   r   r)     s   z&ReplaceVideoLatentFrames.define_schemaNr+   c           	   	   C   s   |d u r	t |S |d jd }|d jd }|dk r|| }||kr4td| d| d t |S || |krNtd| d| d	| d t |S | }|d }|d  }||d d d d |||jd  f< ||d< t |S )
Nr-   rn   r   z ReplaceVideoLatentFrames: Index z0 is out of bounds for destination latent frames .z/ReplaceVideoLatentFrames: Source latent frames z- do not fit within destination latent frames z at the specified index )r   r0   r
   loggingwarningr/   r   )	r(   r   rh   r   dest_framessource_framesry   s_sources_destinationr   r   r   r5     s$   


$
z ReplaceVideoLatentFrames.executeNr7   r   r   r   r   r     s
    
r   c                   @   s(   e Zd Zedeeej  fddZdS )LatentExtensionr+   c                    s"   t ttttttttt	t
tttgS r   )r   r=   r>   rK   r[   rg   rr   r{   r   r   r   r   r   r   )selfr   r   r   get_node_list  s    zLatentExtension.get_node_listN)	r8   r9   r:   r   rw   typer   	ComfyNoder   r   r   r   r   r     s    r   r+   c                      s   t  S r   )r   r   r   r   r   comfy_entrypoint  s   r   )T)comfy.utilsr   "comfy_extras.nodes_post_processingr   rQ   rl   typing_extensionsr   comfy_api.latestr   r   r   ru   r   r   r   r=   r>   rK   r[   rg   rr   r{   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s2    
	'()*$%%