o
    •jf^  ã                
   @   sÎ  d Z ddl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 dd	lmZ dd
lmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ zddl Z W n e!yt Z" zddl#m$Z$ e$e"ƒZ W Y dZ"["ndZ"["ww G dd„ dƒZ%G dd„ de%ƒZ&G dd„ de%ƒZ'G dd„ de%ƒZ(G dd„ dƒZ)de*fdd„Z+d,de)fdd„Z,d,de)fdd„Z-d e.d!e/e0e0f ddfd"d#„Z1d$e2e( ddfd%d&„Z3d'e
j
de)fd(d)„Z4d e.d!e/e0e0f de)fd*d+„Z5dS )-z8Functions and classes for heif images to read and write.é    )ÚcopyÚdeepcopy)ÚSEEK_SET)ÚLock)ÚAny)ÚImageé   )Úoptions)ÚHeifCompressionFormat)Ú	MODE_INFOÚ	CtxEncodeÚ	MimCImageÚ_exif_from_pillowÚ
_get_bytesÚ_get_heif_metaÚ_get_orientation_for_encoderÚ_get_primary_indexÚ_pil_to_supported_modeÚ_retrieve_exifÚ_retrieve_xmpÚ_rotate_pilÚ_xmp_from_pillowÚget_file_mimetypeÚsave_colorspace_chromaÚset_orientationN)ÚDeferredErrorc                   @   sz   e Zd ZU dZeeef ed< 	 eed< 	 dd„ Ze	dd„ ƒZ
e	defd	d
„ƒZe	dd„ ƒZdejfdd„Zddd„ZdS )Ú	BaseImagez^Base class for :py:class:`HeifImage`, :py:class:`HeifDepthImage` and :py:class:`HeifAuxImage`.ÚsizeÚmodec                 C   s&   |j \| _| _|| _d | _tƒ | _d S ©N)Ú	size_moder   r   Ú_c_imageÚ_datar   Ú
_load_lock)ÚselfÚc_image© r&   úZC:\Users\psant\Downloads\Anu Raj Backend\school\venv\Lib\site-packages\pillow_heif/heif.pyÚ__init__3   s   zBaseImage.__init__c                 C   s   |   ¡  | jS )zaDecodes image and returns image data.

        :returns: ``bytes`` of the decoded image.
        N)Úloadr"   ©r$   r&   r&   r'   Údata9   s   zBaseImage.dataÚreturnc                 C   s   |   ¡  | jjS )zÚStride of the image.

        .. note:: from `0.10.0` version this value always will have width * sizeof pixel in default usage mode.

        :returns: An Int value indicating the image stride after decoding.
        N)r)   r!   Ústrider*   r&   r&   r'   r-   B   s   zBaseImage.stridec                 C   sŒ   |   ¡  t| jt| j d  ƒ}t| j d dkrd}nt|d ƒ}d}| jd |f}t| j d dkr>|t| j d f7 }||d| jdœS )
zNumpy array interface support.r   r   é   z|u1é   z<u2é   )ÚshapeÚtypestrÚversionr+   N)r)   Úintr-   r   r   r   r+   )r$   Úwidthr2   r1   r&   r&   r'   Ú__array_interface__M   s   zBaseImage.__array_interface__c                 C   s&   |   ¡  t | j| j| jd| j| j¡S )ú£Helper method to create :external:py:class:`~PIL.Image.Image` class.

        :returns: :external:py:class:`~PIL.Image.Image` class created from an image.
        ÚrawN)r)   r   Z	frombytesr   r   r+   r-   r*   r&   r&   r'   Ú	to_pillow\   s   úzBaseImage.to_pillowNc                 C   sh   | j s2| j�" | j s | jj}| jj\| _}|| _ W d  ƒ dS W d  ƒ dS 1 s+w   Y  dS dS )zÊMethod to decode image.

        .. note:: In normal cases, you should not call this method directly,
            when reading `data` or `stride` property of image will be loaded automatically.
        N)r"   r#   r!   r+   r    r   )r$   r+   Ú_r&   r&   r'   r)   k   s   ü"ÿÿzBaseImage.load©r,   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Útupler4   Ú__annotations__Ústrr(   Úpropertyr+   r-   r6   r   r9   r)   r&   r&   r&   r'   r   '   s   
 


