
    MAiWN                    
   S r SSKJr  SSKrSSK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  SS	KJr  S
SKJr  S
SKJrJrJrJr   SSKr0 SSS\R<                  \R>                  4_SSS\R<                  \R>                  4_SSS\R<                  \R@                  4_SSS\R<                  \R>                  4_SSS\R<                  \R>                  4_SSS\R<                  \R@                  4_SSS\RB                  \RB                  4_SSS\RB                  \RB                  4_SS
S\RB                  \RB                  4_SS
S\RB                  \RB                  4_SS
S\RB                  \RB                  4_SSS\R<                  \R>                  4_SSS\R<                  \R>                  4_S SS\R<                  \R@                  4_S!SS\R<                  \R>                  4_S"SS\R<                  \R>                  4_S#SS\R<                  \R@                  4_0 S$SS\RB                  \RB                  4_S%SS\RB                  \RB                  4_S&S
S\RB                  \RB                  4_S'S
S\RB                  \RB                  4_S(S
S\RB                  \RB                  4_S)SS*\R<                  \R>                  4_S+SS*\R<                  \R>                  4_S,SS*\R<                  \R@                  4_S-SS*\R<                  \R>                  4_S.SS*\R<                  \R>                  4_S/SS*\R<                  \R@                  4_S0SS*\RB                  \RB                  4_S1SS*\RB                  \RB                  4_S2S
S*\RB                  \RB                  4_S3S
S*\RB                  \RB                  4_S4S
S*\RB                  \RB                  4_S5SS6\R<                  \RD                  4_ESS6\R<                  \RD                  4SS6\R<                  \RF                  4SS6\R<                  \RD                  4SS6\R<                  \RD                  4SS6\R<                  \RF                  4SS6\RB                  \RB                  4SS6\RB                  \RB                  4S
S6\RB                  \RB                  4SS6\RH                  \RJ                  4S7.	Er&S8S9S:S;.r'S:S9S8S<.r(STS= jr)SUS> jr*SVS? jr+SWSXS@ jjr,SWSYSA jjr-SZSB jr.S[S\SC jjr/S]SD jr0S]SE jr1S^SF jr2S^SG jr3S_SH jr4 " SI SJ\5      r5S`SK jr6SaSL jr7SbSM jr8ScSN jr9 " SO SP5      r:\ " SQ SR5      5       r;SdSS jr<g! \ a  rS
SKJr  \" \5      r SrCGN$SrCff = f)ezoDifferent miscellaneous helper functions.

Mostly for internal use, so prototypes can change between versions.
    )annotationsN)	dataclass)IntEnum)ceil)Path)packunpack)Image   )options)HeifChannel
