U
    “~Hj{%  ã                
   @   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 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 n6 ek
rÖ Z zddlm Z  e eƒZW 5 dZ[X Y nX G dd„ de	j	ƒZ!G dd„ de!ƒZ"e#dœdd„Z$dd„ Z%dd„ Z&ddœdd„Z'G dd„ de!ƒZ(e#dœdd„Z)dd „ Z*d!d"„ Z+ddœd#d$„Z,d%d&„ Z-ed'œd(d)„Z.ed'œd*d+„Z/eeje#dd,œd-d.„Z0dS )/zPlugins for the Pillow library.é    )Úchain)ÚUnion)Ú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eedf ed< dZe	ed< ‡ fdd„Z
dd	„ Z‡ fd
d„Zedd… dkrledœdd„Zdd„ Zdd„ Zddœdd„Zeedœdd„ƒZeedœdd„ƒZdd„ Zeddœdd„Z‡  ZS ) Ú_LibHeifImageFilezVBase class with all functionality for ``HeifImageFile`` and ``AvifImageFile`` classes.NÚ
_heif_fileTÚ_modec                    s   d| _ tƒ j||Ž d S )Nr   )Ú_LibHeifImageFile__frameÚsuperÚ__init__)ÚselfÚargsÚkwargs©Ú	__class__© ú9/tmp/pip-unpacked-wheel-ne56ham8/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k
rP } ztt|ƒƒd ‚W 5 d }~X Y nX |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   r   Ú	exceptionr!   r!   r"   Ú_open+   s     z_LibHeifImageFile._openc                    s®   | j r¤| j |  ¡  }z0|j}|j| _|  ¡  |  |d|j|jf¡ W n$ t	k
rh   t
js\‚ |  ¡  Y nX | jr’t| ddƒr’t| jdƒr’| j ¡  d | _| js¤d | _ tƒ  ¡ S )NÚrawZ_exclusive_fpFÚclose)r   ÚtellÚdataÚsizeÚ_sizeZload_prepareZ	frombytesÚmodeZstrider(   r   ZLOAD_TRUNCATED_IMAGESr#   ÚgetattrÚhasattrr0   Úis_animatedr   Úload)r   Z
frame_heifr2   r   r!   r"   r9   7   s"    
z_LibHeifImageFile.loadé   )z10.1z10.2z10.3©Úreturnc                 C   s:   | j  dd¡r6| j d  dd¡}|d r6|  |d ¡S i S )z”Returns 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"   ÚgetxmpM   s
    z_LibHeifImageFile.getxmpc                 C   sH   |   |¡sd S || _|  |¡ t| dd ƒ}|d k	rDt|dd ƒrDd|_d S )NÚ_exifÚ_loadedF)Ú_seek_checkr   r,   r6   rD   )r   ÚframerC   r!   r!   r"   ÚseekX   s    

z_LibHeifImageFile.seekc                 C   s   | j S ©N)r   ©r   r!   r!   r"   r1   a   s    z_LibHeifImageFile.tellc                 C   s   d S rH   r!   rI   r!   r!   r"   Úverifyd   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   ÚlenrI   r!   r!   r"   Ún_framesg   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	   )rL   rI   r!   r!   r"   r8   o   s    z_LibHeifImageFile.is_animatedc                 C   s&   |dk s|| j krtdƒ‚|  ¡ |kS )Nr   z attempt to seek outside sequence)rL   r(   r1   )r   rF   r!   r!   r"   rE   t   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   r3   r4   r5   r   r?   r   )r   rM   r!   r!   r"   r,   y   s
    z&_LibHeifImageFile._init_from_heif_file)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Ú__annotations__Z!_close_exclusive_fp_after_loadingr)   r   r.   r9   Úpil_versionÚdictrB   rG   r1   rJ   ÚpropertyÚintrL   Úboolr8   rE   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©rN   rO   rP   rQ   ÚformatÚformat_descriptionr!   r!   r!   r"   rY   �   s   rY   r;   c                 C   s.   t | dƒ}|dd… dkrdS |dd… dkS )Né   r:   é   ó   ftypF)