r   c                       s<   e Zd ZdZ‡ fdd„Zdd„ Zdejf‡ fdd„Z‡  ZS )	ÚHeifDepthImagez`Class representing the depth image associated with the :py:class:`~pillow_heif.HeifImage` class.c                    s,   t ƒ  |¡ |j}d|i| _t|| jƒ d S )NÚmetadata)Úsuperr(   rE   Úinfor   )r$   r%   rE   ©Ú	__class__r&   r'   r(   |   s
   ÿzHeifDepthImage.__init__c              	   C   ó0   d| j j› d| jd › d| jd › d| j› d�	S ©Nú<ú r   Úxr   ú>©rI   r<   r   r   r*   r&   r&   r'   Ú__repr__„   ó   0zHeifDepthImage.__repr__r,   c                    s   t ƒ  ¡ }| j ¡ |_|S )r7   N)rF   r9   rG   r   ©r$   ÚimagerH   r&   r'   r9   ‡   s   
zHeifDepthImage.to_pillow)	r<   r=   r>   r?   r(   rQ   r   r9   Ú__classcell__r&   r&   rH   r'   rD   y   s
    rD   c                   @   s   e Zd ZdZdd„ ZdS )ÚHeifAuxImagezdClass representing the auxiliary image associated with the :py:class:`~pillow_heif.HeifImage` class.c              	   C   rJ   rK   rP   r*   r&   r&   r'   rQ   ”   rR   zHeifAuxImage.__repr__N)r<   r=   r>   r?   rQ   r&   r&   r&   r'   rV   ‘   s    rV   c                       s†   e Zd ZdZ‡ fdd„Zdd„ Zedefdd„ƒZedefd	d
„ƒZ	e	j
defdd
„ƒZ	dejf‡ fdd„Zdedefdd„Z‡  ZS )Ú	HeifImagez;One image in a :py:class:`~pillow_heif.HeifFile` container.c                    sN  t ƒ  |¡ |j}t|ƒ}t|ƒ}tjrdd„ |jD ƒng }tjr)dd„ |j	D ƒng }t
|jƒt|jƒ||||dœ| _tjr_i }|jD ]}| |¡}	|	|vrRg ||	< ||	  |¡ qC|| jd< t|ƒ}
|rj|| jd< |
rq|
| jd< |j}|r{|| jd< t|| jƒ |j}|r¥|d	 d
v rœ|d | jd< |d	 | jd< d S |d | jd< d S d S )Nc                 S   s   g | ]}|d ur|‘qS r   r&   ©Ú.0Úir&   r&   r'   Ú
<listcomp>    s    z&HeifImage.__init__.<locals>.<listcomp>c                 S   ó   g | ]
}|d urt |ƒ‘qS r   )rD   rX   r&   r&   r'   r[   ¢   ó    )ÚprimaryÚ	bit_depthÚexifrE   Ú
thumbnailsÚdepth_imagesZauxÚxmpÚheifÚpixel_aspect_ratioÚtype)ZrICCZprofr+   Úicc_profileÚicc_profile_typeÚnclx_profile)rF   r(   rE   r   r   r	   Z
THUMBNAILSra   ZDEPTH_IMAGESZdepth_image_listÚboolr^   r4   r_   rG   Z
AUX_IMAGESZaux_image_idsZget_aux_typeÚappendr   re   r   Úcolor_profile)r$   r%   rE   r`   rc   ra   rb   Zctx_aux_infoÚaux_idZaux_typeZ	heif_metare   rl   rH   r&   r'   r(   ›   sL   ÿú





ûzHeifImage.__init__c                 C   sp   | j s	t| jtƒrt| jƒ› d�nd}d| jj› d| jd › d| jd › d| j	› d|› d	t| j
 d
