
    MAi                    p   S r SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
JrJr  SSKJr  SS	KJr  SS
KJr  SSKJrJrJrJrJrJrJrJr   SSKr " S S\R                  5      r! " S S\!5      r"SS jr#SS jr$SS jr%SS jr&S r'SS jr(SS jr)SS jr*g! \ a  rSSKJ r   \ " \5      r SrCN`SrCff = f)zPlugins for the Pillow library.    )annotations)chain)IO)warn)Image	ImageFileImageSequence   )options)HeifCompressionFormat)HeifFile)	CtxEncode_exif_from_pillow
_get_bytes_get_orientation_for_encoder_get_primary_index_pil_to_supported_mode_xmp_from_pillowset_orientationN)DeferredErrorc                     ^  \ rS rSr% SrSrS\S'   SrS\S'   U 4S	 jrS
 r	U 4S jr
SS jrSS jrSS jr\SS j5       r\SS j5       rSS jrSS jrSrU =r$ )_LibHeifImageFile!   z>Base class with all functionality for ``HeifImageFile`` class.NzHeifFile | None
_heif_fileTstr_modec                4   > SU l         [        TU ]  " U0 UD6  g )Nr   )_LibHeifImageFile__framesuper__init__)selfargskwargs	__class__s      M/var/www/html/land-ocr/venv/lib/python3.13/site-packages/pi_heif/as_plugin.pyr    _LibHeifImageFile.__init__(   s    $)&)    c                >    [        U R                  SSSS9nUR                  U l	        Xl
        UR                  U l        U R                  U R                  5        / U l        g ! [        [        [        [
        [        4 a  n[	        [        U5      5      S eS nAff = f)NTF)convert_hdr_to_8bithdr_to_16bitremove_stride)r   fpOSError
ValueErrorSyntaxErrorRuntimeErrorEOFErrorr   mimetypecustom_mimetyper   primary_indexr   _init_from_heif_filetile)r!   	heif_file	exceptions      r%   _open_LibHeifImageFile._open,   s    	8 dQUejkI  )11# ..!!$,,/	 [,I 	8c)n-47	8s   A# #BBBc                  > U R                   (       a  U R                   U R                  5          n UR                  nUR                  U l        U R                  5         U R                  USUR                  UR                  45        U R                  (       aG  [        U SS5      (       a5  [        U R                  S5      (       a  U R                  R                  5         S U l        U R                   (       d  S U l         ["        TU ]I  5       $ ! [         a)    [        R                  (       d  e U R                  5          Nf = f)Nraw_exclusive_fpFclose)r   telldatasize_sizeload_prepare	frombytesmodestrider1   r   LOAD_TRUNCATED_IMAGESr,   getattrhasattrr>   is_animatedr   load)r!   
frame_heifr@   r$   s      r%   rK   _LibHeifImageFile.load8   s    ??5J$!'__
!!#tUZ__j>O>O,PQ ww74%@@WTWWV]E^E^DG##"&w|~  $ 66!!#$s   AD 0D?>D?c                (   U R                  U5      (       d  g Xl        U R                  U5        [        R                  R                  U R                  U R                  5      U l        [        U SS 5      nUb  [        USS 5      (       a  SUl
        g g g )N_exif_loadedF)_seek_checkr   r5   r   corenewr   rB   imrH   rP   )r!   frameexifs      r%   seek_LibHeifImageFile.seekL   sz    &&!!%( **..TZZ8tWd+i > > DL !?r'   c                    U R                   $ N)r   r!   s    r%   r?   _LibHeifImageFile.tell\   s    ||r'   c                    g rZ    r[   s    r%   verify_LibHeifImageFile.verify_   s    r'   c                R    U R                   (       a  [        U R                   5      $ S$ )zRReturns the number of available frames.

:returns: Frame number, starting with 0.
r
   )r   lenr[   s    r%   n_frames_LibHeifImageFile.n_framesb   s     (,s4??#=A=r'   c                     U R                   S:  $ )zTReturns ``True`` if this image contains more than one frame, or ``False`` otherwise.r
   )rc   r[   s    r%   rJ   _LibHeifImageFile.is_animatedj   s     }}q  r'   c                h    US:  d  XR                   :  a  [        S5      eU R                  5       U:g  $ )Nr   z attempt to seek outside sequence)rc   r1   r?   )r!   rU   s     r%   rQ   _LibHeifImageFile._seek_checko   s/    19.=>>yy{e##r'   c                    U R                   (       a}  U R                   U   R                  U l        U R                   U   R                  U l        U R                   U   R
                  U l        [        U R
                  5      U R
                  S'   g g )Noriginal_orientation)r   rA   rB   rE   r   infor   )r!   	img_indexs     r%   r5   &_LibHeifImageFile._init_from_heif_filet   sh    ??388DJ388DJ	277DI0?		0JDII,-	 r'   )	__framer   r   rB   r3   r,   rT   rk   r6   )rU   int)returnro   rp   Nonerp   bool)rl   ro   rp   rr   )__name__
__module____qualname____firstlineno____doc__r   __annotations__!_close_exclusive_fp_after_loadingr    r9   rK   rW   r?   r_   propertyrc   rJ   rQ   r5   __static_attributes____classcell__)r$   s   @r%   r   r   !   sr    H"&J&(,%J*
(!  > > ! !$
K Kr'   r   c                       \ rS rSrSrSrSrSrg)HeifImageFile|   z1Pillow plugin class type for a HEIF image format.HEIFzHEIF containerr^   N)ru   rv   rw   rx   ry   formatformat_descriptionr}   r^   r'   r%   r   r   |   s    ;F)r'   r   c                >    [        U S5      nUSS S:w  a  gUSS S;   $ )N         s   ftypF)
s   heics   heixs   heims   heiss   hevcs   hevxs   hevms   hevss   mif1s   msf1)r   )r,   magics     r%   _is_supported_heifr      s1    r2EQqzW2;tttr'   c                8    [        X[        R                  5        g rZ   )
__save_oner   HEVCrT   r,   	_filenames      r%   
_save_heifr          r,112r'   c                8    [        X[        R                  5        g rZ   )
__save_allr   r   r   s      r%   _save_all_heifr      r   r'   c                    [        S0 U D6  [        R                  " [        R                  [        [
        5        [        R                  " 5       S   (       aR  [        R                  " [        R                  [        5        [        R                  " [        R                  [        5        / SQn[        R                  " [        R                  S5        [        R                  " [        R                  U5        g)zzRegisters a Pillow plugin for HEIF format.

:param kwargs: dictionary with values to set in options. See: :ref:`options`.
r   )z.heicz.heicsz.heifz.heifsz.hifz
image/heifNr^   )__options_updater   register_openr   r   r   _pi_heifget_lib_inforegister_saver   register_save_allr   register_mimeregister_extensions)r#   
extensionss     r%   register_heif_openerr      s    
 v	,,m=OPv&M00*= 4 4nE?J	,,l;	m22J?r'   c                    U R                  5        H  u  pUS:X  a  U[        l        M  US:X  a  U[        l        M+  US:X  a  U[        l        M>  US:X  a  U[        l        MQ  US:X  a  U[        l        Md  US:X  a  U[        l        Mw  US:X  a  U[        l        M  US:X  a  U[        l	        M  US	:X  a  U[        l
        M  US
:X  a  U[        l        M  [        SU 3SS9  M     g)zDInternal function to set options from `register_heif_opener` method.
thumbnailsdepth_images
aux_imagesqualitysave_to_12bitdecode_threadsallow_incorrect_headerssave_nclx_profilepreferred_encoderpreferred_decoderzUnknown option: r
   )
stacklevelN)itemsr   
THUMBNAILSDEPTH_IMAGES
AUX_IMAGESQUALITYSAVE_HDR_TO_12_BITDECODE_THREADSALLOW_INCORRECT_HEADERSSAVE_NCLX_PROFILEPREFERRED_ENCODERPREFERRED_DECODERr   )r#   kvs      r%   r   r      s    !"G. #$G ,!"G)^GO/!)*G&""%&G"++./G+%%()G%%%()G%%%()G%#A3'A6- r'   c                    [        U40 U R                  D6n[        X0S40 U R                  D6  UR                  U5        g )NT)r   encoderinfo_pil_encode_imagesave)rT   r,   compression_format	ctx_writes       r%   r   r      s5    ,??IiT<R^^<NN2r'   c                8   [        U40 U R                  D6n[        U S5      (       a  U R                  5       OS nU R                  R	                  S/ 5      n[        [        [        R                  " U 5      U5      U R                  R	                  SS 5      5      n[        [        [        R                  " U 5      U5      5       H  u  px[        X8Xv:H  40 U R                  D6  M      Ub"  [        U S5      (       a  U R                  U5        UR                  U5        g )Nr?   append_imagesr4   rW   )r   r   rI   r?   getr   r   r	   Iterator	enumerater   rW   r   )	rT   r,   r   r   current_framer   r4   irU   s	            r%   r   r      s    ,??I!(V!4!4BGGI$MNN&&;M&m$$R(-8"..:L:L_^b:cM eM$:$:2$>NO)A,>Q"..Q P WR%8%8
NN2r'   c                   UR                   S   S::  d  UR                   S   S::  a  [        S5      eUR                  R                  5       n[	        U5      US'   [        U5      US'   UR                  " S	0 UD6  X$S'   UR                  S:X  a  U R                  " U4S[        U5      0UD6  g [        U5      nU R                  " UR                   UR                  UR                  5       4S[        U5      0UD6  g )
Nr   r
   zEmpty images are not supported.rV   xmpprimaryYCbCrimage_orientationr^   )rA   r.   rk   copyr   r   updaterE   add_image_ycbcrr   r   	add_imagetobytes)ctximgr   r#   rk   s        r%   r   r      s    
xx{a388A;!+:;;88==?D$S)DL"3'DKKK&O
xx7C^3OPT3U^Y]^$S)chh#++-vKghlKmvquvr'   rs   )rT   Image.Imager,   	IO[bytes]r   zstr | bytesrq   )rT   r   r,   r   r   r   )r   r   r   r   r   rt   rp   rr   )+ry   
__future__r   	itertoolsr   typingr   warningsr   PILr   r   r	    r   	constantsr   heifr   miscr   r   r   r   r   r   r   r   r   ImportErrorex_deffered_errorr   r   r   r   r   r   r   r   r   r   r   r^   r'   r%   <module>r      s    % "    / /  , 	 	 	!XK	++ XKv*% *u33@76wk  !.R H!s   B B5B00B5