o
    پi                     @   s  d dl Z d dlZd dlZd dlZd dlZ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 deddfddZde	jdejfd	d
Zde	jde	jfddZdde	jdedefddZde	jdefddZdede	jfddZG dd deZdedededefddZdS )    N)
VideoInput)Imagepathreturnc                 C   s   t j| dd d S )NTexist_ok)osmakedirs)r    r
   i/home/ubuntu/.local/lib/python3.10/site-packages/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/utils.py_ensure_dir   s   r   imagec                 C   s   |   dkr
| d } |   dkr| jd dv r| ddd} n|   dkr*| d} |    }t|dd	}|d
 	tj
}|jd dkrPtj|ddd}|S )z<Convert ComfyUI image tensor to uint8 numpy array (H, W, C).   r         r   r   r                    ?   )axis)dimshapepermute	unsqueezedetachcpunumpynpclipastypeuint8repeat)r   np_imgr
   r
   r   _to_numpy_image   s   
r%   c                 C   s   |   }| dkr|d }| dkr#|jd dv r#|ddd}n| dkr.|d}t|dd	}|jd dkrC|ddd}|S )
z?Convert ComfyUI image tensor to HWC format (normalized [0, 1]).r   r   r   r   r   r   r   r   r   )cloner   r   r   r   torchclampr#   )r   imgr
   r
   r   _to_hwc_tensor#   s   
r*   ư>	tolerancec           
      C   sb   | du rdS t | }|ddddf }|j\}}}|d|}t|| }t|}	|	 |kS )a]  
    Check if the input image is an empty/solid color image (like ComfyUI's empty image).
    Args:
        image: Input tensor image in ComfyUI format (BCHW, CHW, HWC, etc.)
        tolerance: Tolerance for floating point comparison (default: 1e-6)

    Returns:
        True if the image is empty (all pixels have same color), False otherwise
    NTr   r   )r*   r   reshaper'   absmaxitem)
r   r,   img_hwcfirst_pixelhwcpixelsdiffmax_diffr
   r
   r   is_empty_image4   s   

r9   c                 C   sh   t  }td}t jdd }d| d| d}tj	||}t
| }t|}|j|dd |S )	zQ
    Save tensor image to ComfyUI temp directory as PNG and return the path.
    z%Y%m%d-%H%M%SN   sgl_output__z.pngPNG)format)folder_pathsget_temp_directorytimestrftimeuuiduuid4hexr   r   joinr%   r   	fromarraysave)r   temp_dirtsunique	file_name	file_pathr$   r)   r
   r
   r   get_image_pathP   s   

rN   	b64_imagec                 C   s`   t | }tt|}|jdkr|d}t	|
tjd }|tjdf }t|}|S )a.  
    Convert base64 encoded image to ComfyUI IMAGE format (torch.Tensor).

    Args:
        b64_image: Base64 encoded image string

    Returns:
        torch.Tensor with shape [batch_size, height, width, channels] (BHWC format),
        values normalized to [0, 1] range, RGB format (3 channels)
    RGBg     o@.)base64	b64decoder   openioBytesIOmodeconvertr   arrayr!   float32newaxisr'   
from_numpy)rO   image_bytes	pil_imageimage_arraytensor_imager
   r
   r   convert_b64_to_tensor_imaged   s   



r`   c                       sX   e Zd Zdededef fddZdeeef fddZd	d
 ZddefddZ	  Z
S )SGLDVideoInput
video_pathheightwidthc                    s    t    || _|| _|| _d S )N)super__init__rb   rc   rd   )selfrb   rc   rd   	__class__r
   r   rf      s   

zSGLDVideoInput.__init__r   c                 C   s   | j | jfS )zs
        Returns the dimensions of the video input.

        Returns:
            Tuple of (width, height)
        )rd   rc   rg   r
   r
   r   get_dimensions   s   zSGLDVideoInput.get_dimensionsc                 C   s   | j gS )z|
        Returns the components of the video input.
        This is required by the VideoInput abstract base class.
        )rb   rj   r
   r
   r   get_components   s   zSGLDVideoInput.get_componentsNr   c                 C   sF   |}t j| jr!t j|}|rt j|dd t| j| dS dS )zD
        Abstract method to save the video input to a file.
        Tr   N)r   r   existsrb   dirnamer	   shutilcopy2)rg   r   r>   codecmetadata	save_pathsave_dirr
   r
   r   save_to   s   zSGLDVideoInput.save_to)NNN)__name__
__module____qualname__strintrf   tuplerk   rl   ru   __classcell__r
   r
   rh   r   ra      s
    	ra   rb   rc   rd   c                 C   s   t | ||}|S )z=
    Convert video to ComfyUI VIDEO format (VideoInput).
    )ra   )rb   rc   rd   video_inputr
   r
   r   convert_video_to_comfy_video   s   r~   )r+   )rQ   rT   r   ro   rA   rC   r?   r   r   r'   comfy_api.inputr   PILr   ry   r   Tensorndarrayr%   r*   floatboolr9   rN   r`   ra   rz   r~   r
   r
   r
   r   <module>   s6    &