
    9i.                         S r SSKrSSKrSSKJrJrJr  \R                  " \	5      r
Sr " S S\5      r " S S\5      rS	 r " S
 S5      rg)z"
PIL formats for multiple images.
    N   )PillowFormatimage_as_uintndarray_to_pilc                       \ rS rSrSrSrSrg)
TIFFFormat   izTIFF format (Pillow) N)__name__
__module____qualname____firstlineno___modes_description__static_attributes__r       [/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/imageio/plugins/pillowmulti.pyr   r      s    F)Lr   r   c                   J    \ rS rSrSrSrSr " S S\R                  5      rSr	g)		GIFFormat   z(See :mod:`imageio.plugins.pillow_legacy`iIz Static and animated gif (Pillow)c                   6    \ rS rSr      SS jrS rS rSrg)GIFFormat.Writer   Nc           	      $   SSK Jn  [        S UR                  S5       5       5      u  pn
US:X  a  U	S:  a  [	        SU S35      e[        U5      nUS	:  d  US
:  a  [        S5      eUS;  a=  S	[        [        R                  " S5      S-   5      -  n[        R                  SU-  5        Uc  S[        U5      -  U l        OM[        U[        [        45      (       a"  U Vs/ s H  n[        U5      PM     snU l        O[        U5      U l        [        U5      nUS::  d  U[        S5      :X  a  Sn[        U5      n[        U5      nU(       a  SOS	U l        U R"                  R%                  5       n['        XX[        U5      5      U l        g s  snf )Nr   )__version__c              3   8   #    U  H  n[        U5      v   M     g 7fN)int).0xs     r   	<genexpr>)GIFFormat.Writer._open.<locals>.<genexpr>)   s     'R8Q1A8Qs   .
   r   zPillow vz is not supported by ImageIO's legacy pillow plugin when writing GIFs. Consider switching to the new plugin or downgrading to `pillow<10.1.0`.      z!GIF quantize param must be 2..256)r'         r	       @      r(   r-   g+?zDWarning: palettesize (%r) modified to a factor of two between 2-256.g      ?inf)PILr   tuplesplitImportErrorr    
ValueErrornplog2loggerwarningfloat	_duration
isinstancelistbool_disposerequestget_file	GifWriter_writer)selfloopdurationfpspalettesize	quantizersubrectanglespillow_versionmajorminorpatchdfps                r   _openGIFFormat.Writer._open   sv    :"''R8L8LS8Q'R"RE%{uz!~. /@ @  k*KQ+"3 !DEE"AA3rwws|e';#<<)+67
 !$uSz!1HtUm444<!=Hq%(H!=!&x;DqyDE%L0t9D /M!.AADM &&(B$4C4DDL ">s   !Fc                 8    U R                   R                  5         g r   )rA   closerB   s    r   _closeGIFFormat.Writer._closeQ   s    LL r   c                 f   [        USS9nUR                  S:X  a  UR                  S   S:X  a  US S 2S S 2S4   nU R                  n[	        U[
        5      (       a/  U[        [        U5      S-
  U R                  R                  5         nU R                  nU R                  R                  XU5        g )Nr*   )bitdepth   r   r   )r   ndimshaper9   r:   r;   minlenrA   _countr=   	add_image)rB   immetarD   disposes        r   _append_dataGIFFormat.Writer._append_dataT   s    rA.Bww!| 11a[~~H(D))#CH(94<<;N;N$OPmmGLL""29r   )r=   r9   rA   )r   Nr&   r(   r   F)r   r   r   r   rO   rT   rc   r   r   r   r   Writerr      s&     1	f	!
	r   re   r   N)
r   r   r   r   __doc__r   r   r   re   r   r   r   r   r   r      s#    2F5LA$$ Ar   r   c                 "    U R                  SSS9$ )Nr'   little)	byteorder)to_bytes)r
   s    r   intToBinrk   a   s    ::a8:,,r   c                   r    \ rS rSrSr    SS jrS rS rS rS r	S r
SS
 jrS rSS jrS rSS jrSrg	)r@   e   zClass that for helping write the animated GIF file. This is based on
code from images2gif.py (part of visvis). The version here is modified
to allow streamed writing.
c                     Xl         X l        X0l        X@l        XPl        S U l        S U l        SU l        SSKJ	n  X`l	        g )Nr   )getdata)
rN   opt_subrectangleopt_loopopt_quantizeropt_palette_size_previous_image_global_paletter^   PIL.GifImagePluginro   )rB   filerp   rq   rr   rs   ro   s          r   __init__GifWriter.__init__k   s?      0 * 0##.r   c                    USpTU R                   (       a  U R                  U5      u  pEU R                  X@R                  U R                  5      nSSKJn  U" U5      S   S   nU R                  S:X  a"  U R                  XhU R                  5        Xl
        U R                  XhXRU5        Xl        U =R                  S-  sl        g )Nr   r   r   )	getheaderrX   r   )rp   getSubRectangleconverToPILrr   rs   rv   r|   r^   write_headerrq   ru   write_imagert   )	rB   r`   rD   rb   im_rectrectim_pilr|   palettes	            r   r_   GifWriter.add_image   s    F   004MG!!'+=+=t?T?TU
 	1F#A&q) ;;!ft}}=#* $'B  "qr   c                     U R                  U5      nU R                  U5      nU R                  R                  U5        U R                  R                  U5        U R                  R                  U5        g r   )getheaderAnim	getAppExtrN   write)rB   r`   globalPaletterC   headerappexts         r   r   GifWriter.write_header   sP    ##B'%fm$fr   c                 X    U R                   R                  SR                  S5      5        g )N;utf-8)rN   r   encoderS   s    r   rR   GifWriter.close   s    cjj)*r   c                 V   U R                   nU R                  U5      nSnU(       a:  [        U5      S:  a+  XR                  S5      -  nU(       a  [        U5      S:  a  M+  [        U5      S:X  d   eU R	                  X5      n	U R                  XE5      n
X R                  :w  d  US:w  aE  UR                  U
5        UR                  U	5        UR                  U5        UR                  S5        O"UR                  U
5        UR                  U5        U H  nUR                  U5        M     g )Nr      r   r'      )rN   ro   r]   popgetImageDescriptorgetGraphicsControlExtru   r   )rB   r`   r   r   rD   rb   rN   dataimdeslidgraphextrM   s               r   r   GifWriter.write_image   s    WW ||Bs5zBXXa[ E s5zB5zR %%b/--h@ +++AHHXHHSMHHWHHW HHXHHUO AHHQK r   c                     SnU[        UR                  S   5      -  nU[        UR                  S   5      -  nUS-  nU$ )z4Get animation header. To replace PILs getheader()[0]s   GIF89ar   r   s     rk   size)rB   r`   bbs      r   r   GifWriter.getheaderAnim   sD    
hrwwqz""
hrwwqz""
o	r   Nc                     Uc  SnSnU[        US   5      -  nU[        US   5      -  nU[        UR                  S   5      -  nU[        UR                  S   5      -  nUS-  nU$ )a[  Used for the local color table properties per image.
Otherwise global color table applies to all frames irrespective of
whether additional colors comes in play that require a redefined
palette. Still a maximum of 256 color per frame, obviously.

Written by Ant1 on 2010-08-22
Modified by Alex Robinson in Janurari 2011 to implement subrectangles.
r{      ,r   r      r   )rB   r`   xyr   s       r   r   GifWriter.getImageDescriptor   s{     :B  	hr!uo
hr!uo
hrwwqz""
hrwwqz"" 	g 	r   c                 r    US:X  a  gUS:X  a  SnSnUS:w  a  SnUS-  nUS-  nU[        U5      -  nUS-  nU$ )	zlApplication extension. This part specifies the amount of loops.
If loop is 0 or inf, it goes on infinitely.
r   r   r   i  s   !s   NETSCAPE2.0s       )rk   )rB   rC   r   s      r   r   GifWriter.getAppExt   s[     1919D19 B. B+B(4. B'MB	r   c                     SnU[        US-  S-  5      R                  S5      -  nU[        [        US-  S-   5      5      -  nUS-  nUS-  nU$ )a  Graphics Control Extension. A sort of header at the start of
each image. Specifies duration and transparancy.

Dispose
-------
  * 0 - No disposal specified.
  * 1 - Do not dispose. The graphic is to be left in place.
  * 2 - Restore to background color. The area used by the graphic
    must be restored to the background color.
  * 3 - Restore to previous. The decoder is required to restore the
    area overwritten by the graphic with what was there prior to
    rendering the graphic.
  * 4-7 -To be defined.
s   !rX   r'   r   d   g      ?r   )chrr   rk   r    )rB   rD   rb   r   s       r   r   GifWriter.getGraphicsControlExt   sb      
c7Q;1$%,,W55 	hs8c>C/011
g
g	r   c                 .   U R                   S:X  a  US4$ U R                  n[        R                  " X-
  5      nUR                  S:X  a  UR                  S5      n[        R                  " UR                  S5      5      n[        R                  " UR                  S5      5      nUR                  (       aN  UR                  (       a=  [        US   5      [        US   S-   5      pv[        US   5      [        US   S-   5      pOSu  pgSu  pXU	2Xg24   Xh44$ )aJ  Calculate the minimal rectangle that need updating. Returns
a two-element tuple containing the cropped image and an x-y tuple.

Calculating the subrectangles takes extra time, obviously. However,
if the image sizes were reduced, the actual writing of the GIF
goes faster. In some cases applying this method produces a GIF faster.
r   r{   rX   r'   r   rY   )r   r'   )	r^   rt   r4   absrZ   sumargwherer   r    )
rB   r`   prevdiffXYx0x1y0y1s
             r   r}   GifWriter.getSubRectangle  s     ;;!v:## vvbi 99>88A;DKK$KK$66aff1YAbEAI1YAbEAIFBFBR%,"))r   c                     [        US5      nUS;   a/  SnUR                  S5      n[        XE5      nUR                  XCS9nU$ US;   a:  US:X  a  UR                  S5      nOUR                  S5      nUR                  X2S	9nU$ [	        S
U-  5      e)zConvert image to Paletted PIL image.

PIL used to not do a very good job at quantization, but I guess
this has improved a lot (at least in Pillow). I don't think we need
neuqant (and we can add it later if we really want).
gif)nqneuquantr&   RGBA)colors)r   r   r'   r'   RGB)r   methodzInvalid value for quantizer: %r)r   convertNeuQuantquantizer3   )rB   r`   rG   palette_sizer   nq_samplefac
nqInstances          r   r~   GifWriter.converToPIL9  s      E***L^^F+F!&7J(((EF  )#A~/.__L_KF  >JKKr   )	r^   ru   rt   rN   ro   rq   rs   rr   rp   )Tr   r   r(   r   )g?r'   )r(   )r   r   r   r   rf   rx   r_   r   rR   r   r   r   r   r   r}   r~   r   r   r   r   r@   r@   e   sN     .2+B<"4*@r   r@   )rf   loggingnumpyr4   pillow_legacyr   r   r   	getLoggerr   r6   r   r   r   rk   r@   r   r   r   <module>r      sY      F F			8	$* *
I IX-m mr   