+
    UBi                       ^ RI Ht ^ RIt^ RIt^ RIt^ RIt^ RIHt ^ RIH	t	 ^ RI
HtHtHt ^RIHtHtHtHtHtHtHt ^RIHt ^RIHt ^R	IHt ^R
IHt Rt]'       d   ^ RI
HtH t  ^RIH!t! ^RI"H#t#  ! R R]4      t$]$PJ                  t&R R lt' ! R R]P"                  4      t(RRRRRR/t)R R lt*]+],,          ]-].,          ,          ]P(                  ,          t/R R lt0R R lt1R R lt2 ! R  R!]4      t3R" R# lt4R$ R% lt5RBR& R' llt6R( R) lt7R* R+ lt8R, R- lt9Rt:R. R/ lt;R0 R1 lt<R2 R3 lt=R4 R5 lt>R6 R7 lt?R8 R9 lt@R: R; ltARCR< R= lltBRDR> R? lltC]P                  ! ](P                  ](]'4       ]P                  ! ](P                  ]64       ]P                  ! ](P                  ]54       ]P                  ! ](P                  R@4       ]P                  ! ](P                  RA4       R# )E    )annotationsN)IntEnum)cached_property)Any
NamedTuplecast)Image
ImageChops	ImageFile	ImageMathImageOpsImagePaletteImageSequence)i16le)o8)o16le)DeferredErrorF)IOLiteral)_imaging)Bufferc                  &    ] tR t^:tRt^ t^t^tRtR# )LoadingStrategyz.. versionadded:: 9.1.0 N)	__name__
__module____qualname____firstlineno____doc__RGB_AFTER_FIRST RGB_AFTER_DIFFERENT_PALETTE_ONLY
RGB_ALWAYS__static_attributes__r       7/usr/lib/python3.14/site-packages/PIL/GifImagePlugin.pyr   r   :   s    !O'($Jr$   r   c                    V ^8  d   QhRRRR/# )   prefixbytesreturnboolr   )formats   "r%   __annotate__r-   I   s     5 5E 5d 5r$   c                $    V P                  R4      # )   GIF87a)r/   s   GIF89a)
startswith)r(   s   &r%   _acceptr1   I   s    344r$   c                     a  ] tR t^RtRtRtRtRtR R ltR R lt	R	 R
 lt
]R R l4       t]R R l4       tR R ltRR R lltR V 3R lltR R ltR R ltRtV ;t# )GifImageFileGIFzCompuserve GIFFNc                   V ^8  d   QhRR/# )r'   r*   zbytes | Noner   )r,   s   "r%   r-   GifImageFile.__annotate__Y   s      l r$   c                	    V P                   P                  ^4      pV'       d2   V^ ,          '       d#   V P                   P                  V^ ,          4      # R# )   N)fpread)selfss   & r%   dataGifImageFile.dataY   s8    GGLLO177<<!%%r$   c                    V ^8  d   QhRRRR/# )r'   pr)   r*   r+   r   )r,   s   "r%   r-   r6   _   s      E d r$   c                	    \        ^ \        V4      ^4       FI  pV^,          W,          u;8X  d/   W^,           ,          u;8X  d   W^,           ,          8X  d   KC    R#   R# 	  R# )r   TF)rangelen)r;   r@   is   && r%   _is_palette_neededGifImageFile._is_palette_needed_   sM    q#a&!$AFad:aAh:!E(: ; % r$   c                   V ^8  d   QhRR/# r'   r*   Noner   )r,   s   "r%   r-   r6   e   s      t r$   c                	   V P                   P                  ^4      p\        V4      '       g   Rp\        V4      hVR,          V P                  R&   \        V^4      \        V^4      3V n        V^
,          pV^,          ^,           pV^,          '       du   V^,          V P                  R&   V P                   P                  ^V,          4      pV P                  V4      '       d&   \        P                  ! RV4      pV;V n
        V n        V P                   V n        V P                   P                  4       V n        RV n        V P!                  ^ 4       R# )   znot a GIF file:N   Nversion
backgroundRGBN)r9   r:   r1   SyntaxErrorinfoi16_sizerE   r   rawglobal_palettepalette_fptell_GifImageFile__rewind	_n_frames_seek)r;   r<   msgflagsbitsr@   s   &     r%   _openGifImageFile._opene   s    GGLLqzz"Cc"" u		)AYAq	)
"	Q3;;&'eDIIl#Q$Y'A&&q)) $$UA.566#dl77%)

1r$   c                   V ^8  d   QhRR/# r'   r*   intr   )r,   s   "r%   r-   r6      s     	 	# 	r$   c                	*   V P                   f<   V P                  4       p  V P                  V P                  4       ^,           R4       K*  V P                   #   \         d     T P                  4       ^,           T n          Mi ; iT P	                  T4       LK)NFrZ   rX   r[   EOFErrorseek)r;   currents   & r%   n_framesGifImageFile.n_frames   sq    >>!iikG1JJtyy{Q6 ~~  1!%q1IIgs   *A 'B ?B c                   V ^8  d   QhRR/# )r'   r*   r+   r   )r,   s   "r%   r-   r6      s      T r$   c                	    V P                   e   V P                   ^8g  # V P                  4       pV'       d   R#  V P                  ^R4       RpV P	                  V4       V#   \         d    Rp L"i ; i)NTFre   )r;   rh   is_animateds   &  r%   rm   GifImageFile.is_animated   sn    >>%>>Q&&))+	 JJq% K 			'	  	 K	 s   A   A0/A0c                    V ^8  d   QhRRRR/# )r'   framerc   r*   rI   r   )r,   s   "r%   r-   r6      s     + +# +$ +r$   c                	~   V P                  V4      '       g   R # WP                  8  d   R V n        V P                  ^ 4       V P                  p\	        V P                  ^,           V^,           4       F  p V P                  V4       K  	  R #   \
         d%   pT P                  T4       Rp\        T4      ThR p?ii ; i)Nno more images in GIF file)_seek_check_GifImageFile__frame_imr[   rB   rf   rg   )r;   rp   
