
    e,                         d Z ddlZddlZddlmZmZmZ  ej        e	          Z
dZ G d de          Z G d de          Zd	 Z G d
 d          ZdS )z"
PIL formats for multiple images.
    N   )PillowFormatndarray_to_pilimage_as_uintc                       e Zd ZdZdZdS )
TIFFFormatizTIFF format (Pillow)N)__name__
__module____qualname___modes_description     ;lib/python3.11/site-packages/imageio/plugins/pillowmulti.pyr   r      s        F)LLLr   r   c                   @    e Zd ZdZdZdZ G d dej                  ZdS )	GIFFormatz(See :mod:`imageio.plugins.pillow_legacy`iIz Static and animated gif (Pillow)c                   .    e Zd Z	 	 	 	 	 	 d	dZd Zd ZdS )
GIFFormat.Writerr   N
      Fc           	         t          |          }|dk     s|dk    rt          d          |dvrDdt          t          j        d          dz             z  }t                              d|z             |dt          |          z  | _        nBt          |t          t          f          rd	 |D             | _        nt          |          | _        t          |          }|d
k    s|t          d          k    rd
}t          |          }t          |          }|rdnd| _        | j                                        }t          ||||t          |                    | _        d S )N   r   z!GIF quantize param must be 2..256)r                @      r   r    g+?zDWarning: palettesize (%r) modified to a factor of two between 2-256.g      ?c                 ,    g | ]}t          |          S r   )float).0ds     r   
<listcomp>z*GIFFormat.Writer._open.<locals>.<listcomp>6   s    !=!=!=q%((!=!=!=r   r   infr   )int
ValueErrornplog2loggerwarningr"   	_duration
isinstancelisttuplebool_disposerequestget_file	GifWriter_writer)selfloopdurationfpspalettesize	quantizersubrectanglesfps           r   _openzGIFFormat.Writer._open   se    k**KQ+"3"3 !DEEE"AAA3rws||e';#<#<<)+67  
 !$uSzz!1HtUm44 1!=!=H!=!=!=!&x;;DqyyDE%LL00t99D //M!.5AAADM &&((B$M4C4D4D DLLLr   c                 8    | j                                          d S N)r6   closer7   s    r   _closezGIFFormat.Writer._closeH   s    L     r   c                 \   t          |d          }|j        dk    r |j        d         dk    r|d d d d df         }| j        }t	          |t
                    r0|t          t          |          dz
  | j        j	                           }| j
        }| j                            |||           d S )Nr   )bitdepth   r   r   )r   ndimshaper-   r.   r/   minlenr6   _countr2   	add_image)r7   immetar9   disposes        r   _append_datazGIFFormat.Writer._append_dataK   s    rA...Bw!|| 1 1111a[~H(D)) Q#CH(94<;N$O$OPmGL""2x999Fr   )r   Nr   r   r   F)r
   r   r   r?   rD   rR   r   r   r   Writerr      s[         '	 '	 '	 '	R	! 	! 	!
	 
	 
	 
	 
	r   rS   N)r
   r   r   __doc__r   r   r   rS   r   r   r   r   r      sQ        22F5L7 7 7 7 7$ 7 7 7 7 7r   r   c                 0    |                      dd          S )Nr   little)	byteorder)to_bytes)r	   s    r   intToBinrY   X   s    ::a8:,,,r   c                   d    e Zd ZdZ	 	 	 	 ddZd Zd Zd Zd	 Zd
 Z	ddZ
d ZddZd ZddZdS )r5   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.
    Tr   r   c                     || _         || _        || _        || _        || _        d | _        d | _        d| _        ddlm	} || _	        d S )Nr   )getdata)
r>   opt_subrectangleopt_loopopt_quantizeropt_palette_size_previous_image_global_paletterM   PIL.GifImagePluginr\   )r7   filer]   r^   r_   r`   r\   s          r   __init__zGifWriter.__init__b   s^      0 * 0##......r   c                    |d}}| j         r|                     |          \  }}|                     || j        | j                  }ddlm}  ||          d         d         }| j        dk    r#|                     ||| j	                   || _
        |                     |||||           || _        | xj        dz  c_        d S )Nr   r   r   )	getheaderrG   r   )r]   getSubRectangleconverToPILr_   r`   rc   rh   rM   write_headerr^   rb   write_imagera   )	r7   rO   r9   rQ   im_rectrectim_pilrh   palettes	            r   rN   zGifWriter.add_imagey   s    F  	5 0044MGT!!'4+=t?TUU
 	100000)F##A&q) ;!fgt}===#*D $'BBB  "qr   c                     |                      |          }|                     |          }| j                            |           | j                            |           | j                            |           d S rA   )getheaderAnim	getAppExtr>   write)r7   rO   globalPaletter8   headerappexts         r   rk   zGifWriter.write_header   sh    ##B''%%fm$$$fr   c                 `    | j                             d                    d                     d S )N;utf-8)r>   rt   encoderC   s    r   rB   zGifWriter.close   s(    cjj))*****r   c                    | j         }|                     |          }d}|r@t          |          dk     r-||                    d          z  }|rt          |          dk     -t          |          dk    sJ |                     ||          }	|                     ||          }