g ¡ƒ› d�S )Nz bytesÚnorL   rM   r   rN   r   ú with z image data and ra   z thumbnails>)r"   Ú
isinstancer!   r   Úlenr+   rI   r<   r   r   rG   Úget)r$   Zs_bytesr&   r&   r'   rQ   Å   s   &,ÿÿÿzHeifImage.__repr__r,   c                 C   s   | j jdd�d d dv S )zD``True`` for images with the ``alpha`` channel, ``False`` otherwise.ú;©Úsepr   éÿÿÿÿ)ÚAÚaN)r   Úsplitr*   r&   r&   r'   Ú	has_alphaÌ   s   zHeifImage.has_alphac                 C   s   t | jjdd�d d dkƒS )zN``True`` for images with ``premultiplied alpha`` channel, ``False`` otherwise.rs   rt   r   rv   rx   N)rj   r   ry   r*   r&   r&   r'   Úpremultiplied_alphaÑ   s   zHeifImage.premultiplied_alphaÚvaluec                 C   s.   | j r| j |r
dnd|rdnd¡| _d S d S )Nrw   rx   )rz   r   Úreplace©r$   r|   r&   r&   r'   r{   Ö   s   $ÿc                    s*   t ƒ  ¡ }| j ¡ |_t|jƒ|jd< |S )r7   Úoriginal_orientationN)rF   r9   rG   r   r   rS   rH   r&   r'   r9   Û   s   
zHeifImage.to_pillowrm   c                 C   s   | j  |¡}t|ƒS )z‹Method to retrieve the auxiliary image at the given ID.

        :returns: a :py:class:`~pillow_heif.HeifAuxImage` class instance.
        N)r!   Úget_aux_imagerV   )r$   rm   Z	aux_imager&   r&   r'   r€   å   s   zHeifImage.get_aux_image)r<   r=   r>   r?   r(   rQ   rC   rj   rz   r{   Úsetterr   r9   r4   rV   r€   rU   r&   r&   rH   r'   rW   ˜   s    *
rW   c                   @   s4  e Zd ZdZd9dd„Zedd„ ƒZed	d
„ ƒZedd„ ƒZedd„ ƒZ	e	j
defdd„ƒZ	edd„ ƒZedd„ ƒZedd„ ƒZdejfdd„Zd:dd„Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&ed'eeef fd(d)„Zd*edefd+d,„Zd*ejdefd-d.„Zed/d0„ ƒZd1d2„ Zd3d4„ Z d5d6„ Z!d7d8„ Z"e!Z#dS );ÚHeifFileaÝ  Representation of the :py:class:`~pillow_heif.HeifImage` classes container.

    To create :py:class:`~pillow_heif.HeifFile` object, use the appropriate factory functions.

    * :py:func:`~pillow_heif.open_heif`
    * :py:func:`~pillow_heif.read_heif`
    * :py:func:`~pillow_heif.from_pillow`
    * :py:func:`~pillow_heif.from_bytes`

    Exceptions that can be raised when working with methods:
        `ValueError`, `EOFError`, `SyntaxError`, `RuntimeError`, `OSError`
    NTFc                 K   s  t |dƒr| dt¡ |d u rg }d}nKt|ƒ}t|ƒ}| d¡dkr+tj dd¡}n| d¡dks9| d¡dkrAtj d	d¡}nd}t	 
