
hR\qP                 @   s3  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	 d Z
 i d d$ 6d	 d% 6d d& 6d d' 6d d( 6Z d d   Z d d   Z Gd d   d e j  Z Gd d   d e  Z Gd d   d e  Z d d d  Z d d   Z Gd d    d  e  Z Gd! d"   d" e  Z d S))   )Image)isPath    Ni   i   Fzimage buffer overrun errorzdecoding error   zunknown error   zbad configuration   zout of memory error	   c             C   sa   y t  j j |   } Wn! t k
 r9 t j |   } Yn X| sM d |  } n  t | d   d  S)Nzdecoder error %dz when reading image file)r   coreZgetcodecstatusAttributeErrorERRORSgetIOError)errormessage r   B/var/www/dbchiro/venv/lib/python3.4/site-packages/PIL/ImageFile.pyraise_ioerror4   s    r   c             C   s   |  d S)Nr   r   )tr   r   r   	_tilesortB   s    r   c               @   sp   e  Z d  Z d Z d d d d  Z d d   Z d d   Z d	 d
   Z d d   Z d d   Z	 d d   Z
 d S)	ImageFilez*Base class for image file format handlers.Nc             C   s(  t  j  j |   d |  _ d  |  _ d |  _ f  |  _ t |  _ t |  rp t	 | d  |  _
 | |  _ d |  _ n | |  _
 | |  _ d  |  _ y |  j   Wn\ t t t t t j f k
 r } z* |  j r |  j
 j   n  t |   WYd  d  } ~ Xn X|  j s|  j d d k r$t d   n  d  S)Nr   r   rbTznot identified by this driver)r   __init__
_min_frametilereadonlydecoderconfigMAXBLOCKdecodermaxblockr   openfpfilename_exclusive_fp_open
IndexError	TypeErrorKeyErrorEOFErrorstructr   closeSyntaxErrormodesize)selfr   r    vr   r   r   r   N   s2    										zImageFile.__init__c             C   s   d S)zSet draft modeNr   )r,   r*   r+   r   r   r   draftt   s    zImageFile.draftc             C   s&   |  j  r |  j j   n  d |  _ d S)zCheck file integrityN)r!   r   r(   )r,   r   r   r   verifyy   s    	zImageFile.verifyc          -   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	 } d } Wn t
 k
 r |  j j } Yn Xy |  j } d } Wn t
 k
 r |  j j } Yn X| r|  j d \ } } } }	 | d k rt |	  d k r|	 d |  j k r|	 d t  j k ryt t  j d	  rt  j j |  j  |  _ |  j j |  |  j j |  j |  j |	 d |	 d
  |  _ n d d l }
 t |  j d  } t j j |  j  } |
 j | j   | d |
 j |  _ t  j j |  j |  j | | | |	  |  _ d } |  j rVd |  j _ n  Wqt
 t t  f k
 r}d |  _ YqXqn  |  j!   d } |  j s@|  j j" d t#  y |  j$ } Wn t
 k
 rd } Yn Xxe|  j D]W\ } } } }	 t  j% |  j | |	 |  j&  } | |  | j' |  j |  | j( r`| j) |  j  | j* d  \ } } n | } x y | |  j+  } Wn1 t, t- j. f k
 rt/ rPn t d   Yn X| sg  |  _ | j0   t/ rPqt d t |    n  | | } | j* |  \ } } | d k  rPn  | | d  } qiW| j0   qWn  g  |  _ | |  _1 |  j2   |  j3 r~|  j4 r~|  j j5   n  d |  _ |  j rt/ r| d k  rt6 |  n  t  j  j |   S)z"Load image data based on tile listNzcannot load this imager   pypy_version_infor   Frawr   mapr   raccesskey    zimage file is truncatedz0image file is truncated (%d bytes not processed))7r   loadr   r   r2   r    lenhasattrsys	load_readr
   r   read	load_seekseekr*   Z	_MAPMODESr	   Z	readimager+   immmapr   ospathgetsizefilenoZACCESS_READZ
