
    Ng\                       d Z ddlm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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 	 ddl Z n## e!$ rZ"ddl#m$Z$  e$e"          Z Y dZ"["ndZ"["ww xY w G d d          Z% G d de%          Z& G d de%          Z' G d de%          Z( G d d          Z)d+dZ*d,d-dZ+d,d-dZ,d.d#Z-d/d&Z.d0d)Z/d1d*Z0dS )2z8Functions and classes for heif images to read and write.    )annotations)copydeepcopy)SEEK_SET)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                      e Zd ZU dZded<   	 ded<   	 d Zed             Zedd
            Zed             Z	ddZ
ddZdS )	BaseImagez^Base class for :py:class:`HeifImage`, :py:class:`HeifDepthImage` and :py:class:`HeifAuxImage`.tuple[int, int]sizestrmodec                J    |j         \  | _        | _        || _        d | _        d S N)	size_moder    r"   _c_image_data)selfc_images     H/var/www/html/ai-engine/env/lib/python3.11/site-packages/pi_heif/heif.py__init__zBaseImage.__init__4   s%    &0	49


    c                8    |                                   | j        S )zaDecodes image and returns image data.

        :returns: ``bytes`` of the decoded image.
        )loadr'   r(   s    r*   datazBaseImage.data9   s     			zr,   returnintc                B    |                                   | j        j        S )zStride 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/   s    r*   r4   zBaseImage.strideB   s     			}##r,   c                   |                                   t          | j        t          | j                 d         z            }t          | j                 d         dk    rd}nt          |dz            }d}| j        d         |f}t          | j                 d         dk    r|t          | j                 d         fz  }||d| j        dS )	zNumpy array interface support.r   r	      z|u1   z<u2   )shapetypestrversionr0   )r.   r2   r4   r   r"   r    r0   )r(   widthr:   r9   s       r*   __array_interface__zBaseImage.__array_interface__M   s     			DK)DI"6q"99::TY"a''GG	NNEG"&)A,!6TY"Q&&i	*1-//E7q$)TTTr,   Image.Imagec                    |                                   t          j        | 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   	frombytesr"   r    r0   r4   r/   s    r*   	to_pillowzBaseImage.to_pillow\   sA    
 			IIIIK
 
 	
r,   Nonec                b    | j         s'| j        j        | _         | j        j        \  | _        }dS dS )zMethod 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&   r0   r%   r    )r(   _s     r*   r.   zBaseImage.loadk   s8     z 	3+DJ=2LDIqqq	3 	3r,   N)r1   r2   r1   r>   r1   rD   )__name__
__module____qualname____doc____annotations__r+   propertyr0   r4   r=   rC   r.    r,   r*   r   r   (   s         hh(IIIL
  
   X $ $ $ X$ U U XU
 
 
 
3 3 3 3 3 3r,   r   c                  4     e Zd ZdZ fdZd Zd fdZ xZS )HeifDepthImagez\Class representing the depth image associated with the :py:class:`~pi_heif.HeifImage` class.c                    t                                          |           |j        }d|i| _        t	          || j                   d S )Nmetadata)superr+   rS   infor   )r(   r)   	_metadata	__class__s      r*   r+   zHeifDepthImage.__init__y   sK    !!!!*		
	 	w	22222r,   c           	     j    d| j         j         d| j        d          d| j        d          d| j         d	S N< r   xr	   >rW   rI   r    r"   r/   s    r*   __repr__zHeifDepthImage.__repr__   >    W4>*WWTYq\WWDIaLWW49WWWWr,   r1   r>   c                    t                                                      }| j                                        |_        |S )r@   )rT   rC   rU   r   r(   imagerW   s     r*   rC   zHeifDepthImage.to_pillow   s2    
 !!##Y^^%%
r,   rG   )rI   rJ   rK   rL   r+   r_   rC   __classcell__rW   s   @r*   rQ   rQ   v   sn        ff3 3 3 3 3X X X         r,   rQ   c                      e Zd ZdZd ZdS )HeifAuxImagez`Class representing the auxiliary image associated with the :py:class:`~pi_heif.HeifImage` class.c           	     j    d| j         j         d| j        d          d| j        d          d| j         d	S rY   r^   r/   s    r*   r_   zHeifAuxImage.__repr__   r`   r,   N)rI   rJ   rK   rL   r_   rO   r,   r*   rg   rg      s.        jjX X X X Xr,   rg   c                       e Zd ZdZ fdZd Zedd            Zedd            Zej	        dd	            Zd fdZ
ddZ xZS )	HeifImagez7One image in a :py:class:`~pi_heif.HeifFile` container.c                0   t                                          |           |j        }t          |          }t	          |          }t
          j        rd |j        D             ng }t
          j        rd |j	        D             ng }t          |j                  t          |j                  ||||d| _        t
          j        rOi }|j        D ];}|                    |          }	|	|vrg ||	<   ||	                             |           <|| j        d<   t'          |          }
