ó
;c›]c           @   sÂ   d  Z  d d l Z d d l Z d d l Z d d l m Z m Z m Z e j	 e
 ƒ Z d Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ e j d k r¢ d
 „  Z n	 d „  Z d d d „  ƒ  YZ d S(   s"   
PIL formats for multiple images.
iÿÿÿÿNi   (   t   PillowFormatt   ndarray_to_pilt   image_as_uintt
   TIFFFormatc           B   s   e  Z d  Z d Z RS(   t   is   TIFF format (Pillow)(   t   __name__t
   __module__t   _modest   _description(    (    (    s:   lib/python2.7/site-packages/imageio/plugins/pillowmulti.pyR      s   t	   GIFFormatc           B   sL   e  Z d  Z d Z d Z d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ RS(   sØ   A format for reading and writing static and animated GIF, based
    on Pillow.
    
    Images read with this format are always RGBA. Currently,
    the alpha channel is ignored when saving RGB images with this
    format.
    
    Parameters for reading
    ----------------------
    None
    
    Parameters for saving
    ---------------------
    loop : int
        The number of iterations. Default 0 (meaning loop indefinitely).
    duration : {float, list}
        The duration (in seconds) of each frame. Either specify one value
        that is used for all frames, or one value for each frame.
        Note that in the GIF format the duration/delay is expressed in
        hundredths of a second, which limits the precision of the duration.
    fps : float
        The number of frames per second. If duration is not given, the
        duration for each frame is set to 1/fps. Default 10.
    palettesize : int
        The number of colors to quantize the image to. Is rounded to
        the nearest power of two. Default 256.
    subrectangles : bool
        If True, will try and optimize the GIF by storing only the
        rectangular parts of each frame that change with respect to the
        previous. Default False.
    t   iIs    Static and animated gif (Pillow)t   Readerc           B   s   e  Z d d  „ Z RS(   c         C   s   t  j j |  ƒ S(   N(   R    R   t   _open(   t   selft   playback(    (    s:   lib/python2.7/site-packages/imageio/plugins/pillowmulti.pyR   <   s    N(   R   R   t   NoneR   (    (    (    s:   lib/python2.7/site-packages/imageio/plugins/pillowmulti.pyR   ;   s   t   Writerc           B   s5   e  Z d  d d d d  e d „ Z d „  Z d „  Z RS(   i    i
   i   c   	   	   C   so  t  | ƒ } | d k  s$ | d k r3 t d ƒ ‚ n  | d k rp d t  t j d	 ƒ d
 ƒ } t j d | ƒ n  | d  k r’ d t | ƒ |  _ nI t	 | t
 t f ƒ rÌ g  | D] } t | ƒ ^ q® |  _ n t | ƒ |  _ t | ƒ } | d k s| t d ƒ k rd } n  t  | ƒ } t | ƒ } | r2d n d |  _ |  j j ƒ  } t | | | | t  | ƒ ƒ |  _ d  S(   Ni   i   s!   GIF quantize param must be 2..256i   i   i   i    i@   i€   g+‡ÙÎ÷ï?sD   Warning: palettesize (%r) modified to a factor of two between 2-256.g      ð?i    t   infi   (   i   i   i   i   i    i@   i€   i   (   t   intt
   ValueErrort   npt   log2t   loggert   warningR   t   floatt	   _durationt
   isinstancet   listt   tuplet   boolt   _disposet   requestt   get_filet	   GifWritert   _writer(	   R   t   loopt   durationt   fpst   palettesizet	   quantizert   subrectanglest   dt   fp(    (    s:   lib/python2.7/site-packages/imageio/plugins/pillowmulti.pyR   @   s,    %	c         C   s   |  j  j ƒ  d  S(   N(   R"   t   close(   R   (    (    s:   lib/python2.7/site-packages/imageio/plugins/pillowmulti.pyt   _closej   s    c         C   s·   t  | d d ƒ} | j d k rV | j d d k rV | d  d  … d  d  … d f } n  |  j } t | t ƒ r” | t t | ƒ d |  j j	 ƒ } n  |  j
 } |  j j | | | ƒ d  S(   Nt   bitdepthi   i   iÿÿÿÿi   i    (   R   t   ndimt   shapeR   R   R   t   mint   lenR"   t   _countR   t	   add_image(   R   t   imt   metaR$   t   dispose(    (    s:   lib/python2.7/site-packages/imageio/plugins/pillowmulti.pyt   _append_datam   s    ""	&	N(   R   R   R   t   FalseR   R,   R7   (    (    (    s:   lib/python2.7/site-packages/imageio/plugins/pillowmulti.pyR   ?   s   #	(   R   R   t   __doc__R   R   R    R   R   (    (    (    s:   lib/python2.7/site-packages/imageio/plugins/pillowmulti.pyR	      s
   i   c         C   s   |  j  d d d ƒS(   Ni   t	   byteordert   little(   t   to_bytes(   R   (    (    s:   lib/python2.7/site-packages/imageio/plugins/pillowmulti.pyt   <lambda>{   t    c         C   s.   |  d } t  |  d ƒ } t | ƒ t | ƒ S(   s   Integer to two bytesi   (   R   t   chr(   R   t   i1t   i2(    (    s:   lib/python2.7/site-packages/imageio/plugins/pillowmulti.pyt   intToBin~   s    
R!   c           B   s‰   e  Z d  Z e d d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d d	 „ Z d
 „  Z d d d „ Z d „  Z d d „ Z RS(   s´    Class 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.
    i    i   c         C   se   | |  _  | |  _ | |  _ | |  _ | |  _ d  |  _ d  |  _ d |  _ d d l	 m
 } | |  _
 d  S(   Ni    iÿÿÿÿ(   t   getdata(   R*   t   opt_subrectanglet   opt_loopt   opt_quantizert   opt_palette_sizeR   t   _previous_imaget   _global_paletteR2   t   PIL.GifImagePluginRC   (   R   t   fileRD   RE   RF   RG   RC   (    (    s:   lib/python2.7/site-packages/imageio/plugins/pillowmulti.pyt   __init__Œ   s    								c   	      C   sÓ   | d } } |  j  r. |  j | ƒ \ } } n  |  j | |  j |  j ƒ } d d l m } | | ƒ d d } |  j d k rž |  j | | |  j	 ƒ | |  _
 n  |  j | | | | | ƒ | |  _ |  j d 7_ d  S(   Ni    iÿÿÿÿ(   t	   getheaderi   i   (   i    i    (   RD   t   getSubRectanglet   converToPILRF   RG   RJ   RM   R2   t   write_headerRE   RI   t   write_imageRH   (	   R   R4   R$   R6   t   im_rectt   rectt   im_pilRM   t   palette(    (    s:   lib/python2.7/site-packages/imageio/plugins/pillowmulti.pyR3   £   s    		c         C   sR   |  j  | ƒ } |  j | ƒ } |  j j | ƒ |  j j | ƒ |  j j | ƒ d  S(   N(   t   getheaderAnimt	   getAppExtR*   t   write(   R   R4   t   globalPaletteR#   t   headert   appext(    (    s:   lib/python2.7/site-packages/imageio/plugins/pillowmulti.pyRP   ½   s
    c         C   s   |  j  j d j d ƒ ƒ d  S(   Nt   ;s   utf-8(   R*   RX   t   encode(   R   (    (    s:   lib/python2.7/site-packages/imageio/plugins/pillowmulti.pyR+   Æ   s    c         C   s  |  j  } |  j | ƒ } d } x/ | rO t | ƒ d k  rO | | j d ƒ 7} q! Wt | ƒ d k sh t ‚ |  j | | ƒ }	 |  j | | ƒ }
 | |  j k s§ | d k rÞ | j |
 ƒ | j |	 ƒ | j | ƒ | j d ƒ n | j |
 ƒ | j | ƒ x | D] } | j | ƒ qÿ Wd  S(   NR>   i   i    i   s   (	   R*   RC   R1   t   popt   AssertionErrort   getImageDescriptort   getGraphicsControlExtRI   RX   (   R   R4   RU   RS   R$   R6   R*   t   datat   imdest   lidt   graphextR)   (    (    s:   lib/python2.7/site-packages/imageio/plugins/pillowmulti.pyRQ   É   s"    	c         C   sB   d } | t  | j d ƒ 7} | t  | j d ƒ 7} | d 7} | S(   s>    Get animation header. To replace PILs getheader()[0]
        t   GIF89ai    i   s   ‡  (   RB   t   size(   R   R4   t   bb(    (    s:   lib/python2.7/site-packages/imageio/plugins/pillowmulti.pyRV   ë   s
    
c         C   s   | d k r d } n  d } | t | d ƒ 7} | t | d ƒ 7} | t | j d ƒ 7} | t | j d ƒ 7} | d 7} | S(   sŒ   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.
        i    s   ,i   s   ‡N(   i    i    (   R   RB   Rg   (   R   R4   t   xyRh   (    (    s:   lib/python2.7/site-packages/imageio/plugins/pillowmulti.pyR`   ô   s    	
c         C   sr   | d k r d S| d k r% d } n  d } | d k rn d } | d 7} | d 7} | t  | ƒ 7} | d	 7} n  | S(   s}    Application extension. This part specifies the amount of loops.
        If loop is 0 or inf, it goes on infinitely.
        i   R>   i    i   i   s   !ÿs   NETSCAPE2.0s   s    i   iÿÿ  (   RB   (   R   R#   Rh   (    (    s:   lib/python2.7/site-packages/imageio/plugins/pillowmulti.pyRW     s    	

gš™™™™™¹?i   c         C   s]   d } | t  | d @d >ƒ j d ƒ 7} | t t | d d ƒ ƒ 7} | d 7} | d 7} | S(   sc   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   !ùi   i   s   utf-8id   g      à?s    (   R?   R]   RB   R   (   R   R$   R6   Rh   (    (    s:   lib/python2.7/site-packages/imageio/plugins/pillowmulti.pyRa   #  s    !

c   
      C   s#  |  j  d k r | d f S|  j } t j | | ƒ } | j d k rV | j d ƒ } n  t j | j d ƒ ƒ } t j | j d ƒ ƒ } | j rå | j rå t | d ƒ t | d d ƒ } } t | d ƒ t | d d ƒ } }	 n d \ } } d \ } }	 | | |	 … | | … f | | f f S(	   ss   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.
        i    i   i   i   iÿÿÿÿ(   i    i    (   i    i   (   i    i   (	   R2   RH   R   t   absR.   t   sumt   argwhereRg   R   (
   R   R4   t   prevt   difft   Xt   Yt   x0t   x1t   y0t   y1(    (    s:   lib/python2.7/site-packages/imageio/plugins/pillowmulti.pyRN   =  s    

	%(c         C   s¿   t  | d ƒ } | d k rW d } | j d ƒ } t | | ƒ } | j | d | ƒ} nd | d k r« | d	 k r | j d ƒ } n | j d
 ƒ } | j d | d | ƒ } n t d | ƒ ‚ | S(   s  Convert 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).
        t   gift   nqt   neuquanti
   t   RGBAt   colorsi    i   i   t   RGBt   methods   Invalid value for quantizer: %r(   Rv   Rw   (   i    i   i   (   R   t   convertt   NeuQuantt   quantizeR   (   R   R4   R'   t   palette_sizeRT   t   nq_samplefact
   nqInstance(    (    s:   lib/python2.7/site-packages/imageio/plugins/pillowmulti.pyRO   ]  s    N(   R   R   R9   t   TrueRL   R3   RP   R+   RQ   RV   R   R`   RW   Ra   RN   RO   (    (    (    s:   lib/python2.7/site-packages/imageio/plugins/pillowmulti.pyR!   †   s   					"				 (   i   (    (   R9   t   syst   loggingt   numpyR   t   pillowR    R   R   t	   getLoggerR   R   R   R}   R   R	   t   version_infoRB   R!   (    (    (    s:   lib/python2.7/site-packages/imageio/plugins/pillowmulti.pyt   <module>   s   c	