o
    9ήc ,                     @   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ndarray_to_pilimage_as_uintc                   @   s   e Zd ZdZdZdS )
TIFFFormatizTIFF format (Pillow)N)__name__
__module____qualname___modes_description r   r   B/tmp/pip-target-vg8gfxp4/lib/python/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`iIz Static and animated gif (Pillow)c                   @   s2   e Zd Z						dddZdd	 Zd
d ZdS )zGIFFormat.Writerr   N
      Fc                 C   s   t |}|dk s|dkrtd|dvr&dt tdd  }td|  |d u r2dt| | _nt|t	t
frBd	d
 |D | _nt|| _t|}|dksU|tdkrWd}t |}t|}|rcd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                 S   s   g | ]}t |qS r   )float).0dr   r   r   
<listcomp>7   s    z*GIFFormat.Writer._open.<locals>.<listcomp>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fpr   r   r   _open   s2   


zGIFFormat.Writer._openc                 C   s   | j   d S N)r.   closer/   r   r   r   _closeI      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r2|tt|d | jj	 }| j
}| j||| d S )Nr   )bitdepth   r   r   )r   ndimshaper%   r&   r'   minlenr.   _countr*   	add_image)r/   immetar1   disposer   r   r   _append_dataL   s   
zGIFFormat.Writer._append_data)r   Nr   r   r   F)r   r	   r
   r7   r;   rI   r   r   r   r   Writer   s    
*rJ   N)r   r	   r
   __doc__r   r   r   rJ   r   r   r   r   r      s
    r   c                 C   s   | j dddS )Nr   little)	byteorder)to_bytes)r   r   r   r   intToBinY   r<   rO   c                   @   sx   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 )"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                 C   sF   || _ || _|| _|| _|| _d | _d | _d| _ddlm	} || _	d S )Nr   )getdata)
r6   opt_subrectangleopt_loopopt_quantizeropt_palette_size_previous_image_global_paletterD   PIL.GifImagePluginrP   )r/   filerQ   rR   rS   rT   rP   r   r   r   __init__c   s   
zGifWriter.__init__c           	      C   s   |d}}| j r| |\}}| || j| j}ddlm} ||d d }| jdkr6| ||| j	 || _
| ||||| || _|  jd7  _d S )Nr   r   r   )	getheaderr>   r   )rQ   getSubRectangleconverToPILrS   rT   rW   r[   rD   write_headerrR   rV   write_imagerU   )	r/   rF   r1   rH   im_rectrectim_pilr[   paletter   r   r   rE   z   s   

zGifWriter.add_imagec                 C   s<   |  |}| |}| j| | j| | j| d S r8   )getheaderAnim	getAppExtr6   write)r/   rF   globalPaletter0   headerappextr   r   r   r^      s
   

zGifWriter.write_headerc                 C   s   | j dd d S )N;utf-8)r6   rf   encoder:   r   r   r   r9      s   zGifWriter.closec                 C   s   | j }| |}d}|r!t|dk r!||d7 }|r!t|dk st|dks)J | ||}	| ||}
|| jks>|dkrS||
 ||	 || |d n
||
 || |D ]}|| q_d S )N       r   r      )r6   rP   rC   popgetImageDescriptorgetGraphicsControlExtrV   rf   )r/   rF   rc   ra   r1   rH   r6   dataimdeslidgraphextr   r   r   r   r_      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     rO   size)r/   rF   bbr   r   r   rd      s
   zGifWriter.getheaderAnimNc                 C   s`   |du 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.
        NrZ      ,r   r      rw   )r/   rF   xyry   r   r   r   rq      s   zGifWriter.getImageDescriptorc                 C   sP   |dkrdS |dkrd}d}|dkr&d}|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   rm   r   i  s   !s   NETSCAPE2.0s       )rO   )r/   r0   ry   r   r   r   re      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   rk   d   g      ?r}   )chrrl   rO   r   )r/   r1   rH   ry   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rR|jrRt|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   rZ   r>   r   r   r?   )r   r   )	rD   rU   r!   absr@   sumargwhererx   r   )
r/   rF   prevdiffXYx0x1y0y1r   r   r   r\     s   



 zGifWriter.getSubRectanglec                 C   s   t |d}|dv rd}|d}t||}|j||d}|S |dv r:|dkr,|d}n|d}|j||d	}|S td
| )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    )r/   rF   r4   palette_sizerb   nq_samplefac
nqInstancer   r   r   r]   3  s   




zGifWriter.converToPIL)Tr   r   r   r8   )r~   r   )r   )r   r	   r
   rK   rY   rE   r^   r9   r_   rd   rq   re   rr   r\   r]   r   r   r   r   r-   ]   s"    
	"

 r-   )rK   loggingnumpyr!   pillow_legacyr   r   r   	getLoggerr   r#   r   r   r   rO   r-   r   r   r   r   <module>   s    
C