a
    CgA)                  
   @  s  d Z ddl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 ddl	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 n8 ey Z  z ddl!m"Z" e"e ZW Y dZ [ n
dZ [ 0 0 G dd dejZ#G dd de#Z$ddddZ%ddddddZ&ddddddZ'ddd d!Z(G d"d# d#e#Z)ddd$d%Z*ddddd&d'Z+ddddd(d)Z,ddd*d+Z-d,d- Z.ddd.d/d0d1Z/ddd.d/d2d3Z0d4dddd5d6d7Z1dS )8zPlugins for the Pillow library.    )annotations)chain)IO)warn)Image	ImageFileImageSequence)__version__   )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ded< dZded<  fdd	Zd
d Z fddZ	e
dd dv rdddddZddddZddddZddddZeddddZeddd d!Zddd"d#Zddd$d%d&Z  ZS )'_LibHeifImageFilezVBase class with all functionality for ``HeifImageFile`` and ``AvifImageFile`` classes.NzHeifFile | None
_heif_fileTstr_modec                   s   d| _ t j|i | d S )Nr   )_LibHeifImageFile__framesuper__init__)selfargskwargs	__class__ T/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/pillow_heif/as_plugin.pyr   )   s    z_LibHeifImageFile.__init__c                 C  s   zt | jdddd}W n< tttttfyR } ztt|d W Y d }~n
d }~0 0 |j| _	|| _
|j| _| | j g | _d S )NTF)Zconvert_hdr_to_8bitZhdr_to_16bitZremove_stride)r   fpOSError
ValueErrorSyntaxErrorRuntimeErrorEOFErrorr   mimetypeZcustom_mimetyper   primary_indexr   _init_from_heif_fileZtile)r   Z	heif_file	exceptionr#   r#   r$   _open-   s    $z_LibHeifImageFile._openc                   s   | j r| j |   }z0|j}|j| _|   | |d|j|jf W n" t	yf   t
jsZ |   Y n0 | jrt| ddrt| jdr| j  d | _| jsd | _ t  S )NrawZ_exclusive_fpFclose)r   telldatasize_sizeZload_prepare	frombytesmodeZstrider*   r   ZLOAD_TRUNCATED_IMAGESr%   getattrhasattrr1   is_animatedr   load)r   Z
frame_heifr3   r!   r#   r$   r;   9   s"    
z_LibHeifImageFile.load   )z10.1z10.2z10.3dictreturnc                 C  s:   | j ddr6| j d dd}|d r6| |d S i S )zReturns a dictionary containing the XMP tags. Requires ``defusedxml`` to be installed.

            :returns: XMP tags in a dictionary.
            xmpN    r
   r   )infogetrsplitZ_getxmp)r   Zxmp_datar#   r#   r$   getxmpO   s
    z_LibHeifImageFile.getxmpint)framec                 C  sl   |  |sd S || _| | td d dkrBtj| j| j| _	t
| dd }|d urht
|dd rhd|_d S )N   z10.Z_exif_loadedF)_seek_checkr   r-   pil_versionr   corenewr   r5   imr8   rI   )r   rG   exifr#   r#   r$   seekZ   s    

z_LibHeifImageFile.seekc                 C  s   | j S N)r   r   r#   r#   r$   r2   k   s    z_LibHeifImageFile.tellNonec                 C  s   d S rQ   r#   rR   r#   r#   r$   verifyn   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
   )r   lenrR   r#   r#   r$   n_framesq   s    z_LibHeifImageFile.n_framesboolc                 C  s
   | j dkS )zTReturns ``True`` if this image contains more than one frame, or ``False`` otherwise.r
   )rV   rR   r#   r#   r$   r:   y   s    z_LibHeifImageFile.is_animatedc                 C  s&   |dk s|| j krtd|  |kS )Nr   z attempt to seek outside sequence)rV   r*   r2   )r   rG   r#   r#   r$   rJ   ~   s    z_LibHeifImageFile._seek_check)	img_indexr?   c                 C  sD   | j r@| j | j| _| j | j| _| j | j| _t| j| jd< d S )NZoriginal_orientation)r   r4   r5   r7   r   rB   r   )r   rX   r#   r#   r$   r-      s
    z&_LibHeifImageFile._init_from_heif_file)__name__
