
    g5je]                        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 	 ddl Z  G d d      Z% G d de%      Z& G d de%      Z' G d de%      Z( G d d      Z)de*fdZ+d#de)fdZ,d#de)fdZ-de.de/e0e0f   ddfdZ1de2e(   ddfdZ3d e
j                  de)fd!Z4de.de/e0e0f   de)fd"Z5y# e!$ rZ"ddl#m$Z$  e$e"      Z Y dZ"["dZ"["ww xY w)$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                       e Zd ZU dZeeef   ed<   	 eed<   	 d Ze	d        Z
e	defd       Ze	d        Zdej                  fd	ZddZy
)	BaseImagez^Base class for :py:class:`HeifImage`, :py:class:`HeifDepthImage` and :py:class:`HeifAuxImage`.sizemodec                 p    |j                   \  | _        | _        || _        d | _        t               | _        y N)	size_moder   r    _c_image_datar   
_load_lock)selfc_images     B/var/www/html/qr/venv/lib/python3.12/site-packages/pi_heif/heif.py__init__zBaseImage.__init__3   s-    &00	49
&    c                 :    | j                          | j                  S )zaDecodes image and returns image data.

        :returns: ``bytes`` of the decoded image.
        )loadr%   r'   s    r)   datazBaseImage.data9   s     			zzr+   returnc                 N    | j                          | 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)   r2   zBaseImage.strideB   s     			}}###r+   c                 |   | j                          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kD  r|t        | j                     d   fz  }||d| j                  dS )	zNumpy array interface support.r   r	      z|u1   z<u2   )shapetypestrversionr/   )r-   intr2   r   r    r   r/   )r'   widthr8   r7   s       r)   __array_interface__zBaseImage.__array_interface__M   s     			DKK)DII"6q"99:TYY"a'G	NEG"&))A,!6TYY"Q&i		*1-//E7q$))TTr+   c                     | j                          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   r/   r2   r.   s    r)   	to_pillowzBaseImage.to_pillow\   sD    
 			IIIIIIIIKK
 	
r+   Nc                     | j                   s]| j                  5  | j                   s;| j                  j                  }| j                  j                  \  | _        }|| _         ddd       yy# 1 sw Y   yxY w)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&   r$   r/   r#   r   )r'   r/   _s      r)   r-   zBaseImage.loadk   sc     zz &zz==--D#'==#:#:LDIq!%DJ	& & & &s   AA++A4r0   N)__name__
__module____qualname____doc__tupler:   __annotations__strr*   propertyr/   r2   r<   r   rA   r-    r+   r)   r   r   '   s    h
S/(
IL
!   $ $ $ U U
5;; 
&r+   r   c                   L     e Zd ZdZ fdZd Zdej                  f fdZ xZS )HeifDepthImagez\Class representing the depth image associated with the :py:class:`~pi_heif.HeifImage` class.c                 z    t         |   |       |j                  }d|i| _        t	        || j                         y )Nmetadata)superr*   rQ   infor   )r'   r(   rQ   	__class__s      r)   r*   zHeifDepthImage.__init__|   s9    ! ))
	 	w		2r+   c           	          d| j                   j                   d| j                  d    d| j                  d    d| j                   d	S N< r   xr	   >rT   rE   r   r    r.   s    r)   __repr__zHeifDepthImage.__repr__   C    4>>**+1TYYq\N!DIIaL>499+UVWWr+   r0   c                 b    t         |          }| j                  j                         |_        |S )r>   )rR   rA   rS   r   r'   imagerT   s     r)   rA   zHeifDepthImage.to_pillow   s)    
 !#YY^^%