last_framefer\   s   &&    r%   rg   GifImageFile.seek   s    &&<<DHJJqM\\
t||a'3A+

1 4  +		*%2sm*+s   6BB<B77B<c               $    V ^8  d   QhRRRRRR/# )r'   rp   rc   update_imager+   r*   rI   r   )r,   s   "r%   r-   r6      s&     C! C!3 C!d C!d C!r$   c                	  a  \        S P                  \        4      '       d   S P                  P                  hV^ 8X  da   ^ S n        RS n        RS n        S P                  P                  S P                  4       ^ S n	        RS P                  9   d   S P                  R M*S P                  '       d   V'       d   S P                  4        VS P                  ^,           8w  d   RV 2p\        V4      hS P                  S n        S P                  '       dE   S P                  P                  S P                  4       S P                  4       '       d   K  ^ S n        S P                  P!                  ^4      pV'       d   VR8X  d   Rp\#        V4      hRp/ pRpRpRp	 V'       g   S P                  P!                  ^4      pV'       d   VR8X  d   EMVR8X  Ed   S P                  P!                  ^4      pS P                  4       p
V^ ,          ^8X  d^   V
eZ   V
^ ,          pV^,          '       d
   V
^,          p\%        V
^4      ^
,          VR&   ^V,          pV^,	          pV'       d   VS n	        MV^ ,          ^8X  dR   RpV
'       d   W,          pS P                  4       p
K"  RV9   d   VR;;,          R	V,           ,          uu&   MWR&   RpEK2  V^ ,          ^8X  d   V^ 8X  d   V
e   V
S P                  P'                  4       3VR
&   V
P)                  R4      '       dP   S P                  4       p
V
'       d8   \+        V
4      ^8  d(   V
^ ,          ^8X  d   \%        V
^4      S P                  R&   S P                  4       '       d   K  EMVR8X  Ed   S P                  P!                  ^	4      p\%        V^ 4      \%        V^4      rV\%        V^4      ,           V\%        V^4      ,           ppVS P,                  ^ ,          8  g   VS P,                  ^,          8  dh   V'       d`   \/        VS P,                  ^ ,          4      \/        VS P,                  ^,          4      3S n        \2        P4                  ! S P0                  4       WVV3p	V^,          pV^@,          ^ 8g  pV^,          '       dd   V^,          ^,           pS P                  P!                  ^V,          4      pS P7                  V4      '       d   \8        P:                  ! RV4      pMRpS P                  P!                  ^4      ^ ,          pS P                  P'                  4       S n        MRpEK  Vf   Rp\#        V4      hVS n        V'       g   R# . S n        S P
                  '       d1   S P<                  P?                  S P
                  S P@                  4       Ve   TMS PB                  S n"        VS n#        V^ 8X  d   S PD                  '       d0   \H        \J        PL                  8X  d   Ve   RMRS n'        MRS n'        MRS n'        V'       d
   VS n(        EM7S PB                  '       d    ^ RI)H)p V! S PB                  4      S n(        EMRS n(        MS PT                  R8X  d   \H        \J        PV                  8w  g	   V'       d   RS P                  9   d|   S P<                  PY                  S P                  R,          ^ 4       S P<                  P[                  R\2        P\                  P^                  4      S n        RS n'        S P                  R M@RS n'        S P<                  P[                  R\2        P\                  P^                  4      S n        R V 3R llpRS n        V	S n         S P@                  '       Ed   S P                  ^8  Ed    S P                  ^8X  d   S P@                  w  rppVV,
          VV,
          3p\2        P4                  ! V4       RpS P                  Pa                  RV4      pVe$   S PT                  R9   d   RpV! V4      R,           pM7S P                  Pa                  R^ 4      pS PT                  R9   d   RpV! V4      p\2        Pb                  Pe                  VVV4      S n        MS Pf                  e-   S Pi                  S P<                  S P@                  4      S n        MVe   S P@                  w  rppVV,
          VV,
          3p\2        P4                  ! V4       RpTpS PT                  R9   d   RpV! V4      R,           p\2        Pb                  Pe                  VVV4      S n        Vez   RpVeD   V^ 8X  d*   \H        \J        PL                  8w  d   VS P                  R&   MS PT                  R9  d   Tp\l        Pn                  ! RXXXX3S P                  XVV34      .S n        VPa                  R4      '       d   VR,          S P                  R&   R FB  pVV9   d   VV,          S P                  V&   K"  VS P                  9   g   K5  S P                  V KD  	  R#   \j         d     ELi ; i)r   Ncommentzcannot seek to frame    ;rr      !durationr$      
	extension   NETSCAPE2.0loop   ,rO   Fzimage not found in GIF frameRGBAPL)copytransparencyc                    V ^8  d   QhRRRR/# )r'   colorrc   r*   ztuple[int, int, int]r   )r,   s   "r%   r-   (GifImageFile._seek.<locals>.__annotate___  s     		- 		- 		- 4 		-r$   c           	     N  < SP                   '       d   V ^,          ^,           \        SP                   P                  4      8  d   ^ p \        \        \
        \
        \
        3,          \	        SP                   P                  V ^,          V ^,          ^,            4      4      # W V 3# )   )_frame_paletterC   rV   r   tuplerc   )r   r;   s   &r%   _rgb GifImageFile._seek.<locals>._rgb_  s    """19q=3t':':'B'B#CCE#sC-($--55eai%!)a-PQ 
 e,,r$   rN   gifrO   r   r   )r   r   )8
isinstancerW   r   ex_GifImageFile__offsetdisposert   rg   rY   disposal_methodrQ   tileload
ValueErrorr9   r=   r:   rf   rR   rX   r0   rC   sizemaxrS   r	   _decompression_bomb_checkrE   r   rT   impastedispose_extentrU   r   _frame_transparencyLOADING_STRATEGYr   r"   _moderV   r   moder!   putpalettealphaconvertDitherFLOYDSTEINBERGgetcorefillru   _cropAttributeErrorr   _Tile)r;   rp   r{   r\   r<   rV   rQ   frame_transparency	interlaceframe_dispose_extentblockr]   dispose_bitsr}   x0y0x1y1r^   r@   r   r   dispose_sizedispose_moder   r   ks   f&&                        r%   r[   GifImageFile._seek   s   dhh..((++A:DM8<DLDLHHMM$--(#$D DII%IIi( yyy\		DLL1$$)%1CS/!((===GGLL'))++DMGGLLOAI.C3-EI!!	#GGLLOT	d GGLLO		Q43;5#4 "!HEqyy-21X*'*5!}r'9D$ $.#5L#/1#4L#
 0<,qTS[ "G  ( $		 D(Y57?:*1YAqTS[UaZE4E ).tww||~(=D%''77 $		 SZ1_qQ03E1DIIf-iikk " d GGLLO QC1IBc!Qic!QiB1%diil):!$R1!6B		!8M!MDJ33DJJ?')r2~$!"RZA-	3;;!AI?DQ$Y/A..q11"."2"25!"<"' ww||Aq) $A0C3-	<<<GGMM$,,(;(;<)0)<g$BUBU#5 A:"""#'A'AA+=+IuDJ!$DJ 
&$$$%#D$7$78#yyC$(X(XX%2//		.0I1M"&''//&%,,:U:U"V%+
 IIn5%*
"&''//%9T9T"U		- 		- @T4#7#71#<*''1, &*%8%8NBB$&GR"W#5L33LA $'L IIMM.:LME(997+1L$(K$$6E $		lA >997+0L$(KE#(::??<u#UDL xx+'+zz$''4;N;N'O+7)-)<)<B(*Rb'977E'* 2997+1L$();$<t$CE',zz(,(  L!-A:'?+E+EE4F		.1YYo5#5LR$MM9l3	DI 88I#'	?DIIi *ADy#Aw		!diiIIaL	 +- " s    *C+i 9i Bi iic                   V ^8  d   QhRR/# rH   r   )r,   s   "r%   r-   r6     s      d r$   c                	  < V P                   '       d   R MRpRV n        V P                  ^ 8X  dI   V P                  e:   \        P
                  P                  WP                  V P                  4      V n        MV P                  R9   d   V P                  V n        V P                   '       d|   \        P
                  P                  R V P                  V P                  ;'       g    ^ 4      V n        V P                  P                  ! R.V P                   P                  4       O5!   MRV n        V P                  '       g   V P                  e   V P                  V P                  P                  8w  d   \        P
                  P                  V P                  P                  V P                  4      pV P                   '       d-   VP                  ! R.V P                   P                  4       O5!   VP                  V P                  RV P                  P                  ,           4       W n        Wn        RV n         \        SV `A  4        R# )r   r   NrO   r   r   r   )r   _prev_imrt   r   r	   r   r   r   r   r   
putpalettegetdataru   r   r   superload_prepare)r;   	temp_modeexpanded_im	__class__s   &  r%   r   GifImageFile.load_prepare  s   ...CC	<<1''3**//yy$*B*B YY/) GGDM"""**//#tyy$:R:R:W:WVWX""5I4+>+>+F+F+HI}}}!5$))tww||:S**//$'',,		BK"""&&uMt/B/B/J/J/LMdggv'<=!G
"r$   c                   V ^8  d   QhRR/# rH   r   )r,   s   "r%   r-   r6     s     (9 (9$ (9r$   c                	   V P                   ^ 8X  d   V P                  R8X  d   \        \        P                  8X  d   V P
                  e/   V P                  P                  V P
                  ^ 4       RV n        MRV n        V P                  P                  V P                  \        P                  P                  4      V n        R# V P                  '       g   R# V P                  V P                  P                  8w  d   V P
                  e,   \        P                  P!                  RV P                  4      pMf\        P                  P!                  RV P                  4      pVP#                  RRV P                  P%                  4       4       VP                  R4      pVP'                  V P                  RV P                  P                  ,           4       Wn        V P                  f   Q hV P
                  e{   V P                  R8X  d(   V P                  P)                  RV P
                  4      pM]V P                  P                  V P
                  ^ 4       V P                  P                  R4      pMV P                  P                  R4      pV P*                  f   Q hV P-                  W P*                  4      pV P                  V n        V P                  P                  V n        VP                  R9   d)   V P                  P'                  W P*                  V4       R# V P                  P'                  W P*                  4       R# )	r   r   Nr   rO   r   LAr   )r   r   )rt   r   r   r   r"   r   r   r   r   r   r	   r   r   r   r   r   r   r   
getpaletter   convert_transparentr   r   )r;   r   frame_ims   &  r%   load_endGifImageFile.load_end  s4   <<1yyC$48R8R$R++7GG++D,D,DaH!'DJ!&DJ''//$))U\\5P5PQ}}}99***''3#jjoofdii@#jjooc499=&&ueTWW5G5G5IJ)11%8dmmVdmm6H6H-HI'M==,,,##/yyC7766tT=U=UV''(@(@!D77??62wwu-H""...::h(;(;<--WW\\
==N*GGMM($7$7BGGMM($7$78r$   c                   V ^8  d   QhRR/# rb   r   )r,   s   "r%   r-   r6     s      c r$   c                	    V P                   # N)rt   )r;   s   &r%   rX   GifImageFile.tell  s    ||r$   )__frame__offset__rewindrW   r   r   ru   r   rZ   r   rS   r   r   r   r9   rU   r   rV   r   )T)r   r   r   r   r,   format_description!_close_exclusive_fp_after_loadingrU   r=   rE   r_   propertyri   r   rm   rg   r[   r   r   rX   r#   __classcell__)r   s   @r%   r3   r3   R   sv    F)(-%N4 	 	  "+ C!J 6(9T r$   r3   1r   r   c                    V ^8  d   QhRRRR/# )r'   r   Image.Imager*   r   )r,   s   "r%   r-   r-     s        r$   c                   V P                   \        9   d   V P                  4        V # \        P                  ! V P                   4      R8X  d   V P                  R\        P                  P                  R7      p V P                  f   Q hV P                  P                   R8X  dY   V P                  P                   F>  pV^,          ^ 8X  g   K  V P                  P                  V,          V P                  R&    V # 	  V # V P                  R4      # )z
Takes an image (or frame), returns an image in a mode that is appropriate
for saving in a Gif.

It may return the original image, or it may return an image converted to
palette or 'L' mode.

:param im: Image object
:returns: Image object
rO   r   rV   r   r   r   )r   RAWMODEr   r	   getmodebaser   PaletteADAPTIVErV   colorsrQ   )r   rgbas   & r%   _normalize_moder     s     
ww'
		!U*ZZU]]%;%;Z<zz%%%::??f$

))7a<.0jj.?.?.EBGGN+		 * 	::c?r$   c               (    V ^8  d   QhRRRRRRRR/# )r'   r   r   rV   _Palette | NonerQ   dict[str, Any]r*   r   )r,   s   "r%   r-   r-     s2     D DD-D5CDDr$   c                   RpV'       di   \        V\        \        \        34      '       d   \        VR,          4      p\        V\        P                  4      '       d   \        VP
                  4      pV P                  R8X  d,   V'       g#   V P                  R4      pVf   Q h\        V4      pM@V'       g   \        R \        R4       4       4      p\        P                  ! RVR7      V n        Vf   Q hV'       d   . pV P
                  f   Q h\        ^ \        V4      ^4       FU  p\        W6V^,            4      pV P
                  P                  P                  V4      pW9   d   RpVP                  V4       KW  	  \        V4       F3  w  rhVe   K  \        \        V4      4       F  p	W9  g   K  WV&    K1  	  K5  	  . p
V F  pVf   Q hV
P                  V4       K  	  V P                  V
4      p MG\!        W4      pVe9   V P                  W4      p RV9   d    VP#                  VR,          4      VR&   V # V # V P
                  f   Q hW0P
                  n        V #   \$         d    TR  T # i ; i)	aP  
Normalizes the palette for image.
  - Sets the palette to the incoming palette, if provided.
  - Ensures that there's a palette for L mode images
  - Optimizes the palette if necessary/desired.

:param im: Image object
:param palette: bytes object containing the source palette, or ....
:param info: encoderinfo
:returns: Image object
N:Ni   Nr   c              3  2   "   T F  q^,          x  K  	  R# 5i)r   Nr   ).0rD   s   & r%   	<genexpr>%_normalize_palette.<locals>.<genexpr><  s     &Bz!Avvzs   i   rO   r   r   )r   r)   	bytearraylistr   rV   r   r   rB   rC   r   r   r   append	enumerateremap_palette_get_optimizeindexr   )r   rV   rQ   source_palette
im_paletteused_palette_colorsrD   source_colorr   jdest_mapoptimized_palette_colorss   &&&         r%   _normalize_paletter    s>    Ngy$788&wt}5Ng|8899&w7N	ww#~t,J)))&z2N&&BuSz&BBN!..unM
%%%02zz%%%q#n-q1A AE!:;LJJ%%)),7E+&&u- 2 ""56HA}s#678A312A. 9 7 !(E$$$OOE" ) h'#0#: #/!!":KB%-+C+I+I^,,D(
 I2I::!!!'JJI " -^,I-s   I I/.I/c               (    V ^8  d   QhRRRRRRRR/# )	r'   r   r   r9   	IO[bytes]rV   r   r*   rI   r   )r,   s   "r%   r-   r-   f  s0        
	r$   c                   \        V 4      pVP                  P                  4        F8  w  rE\        V\        4      '       g   K  V P
                  P                  WE4       K:  	  \        W2V P
                  4      p\        W0P
                  4       F  pVP                  V4       K  	  ^ p\        V 4      '       d
   V^@,          p\        WRV4       ^\        V 4      3Vn        \        P                  ! W1\        P                  ! RRV P                   ,           ^ \"        VP$                  ,          4      .4       VP                  R4       R# r   r       Nr   )r   rQ   itemsr   strencoderinfo
