3
V^Q  ã            
   @   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dZ	d$dd%d
d&dd'dd(diZ
dd„ Zdd„ ZG dd„ dejƒZG dd„ deƒZG dd„ dƒZd)dd„Zdd„ ZG dd „ d ƒZG d!d"„ d"ƒZdS )*é    Né   )ÚImage)ÚisPathi   i   Fzimage buffer overrun erroré   zdecoding erroré   zunknown erroré   zbad configurationé	   zout of memory errorc             C   sL   yt jj| ƒ}W n tk
r.   tj| ƒ}Y nX |s<d|  }t|d ƒ‚d S )Nzdecoder error %dz when reading image file)r   ÚcoreZgetcodecstatusÚAttributeErrorÚERRORSÚgetÚOSError)ÚerrorÚmessage© r   ú//tmp/pip-build-zn8x4q5s/pillow/PIL/ImageFile.pyÚraise_ioerror4   s    r   c             C   s   | d S )Nr   r   )Útr   r   r   Ú	_tilesortC   s    r   c                   sR   e Zd ZdZd‡ fdd„	Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
‡  ZS )Ú	ImageFilez*Base class for image file format handlers.Nc                sô   t ƒ jƒ  d| _d | _d | _d| _f | _t| _t	|ƒrPt
|dƒ| _|| _d| _n|| _|| _d | _yfy| jƒ  W n6 tttttjfk
r¦ } zt|ƒ‚W Y d d }~X nX | j s¾| jd dkrÆtdƒ‚W n& tk
rî   | jrè| jjƒ  ‚ Y nX d S )Nr   r   ÚrbTznot identified by this driver)ÚsuperÚ__init__Ú
_min_frameÚcustom_mimetypeÚtileÚreadonlyÚdecoderconfigÚMAXBLOCKÚdecodermaxblockr   ÚopenÚfpÚfilenameÚ_exclusive_fpÚ_openÚ
IndexErrorÚ	TypeErrorÚKeyErrorÚEOFErrorÚstructr   ÚSyntaxErrorÚmodeÚsizeÚBaseExceptionÚclose)Úselfr!   r"   Úv)Ú	__class__r   r   r   P   s<    

zImageFile.__init__c             C   s,   | j r| j S | jd k	r(tjj| jjƒ ƒS d S )N)r   Úformatr   ZMIMEr   Úupper)r/   r   r   r   Úget_format_mimetype}   s    
zImageFile.get_format_mimetypec             C   s   | j r| jjƒ  d| _dS )zCheck file integrityN)r#   r!   r.   )r/   r   r   r   Úverifyƒ   s    
zImageFile.verifyc          8   C   s”  t j j| ƒ}| jdkrtdƒ‚| js(|S d| _| jo@t| jƒdk}|oPttdƒ }d}y| j	}d}W n t
k
r€   | jj}Y nX y| j}d}W n t
k
r¬   | jj}Y nX |�rÔ| jd \}}}}	|dkoôt|	ƒdkoô|	d | jkoô|	d t jk�rÔy¸tt jd	ƒ�rFt jj| jƒ| _| jj|ƒ | jj| j| j|	d |	d
 ƒ| _nTddl}
t| jdƒ�}|
j|jƒ d|
jd�| _W dQ R X t jj| j| j||||	ƒ| _d}| j�r®d| j_W n" t
ttfk
�rÒ   d| _Y nX | jƒ  d}| j�s0| jjtd� y
| j }W n t
k
�r   d}Y nX �x| jD �]\}}}}	t j!| j||	| j"ƒ}zÖ||ƒ |j#| j|ƒ |j$�r„|j%| jƒ |j&dƒ\}}n˜|}x’y|| j'ƒ}W n. t(t)j*fk
�rÆ   t+�rºP ntdƒ‚Y nX |�sèt+�rØP ntdt|ƒ ƒ‚|| }|j&|ƒ\}}|dk �r
P ||d… }�qŠW W d|j,ƒ  X �q&W g | _|| _-| j.ƒ  | j/�r^| j0�r^| jj1ƒ  d| _| j �rˆt+ �rˆ|dk �rˆt2|ƒ 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)éýÿÿÿ)3r   Úloadr   r   r8   r"   ÚlenÚhasattrÚsysÚ	load_readr
   r!   ÚreadÚ	load_seekÚseekr+   Z	_MAPMODESr	   Z	readimager,   ÚimÚmmapr    ÚfilenoZACCESS_READZ