r+   )	rE   rF   rG   rH   r*   r\   r   rA   __classcell__rT   s   @r)   rO   rO   y   s&    f3X5;;  r+   rO   c                       e Zd ZdZd Zy)HeifAuxImagez`Class representing the auxiliary image associated with the :py:class:`~pi_heif.HeifImage` class.c           	          d| j                   j                   d| j                  d    d| j                  d    d| j                   d	S rV   r[   r.   s    r)   r\   zHeifAuxImage.__repr__   r]   r+   N)rE   rF   rG   rH   r\   rM   r+   r)   rd   rd      s    jXr+   rd   c                        e Zd ZdZ fdZd Zedefd       Zedefd       Z	e	j                  defd       Z	dej                  f fd	Zd
edefdZ xZS )	HeifImagez7One image in a :py:class:`~pi_heif.HeifFile` container.c                    t         |   |       |j                  }t        |      }t	        |      }t
        j                  r|j                  D cg c]  }||	 c}ng }t
        j                  r&|j                  D cg c]  }|t        |       c}ng }t        |j                        t        |j                        ||||d| _        t
        j                   rPi }|j"                  D ]0  }	|j%                  |	      }
|
|vrg ||
<   ||
   j'                  |	       2 || j                  d<   t)        |      }|r|| j                  d<   |r|| j                  d<   |j*                  }|r|| j                  d<   t-        || j                         |j.                  }|r?|d   dv r%|d   | j                  d	<   |d   | j                  d
<   y |d   | j                  d<   y y c c}w c c}w )N)primary	bit_depthexifrQ   
thumbnailsdepth_imagesauxxmpheifpixel_aspect_ratiotype)rICCprofr/   icc_profileicc_profile_typenclx_profile)rR   r*   rQ   r   r   r
   
THUMBNAILSrl   DEPTH_IMAGESdepth_image_listrO   boolri   r:   rj   rS   
AUX_IMAGESaux_image_idsget_aux_typeappendr   rq   r   color_profile)r'   r(   rQ   rk   ro   irl   rm   ctx_aux_infoaux_idaux_type	heif_metarq   r   rT   s                 r)   r*   zHeifImage.__init__   s   !&//h'H%[b[m[m73E3E'Wa'Wsu
V]VjVj(@(@R1AM^ARpr 	 GOO,W../ $(
	 L!// 6"//7</-/L*X&--f5	6
  ,DIIe"7+	"DIIe )DIIf$77.@DII*+w		2(/(=(=V$(88+8+@		-(0=f0E		,-,9&,A		.) = (XRs   GG G!G!c                 b   | 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                  j                  d
g              dS )Nz bytesnorW   rX   r   rY   r	    with z image data and rl   z thumbnails>)r%   
isinstancer$   r   lenr/   rT   rE   r   r    rS   get)r'   s_bytess     r)   r\   zHeifImage.__repr__   s    /3zzZW`=aS^$F+gk''($))A,q1a		{ S9,S|R1P-Q,RR^`	
r+   r0   c                 J    | j                   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(     yy3'*2.*<<r+   c                 ^    t        | j                  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   s-     DIIOOO,Q/3s:;;r+   valuec                 p    | j                   r*| j                  j                  |rdnd|rdnd      | _        y y )Nr   r   )r   r    replacer'   r   s     r)   r   zHeifImage.premultiplied_alpha   s/    >>		))#CSVWDI r+   c                     t         |          }| j                  j                         |_        t	        |j                        |j                  d<   |S )r>   original_orientation)rR   rA   rS   r   r   r_   s     r)   rA   zHeifImage.to_pillow   sA    
 !#YY^^%
-<UZZ-H

)*r+   r   c                 N    | j                   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_imagerd   )r'   r   	aux_images      r)   r   zHeifImage.get_aux_image   s#    
 MM//7	I&&r+   )rE   rF   rG   rH   r*   r\   rL   r{   r   r   setterr   rA   r:   rd   r   ra   rb   s   @r)   rg   rg      s    A(BT
 =4 = = <T < < X X  X5;; 'C 'L 'r+   rg   c                   r   e Zd ZdZd dZed        Zed        Zed        Zed        Z	e	j                  defd	       Z	ed
        Zed        Zed        Zdej                  fdZd!dZd Zd Zd Zd Zd Zdedeeef   fdZdedefdZdej                  defdZed        Zd Zd Z d Z!d Z"e!Z#y)"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`
    Nc                    t        |d      r|j                  dt               |g }d}nt        |      }t	        |      }|j                  d      dk7  r!t        j                  j                  dd      }nK|j                  d      dk7  s|j                  d      dk7  r!t        j                  j                  d	d      }nd}t        j                  |t        j                  |||j                  d
