o
    _#ûaSR  ã                   @   sÊ   d dl Z 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œ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 errorzdecoding errorzunknown errorzbad configurationzout of memory error)éÿÿÿÿéþÿÿÿéýÿÿÿiøÿÿÿi÷ÿÿÿc                 C   sH   zt j | ¡}W n ty   t | ¡}Y nw |sd| › �}t|d ƒ‚)Nzdecoder error z when reading image file)r   ÚcoreÚgetcodecstatusÚAttributeErrorÚERRORSÚgetÚOSError)ÚerrorÚmessage© r   ú//usr/lib/python3/dist-packages/PIL/ImageFile.pyÚraise_oserror<   s   ÿ
r   c                 C   s   | d S )Né   r   )Útr   r   r   Ú	_tilesortF   ó   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 ƒ  ¡  d| _d | _d | _	 d| _d| _t| _t	|ƒr)t
|dƒ| _|| _d| _n	|| _|| _d | _z/z|  ¡  W n tttttjfyP } zt|ƒ|‚d }~ww | jr[| jd dkr_tdƒ‚W d S  tyq   | jrp| j ¡  ‚ w )Nr   r   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   S   sH   
û
€ù	ÿ
üzImageFile.__init__c                 C   s,   | j r| j S | jd urtj | j ¡ ¡S d S ©N)r   Úformatr   ÚMIMEr   Úupper©r1   r   r   r   Úget_format_mimetype‚   s
   
ÿzImageFile.get_format_mimetypec                 C   s   | j r| j ¡  d| _dS )zCheck file integrityN)r%   r#   r0   r9   r   r   r   Úverifyˆ   s   

zImageFile.verifyc                 C   sj  | j du r	tdƒ‚tj | ¡}| j s|S d| _| jo t| j ƒdk}|o(ttdƒ }d}z| j	}d}W n t
y?   | jj}Y nw z| j}d}W n t
yT   | jj}Y nw |rÅ| j d \}}}}	|dkrÅt|	ƒdkrÅ|	d | jkrÅ|	d tjv rÅz=ddl}
t| jƒ�}|
j| ¡ d|
jd	�| _W d  ƒ n1 s™w   Y  tj | j| j|||	¡| _d}| jr´d| j_W n t
ttfyÄ   d| _Y nw |  ¡  d
}| j�s…| j jtd� z| j}W n t
yæ   d}Y nw dd„ t  !| j dd„ ¡D ƒ| _ | j D ]‹\}}}}	t "| j||	| j#¡}zv||ƒ | $| j|¡ |j%�r'| &| j¡ | 'd¡\}}nS|}	 z|| j(ƒ}W n t)t*j+f�yO } zt,�rFW Y d}~n4tdƒ|‚d}~ww |�sat,�rWn#tdt|ƒ› d�ƒ‚|| }| '|¡\}}|dk �rrn||d… }�q*W | -¡  qù| -¡  w g | _ || _.|  /¡  | j0�rœ| j1�rœ| j 2¡  d| _| j�s¯t,�s¯|dk �r¯t3|ƒ tj | ¡S )z"Load image data based on tile listNzcannot load this imager   Úpypy_version_infor   FÚrawé   )Úaccessr   ©Úkeyó    c                 S   s   g | ]
\}}t |ƒd  ‘qS )r   )Úlist)Ú.0Ú_Útilesr   r   r   Ú
<listcomp>×   s    
ÿÿz"ImageFile.load.<locals>.<listcomp>c                 S   s   | d | d | d fS )Nr   r   r>   r   )r   r   r   r   Ú<lambda>Ú   s    z ImageFile.load.<locals>.<lambda>Tzimage file is truncatedzimage file is truncated (z bytes not processed))4r   r   r   ÚloadÚmapr$   ÚlenÚhasattrÚsysÚ	load_readr
   r#   ÚreadÚ	load_seekÚseekr-   Ú	_MAPMODESÚmmapr"   ÚfilenoÚACCESS_READr   Ú
