
    0-Ph.                         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image_as_uintndarray_to_pilc                       e Zd ZdZdZdS )
TIFFFormatizTIFF format (Pillow)N)__name__
__module____qualname___modes_description     [/var/www/html/test/jupyter/venv/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           	      n   ddl m} t          d |                    d          D                       \  }}	}
|dk    r|	dk    rt	          d| d          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 )Nr   )__version__c              3   4   K   | ]}t          |          V  d S N)int).0xs     r   	<genexpr>z)GIFFormat.Writer._open.<locals>.<genexpr>)   s(      'R'R1A'R'R'R'R'R'Rr   .r   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`.   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)r   ds     r   
<listcomp>z*GIFFormat.Writer._open.<locals>.<listcomp>?   s    !=!=!=q%((!=!=!=r   inf)PILr   tuplesplitImportErrorr   
ValueErrornplog2loggerwarningr*   	_duration
isinstancelistbool_disposerequestget_file	GifWriter_writer)selfloopdurationfpspalettesize	quantizersubrectanglespillow_versionmajorminorpatchfps               r   _openzGIFFormat.Writer._open   s    :99999"''R'R8L8LS8Q8Q'R'R'R"R"RE5%{{uzz!@~ @ @ @   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 r   )r?   closer@   s    r   _closezGIFFormat.Writer._closeQ   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shaper7   r8   r9   minlenr?   _countr;   	add_image)r@   immetarB   disposes        r   _append_datazGIFFormat.Writer._append_dataT   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   rL   rP   r^   r   r   r   Writerr      s[         1	 1	 1	 1	f	! 	! 	!
	 
	 
	 
	 
	r   r_   N)r
   r   r   __doc__r   r   r   r_   r   r   r   r   r      s[        22F5LA A A A A$ A A A A Ar   r   c                 0    |                      dd          S )Nr"   little)	byteorder)to_bytes)r	   s    r   intToBinre   a   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 )r>   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)
rK   opt_subrectangleopt_loopopt_quantizeropt_palette_size_previous_image_global_paletterY   PIL.GifImagePluginrh   )r@   fileri   rj   rk   rl   rh   s          r   __init__zGifWriter.__init__k   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   )	getheaderrS   r   )ri   getSubRectangleconverToPILrk   rl   ro   rt   rY   write_headerrj   rn   write_imagerm   )	r@   r[   rB   r]   im_rectrectim_pilrt   palettes	            r   rZ   zGifWriter.add_image   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 r   )getheaderAnim	getAppExtrK   write)r@   r[   globalPaletterA   headerappexts         r   rw   zGifWriter.write_header   sh    ##B''%%fm$$$fr   c                 `    | j                             d                    d                     d S )N;utf-8)rK   r   encoderO   s    r   rN   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"      )rK   rh   rX   popgetImageDescriptorgetGraphicsControlExtrn   r   )r@   r[   r|   rz   rB   r]   rK   dataimdeslidgraphextr+   s               r   rx   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     re   size)r@   r[   bbs      r   r~   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.
        Nrs      ,r   r      r   )r@   r[   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       )re   )r@   rA   r   s      r   r   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   !rS   r"   r   d   g      ?r   )chrr   re   r   )r@   rB   r]   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   rs   rS   r"   r   rT   )r   r"   )	rY   rm   r3   absrU   sumargwherer   r   )
r@   r[   prevdiffXYx0x1y0y1s
             r   ru   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quantizer2   )r@   r[   rE   palette_sizer{   nq_samplefac
nqInstances          r   rv   zGifWriter.converToPIL9  s      E*****L^^F++F!&,77J(((EEFF)##A~~//..__L_KKFF>JKKKr   )Tr   r   r   r   )r   r"   )r   )r
   r   r   r`   rq   rZ   rw   rN   rx   r~   r   r   r   ru   rv   r   r   r   r>   r>   e   s             .  2  + + +  B     <  "   4* * *@     r   r>   )r`   loggingnumpyr3   pillow_legacyr   r   r   	getLoggerr
   r5   r   r   r   re   r>   r   r   r   <module>r      s         F F F F F F F F F F		8	$	$* * * * * * * *
I I I I I I I IX- - -m m m m m m m m m mr   