|r
|| j        d<   |
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                    g | ]}||S r$   rO   .0is     r*   
<listcomp>z&HeifImage.__init__.<locals>.<listcomp>   s    (X(X(Xq!----r,   c                0    g | ]}|t          |          S r$   )rQ   rm   s     r*   rp   z&HeifImage.__init__.<locals>.<listcomp>   s!    RRR1AM^AMMMr,   )primary	bit_depthexifrS   
thumbnailsdepth_imagesauxxmpheiftype)rICCprofr0   icc_profileicc_profile_typenclx_profile)rT   r+   rS   r   r   r
   
THUMBNAILSru   DEPTH_IMAGESdepth_image_listboolrr   r2   rs   rU   
AUX_IMAGESaux_image_idsget_aux_typeappendr   r   color_profile)r(   r)   rV   _exif_xmp_thumbnails_depth_images_ctx_aux_infoaux_idaux_type
_heif_meta_color_profilerW   s               r*   r+   zHeifImage.__init__   s   !!! ' 0	y))Y''\c\n(v(X(XG4F(X(X(X(XtvV]VjrRR(@RRRRpr 	 GO,,W.//!%)
 
	  	-M!/ 7 7"//77=00.0M(+h'..v6666,DIe#G,,
 	$#DIe 	+ *DIfw	222)0)> 	Cf%)999+9&+A	-(0>v0F	,---,:6,B	.)))	C 	Cr,   c                6   | 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norZ   r[   r   r\   r	    with z image data and ru   z thumbnails>)r'   
isinstancer&   r   lenr0   rW   rI   r    r"   rU   get)r(   _bytess     r*   r_   zHeifImage.__repr__   s    .2jjJt}V_<`<`jC	NN****fj_' _ _$)A, _ _1 _ _	 _ __ _,/	lB0O0O,P,P_ _ _	
r,   r1   r   c                T    | j                             d          d         d         dv S )zD``True`` for images with the ``alpha`` channel, ``False`` otherwise.;sepr   )Aa)r"   splitr/   s    r*   	has_alphazHeifImage.has_alpha   s)     y3''*2.*<<r,   c                r    t          | j                            d          d         d         dk              S )zN``True`` for images with ``premultiplied alpha`` channel, ``False`` otherwise.r   r   r   r   r   )r   r"   r   r/   s    r*   premultiplied_alphazHeifImage.premultiplied_alpha   s1     DIOOO,,Q/3s:;;;r,   valuec                h    | j         r*| j                            |rdnd|rdnd          | _        d S d S )Nr   r   )r   r"   replacer(   r   s     r*   r   zHeifImage.premultiplied_alpha   sI    > 	X	))*?##CAVSVWWDIII	X 	Xr,   r>   c                    t                                                      }| j                                        |_        t	          |j                  |j        d<   |S )r@   original_orientation)rT   rC   rU   r   r   rb   s     r*   rC   zHeifImage.to_pillow   sI    
 !!##Y^^%%
-<UZ-H-H
)*r,   r   r2   rg   c                T    | j                             |          }t          |          S )zMethod to retrieve the auxiliary image at the given ID.

        :returns: a :py:class:`~pi_heif.HeifAuxImage` class instance.
        )r&   get_aux_imagerg   )r(   r   	aux_images      r*   r   zHeifImage.get_aux_image   s'    
 M//77	I&&&r,   r1   r   r   r   rG   )r   r2   r1   rg   )rI   rJ   rK   rL   r+   r_   rN   r   r   setterrC   r   rd   re   s   @r*   rj   rj      s        AA%C %C %C %C %CN
 
 
 = = = X= < < < X< X X X  X     ' ' ' ' ' ' ' 'r,   rj   c                  N   e Zd ZdZd(dZed             Zed             Zed             Zed	             Z	e	j
        d)d            Z	ed             Zed             Zed             Zd*dZd+dZd Zd Zd Zd Zd Zd,dZd-d!Zd.d"Zed#             Zd$ Zd% Zd& Zd' ZeZdS )/HeifFilea  Representation of the :py:class:`~pi_heif.HeifImage` classes container.

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

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

    Exceptions that can be raised when working with methods:
        `ValueError`, `EOFError`, `SyntaxError`, `RuntimeError`, `OSError`
    NTFc                R   t          |d          r|                    dt                     |g }d}nt          |          }t	          |          }|                    d          dk    r!t          j                            dd          }nU|                    d          dk    s|                    d          dk    r!t          j                            d	d          }nd}t          j
        |t          j        |||                    d