map_bufferr.   ÚimÚpaletteÚdirtyÚImportErrorÚload_prepareÚsortr   Útile_prefixÚ	itertoolsÚgroupbyÚ_getdecoderr   ÚsetimageÚpulls_fdÚsetfdÚdecoder!   r'   r+   r   ÚLOAD_TRUNCATED_IMAGESÚcleanupr   Úload_endr%   Ú!_close_exclusive_fp_after_loadingr0   r   )r1   ÚpixelÚuse_mmapr   rO   rQ   Údecoder_nameÚextentsÚoffsetÚargsrS   r#   Úerr_codeÚprefixÚdecoderÚstatusÚbÚsÚeÚnr   r   r   rI   ‘   sÊ   
ÿÿÿÿ€
ÿ
ÿ
ÿþÿ
€ûÿÿ
é€
zImageFile.loadc                 C   sT   | j r| j j| jks| j j| jkrtj | j| j¡| _ | jdkr(tj | ¡ d S d S )NÚP)rW   r-   r.   r   r   ÚnewrI   r9   r   r   r   r[     s
   "
ÿzImageFile.load_preparec                 C   s   d S r5   r   r9   r   r   r   rg     s   zImageFile.load_endc                 C   sB   || j k st| dƒr| jd u s|| j| j  krtdƒ‚|  ¡ |kS )NÚ	_n_framesz attempt to seek outside sequence)r   rL   ry   Ún_framesr*   Útell)r1   Úframer   r   r   Ú_seek_check(  s   
ü
zImageFile._seek_check)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r:   r;   rI   r[   rg   r}   Ú__classcell__r   r   r3   r   r   P   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   ó   t dƒ‚)Nz+StubImageFile subclass must implement _open©ÚNotImplementedErrorr9   r   r   r   r&   ?  s   zStubImageFile._openc                 C   sL   |   ¡ }|d u rtd| j› d�ƒ‚| | ¡}|d usJ ‚|j| _|j| _d S )Nzcannot find loader for this z file)Ú_loadr   r6   rI   r4   Ú__dict__)r1   ÚloaderÚimager   r   r   rI   B  s   
zStubImageFile.loadc                 C   r„   )z (Hook) Find actual image loader.z+StubImageFile subclass must implement _loadr…   r9   r   r   r   r‡   L  r   zStubImageFile._loadN)r~   r   r€   r�   r&   rI   r‡   r   r   r   r   rƒ   7  s
    
rƒ   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u s	J 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)Údatar9   r   r   r   Úreset^  s   zParser.resetc                 C   sÖ  | j rdS | jdu r|| _n| j| | _| jrh| jdkr=tt| jƒ| jƒ}| j|d… | _| j| | _| jdks;| js=dS | j | j¡\}}|dk r^d| _d| _ |dk r\d| _t|ƒ ndS | j|d… | _dS | jrmdS zt	 
| j¡�}t |¡}W d  ƒ n1 s„w   Y  W n
 ty”   Y dS w t|dƒpžt|dƒ}|s¨t|jƒdkr¬d| _n:| ¡  |jd \}}}	}
g |_t |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 OSError: If the parser failed to parse the image file.
        Nr   r   rP   rN   )ÚfinishedrŒ   rq   rm   ÚminrK   rd   rŠ   r   ÚioÚBytesIOr   r"   r   rL   r   r[   r`   r-   r   ra   rW   )r1   rŒ   Úskiprv   ru   r#   rW   ÚflagÚdÚoÚar   r   r   Úfeedf  sX   	


ÿ€þ
zParser.feedc                 C   s   | S r5   r   r9   r   r   r   Ú	__enter__¶  s   zParser.__enter__c                 G   s   |   ¡  d S r5   )r0   ©r1   rn   r   r   r   Ú__exit__¹  s   zParser.__exit__c              	   C   sœ   | j r|  d¡ d | _| _ | jstdƒ‚| jstdƒ‚| jrKt | j¡�}zt 	|¡| _W | j 
¡  n| j 
¡  w W d  ƒ | jS 1 sFw   Y  | jS )a  
        (Consumer) Close the stream.

        :returns: An image object.
        :exception OSError: If the parser failed to parse the image file either
                            because it cannot be identified or cannot be
                            decoded.
        rB   Nzimage was incompletezcannot parse this image)rq   r—   rŒ   rŽ   r   rŠ   r�   r‘   r   r"   rI   )r1   r#   r   r   r   r0   ¼  s    


üûzParser.close)r~   r   r€   r�   ÚincrementalrŠ   rŒ   rq   rm   rŽ   r�   r—   r˜   rš   r0   r   r   r   r   r‹   Q  s    Pr‹   c              
   C   sÎ  |   ¡  t| dƒsd| _|jtd� tt|| jd d ƒ}z
