o
    •j‚  ã                
   @   s¨  d Z ddlm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 ddlmZ dd	lmZ dd
lmZmZmZmZmZmZmZmZ zddlZW n eyf Z zddlmZ eeƒZW Y dZ[ndZ[ww G dd„ de	j	ƒZG dd„ deƒZ de!fdd„Z"dejdee# de$e#B fdd„Z%dejdee# de$e#B fdd„Z&d)dd„Z'dd„ Z(dejdee# defd d!„Z)dejdee# defd"d#„Z*d$ed%ejd&e!ddfd'd(„Z+dS )*zPlugins for the Pillow library.é    )Úchain)ÚIO)Úwarn)ÚImageÚ	ImageFileÚImageSequenceé   )Úoptions)ÚHeifCompressionFormat)ÚHeifFile)Ú	CtxEncodeÚ_exif_from_pillowÚ
_get_bytesÚ_get_orientation_for_encoderÚ_get_primary_indexÚ_pil_to_supported_modeÚ_xmp_from_pillowÚset_orientationN)ÚDeferredErrorc                       s¼   e Zd ZU dZdZedB ed< dZeed< ‡ fdd„Z	dd	„ Z
‡ fd
d„Zdefdd„Zdefdd„Zddd„Zedefdd„ƒZedefdd„ƒZdefdd„Zdeddfdd„Z‡  ZS )Ú_LibHeifImageFilez>Base class with all functionality for ``HeifImageFile`` class.NÚ
_heif_fileTÚ_modec                    s   d| _ tƒ j|i |¤Ž d S )Nr   )Ú_LibHeifImageFile__frameÚsuperÚ__init__)ÚselfÚargsÚkwargs©Ú	__class__© ú_C:\Users\psant\Downloads\Anu Raj Backend\school\venv\Lib\site-packages\pillow_heif/as_plugin.pyr   &   s   z_LibHeifImageFile.__init__c                 C   st   zt | jdddd�}W n tttttfy# } ztt|ƒƒd ‚d }~ww |j| _	|| _
|j| _|  | j¡ g | _d S )NTF)Zconvert_hdr_to_8bitZhdr_to_16bitZremove_stride)r   ÚfpÚOSErrorÚ
ValueErrorÚSyntaxErrorÚRuntimeErrorÚEOFErrorÚstrÚmimetypeZcustom_mimetyper   Úprimary_indexr   Ú_init_from_heif_fileZtile)r   Z	heif_fileÚ	exceptionr    r    r!   Ú_open*   s   €ÿ
z_LibHeifImageFile._openc              	      s°   | j rS| j |  ¡  }z|j}|j| _|  ¡  |  |d|j|jf¡ W n t	t
fy5   tjs/‚ |  ¡  Y nw | jrJt| ddƒrJt| jdƒrJ| j ¡  d | _| jsSd | _ tƒ  ¡ S )NÚrawZ_exclusive_fpFÚclose)r   ÚtellÚdataÚsizeÚ_sizeZload_prepareZ	frombytesÚmodeZstrider'   r$   r   ZLOAD_TRUNCATED_IMAGESr"   ÚgetattrÚhasattrr/   Úis_animatedr   Úload)r   Z
frame_heifr1   r   r    r!   r8   6   s$   ý

z_LibHeifImageFile.loadÚframec                 C   sd   |   |¡sd S || _|  |¡ tj | j| j¡| _t	| dd ƒ}|d ur.t	|dd ƒr0d|_
d S d S d S )NZ_exifÚ_loadedF)Ú_seek_checkr   r+   r   ÚcoreÚnewr   r3   Úimr5   r:   )r   r9   Úexifr    r    r!   ÚseekJ   s   


ÿz_LibHeifImageFile.seekÚreturnc                 C   s   | j S ©N)r   ©r   r    r    r!   r0   Z   s   z_LibHeifImageFile.tellc                 C   s   d S rB   r    rC   r    r    r!   Úverify]   s   z_LibHeifImageFile.verifyc                 C   s   | j rt| j ƒS dS )zbReturns the number of available frames.

        :returns: Frame number, starting with 0.
        r   N)r   ÚlenrC   r    r    r!   Ún_frames`   s   z_LibHeifImageFile.n_framesc                 C   s
   | j dkS )zTReturns ``True`` if this image contains more than one frame, or ``False`` otherwise.r   N)rF   rC   r    r    r!   r7   h   s   
z_LibHeifImageFile.is_animatedc                 C   s&   |dk s	|| j krtdƒ‚|  ¡ |kS )Nr   z attempt to seek outside sequence)rF   r'   r0   )r   r9   r    r    r!   r;   m   s   z_LibHeifImageFile._seek_checkÚ	img_indexc                 C   sH   | j r"| j | j| _| j | j| _| j | j| _t| jƒ| jd< d S d S )NZoriginal_orientation)r   r2   r3   r4   r   Úinfor   )r   rG   r    r    r!   r+   r   s   üz&_LibHeifImageFile._init_from_heif_file©rA   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ú__annotations__Z!_close_exclusive_fp_after_loadingr(   r   r-   r8   Úintr@   r0   rD   ÚpropertyrF   Úboolr7   r;   r+   Ú__classcell__r    r    r   r!   r      s"   
 
r   c                   @   s   e Zd ZdZdZdZdS )ÚHeifImageFilez1Pillow plugin class type for a HEIF image format.ÚHEIFzHEIF containerN)rJ   rK   rL   rM   ÚformatÚformat_descriptionr    r    r    r!   rS   z   s    rS   rA   c                 C   s.   t | dƒ}|dd… dkrdS |dd… dv S )Né   é   é   s   ftypF)
s   heics   heixs   heims   heiss   hevcs   hevxs   hevms   hevss   mif1s   msf1)r   )r"   Úmagicr    r    r!   Ú_is_supported_heif�   s   
r[   r>   r"   Ú	_filenamec                 C   ó   t | |tjƒ d S rB   )Ú
__save_oner
   ÚHEVC©r>   r"   r\   r    r    r!   Ú
_save_heifˆ   ó   ra   c                 C   r]   rB   )Ú
__save_allr
   r_   r`   r    r    r!   Ú_save_all_heifŒ   rb   rd   c                  K   sn   t di | ¤Ž t tjtt¡ t ¡ d r#t tjt	¡ t 