d          |                    dd          |                    dt          j                  |          }|| _        d |D             | _        d| _        t!          | j                  D ]'\  }	}
|
j                            dd          r|	| _        (d S )Nseekr    avifr   AVIFheicry   HEIFremove_strideThdr_to_16bitreload_sizec                0    g | ]}|t          |          S r$   )rj   rm   s     r*   rp   z%HeifFile.__init__.<locals>.<listcomp>  s    (W(W(W!1r,   rr   F)hasattrr   r   r   r   findr
   PREFERRED_DECODERr   _pi_heif	load_fileDECODE_THREADSALLOW_INCORRECT_HEADERSmimetype_imagesprimary_index	enumeraterU   )r(   fpconvert_hdr_to_8bitbgr_modekwargsimagesr   fp_bytespreferred_decoderindexrF   s              r*   r+   zHeifFile.__init__   s   2v 	!GGAx   :FHH!"~~H(22H}}V$$**$+$=$A$A&"$M$M!!v&&",,f0E0E0K0K$+$=$A$A&"$M$M!!$&!'&#

?D11

>400

='*IJJ!	 	F !(W(Wv(W(W(W!$,// 	+ 	+HE1vzz)U++ +%*"	+ 	+r,   c                0    | j         | j                 j        S )z:attr:`~pi_heif.HeifImage.size` property of the primary :class:`~pi_heif.HeifImage`.

        :exception IndexError: If there are no images.
        )r   r   r    r/   s    r*   r    zHeifFile.size       |D./44r,   c                0    | j         | j                 j        S )z:attr:`~pi_heif.HeifImage.mode` property of the primary :class:`~pi_heif.HeifImage`.

        :exception IndexError: If there are no images.
        )r   r   r"   r/   s    r*   r"   zHeifFile.mode  r   r,   c                0    | j         | j                 j        S )z:attr:`~pi_heif.HeifImage.has_alpha` property of the primary :class:`~pi_heif.HeifImage`.

        :exception IndexError: If there are no images.
        )r   r   r   r/   s    r*   r   zHeifFile.has_alpha'  s     |D./99r,   c                0    | j         | j                 j        S )z:attr:`~pi_heif.HeifImage.premultiplied_alpha` property of the primary :class:`~pi_heif.HeifImage`.

        :exception IndexError: If there are no images.
        r   r   r   r/   s    r*   r   zHeifFile.premultiplied_alpha/  s     |D./CCr,   r   r   c                4    || j         | j                 _        d S r$   r   r   s     r*   r   zHeifFile.premultiplied_alpha7  s    ?DT'(<<<r,   c                0    | j         | j                 j        S )z:attr:`~pi_heif.HeifImage.data` property of the primary :class:`~pi_heif.HeifImage`.

        :exception IndexError: If there are no images.
        )r   r   r0   r/   s    r*   r0   zHeifFile.data;  r   r,   c                0    | j         | j                 j        S )z:attr:`~pi_heif.HeifImage.stride` property of the primary :class:`~pi_heif.HeifImage`.

        :exception IndexError: If there are no images.
        )r   r   r4   r/   s    r*   r4   zHeifFile.strideC  s     |D./66r,   c                0    | j         | j                 j        S )z`info`` dict of the primary :class:`~pi_heif.HeifImage` in the container.

        :exception IndexError: If there are no images.
        )r   r   rU   r/   s    r*   rU   zHeifFile.infoK  r   r,   r1   r>   c                J    | j         | j                                                 S )zHelper 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   rC   r/   s    r*   rC   zHeifFile.to_pillowS  s     
 |D./99;;;r,   rD   c                ,    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:`~pi_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:`~pi_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   s      r*   savezHeifFile.saveZ  s%    V 	t|R22622222r,   c                \    d| j         j         dt          |            dd | D              dS )NrZ   r   z	 images: c                ,    g | ]}t          |          S rO   )r!   rm   s     r*   rp   z%HeifFile.__repr__.<locals>.<listcomp>  s    F\F\F\RSs1vvF\F\F\r,   r]   )rW   rI   r   r/   s    r*   r_   zHeifFile.__repr__  s>    _4>*__#d))__F\F\W[F\F\F\____r,   c                *    t          | j                  S r$   )r   r   r/   s    r*   __len__zHeifFile.__len__  s    4<   r,   c              #  $   K   | j         E d {V  d S r$   )r   r/   s    r*   __iter__zHeifFile.__iter__  s&      <r,   c                |    |dk     s|t          | j                  k    rt          d|           | j        |         S Nr   zinvalid image index: r   r   
IndexError)r(   r   s     r*   __getitem__zHeifFile.__getitem__  sC    199T\!2!222<U<<===|E""r,   c                v    |dk     s|t          | j                  k    rt          d|           | j        |= d S r   r   )r(   keys     r*   __delitem__zHeifFile.__delitem__  sF    77cS....:S::;;;Lr,   r"   r!   r    r   c                r    t          t          |||fi |          }| j                            |           |S )aL  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:`~pi_heif.HeifImage` added object.
        )rj   r   r   r   )r(   r"   r    r0   r   added_images         r*   add_frombyteszHeifFile.add_frombytes  sA      	$d E Ef E EFFK(((r,   rc   rj   c                    |                                  |                     |j        |j        |j        |j                  }t          |j                  |_        |j                            dd           |S )zAdd image to the container.

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

        :returns: :py:class:`~pi_heif.HeifImage` added object.
        )r4   rr   N)	r.   r   r"   r    r0   r4   r   rU   pop)r(   rc   r   s      r*   add_from_heifzHeifFile.add_from_heif  sr     	

((JJJ<	 ) 
 
 $EJ//Y---r,   c                   |j         d         dk    s|j         d         dk    rt          d          |j                                        }t	          |          |d<   t          |          }|r||d<   t          |          }t          |          }||dk    rt          ||          }|	                                 | 
                    |j        |j         |                                          }dD ] }||j        v r|j        |         |j        |<   !dD ]-}||j        v r"t          |j        |                   |j        |<   .t	          |          |j        d<   t          |          }|r
||j        d<   |S )	zAdd image to the container.

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

        :returns: :py:class:`~pi_heif.HeifImage` added object.
        r   r	   zEmpty images are not supported.rt   rx   N)rs   ru   r}   r~   )r   rS   )r    
ValueErrorrU   r   r   r   r   r   r   r.   r   r"   tobytesr   )r(   rc   _infor   r   _imgr   r   s           r*   add_from_pillowzHeifFile.add_from_pillow  s    :a=AA!!3!3>???
!!)%00f&& 	 E%L.u55%e,,+0D0I0It%9::D		((IILLNN
 

 R 	8 	8Cej  (-
3 %/ 	B 	BCej  (0C(A(A %#4U#;#; && 	+&*KU#r,   c                0    | j         | j                 j        S )z+Returns the primary image as a numpy array.)r   r   r=   r/   s    r*   r=   zHeifFile.__array_interface__  s     |D./CCr,   c                    g }| j         D ]>}t          |j                  }|                    |j        |j        ||j        g           ?| j        | j        |gS r$   )	r   bytesr0   r   r"   r    rU   r   r   )r(   im_descimim_datas       r*   __getstate__zHeifFile.__getstate__  s]    , 	A 	ABBGnnGNNBGRWgrw?@@@@"DM7;;r,   c                    |                                   |\  | _        | _        }|D ]'}|\  }}}}|                     |||          }||_        (d S r$   )r+   r   r   r   rU   )	r(   stater   r   im_modeim_sizer   im_infor   s	            r*   __setstate__zHeifFile.__setstate__  si    491DM6 	' 	'G18.GWgw,,WgwGGK&K	' 	'r,   c                    t                      }t          | j                  |_        | j        |_        | j        |_        |S r$   )r   r   r   r   r   )r(   _im_copys     r*   __copyzHeifFile.__copy  s7    ::-- M!%!3r,   c                L    | j         | j                                     |          S )z`get_aux_image`` method of the primary :class:`~pi_heif.HeifImage` in the container.

        :exception IndexError: If there are no images.
        )r   r   r   )r(   r   s     r*   r   zHeifFile.get_aux_image  s"    
 |D./==fEEEr,   )NTFr   rG   rH   )r"   r!   r    r   )rc   rj   r1   rj   )rc   r>   r1   rj   )rI   rJ   rK   rL   r+   rN   r    r"   r   r   r   r0   r4   rU   rC   r   r_   r   r   r   r   r   r   r   r=   r   r  _HeifFile__copyr   __copy__rO   r,   r*   r   r      s-        + + + +B 5 5 X5 5 5 X5 : : X: D D XD E E E  E 5 5 X5 7 7 X7 5 5 X5< < < <+3 +3 +3 +3Z` ` `! ! !     # # #
  
      $" " " "H D D XD< < <' ' '  F F F HHHr,   r   r1   r   c                h    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.
          r6   s   ftypFr   )r   r   )r   __datas     r*   is_supportedr    s>     BFac{guV$$**r,   TFc                     t          | ||fi |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:`~pi_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   s       r*   	open_heifr    s    & B+X@@@@@r,   c                Z    t          | ||fddi|}|D ]}|                                 |S )aT  Opens the given HEIF(AVIF) image file and decodes all images.

    .. note:: In most cases it is better to call :py:meth:`~pi_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:`~pi_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imgs         r*   	read_heifr  )  sF    , 2*H
