o
    i                     @   s   d dl Z d dlmZ d dlmZ d dlmZmZmZ d dl	m
Z
 er(d dlmZ eG dd dZeG d	d
 d
ZdddZde
jdedefddZde
jddfddZdddeddfddZdS )    N)	dataclass)import_module)TYPE_CHECKINGLiteralOptional)rtc)Imagec                   @   sF   e Zd ZU dZdZed ed< 	 dZed ed< 	 dZ	e
dB ed	< dS )
EncodeOptionsz>Options for encoding rtc.VideoFrame to portable image formats.JPEG)r
   PNGformatNResizeOptionsresize_optionsK   quality)__name__
__module____qualname____doc__r   r   __annotations__r   r   r   int r   r   c/var/www/html/livekit_bhavya/venv/lib/python3.10/site-packages/livekit/agents/utils/images/image.pyr	      s   
 r	   c                   @   s2   e Zd ZU dZeed< 	 eed< 	 ed ed< dS )r   zSOptions for resizing rtc.VideoFrame as part of encoding to a portable image format.widthheight)center_aspect_fitcenter_aspect_coverscale_aspect_fitscale_aspect_coverskewstrategyN)r   r   r   r   r   r   r   r   r   r   r   r   (   s   
 r   returnc                   C   s>   zdt  vrtdt  d< W d S W d S  ty   tdd w )Nr   z	PIL.ImagezYou haven't included the 'images' optional dependencies. Please install the 'codecs' extra by running `pip install livekit-agents[images]`)globalsr   ImportErrorr   r   r   r   
import_pilB   s   
r$   frameoptionsc                 C   sj   t   t| }t||}t }i }|jdkr!|jdur!|j|d< |j||jfi | |d |	 S )zoEncode a rtc.VideoFrame to a portable image format (JPEG or PNG).

    See EncodeOptions for more details.
    r
   Nr   r   )
r$   _image_from_frame_resize_imageioBytesIOr   r   saveseekread)r%   r&   imgresizedbufferkwargsr   r   r   encodeL   s   


r2   zImage.Imagec                 C   sF   | }| j tjjkr| tjj}td| j| jft	|j
d}|S )NRGBARGB)typer   VideoBufferTyper3   convertr   	frombytesr   r   bytesdata)r%   	converted	rgb_imager   r   r   r'   ]   s   r'   imagec                 C   sH  |j d u r| S |j }|jdkr| |j|jfS |jdkrltd|j|jf}|j}t| j|j| j  }|j|j | j| j krO|j}t| j|j| j  }| ||f}tj|||j| d |j| d f |S |jdkrtd|j|jf}t| j|j| j  }|j}|j|j | j| j krt| j|j| j  }|j}| ||f}tj|||j| d |j| d f |S |jdkr|j}t| j|j| j  }||jk r|j}t| j|j| j  }| ||fS |jdkr|j}t| j|j| j  }||jkr|j}t| j|j| j  }| ||fS t	d|j )	Nr   r   r4      r   r   r   zUnknown resize strategy: )
r   r    resizer   r   r   newr   paste
ValueError)r=   r&   resize_optsresult	new_width
new_heightr/   r   r   r   r(   h   sh   





r(   )r!   N)r)   dataclassesr   	importlibr   typingr   r   r   livekitr   PILr   r	   r   r$   
VideoFramer9   r2   r'   r(   r   r   r   r   <module>   s   

