o
    i                     @   sn   d dl Z d dlmZ d dlmZ d dlmZmZ ddd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io      ?      ?   c                 C   s   | j | j}}| tj} tj| dd}tj|dd}tj|j	|d| }|}t
t|j	d D ]}	|	d }
|j	|
 d }t||}||
|| |d }q0||dd< || }tj|dd}tj|ddj}||}|S )a  
    Apply frequency-dependent scaling to an image tensor using Fourier transforms.

    Parameters:
        x:           Input tensor of shape (B, C, H, W)
        scale_low:   Scaling factor for low-frequency components (default: 1.0)
        scale_high:  Scaling factor for high-frequency components (default: 1.5)
        freq_cutoff: Number of frequency indices around center to consider as low-frequency (default: 20)

    Returns:
        x_filtered: Filtered version of x in spatial domain with frequency-specific scaling applied.
    ))dim)device   N)dtyper   totorchfloat32fftfftnfftshiftonesshaperangelenminnarrow	ifftshiftifftnreal)x	scale_low
scale_highfreq_cutoffr   r   x_freqmaskmdr
   ccf_c
x_filtered r(   7/mnt/c/Users/fbmor/ComfyUI/comfy_extras/nodes_fresca.pyFourier_filter   s"   

r*   c                   @   s$   e Zd Zedd Zedd ZdS )FreScac                 C   sv   t jddgdddt jdt jjdddd	d
dddt jjdddd	d
dddt jjddddddddgt j gddS )Nr+   zfrequency guidance_for_testingz3Applies frequency-dependent scaling to the guidancemodelr   r   r   
   g{Gz?z+Scaling factor for low-frequency componentsT)defaultr   maxsteptooltipadvancedr   g      ?z,Scaling factor for high-frequency componentsr    r      i'  zFNumber of frequency indices around center to consider as low-frequency)node_idsearch_aliasesdisplay_namecategorydescriptioninputsoutputsis_experimental)r   SchemaModelInputFloatIntOutput)clsr(   r(   r)   define_schema9   s*   

zFreSca.define_schemac                    s,    fdd}|  }|| t|S )Nc                    sp   | d }t |dksd | d d d v r|S |d }|d }|| }t| d}|| }||g|dd   S )N	conds_outr4   condsr   r   )r   r   r    )r   r*   )argsrE   conduncondguidancefiltered_guidancefiltered_condr    r   r   r(   r)   custom_cfg_functionR   s    z+FreSca.execute.<locals>.custom_cfg_function)clone"set_model_sampler_pre_cfg_functionr   
NodeOutput)rC   r-   r   r   r    rN   r#   r(   rM   r)   executeP   s   

zFreSca.executeN)__name__
__module____qualname__classmethodrD   rR   r(   r(   r(   r)   r+   8   s
    
r+   c                   @   s(   e Zd Zedeeej  fddZdS )FreScaExtensionreturnc                    s   t gS N)r+   )selfr(   r(   r)   get_node_listk   s   zFreScaExtension.get_node_listN)	rS   rT   rU   r   listtyper   	ComfyNoder[   r(   r(   r(   r)   rW   j   s    rW   rX   c                      s   t  S rY   )rW   r(   r(   r(   r)   comfy_entrypointr   s   r_   )r   r   r   )r   	torch.fftr   typing_extensionsr   comfy_api.latestr   r   r*   r^   r+   rW   r_   r(   r(   r(   r)   <module>   s   
02