U
    ­A›dó[  ã                   @   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d„Zdd„ ZG dd„ dƒZG d d!„ d!ƒZG d"d#„ d#eƒZG d$d%„ d%eƒZdS )(é    Né   )ÚImage)Úis_pathi   i   Fzimage buffer overrun errorzdecoding errorzunknown errorzbad configurationzout of memory error)éÿÿÿÿéþÿÿÿéýÿÿÿéøÿÿÿi÷ÿÿÿc                 C   sR   zt j | ¡}W n tk
r.   t | ¡}Y nX |s>d| › �}|d7 }t|ƒ‚d S )Nzdecoder error z when reading image file)r   ÚcoreZgetcodecstatusÚAttributeErrorÚERRORSÚgetÚOSError)ÚerrorÚmsg© r   úF/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/PIL/ImageFile.pyÚraise_oserror@   s    
r   c                 C   s   | d S )Né   r   )Útr   r   r   Ú	_tilesortK   s    r   c                       s^   e Zd ZdZd‡ fdd„	Zdd„ Z‡ f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	|ƒrPt
|dƒ| _|| _d| _n|| _|| _d | _zxz|  ¡  W n8 tttttjfk
r¨ } zt|ƒ|‚W 5 d }~X Y nX | jrÌ| jd dksÌ| jd dkrØd}t|ƒ‚W n( tk
�r   | jrü| j ¡  ‚ Y nX d S )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#   Úvr   ©Ú	__class__r   r   r   X   s@    
û"
zImageFile.__init__c                 C   s,   | j r| j S | jd k	r(tj | j ¡ ¡S d S ©N)r   Úformatr   ÚMIMEr   Úupper©r0   r   r   r   Úget_format_mimetypeˆ   s    
zImageFile.get_format_mimetypec                    s   g | _ tƒ  |¡ d S r4   )r   r   Ú__setstate__)r0   Ústater2   r   r   r:   Ž   s    zImageFile.__setstate__c                 C   s   | j r| j ¡  d| _dS )zCheck file integrityN)r$   r"   r/   r8   r   r   r   Úverify’   s    
zImageFile.verifyc                 C   s¬  | j dkrd}t|ƒ‚tj | ¡}| j s,|S d| _| joDt| j ƒdk}|oTttdƒ }d}z| j	}d}W n t
k
r„   | jj}Y nX z| j}d}W n t
k
r°   | jj}Y nX |�r´| j d \}}}	}
|dk�r´t|
ƒdk�r´|
d | jk�r´|
d tjk�r´zŽddl}t| jƒ�}|j| ¡ d|jd	�| _W 5 Q R X |	| jd |
d   | j ¡ k�r`t‚tj | j| j||	|
¡| _d}| j�rŽd| j_W n" t
ttfk
�r²   d| _Y nX |  ¡  d
}| j�sL| j jtd� z
| j}W n t
k
�rú   d}Y nX dd„ t  !| j dd„ ¡D ƒ| _ | j D �](\}}}	}
||	ƒ t "| j||
| j#¡}zò| %| j|¡ |j&�r~| '| j¡ | (d¡d }n¼|}z|| j)ƒ}W nJ t*t+j,fk
�rÚ } z$t-�r¼W Y ¢�q:nd}t|ƒ|‚W 5 d}~X Y nX |�st-�rî�q:ndt|ƒ› d�}t|ƒ‚|| }| (|¡\}}|dk �r*�q:||d… }�q‚W 5 | $¡  X �q g | _ || _.|  /¡  | j0�rz| j1�rz| 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Ú_Z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>è   rC   z ImageFile.load.<locals>.<lambda>z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,   Z	_MAPMODESÚmmapr!   ÚfilenoÚACCESS_READr-   r	   Z
map_bufferÚimÚpaletteZdirtyÚImportErrorÚload_prepareÚsortr   Ztile_prefixÚ	itertoolsÚgroupbyÚ_getdecoderr   ÚcleanupÚsetimageÚpulls_fdÚsetfdÚdecoder    r&   r*   r   ÚLOAD_TRUNCATED_IMAGESr   Úload_endr$   Z!_close_exclusive_fp_after_loadingr/   r   )r0   r   ZpixelZuse_mmapr   rO   rQ   Zdecoder_nameÚextentsÚoffsetÚargsrR   r"   Zerr_codeÚprefixÚdecoderÚbÚsÚeÚnr   r   r   rI   ›   sÐ    
ÿ
þýü""    ÿ

 ÿþ   ÿÿ

zImageFile.loadc                 C   sP   | j r"| j j| jks"| j j| jkr6tj | j| j¡| _ | jdkrLtj | ¡ d S )NÚP)rU   r,   r-   r   r	   ÚnewrI   r8   r   r   r   rX   $  s    "
zImageFile.load_preparec                 C   s   d S r4   r   r8   r   r   r   rc   ,  s    zImageFile.load_endc                 C   sF   || j k s.t| dƒr| jd ks:|| j| j  kr:d}t|ƒ‚|  ¡ |kS )NÚ	_n_framesz attempt to seek outside sequence)r   rL   ro   Zn_framesr)   Útell)r0   Úframer   r   r   r   Ú_seek_check8  s    ÿûûú	zImageFile._seek_check)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r9   r:   r<   rI   rX   rc   rr   Ú__classcell__r   r   r2   r   r   U   s   0	 
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   d}t |ƒ‚d S )Nz+StubImageFile subclass must implement _open©ÚNotImplementedError©r0   r   r   r   r   r%   P  s    zStubImageFile._openc                 C   sT   |   ¡ }|d kr&d| j› d�}t|ƒ‚| | ¡}|d k	s<t‚|j| _|j| _| ¡ S )Nzcannot find loader for this z file)Ú_loadr5   r   rI   ÚAssertionErrorr3   Ú__dict__)r0   Úloaderr   Úimager   r   r   rI   T  s    
zStubImageFile.loadc                 C   s   d}t |ƒ‚dS )z (Hook) Find actual image loader.z+StubImageFile subclass must implement _loadNry   r{   r   r   r   r|   `  s    zStubImageFile._loadN)rs   rt   ru   rv   r%   rI   r|   r   r   r   r   rx   H  s   rx   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)Údatar}   r8   r   r   r   Úresets  s    zParser.resetc              	   C   sÈ  | j r
dS | jdkr|| _n| j| | _| jrÎ| jdkrztt| jƒ| jƒ}| j|d… | _| j| | _| jdksv| jszdS | j | j¡\}}|dk r¼d| _d| _ |dk r¸d| _t|ƒ ndS | j|d… | _nö| jrÖnîz&t	 
| j¡�}t |¡}W 5 Q R X W n tk
�r   Y n²X t|dƒ�p(t|dƒ}|�s@t|jƒdk�rHd| _nv| ¡  |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‚   rh   re   ÚminrK   ra   r€   r   ÚioÚBytesIOr   r!   r   rL   r   rX   r\   r,   r   r^   rU   )r0   r‚   Úskiprl   rk   r"   rU   ÚflagÚdÚoÚar   r   r   Úfeed{  sR    	