Q
Q$
Q&
Q
QC  



Jr,   r"   r!   r    r   rD   c           	     X    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   rj   r   )r"   r    r0   r   r   s        r*   encoder  E  sA     IidDCCFCCDDErTTVTTTTTr,   r   list[HeifImage]c                <   |                     dd          }|dk    rt          j        nt          j        }t	          j                    |         st          d| d          | |                     dg           z   }|                     dd          s
|d d	         }|st          d
          t          ||                     d                    }t          |fi |}t          |          D ]\  }}	|	                                 |	j                                        }
d|
d<   ||k    r |
j        di | d|
d<   |
                    dd            |j        |	j        |	j        |	j        fdt)          |
          i|
d|	j        i |                    |           d S )Nformatr   r   zNo z encoder found.append_imagessave_allTr	   z)Cannot write file with no images as HEIF.r   Frr   r4   r   image_orientationrO   )r   r   AV1HEVCr   get_lib_infoRuntimeErrorr   r   r   r   r.   rU   r   updater   	add_imager    r"   r0   r   r4   r   )r   r   r   compressioncompression_formatimages_to_saver   	ctx_writero   r  r   s              r*   r   r   P  s   **Xv..K6AV6K6K.22QfQk "";/ ?====>>>&,vzz/2/N/N&NN::j$'' ,'+ FDEEE&~vzz/7R7RSSM,7777IN++ 
 