setdefaultr  _get_global_headerwriteget_interlace_write_local_headerencoderconfigr   _saver   r   r   r   )r   r9   rV   im_outr   vr<   r]   s   &&&     r%   _write_single_framer  f  s    
 R F!!#aNN%%a+ $  @F7
 8 ER
.}R01FOOY__UFRWW,<aAUVW HHUOr$   c               $    V ^8  d   QhRRRRRR/# )r'   base_imr   im_framer*   z4tuple[Image.Image, tuple[int, int, int, int] | None]r   )r,   s   "r%   r-   r-     s$     
2 
2
2$/
29
2r$   c                P   W3 Uu. uF5  q"P                   '       d    \        VP                   P                   4      MR NK7  	  ppV^ ,          V^,          8w  d#   VP                  R4      pV P                  R4      p \        P                  ! W4      pWDP                  RR7      3# u upi )r$   r   F)
alpha_only)rV   r)   r   r
   subtract_modulogetbbox)r  r  r   palette_bytesdeltas   &&   r%   _getbboxr    s     ELCVCVRZZZbjj  !S8CV   Q=++##F+//&)&&x9E--5-111s   ;B#c                  6    ] tR tRt$ R]R&   R]R&   R]R&   RtR	# )
_Framei  r   r   z tuple[int, int, int, int] | Nonebboxr   r
  r   N)r   r   r   r   __annotations__r#   r   r$   r%   r   r     s    O
