
ܘ!Xc           @  s  d  d l  m Z d Z d  d l Z d  d l m Z m Z m Z m Z d  d l	 Z	 e j
 Z
 e j Z e j Z e j d  j Z d Z i dB dC 6dD dE 6dF dG 6dH dI 6dJ dK 6dL dM 6dN dO 6dP dQ 6dR dS 6dT dU 6dV dW 6dX dY 6dZ d[ 6d\ d] 6d^ d_ 6Z e j d  Z e j Z d  e Z d!   Z d" d` d#     YZ d$ e f d%     YZ d& da d'     YZ d( e f d)     YZ d*   Z d+ e j f d,     YZ e j  Z  e j! Z" e j# Z$ i db d 6dc d. 6dd d 6de d 6df d
 6dg d 6dh d 6di d 6dj d 6dk d 6dl d 6dm d 6dn d 6Z% d:   Z& d; do d<     YZ' e& d	 d=  Z( d>   Z) e j* d? e e  e j+ d? e(  e j, d? d@  e j- d? dA  d S(p   i(   t   print_functions   0.9N(   t   Imaget	   ImageFilet   ImagePalettet   _binarys   \w\w\w\ws   PNG

t   1i   i    t   Ls   L;2i   s   L;4i   i   t   Is   I;16Bi   t   RGBs   RGB;16Bt   Ps   P;1i   s   P;2s   P;4t   LAt   RGBAs   LA;16Bi   s   RGBA;16Bs   ^+ *$i@   c         C  s:   t  j   } | j |  t  } | j r6 t d   n  | S(   Ns   Decompressed Data Too Large(   t   zlibt   decompressobjt
   decompresst   MAX_TEXT_CHUNKt   unconsumed_tailt
   ValueError(   t   st   dobjt	   plaintext(    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt   _safe_zlib_decompressQ   s
    	t   ChunkStreamc           B  sS   e  Z d    Z d   Z d   Z d   Z d   Z d   Z d   Z d d  Z	 RS(	   c         C  s7   | |  _  g  |  _ t t j d  s3 |  j |  _ n  d  S(   Nt   crc32(   t   fpt   queuet   hasattrR   t   coret   crc_skipt   crc(   t   selfR   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt   __init__^   s    		c         C  s   |  j  r< |  j  d \ } } } |  j  d =|  j j |  n7 |  j j d  } | d } |  j j   } t |  } t |  s t d t |    n  | | | f S(   s.   Fetch a new chunk. Returns header information.ii   i   s   broken PNG file (chunk %s)(	   R   R   t   seekt   readt   tellt   i32t   is_cidt   SyntaxErrort   repr(   R   t   cidt   post   lengthR   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyR!   f   s    	

c         C  s   d  |  _ |  _ |  _ d  S(   N(   t   NoneR   R   R   (   R   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt   closex   s    c         C  s   |  j  j | | | f  d  S(   N(   R   t   append(   R   R'   R(   R)   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt   push{   s    c         C  sB   t  j r t d | | |  n  t |  d | j d   | |  S(   s"   Call the appropriate chunk handlert   STREAMt   chunk_t   ascii(   R   t   DEBUGt   printt   getattrt   decode(   R   R'   R(   R)   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt   call   s    	c         C  st   t  j j | t  j j |   } t |  j j d   t |  j j d   f } | | k rp t d |   n  d S(   s   Read and verify checksumi   s*   broken PNG file(bad header checksum in %s)N(   R   R   R   t   i16R   R!   R%   (   R   R'   t   datat   crc1t   crc2(    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyR      s
    !0c         C  s   |  j  j d  d S(   s3   Read checksum.  Used if the C module is not presenti   N(   R   R!   (   R   R'   R7   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyR      s    t   IENDc         C  sh   g  } x[ t  rc |  j   \ } } } | | k r4 Pn  |  j | t j |  j |   | j |  q	 W| S(   N(   t   TrueR!   R   R   t
   _safe_readR   R,   (   R   t   endchunkt   cidsR'   R(   R)   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt   verify   s    	(
   t   __name__t
   __module__R   R!   R+   R-   R5   R   R   R?   (    (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyR   \   s   								t   iTXtc           B  s   e  Z e d     Z RS(   c         C  s(   t  j |  |  } | | _ | | _ | S(   N(   t   strt   __new__t   langt   tkey(   t   clst   textRE   RF   R   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyRD      s    		(   R@   RA   t   staticmethodRD   (    (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyRB      s   t   PngInfoc           B  s8   e  Z d    Z d   Z d d e d  Z d d  Z RS(   c         C  s   g  |  _  d  S(   N(   t   chunks(   R   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyR      s    c         C  s   |  j  j | | f  d  S(   N(   RK   R,   (   R   R'   R7   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt   add   s    t    c         C  s  t  | t  s$ | j d d  } n  t  | t  sH | j d d  } n  t  | t  sl | j d d  } n  t  | t  s | j d d  } n  | r d d  l } |  j d | d | d | d | j |   n( |  j d | d | d | d |  d  S(	   Ns   latin-1t   stricts   utf-8iRB   t     t    t      (   t
   isinstancet   bytest   encodeR   RL   t   compress(   R   t   keyt   valueRE   RF   t   zipR   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt   add_itxt   s      i    c         C  s   t  | t  r4 |  j | | | j | j t |   St  | t  s y | j d d  } Wq t k
 r |  j | | d t |  SXn  t  | t  s | j d d  } n  | r d d  l	 } |  j
 d | d | j |   n |  j
 d | d |  d  S(	   Ns   latin-1RN   RX   it   zTXtt     t   tEXtRP   (   RR   RB   RY   RE   RF   t   boolRS   RT   t   UnicodeErrorR   RL   RU   (   R   RV   RW   RX   R   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt   add_text   s    % $(   R@   RA   R   RL   t   FalseRY   R_   (    (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyRJ      s   		t	   PngStreamc           B  s}   e  Z d    Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z d   Z d   Z RS(   c         C  sS   t  j |  |  i  |  _ i  |  _ d |  _ d  |  _ d  |  _ d  |  _ d |  _	 d  S(   Ni    (   i    i    (
   R   R   t   im_infot   im_textt   im_sizeR*   t   im_modet   im_tilet
   im_palettet   text_memory(   R   R   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyR      s    						c         C  s8   |  j  | 7_  |  j  t k r4 t d |  j    n  d  S(   Ns7   Too much memory used in text chunks: %s>MAX_TEXT_MEMORY(   Rh   t   MAX_TEXT_MEMORYR   (   R   t   chunklen(    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt   check_text_memory   s    c         C  s   t  j |  j |  } | j d  } t j rX t d | |   t d t | |   n  t | |  } | d k r t d |   n  y t	 | | d  } Wn t
 j k
 r d  } n X| |  j d <| S(   NRP   s   iCCP profile names   Compression methodi    s+   Unknown compression method %s in iCCP chunki   t   icc_profile(   R   R<   R   t   findR   R1   R2   t   i8R%   R   R   t   errorR*   Rb   (   R   R(   R)   R   t   it   comp_methodRl   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt
   chunk_iCCP   s    	
c         C  s   t  j |  j |  } t |  t | d  f |  _ y4 t t | d  t | d  f \ |  _ |  _ Wn n Xt | d  r d |  j	 d <n  t | d  r t
 d   n  | S(	   Ni   i   i	   i   i   t	   interlacei   s   unknown filter category(   R   R<   R   R#   Rd   t   _MODESRn   Re   t
   im_rawmodeRb   R%   (   R   R(   R)   R   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt
   chunk_IHDR  s    4c         C  s5   d d |  j  | |  j f g |  _ | |  _ t  d  S(   NRX   i    (   i    i    (   Rd   Ru   Rf   t   im_idatt   EOFError(   R   R(   R)   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt
   chunk_IDAT$  s    "	c         C  s
   t   d  S(   N(   Rx   (   R   R(   R)   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt
   chunk_IEND+  s    c         C  s:   t  j |  j |  } |  j d k r6 d | f |  _ n  | S(   NR	   R   (   R   R<   R   Re   Rg   (   R   R(   R)   R   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt
   chunk_PLTE0  s    c         C  s   t  j |  j |  } |  j d k rq t j |  ra | j d  } | d k rn | |  j d <qn q | |  j d <ng |  j d k r t |  |  j d <nB |  j d k r t |  t | d  t | d  f |  j d <n  | S(	   NR	   RP   i    t   transparencyR   R   i   i   (	   R   R<   R   Re   t   _simple_palettet   matchRm   Rb   R6   (   R   R(   R)   R   Rp   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt
   chunk_tRNS8  s    3c         C  s0   t  j |  j |  } t |  d |  j d <| S(   Ng     j@t   gamma(   R   R<   R   R#   Rb   (   R   R(   R)   R   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt
   chunk_gAMAI  s    c         C  s   t  j |  j |  } t |  t | d  } } t | d  } | d k r t | d d  t | d d  f } | |  j d <n" | d k r | | f |  j d <n  | S(	   Ni   i   i   g
F%u?g      ?t   dpii    t   aspect(   R   R<   R   R#   Rn   t   intRb   (   R   R(   R)   R   t   pxt   pyt   unitR   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt
   chunk_pHYsP  s    (c         C  s   t  j |  j |  } y | j d d  \ } } Wn t k
 rP | } d } n X| r t t k	 r | j d d  } | j d d  } n  | |  j | <|  j	 | <|  j
 t |   n  | S(   NRP   i   RM   s   latin-1RN   t   replace(   R   R<   R   t   splitR   RS   RC   R4   Rb   Rc   Rk   t   len(   R   R(   R)   R   t   kt   v(    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt
   chunk_tEXt]  s    
c         C  s7  t  j |  j |  } y | j d d  \ } } Wn t k
 rP | } d } n X| rj t | d  } n d } | d k r t d |   n  d d  l } y t | d  } Wn | j	 k
 r d } n X| r3t
 t k	 r| j d d  } | j d d	  } n  | |  j | <|  j | <|  j t |   n  | S(
   NRP   i   RM   i    s+   Unknown compression method %s in zTXt chunkis   latin-1RN   R   (   R   R<   R   R   R   Rn   R%   R   R   Ro   RS   RC   R4   Rb   Rc   Rk   R   (   R   R(   R)   R   R   R   Rq   R   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt
   chunk_zTXtq  s0    

c         C  s  t  j |  j |  } } y | j d d  \ } } Wn t k
 rI | SXt |  d k  r` | St | d  t | d  | d } } } y | j d d  \ } }	 }
 Wn t k
 r | SX| d k r| d k rd d  l } y t |
  }
 Wq| j	 k
 r| SXq| Sn  t
 t k	 ryL | j d d  } | j d d  } |	 j d d  }	 |
 j d d  }
 Wqt k
 r| SXn  t |
 | |	  |  j | <|  j | <|  j t |
   | S(	   NRP   i   i   i    is   latin-1RN   s   utf-8(   R   R<   R   R   R   R   Rn   R   R   Ro   RS   RC   R4   R^   RB   Rb   Rc   Rk   (   R   R(   R)   t   rR   R   t   cft   cmRE   t   tkR   R   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt
   chunk_iTXt  s>    ,$(   R@   RA   R   Rk   Rr   Rv   Ry   Rz   R{   R   R   R   R   R   R   (    (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyRa      s   												 c         C  s   |  d  t  k S(   Ni   (   t   _MAGIC(   t   prefix(    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt   _accept  s    t   PngImageFilec           B  sA   e  Z d  Z d Z d   Z d   Z d   Z d   Z d   Z RS(   t   PNGs   Portable network graphicsc         C  st  |  j  j d  t k r' t d   n  t |  j   |  _ x t r |  j j   \ } } } y |  j j | | |  } WnV t k
 r PnE t	 k
 r t
 j r t | | | d  n  t j |  j  |  } n X|  j j | |  q< W|  j j |  _ |  j j |  _ |  j j |  _ |  j j |  _ |  j j |  _ |  j j rg|  j j \ } } t j | |  |  _ n  | |  _ d  S(   Ni   s   not a PNG files	   (unknown)(   R   R!   R   R%   Ra   t   pngR;   R5   Rx   t   AttributeErrorR   R1   R2   R   R<   R   Re   t   modeRd   t   sizeRb   t   infoRc   RH   Rf   t   tileRg   R   t   rawt   palettet   _PngImageFile__idat(   R   R'   R(   R)   R   t   rawmodeR7   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt   _open  s.    			c         C  sd   |  j  d k r t d   n  |  j  j |  j d d d  |  j j   |  j j   d |  _  d S(   s   Verify PNG files)   verify must be called directly after openi    i   i   N(   R   R*   t   RuntimeErrorR    R   R   R?   R+   (   R   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyR?     s    c         C  s9   |  j  j d  r% |  j d |  _ n  t j j |   d S(   s"   internal: prepare to read PNG fileRs   i   N(   i   (   R   t   gett   decoderconfigR   t   load_prepare(   R   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyR     s    c         C  s   xj |  j  d k rl |  j j d  |  j j   \ } } } | d k r` |  j j | | |  d S| |  _  q W| d k r |  j  } n t | |  j   } |  j  | |  _  |  j j |  S(   s   internal: read more image datai    i   t   IDATt   DDATRM   (   R   R   (   R   R   R!   R   R-   t   min(   R   t
   read_bytesR'   R(   R)   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt	   load_read  s    c         C  s   |  j  j   d |  _  d S(   s%   internal: finished reading image dataN(   R   R+   R*   (   R   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt   load_end&  s    (	   R@   RA   t   formatt   format_descriptionR   R?   R   R   R   (    (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyR     s   	/			s    s   L;1s    s    s    s   s    s   s   s   s   s   s   c         G  s   d j  |  } |  j t t |   |  |  j |  t j j | t j j |   \ } } |  j t |  t |   d S(   s'   Write a PNG chunk (including CRC field)RM   N(   t   joint   writet   o32R   R   R   R   t   o16(   R   R'   R7   t   hit   lo(    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt   putchunkF  s
    't   _idatc           B  s   e  Z d    Z d   Z RS(   c         C  s   | |  _  | |  _ d  S(   N(   R   t   chunk(   R   R   R   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyR   T  s    	c         C  s   |  j  |  j d |  d  S(   NR   (   R   R   (   R   R7   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyR   X  s    (   R@   RA   R   R   (    (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyR   Q  s   	c      	   C  s7  |  j  } | d k r d |  j k r8 d |  j d >} nA |  j rs t t t |  j j   d  d d  d  } n d } | d k r d } n0 | d k r d } n | d k r d } n d	 } | d	 k r d
 | | f } q n  d |  j k r |  j d } n d } d |  j k |  j j d d  |  j j d d  | f |  _ y t	 | \ }	 } Wn! t
 k
 rxt d |   n X| r| S| j t  | | d t |  j d  t |  j d  | d d d  |  j  d k r2d | d }
 |  j j d  |
  } x  t |  |
 k  r| d 7} qW| | d |  n  |  j j d |  j j d d    } | sh| d k r|  j  d k rd | } t | t  r| | d | |   qt d t d |   } d | d } | | d | |   q|  j  d k r$t d t d |   } | | d t |   q|  j  d k ro| \ } } } | | d t |  t |  t |   qd |  j k rt d   qnZ |  j  d k r|  j j   d k r|  j j d d  } d | } | | d | |   n  |  j j d   } | rK| | d! t t | d d" d#   t t | d d" d#   d$  n  |  j j d%  } | rx* | j D] \ } } | | | |  qmWn  |  j j d&  rd' } | d( t j |  j d&  } | | d) |  n  t j |  t | |  d* d, |  j d |	 f g  | | d+ d  y | j   Wn n Xd  S(-   NR	   t   bitsi   i   i   i   i   i   i   s   %s;%dt
   dictionaryRM   t   optimizet   compress_levelit   compress_types   cannot write mode %s as PNGt   IHDRi    RP   R   t   PLTER|   t   tRNSi   s   R   i  s%   cannot use transparency for this modeR   t   AR   t   pHYsg
F%u?g      ?s   t   pnginfoRl   s   ICC ProfileR[   t   iCCPRX   R:   (   i    i    (    R   t   encoderinfoR   t   maxR   R   t   getdataR   t   encoderconfigt	   _OUTMODESt   KeyErrort   IOErrorR   R   R   R   t   imt
   getpaletteR   R*   RR   RS   R   t   getpalettemodeR   RK   R   RU   R   t   _saveR   t   flush(   R   R   t   filenameR   t   checkR   t   colorsR   R   R   t   palette_byte_numbert   palette_bytesR|   t   alpha_bytest   alphat   redt   greent   blueR   R   R'   R7   t   name(    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyR   \  s    		2				 
-$
	
c         K  sV   d d d     Y} d   } |   } z  | |  _  t |  | d |  Wd |  `  X| j S(   s4   Return a list of PNG chunks representing this image.t	   collectorc           B  s    e  Z g  Z d    Z d   Z RS(   c         S  s   d  S(   N(    (   R   R7   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyR     s    c         S  s   |  j  j |  d  S(   N(   R7   R,   (   R   R   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyR,     s    (   R@   RA   R7   R   R,   (    (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyR     s   	c         W  sf   d j  |  } t j j | t j j |   \ } } t |  t |  } |  j | | | f  d  S(   NRM   (   R   R   R   R   R   R,   (   R   R'   R7   R   R   R   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyR,     s    'N(    (   R   R   R*   R7   (   R   t   paramsR   R,   R   (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt	   getchunks  s    				R   s   .pngs	   image/png(   R   R   (   i   i    (   R   s   L;2(   i   i    (   R   s   L;4(   i   i    (   R   R   (   i   i    (   R   s   I;16B(   i   i    (   s   RGBs   RGB(   i   i   (   s   RGBs   RGB;16B(   i   i   (   R	   s   P;1(   i   i   (   R	   s   P;2(   i   i   (   R	   s   P;4(   i   i   (   R	   R	   (   i   i   (   R
   R
   (   i   i   (   s   RGBAs   LA;16B(   i   i   (   s   RGBAs   RGBA(   i   i   (   s   RGBAs   RGBA;16B(   i   i   (    (    (   R   s    (   s   L;1s    (   s   L;2s    (   s   L;4s    (   R   s    (   R
   s   (   s   I;16Bs    (   s   P;1s   (   s   P;2s   (   s   P;4s   (   R	   s   (   s   RGBs   (   s   RGBAs   (    (.   t
   __future__R    t   __version__t   ret   PILR   R   R   R   R   Rn   t   i16beR6   t   i32beR#   t   compileR~   R$   R   Rt   R}   t	   SAFEBLOCKR   Ri   R   R   RC   RB   RJ   Ra   R   R   t   o8t   o16beR   t   o32beR   R   R   R   R   R   t   register_opent   register_savet   register_extensiont   register_mime(    (    (    s6   /usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.pyt   <module>"   sx   "			
	
	M2	n			
		 