|tj||| d
d¡| dd¡| dtj¡|tj¡	}|| _dd„ |D ƒ| _d| _t| jƒD ]\}	}
|
j dd¡r€|	| _qrd S )NÚseekr   Ú Zavifrv   ÚAVIFZheicrd   ÚHEIFZremove_strideTZhdr_to_16bitÚreload_sizec                 S   r\   r   )rW   rX   r&   r&   r'   r[     r]   z%HeifFile.__init__.<locals>.<listcomp>r^   F)Úhasattrrƒ   r   r   r   Úfindr	   ZPREFERRED_DECODERrr   Ú_pillow_heifZ	load_fileZDECODE_THREADSZALLOW_INCORRECT_HEADERSZDISABLE_SECURITY_LIMITSÚmimetypeÚ_imagesÚprimary_indexÚ	enumeraterG   )r$   ÚfpÚconvert_hdr_to_8bitÚbgr_modeÚkwargsÚimagesr‹   Zfp_bytesZpreferred_decoderÚindexr:   r&   r&   r'   r(   ü   s>   


÷€þzHeifFile.__init__c                 C   ó   | j | j jS )z�:attr:`~pillow_heif.HeifImage.size` property of the primary :class:`~pillow_heif.HeifImage`.

        :exception IndexError: If there are no images.
        N)rŒ   r�   r   r*   r&   r&   r'   r     ó   zHeifFile.sizec                 C   r•   )z�:attr:`~pillow_heif.HeifImage.mode` property of the primary :class:`~pillow_heif.HeifImage`.

        :exception IndexError: If there are no images.
        N)rŒ   r�   r   r*   r&   r&   r'   r   &  r–   zHeifFile.modec                 C   r•   )z¢:attr:`~pillow_heif.HeifImage.has_alpha` property of the primary :class:`~pillow_heif.HeifImage`.

        :exception IndexError: If there are no images.
        N)rŒ   r�   rz   r*   r&   r&   r'   rz   .  r–   zHeifFile.has_alphac                 C   r•   )z¬:attr:`~pillow_heif.HeifImage.premultiplied_alpha` property of the primary :class:`~pillow_heif.HeifImage`.

        :exception IndexError: If there are no images.
        N©rŒ   r�   r{   r*   r&   r&   r'   r{   6  r–   zHeifFile.premultiplied_alphar|   c                 C   s   || j | j _d S r   r—   r~   r&   r&   r'   r{   >  s   c                 C   r•   )z�:attr:`~pillow_heif.HeifImage.data` property of the primary :class:`~pillow_heif.HeifImage`.

        :exception IndexError: If there are no images.
        N)rŒ   r�   r+   r*   r&   r&   r'   r+   B  r–   zHeifFile.datac                 C   r•   )zŸ:attr:`~pillow_heif.HeifImage.stride` property of the primary :class:`~pillow_heif.HeifImage`.

        :exception IndexError: If there are no images.
        N)rŒ   r�   r-   r*   r&   r&   r'   r-   J  r–   zHeifFile.stridec                 C   r•   )zŽ`info`` dict of the primary :class:`~pillow_heif.HeifImage` in the container.

        :exception IndexError: If there are no images.
        N)rŒ   r�   rG   r*   r&   r&   r'   rG   R  r–   zHeifFile.infor,   c                 C   s   | j | j  ¡ S )z­Helper method to create Pillow :external:py:class:`~PIL.Image.Image`.

        :returns: :external:py:class:`~PIL.Image.Image` class created from the primary image.
        N)rŒ   r�   r9   r*   r&   r&   r'   r9   Z  s   zHeifFile.to_pillowc                 K   s   t | j|fi |¤Ž dS )a*  Saves image(s) under the given fp.

        Keyword options can be used to provide additional instructions to the writer.
        If a writer does not recognize an option, it is silently ignored.

        Supported options:
            ``save_all`` - boolean. Should all images from ``HeiFile`` be saved?
            (default = ``True``)

            ``append_images`` - do the same as in Pillow. Accepts the list of ``HeifImage``

            .. note:: Appended images always will have ``info["primary"]=False``

            ``quality`` - see :py:attr:`~pillow_heif.options.QUALITY`

            ``enc_params`` - dictionary with key:value to pass to :ref:`x265 <hevc-encoder>` encoder.

            ``exif`` - override primary image's EXIF with specified.
            Accepts ``None``, ``bytes`` or ``PIL.Image.Exif`` class.

            ``xmp`` - override primary image's XMP with specified. Accepts ``None`` or ``bytes``.

            ``primary_index`` - ignore ``info["primary"]`` and set `PrimaryImage` by index.

            ``chroma`` - custom subsampling value. Possible values: ``444``, ``422`` or ``420`` (``x265`` default).

            ``subsampling`` - synonym for *chroma*. Format is string, compatible with Pillow: ``x:x:x``, e.g. '4:4:4'.

            ``save_nclx_profile`` - boolean, see :py:attr:`~pillow_heif.options.SAVE_NCLX_PROFILE`

            ``matrix_coefficients`` - int, nclx profile: color conversion matrix coefficients, default=6 (see h.273)

            ``color_primaries`` - int, nclx profile: color primaries (see h.273)

            ``transfer_characteristic`` - int, nclx profile: transfer characteristics (see h.273)

            ``full_range_flag`` - nclx profile: full range flag, default: 1

        :param fp: A filename (string), pathlib.Path object or an object with `write` method.
        N)Ú_encode_imagesrŒ   )r$   r�   r’   r&   r&   r'   Úsavea  s   )zHeifFile.savec                 C   s*   d| j j› dt| ƒ› ddd„ | D ƒ› d�S )NrL   ro   z	 images: c                 S   s   g | ]}t |ƒ‘qS r&   )rB   rX   r&   r&   r'   r[   �  s    z%HeifFile.__repr__.<locals>.<listcomp>rO   )rI   r<   rq   r*   r&   r&   r'   rQ   Œ  s   *zHeifFile.__repr__c                 C   s
   t | jƒS r   )rq   rŒ   r*   r&   r&   r'   Ú__len__�  s   
