U
    :j0Y  ã                
   @   s¤  d Z ddlmZmZ ddlmZ ddlmZmZ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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ƒZ'G dd„ de'ƒZ(G dd„ de'ƒZ)G dd„ dƒZ*e+dœdd„Z,d(e*dœdd„Z-d)e*dœdd„Z.e/e0d
dœdd„Z1ee) d
d œd!d"„Z2eje*d#œd$d%„Z3e/e0e*dœd&d'„Z4d
S )*z8Functions and classes for heif images to read and write.é    )ÚcopyÚdeepcopy)ÚSEEK_SET)ÚAnyÚDictÚListÚOptionalÚTuple)Ú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                   @   sr   e Zd ZU dZeed< eed< dd„ Zedd„ ƒZ	ee
dœd	d
„ƒZedd„ ƒZejdœdd„Zddœdd„ZdS )Ú	BaseImagezDBase class for :py:class:`HeifImage` and :py:class:`HeifDepthImage`.ÚsizeÚmodec                 C   s   |j \| _| _|| _d | _d S ©N)Ú	size_moder    r!   Ú_c_imageÚ_data)ÚselfÚc_image© r(   ú4/tmp/pip-unpacked-wheel-8l127pjy/pillow_heif/heif.pyÚ__init__2   s    zBaseImage.__init__c                 C   s   |   ¡  | jS )zaDecodes image and returns image data.

        :returns: ``bytes`` of the decoded image.
        )Úloadr%   ©r&   r(   r(   r)   Údata7   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.
        )r+   r$   Ústrider,   r(   r(   r)   r0   @   s    zBaseImage.stridec                 C   sŒ   |   ¡  t| jt| j d  ƒ}t| j d dkr8d}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-   )r+   Úintr0   r   r!   r    r-   )r&   Úwidthr5   r4   r(   r(   r)   Ú__array_interface__K   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.
        Úraw)r+   r
   Z	frombytesr!   r    r-   r0   r,   r(   r(   r)   Ú	to_pillowZ   s    úzBaseImage.to_pillowNc                 C   s"   | j s| jj| _ | jj\| _}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+   i   s    
zBaseImage.load)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚtupleÚ__annotations__Ústrr*   Úpropertyr-   r7   r0   r9   r
   r<   r+   r(   r(   r(   r)   r   &   s   



r   c                       s<   e Zd ZdZ‡ fdd„Zdd„ Zejdœ‡ 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*   rG   Úinfor   )r&   r'   Ú	_metadata©Ú	__class__r(   r)   r*   w   s     ÿzHeifDepthImage.__init__c              	   C   sV   | j st| jtƒr"t| jƒ› d�nd}d| jj› d| jd › d| jd › d| j	› d�	S )	Nú bytesÚnoú<ú r   Úxr   ú>)
r%   Ú
isinstancer$   r   Úlenr-   rL   r>   r    r!   ©r&   Z_bytesr(   r(   r)   Ú__repr__   s    &zHeifDepthImage.__repr__r.   c                    s   t ƒ  ¡ }| j ¡ |_|S )r:   )rH   r<   rI   r   ©r&   ÚimagerK   r(   r)   r<   ƒ   s    
zHeifDepthImage.to_pillow)	r>   r?   r@   rA   r*   rV   r
   r<   Ú__classcell__r(   r(   rK   r)   rF   t   s   rF   c                       st   e Zd ZdZ‡ fdd„Zdd„ Zeedœdd„ƒZeedœd	d
„ƒZ	e	j
edœdd
„ƒZ	ejdœ‡ fdd„Z‡  ZS )Ú	HeifImagez;One image in a :py:class:`~pillow_heif.HeifFile` container.c           	         sì   t ƒ  |¡ |j}t|ƒ}t|ƒ}tjr8dd„ |jD ƒng }tjrRdd„ |j	D ƒng }t
|ƒ}t|jƒt|jƒ||||dœ| _|rŒ|| jd< |rš|| jd< t|| jƒ |j}|rè|d dkrÚ|d	 | jd
< |d | jd< n|d	 | jd< d S )Nc                 S   s   g | ]}|d k	r|‘qS r"   r(   ©Ú.0Úir(   r(   r)   Ú
<listcomp>–   s      z&HeifImage.__init__.<locals>.<listcomp>c                 S   s   g | ]}|d k	rt |ƒ‘qS r"   )rF   r[   r(   r(   r)   r^   ™   s      )ÚprimaryÚ	bit_depthÚexifrG   Ú
thumbnailsZdepth_imagesÚxmpÚheifÚtype)ZrICCZprofr-   Úicc_profileÚicc_profile_typeÚnclx_profile)rH   r*   rG   r   r   r   Z
THUMBNAILSrb   ZDEPTH_IMAGESZdepth_image_listr   Úboolr_   r7   r`   rI   r   Zcolor_profile)	r&   r'   rJ   Z_exifÚ_xmpZ_thumbnailsZ_depth_imagesZ
_heif_metaZ_color_profilerK   r(   r)   r*   �   s6    ÿÿú

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 )NrM   rN   rO   rP   r   rQ   r   ú with z image data and rb   z thumbnails>)r%   rS   r$   r   rT   r-   rL   r>   r    r!   rI   ÚgetrU   r(   r(   r)   rV   ±   s    &HÿzHeifImage.__repr__r.   c                 C   s   | j jdd�d d dkS )zD``True`` for images with the ``alpha`` channel, ``False`` otherwise.ú;©Úsepr   éÿÿÿÿ)ÚAÚa)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.rm   rn   r   rp   rr   )ri   r!   rs   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 )Nrq   rr   )rt   r!   Úreplace©r&   rw   r(   r(   r)   ru   Â   s    c                    s*   t ƒ  ¡ }| j ¡ |_t|jƒ|jd< |S )r:   Úoriginal_orientation)rH   r<   rI   r   r   rW   rK   r(   r)   r<   Ç   s    
zHeifImage.to_pillow)r>   r?   r@   rA   r*   rV   rE   ri   rt   ru   Úsetterr
   r<   rY   r(   r(   rK   r)   rZ   �   s   !rZ   c                   @   s"  e Zd ZdZd6dd„Zedd„ ƒZed	d
„ ƒZedd„ ƒZedd„ ƒZ	e	j
edœdd„ƒZ	edd„ ƒZedd„ ƒZedd„ ƒZejdœdd„Zddœdd„Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zeed&œd'd(„Zeed)œd*d+„Zejed)œd,d-„Zed.d/„ ƒZd0d1„ Zd2d3„ Zd4d5„ Z e Z!dS )7Ú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 kr(g }d}n’t|ƒ}t|ƒ}| d¡dkrVtj dd¡}n0| d¡dksr| d¡dkr‚tj d	d¡}nd}t	 
|tj||| d
d¡| dd¡| dtj¡|¡}|| _dd„ |D ƒ| _d| _t| jƒD ]\}	}
|
j dd¡rà|	| _qàd S )NÚseekr   Ú Zavifrp   ÚAVIFZheicrd   ÚHEIFZremove_strideTZhdr_to_16bitÚreload_sizec                 S   s   g | ]}|d k	rt |ƒ‘qS r"   )rZ   r[   r(   r(   r)   r^   û   s      z%HeifFile.__init__.<locals>.<listcomp>r_   F)Úhasattrr}   r   r   r   Úfindr   ZPREFERRED_DECODERrl   Ú_pillow_heifZ	load_fileZDECODE_THREADSZALLOW_INCORRECT_HEADERSÚmimetypeÚ_imagesÚprimary_indexÚ	enumeraterI   )r&   ÚfpÚconvert_hdr_to_8bitÚbgr_modeÚkwargsÚimagesr…   Zfp_bytesZpreferred_decoderÚindexr=   r(   r(   r)   r*   à   s8    


ø
zHeifFile.__init__c                 C   s   | 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.
        )r†   r‡   r    r,   r(   r(   r)   r      s    zHeifFile.sizec                 C   s   | j | j jS )z�:attr:`~pillow_heif.HeifImage.mode` property of the primary :class:`~pillow_heif.HeifImage`.

        :exception IndexError: If there are no images.
        )r†   r‡   r!   r,   r(   r(   r)   r!   	  s    zHeifFile.modec                 C   s   | j | j jS )z¢:attr:`~pillow_heif.HeifImage.has_alpha` property of the primary :class:`~pillow_heif.HeifImage`.

        :exception IndexError: If there are no images.
        )r†   r‡   rt   r,   r(   r(   r)   rt     s    zHeifFile.has_alphac                 C   s   | j | j jS )z¬:attr:`~pillow_heif.HeifImage.premultiplied_alpha` property of the primary :class:`~pillow_heif.HeifImage`.

        :exception IndexError: If there are no images.
        ©r†   r‡   ru   r,   r(   r(   r)   ru     s    zHeifFile.premultiplied_alpharv   c                 C   s   || j | j _d S r"   r�   ry   r(   r(   r)   ru   !  s    c                 C   s   | j | j jS )z�:attr:`~pillow_heif.HeifImage.data` property of the primary :class:`~pillow_heif.HeifImage`.

        :exception IndexError: If there are no images.
        )r†   r‡   r-   r,   r(   r(   r)   r-   %  s    zHeifFile.datac                 C   s   | j | j jS )zŸ:attr:`~pillow_heif.HeifImage.stride` property of the primary :class:`~pillow_heif.HeifImage`.

        :exception IndexError: If there are no images.
        )r†   r‡   r0   r,   r(   r(   r)   r0   -  s    zHeifFile.stridec                 C   s   | j | j jS )zŽ`info`` dict of the primary :class:`~pillow_heif.HeifImage` in the container.

        :exception IndexError: If there are no images.
        )r†   r‡   rI   r,   r(   r(   r)   rI   5  s    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.
        )r†   r‡   r<   r,   r(   r(   r)   r<   =  s    zHeifFile.to_pillowc                 K   s   t | j|f|Ž 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'.

            ``format`` - string with encoder format name. Possible values: ``HEIF`` (default) or ``AVIF``.

            ``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)   ÚsaveD  s    +zHeifFile.savec                 C   s*   d| j j› dt| ƒ› ddd„ | D ƒ› d�S )NrO   rk   z	 images: c                 S   s   g | ]}t |ƒ‘qS r(   )rD   r[   r(   r(   r)   r^   r  s     z%HeifFile.__repr__.<locals>.<listcomp>rR   )rL   r>   rT   r,   r(   r(   r)   rV   q  s    zHeifFile.__repr__c                 C   s
   t | jƒS r"   )rT   r†   r,   r(   r(   r)   Ú__len__t  s    zHeifFile.__len__c                 c   s   | j E d H  d S r"   )r†   r,   r(   r(   r)   Ú__iter__w  s    zHeifFile.__iter__c                 C   s.   |dk s|t | jƒkr$td|› �ƒ‚| j| S ©Nr   zinvalid image index: ©rT   r†   Ú
IndexError)r&   rŽ   r(   r(   r)   Ú__getitem__z  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|Žƒ}| 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.
        )rZ   r   r†   Úappend)r&   r!   r    r-   rŒ   Úadded_imager(   r(   r)   Úadd_frombytes„  s    zHeifFile.add_frombytes)rX   r/   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.
        )r0   r_   N)	r+   rœ   r!   r    r-   r0   r   rI   Úpop)r&   rX   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|ƒ}|rN||d< t|ƒ}t|ƒ}|dk	rx|dkrxt||ƒ}| 	¡  |  