s   heics   heixs   heims   heiss   hevcs   hevxs   hevms   hevss   mif1s   msf1©r   ©r#   Úmagicr!   r!   r"   Ú_is_supported_heifˆ   s    
rd   c                 C   s   t | |tjƒ d S rH   )Ú
__save_oner   ÚHEVC©Úimr#   Ú	_filenamer!   r!   r"   Ú
_save_heif�   s    rj   c                 C   s   t | |tjƒ d S rH   )Ú
__save_allr   rf   rg   r!   r!   r"   Ú_save_all_heif“   s    rl   c                  K   sp   t f | Ž t tjtt¡ t ¡ d rBt tjt	¡ t 
tjt¡ dddddg}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`.
    rZ   z.heicz.heicsz.heifz.heifsz.hifz
image/heifN)Ú__options_updater   Úregister_openrY   r\   rd   Ú_pillow_heifÚget_lib_infoÚregister_saverj   Úregister_save_allrl   Úregister_mimeÚregister_extensions©r   Ú
extensionsr!   r!   r"   Úregister_heif_opener—   s    
rw   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 containerNr[   r!   r!   r!   r"   rx   ¦   s   rx   c                 C   s.   t | dƒ}|dd… dkrdS |dd… dkS )Nr^   r:   r_   r`   Fs   avifra   rb   r!   r!   r"   Ú_is_supported_avif­   s    
rz   c                 C   s   t | |tjƒ d S rH   )re   r   ÚAV1rg   r!   r!   r"   Ú
_save_avifº   s    r|   c                 C   s   t | |tjƒ d S rH   )rk   r   r{   rg   r!   r!   r"   Ú_save_all_avif¾   s    r}   c                  K   sx   t  ¡ d stddd� dS tf | Ž t tjtt¡ t 	tjt
¡ t tjt¡ dg}t tjd¡ t tj|¡ dS )z‚Registers a Pillow plugin for AVIF format.

    :param kwargs: dictionary with values to set in options. See: :ref:`options`.
    ry   z=This version of `pillow-heif` was built without AVIF support.r	   ©Ú
stacklevelNz.avifz
image/avif)ro   rp   r   rm   r   rn   rx   r\   rz   rq   r|   rr   r}   rs   rt   ru   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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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QUALITYZSAVE_HDR_TO_12_BITZDECODE_THREADSZALLOW_INCORRECT_HEADERSZSAVE_NCLX_PROFILEZPREFERRED_ENCODERZPREFERRED_DECODERr   )r   ÚkÚvr!   r!   r"   rm   Ô   s(    rm   )Úcompression_formatc                 C   s.   t |f| jŽ}t|| df| jŽ | |¡ d S )NT)r   ÚencoderinfoÚ_pil_encode_imageÚsave)rh   r#   r„   Ú	ctx_writer!   r!   r"   re   í   s    re   c           	      C   s°   t |f| jŽ}t| dƒr |  ¡ nd }| j dg ¡}ttt | ¡|ƒ| j dd ¡ƒ}t	tt | ¡|ƒƒD ]\}}t
||||kf| jŽ qf|d k	r¢t| dƒr¢|  |¡ | |¡ d S )Nr1   Úappend_imagesr+   rG   )r   r…   r7   r1   r@   r   r   r   ÚIteratorÚ	enumerater†   rG   r‡   )	rh   r#   r„   rˆ   Úcurrent_framer‰   r+   ÚirF   r!   r!   r"   rk   ó   s     ÿ
rk   )ÚctxÚimgÚprimaryr<   c                 K   s¶   |j d dks|j d dkr$tdƒ‚|j ¡ }t|ƒ|d< t|ƒ|d< |rV|jf |Ž ||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.Zexifr=   r�   ZYCbCrZimage_orientation)r3   r%   r?   Úcopyr   r   Úupdater5   Zadd_image_ycbcrr   r   Z	add_imageÚtobytes)rŽ   r�   r�   r   Ú_infoZ_imgr!   r!   r"   r†     s(    

  ÿÿÿr†   )1rQ   Ú	itertoolsr   Útypingr   Úwarningsr   ZPILr   r   r   r   rS   Ú r
   Ú	constantsr   Zheifr   Úmiscr   r   r   r   r   r   r   r   ro   ÚImportErrorÚexZ_deffered_errorr   r   rY   rW   rd   rj   rl   rw   rx   rz   r|   r}   r€   rm   re   rk   r†   r!   r!   r!   r"   Ú<module>   s:   (a