zHeifFile.__len__c                 c   s   � | j E d H  d S r   )rŒ   r*   r&   r&   r'   Ú__iter__’  s   €zHeifFile.__iter__c                 C   s.   |dk s|t | jƒkrtd|› �ƒ‚| j| S ©Nr   zinvalid image index: ©rq   rŒ   Ú
IndexError)r$   r”   r&   r&   r'   Ú__getitem__•  s   
zHeifFile.__getitem__c                 C   s0   |dk s|t | jƒkrtd|› �ƒ‚| j|= d S rœ   r�   )r$   Úkeyr&   r&   r'   Ú__delitem__š  s   zHeifFile.__delitem__r   r   c                 K   s(   t t|||fi |¤Žƒ}| j |¡ |S )aP  Adds image from bytes to container.

        .. note:: Supports ``stride`` value if needed.

        :param mode: see :ref:`image-modes`.
        :param size: tuple with ``width`` and ``height`` of image.
        :param data: bytes object with raw image data.

        :returns: :py:class:`~pillow_heif.HeifImage` added object.
        N)rW   r   rŒ   rk   )r$   r   r   r+   r’   Úadded_imager&   r&   r'   Úadd_frombytesŸ  s   zHeifFile.add_frombytesrT   c                 C   s@   |  ¡  | j|j|j|j|jd�}t|jƒ|_|j dd¡ |S )zµAdd image to the container.

        :param image: :py:class:`~pillow_heif.HeifImage` class to add from.

        :returns: :py:class:`~pillow_heif.HeifImage` added object.
        )r-   r^   N)	r)   r£   r   r   r+   r-   r   rG   Úpop)r$   rT   r¢   r&   r&   r'   Úadd_from_heif®  s   üzHeifFile.add_from_heifc           	      C   s
  |j d dks|j d dkrtdƒ‚|j ¡ }t|ƒ|d< t|ƒ}|r'||d< t|ƒ}t|ƒ}|dur<|dkr<t||ƒ}| 	¡  |  
