ó
î Xc           @   sø   d  d l  m Z d  d l m Z d  d l Z d  d l Z d  d l Z d  d l Z d Z d Z	 e
 Z i d d  6d d 6d	 d
 6d d 6d d 6Z d „  Z d „  Z d e j f d „  ƒ  YZ d e f d „  ƒ  YZ d d d „  ƒ  YZ d d „ Z d „  Z d S(   iÿÿÿÿ(   t   Image(   t   isPathNi   i   s   image buffer overrun errors   decoding erroriþÿÿÿs   unknown erroriýÿÿÿs   bad configurationiøÿÿÿs   out of memory errori÷ÿÿÿc         C   s`   y t  j j |  ƒ } Wn  t k
 r8 t j |  ƒ } n X| sL d |  } n  t | d ƒ ‚ d  S(   Ns   decoder error %ds    when reading image file(   R    t   coret   getcodecstatust   AttributeErrort   ERRORSt   gett   IOError(   t   errort   message(    (    s1   /usr/lib/python2.7/dist-packages/PIL/ImageFile.pyt   raise_ioerror4   s    c         C   s   |  d S(   Ni   (    (   t   t(    (    s1   /usr/lib/python2.7/dist-packages/PIL/ImageFile.pyt	   _tilesortB   s    t	   ImageFilec           B   sJ   e  Z d  Z d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   s*   Base class for image file format handlers.c         C   sš  t  j  j |  ƒ d  |  _ d |  _ d |  _ t |  _ t | ƒ r^ t	 | d ƒ |  _
 | |  _ n | |  _
 | |  _ y |  j ƒ  Wné t k
 r» } t  j d k r¬ t j ƒ  n  t | ƒ ‚ n¯ t k
 rõ } t  j d k ræ t j ƒ  n  t | ƒ ‚ nu t k
 r/} t  j d k r t j ƒ  n  t | ƒ ‚ n; t k
 ri} t  j d k rZt j ƒ  n  t | ƒ ‚ n X|  j s‡|  j d d k r–t d ƒ ‚ n  d  S(   Ni   t   rbi    s   not identified by this driver(    (   R    t   __init__t   Nonet   tilet   readonlyt   decoderconfigt   MAXBLOCKt   decodermaxblockR   t   opent   fpt   filenamet   _opent
   IndexErrort   DEBUGt	   tracebackt	   print_exct   SyntaxErrort	   TypeErrort   KeyErrort   EOFErrort   modet   size(   t   selfR   R   t   v(    (    s1   /usr/lib/python2.7/dist-packages/PIL/ImageFile.pyR   N   s<    						c         C   s   d S(   s   Set draft modeN(    (   R$   R"   R#   (    (    s1   /usr/lib/python2.7/dist-packages/PIL/ImageFile.pyt   draftv   s    c         C   s   d |  _ d S(   s   Check file integrityN(   R   R   (   R$   (    (    s1   /usr/lib/python2.7/dist-packages/PIL/ImageFile.pyt   verify{   s    c         C   sª  t  j  j |  ƒ } |  j d	 k r0 t d ƒ ‚ n  |  j s= | Sd	 |  _ |  j oa t |  j ƒ d k } | ow t t	 d ƒ } d } y |  j
 } t } Wn t k
 r² |  j j } n Xy |  j } t } Wn t k
 rå |  j j } n X| rN|  j d \ } } } }	 | d k rN|	 d |  j k rN|	 d t  j k rNyë t t  j d ƒ r£t  j j |  j ƒ |  _ |  j j | ƒ |  j j |  j |  j |	 d |	 d ƒ |  _ nx d d	 l }
 t |  j d
 ƒ } t j j |  j ƒ } |
 j | j ƒ  | ƒ |  _ t  j j |  j |  j | | | |	 ƒ |  _ d } WqKt t t f k
 rGd	 |  _ qKXqNn  |  j  ƒ  |  j s|  j j! d t" ƒ y |  j# } Wn t k
 ršd } n Xxh|  j D]Z\ } } } }	 t  j$ |  j | |	 |  j% ƒ } | | ƒ y | j& |  j | ƒ Wn t' k
 rq¥n X| } t | ƒ } xÓ t( rôy | |  j) ƒ } Wn) t* k
 rf} t+ rWPqgt* | ƒ ‚ n X| r®| j, r®g  |  _ | j- ƒ  t+ r•Pq®t d t | ƒ ƒ ‚ n  | | } | j. | ƒ \ } } | d k  rÝPn  | | } | | } q"W| j- ƒ  q¥Wn  g  |  _ | |  _/ d	 |  _ |  j rWt+ s>| d k rW| d k  rWt0 | ƒ n  t |  d ƒ r|  j j1 |  j2 ƒ |  _ |  j j |  _ n  |  j3 ƒ  t  j  j |  ƒ S(   s"   Load image data based on tile lists   cannot load this imagei   t   pypy_version_infoi    t   rawt   mapi   iÿÿÿÿNs   r+t   keyt    s0   image file is truncated (%d bytes not processed)t   tile_post_rotate(4   R    t   loadR   R   R   R*   R   t   lent   hasattrt   syst	   load_readt   FalseR   R   t   readt	   load_seekt   seekR"   t	   _MAPMODESR   t	   readimageR#   t   imt   mmapR   t   ost   patht   getsizet   filenot
   map_buffert   EnvironmentErrort   ImportErrort   load_preparet   sortR   t   tile_prefixt   _getdecoderR   t   setimaget
   ValueErrort   TrueR   R   t   LOAD_TRUNCATED_IMAGESt   handles_eoft   cleanupt   decodeR   R
   t   rotateR-   t   load_end(   R$   t   pixelt   use_mmapR   R4   R6   t   dt   et   ot   aR:   t   fileR#   t   prefixt   bR   t   st   iet   n(    (    s1   /usr/lib/python2.7/dist-packages/PIL/ImageFile.pyR.   ‚   s¨    			
	
2	&	!

	

		


			)
c         C   s{   |  j  s4 |  j  j |  j k s4 |  j  j |  j k rU t j j |  j |  j ƒ |  _  n  |  j d k rw t j j |  ƒ n  d  S(   Nt   P(   R9   R"   R#   R    R   t   newR.   (   R$   (    (    s1   /usr/lib/python2.7/dist-packages/PIL/ImageFile.pyRB     s
    
*!c         C   s   d  S(   N(    (   R$   (    (    s1   /usr/lib/python2.7/dist-packages/PIL/ImageFile.pyRN     s    N(
   t   __name__t
   __module__t   __doc__R   R   R&   R'   R.   RB   RN   (    (    (    s1   /usr/lib/python2.7/dist-packages/PIL/ImageFile.pyR   K   s   (					t   StubImageFilec           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s±   
    Base class for stub image loaders.

    A stub loader is an image loader that can identify files of a
    certain format, but relies on external code to load the file.
    c         C   s   t  d ƒ ‚ d  S(   Ns+   StubImageFile subclass must implement _open(   t   NotImplementedError(   R$   (    (    s1   /usr/lib/python2.7/dist-packages/PIL/ImageFile.pyR   !  s    c         C   sk   |  j  ƒ  } | d  k r. t d |  j ƒ ‚ n  | j |  ƒ } | d  k	 sO t ‚ | j |  _ | j |  _ d  S(   Ns#   cannot find loader for this %s file(   t   _loadR   R   t   formatR.   t   AssertionErrort	   __class__t   __dict__(   R$   t   loadert   image(    (    s1   /usr/lib/python2.7/dist-packages/PIL/ImageFile.pyR.   &  s    c         C   s   t  d ƒ ‚ d S(   s    (Hook) Find actual image loader.s+   StubImageFile subclass must implement _loadN(   Ra   (   R$   (    (    s1   /usr/lib/python2.7/dist-packages/PIL/ImageFile.pyRb   0  s    (   R]   R^   R_   R   R.   Rb   (    (    (    s1   /usr/lib/python2.7/dist-packages/PIL/ImageFile.pyR`     s   		
t   Parserc           B   sG   e  Z d  Z d Z d Z d Z d Z d Z d „  Z	 d „  Z
 d „  Z RS(   sš   
    Incremental image parser.  This class implements the standard
    feed/close consumer interface.

    In Python 2.x, this is an old-style class.
    i    c         C   s   |  j  d k s t d ƒ ‚ d S(   sµ   
        (Consumer) Reset the parser.  Note that you can only call this
        method immediately after you've created a parser; parser
        instances cannot be reused.
        s   cannot reuse parsersN(   t   dataR   Rd   (   R$   (    (    s1   /usr/lib/python2.7/dist-packages/PIL/ImageFile.pyt   resetD  s    c         C   sl  |  j  r d S|  j d k r( | |  _ n |  j | |  _ |  j r |  j d k r« t t |  j ƒ |  j ƒ } |  j | |  _ |  j | |  _ |  j d k s¤ |  j r« d Sn  |  j j |  j ƒ \ } } | d k  rd |  _ d |  _  | d k  rd |  _ t	 | ƒ qd Sn  |  j | |  _ nH|  j r,n<y7 z% t
 j |  j ƒ } t j | ƒ } Wd | j ƒ  XWn t k
 rvnò Xt | d ƒ p’t | d ƒ } | s°t | j ƒ d k r¼d |  _ n£ | j ƒ  | j d \ } } }	 }
 g  | _ t j | j | |
 | j ƒ |  _ |  j j | j | ƒ |	 |  _ |  j t |  j ƒ k r_|  j |  j |  _ d |  _ n  | |  _ d S(   s¦   
        (Consumer) Feed data to the parser.

        :param data: A string buffer.
        :exception IOError: If the parser failed to parse the image file.
        Ni    i   R5   R2   (   t   finishedRj   R   t   decodert   offsett   minR/   RL   Rh   R
   t   iot   BytesIOR    R   t   closeR   R0   R   RB   RE   R"   R   RF   R9   (   R$   Rj   t   skipRZ   RR   R   R9   t   flagRQ   RS   RT   (    (    s1   /usr/lib/python2.7/dist-packages/PIL/ImageFile.pyt   feedL  sX    							
		c         C   s¯   |  j  rA |  j d ƒ d |  _ |  _  |  j sA t d ƒ ‚ qA n  |  j sY t d ƒ ‚ n  |  j r¨ z( t j |  j ƒ } t	 j
 | ƒ |  _ Wd |  j j ƒ  | j ƒ  Xn  |  j S(   s  
        (Consumer) Close the stream.

        :returns: An image object.
        :exception IOError: If the parser failed to parse the image file either
                            because it cannot be identified or cannot be
                            decoded.
        R,   s   image was incompletes   cannot parse this imageN(   Rm   Ru   R   Rj   Rl   R   Rh   Rp   Rq   R    R   R.   Rr   (   R$   R   (    (    s1   /usr/lib/python2.7/dist-packages/PIL/ImageFile.pyRr   ¡  s    
				N(   R]   R^   R_   R   t   incrementalRh   Rj   Rm   Rl   Rk   Ru   Rr   (    (    (    s1   /usr/lib/python2.7/dist-packages/PIL/ImageFile.pyRi   7  s   		Ui    c         C   s  |  j  ƒ  t |  d ƒ s% d |  _ n  | j d t ƒ t t | |  j d d ƒ } y | j ƒ  } | j	 ƒ  Wnì t
 t j f k
 rZx{| D]Ç \ } } } } t j |  j | | |  j ƒ } | d k rÛ | j | d ƒ n  | j |  j | ƒ x9 t r)| j | ƒ \ }	 }
 } | j | ƒ |
 rñ Pqñ qñ W|
 d k  rIt d |
 ƒ ‚ n  | j ƒ  qŒ Wn© Xx¥ | D] \ } } } } t j |  j | | |  j ƒ } | d k r±| j | d ƒ n  | j |  j | ƒ | j | | ƒ }
 |
 d k  rõt d |
 ƒ ‚ n  | j ƒ  qbWy | j	 ƒ  Wn n Xd S(   s«   Helper to save image based on tile list

    :param im: Image object.
    :param fp: File object.
    :param tile: Tile list.
    :param bufsize: Optional buffer size
    t   encoderconfigR+   i    i   s(   encoder error %d when writing image fileN(    (   R.   R0   Rw   RC   R   t   maxR   R#   R>   t   flushR   Rp   t   UnsupportedOperationR    t   _getencoderR"   R6   RF   R9   RH   t   encodet   writeR   RK   t   encode_to_file(   R9   R   R   t   bufsizet   fhRR   RW   RS   RT   t   lRX   RQ   (    (    s1   /usr/lib/python2.7/dist-packages/PIL/ImageFile.pyt   _saveÁ  sF    	
	c         C   sŽ   | d k r d S| t  k r) |  j | ƒ Sg  } xO | d k r€ |  j t | t  ƒ ƒ } | s` Pn  | j | ƒ | t | ƒ 8} q2 Wd j | ƒ S(   so  
    Reads large blocks in a safe way.  Unlike fp.read(n), this function
    doesn't trust the user.  If the requested size is larger than
    SAFEBLOCK, the file is read block by block.

    :param fp: File handle.  Must implement a <b>read</b> method.
    :param size: Number of bytes to read.
    :returns: A string containing up to <i>size</i> bytes of data.
    i    R,   (   t	   SAFEBLOCKR4   Ro   t   appendR/   t   join(   R   R#   Rj   t   block(    (    s1   /usr/lib/python2.7/dist-packages/PIL/ImageFile.pyt
   _safe_readö  s    
i   (    (   t   PILR    t	   PIL._utilR   Rp   R;   R1   R   R   Rƒ   R3   RI   R   R
   R   R   R`   Ri   R‚   R‡   (    (    (    s1   /usr/lib/python2.7/dist-packages/PIL/ImageFile.pyt   <module>   s*   
			ÎŠ5