|j|j | ¡ ¡}dD ]}||jkrš|j| |j|< qšdD ]"}||jkr¾t|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.ra   rc   N)r`   rb   rf   rg   )rh   rG   )r    Ú
ValueErrorrI   r   r   r   r   r   r   r+   rœ   r!   Útobytesr   )r&   rX   Ú_inforj   rz   Z_imgr›   r˜   r(   r(   r)   Úadd_from_pillow¥  s8    

ý


zHeifFile.add_from_pillowc                 C   s   | j | j jS )z+Returns the primary image as a numpy array.)r†   r‡   r9   r,   r(   r(   r)   r9   É  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-   rš   r!   r    rI   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œ   rI   )	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.__copy)NTF)"r>   r?   r@   rA   r*   rE   r    r!   rt   ru   r{   ri   r-   r0   rI   r
   r<   r‘   rV   r’   r“   r—   r™   rD   rB   rœ   rZ   rž   r¢   r9   r¦   r¨   Z_HeifFile__copyÚ__copy__r(   r(   r(   r)   r|   Ò   sD   
!






-$
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.
    é   é   r1   s   ftypFr~   )r   r   )r‰   Z__datar(   r(   r)   Úis_supportedç  s    	
r¬   TFc                 K   s   t | ||f|ŽS )a±  Opens the given HEIF(AVIF) 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.
    )r|   )r‰   rŠ   r‹   rŒ   r(   r(   r)   Ú	open_heifö  s    r­   c                 K   s.   t | ||fddi|—Ž}|D ]}| ¡  q|S )a\  Opens the given HEIF(AVIF) 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�   T)r|   r+   )r‰   rŠ   r‹   rŒ   ÚretÚimgr(   r(   r)   Ú	read_heif  s    
r°   )r!   r    r/   c                 K   s$   t tt| ||f|Žƒg|f|Ž 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�   rZ   r   )r!   r    r-   r‰   rŒ   r(   r(   r)   Úencode(  s    r±   )r�   r/   c                 K   s  |  dd¡}|dkrtjntj}t ¡ | s<td|› d�ƒ‚| |  dg ¡ }|  dd¡sd|d d	… }|sptd
ƒ‚t||  dd ¡ƒ}t	|f|Ž}t
|ƒD ]v\}}	|	 ¡  |	j ¡ }
d|
d< ||krÔ|
jf |Ž d|
d< |
 dd¡ |j|	j|	j|	jfdt|
ƒi|
d|	ji—Ž q–| |¡ 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_   r0   r   Zimage_orientation)rl   r   ZAV1ZHEVCr„   Zget_lib_infoÚRuntimeErrorrŸ   r   r   rˆ   r+   rI   r   Úupdater�   Z	add_imager    r!   r-   r   r0   r‘   )r�   r‰   rŒ   ÚcompressionZcompression_formatZimages_to_saver‡   Z	ctx_writer]   r¯   r¡   r(   r(   r)   r�   3  s>    
ýüûú
r�   )Ú	pil_imager/   c                 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.
    )r|   r¢   )r¶   r=   r(   r(   r)   Úfrom_pillowR  s    
r·   c                 K   s   t ƒ }|j| ||f|Ž |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.
    )r|   rœ   )r!   r    r-   rŒ   r=   r(   r(   r)   Ú
from_bytes^  s    r¸   )TF)TF)5rA   r   r   Úior   Útypingr   r   r   r   r	   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   rF   rZ   r|   ri   r¬   r­   r°   rD   rB   r±   r�   r·   r¸   r(   r(   r(   r)   Ú<module>   s2   HNE  