ó
Č4ec           @   s¼  d  d l  Z  d d l m Z m Z m Z m Z m Z d d l m Z m	 Z
 m Z m Z d Z d   Z d e j f d     YZ i d	 d
 6d	 d	 6d d 6Z e d  Z d   Z d   Z d   Z d   Z e d  Z d   Z d   Z d   Z e Z d   Z d   Z d   Z d   Z  d   Z! d   Z" d   Z# e$ e$ d  Z% d d f d  Z& e j' e j( e e  e j) e j( e  e j* e j( e  e j+ e j( d  e j, e j( d   d S(!   i’’’’Ni   (   t   Imaget
   ImageChopst	   ImageFilet   ImagePalettet   ImageSequence(   t   i8t   i16let   o8t   o16les   0.9c         C   s   |  d  d k S(   Ni   t   GIF87at   GIF89a(   R	   R
   (    (   t   prefix(    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyt   _accept)   s    t   GifImageFilec           B   s}   e  Z d  Z d Z e Z d Z d   Z d   Z	 e
 d    Z e
 d    Z d   Z d   Z d   Z d	   Z d
   Z RS(   t   GIFs   Compuserve GIFc         C   s>   |  j  j d  } | r: t |  r: |  j  j t |   Sd  S(   Ni   (   t   fpt   readR   t   None(   t   selft   s(    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyt   data:   s    c         C   s”  |  j  j d  } | d  d k r1 t d   n  | d  |  j d <t | d  t | d  f |  _ g  |  _ t | d  } | d	 @d
 } | d @r`t | d  |  j d <|  j  j d | > } x t d t	 |  d  D]} } | d t | |  k o.t | | d
  k o.t | | d  k n sÜ t
 j d |  } | |  _ |  _ PqÜ qÜ Wn  |  j  |  _ |  j  j   |  _ d  |  _ d  |  _ |  j d  d  S(   Ni   i   R	   R
   s   not a GIF filet   versioni   i
   i   i   i   i   t
   backgroundi   i    i   t   RGB(   R	   R
   (   R   R   t   SyntaxErrort   infot   i16t   _sizet   tileR   t   ranget   lenR   t   rawt   global_palettet   palettet   _GifImageFile__fpt   tellt   _GifImageFile__rewindR   t	   _n_framest   _is_animatedt   _seek(   R   R   t   flagst   bitst   pt   i(    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyt   _open@   s*    #	
Q		c         C   s   |  j  d  k rz |  j   } y( x! t rA |  j |  j   d  q! WWn$ t k
 ri |  j   d |  _  n X|  j |  n  |  j  S(   Ni   (   R%   R   R#   t   Truet   seekt   EOFError(   R   t   current(    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyt   n_frames^   s    	c         C   s   |  j  d  k r |  j d  k	 r3 |  j d k |  _  q |  j   } y |  j d  t |  _  Wn t k
 ru t |  _  n X|  j |  n  |  j  S(   Ni   (   R&   R   R%   R#   R.   R-   R/   t   False(   R   R0   (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyt   is_animatedj   s    c         C   sŗ   |  j  |  s d  S| |  j k  rJ | d k r: d  |  _ n  |  j d  n  |  j } x` t |  j d | d  D]D } y |  j |  Wqn t k
 r± |  j |  t d   qn Xqn Wd  S(   Ni    i   s   no more images in GIF file(   t   _seek_checkt   _GifImageFile__frameR   t   imR'   R   R/   R.   (   R   t   framet
   last_framet   f(    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyR.   {   s    	!c         C   s  | d k rd d |  _  d  |  _ d d d d g |  _ d |  _ |  j j |  j  d  |  _ d |  _	 n |  j
 sz |  j   n  | |  j d k r  t d |   n  | |  _ g  |  _ |  j |  _ |  j  rł |  j j |  j   x |  j   rģ qŻ Wd |  _  n  |  j r|  j
 j |  j |  j  n  d d l m } | |  j  |  _ i  } xst r»|  j j d  } | st| d k rxPqI| d k r(|  j j d  } |  j   } t |  d k r)t | d  } | d @råt | d	  | d
 <n  t | d d	 ! d | d <d | @} | d ?} | r| |  _	 qné t |  d k rx? | r|d | k rc| d c | 7<n
 | | d <|  j   } q>WqIn t |  d k r| |  j j   f | d <| d  d k r|  j   } t |  d	 k rt | d  d k rt | d d	 ! | d <qqn  x£|  j   r$qWqI| d k rI|  j j d  } t | d  t | d  } }	 | t | d  |	 t | d  }
 } |
 |  j d k s¶| |  j d k rčt |
 |  j d  t | |  j d  f |  _ n  | |	 |
 | f |  _ t | d  } | d @d k } | d @r]| d @d } t j d |  j j d	 | >  |  _ n  t |  j j d   } |  j j   |  _  d | |	 |
 | f |  j  | | f f g |  _ PqIqIWy« |  j	 d k  rŚd  |  _ ne |  j	 d k r!t j  |  j  t j! j" d  |  j |  j# d!  |  _ n |  j
 r?|  j
 j   |  _ n  |  j rf|  j$ |  j |  j  |  _ n  Wn t% t& f k
 rn X|  j st'  n  xY d
 d d d d g D]B } | | k rĻ| | |  j# | <q©| |  j# k r©|  j# | =q©q©Wd" |  _( |  j rd  |  _( n  d  S(#   Ni    i’’’’i   s   cannot seek to frame %d(   t   copyt   ;t   !ił   i   t   transparencyi
   t   durationi   i   iž   t   commenti’   t	   extensioni   s   NETSCAPE2.0t   loopt   ,i	   i   i   i   i@   i   i   R   t   gift   PR   t   L()   t   _GifImageFile__offsetR   t   disposet   dispose_extentR5   R"   R.   R$   t   _prev_imt   disposal_methodR6   t   loadt
   ValueErrorR   R   R   t   pasteR:   R    R!   R-   R   R   R   R#   R   t   sizet   maxR   R   R   R    t   _decompression_bomb_checkt   coret   fillR   t   _cropt   AttributeErrort   KeyErrorR/   t   mode(   R   R7   R:   R   R   t   blockR(   t   dispose_bitst   x0t   y0t   x1t   y1t	   interlaceR)   t   k(    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyR'      s¾    										


	