|| j        k    s|dk    rU|                    |
           |                    |	           |                    |           |                    d           n*|                    |
           |                    |           |D ]}|                    |           d S )Nr      r   r      )r>   r\   rL   popgetImageDescriptorgetGraphicsControlExtrb   rt   )r7   rO   rp   rn   r9   rQ   r>   dataimdeslidgraphextr$   s               r   rl   zGifWriter.write_image   sU   W ||B 	!s5zzBTXXa[[ E  	!s5zzB5zzR %%b$//--h@@ t+++AHHXHHSMMMHHWHHW HHXHHUOOO  	 	AHHQKKKK	 	r   c                     d}|t          |j        d                   z  }|t          |j        d                   z  }|dz  }|S )z4Get animation header. To replace PILs getheader()[0]s   GIF89ar   r   s     rY   size)r7   rO   bbs      r   rr   zGifWriter.getheaderAnim   sF    
hrwqz"""
hrwqz"""
o	r   Nc                     |d}d}|t          |d                   z  }|t          |d                   z  }|t          |j        d                   z  }|t          |j        d                   z  }|dz  }|S )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.
        Nrg      ,r   r      r   )r7   rO   xyr   s       r   r   zGifWriter.getImageDescriptor   s     :B  	hr!uoo
hr!uoo
hrwqz"""
hrwqz""" 	g 	r   c                 |    |dk    rdS |dk    rd}d}|dk    r#d}|dz  }|dz  }|t          |          z  }|dz  }|S )	z|Application 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       )rY   )r7   r8   r   s      r   rs   zGifWriter.getAppExt   se     1993199D199 B. B+B(4.. B'MB	r   皙?r   c                     d}|t          |dz  dz                                d          z  }|t          t          |dz  dz                       z  }|dz  }|dz  }|S )ab  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   !rG   r   rz   d   g      ?r   )chrr{   rY   r'   )r7   r9   rQ   r   s       r   r   zGifWriter.getGraphicsControlExt   sn      
c7Q;1$%%,,W555 	hs8c>C/00111
g
g	r   c                 B   | j         dk    r|dfS | j        }t          j        ||z
            }|j        dk    r|                    d          }t          j        |                    d                    }t          j        |                    d                    }|j        rb|j        r[t          |d                   t          |d         dz             }}t          |d                   t          |d         dz             }	}n
d\  }}d\  }}	|||	||f         ||ffS )ar  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   rg   rG   r   r   rH   )r   r   )	rM   ra   r)   absrI   sumargwherer   r'   )
r7   rO   prevdiffXYx0x1y0y1s
             r   ri   zGifWriter.getSubRectangle  s    ;!v:# vb4i  9>>88A;;DK$$K$$6 	af 	1YYAbEAIB1YYAbEAIBBFBFB"R%B,"b))r   c                 j   t          |d          }|dv r?d}|                    d          }t          ||          }|                    ||          }n_|dv rI|dk    r|                    d          }n|                    d          }|                    ||	          }nt	          d
|z            |S )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quantizer(   )r7   rO   r<   palette_sizero   nq_samplefac
nqInstances          r   rj   zGifWriter.converToPIL0  s      E*****L^^F++F!&,77J(((EEFF)##A~~//..__L_KKFF>JKKKr   )Tr   r   r   rA   )r   r   )r   )r
   r   r   rT   re   rN   rk   rB   rl   rr   r   rs   r   ri   rj   r   r   r   r5   r5   \   s             .  2  + + +  B     <  "   4* * *@     r   r5   )rT   loggingnumpyr)   pillow_legacyr   r   r   	getLoggerr
   r+   r   r   r   rY   r5   r   r   r   <module>r      s          F F F F F F F F F F 
	8	$	$* * * * * * * *
? ? ? ? ? ? ? ?D- - -m m m m m m m m m mr   