**r$   r   c               (    V ^8  d   QhRRRRRRRR/# )	r'   r   r   r9   r  rV   r   r*   r+   r   )r,   s   "r%   r-   r-     s2      "-<	r$   c           
        V P                   P                  R 4      pV P                   P                  RV P                  P                  R4      4      p. pRp^ pRp\        P                  ! V .V P                   P                  R. 4      4       EF  p	\
        P                  ! V	4       EFm  p
\        V
P                  4       4      p
V^ 8X  d`   V
P                  P                  4        FA  w  rVR8X  d   K  \        V\        4      '       g   K&  V P                   P                  W4       KC  	  V P                   P                  4       pRV
P                  9   d$   VP                  RV
P                  R,          4       \        WV4      p
\        V\        \        34      '       d   W7,          VR &   M+Vf(   R V
P                  9   d   V
P                  R ,          VR &   \        V\        \        34      '       d   WG,          VR&   V^,          pRpV'       Ed   V'       Ed   \!        Wj4      w  ppV'       gH   VP                  R 4      '       d.   VR,          P                   R ;;,          VR ,          ,          uu&   EK  VR,          P                   P                  R4      ^8X  d   V P                   P                  RV P                  P                  R4      4      pVe   Vfn   \#        V
V4      p\$        P&                  ! RV
P(                  V4      pV^ ,          P*                  P,                  pVf   Q hVP/                  VVP0                  4       \!        W4      ^,          pEMRV
P(                  ,           pEMzVP                  R4      '       Ed`   V
P0                  R8w  EdN   RV9  d0   V
P,                  f   Q h V
P,                  P3                  V
4      VR&   RV9   Ed   V
P                  4       p\$        P&                  ! RVP(                  VR,          4      pVP0                  R8X  d3   VP7                  4       w  pppp\8        P:                  ! R	 VVVVR
7      pMlVP0                  R8X  dC   \$        P&                  ! RVP(                  4      pVP=                  VP?                  4       4       Tp\8        P:                  ! R VR7      pVPA                  V\B        PD                  ! V4      R7       MRpT
pTPG                  \I        T;'       g    T
VV4      4       EKp  	  EK  	  \K        V4      ^8X  d;   R V P                   9   d(   V^ ,          P                   R ,          V P                   R &   R# V F  pVP*                  p
VPL                  '       g2   \O        V
VP                   4       F  pVPQ                  V4       K  	  RpMgV'       g   RVP                   R&   VPL                  RV
P(                  ,           8w  d   V
PS                  VPL                  4      p
VPL                  R,          p\U        WVVP                   4       K  	  R#   \4         d     EL|i ; i)r   disposalNappend_imagesr   r   optimizer   r   c                    V R ,          ! V R,          ! V R,          ! V R,          ! V R,          V R,          4      V R,          4      V R,          4      ^,          R4      # )r   r   rgbar   r   argss   &r%   <lambda>(_write_multiple_frames.<locals>.<lambda>  sZ    T)_$(K(,U,0KS	49,MtTWy)* )-S		%& '*%* %(	."r$   )r)  r*  r+  r,  r   c                >    V R ,          ! V R,          ^,          R4      # )r   r   r   r   r-  s   &r%   r/  r0    s    T)_T$Z#=Ms-Sr$   )r   )maskFTinclude_color_table:Nr'   Nr   r   )+r
  r   rQ   	itertoolschainr   Iteratorr   r   r  r   r	  r  r  r   r   r  _get_backgroundr	   newr   r   rV   r   r   _new_color_indexr   splitr   lambda_evalputdatar   r   r   invertr   r   rC   r!  r  r  crop_write_frame_data)r   r9   rV   r   r%  	im_framesprevious_imframe_countbackground_im
imSequencer  r   r  r
  
diff_framer  r!  r   rN   first_paletter   r)  r*  r+  r,  r2  delta_l
frame_datar<   offsets   &&&                           r%   _write_multiple_framesrJ    s    ~~!!*-H~~!!*bggkk*.EFH I&*KKMoordBNN,>,>PR,ST
%..z:H&x}}7Ha$MM//1DAN* !!S))11!7	 2 ..--/K.&&~x}}^7TU)([IH(T5M22*2*?J'!jHMM&A*2--
*CJ'(T5M22*2*?J'1KJy[[&{=t"z22!"11*=ZAXX=R=,,00<A NN..&N(CE ((0)85)IJ,1IIc8==*,UM,5aLOO,C,CM#0#<<#<)44]MDVDVW'@C%5 __Z00X]]c5I%[8'//;;;! ( 0 0 A A( K (7
 &4%-]]_
$yyejj+n:UV ::/).JAq!Q#,#8#8	!" #$"#"#"#$D"  %zzS0*/))C*D ' @(/#,#8#8 S#($D #((HOOD4I(J"KVJ$:$:(D+NOy ; U~ 9~')21)A)A*)MBNN:&
=='*2H2HI JF @D
&&'<=&8=="88#==9__R(F"
0F0FG    }  * ! !s   2X>>YYc               (    V ^8  d   QhRRRRRRRR/# 	r'   r   r   r9   r  filenamestr | bytesr*   rI   r   )r,   s   "r%   r-   r-     s(     + ++ +9 + + +r$   c                "    \        WVR R7       R# )T)save_allN)r  )r   r9   rM  s   &&&r%   	_save_allrQ    s    	"(T*r$   c          
     ,    V ^8  d   QhRRRRRRRRR	R
/# )r'   r   r   r9   r  rM  rN  rP  r+   r*   rI   r   )r,   s   "r%   r-   r-     s4      ".9EI	r$   c                   R V P                   9   g   R V P                  9   d7   V P                   P                  R V P                  P                  R 4      4      pMRpV P                   P                  RR4       V'       d   \	        WV4      '       g   \        WV4       VP                  R4       \        VR4      '       d   VP                  4        R# R# )rV   Nr'  Tr~   flush)	r
  rQ   r   r  rJ  r  r  hasattrrT  )r   r9   rM  rP  rV   s   &&&& r%   r  r    s     BNN"i277&:..$$YI0FG
!!*d31"'BBBG,HHTNr7

 r$   c                    V ^8  d   QhRRRR/# )r'   r   r   r*   rc   r   )r,   s   "r%   r-   r-   .  s      k c r$   c                v    V P                   P                  R ^4      p\        V P                  4      ^8  d   ^ pV# )r   )r
  r   minr   )r   r   s   & r%   r  r  .  s5    "";2I 277|b	r$   c          
     ,    V ^8  d   QhRRRRRRRRR	R