|j|j | ¡ ¡}dD ]}||jv r\|j| |j|< qMdD ]}||jv rpt|j| ƒ|j|< q_t|ƒ|jd< t|ƒ}|rƒ||jd< |S )	z¿Add image to the container.

        :param image: Pillow :external:py:class:`~PIL.Image.Image` class to add from.

        :returns: :py:class:`~pillow_heif.HeifImage` added object.
        r   r   zEmpty images are not supported.r`   rc   N)r_   ra   rg   rh   re   )ri   rE   )r   Ú
ValueErrorrG   r   r   r   r   r   r   r)   r£   r   Útobytesr   )	r$   rT   rG   rc   r   Úimgr¢   r    Zim_xmpr&   r&   r'   Úadd_from_pillowÀ  s<   

ý
€
€
zHeifFile.add_from_pillowc                 C   r•   )z+Returns the primary image as a numpy array.N)rŒ   r�   r6   r*   r&   r&   r'   r6   ä  s   zHeifFile.__array_interface__c                 C   s@   g }| j D ]}t|jƒ}| |j|j||jg¡ q| j| j|gS r   )	rŒ   Úbytesr+   rk   r   r   rG   r�   r‹   )r$   Úim_descZimÚim_datar&   r&   r'   Ú__getstate__é  s
   

zHeifFile.__getstate__c           	      C   sD   |   ¡  |\| _| _}|D ]}|\}}}}|  |||¡}||_qd S r   )r(   r�   r‹   r£   rG   )	r$   Ústater“   r«   Zim_modeZim_sizer¬   Zim_infor¢   r&   r&   r'   Ú__setstate__ð  s   ýzHeifFile.__setstate__c                 C   s&   t ƒ }t| jƒ|_| j|_| j|_|S r   )r‚   r   rŒ   r‹   r�   )r$   Zim_copyr&   r&   r'   Z__copyø  s
   zHeifFile.__copyc                 C   s   | j | j  |¡S )z™`get_aux_image`` method of the primary :class:`~pillow_heif.HeifImage` in the container.

        :exception IndexError: If there are no images.
        N)rŒ   r�   r€   )r$   rm   r&   r&   r'   r€   ÿ  s   zHeifFile.get_aux_image)NTFr;   )$r<   r=   r>   r?   r(   rC   r   r   rz   r{   r�   rj   r+   r-   rG   r   r9   r™   rQ   rš   r›   rŸ   r¡   rB   r@   r4   r£   rW   r¥   r©   r6   r­   r¯   Z_HeifFile__copyr€   Ú__copy__r&   r&   r&   r'   r‚   î   sH    
"







+$
r‚   r,   c                 C   s*   t | dƒ}|dd… dkrdS t|ƒdkS )aV  Checks if the given `fp` object contains a supported file type.

    :param fp: A filename (string), pathlib.Path object or a file object.
        The file object must implement ``file.read``, ``file.seek``, and ``file.tell`` methods,
        and be opened in binary mode.

    :returns: A boolean indicating if the object can be opened.
    é   é   r.   s   ftypFr„   N)r   r   )r�   Zf_datar&   r&   r'   Úis_supported	  s   
	r³   TFc                 K   s   t | ||fi |¤ŽS )a«  Opens the given HEIF image file.

    :param fp: See parameter ``fp`` in :func:`is_supported`
    :param convert_hdr_to_8bit: Boolean indicating should 10 bit or 12 bit images
        be converted to 8-bit images during decoding. Otherwise, they will open in 16-bit mode.
        ``Does not affect "monochrome" or "depth images".``
    :param bgr_mode: Boolean indicating should be `RGB(A)` images be opened in `BGR(A)` mode.
    :param kwargs: **hdr_to_16bit** a boolean value indicating that 10/12-bit image data
        should be converted to 16-bit mode during decoding. `Has lower priority than convert_hdr_to_8bit`!
        Default = **True**

    :returns: :py:class:`~pillow_heif.HeifFile` object.
    :exception ValueError: invalid input data.
    :exception EOFError: corrupted image data.
    :exception SyntaxError: unsupported feature.
    :exception RuntimeError: some other error.
    :exception OSError: out of memory.
    N)r‚   )r�   r�   r‘   r’   r&   r&   r'   Ú	open_heif  s   r´   c                 K   s.   t | ||fddi|¤Ž}|D ]}| ¡  q|S )aV  Opens the given HEIF image file and decodes all images.

    .. note:: In most cases it is better to call :py:meth:`~pillow_heif.open_heif`, and
        let images decoded automatically only when needed.

    :param fp: See parameter ``fp`` in :func:`is_supported`
    :param convert_hdr_to_8bit: Boolean indicating should 10 bit or 12 bit images
        be converted to 8-bit images during decoding. Otherwise, they will open in 16-bit mode.
        ``Does not affect "monochrome" or "depth images".``
    :param bgr_mode: Boolean indicating should be `RGB(A)` images be opened in `BGR(A)` mode.
    :param kwargs: **hdr_to_16bit** a boolean value indicating that 10/12-bit image data
        should be converted to 16-bit mode during decoding. `Has lower priority than convert_hdr_to_8bit`!
        Default = **True**

    :returns: :py:class:`~pillow_heif.HeifFile` object.
    :exception ValueError: invalid input data.
    :exception EOFError: corrupted image data.
    :exception SyntaxError: unsupported feature.
    :exception RuntimeError: some other error.
    :exception OSError: out of memory.
    r‡   TN)r‚   r)   )r�   r�   r‘   r’   Úretr¨   r&   r&   r'   Ú	read_heif.  s   