HeifChromaHeifColorspaceHeifCompressionFormat)DeferredErrorzBGRA;16      zBGRa;16zBGR;16   zRGBA;16zRGBa;16zRGB;16zLA;16   zLa;16zL;16zI;16I;16LzBGRA;12   zBGRa;12zBGR;12zRGBA;12zRGBa;12zRGB;12zLA;12zLa;12zL;12zI;12zI;12LzBGRA;10
   zBGRa;10zBGR;10zRGBA;10zRGBa;10zRGB;10zLA;10zLa;10zL;10zI;10zI;10LRGBA   )	RGBaRGBBGRABGRaBGRLALaLYCbCri  i  i  )z4:4:4z4:2:2z4:2:0)r   r   r   c                R    [         R                  U R                  5      nUb  X!S'   gg)zYConverts `chroma` value from `c_image` to useful values and stores them in ``info`` dict.Nchroma)LIBHEIF_CHROMA_MAPgetr%   )c_imageinfor%   s      H/var/www/html/land-ocr/venv/lib/python3.13/site-packages/pi_heif/misc.pysave_colorspace_chromar+   Y   s*      ##GNN3FX     c                    [        U S5      $ )a  Reset orientation in ``EXIF`` to ``1`` if any orientation present.

Removes ``XMP`` orientation tag if it is present.
In Pillow plugin mode, it is called automatically for images.
When ``pi_heif`` used in ``standalone`` mode, if you wish, you can call it manually.

.. note:: If there is no orientation tag, this function will not add it and do nothing.

    If both XMP and EXIF orientation tags are present, EXIF orientation tag will be returned,
    but both tags will be removed.

:param info: `info` dictionary from :external:py:class:`~PIL.Image.Image` or :py:class:`~pi_heif.HeifImage`.
:returns: Original orientation or None if it is absent.
T_get_orientation)r)   s    r*   set_orientationr0   a   s     D$''r,   c                (    [        U S5      nUc  S$ U$ )NFr   r.   )r)   image_orientations     r*   _get_orientation_for_encoderr3   s   s     (u5!)1@/@@r,   c                D   SnU R                  S5      (       a  U S   R                  SS5      nUS   (       a  S nS H  n US   R                  U5      n  O   U(       a  [        R
                  " SU5      nU(       aE  [        US   5      nU(       a0  [        R                  " SS	U5      n[        R                  " S
S	U5      nWS:w  d  US:w  a6  SR                  UR                  S5      [        U5      S:  a  SOS/5      U S'   Uc  US:w  a  U$ S $ ! [         a     M  f = f)Nr   xmp    r   )utf-8latin1ztiff:Orientation(="|>)([0-9])r   ztiff:Orientation="([0-9])" z,<tiff:Orientation>([0-9])</tiff:Orientation>r7   r,   )r'   rsplitdecode	Exceptionresearchintsubjoinencodelen)r)   exif_orientationresetxmp_orientationxmp_datadecoded_xmp_dataencodingmatchs           r*   _get_orientation_xmprK   x   s$   Oxx;%%gq1A;#/'/{'9'9('C$ 0  		"BDTU&)%(mO+-662OQSUe+f(+-662acegw+x(w&/Q*>"%((,<,C,CG,LY\]eYfijYjgps+t"uDK.6?a;O?YUYY ! s    D
DDc                   S nU R                  S5      (       Ga   U S   nSnUR                  S5      (       a  SnUSS  nUSS S:X  a  S	OS
n[        US-   USS 5      S   n[        US-   X6US-    5      S   nUS-   n[        U5       H  n	USU	-  -   n[        US-   X6US-    5      S   S:w  a  M(  X6S-   US-    n
[        US-   U
SS 5      S   nUS:w  d  MO  UnU(       d    O?US-   nU(       a  US-  n[	        US-   S5      nU S   S U U-   U S   US-   S  -   U S'     O    [        XUS9nU=(       d    U$ ! [
         a     N!f = f)NexifF   Exif  T   r   r   s   II<>r"   r   r   Hr   i  r   )rE   )r'   
startswithr	   ranger   r<   rK   )r)   rE   original_orientationtif_tagskipped_exif00endian_markpointer	tag_countoffsettag_nvaluet_original_orientationp_valuenew_orientationrF   s                  r*   r/   r/      s   xx	6lG"N!!/22!%!!"+!(1!<##K[3.!=a@G{S0'GaK2PQRSTIq[Fy) 2:-++Ww{-KLQOSVV!gl;)/c0A51:)Nq)Q&)Q.+A( %kG%1&*;+<a&@O#'<#9O#KdSYl[bef[f[hNi#iDL *$ +4USO222  		s$   B8D6 
D6 ";D6 D6 6
EEc                    [        U S5      SS nU(       a*  US:X  a  gUS:X  a  gUS;   a  g	US
;   a  gUS:X  a  gUS:X  a  gg)a|  Gets the MIME type of the HEIF(or AVIF) object.

:param fp: A filename (string), pathlib.Path object, file object or bytes.
    The file object must implement ``file.read``, ``file.seek`` and ``file.tell`` methods,
    and be opened in binary mode.
:returns: "image/heic", "image/heif", "image/heic-sequence", "image/heif-sequence",
    "image/avif", "image/avif-sequence" or "".
r   r   Ns   avifz
image/avifs   aviszimage/avif-sequence)s   heics   heixs   heims   heisz
image/heic)s   hevcs   hevxs   hevms   hevszimage/heic-sequences   mif1z
image/heifs   msf1zimage/heif-sequencer9   )
_get_bytes)fp
heif_brands     r*   get_file_mimetypere      sa     B#AB'J  (====(  (r,   c                   [        U [        [        45      (       a;  [        R                  " U S5       nUR                  U=(       d    S5      sS S S 5        $ [        U S5      (       ad  [        U S5      (       a  U R                  5       OS nU R                  U=(       d    S5      nUb"  [        U S5      (       a  U R                  U5        U$ [        U 5      S U $ ! , (       d  f       N= f)Nrbreadtellseek)

isinstancestrr   builtinsopenri   hasattrrj   rk   bytes)rc   lengthfiler[   results        r*   rb   rb      s    "sDk""]]2t$99V\r* %$r6%b&11t2&'"f"5"5GGFO9Wf %$s   C
C(c                r   S n/ n[        U 5       H  u  p4US   S:X  d  M  UR                  U5        [        R                  US   S S SSS9nUS-  n[	        US   5      U-
  S::  a  SnOUS:  a  US   US-
  U S	:X  a  US-  nUS   US  nU(       a  M  U(       d  M  UnM     [        U5       H  nX	 M     U$ )
NtypeExifdatar   bigF)	byteordersignedrO   rN   )	enumerateappendr?   
from_bytesrC   reversed)metadatart   purgeimd_block	skip_sizerx   s          r*   _retrieve_exifr      s    FE *Fv%LLOx'7';uUZ[INI8F#$y0A5	aHV$4Y]Y$OSb$bQ	F#IJ/D6dd + e_K Mr,   c                    S n/ n[        U 5       H/  u  p4US   S:X  d  M  UR                  U5        U(       a  M*  US   nM1     [        U5       H  nX	 M     U$ )Nrv   mimerx   )r|   r}   r   )r   rt   r   r   r   s        r*   _retrieve_xmpr      s^    FE *Fv%LLO6!&)	 +
 e_K Mr,   c                    SU R                   ;   a  U R                   S   $ [        U S5      (       a'  U R                  5       nU(       a  UR                  5       $ g )NrM   getexif)r)   rp   r   tobytes)imgrM   s     r*   _exif_from_pillowr      sG    xxsI{{}<<>!r,   c                    S nSU R                   ;   a  U R                   S   nOSU R                   ;   a  U R                   S   n[        U[        5      (       a  UR                  S5      nU$ )Nr5   zXML:com.adobe.xmpr7   )r)   rl   rm   rB   )r   im_xmps     r*   _xmp_from_pillowr     s[    F%		(-.&#w'Mr,   c                   U R                   S:X  a2  U R                  R                  SS 5      b  SOSnU R                  US9n U $ U R                   S:X  a  U R                  SS9n U $ U R                   S:X  a  U R                  S	S9n U $ U R                   S
:X  a  U R                  SS9n U $ U R                   S:X  a  U R                  SS9n U $ )NPtransparencyr   r   )modeIr   1r"   CMYKr#   )r   r)   r'   convert)r   r   s     r*   _pil_to_supported_moder     s    
xx3nd;GvUkktk$ J 
Skkwk' J 
Skksk#
 J	 
V	kkvk& J 
W	kkuk%Jr,   c                  4    \ rS rSrSrSrSrSrSrSr	Sr
S	rS
rg)	Transposei!  zkTemporary workaround till we support old Pillows, remove this when a minimum Pillow version will have this.r   r   r   r   r      rO    N)__name__
__module____qualname____firstlineno____doc__FLIP_LEFT_RIGHTFLIP_TOP_BOTTOM	ROTATE_90
ROTATE_180
ROTATE_270	TRANSPOSE
TRANSVERSE__static_attributes__r   r,   r*   r   r   !  s(    uOOIJJIJr,   r   c                $   [         R                  [         R                  [         R                  [         R                  [         R
                  [         R                  [         R                  S.R                  U5      nUb  U R                  U5      $ U $ )N)r   r   r   r   rO      r   )
r   r   r   r   r   r   r   r   r'   	transpose)r   orientationmethods      r*   _rotate_pilr   -  su     $$$$ 
c+  }}V$$Jr,   c                    U  Vs/ s H  o"R                   R                  SS5      PM      nnUc#  Sn[        U5       H  u  pEU(       d  M  UnM     U$ US:X  d  U[        U5      :  a  [        U5      S-
  nU$ s  snf )NprimaryFr   rh   r   )r)   r'   r|   rC   )some_iteratorprimary_index_primary_attrsr   vs         r*   _get_primary_indexr   >  s    ;HI=aVVZZ	51=MIm,DAq ! -
  
"	]1C CM*Q. Js   %A7c                B    U (       a  U S   U S   U S   U S   U S   S.$ S $ )Nr   r   r   r   r   )focal_length_xfocal_length_yprincipal_point_xprincipal_point_yskewr   )valuess    r*   __get_camera_intrinsic_matrixr   J  s@      %Qi$Qi!'!'1I	

 
r,   c                x    0 n[        U R                  5      nU(       a  X!S'   U R                  nU(       a  X1S'   U$ )Ncamera_intrinsic_matrixcamera_extrinsic_matrix_rot)r   r   r   )r(   rr   r   s       r*   _get_heif_metar   X  sA    
A;G<[<[\'>
#$")"E"E"+F
'(Hr,   c                  J    \ rS rSrSrS
S jrSS jrSS jrSS jrSS jr	Sr
g	)	CtxEncodeic  z0Encoder bindings from python to python C module.c                <   UR                  S[        R                  5      n[        R                  " UUc  SOU[        R
                  R                  U[        R                  :X  a  SOSS5      5      U l        UR                  S0 5      nS nSU;   a  [        R                  US   5      nUc  UR                  S5      nU(       a  XTS'   UR                  5        HA  u  pgU R                  R                  U[        U[        5      (       a  UO
[        U5      5        MC     g )	NqualityHEIFAVIFr9   
enc_paramssubsamplingr%   )r'   r   QUALITY_pi_heifCtxWritePREFERRED_ENCODERr   HEVC	ctx_writeSUBSAMPLING_CHROMA_MAPitemsset_parameterrl   rm   )selfcompression_formatkwargsr   r   r%   keyr]   s           r*   __init__CtxEncode.__init__f  s    **Y8!**/Bw%%))4FJ_JdJd4d&jprtu

 ZZb1
F"+//}0EFF>ZZ)F#)x $**,JCNN((z%7M7MeSVW\S]^ -r,   c                   US   S::  d	  US   S::  a  [        S5      e[        U   S   nUS:X  a  SOUR                  SS5      nUS:X  a  [        R                  (       a  SOSn[        UR                  S	S
9S   S   S:H  5      nU R                  R                  U[        U   S   [        U   S   U5      n[        U   S   S:X  a3  UR                  XXSUR                  SS5      [        R                  5        Oj[        U   S   S:X  a$  UR                  XXSUR                  SS5      5        O6UR                  XXSUR                  S5      S:g  UR                  SS5      5        U R                  " X40 UD6  g)zAdds image to the encoder.r   r   zEmpty images are not supported.r   	bit_depthr   r   r   ;)seprh   ar   r   strider   N)
ValueError	MODE_INFOr'   r   SAVE_HDR_TO_12_BITr?   splitr   create_imageadd_plane_lr   	CHANNEL_Yadd_plane_la	add_planefind_finish_add_image)	r   sizer   rx   r   bit_depth_inbit_depth_outpremultiplied_alphaim_outs	            r*   	add_imageCtxEncode.add_imagex  se   7a<47a<>?? q))Q.FJJ{B4OB")"<"<B"M!$***"5a"8"<"CD,,T9T?13EyQUWXGY[noT?1"tL

S[]^H_alavavwt_Q1$\T\^_I`aT,diiPUFVZ\F\^d^h^hiqst^uvv6v6r,   c                   U R                   R                  UR                  [        UR                     S   [        UR                     S   S5      n[
        R                  [
        R                  [
        R                  4 HK  nUR                  UR                  SS[        UR                  U5      5      UR                  SS5      U5        MM     U R                  " X1R                  40 UD6  g)z*Adds image in `YCbCR` mode to the encoder.r   r   r   r   r   N)r   r   r   r   r   r   r   
CHANNEL_CB
CHANNEL_CRr   rq   getdatar'   r   )r   r   r   r   r   s        r*   add_image_ycbcrCtxEncode.add_image_ycbcr  s     ,,SXXy7J17MyY\YaYaObcdOeghi'')?)?AWAWXAsxxAuS[[^/DfjjQY[\F]_`a Yvxx:6:r,   c                v   UR                  S5      nUb"  UR                  UR                  SS5      U5        UR                  S5      (       a&  UR                  " S Vs/ s H  nUS   U   PM     sn6   UR                  SS5      nUR                  U R                  UR                  SS	5      UR                  S
[
        R                  5      UR                  SS5      UR                  SS5      UR                  SS5      UR                  SS5      U5        UR                  S5      nUbK  [        U[        R                  5      (       a  UR                  5       nUR                  U R                  U5        UR                  S5      nUb  UR                  U R                  U5        UR                  S/ 5       H*  n	UR                  U R                  U	S   U	S   U	S   5        M,     UR                  S/ 5       H;  n
[        U5      U
s=:  a  S:  d  M  O  M  UR                  U R                  X5        M=     g s  snf )Nicc_profileicc_profile_typeprofnclx_profile)color_primariestransfer_characteristicsmatrix_coefficientsfull_range_flagr2   r   r   Fsave_nclx_profiler  rh   r  r  r  rM   r5   r   rv   content_typerx   
thumbnailsr   )r'   set_icc_profileset_nclx_profilerB   r   r   SAVE_NCLX_PROFILErl   r
   rw   r   set_exifset_xmpset_metadatamaxencode_thumbnail)r   r   r   r   r   r   r2   rM   r5   r   	thumb_boxs              r*   r   CtxEncode._finish_add_image  s   jj/"""6::.@&#I;W::n%%## wv >*1-v #JJ':A>NNJJy%(JJ*G,E,EFJJ("-JJ126JJ,b1JJ("-		
 zz&!$

++||~OODNND1jj?NN4>>3/

:r2H0@(>BZ\dek\lm 3  L"5I4y9(q((''	U 6;s   H6c                    U R                   R                  5       n[        U[        [        45      (       a  [	        U5      R                  U5        g[        US5      (       a  UR                  U5        g[        S5      e)z?Ask encoder to produce output based on previously added images.writez=`fp` must be a path to file or an object with `write` method.N)	r   finalizerl   rm   r   write_bytesrp   r  	TypeError)r   rc   rx   s      r*   saveCtxEncode.save  s[    ~~&&(b3+&&H  &R!!HHTN[\\r,   )r   N)r   r   )r   tuple[int, int]r   rm   returnNone)r   Image.Imager  r  )r   r  )r  r  )r   r   r   r   r   r   r   r   r   r  r   r   r,   r*   r   r   c  s"    :_$7(;'VR]r,   r   c                  F    \ rS rSrSrSS jr\S 5       r\S	S j5       rSr	g)
	MimCImagei  zMimicry of the HeifImage class.c                   Xl         X l        UR                  SUS   [        U   S   -  [	        [        U   S   S-  5      -  5      U l        X0l        / U l        S U l        / U l	        / U l
        / U l        SU l        [        R                  R                  U l        ["        R                  R                  U l        S U l        S U l        g )Nr   r   r   r   F)r   r   r'   r   r   r   rx   r   color_profiler  depth_image_listaux_image_idsr   r   	UNDEFINEDr]   r%   r   
colorspacer   r   )r   r   r   rx   r   s        r*   r   MimCImage.__init__  s    		!::hQ)D/!:L0LtT]^bTcdeTfijTjOk0kl	$&!%'&((* **00(2288'+$+/(r,   c                2    U R                   U R                  4$ )zMimicry of c_image property.)r   r   r   s    r*   	size_modeMimCImage.size_mode  s     yy$))##r,   c                .    [         U R                     S   $ )z%Return bit-depth based on image mode.r   )r   r   r'  s    r*   r   MimCImage.bit_depth  s     #A&&r,   )r"  r   r   r%   r   r$  rx   r!  r   r   r   r   r   r  N)r   rm   r   r  rx   rq   )r  r?   )
r   r   r   r   r   r   propertyr(  r   r   r   r,   r*   r  r    s2    )0  $ $ ' 'r,   r  c                0    [         R                  " U 5        g)zLoad specified LibHeif plugin.N)r   load_plugin)plugin_paths    r*   load_libheif_pluginr0    s    %r,   )r)   dictr  r  )r)   r1  r  
int | None)r)   r1  r  r?   )F)r)   r1  rD   r2  rE   boolr  r2  )r)   r1  rE   r3  r  r2  )r  rm   )N)r  rq   )r   z
list[dict]r  bytes | None)r   r  r  r4  )r   r  r  r  )r   r  r   r?   r  r  )r   r2  r  r?   )r   ztuple | None)r  r1  )r/  z
str | Pathr  r  )=r   
__future__r   rn   r=   dataclassesr   enumr   mathr   pathlibr   structr   r	   PILr
   r9   r   	constantsr   r   r   r   r   ImportErrorex_deffered_errorr   r   INTERLEAVED_RRGGBBAA_LEINTERLEAVED_RRGGBB_LE
MONOCHROMEINTERLEAVED_RGBAINTERLEAVED_RGBYCBCR
CHROMA_444r   r   r&   r+   r0   r3   rK   r/   re   rb   r   r   r   r   r   r   r   r   r   r   r   r  r0  r   r,   r*   <module>rG     s[  
 #  	 !       U U!-2~)):+M+MN- 2~)):+M+MN- q"n((**J*JK	-
 2~)):+M+MN- 2~)):+M+MN- q"n((**J*JK- a^..
0E0EF- a^..
0E0EF- QN--z/D/DE- QN--z/D/DE- a^..
0E0EF- 2~)):+M+MN- 2~)):+M+MN- q"n((**J*JK-  2~)):+M+MN!-" 2~)):+M+MN#-$ q"n((**J*JK%-& a^..
0E0EF'-( a^..
0E0EF)-* QN--z/D/DE+-, QN--z/D/DE--. a^..
0E0EF/-0 2~)):+M+MN1-2 2~)):+M+MN3-4 q"n((**J*JK5-6 2~)):+M+MN7-8 2~)):+M+MN9-: q"n((**J*JK;-< a^..
0E0EF=-> a^..
0E0EF?-@ QN--z/D/DEA-B QN--z/D/DEC-D a^..
0E0EFE-F Q>%%z'B'BCG-H >%%z'B'BCq.$$j&@&@A>%%z'B'BC>%%z'B'BCq.$$j&@&@Aa**J,A,A
Ba**J,A,A
B
Q)):+@+@	AN((**?*?@Y-	`   

  ($A
Z2 3F4
(
 	 	"	c] c]L ' ' '<&c  !.R H!s   
U U7U22U7