d      |j                  dd      |t        j                        }|| _        |D 	cg c]  }	|	t        |	       c}	| _        d| _        t#        | j                        D ])  \  }
}|j$                  j                  dd      s#|
| _        + y c c}	w )Nseekr    avifr   AVIFheicrp   HEIFremove_strideThdr_to_16bitri   F)hasattrr   r   r   r   findr
   PREFERRED_DECODERr   _pi_heif	load_fileDECODE_THREADSDISABLE_SECURITY_LIMITSmimetyperg   _imagesprimary_index	enumeraterS   )r'   fpconvert_hdr_to_8bitbgr_modekwargsimagesr   fp_bytespreferred_decoderr   indexrC   s               r)   r*   zHeifFile.__init__   sW   2vGGAx :FH!"~H(2H}}V$*$+$=$=$A$A&"$M!v&",f0E0K$+$=$=$A$A&"$M!$&!''&&#

?D1

>40!//	F !?E(W!1(W!$,,/ 	+HE1vvzz)U+%*"	+ )Xs   #F	+F	c                 H    | 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44r+   c                 H    | 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                 H    | 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99r+   c                 H    | 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_alpha5  s     ||D../CCCr+   r   c                 @    || j                   | j                     _        y r"   r   r   s     r)   r   zHeifFile.premultiplied_alpha=  s    ?DT''(<r+   c                 H    | 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   r/   r.   s    r)   r/   zHeifFile.dataA  r   r+   c                 H    | 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   r2   r.   s    r)   r2   zHeifFile.strideI  s     ||D../666r+   c                 H    | 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   rS   r.   s    r)   rS   zHeifFile.infoQ  r   r+   r0   c                 P    | 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   rA   r.   s    r)   rA   zHeifFile.to_pillowY  s"    
 ||D../99;;r+   c                 2    t        | j                  |fi | y)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'.

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

            ``matrix_coefficients`` - int, nclx profile: color conversion matrix coef., default=6 (BT.601, 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.save`  s    R 	t||R262r+   c                     d| j                   j                   dt        |        d| D cg c]  }t        |       c} dS c c}w )NrW   r   z	 images: rZ   )rT   rE   r   rK   )r'   r   s     r)   r\   zHeifFile.__repr__  sB    4>>**+6#d)IW[F\RSs1vF\E]]^__F\s   Ac                 ,    t        | j                        S r"   )r   r   r.   s    r)   __len__zHeifFile.__len__  s    4<<  r+   c              #   8   K   | j                   E d {    y 7 wr"   )r   r.   s    r)   __iter__zHeifFile.__iter__  s     <<s   c                 v    |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__  s;    19T\\!224UG<==||E""r+   c                 t    |dk  s|t        | j                        k\  rt        d|       | j                  |= y r   r   )r'   keys     r)   __delitem__zHeifFile.__delitem__  s8    7cS..4SE:;;LLr+   r    r   c                 j    t        t        |||fi |      }| j                  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.
        )rg   r   r   r   )r'   r    r   r/   r   added_images         r)   add_frombyteszHeifFile.add_frombytes  s4      	$d Ef EFK(r+   r`   c                    |j                          | j                  |j                  |j                  |j                  |j
                        }t        |j                        |_        |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.
        )r2   ri   N)	r-   r   r    r   r/   r2   r   rS   pop)r'   r`   r   s      r)   add_from_heifzHeifFile.add_from_heif  sm     	

((JJJJJJ<<	 ) 
 $EJJ/Y-r+   c                    |j                   d   dk  s|j                   d   dk  rt        d      |j                  j                         }t	        |      |d<   t        |      }|r||d<   t        |      }t        |      }||dk7  rt        ||      }|j                          | j                  |j                  |j                   |j                               }dD ]-  }||j                  v s|j                  |   |j                  |<   / dD ]6  }||j                  v st        |j                  |         |j                  |<   8 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.rk   ro   )rj   rl   ru   rv   rq   )rw   rQ   )r   
