U
    ?h.                     @   sp   d Z ddlZddlZddlmZmZmZ ee	Z
dZG dd deZG dd deZd	d
 ZG dd dZdS )z"
PIL formats for multiple images.
    N   )PillowFormatimage_as_uintndarray_to_pilc                   @   s   e Zd ZdZdZdS )
TIFFFormatizTIFF format (Pillow)N)__name__
__module____qualname___modes_description r   r   M/var/www/html/venv/lib/python3.8/site-packages/imageio/plugins/pillowmulti.pyr      s   r   c                   @   s*   e Zd ZdZdZdZG dd dejZdS )	GIFFormatz(See :mod:`imageio.plugins.pillow_legacy`ZiIz Static and animated gif (Pillow)c                   @   s&   e Zd ZdddZdd	 Zd
d ZdS )zGIFFormat.Writerr   N
      Fc                 C   s<  ddl m} tdd |dD \}}	}
|dkrJ|	dkrJtd| d	t|}|d
k sb|dkrjtd|dkrd
ttdd  }t	
d|  |d krdt| | _n*t|ttfrdd |D | _n
t|| _t|}|dks|tdkrd}t|}t|}|rdnd
| _| j }t||||t|| _d S )Nr   )__version__c                 s   s   | ]}t |V  qd S N)int).0xr   r   r   	<genexpr>)   s     z)GIFFormat.Writer._open.<locals>.<genexpr>.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                 S   s   g | ]}t |qS r   )float)r   dr   r   r   
<listcomp>?   s     z*GIFFormat.Writer._open.<locals>.<listcomp>inf)ZPILr   tuplesplitImportErrorr   
ValueErrornplog2loggerwarningr    	_duration
isinstancelistbool_disposerequestZget_file	GifWriter_writer)selfloopdurationZfpsZpalettesize	quantizerZsubrectanglesZpillow_versionmajorminorpatchfpr   r   r   _open   sF    	


    zGIFFormat.Writer._openc                 C   s   | j   d S r   )r3   closer4   r   r   r   _closeQ   s    zGIFFormat.Writer._closec                 C   s~   t |dd}|jdkr:|jd dkr:|d d d d df }| j}t|trd|tt|d | jj	 }| j
}| j||| d S )Nr   )Zbitdepth   r   r   )r   ndimshaper,   r-   r.   minlenr3   _countr0   	add_image)r4   immetar6   disposer   r   r   _append_dataT   s    
zGIFFormat.Writer._append_data)r   Nr   r   r   F)r   r	   r
   r<   r?   rK   r   r   r   r   Writer   s         
3rL   N)r   r	   r
   __doc__r   r   r   rL   r   r   r   r   r      s   r   c                 C   s   | j dddS )Nr   little)	byteorder)to_bytes)r   r   r   r   intToBina   s    rQ   c                   @   sp   e Zd ZdZdddZdd Zd	d
 Zdd Zdd Zdd Z	dddZ
dd Zd ddZdd Zd!ddZdS )"r2   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                 C   sF   || _ || _|| _|| _|| _d | _d | _d| _ddlm	} || _	d S )Nr   )getdata)
r;   opt_subrectangleopt_loopopt_quantizeropt_palette_size_previous_image_global_paletterF   PIL.GifImagePluginrR   )r4   filerS   rT   rU   rV   rR   r   r   r   __init__k   s    zGifWriter.__init__c           	      C   s   |d }}| j r| |\}}| || j| j}ddlm} ||d d }| jdkrl| ||| j	 || _
| ||||| || _|  jd7  _d S )Nr   r   r   )	getheaderr@   r   )rS   getSubRectangleconverToPILrU   rV   rY   r]   rF   write_headerrT   rX   write_imagerW   )	r4   rH   r6   rJ   Zim_rectrectim_pilr]   paletter   r   r   rG      s    

zGifWriter.add_imagec                 C   s<   |  |}| |}| j| | j| | j| d S r   )getheaderAnim	getAppExtr;   write)r4   rH   ZglobalPaletter5   headerZappextr   r   r   r`      s
    

zGifWriter.write_headerc                 C   s   | j dd d S )N;utf-8)r;   rg   encoder>   r   r   r   r=      s    zGifWriter.closec                 C   s   | j }| |}d}|r4t|dk r4||d7 }qt|dksDt| ||}	| ||}
|| jksn|dkr||
 ||	 || |d n||
 || |D ]}|| qd S )N       r   r      )	r;   rR   rE   popAssertionErrorgetImageDescriptorgetGraphicsControlExtrX   rg   )r4   rH   rd   rb   r6   rJ   r;   dataZimdeslidZgraphextr!   r   r   r   ra      s"    





zGifWriter.write_imagec                 C   s4   d}|t |jd 7 }|t |jd 7 }|d7 }|S )z4Get animation header. To replace PILs getheader()[0]s   GIF89ar   r   s     rQ   size)r4   rH   bbr   r   r   re      s
    zGifWriter.getheaderAnimNc                 C   s`   |dkrd}d}|t |d 7 }|t |d 7 }|t |jd 7 }|t |jd 7 }|d7 }|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.
        Nr\      ,r   r      ru   )r4   rH   Zxyrw   r   r   r   rq      s    zGifWriter.getImageDescriptorc                 C   sP   |dkrdS |dkrd}d}|dkrLd}|d7 }|d7 }|t |7 }|d7 }|S )	z|Application extension. This part specifies the amount of loops.
        If loop is 0 or inf, it goes on infinitely.
        r   rl   r   i  s   !s   NETSCAPE2.0s       )rQ   )r4   r5   rw   r   r   r   rf      s    zGifWriter.getAppExt皙?r   c                 C   sJ   d}|t |d@ d> d7 }|tt|d d 7 }|d7 }|d7 }|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   !r@   r   rj   d   g      ?rz   )chrrk   rQ   r   )r4   r6   rJ   rw   r   r   r   rr      s    zGifWriter.getGraphicsControlExtc           
      C   s   | j dkr|dfS | j}t|| }|jdkr:|d}t|d}t|d}|jr|jrt|d t|d d  }}t|d t|d d  }}	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   r\   r@   r   r   rA   )r   r   )	rF   rW   r(   absrB   sumZargwhererv   r   )
r4   rH   prevdiffXYZx0x1Zy0y1r   r   r   r^     s    



 zGifWriter.getSubRectanglec                 C   s   t |d}|dkr:d}|d}t||}|j||d}nB|dkrp|dkrV|d}n
|d}|j||d	}ntd
| |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).
        Zgif)ZnqZneuquantr   ZRGBA)colors)r   r   r   r   RGB)r   methodzInvalid value for quantizer: %r)r   convertNeuQuantquantizer'   )r4   rH   r7   Zpalette_sizerc   Znq_samplefacZ
nqInstancer   r   r   r_   9  s    



zGifWriter.converToPIL)Tr   r   r   )N)r{   r   )r   )r   r	   r
   rM   r[   rG   r`   r=   ra   re   rq   rf   rr   r^   r_   r   r   r   r   r2   e   s        
	!

 r2   )rM   loggingnumpyr(   Zpillow_legacyr   r   r   	getLoggerr   r*   r   r   r   rQ   r2   r   r   r   r   <module>   s   
L