
    
3j{                     L   S SK r S SKrS SKrS SKJr  S SKJr  S SKJr	  S SK
r
SrSrSrSrSrS	rS
r\\-  \-   rS\R&                  S\R&                  4S jrS\R&                  S\R&                  4S jrS\R&                  S\R&                  4S jr " S S\R.                  5      rg)    N)Image)iogb8@g6Deê?g?gxa?gdzx@gFCƣT·?gv4?logcreturnc                     U R                  SS5      n SU [        -
  [        -  -  [        -
  [        -  nU [
        -
  [        -  n[        R                  " U [        :  X5      $ )N              ?      $@)
clampLC_DLC_CLC_BLC_ALC_FLC_Etorchwhere
LC_CUT_LOG)r   lin_from_loglin_from_lins      S/home/wildlama/comfy/ComfyUI/custom_nodes/ComfyUI-KJNodes/nodes/hdr_preview_node.py_logc3_decompressr      sS    ::c3DdTkT12T9TAL4K4'L;;tz)<FF    xc                     Sn[         R                  " X:*  SU -  S[         R                  " U R                  US9S5      -  S-
  5      R	                  SS5      $ )	Ngsåi?ףp=
)@zG?ming?)\(?r   r	   )r   r   powr   clamp_r   cutoffs     r   _linear_to_srgbr%      sU    F;;				!''f'-y99EA fS#	r   c                 l    Sn[         R                  " X:*  U S-  U R                  SS9S-   S-  S-  5      $ )Ng?ܵ?r   r   r   r    r   g333333@)r   r   r   r#   s     r   _srgb_to_linearr'   %   sA    F;;		E	
''c'
U
"e	+3 r   c                   |    \ rS rSr\S 5       r\SS\R                  S\S\S\S\	S\
R                  4S	 jj5       rS
rg)HDRPreviewKJ.   c                    [         R                  " SSSSSS[         R                  R                  SSS9[         R                  R                  S	S
SSSSS9[         R                  R                  SSS
SSSS9[         R                  R                  SSSSSSSS9[         R
                  R                  S/ SQSSSS9/[         R                  R                  SSS 9/S!9$ )"Nr)   zHDR Preview KJzKJNodes/imageTa  Realtime-exposure preview for HDR-compressed images.

Input: LogC3-compressed [0,1] image/video batch (e.g. the VAE-decoded output of an HDR IC-LoRA workflow, prior to HDR decompression).

Decompression + exposure + saturation + Reinhard tonemap + sRGB runs in a WebGL fragment shader in the browser for realtime slider feedback, and the same math runs server-side to produce the baked sRGB IMAGE output. Slider changes update the preview immediately; the IMAGE output only updates when the workflow is re-queued.imagezVLogC3-compressed HDR image/video in [0,1], or linear HDR if 'input_space' is 'linear'.)tooltipexposurer   g      $r
   g{Gz?z6Exposure in EV stops. 0 = no change, +1 = 2x brighter.)defaultr   maxstepr-   
saturationr	          @z<Saturation multiplier. 0 = grayscale, 1 = unchanged, 2 = 2x.fps      8@g      ^@g?z-Playback frame rate for video (batch) inputs.)r/   r   r0   r1   optionalr-   input_space)logc3linearsrgbr8   zColor space of input. 'logc3' = ARRI LogC3 compressed HDR; 'linear' = linear HDR directly; 'srgb' = already-graded sRGB image (skips Reinhard tonemap).)optionsr/   r6   r-   z.Tonemapped sRGB image, ready for preview/save.)display_namer-   )node_idr<   categoryis_output_nodeis_experimentaldescriptioninputsoutputs)r   Schemar   InputFloatComboOutput)clss    r   define_schemaHDRPreviewKJ.define_schema/   s   yy")$ i wt  vz3EtRVT  V|ScsQUZ  \udS[_K  M}6Q[bmq v  w WL   N7
 	
r   r,   r.   r2   r4   r7   r   c                    [         R                  " 5       n[        R                  " USS9  S[        R
                  " SS5      S 3nUR                  u  pp[        R                  " 5       nSU-  n[        R                  " / SQUS	9nX-  S
-  S-  n[        S[        U[        S[        US-  S5      -  5      5      5      nSnUS:X  a8  [        USS S
24   R                  5       R                  5       5      nUS:  a  UOSn/ n/ n[!        SUU5       GH;  n[        UU-   U5      nUUU2SS S
24   R                  5       R#                  USS9nUS:X  a  UU-  R%                  SS5      nOUR'                  SS5      nUR)                  S5      R+                  S5      R%                  SS5      R#                  [        R,                  5      R/                  5       R1                  5       nA[!        UU-
  5       He  nU SUU-   S S3n[2        R4                  " UU   SS9R7                  [        R8                  R;                  UU5      SSS9  UR=                  U5        Mg     AUS:X  a  [?        U5      R%                  SS9nO.US:X  a  [A        U5      R%                  SS9nOUR'                  SS9nAUR)                  U5      nUUR#                  URB                  5      -  RE                  S SS!9nUUU-
  U-  -   R%                  SS9nAAUS:X  a  UR%                  SS5      n O	USU-   -  n AUR=                  [G        U 5      R/                  5       5        A GM>     [        RH                  " USS"9n!AU V"s/ s H  n"U"S#S$.PM
     sn"[        U
5      [        U	5      [        U5      U[        U5      [        U5      [        U5      [        U5      S%.	n#[J        RL                  " U!S&U#/0S'9$ s  sn"f )(NT)exist_okhdrprv_r   i 06xr3   )gz6?g,C?g]m{?)device         i ʚ;
   r	   r9   .)non_blockingr   g     o@g      ?_05dz.pngRGB)modePNG)formatcompress_levelr8   r   r:   )dimkeepdim)r^   temp)filenametype)	frameswidthheightr4   r7   linear_scaleframe_countr.   r2   hdr_preview_data)ui)'folder_pathsget_temp_directoryosmakedirsrandomrandintshapemmget_torch_devicer   tensorr0   r   intfloatitemrangetor"   r   mul_add_uint8cpunumpyr   	fromarraysavepathjoinappendr   r'   dtypesumr%   catr   
NodeOutput)$rI   r,   r.   r2   r4   r7   temp_dirprefixBHWrV   rP   exposure_mulluma_weightsbytes_per_frame
chunk_size
norm_scalemax_val	filenamessrgb_chunksstartend	image_rgbpreview
preview_npifnamehdrexposedluma	saturated
tonemappedr:   fdatas$                                       r   executeHDRPreviewKJ.executeR   s   224
Ht,6>>!X6s;<[[
a$$&h||$<VL%!)a-C3}Ob<PRS8T'T#UVW
 
("E#rr'N..05578G$+cMsJ	1a,Eej(!,CeCibqb01779<<VRV<WI h&$z199#sC#//#s3 e,11#6==c5ILLU[[Y]]_eegJ3;'!(!EAIc?$7
1E:??GGLL51 #$ @ 
   ' (  g%'	299c9B&%i077C7@oo#o.hh|,Gloogmm<<AAbRVAWD4: ==EE#ENIgf$&--c37
&#	/:
z:>>@A[ -^ yy!, AJJ	1Av6	JV!f:&!*-q6h
+

 }}T'9D6&BCC Ks   *O N)r   r	   r5   r8   )__name__
__module____qualname____firstlineno__classmethodrJ   r   Tensorru   strr   r   r   __static_attributes__r   r   r   r)   r)   .   s|     
  
D SDELL SDE SDU SD_d SDz} SD  MO  MZ  MZ SD SDr   r)   )rl   rn   r   PILr   comfy_api.latestr   comfy.model_managementmodel_managementrq   rj   r   r   r   r   r   r   LC_CUTr   r   r   r%   r'   	ComfyNoder)   r   r   r   <module>r      s    	     #  	F]T!
GELL GU\\ Gu||  u||  xD2<< xDr   