r¶   r   r   c                 K   s,   t tt| ||fi |¤Žƒg|fi |¤Ž dS )aR  Encodes data in a ``fp``.

    :param mode: `BGR(A);16`, `RGB(A);16`, LA;16`, `L;16`, `I;16L`, `BGR(A)`, `RGB(A)`, `LA`, `L`
    :param size: tuple with ``width`` and ``height`` of an image.
    :param data: bytes object with raw image data.
    :param fp: A filename (string), pathlib.Path object or an object with ``write`` method.
    N)r˜   rW   r   )r   r   r+   r�   r’   r&   r&   r'   ÚencodeJ  s   ,r·   r“   c                 K   s$  |  dd¡}|dkrtjntj}t ¡ | std|› d�ƒ‚| |  dg ¡ }|  dd¡s2|d d	… }|s8td
ƒ‚t||  d¡ƒ}t	|fi |¤Ž}t
|ƒD ]>\}}	|	 ¡  |	j ¡ }
d|
d< ||krm|
jdi |¤Ž d|
d< |
 dd¡ |j|	j|	j|	jfdt|
ƒi|
¤d|	ji¤Ž qL| |¡ d S )NÚformatr†   r…   zNo z encoder found.Zappend_imagesZsave_allTr   z)Cannot write file with no images as HEIF.r�   Fr^   r-   r   Zimage_orientationr&   )rr   r
   ZAV1ZHEVCrŠ   Zget_lib_infoÚRuntimeErrorr¦   r   r   rŽ   r)   rG   r   Úupdater¤   Z	add_imager   r   r+   r   r-   r™   )r“   r�   r’   ÚcompressionZcompression_formatZimages_to_saver�   Z	ctx_writerZ   r¨   rG   r&   r&   r'   r˜   U  s>   
ýüû
úr˜   Ú	pil_imagec                 C   s   t ƒ }| | ¡ |S )zËCreates :py:class:`~pillow_heif.HeifFile` from a Pillow Image.

    :param pil_image: Pillow :external:py:class:`~PIL.Image.Image` class.

    :returns: New :py:class:`~pillow_heif.HeifFile` object.
    N)r‚   r©   )r¼   r:   r&   r&   r'   Úfrom_pillowt  s   
r½   c                 K   s    t ƒ }|j| ||fi |¤Ž |S )aJ  Creates :py:class:`~pillow_heif.HeifFile` from bytes.

    .. note:: Supports ``stride`` value if needed.

    :param mode: see :ref:`image-modes`.
    :param size: tuple with ``width`` and ``height`` of an image.
    :param data: bytes object with raw image data.

    :returns: New :py:class:`~pillow_heif.HeifFile` object.
    N)r‚   r£   )r   r   r+   r’   r:   r&   r&   r'   Ú
from_bytes€  s   r¾   )TF)6r?   r   r   Úior   Ú	threadingr   Útypingr   ZPILr   r„   r	   Ú	constantsr
   Úmiscr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rŠ   ÚImportErrorÚexZ_deffered_errorr   r   rD   rV   rW   r‚   rj   r³   r´   r¶   rB   r@   r4   r·   Úlistr˜   r½   r¾   r&   r&   r&   r'   Ú<module>   s<    H€ýRV  "