
    
3j                          S r SSKrSSKrSSKrSSKJr  SSKJr  \R                  " S5      r " S S5      r	\	" 5       r
S	\S
\4S jrS\S
\4S jr\" SS9 " S S5      5       rg)a  HDR utilities and ComfyUI node for HDR IC-LoRA inference.

Provides HDR compression/decompression transforms and a single ComfyUI node:

- LTXVHDRDecodePostprocess: Decompress VAE output, tonemap for preview,
  and optionally save raw linear HDR frames as EXR.
    N)Tensor   )
comfy_nodezLTXVideo.hdrc                   X    \ rS rSrSrSrSrSrSrSr	Sr
S	rS
\S\4S jrS\S\4S jrSrg)LogC3   zARRI LogC3 (EI 800) HDR compression.

Maps linear [0, inf) -> [0, 1] via the camera log curve, then scales to
[-1, 1] for VAE input.
gb8@g6Deê?g?gxa?gdzx@gFCƣT·?gv4?hdrreturnc                 ~   [         R                  " USS9nU R                  [         R                  " U R                  U-  U R
                  -   5      -  U R                  -   nU R                  U-  U R                  -   n[         R                  " X R                  :  X45      n[         R                  " USS5      nUS-  S-
  $ )N        min      ?       @)torchclampClog10ABDEFwhereCUT)selfr	   xlog_partlin_partlogcs         A/home/wildlama/comfy/ComfyUI/custom_nodes/ComfyUI-LTXVideo/hdr.pycompressLogC3.compress(   s    KK%66EKK
TVV(;<<tvvE66A:&{{1=(={{4c*czC    zc                    [         R                  " US-   S-  SS5      nU R                  U R                  -  U R                  -   n[         R
                  " SX R                  -
  U R                  -  5      U R                  -
  U R                  -  nX R                  -
  U R                  -  n[         R                  " X#:  XE5      $ )Nr   r   r         $@)r   r   r   r   r   powr   r   r   r   r   )r   r%   r    cut_loglin_from_loglin_from_lins         r!   
decompressLogC3.decompress0   s    {{AGs?C5&&488#dff,		$$&&(@ADFFJdffTvv/{{4?LGGr$    N)__name__
__module____qualname____firstlineno____doc__r   r   r   r   r   r   r   r   r"   r,   __static_attributes__r.   r$   r!   r   r      sU     	AAAAAA
C F  v  HF Hv Hr$   r   
decoded_01r
   c                 X    U R                  5       S-  S-
  n[        R                  U5      $ )zDecompress VAE-decoded image from [0,1] to linear HDR [0, inf).

ComfyUI's VAE decode returns images in [0, 1] via ``(raw + 1) / 2``.
This function reverses that and applies LogC3 HDR decompression.
r   r   )float_LOGC3r,   )r5   raws     r!   _hdr_decompressr:   ?   s-     


s
"S
(CS!!r$   r   c                     [         R                  " U S:*  SU -  S[         R                  " U R                  SS9S5      -  S-
  5      R                  SS5      $ )	z%Convert linear [0, 1] to sRGB [0, 1].gsåi?gףp=
)@gzG?r   g?g)\(?r   r   )r   r   r(   r   )r   s    r!   _linear_to_srgbr<   I   sQ    ;;	Y			!''i'0)<<uD eCo	r$   LTXVHDRDecodePostprocess)namec                       \ rS rSrSr\S 5       rSrSrSr	Sr
SrS	r     SS
\R                  S\S\S\S\S\S\4S jjr\S\R                  S\S\S\SS4
S j5       rSrg)r=   W   aE  Decompress HDR from VAE output, tonemap for preview, optionally save EXR.

Place after VAE Decode in an HDR IC-LoRA workflow. Recovers linear HDR
values from the compressed latent space and tonemaps them to SDR for
display. When ``save_exr`` is enabled, also writes the raw linear HDR
frames as an EXR image sequence.
make sure to set OPENCV_IO_ENABLE_OPENEXR=1 environment in the command line  # Must be set before cv2 import


Outputs:
    tonemapped: SDR preview [0, 1] after Reinhard tonemap + sRGB gamma.
    hdr_linear: Raw linear HDR values [0, inf) for further processing.
c           	      T    SS0SSSSSSS	S
.4SSSS.4SSSS.4SSS04SSSS.4S.S.$ )Nimage)IMAGEFLOATr   g      $r'   g?sliderzNExposure in stops (EV). 0 = no change, +1 = 2x brighter, -1 = half brightness.)defaultr   maxstepdisplaytooltipBOOLEANFz+Save raw linear HDR frames as EXR sequence.)rF   rJ   STRINGoutput/hdr_exrzRDirectory for EXR frames (relative to ComfyUI output directory, or absolute path).rF   frameTzVSave EXR as float16 (half). Smaller files, negligible quality loss for most workflows.)exposuresave_exr
output_dirfilename_prefixhalf_precision)requiredoptionalr.   )ss    r!   INPUT_TYPES$LTXVHDRDecodePostprocess.INPUT_TYPESg   s     
 #&$# ##+F
 #(#P #3C	 ($
 #'J	#I.	3
 3	
r$   )rC   rC   )
tonemapped
hdr_linearTpostprocesszLightricks/HDRa[  Decompresses VAE-decoded output from HDR IC-LoRA (LogC3) and applies Reinhard tonemapping. Place after VAE Decode. 'tonemapped' is the SDR preview; 'hdr_linear' is raw linear HDR for downstream use. Enable 'save_exr' to write an EXR image sequence.if save_exr is enabled, make sure to set OPENCV_IO_ENABLE_OPENEXR=1 environment in the command linerB   rO   rP   rQ   rR   rS   r
   c                 (   [        U5      n[        R                  " USSS9nSU-  nXx-  n	U	SU	-   -  R                  SS5      n
[        U
5      nU(       a<  [        R
                  R                  S5      S:X  d   S5       eU R                  XtXV5        X4$ )	Nr   g     @)r   rG   r   r   OPENCV_IO_ENABLE_OPENEXR1zEXR output is enabled (save_exr = TRUE), but OpenCV does not support EXR by default. To enable it, set the environment variable OPENCV_IO_ENABLE_OPENEXR=1 before starting ComfyUI, then restart. Alternatively, disable EXR output or switch to PNG/JPG.)r:   r   r   r<   osenvironget_save_exr_frames)r   rB   rO   rP   rQ   rR   rS   r	   exposure_multhdr_exposedtonemapped_linearrY   s               r!   r[   $LTXVHDRDecodePostprocess.postprocess   s     e$kk#3C0 X)(C+,=>EEc3O$%67
::>>"<=D JD
 !!#?S  r$   	hdr_imageNc                 |    SS K nSS KnSS Kn[        R                  R                  U5      (       d.  [        R                  R                  UR                  5       U5      n[        R                  " USS9  U R                  5       R                  5       nU(       a  UR                  OUR                  nUR                  UUR                   UR"                  /n	[%        UR&                  S   5       Hs  n
Xz   S S 2S S 2S S S24   R)                  UR*                  5      R-                  5       n[        R                  R                  X SU
S S35      nUR/                  XU	5        Mu     [        R1                  S	UR&                  S   UU(       a  S
5        g S5        g ! [         a    [        R                  S5         g f = f)Nr   zQopencv-python is required for EXR export. Install with: pip install opencv-pythonT)exist_ok_05dz.exrz Saved %d EXR frame(s) to %s (%s)float16float32)cv2ImportErrorloggererrorfolder_pathsnumpyr_   pathisabsjoinget_output_directorymakedirscpuIMWRITE_EXR_TYPE_HALFIMWRITE_EXR_TYPE_FLOATIMWRITE_EXR_TYPEIMWRITE_EXR_COMPRESSIONIMWRITE_EXR_COMPRESSION_ZIPrangeshapeastypern   copyimwriteinfo)rg   rQ   rR   rS   ro   rs   npframesexr_typeparamsi	frame_bgrru   s                r!   rb   )LTXVHDRDecodePostprocess._save_exr_frames   so   	 	ww}}Z((l&G&G&I:VJ
J.&&()7C%%S=W=W 	   ''++	
 v||A'A	!Q"*-44RZZ@EEGI77<<
.?q3t,LMDKK0 (
 	.LLO'I		
 .7		
?  	LL: 	s   F F;:F;r.   )r   FrM   rN   T)r/   r0   r1   r2   r3   classmethodrW   RETURN_TYPESRETURN_NAMESOUTPUT_NODEFUNCTIONCATEGORYDESCRIPTIONr   r   r7   boolstrtupler[   staticmethodrb   r4   r.   r$   r!   r=   r=   W   s     4
 4
l &L/LKHH	n  *&#!||! ! 	!
 ! ! ! 
!: ,
<<,
,
 ,
 	,

 
,
 ,
r$   )r3   loggingr_   r   r   nodes_registryr   	getLoggerrq   r   r8   r:   r<   r=   r.   r$   r!   <module>r      s     	   &			>	*H HF 
" "6 "v &  +,]
 ]
 -]
r$   