ValueErrorrS   r   r   r   r   r   r   r-   r   r    tobytesr   )	r'   r`   rS   ro   r   imgr   r   im_xmps	            r)   add_from_pillowzHeifFile.add_from_pillow  so    ::a=AA!!3>??zz (/Vu%DK.t4$U++0D0Ic#78C
((HHHHKKM

 h 	8Cejj (-

3  %	8 0 	BCejj (0C(A  %	B $5U#; !%(&,KU#r+   c                 H    | 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CCr+   c                     g }| j                   D ]J  }t        |j                        }|j                  |j                  |j
                  ||j                  g       L | j                  | j                  |gS r"   )	r   bytesr/   r   r    r   rS   r   r   )r'   im_descimim_datas       r)   __getstate__zHeifFile.__getstate__  sc    ,, 	ABBGGnGNNBGGRWWgrww?@	A ""DMM7;;r+   c                     | j                          |\  | _        | _        }|D ]#  }|\  }}}}| j                  |||      }||_        % y r"   )r*   r   r   r   rS   )	r'   stater   r   im_modeim_sizer   im_infor   s	            r)   __setstate__zHeifFile.__setstate__  sU    491DM6 	'G18.GWgw,,WgwGK&K	'r+   c                     t               }t        | j                        |_        | j                  |_        | j                  |_        |S r"   )r   r   r   r   r   )r'   im_copys     r)   __copyzHeifFile.__copy  s8    *t||,== $ 2 2r+   c                 R    | j                   | 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Er+   )NTFrD   )$rE   rF   rG   rH   r*   rL   r   r    r   r   r   r{   r/   r2   rS   r   rA   r   r\   r   r   r   r   rK   rI   r:   r   rg   r   r   r<   r   r   _HeifFile__copyr   __copy__rM   r+   r)   r   r      sg   +B 5 5 5 5 : : D D E E  E 5 5 7 7 5 5<5;; <)3V`! #

# U38_ 9  $"U[[ "Y "H D D<'F Hr+   r   r0   c                 H    t        | d      }|dd dk7  ryt        |      dk7  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.
          r4   s   ftypFr   )r   r   )r   f_datas     r)   is_supportedr     s1     BFa{gV$**r+   c                     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:`~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                 P    t        | ||fi |}|D ]  }|j                           |S )aN  Opens the given HEIF 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   r-   )r   r   r   r   retr   s         r)   	read_heifr  -  s5    , 2*H
?
?C 
Jr+   r    r   c           	      J    t        t        t        | ||fi |      g|fi | y)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   rg   r   )r    r   r/   r   r   s        r)   encoder  I  s)     IidDCFCDErTVTr+   r   c                    |j                  dd      }|dk(  rt        j                  nt        j                  }t	        j
                         |   st        d| d      | |j                  dg       z   }|j                  dd      s|d d	 }|st        d
      t        ||j                  d            }t        |fi |}t        |      D ]  \  }}	|	j                          |	j                  j                         }
d|
d<   ||k(  r |
j                  di | d|
d<   |
j                  dd        |j                   |	j"                  |	j$                  |	j&                  fdt)        |
      i|
d|	j*                  i  |j-                  |       y )Nformatr   r   zNo z encoder found.append_imagessave_allTr	   z)Cannot write file with no images as HEIF.r   Fri   r2   r   image_orientationrM   )r   r   AV1HEVCr   get_lib_infoRuntimeErrorr   r   r   r   r-   rS   r   updater   	add_imager   r    r/   r   r2   r   )r   r   r   compressioncompression_formatimages_to_saver   	ctx_writer   r   rS   s              r)   r   r   T  sz   **Xv.K6AV6K.22QfQkQk  ";/S_=>>&,vzz/2/N&NN::j$''+DEE&~vzz/7RSM,77IN+ 
3
xx}}YDKK!&!"DO1	HHHHHH	
 ;4@		

 	
 ::	

  NN2r+   	pil_imagec                 <    t               }|j                  |        |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  rC   s     r)   from_pillowr  s  s     	
Ai Hr+   c                 D    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   r/   r   rC   s        r)   
from_bytesr    s'     	
AAOOD$//Hr+   )TF)6rH   r   r   ior   	threadingr   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   rO   rd   rg   r   r{   r   r   r  rK   rI   r:   r  listr   r  r  rM   r+   r)   <module>r$     s=   >       ,    &!O& O&dY 0X9 XS'	 S'lW Wt+ +A A, 8U UE#s(O UD U4	? T >	5;; 	8 	S c3h H }  !.R H!s   C C:"C55C: