
    +j                         d dl Z d dlmZ d dlmZ d dlmZmZ ddZ G d d	ej	                  Z
 G d
 de          ZdefdZdS )    N)override)ComfyExtensionio      ?      ?   c                 n   | j         | j        }}|                     t          j                  } t          j        | d          }t          j        |d          }t          j        |j	        |          |z  }|}t          t          |j	                  dz
            D ]D}	|	dz   }
|j	        |
         dz  }t          ||          }|                    |
||z
  |dz            }E||dd<   ||z  }t          j        |d          }t          j        |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_filtereds                 9/home/wildlama/comfy/ComfyUI/comfy_extras/nodes_fresca.pyFourier_filterr+      s@    GQX6E 	
U]A XaX&&&F\&h///F :fl6222Z?DA3v|$$q()) - -!e\#!#+r""HHS"s(C!G,, AaaaD d]F ]6x000F6x0005J u%%J    c                   :    e Zd Zed             Zed             ZdS )FreScac                    t          j        ddgdddt           j                            d          t           j                            dddd	d
dd          t           j                            dddd	d
dd          t           j                            ddddddd          gt           j                                        gd          S )Nr.   zfrequency guidanceexperimentalz3Applies 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)clss    r*   define_schemazFreSca.define_schema9   s    y01!#Mw''{CQBT'T_c  e e|Tqbt'U`d  f f]BA5q%mx|  ~ ~ !! !%
 
 
 	
r,   c                     fd}|                                 }|                    |           t          j        |          S )Nc                     | d         }t          |          dk    sd | d         d d         v r|S |d         }|d         }||z
  }t          |	          }||z   }||g|dd          z   S )N	conds_outr8   condsr   r   )r    r!   r"   )r   r+   )
argsrK   conduncondguidancefiltered_guidancefiltered_condr"   r!   r    s
          r*   custom_cfg_functionz+FreSca.execute.<locals>.custom_cfg_functionR   s    [)I9~~""dd7mBQB.?&?&?  Q<Dq\Ff}H .#%'	! ! ! .6M!6*Yqrr]::r,   )clone"set_model_sampler_pre_cfg_functionr   
NodeOutput)rG   r1   r    r!   r"   rS   r%   s     ```  r*   executezFreSca.executeP   s\    	; 	; 	; 	; 	; 	; 	;$ KKMM	,,-@AAA}Qr,   N)__name__
__module____qualname__classmethodrH   rW    r,   r*   r.   r.   8   sH        
 
 [
,     [     r,   r.   c                   L    e Zd Zedeeej                          fd            ZdS )FreScaExtensionreturnc                    K   t           gS N)r.   )selfs    r*   get_node_listzFreScaExtension.get_node_listk   s       
 	
r,   N)	rX   rY   rZ   r   listtyper   	ComfyNoderc   r\   r,   r*   r^   r^   j   sE        
T$r|*<%= 
 
 
 X
 
 
r,   r^   r_   c                  "   K   t                      S ra   )r^   r\   r,   r*   comfy_entrypointrh   r   s      r,   )r   r   r   )r   	torch.fftr   typing_extensionsr   comfy_api.latestr   r   r+   rf   r.   r^   rh   r\   r,   r*   <module>rl      s           & & & & & & / / / / / / / /- - - -`/  /  /  /  / R\ /  /  / d
 
 
 
 
n 
 
 
      r,   