map_bufferZpaletteZdirtyEnvironmentErrorImportErrorload_preparesortr   Ztile_prefix_getdecoderr   setimagepulls_fdsetfddecoder   r#   r'   r   LOAD_TRUNCATED_IMAGEScleanupr   load_endr!   Z!_close_exclusive_fp_after_loadingr(   r   )r,   ZpixelZuse_mmapr   r=   r?   Zdecoder_nameextentsoffsetargsrA   r   r+   Zerr_codeprefixdecoderstatusbsnr   r   r   r8      s    			
	
1	&$	!	
	
		

		
	zImageFile.loadc             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)NP)r@   r*   r+   r   r	   newr8   )r,   r   r   r   rH     s
    
*!zImageFile.load_preparec             C   s   d  S)Nr   )r,   r   r   r   rQ     s    zImageFile.load_endc             C   sc   | |  j  k  sD t |  d  o* |  j d  k rS | |  j |  j  k rS t d   n  |  j   | k S)N	_n_framesz attempt to seek outside sequence)r   r:   r]   Zn_framesr&   tell)r,   framer   r   r   _seek_check  s
    zImageFile._seek_check)__name__
__module____qualname____doc__r   r.   r/   r8   rH   rQ   r`   r   r   r   r   r   K   s   &		r   c               @   s:   e  Z d  Z d Z d d   Z d d   Z d d   Z d S)	StubImageFilez
    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)Nz+StubImageFile subclass must implement _open)NotImplementedError)r,   r   r   r   r"   +  s    zStubImageFile._openc             C   sk   |  j    } | d  k r. t d |  j   n  | j |   } | d  k	 sO t  | j |  _ | j |  _ d  S)Nz#cannot find loader for this %s file)_loadr   formatr8   AssertionError	__class____dict__)r,   loaderimager   r   r   r8   0  s    zStubImageFile.loadc             C   s   t  d   d S)z (Hook) Find actual image loader.z+StubImageFile subclass must implement _loadN)rf   )r,   r   r   r   rg   :  s    zStubImageFile._loadN)ra   rb   rc   rd   r"   r8   rg   r   r   r   r   re   #  s   
re   c               @   sv   e  Z d  Z d Z d Z d Z d Z d Z d Z d Z	 d d   Z
 d d   Z d d	   Z d
 d   Z d d   Z d S)Parserzj
    Incremental image parser.  This class implements the standard
    feed/close consumer interface.
    Nr   c             C   s   |  j  d k s t d   d S)z
        (Consumer) Reset the parser.  Note that you can only call this
        method immediately after you've created a parser; parser
        instances cannot be reused.
        Nzcannot reuse parsers)datari   )r,   r   r   r   resetM  s    zParser.resetc             C   sv  |  j  r d S|  j d k r( | |  _ n |  j | |  _ |  j r,|  j d k r t t |  j  |  j  } |  j | d  |  _ |  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 | d  |  _ nF|  j r8n:y. t	 j
 |  j   } t j |  } Wd QXWn t k
 rzYn 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 ri|  j |  j d  |  _ d |  _ n  | |  _ d S)z
        (Consumer) Feed data to the parser.

        :param data: A string buffer.
        :exception IOError: If the parser failed to parse the image file.
        Nr   r   r>   r<   )finishedro   rV   rS   minr9   rN   rm   r   ioBytesIOr   r   r   r:   r   rH   rJ   r*   r   rK   r@   )r,   ro   skiprZ   er   r@   flagdoar   r   r   feedU  sT    							
		zParser.feedc             C   s   |  S)Nr   )r,   r   r   r   	__enter__  s    zParser.__enter__c             G   s   |  j    d  S)N)r(   )r,   rT   r   r   r   __exit__  s    zParser.__exit__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)a  
        (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.
        r6   Nzimage was incompletezcannot parse this image)rV   r{   ro   rq   r   rm   rs   rt   r   r   r8   )r,   r   r   r   r   r(     s    
				zParser.close)ra   rb   rc   rd   Zincrementalrm   ro   rV   rS   rq   rp   r{   r|   r}   r(   r   r   r   r   rn   A  s   Rrn   c             C   s  |  j    t |  d  s% f  |  _ 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 | d  n  | j |  j |  | j r6| j |  | j   \ }	 }
 n6 x3 | j |  \ }	 }
 } | j |  |
 r9Pq9q9W|
 d k  rt d |
   n  | j   q WYn Xx | D] \ } } } } t j |  j | | |  j  } | d k r| j | d  n  | j |  j |  | j r2| j |  | j   \ }	 }
 n | j | |  }
 |
 d k  rct d |
   n  | j   qWt | d  r| j
   n  d S)zHelper to save image based on tile list

    :param im: Image object.
    :param fp: File object.
    :param tile: Tile list.
    :param bufsize: Optional buffer size
    encoderconfigr5   r      Nz(encoder error %d when writing image fileflush)r8   r:   r~   rI   r   maxr   r+   r;   stdoutr   rE   r
   rs   UnsupportedOperationr   Z_getencoderr*   r?   rK   r@   Z	pushes_fdrM   Zencode_to_pyfdencodewriter   rP   Zencode_to_file)r@   r   r   bufsizeZfhrv   rX   ry   rz   lrY   rx   r   r   r   _save  sT    	

		r   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)ao  
    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.
    r   r6   )	SAFEBLOCKr=   rr   appendr9   join)r   r+   ro   blockr   r   r   
_safe_read
  s    
r   c               @   s(   e  Z d  Z d d   Z d d   Z d S)PyCodecStatec             C   s(   d |  _  d |  _ d |  _ d |  _ d  S)Nr   )xsizeysizexoffyoff)r,   r   r   r   r   #  s    			zPyCodecState.__init__c             C   s*   |  j  |  j |  j  |  j |  j |  j f S)N)r   r   r   r   )r,   r   r   r   rR   )  s    zPyCodecState.extentsN)ra   rb   rc   r   rR   r   r   r   r   r   "  s   r   c               @   s   e  Z d  Z d Z d Z d d   Z d d   Z e d d    Z d	 d
   Z	 d d   Z
 d d   Z d d d  Z d d d  Z d S)	PyDecoderz
    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>`
    Fc             G   s8   d  |  _  t   |  _ d  |  _ | |  _ |  j |  d  S)N)r@   r   statefdr*   init)r,   r*   rT   r   r   r   r   8  s
    			zPyDecoder.__init__c             C   s   | |  _  d S)z
        Override to perform decoder specific initialization

        :param args: Array of args items from the tile entry
        :returns: None
        N)rT   )r,   rT   r   r   r   r   ?  s    zPyDecoder.initc             C   s   |  j  S)N)	_pulls_fd)r,   r   r   r   rL   H  s    zPyDecoder.pulls_fdc             C   s   t     d S)az  
        Override to perform the decoding process.

        :param buffer: A bytes object with the data to be decoded.  If `handles_eof`
             is set, then `buffer` will be empty and `self.fd` will be set.
        :returns: A tuple of (bytes consumed, errcode). If finished with decoding
             return <0 for the bytes consumed. Err codes are from `ERRORS`
        N)rf   )r,   bufferr   r   r   rN   L  s    	zPyDecoder.decodec             C   s   d S)zV
        Override to perform decoder specific cleanup

        :returns: None
        Nr   )r,   r   r   r   rP   W  s    zPyDecoder.cleanupc             C   s   | |  _  d S)z
        Called from ImageFile to set the python file-like object

        :param fd: A python file-like object
        :returns: None
        N)r   )r,   r   r   r   r   rM   _  s    zPyDecoder.setfdNc             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)z
        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
        r   zSize cannot be negativer   z Tile cannot extend outside imageN)r   r   r   r   )r@   r+   r   r   r   r   r   
ValueError)r,   r@   rR   Zx0Zy0Zx1y1r   r   r   rK   h  s    	!$&&zPyDecoder.setimagec             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)a  
        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
        r1   r   znot enough image datar   zcannot decode image dataN)	r*   r   rJ   rK   r@   r   rR   rN   r   )r,   ro   rawmoderx   rY   r   r   r   
set_as_raw  s    
zPyDecoder.set_as_raw)ra   rb   rc   rd   r   r   r   propertyrL   rN   rP   rM   rK   r   r   r   r   r   r   .  s   		!r   i   r7   ii) r   Z_utilr   rs   rB   r;   r'   r   r   rO   r   r   r   r   re   objectrn   r   r   r   r   r   r   r   r   <module>   s.   
	>