ó
È4ec           @   s  d  d l  Z  d  d l Z d  d l Z d d l m Z d d l m 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 e f d „  ƒ  YZ d d „ Z d „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(    iÿÿÿÿNi   (   t   Image(   t   isPathi   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(    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyt   raise_ioerror4   s    c         C   s   |  d S(   Ni   (    (   t   t(    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyt	   _tilesortC   s    t	   ImageFilec           B   s\   e  Z d  Z d	 d	 d „ Z d „  Z 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  |  _ d |  _ d |  _ t |  _	 t
 | ƒ ry t | d ƒ |  _ | |  _ t |  _ n | |  _ | |  _ d  |  _ y |  j ƒ  WnJ t t t t t j f k
 rî } |  j rß |  j j ƒ  n  t | ƒ ‚ n X|  j s|  j d d k rt d ƒ ‚ n  d  S(   Ni    i   t   rbs   not identified by this driver(    (   R    t   __init__t
   _min_framet   Nonet   custom_mimetypet   tilet   readonlyt   decoderconfigt   MAXBLOCKt   decodermaxblockR   t   opent   fpt   filenamet   Truet   _exclusive_fpt   _opent
   IndexErrort	   TypeErrort   KeyErrort   EOFErrort   structR   t   closet   SyntaxErrort   modet   size(   t   selfR   R   t   v(    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyR   P   s8    											c         C   s   d S(   s   Set draft modeN(    (   R'   R%   R&   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyt   draftz   s    c         C   s<   |  j  r |  j  S|  j d  k	 r8 t j j |  j j ƒ  ƒ Sd  S(   N(   R   t   formatR   R    t   MIMER   t   upper(   R'   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyt   get_format_mimetype   s    	c         C   s&   |  j  r |  j j ƒ  n  d |  _ d S(   s   Check file integrityN(   R   R   R#   R   (   R'   (    (    s7   /usr/local/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| ru|  j d \ } } } }	 | d k rut |	 ƒ d k ru|	 d |  j k ru|	 d t  j k ruy t t  j d ƒ rµt  j j |  j ƒ |  _ |  j j | ƒ |  j j |  j |  j |	 d |	 d ƒ |  _ nu d	 d
 l }
 t |  j d ƒ + } |
 j | j ƒ  d d |
 j ƒ|  _ Wd
 QXt  j j |  j |  j | | | |	 ƒ |  _ d } |  j rHd |  j _ n  Wqrt t t f k
 rnd
 |  _ qrXqun  |  j  ƒ  d } |  j s$|  j j! d t" ƒ y |  j# } Wn t k
 rÇd } n XxY|  j D]K\ } } } }	 t  j$ |  j | |	 |  j% ƒ } z| | ƒ | j& |  j | ƒ | j' rS| j( |  j ƒ | j) d ƒ \ } } n» | } x² t* ry | |  j+ ƒ } Wn0 t, t- j. f k
 r§t/ r˜Pq¨t d ƒ ‚ n X| sÑt/ r¸PqÑt d t | ƒ ƒ ‚ n  | | } | j) | ƒ \ } } | d k  r Pn  | | } q\WWd
 | j0 ƒ  XqÒWn  g  |  _ | |  _1 |  j2 ƒ  |  j3 rb|  j4 rb|  j j5 ƒ  n  d
 |  _ |  j r•t/ r•| d k  r•t6 | ƒ n  t  j  j |  ƒ S(   s"   Load image data based on tile lists   cannot load this imagei   t   pypy_version_infoi    t   rawi   t   mapi   iÿÿÿÿNt   rt   accessiýÿÿÿt   keyt    s   image file is truncateds0   image file is truncated (%d bytes not processed)(7   R    t   loadR   R   R   R1   R   t   lent   hasattrt   syst	   load_readt   FalseR   R   t   readt	   load_seekt   seekR%   t	   _MAPMODESR   t	   readimageR&   t   imt   mmapR   t   filenot   ACCESS_READt
   map_buffert   palettet   dirtyt   EnvironmentErrort   ImportErrort   load_preparet   sortR   t   tile_prefixt   _getdecoderR   t   setimaget   pulls_fdt   setfdt   decodeR   R   R   R"   R   t   LOAD_TRUNCATED_IMAGESt   cleanupR   t   load_endR   t!   _close_exclusive_fp_after_loadingR#   R
   (   R'   t   pixelt   use_mmapR   R<   R>   t   decoder_namet   extentst   offsett   argsRB   R   t   err_codet   prefixt   decodert   statust   bt   st   n(    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyR6   Ž   s°    			
	
	&$	!	
	

		
		
	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(   RA   R%   R&   R    R   t   newR6   (   R'   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyRJ     s    4!c         C   s   d  S(   N(    (   R'   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyRT     s    c         C   sc   | |  j  k  sD t |  d ƒ o* |  j d  k rS | |  j |  j  k rS t d ƒ ‚ n  |  j ƒ  | k S(   Nt	   _n_framess    attempt to seek outside sequence(   R   R8   Re   R   t   n_framesR!   t   tell(   R'   t   frame(    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyt   _seek_check(  s
    N(   t   __name__t
   __module__t   __doc__R   R   R)   R-   R.   R6   RJ   RT   Ri   (    (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyR   M   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'   (    (    s7   /usr/local/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   R*   R6   t   AssertionErrort	   __class__t   __dict__(   R'   t   loadert   image(    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyR6   B  s    c         C   s   t  d ƒ ‚ d S(   s    (Hook) Find actual image loader.s+   StubImageFile subclass must implement _loadN(   Rn   (   R'   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyRo   L  s    (   Rj   Rk   Rl   R   R6   Ro   (    (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyRm   7  s   		
t   Parserc           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 RS(   sj   
    Incremental image parser.  This class implements the standard
    feed/close consumer interface.
    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   Rp   (   R'   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyt   reset^  s    c         C   sc  |  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 | |  _ n?|  j r,n3y. t
 j |  j ƒ  } t j | ƒ } Wd QXWn t k
 rm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 rV|  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   R=   R:   (   t   finishedRv   R   R^   RZ   t   minR7   RQ   Rt   R
   t   iot   BytesIOR    R   R   R8   R   RJ   RM   R%   R   RN   RA   (   R'   Rv   t   skipRb   t   eR   RA   t   flagt   dt   ot   a(    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyt   feedf  sR    							
	!	c         C   s   |  S(   N(    (   R'   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyt	   __enter__¶  s    c         G   s   |  j  ƒ  d  S(   N(   R#   (   R'   R[   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyt   __exit__¹  s    c         C   s®   |  j  rA |  j d ƒ d |  _ |  _  |  j sA t d ƒ ‚ qA n  |  j sY t d ƒ ‚ n  |  j r§ t j |  j ƒ . } z t	 j
 | ƒ |  _ Wd |  j j ƒ  XWd QXn  |  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.
        R5   s   image was incompletes   cannot parse this imageN(   R^   R‚   R   Rv   Rx   R   Rt   Rz   R{   R    R   R6   (   R'   R   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyR#   ¼  s    
				N(   Rj   Rk   Rl   R   t   incrementalRt   Rv   R^   RZ   Rx   Rw   R‚   Rƒ   R„   R#   (    (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyRu   Q  s   		P		i    c         C   s  |  j  ƒ  t |  d ƒ s% d |  _ n  | j d t ƒ t t | |  j d d ƒ } | t j	 k ro | j
 ƒ  d Sy | j ƒ  } | j
 ƒ  Wnt t j f k
 rŸxË| D]ï \ } } } } t j |  j | | |  j ƒ } | d k rõ | j | ƒ n  | j |  j | ƒ | j r3| j | ƒ | j ƒ  \ }	 }
 n< x9 t rn| j | ƒ \ }	 }
 } | j | ƒ |
 r6Pq6q6W|
 d k  rŽt d |
 ƒ ‚ n  | j ƒ  q© WnÑ XxÍ | D]Å \ } } } } t j |  j | | |  j ƒ } | d k ró| j | ƒ n  | j |  j | ƒ | j r1| j | ƒ | j ƒ  \ }	 }
 n | j | | ƒ }
 |
 d k  rbt d |
 ƒ ‚ n  | j ƒ  q§Wt | d ƒ rŒ| j
 ƒ  n  d 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   encoderconfigR4   i    i   Ns(   encoder error %d when writing image filet   flush(    (   R6   R8   R†   RK   R   t   maxR   R&   R9   t   stdoutR‡   RC   R   Rz   t   UnsupportedOperationR    t   _getencoderR%   R>   RN   RA   t	   pushes_fdRP   t   encode_to_pyfdR   t   encodet   writeR   RS   t   encode_to_file(   RA   R   R   t   bufsizet   fhR}   R`   R€   R   t   lRa   R   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyt   _saveÜ  sT    	

			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    R5   (   t	   SAFEBLOCKR<   Ry   t   appendR7   t   join(   R   R&   Rv   t   block(    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyt
   _safe_read  s    
t   PyCodecStatec           B   s   e  Z d  „  Z d „  Z RS(   c         C   s(   d |  _  d |  _ d |  _ d |  _ d  S(   Ni    (   t   xsizet   ysizet   xofft   yoff(   R'   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyR   3  s    			c         C   s*   |  j  |  j |  j  |  j |  j |  j f S(   N(   R   Rž   R›   Rœ   (   R'   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyRY   9  s    (   Rj   Rk   R   RY   (    (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyRš   2  s   	t	   PyDecoderc           B   sh   e  Z d  Z e Z d „  Z d „  Z e d „  ƒ Z d „  Z	 d „  Z
 d „  Z d	 d „ Z d	 d „ Z RS(
   sÉ   
    Python implementation of a format decoder. Override this class and
    add the decoding logic in the `decode` method.

    See :ref:`Writing Your Own File Decoder in Python<file-decoders-py>`
    c         G   s8   d  |  _ t ƒ  |  _ d  |  _ | |  _ |  j | ƒ d  S(   N(   R   RA   Rš   t   statet   fdR%   t   init(   R'   R%   R[   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyR   G  s
    			c         C   s   | |  _  d S(   sš   
        Override to perform decoder specific initialization

        :param args: Array of args items from the tile entry
        :returns: None
        N(   R[   (   R'   R[   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyR¢   N  s    c         C   s   |  j  S(   N(   t	   _pulls_fd(   R'   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyRO   W  s    c         C   s   t  ƒ  ‚ d S(   s'  
        Override to perform the decoding process.

        :param buffer: A bytes object with the data to be decoded.
        :returns: A tuple of (bytes consumed, errcode).
            If finished with decoding return <0 for the bytes consumed.
            Err codes are from `ERRORS`
        N(   Rn   (   R'   t   buffer(    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyRQ   [  s    	c         C   s   d S(   sV   
        Override to perform decoder specific cleanup

        :returns: None
        N(    (   R'   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyRS   f  s    c         C   s   | |  _  d S(   s   
        Called from ImageFile to set the python file-like object

        :param fd: A python file-like object
        :returns: None
        N(   R¡   (   R'   R¡   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyRP   n  s    c         C   s9  | |  _  | r$ | \ } } } } n d \ } } } } | d k ro | d k ro |  j  j \ |  j _ |  j _ n8 | |  j _ | |  j _ | | |  j _ | | |  j _ |  j j d k sË |  j j d k rÚ t d ƒ ‚ n  |  j j |  j j |  j  j d k s&|  j j |  j j |  j  j d k r5t d ƒ ‚ n  d S(   sú   
        Called from ImageFile to set the core output image for the decoder

        :param im: A core image object
        :param extents: a 4 tuple of (x0, y0, x1, y1) defining the rectangle
            for this tile
        :returns: None
        i    s   Size cannot be negativei   s    Tile cannot extend outside imageN(   i    i    i    i    (   RA   R&   R    R›   Rœ   R   Rž   t
   ValueError(   R'   RA   RY   t   x0t   y0t   x1t   y1(    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyRN   w  s    	!$&&c         C   s—   | s |  j  } n  t j |  j  d | ƒ } | j |  j |  j j ƒ  ƒ | j | ƒ } | d d k rt t d ƒ ‚ n  | d d k r“ t d ƒ ‚ n  d S(   s  
        Convenience method to set the internal image from a stream of raw data

        :param data: Bytes to be set
        :param rawmode: The rawmode to be used for the decoder.
            If not specified, it will default to the mode of the image
        :returns: None
        R0   i    s   not enough image datai   s   cannot decode image dataN(	   R%   R    RM   RN   RA   R    RY   RQ   R¥   (   R'   Rv   t   rawmodeR   Ra   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyt
   set_as_rawš  s    
N(   Rj   Rk   Rl   R;   R£   R   R¢   t   propertyRO   RQ   RS   RP   R   RN   R«   (    (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyRŸ   =  s   							#i   (   Rz   R"   R9   R5   R    t   _utilR   R   R•   R;   RR   R   R
   R   R   Rm   t   objectRu   R”   R™   Rš   RŸ   (    (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageFile.pyt   <module>   s,   
		
ê‹>	