map_bufferZpaletteZdirtyÚ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   rC   rE   Zdecoder_nameÚextentsÚoffsetÚargsrG   r!   Zerr_codeÚprefixÚdecoderÚstatusÚbÚsÚnr   r   r   r>   Œ   s°    




zImageFile.loadc             C   sR   | j  s$| j j| jks$| j j| jkr8tjj| j| jƒ| _ | jdkrNtjj| ƒ d S )NÚP)rF   r+   r,   r   r	   Únewr>   )r/   r   r   r   rJ     s    $
zImageFile.load_preparec             C   s   d S )Nr   )r/   r   r   r   rS     s    zImageFile.load_endc             C   sD   || j k s0t| dƒo| jd k r8|| j| j  kr8tdƒ‚| 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)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r4   r5   r>   rJ   rS   rb   Ú__classcell__r   r   )r1   r   r   M   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   sH   | j ƒ }|d krtd| j ƒ‚|j| ƒ}|d k	s4t‚|j| _|j| _d S )Nz#cannot find loader for this %s file)Ú_loadr   r2   r>   ÚAssertionErrorr1   Ú__dict__)r/   ÚloaderÚimager   r   r   r>   @  s    
zStubImageFile.loadc             C   s   t dƒ‚dS )z (Hook) Find actual image loader.z+StubImageFile subclass must implement _loadN)ri   )r/   r   r   r   rj   J  s    zStubImageFile._loadN)rc   rd   re   rf   r$   r>   rj   r   r   r   r   rh   5  s   
rh   c               @   sP   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)Údatark   )r/   r   r   r   Úreset\  s    zParser.resetc             C   sÊ  | 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sx| j r|dS | jj| jƒ\}}|dk r¾d| _d| _ |dk rºd| _t|ƒ ndS | j|d… | _nö| jrØnîy&t	j
| jƒ�}tj|ƒ}W dQ R X W n tk
�r   Y n²X t|dƒ�p*t|dƒ}|�sBt|jƒdk�rJd| _nv|jƒ  |jd \}}}	}
g |_tj|j||
|jƒ| _| jj|j|ƒ |	| _| jt| jƒk�rÀ| j| jd… | _d| _|| _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   rD   rB   )Úfinishedrp   rX   rU   Úminr?   rP   rn   r   ÚioÚBytesIOr   r    r   r@   r   rJ   rL   r+   r   rM   rF   )r/   rp   Úskipr\   Úer!   rF   ÚflagÚdÚoÚar   r   r   Úfeedd  sR    	


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/   rV   r   r   r   Ú__exit__·  s    zParser.__exit__c             C   sz   | j r*| jdƒ d | _| _ | js*tdƒ‚| js8tdƒ‚| jrttj| jƒ�$}ztj	|ƒ| _W d| jj
ƒ  X W dQ R X | 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.
        r<   Nzimage was incompletezcannot parse this image)rX   r|   rp   rr   r   rn   rt   ru   r   r    r>   )r/   r!   r   r   r   r.   º  s    

zParser.close)rc   rd   re   rf   Úincrementalrn   rp   rX   rU   rr   rq   r|   r}   r~   r.   r   r   r   r   ro   O  s   Pro   c             C   sÞ  | j ƒ  t| dƒsf | _|jtd� tt|| jd d ƒ}|tj	krP|j
ƒ  dS y|jƒ }|j
ƒ  W nÈ ttjfk
�r,   xª|D ]¢\}}}}tj| j||| jƒ}|dkr´|j|ƒ |j| j|ƒ |jrà|j|ƒ |jƒ \}	}
n&x$|j|ƒ\}	}
}|j|ƒ |
râP qâW |
dk �rtd|
 ƒ‚|jƒ  q‚W Y nšX x–|D ]Ž\}}}}tj| j||| jƒ}|dk�rh|j|ƒ |j| j|ƒ |j�r–|j|ƒ |jƒ \}	}