/# )r'   r9   r  r   r   rI  tuple[int, int]r]   rc   r*   rI   r   )r,   s   "r%   r-   r-   8  s4     / //"/,;/DG/	/r$   c                    VP                   R ,          pRVP                   9   d%   \        VP                   R,          ^
,          4      pM^ p\        VP                   P                  R^ 4      4      pVf   V^ 8w  g	   V'       d   Ve   ^M^ pWv^,          ,          pT P	                  R\        ^4      ,           \        ^4      ,           \        V4      ,           \        V4      ,           \        T;'       g    ^ 4      ,           \        ^ 4      ,           4       VP                   P                  R4      pV'       d0   \        V4      p	\        V	4      p
V
'       d   V^,          pW:,          pV P	                  R\        V^ ,          4      ,           \        V^,          4      ,           \        VP                  ^ ,          4      ,           \        VP                  ^,          4      ,           \        V4      ,           4       V'       d#   X
'       d   V P	                  \        X	4      4       V P	                  \        ^4      4       R#   \         d    Rp EL:i ; i)r   Nr   r%  r   r3  r   )r
  KeyErrorrc   r   r  r   o16_get_palette_bytes_get_color_table_sizer   _get_header_palette)r9   r   rI  r]   r   r   r%  packed_flagr3  r  color_table_sizes   &&&&       r%   r  r  8  s   ~~n5 R^^#r~~j1B672>>%%j!45H8q=H'3a1}$