( !)&2
(-(		"				c         C   s   |  j  S(   N(   R5   (   R   (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyR#   '  s    c         C   s   t  j  j |   |  j rq |  j d k rq |  j |  j |  j  } |  j j | |  j | j d   |  j |  _ n  |  j j	   |  _ d  S(   Ni   t   RGBA(
   R   t   load_endRI   RJ   RS   R6   RH   RM   t   convertR:   (   R   t   updated(    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyR`   *  s    "c         C   sO   z> y& |  j  |  j k r( |  j  j   n  Wn t k
 r< n XWd  d  |  _  Xd  S(   N(   R"   R   t   closeRT   R   (   R   (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyt
   _close__fp7  s     N(   t   __name__t
   __module__t   formatt   format_descriptionR2   t!   _close_exclusive_fp_after_loadingR   R    R   R,   t   propertyR1   R3   R.   R'   R#   R`   Rd   (    (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyR   2   s   						RE   t   1RD   c         C   s£   |  j  t k r |  j   |  St j |  j   d k r | r d } |  j rj t |  j j   d  d } n  |  j d d t j	 d | S|  j d  Sn  |  j d  S(	   s  
    Takes an image (or frame), returns an image in a mode that is appropriate
    for saving in a Gif.

    It may return the original image, or it may return an image converted to
    palette or 'L' mode.

    UNDONE: What is the point of mucking with the initial call palette, for
    an image that shouldn't have a palette, or it would be a mode 'P' and
    get returned in the RAWMODE clause.

    :param im: Image object
    :param initial_call: Default false, set to true for a single frame.
    :returns: Image object
    R   i   i   i   RD   R!   t   colorsRE   (
   RV   t   RAWMODERK   R    t   getmodebaseR!   R   t   getdataRa   t   ADAPTIVE(   R6   t   initial_callt   palette_size(    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyt   _normalize_modeH  s    
	 c         C   s7  d } | r t | t t t f  r7 t | d   } n  t | t j  r t t j j t	 | j
 d  | j
 d d !| j
 d d !   } q n  |  j d k ræ | sü |  j j d  d  } qü n= | sä t d   t d  D  } n  t j d d | |  _
 t |  |  } | d k	 r'|  j | |  S| |  j
 _
 |  S(	   st  
    Normalizes the palette for image.
      - Sets the palette to the incoming palette, if provided.
      - Ensures that there's a palette for L mode images
      - Optimizes the palette if necessary/desired.

    :param im: Image object
    :param palette: bytes object containing the source palette, or ....
    :param info: encoderinfo
    :returns: Image object
    i   i   i   RD   R   c         s   s   |  ] } | d  Vq d S(   i   N(    (   t   .0R+   (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pys	   <genexpr>  s    R!   N(   R   t
   isinstancet   bytest	   bytearrayt   listR   t	   itertoolst   chaint   from_iterablet   zipR!   RV   R6   t
   getpaletteR   t   _get_optimizet   remap_palette(   R6   R!   R   t   source_palettet   used_palette_colors(    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyt   _normalize_palettef  s,    	
c         C   s	  t  |  t  } x0 | j j   D] \ } } |  j j | |  q Wt | | |  j  } x' t | |  j  D] } | j |  qj Wd } t	 |   r  | d B} n  t
 | |  d |  d t	 |   f | _ t j | | d d |  j d t | j f g  | j d  d  S(   Ni    i@   i   RC   s    (   i    i    (   i    i    (   Rs   R-   R   t   itemst   encoderinfot
   setdefaultR   t   _get_global_headert   writet   get_interlacet   _write_local_headert   encoderconfigR   t   _saveRN   Rm   RV   (   R6   R   R!   t   im_outR^   t   vR   R(   (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyt   _write_single_frame  s    0c         C   sĀ  |  j  j d |  j j d   } |  j  j d |  j j d   } g  } d } d  } xjt j |  g |  j  j d g    D]D} x;t j |  D]*}	 t |	 j	    }	 | d k ré x3 |	 j j
   D] \ }
 } |  j  j |
 |  qĆ Wn  t |	 | |  j   }	 |  j  j	   } t | t t f  r3| | | d <n  t | t t f  rY| | | d <n  | d 7} | r| d } | j d  d k rś| d  k rńt |  |  j  j d |  j j d    } t j d	 |	 j |  } | j | d d
 j  n  | } n
 | d
 } t |	  t |  k r1t j |	 |  } n$ t j |	 j d  | j d   } | j   } | s| r | d d c | d 7<q q qn d  } | j i |	 d
 6| d 6| d 6 q Wqy Wt |  d k rvx | D] } | d
 }	 | d s(x( t |	 | d  D] } | j |  qWd } n/ t | d d <|	 j  | d  }	 | d d  } t! | |	 | | d  qŚWt Sd |  j  k r¾t |  j  d t t f  r¾t" |  j  d  |  j  d <n  d  S(   NR>   t   disposali    t   append_imagesi   i’’’’i   R   RD   R6   R   R   t   bboxt   include_color_table(   i    i    (#   R   t   getR   R   Ry   Rz   R   t   IteratorRs   R:   R   R   R   Ru   Rx   t   tuplet   _get_backgroundR    t   newRN   t
   putpaletteR!   t   _get_palette_bytesR   t   subtract_moduloRa   t   getbboxt   appendR   R   R   R-   t   cropt   _write_frame_datat   sum(   R6   R   R!   R>   R   t	   im_framest   frame_countt   background_imt
   imSequencet   im_frameR^   R   R   t   previousR   t   base_imt   deltaR   t
   frame_dataR   t   offset(    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyt   _write_multiple_frames§  sn    !!+

$	
	*

	c         C   s   t  |  | | d t d  S(   Nt   save_all(   R   R-   (   R6   R   t   filename(    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyt	   _save_allō  s    c         C   s¾   d |  j  k s d |  j k rB |  j  j d |  j j d   } n" d  } |  j  j d t  |  j  d <| s~ t |  | |  r t |  | |  n  | j d  t | d  rŗ | j	   n  d  S(   NR!   t   optimizeR;   t   flush(
   R   R   R   R   R-   RŖ   R   R   t   hasattrRÆ   (   R6   R   R¬   R«   R!   (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyR   ų  s    $c         C   s7   |  j  j d d  } t |  j  d k  r3 d } n  | S(   NR]   i   i   i    (   R   R   t   minRN   (   R6   R]   (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyR   	  s    	c         C   sf  t  } y | j d } Wn t k
 r* na Xt |  } t } t | | j  } | d  k	 r y | j |  } Wq t k
 r t  } q Xn  d | j k r“ t | j d d  } n d } t | j j	 d d   } | sķ | d k sķ | rh| rł d n d }	 |	 | d >O}	 | sd } n  |  j
 d t d	  t d
  t |	  t |  t |  t d   n  d | j k rd t | j d  k r|  j
 d t d   x\ t d t | j d  d  D]; }
 | j d |
 |
 d !} |  j
 t t |   |  qĒW|  j
 t d   n  d | j k r| j d } |  j
 d t d  t d  d t d  t d  t |  t d   n  | j j	 d  } | rŲt |  } t |  } | rŲ| d B} | | B} qŲn  |  j
 d t | d  t | d  t | j d  t | j d  t |   | rO| rO|  j
 t |   n  |  j
 t d   d  S(   NR=   R>   i
   i    R   i   i   R<   ił   i   R?   iž   i’   RA   i   s   NETSCAPE2.0i   R   i   RB   i   (   R2   R   RU   t   intR-   R~   R   t   indexRL   R   R   R   t   o16R   R   R   t   _get_color_table_sizeRN   t   _get_header_palette(   R   R6   R©   R(   t   transparent_color_existsR=   R   R>   R   t   packed_flagR+   t   subblockt   number_of_loopsR   t   palette_bytest   color_table_size(    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyR     s\    	F(&!J
Oc         C   s  d d  l  } d d l m } m } m } m } |  j   } t | d  }	 |  j d k r t | j	 d  # }
 | d | g d |	 d |
 Wd  QXnĮ d d	 | g } d g } t | j	 d  @ }
 | | d | d |
 } | | d
 | j
 d |	 d |
 } Wd  QX| j
 j   | j   } | r2| | |   n  | j   } | rV| | |   n  Wd  QXy | j |  Wn t k
 rn Xd  S(   Ni’’’’(   t   Popent
   check_callt   PIPEt   CalledProcessErrort   wbR   t   ppmtogift   stdoutt   stderrt   ppmquantt   256t   stdin(   t   ost
   subprocessR½   R¾   Ræ   RĄ   t   _dumpt   openRV   t   devnullRĆ   Rc   t   waitt   unlinkt   OSError(   R6   R   R¬   RČ   R½   R¾   Ræ   RĄ   t   tempfileR9   RĢ   t	   quant_cmdt	   togif_cmdt
   quant_proct
   togif_proct   retcode(    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyt   _save_netpbmd  s0    	"%	$c         C   sÕ   |  j  d k rŃ | rŃ | j d d  rŃ t p9 |  j  d k } | sX |  j |  j d	 k  rŃ g  } x6 t |  j    D]" \ } } | rq | j |  qq qq W| sĒ t |  d k rĪ t	 |  t |  k rĪ | SqŃ n  d S(
   sL  
    Palette optimization is a potentially expensive operation.

    This function determines if the palette should be optimized using
    some heuristics, then returns the list of palette entries in use.

    :param im: Image object
    :param info: encoderinfo
    :returns: list of indexes of palette entries in use, or None
    RD   RE   R®   i    i   i   N(   RD   RE   i   (
   RV   R   t   _FORCE_OPTIMIZEt   widtht   heightt	   enumeratet	   histogramR   R   RO   (   R6   R   t   optimiseR   R+   t   count(    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyR~     s    'c         C   s]   d d  l  } |  s d St |   d k  r, d St | j | j t |   d d    d Sd  S(   Ni’’’’i    i	   i   i   i   (   t   mathR   R²   t   ceilt   log(   R»   RŽ   (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyRµ   ¼  s    c         C   sO   t  |   } d | >t |   d } | d k rK |  t d  d | 7}  n  |  S(   sä   
    Returns the palette, null padded to the next power of 2 (*3) bytes
    suitable for direct inclusion in the GIF header

    :param palette_bytes: Unpadded palette bytes, in RGBRGB form
    :returns: Null padded palette
    i   i   i    (   Rµ   R   R   (   R»   R¼   t   actual_target_size_diff(    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyR¶   Č  s
    c         C   s
   |  j  j  S(   s   
    Gets the palette for inclusion in the gif header

    :param im: Image object
    :returns: Bytes, len<=768 suitable for inclusion in gif header
    (   R!   (   R6   (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyR   Ś  s    c         C   s=   d } | r9 | } t  | t  r9 |  j j |  } q9 n  | S(   Ni    (   Ru   R   R!   t   getcolor(   R6   t   infoBackgroundR   (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyR   ä  s    c         C   s;  d } x¬ d d d d g D]w } | r | | k r | d k rM | | d k s | d k r d t  | |  k oz d k n r q n  d	 } Pq q W|  j j d
  d	 k rµ d	 } n  t |  | j d   } t |   } t |  } d | t |  j d  t |  j d  t | d  t |  t d  t	 |  g S(   s2   Return a list of strings representing a GIF headert   87aR=   R>   RA   R?   i    i   i’   t   89aR   R   R   i   (
   R   R   R   R   R   Rµ   R“   RN   R   R¶   (   R6   R   R   t   extensionKeyR   R»   R¼   (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyR   š  s"    3	)c         C   sk   z] | | _  t |  | | d  t j | |  d d | j d t | j f g  |  j d  Wd  | `  Xd  S(   Ni    RC   s    (   i    i    (   R   R   R   R   RN   Rm   RV   R   (   R   R¤   R©   t   params(    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyR     s    	*c         C   s   t  |  |  } | d k r$ i  } n  d | k rS d |  j k rS |  j d | d <n  t |  | |  } | j |  _ | j |  _ t |  |  } | | f S(   s  
    Legacy Method to get Gif data from image.

    Warning:: May modify image data.

    :param im: Image object
    :param palette: bytes object containing the source palette, or ....
    :param info: encoderinfo
    :returns: tuple of(list of header items, optimized palette)

    R   N(   R~   R   R   R   R!   R6   R   (   R6   R!   R   R   t   im_modt   header(    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyt	   getheader+  s    	i    c         K   sC   d t  f d     Y} |  j   |   } t | |  | |  | j S(   s  
    Legacy Method

    Return a list of strings representing this image.
    The first string is a local image header, the rest contains
    encoded image data.

    :param im: Image object
    :param offset: Tuple of (x, y) pixels. Defaults to (0,0)
    :param \**params: E.g. duration or other encoder info parameters
    :returns: List of Bytes containing gif encoded frame data

    t	   Collectorc           B   s   e  Z g  Z d    Z RS(   c         S   s   |  j  j |  d  S(   N(   R   R   (   R   R   (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyR   [  s    (   Re   Rf   R   R   (    (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyRė   X  s   (   t   objectRK   R   R   (   R6   R©   Rē   Rė   R   (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyRo   I  s
    
	s   .gifs	   image/gif(-   Ry   t    R    R   R   R   R   t   _binaryR   R   R   R   R   R“   t   __version__R   R   Rm   R2   Rs   R   R   RŖ   R­   R   R   R   RÖ   R×   R~   Rµ   R¶   R   R   R   R   R   Rź   Ro   t   register_openRg   t   register_savet   register_save_allt   register_extensiont   register_mime(    (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/GifImagePlugin.pyt   <module>   s>   ("		’ 	,		M		
	Q	0	%			
		'	!