| ¡ }| 	¡  W nj t
tjfy‘ } z[|D ]R\}}}}	t | j||	| j¡}|dkrM| |¡ | | j|¡ |jrc| |¡ | ¡ \}
}n	 | |¡\}
}}| |¡ |rtnqd|dk r‚td|› d�ƒ|‚| ¡  q4W Y d	}~nMd	}~ww |D ]E\}}}}	t | j||	| j¡}|dkr­| |¡ | | j|¡ |jrÃ| |¡ | ¡ \}
}n| ||¡}|dk rÕtd|› d�ƒ‚| ¡  q”t|d
ƒrå| 	¡  d	S 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
    Úencoderconfigr   r@   r   é   Tzencoder error z when writing image fileNÚflush)rI   rL   rœ   r\   r   Úmaxr    r.   rT   rž   r
   r�   ÚUnsupportedOperationr   Ú_getencoderr-   rQ   ra   rW   Ú	pushes_fdrc   Úencode_to_pyfdÚencodeÚwriter   rf   Úencode_to_file)rW   r#   r   ÚbufsizeÚfhÚexcru   rs   r•   r–   Úlrt   r”   r   r   r   Ú_saveÜ  sX   	



ü
ð€þ



ÿr«   c                 C   s¢   |dkrdS |t kr|  |¡}t|ƒ|k rtdƒ‚|S g }|}|dkr=|  t|t ƒ¡}|s.n| |¡ |t|ƒ8 }|dks#tdd„ |D ƒƒ|k rLtdƒ‚d |¡S )a»  
    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 <i>size</i> bytes of data.

    Raises an OSError if the file is truncated and the read cannot be completed

    r   rB   zTruncated File Readc                 s   s   � | ]}t |ƒV  qd S r5   )rK   )rD   r”   r   r   r   Ú	<genexpr>3  s   € z_safe_read.<locals>.<genexpr>)Ú	SAFEBLOCKrO   rK   r   r�   ÚappendÚsumÚjoin)r#   r.   rŒ   Úremaining_sizeÚ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Úyoffr9   r   r   r   r   9  s   
zPyCodecState.__init__c                 C   s    | j | j| j | j | j| j fS r5   )r·   r¸   rµ   r¶   r9   r   r   r   rl   ?  s    zPyCodecState.extentsN)r~   r   r€   r   rl   r   r   r   r   r´   8  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 :meth:`decode` method.

    See :ref:`Writing Your Own File Decoder in Python<file-decoders-py>`
    Fc                 G   s(   d | _ tƒ | _d | _|| _|  |¡ d S r5   )rW   r´   ÚstateÚfdr-   Úinit)r1   r-   rn   r   r   r   r   M  s
   zPyDecoder.__init__c                 C   ó
   || _ dS )zš
        Override to perform decoder specific initialization

        :param args: Array of args items from the tile entry
        :returns: None
        N)rn   r™   r   r   r   r¼   T  ó   
zPyDecoder.initc                 C   s   | j S r5   )Ú	_pulls_fdr9   r   r   r   rb   ]  s   zPyDecoder.pulls_fdc                 C   s   t ƒ ‚)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 :data:`.ImageFile.ERRORS`.
        r…   )r1   Úbufferr   r   r   rd   a  s   	zPyDecoder.decodec                 C   s   dS )zV
        Override to perform decoder specific cleanup

        :returns: None
        Nr   r9   r   r   r   rf   l  s   zPyDecoder.cleanupc                 C   r½   )z�
        Called from ImageFile to set the python file-like object

        :param fd: A python file-like object
        :returns: None
        N)r»   )r1   r»   r   r   r   rc   t  r¾   zPyDecoder.setfdNc                 C   sÖ   || _ |r|\}}}}nd\}}}}|dkr%|dkr%| j j\| j_| j_n|| j_|| j_|| | j_|| | j_| jjdksE| jjdkrItdƒ‚| jj| jj | j jd kse| jj| jj | j jd krit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   r   r   r   r   zSize cannot be negativer   z Tile cannot extend outside imageN)rW   r.   rº   rµ   r¶   r·   r¸   Ú
ValueError)r1   rW   rl   Úx0Úy0Úx1Úy1r   r   r   ra   }  s    þzPyDecoder.setimagec                 C   sd   |s| j }t | j d|¡}| | j| j ¡ ¡ | |¡}|d dkr&tdƒ‚|d dkr0t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
        r=   r   znot enough image datar   zcannot decode image dataN)	r-   r   r`   ra   rW   rº   rl   rd   rÁ   )r1   rŒ   Úrawmoder”   rt   r   r   r   Ú
set_as_raw   s   

ÿzPyDecoder.set_as_rawr5   )r~   r   r€   r�   r¿   r   r¼   Úpropertyrb   rd   rf   rc   ra   rÇ   r   r   r   r   r¹   C  s    	

	#r¹   )r   )r�   r^   r+   rM   Ú r   Ú_utilr   r    r­   re   r   r   r   r   rƒ   r‹   r«   r³   r´   r¹   r   r   r   r   Ú<module>   s8   û

 h 
;!