ge o (m	
 ""#$ e	
 ..,,-BC*2.0?CKE,EHH
fQi.	
fQi.	 bggaj/	 bggaj/		
 U)	 /
$]34HHRUOU  s   H? ?IIc               (    V ^8  d   QhRRRRRRRR/# rL  r   )r,   s   "r%   r-   r-   j  s(     - -[ -i -; -4 -r$   c           	        V P                  4       p \        VR 4      ;_uu_ 4       pV P                  R8w  d+   \        P                  ! RV.V\        P
                  R7       MRRV.pR.p\        P                  ! V\        P                  \        P
                  R7      p\        P                  ! VVP                  V\        P
                  R7      pVP                  f   Q hVP                  P                  4        VP                  4       p	V	'       d   \        P                  ! W4      hVP                  4       p	V	'       d   \        P                  ! W4      hRRR4        \        P                  ! V4       R#   + '       g   i     L); i  \         d     R# i ; i   \        P                  ! T4       i   \         d     i i ; i; i)wbrO   ppmtogif)stdoutstderrppmquant256)stdinrg  rh  N)_dumpopenr   
subprocess
check_callDEVNULLPopenPIPErg  closewaitCalledProcessErrorosunlinkOSError)
r   r9   rM  tempfilerw   	quant_cmd	togif_cmd
quant_proc
togif_procretcodes
   &&&       r%   _save_netpbmr  j  sm    xxzH$(D!!Qww%%%*1Z=O=O (9	'L	'--jooj>P>P
 (--$++%--	
 "((444!!'')$//+$77KK$//+$77KK= "@	IIhC "!D  			IIh 		se   F C,E8-E8E8F  F 8F	F FFGF65G6GGGGc               $    V ^8  d   QhRRRRRR/# )r'   r   r   rQ   r   r*   zlist[int] | Noner   )r,   s   "r%   r-   r-     s"     - -k - -<L -r$   c                   V P                   R9   Edg   V'       Ed^   VP                  R4      '       EdF   \        ;'       g    V P                   R8H  pV'       g#   V P                  V P                  ,          R8  d   . p\        V P                  4       4       F   w  rEV'       g   K  VP                  V4       K"  	  V'       g   \        V4      \        V4      8  d   V# V P                  f   Q h\        V P                  P                  4      \        P                  ! V P                  P                   4      ,          p^V^,
          P                  4       ,          p\        V4      V^,          8:  d
   V^8  d   V# R# )a0  
Palette optimization is a potentially expensive operation.

This function determines if the palette should be optimized using
some heuristics, then returns the list of palette entries in use.

:param im: Image object
:param info: encoderinfo
:returns: list of indexes of palette entries in use, or None
r   r'  N)r   r   i   )r   r   _FORCE_OPTIMIZEwidthheightr   	histogramr   r   rC   rV   r	   getmodebands
bit_length)r   rQ   optimiser   rD   countnum_palette_colorscurrent_palette_sizes   &&      r%   r   r     s#    
ww*$((:*>*> #44bggnrxx"))+i7"$%blln55'..q1 6 323s;N7OO**::)))!$RZZ%7%7!8E<N<N

= " $%);a)?(K(K(M#M  '(,@A,EE(1,**r$   c                    V ^8  d   QhRRRR/# )r'   r  r)   r*   rc   r   )r,   s   "r%   r-   r-     s     C C C3 Cr$   c                    V '       g   ^ # \        V 4      ^	8  d   ^# \        P                  ! \        P                  ! \        V 4      ^,          ^4      4      ^,
          # r   )rC   mathceillog)r  s   &r%   r_  r_    sA    	]	a	yy#m"4"91=>BBr$   c                    V ^8  d   QhRRRR/# )r'   r  r)   r*   r   )r,   s   "r%   r-   r-     s      u  r$   c                    \        V 4      p^V,          \        V 4      ^,          ,
          pV^ 8  d!   V \        ^ 4      ^,          V,          ,          p V # )z
Returns the palette, null padded to the next power of 2 (*3) bytes
suitable for direct inclusion in the GIF header

:param palette_bytes: Unpadded palette bytes, in RGBRGB form
:returns: Null padded palette
)r_  rC   r   )r  rb  actual_target_size_diffs   &  r%   r`  r`    sR     -];  !$44M8Ja8OO"A%<<<r$   c                    V ^8  d   QhRRRR/# )r'   r   r   r*   r)   r   )r,   s   "r%   r-   r-     s      ; 5 r$   c                  a V P                   '       g   R# \        V P                   P                   4      oV P                   P                  R8X  d5   RP                  V3R l\	        \        S4      ^,          4       4       4      oS# )z
Gets the palette for inclusion in the gif header

:param im: Image object
:returns: Bytes, len<=768 suitable for inclusion in gif header
r$   r   c              3  X   <"   T F  pSV^,          V^,          ^,            x  K!  	  R# 5i)   Nr   )r   rD   rV   s   & r%   r   %_get_palette_bytes.<locals>.<genexpr>  s&     X?W!71q51q5195?Ws   '*)rV   r)   r   joinrB   rC   )r   rV   s   &@r%   r^  r^    s]     :::BJJ&&'G	zz& ((XuS\UVEV?WXXNr$   c               $    V ^8  d   QhRRRRRR/# )r'   r   r   info_backgroundz=int | tuple[int, int, int] | tuple[int, int, int, int] | Noner*   rc   r   )r,   s   "r%   r-   r-     s&      R 	r$   c                    ^ pV'       dG   \        V\        4      '       d/   V P                  f   Q h V P                  P                  W4      pV# TpV#   \         d   p\        T4      R9  d   h  Rp?T# Rp?ii ; i)r   N)z$cannot allocate more than 256 colorsz/cannot add non-opaque RGBA color to RGB palette)r   r   rV   getcolorr   r	  )r   r  rN   rx   s   &&  r%   r7  r7    s     Jou-- ::)))
ZZ00E
  )J  q6 "   s   A A:A55A:c               $    V ^8  d   QhRRRRRR/# )r'   r   r   rQ   r   r*   list[bytes]r   )r,   s   "r%   r-   r-     s!     ; ;; ;n ; ;r$   c                   RpV P                   P                  R4      R8X  gQ   V'       dK   RV9   gB   VP                  R4      f/   VP                  R4      '       g   VP                  R4      '       d   Rp\        WP                  R4      4      p\        V 4      p\	        V4      pR	V,           \        V P                  ^ ,          4      ,           \        V P                  ^,          4      ,           \        V^,           4      \        V4      \        ^ 4      ,           \        V4      .pVP                  R4      e   VP                  R