3


 iEL""6"""#E)		(A	HHH	
 	
 ;5AA		

 	
 	
 :	
 	
 	
 	
 	
 NN2r,   	pil_imager>   c                L    t                      }|                    |            |S )zCreates :py:class:`~pi_heif.HeifFile` from a Pillow Image.

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

    :returns: New :py:class:`~pi_heif.HeifFile` object.
    )r   r   )r(  rF   s     r*   from_pillowr*  o  s&     	

Ai   Hr,   c                B    t                      } |j        | ||fi | |S )aB  Creates :py:class:`~pi_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:`~pi_heif.HeifFile` object.
    )r   r   )r"   r    r0   r   rF   s        r*   
from_bytesr,  {  s0     	

AAOD$/////Hr,   r   )TF)r1   r   )r"   r!   r    r   r1   rD   )r   r  r1   rD   )r(  r>   r1   r   )r"   r!   r    r   r1   r   )1rL   
__future__r   r   r   ior   typingr   PILr   r   r
   	constantsr   miscr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ImportErrorex_deffered_errorr   r   rQ   rg   rj   r   r  r  r  r  r   r*  r,  rO   r,   r*   <module>r6     s7   > > " " " " " "                                 , , , , , ,                                   &!OOOO ! ! !......}R  HHHHHH!K3 K3 K3 K3 K3 K3 K3 K3\    Y   0X X X X X9 X X XP' P' P' P' P'	 P' P' P'fY Y Y Y Y Y Y Yx+ + + +A A A A A,    8U U U U   >	 	 	 	     s   A A9A44A9