tjt¡ g d¢}t tjd¡ t tj|¡ dS )z‚Registers a Pillow plugin for HEIF format.

    :param kwargs: dictionary with values to set in options. See: :ref:`options`.
    rT   )z.heicz.heicsz.heifz.heifsz.hifz
image/heifNr    )Ú__options_updater   Zregister_openrS   rU   r[   Ú_pillow_heifZget_lib_infoZregister_savera   Zregister_save_allrd   Zregister_mimeZregister_extensions)r   Ú
extensionsr    r    r!   Úregister_heif_opener�   s   rh   c                  K   sÈ   |   ¡ D ]]\}}|dkr|t_q|dkr|t_q|dkr |t_q|dkr(|t_q|dkr0|t_q|dkr8|t_q|dkr@|t_q|dkrH|t_	q|d	krP|t_
q|d
krX|t_qtd|› �dd� qdS )zDInternal function to set options from `register_heif_opener` method.Z
thumbnailsZdepth_imagesZ
aux_imagesZqualityZsave_to_12bitZdecode_threadsZallow_incorrect_headersZsave_nclx_profileZpreferred_encoderZpreferred_decoderzUnknown option: r   )Ú
stacklevelN)Úitemsr	   Z
THUMBNAILSZDEPTH_IMAGESZ
AUX_IMAGESZQUALITYZSAVE_HDR_TO_12_BITZDECODE_THREADSZALLOW_INCORRECT_HEADERSZSAVE_NCLX_PROFILEZPREFERRED_ENCODERZPREFERRED_DECODERr   )r   ÚkÚvr    r    r!   re   Ÿ   s.   êre   Úcompression_formatc                 C   s6   t |fi | j¤Ž}t|| dfi | j¤Ž | |¡ d S )NT)r   ÚencoderinfoÚ_pil_encode_imageÚsave)r>   r"   rm   Ú	ctx_writer    r    r!   r^   º   s   r^   c           	      C   s¸   t |fi | j¤Ž}t| dƒr|  ¡ nd }| j dg ¡}ttt | ¡|ƒ| j dd ¡ƒ}t	tt | ¡|ƒƒD ]\}}t
||||kfi | j¤Ž q5|d urUt| dƒrU|  |¡ | |¡ d S )Nr0   Úappend_imagesr*   r@   )r   rn   r6   r0   Úgetr   r   r   ÚIteratorÚ	enumeratero   r@   rp   )	r>   r"   rm   rq   Úcurrent_framerr   r*   Úir9   r    r    r!   rc   À   s   ÿ
rc   ÚctxÚimgÚprimaryc                 K   s¸   |j d dks|j d dkrtdƒ‚|j ¡ }t|ƒ|d< t|ƒ|d< |jd	i |¤Ž ||d< |jdkrC| j|fdt	|ƒi|¤Ž d S t
|ƒ}| j|j |j| ¡ fdt	|ƒi|¤Ž d S )
Nr   r   zEmpty images are not supported.r?   Úxmprz   ZYCbCrZimage_orientationr    )r2   r$   rH   Úcopyr   r   Úupdater4   Zadd_image_ycbcrr   r   Z	add_imageÚtobytes)rx   ry   rz   r   rH   r    r    r!   ro   Î   s   

*ro   rI   ),rM   Ú	itertoolsr   Útypingr   Úwarningsr   ZPILr   r   r   Ú r	   Ú	constantsr
   Zheifr   Úmiscr   r   r   r   r   r   r   r   rf   ÚImportErrorÚexZ_deffered_errorr   r   rS   rQ   r[   Úbytesr(   ra   rd   rh   re   r^   rc   ro   r    r    r    r!   Ú<module>   s4    (€ý[  
 