\        ^4      ,           \        ^4      ,           R,           \        ^4      ,           \        ^4      ,           \        VR,          4      ,           \        ^ 4      ,           4       VP                  R4      '       d   R
\        ^4      ,           pVR,          p\        V\        4      '       d   VP                  4       p\        ^ \        V4      ^4       F0  p	WV	^,            p
V\        \        V
4      4      V
,           ,          pK2  	  V\        ^ 4      ,          pVP                  V4       V# )z2Return a list of strings representing a GIF headers   87arM   s   89ar   r   r   r}   rN   s   GIFr   r   )rQ   r   r7  r^  r_  r]  r   r   r`  r   r   r	  encoderB   rC   )r   rQ   rM   rN   r  rb  headercomment_blockr}   rD   subblocks   &&         r%   r  r    s    G	ww{{9'd"xx+xx
##xx	""  XXl%;<J&r*M,]; 	
	
bggaj/	 bggaj/	 	c!"
:AM*F xx#gf  e	
 e $v,  e		
 xx	r#wy/gs##nn&Gq#g,,A1s7+HRH.99M - 	Am$Mr$   c          
     ,    V ^8  d   QhRRRRRRRRR	R
/# )r'   r9   r  r  r   rI  rZ  paramsr   r*   rI   r   )r,   s   "r%   r-   r-   T  s:     ! !!! ! 	!
 