zParser.feedc                 C   s   | S r4   r   r8   r   r   r   Ú	__enter__È  s    zParser.__enter__c                 G   s   |   ¡  d S r4   )r/   ©r0   rf   r   r   r   Ú__exit__Ë  s    zParser.__exit__c              	   C   s‚   | j r.|  d¡ d | _| _ | js.d}t|ƒ‚| js@d}t|ƒ‚| jr|t | j¡�$}zt	 
|¡| _W 5 | j ¡  X W 5 Q R X | 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.
        rC   Nzimage was incompletezcannot parse this image)rh   r�   r‚   r„   r   r€   r†   r‡   rI   r   r!   )r0   r   r"   r   r   r   r/   Î  s    

zParser.close)rs   rt   ru   rv   Úincrementalr€   r‚   rh   re   r„   rƒ   r�   rŽ   r�   r/   r   r   r   r   r�   f  s   Mr�   c              
   C   s°   |   ¡  t| dƒsd| _|jtd� tt|| jd d ƒ}z$| ¡ }| 	¡  t
| ||||ƒ W n: ttjfk
r˜ } zt
| |||d|ƒ W 5 d}~X Y nX t|dƒr¬| 	¡  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   rA   r   é   NÚflush)rI   rL   r’   rY   r   Úmaxr   r-   rS   r”   Ú_encode_tiler
   r†   ÚUnsupportedOperation)rU   r"   r   ÚbufsizeÚfhÚexcr   r   r   Ú_saveð  s    	
$
r›   c              	   C   sÐ   |D ]Æ\}}}}	|dkr"|  |¡ t | j||	| j¡}
zˆ|
 | j|¡ |
jrd|
 	|¡ |
 