__module____qualname____doc__r   __annotations__Z!_close_exclusive_fp_after_loadingr   r/   r;   rK   rE   rP   r2   rT   propertyrV   r:   rJ   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rY   rZ   r[   r\   formatformat_descriptionr#   r#   r#   r$   r`      s   r`   rW   r>   c                 C  s.   t | d}|dd dkrdS |dd dv S )N   r<         ftypF)
s   heics   heixs   heims   heiss   hevcs   hevxs   hevms   hevss   mif1s   msf1r   r%   magicr#   r#   r$   _is_supported_heif   s    
rk   zImage.Imagez	IO[bytes]zstr | bytesrN   r%   	_filenamec                 C  s   t | |tj d S rQ   )
__save_oner   HEVCrl   r#   r#   r$   
_save_heif   s    rp   c                 C  s   t | |tj d S rQ   )
__save_allr   ro   rl   r#   r#   r$   _save_all_heif   s    rr   rS   c                  K  sn   t f i |  ttjtt t d rFttjt	 t
tjt g d}ttjd ttj| dS )zRegisters a Pillow plugin for HEIF format.

    :param kwargs: dictionary with values to set in options. See: :ref:`options`.
    ra   )z.heicz.heicsz.heifz.heifsz.hifz
image/heifN)__options_updater   register_openr`   rc   rk   _pillow_heifget_lib_inforegister_saverp   register_save_allrr   register_mimeregister_extensionsr    
extensionsr#   r#   r$   register_heif_opener   s    r}   c                   @  s   e Zd ZdZdZdZdS )AvifImageFilez2Pillow plugin class type for an AVIF image format.AVIFzAVIF containerNrb   r#   r#   r#   r$   r~      s   r~   c                 C  s.   t | d}|dd dkrdS |dd dkS )Nre   r<   rf   rg   Fs   avifrh   ri   r#   r#   r$   _is_supported_avif   s    
r   c                 C  s   t | |tj d S rQ   )rn   r   AV1rl   r#   r#   r$   
_save_avif   s    r   c                 C  s   t | |tj d S rQ   )rq   r   r   rl   r#   r#   r$   _save_all_avif   s    r   c                  K  s   t dtdd t d s*t ddd dS tf i |  ttjtt	 t
tjt ttjt dg}ttjd	 ttj| dS )
zRegisters a Pillow plugin for AVIF format.

    :param kwargs: dictionary with values to set in options. See: :ref:`options`.
    zThe AVIF support in this library is marked as deprecated and will be removed in the next version. If you still need AVIF support until it natively appears in Pillow, use the https://github.com/fdintino/pillow-avif-plugin project instead.   
stacklevelr   z=This version of `pillow-heif` was built without AVIF support.r
   Nz.avifz
image/avif)r   DeprecationWarningru   rv   rs   r   rt   r~   rc   r   rw   r   rx   r   ry   rz   r{   r#   r#   r$   register_avif_opener   s    r   c                  K  s   |   D ]\}}|dkr |t_q|dkr0|t_q|dkr@|t_q|dkrP|t_q|dkr`|t_q|dkrp|t_q|dkr|t_q|dkr|t_	q|d	kr|t_
q|d
kr|t_qtd| dd qdS )z`Internal function to set options from `register_avif_opener` and `register_heif_opener` methods.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
   r   N)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$   rs      s,    rs   r   )rN   r%   compression_formatc                 C  s6   t |fi | j}t|| dfi | j || d S )NT)r   encoderinfo_pil_encode_imagesave)rN   r%   r   	ctx_writer#   r#   r$   rn      s    rn   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 qj|d urt| dr| | || d S )Nr2   append_imagesr,   rP   )r   r   r9   r2   rC   r   r   r   Iterator	enumerater   rP   r   )	rN   r%   r   r   current_framer   r,   irG   r#   r#   r$   rq     s    
rq   r   )ctximgprimaryr?   c                 K  s   |j d dks|j d dkr$td|j }t||d< t||d< |jf i | ||d< |jdkr| j|fdt	|i| n.t
|}| j|j |j| fdt	|i| d S )	Nr   r
   zEmpty images are not supported.rO   r@   r   ZYCbCrZimage_orientation)r4   r'   rB   copyr   r   updater7   Zadd_image_ycbcrr   r   Z	add_imagetobytes)r   r   r   r    rB   r#   r#   r$   r     s    

r   )2r\   
__future__r   	itertoolsr   typingr   warningsr   ZPILr   r   r   r	   rK    r   	constantsr   Zheifr   miscr   r   r   r   r   r   r   r   ru   ImportErrorexZ_deffered_errorr   r   r`   rk   rp   rr   r}   r~   r   r   r   r   rs   rn   rq   r   r#   r#   r#   r$   <module>   s<   (i