!r$   c                    W1n         \        WV^ 4       \        P                  ! VV \        P                  ! RRVP
                  ,           ^ \        VP                  ,          4      .4       V P                  R4       V= R#   T= i ; ir  )	r
  r  r   r  r   r   r   r   r  )r9   r  rI  r  s   &&&&r%   r?  r?  T  sn    !% 	B&!4__UFX]]$:Awx}}?UVW	
 	 H s   A:B   Bc               (    V ^8  d   QhRRRRRRRR/# )	r'   r   r   rV   r   rQ   zdict[str, Any] | Noner*   z$tuple[list[bytes], list[int] | None]r   )r,   s   "r%   r-   r-   o  s,     ' ''-'<Q')'r$   c                    Vf   / p\        W4      pRV9  d(   RV P                  9   d   V P                  R,          VR&   \        WV4      pVP                  V n        VP                  V n        \        W4      pWS3# )a   
Legacy Method to get Gif data from image.

Warning:: May modify image data.

:param im: Image object
:param palette: bytes object containing the source palette, or ....
:param info: encoderinfo
:returns: tuple of(list of header items, optimized palette)

rN   )r   rQ   r  rV   r   r  )r   rV   rQ   r   im_modr  s   &&&   r%   	getheaderr  o  st     |'14LBGG$;WW\2\T2FBJIIBE)F&&r$   c               (    V ^8  d   QhRRRRRRRR/# )	r'   r   r   rI  rZ  r  r   r*   r  r   )r,   s   "r%   r-   r-     s,     " "","AD""r$   c                    ^ RI Hp  ! R RV4      pV P                  4        V! 4       p\        WPW4       VP                  # )a  
Legacy Method

Return a list of strings representing this image.
The first string is a local image header, the rest contains
encoded image data.

To specify duration, add the time in milliseconds,
e.g. ``getdata(im_frame, duration=1000)``

:param im: Image object
:param offset: Tuple of (x, y) pixels. Defaults to (0, 0)
:param \**params: e.g. duration or other encoder info parameters
:returns: List of bytes containing GIF encoded frame data

)BytesIOc                  &    ] tR tRt. tR R ltRtR# )getdata.<locals>.Collectori  c                    V ^8  d   QhRRRR/# )r'   r=   r   r*   rc   r   )r,   s   "r%   r-   'getdata.<locals>.Collector.__annotate__  s     	 	f 	 	r$   c                	N    V P                   P                  V4       \        V4      # r   )r=   r   rC   )r;   r=   s   &&r%   r   getdata.<locals>.Collector.write  s    IIT"t9r$   r   N)r   r   r   r   r=   r  r#   r   r$   r%   	Collectorr    s    	 	r$   r  )ior  r   r?  r=   )r   rI  r  r  r  r9   s   &&,   r%   r   r     s8    & G  GGI	Bbf-77Nr$   z.gifz	image/gif)F)NN)r   )J
__future__r   r4  r  rv  rn  enumr   	functoolsr   typingr   r   r    r	   r
   r   r   r   r   r   _binaryr   rR   r   r   r]  _utilr   TYPE_CHECKINGr   r   r   _typingr   r   r    r   r1   r3   r   r   r)   r   r   rc   _Paletter  r  r  r   rJ  rQ  r  r  r  r  r  r   r_  r`  r^  r7  r  r?  r  r   register_openr,   register_saveregister_save_allregister_extensionregister_mimer   r$   r%   <module>r     s  4 #   	   % ( (   "  !  "g  #22 5f9&& fZ S#sC
(4 9tCy(<+D+DDDN8
2 Z  D+&/d-d -`C$ 6;|!6'<"P   L''w ?   L'' /   ++Y 7   ,,f 5   L'' 5r$   