n|j||ƒ}
|
dk �r¸td|
 ƒ‚|jƒ  �q4W t|dƒ�rÚ|j
ƒ  dS )z«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
    Úencoderconfig)r;   r   é   Nz(encoder error %d when writing image fileÚflush)r>   r@   r€   rK   r   Úmaxr   r,   rA   Ústdoutr‚   rH   r
   rt   ÚUnsupportedOperationr   Z_getencoderr+   rE   rM   rF   Z	pushes_fdrO   Zencode_to_pyfdÚencodeÚwriter   rR   Zencode_to_file)rF   r!   r   ÚbufsizeÚfhrw   rZ   rz   r{   Úlr[   ry   r   r   r   Ú_saveÚ  sT    	









r‹   c             C   sf   |dkrdS |t kr| j|ƒS g }x8|dkrZ| jt|t ƒƒ}|sBP |j|ƒ |t|ƒ8 }q$W d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   r<   )Ú	SAFEBLOCKrC   rs   Úappendr?   Újoin)r!   r,   rp   Ú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   1  s    zPyCodecState.__init__c             C   s    | j | j| j | j | j| j fS )N)r”   r•   r’   r“   )r/   r   r   r   rT   7  s    zPyCodecState.extentsN)rc   rd   re   r   rT   r   r   r   r   r‘   0  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   s(   d | _ tƒ | _d | _|| _| j|ƒ d S )N)rF   r‘   ÚstateÚfdr+   Úinit)r/   r+   rV   r   r   r   r   E  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)rV   )r/   rV   r   r   r   r™   L  s    zPyDecoder.initc             C   s   | j S )N)Ú	_pulls_fd)r/   r   r   r   rN   U  s    zPyDecoder.pulls_fdc             C   s
   t ƒ ‚dS )a'  
        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)ri   )r/   Úbufferr   r   r   rP   Y  s    	zPyDecoder.decodec             C   s   dS )zV
        Override to perform decoder specific cleanup

        :returns: None
        Nr   )r/   r   r   r   rR   d  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   rO   l  s    zPyDecoder.setfdNc             C   sÖ   || _ |r|\}}}}nd\}}}}|dkrJ|dkrJ| j j\| j_| j_n(|| j_|| j_|| | j_|| | j_| jjdksŠ| jjdkr’tdƒ‚| jj| jj | j jd ksÊ| jj| jj | j jd krÒtdƒ‚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   )rF   r,   r—   r’   r“   r”   r•   Ú
ValueError)r/   rF   rT   Zx0Zy0Úx1Úy1r   r   r   rM   u  s    zPyDecoder.setimagec             C   sd   |s
| j }tj| j d|ƒ}|j| j| jjƒ ƒ |j|ƒ}|d dkrLtdƒ‚|d dkr`tdƒ‚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
        r7   r   znot enough image datar   zcannot decode image dataN)	r+   r   rL   rM   rF   r—   rT   rP   rœ   )r/   rp   Úrawmodery   r[   r   r   r   Ú
set_as_raw˜  s    

zPyDecoder.set_as_raw)N)N)rc   rd   re   rf   rš   r   r™   ÚpropertyrN   rP   rR   rO   rM   r    r   r   r   r   r–   ;  s   		
#r–   i   éÿÿÿÿéþÿÿÿr=   iøÿÿÿi÷ÿÿÿ)r   )rt   r)   rA   Ú r   Z_utilr   r   rŒ   rQ   r   r   r   r   rh   ro   r‹   r�   r‘   r–   r   r   r   r   Ú<module>   s.   
 i 
>