¡ d }n:|r’|
 |¡dd … \}}| |¡ |rhqžqhn|
 ||¡}|dk r¼d|› d�}t|ƒ|‚W 5 |
 ¡  X qd S )Nr   r   zencoder error z when writing image file)rQ   r   Z_getencoderr,   r’   r]   r^   rU   Ú	pushes_fdr`   Úencode_to_pyfdÚencodeÚwriteÚencode_to_filer   )rU   r"   r   r˜   r™   rš   rk   ri   r‹   rŒ   ÚencoderÚerrcoder‚   r   r   r   r   r–     s&    


r–   c                 C   s¤   |dkrdS |t kr:|  |¡}t|ƒ|k r6d}t|ƒ‚|S g }|}|dkrx|  t|t ƒ¡}|s`qx| |¡ |t|ƒ8 }qBtdd„ |D ƒƒ|k ršd}t|ƒ‚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   rC   zTruncated File Readc                 s   s   | ]}t |ƒV  qd S r4   )rK   )rE   rŠ   r   r   r   Ú	<genexpr>E  s     z_safe_read.<locals>.<genexpr>)Ú	SAFEBLOCKrO   rK   r   r…   ÚappendÚsumÚjoin)r"   r-   r‚   r   Z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Úyoffr8   r   r   r   r   L  s    zPyCodecState.__init__c                 C   s    | j | j| j | j | j| j fS r4   )r­   r®   r«   r¬   r8   r   r   r   rd   R  s    zPyCodecState.extentsN)rs   rt   ru   r   rd   r   r   r   r   rª   K  s   rª   c                   @   s6   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zdd
d„Zd	S )ÚPyCodecc                 G   s(   d | _ tƒ | _d | _|| _|  |¡ d S r4   )rU   rª   r;   Úfdr,   Úinit)r0   r,   rf   r   r   r   r   W  s
    zPyCodec.__init__c                 C   s
   || _ dS )z˜
        Override to perform codec specific initialization

        :param args: Array of args items from the tile entry
        :returns: None
        N)rf   r�   r   r   r   r±   ^  s    zPyCodec.initc                 C   s   dS )zT
        Override to perform codec specific cleanup

        :returns: None
        Nr   r8   r   r   r   r]   g  s    zPyCodec.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°   )r0   r°   r   r   r   r`   o  s    zPyCodec.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–d}t|ƒ‚| jj| jj | j jd ksÎ| jj| jj | j jd krÚd}t|ƒ‚dS )zø
        Called from ImageFile to set the core output image for the codec

        :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)rU   r-   r;   r«   r¬   r­   r®   Ú
ValueError)r0   rU   rd   Zx0Zy0Úx1Úy1r   r   r   r   r^   x  s&    ÿþzPyCodec.setimage)N)rs   rt   ru   r   r±   r]   r`   r^   r   r   r   r   r¯   V  s
   		r¯   c                   @   s2   e Zd ZdZdZedd„ ƒZ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 Codec in Python<file-codecs-py>`
    Fc                 C   s   | j S r4   )Ú	_pulls_fdr8   r   r   r   r_   ¨  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 -1 for the bytes consumed.
            Err codes are from :data:`.ImageFile.ERRORS`.
        Nry   )r0   Úbufferr   r   r   ra   ¬  s    	zPyDecoder.decodeNc                 C   sl   |s
| j }t | j d|¡}| | j| j ¡ ¡ | |¡}|d dkrPd}t|ƒ‚|d dkrhd}t|ƒ‚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\   r^   rU   r;   rd   ra   r²   )r0   r‚   ÚrawmoderŠ   rj   r   r   r   r   Ú
set_as_raw·  s    

zPyDecoder.set_as_raw)N)	rs   rt   ru   rv   r¶   Úpropertyr_   ra   r¹   r   r   r   r   rµ   ž  s   
rµ   c                   @   s8   e Zd ZdZdZedd„ ƒZdd„ Zdd„ Zd	d
„ Z	dS )Ú	PyEncoderzË
    Python implementation of a format encoder. Override this class and
    add the decoding logic in the :meth:`encode` method.

    See :ref:`Writing Your Own File Codec in Python<file-codecs-py>`
    Fc                 C   s   | j S r4   )Ú
_pushes_fdr8   r   r   r   rœ   Ù  s    zPyEncoder.pushes_fdc                 C   s
   t ƒ ‚dS )a   
        Override to perform the encoding process.

        :param bufsize: Buffer size.
        :returns: A tuple of ``(bytes encoded, errcode, bytes)``.
            If finished with encoding return 1 for the error code.
            Err codes are from :data:`.ImageFile.ERRORS`.
        Nry   )r0   r˜   r   r   r   rž   Ý  s    	zPyEncoder.encodec                 C   s2   | j s
dS |  d¡\}}}|r*| j |¡ ||fS )zø
        If ``pushes_fd`` is ``True``, then this method will be used,
        and ``encode()`` will only be called once.

        :returns: A tuple of ``(bytes consumed, errcode)``.
            Err codes are from :data:`.ImageFile.ERRORS`.
        )r   r   r   )rœ   rž   r°   rŸ   )r0   Zbytes_consumedr¢   r‚   r   r   r   r�   è  s    zPyEncoder.encode_to_pyfdc                 C   s<   d}|dkr8|   |¡\}}}|dkr| ||d… ¡ q|S )zì
        :param fh: File handle.
        :param bufsize: Buffer size.

        :returns: If finished successfully, return 0.
            Otherwise, return an error code. Err codes are from
            :data:`.ImageFile.ERRORS`.
        r   N)rž   rŸ   )r0   r™   r˜   r¢   ÚstatusÚbufr   r   r   r    ÷  s    	zPyEncoder.encode_to_fileN)
rs   rt   ru   rv   r¼   rº   rœ   rž   r�   r    r   r   r   r   r»   Ï  s   
r»   )r   )N)r†   rZ   r*   rM   Ú r   Z_utilr   r   r¤   rb   r   r   r   r   rx   r�   r›   r–   r©   rª   r¯   rµ   r»   r   r   r   r   Ú<module>   s8   û
 t 

#H1