U
    ûØfe_  ã                   @  s@  d dl mZ d dlZd dlZd dlZd dlZd dlmZmZm	Z	 ddl
m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dd„ ZG dd„ de	ƒZG dd„ dejƒZG dd„ deƒZG dd„ dƒZd1dd œd!d"„Zd2d#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/d0„ d0eƒZ!dS )3é    )ÚannotationsN)ÚIOÚAnyÚ
NamedTupleé   )ÚImage)Ú	deprecate)Úis_pathi   i   Fzimage buffer overrun errorzdecoding errorzunknown errorzbad configurationzout of memory error)éÿÿÿÿéþÿÿÿéýÿÿÿéøÿÿÿi÷ÿÿÿc                C  sj   zt j | ¡}W n tk
r.   t | ¡}Y nX |sJ|r<dnd› d| › }|d|rVdnd› d7 }t|ƒS )NÚencoderÚdecoderz error z when ÚwritingÚreadingz image file)r   ÚcoreZgetcodecstatusÚAttributeErrorÚERRORSÚgetÚOSError)Úerrorr   Úmsg© r   ú4/tmp/pip-target-lpfmz8o1/lib/python/PIL/ImageFile.pyÚ_get_oserrorC   s    r   c                 C  s   t dddd t| dd‚d S )NÚraise_oserroré   z€It is only useful for translating error codes returned by a codec's decode() method, which ImageFile already does automatically.)ÚactionF©r   )r   r   )r   r   r   r   r   N   s    ýr   c                 C  s   | d S )Né   r   )Útr   r   r   Ú	_tilesortX   s    r"   c                   @  s.   e Zd ZU ded< ded< ded< ded< d	S )
Ú_TileÚstrZ
codec_nameztuple[int, int, int, int]ÚextentsÚintÚoffsetztuple[Any, ...] | str | NoneÚargsN)Ú__name__Ú
__module__Ú__qualname__Ú__annotations__r   r   r   r   r#   ]   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)Úselfr9   r:   Úvr   ©Ú	__class__r   r   r0   l   s@    
û"
zImageFile.__init__c                 C  s,   | j r| j S | jd k	r(tj | j ¡ ¡S d S ©N)r2   Úformatr   ÚMIMEr   Úupper©rG   r   r   r   Úget_format_mimetypeœ   s    
zImageFile.get_format_mimetypec                   s   g | _ tƒ  |¡ d S rK   )r3   r/   Ú__setstate__)rG   ÚstaterI   r   r   rQ   ¢   s    zImageFile.__setstate__c                 C  s   | j r| j ¡  d| _dS )zCheck file integrityN)r;   r9   rF   rO   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 \}}}	}
t|
tƒrÞ|
ddf}
|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|d
}t|ƒ‚tj | j| j||	|
¡| _d}| jrªd| j_W n" t
ttfk
rÎ   d| _Y nX |  ¡  d}| jsh| 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 ¢qVnd}t|ƒ|‚W 5 d}~X Y nX |s"t/r
qVndt|ƒ› d}t|ƒ‚|| }| *|¡\}}|dk rFqV||d… }qžW 5 | &¡  X q<g | _ || _0|  1¡  | j2r–| j3r–| j 4¡  d| _| jsÀt/sÀ|dk rÀt5|dd‚tj | ¡S )z"Load image data based on tile listNzcannot load this imager   Úpypy_version_infor   FÚrawé   )Úaccesszbuffer is not large enoughr   ©Ú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   rV   r   ©r3   r   r   r   Ú<lambda>þ   rZ   z ImageFile.load.<locals>.<lambda>zimage file is truncatedzimage file is truncated (z bytes not processed)r   )6r3   r   r   ÚloadÚmapr:   ÚlenÚhasattrÚsysÚ	load_readr   r9   ÚreadÚ	load_seekÚseekÚ
isinstancer$   rC   Z	_MAPMODESÚmmapr8   ÚfilenoÚACCESS_READrD   r   Z
map_bufferÚimÚpaletteZdirtyÚImportErrorÚload_prepareÚsortr"   Ztile_prefixÚ	itertoolsÚgroupbyÚ_getdecoderr5   ÚcleanupÚsetimageÚpulls_fdÚsetfdÚdecoder7   r=   rA   r   ÚLOAD_TRUNCATED_IMAGESr4   Úload_endr;   Z!_close_exclusive_fp_after_loadingrF   r   )rG   r   ZpixelZuse_mmapr4   rg   ri   Zdecoder_namer%   r'   r(   rk   r9   Zerr_codeÚprefixr   ÚbÚsÚeÚnr   r   r   ra   ¯   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)rn   rC   rD   r   r   Únewra   rO   r   r   r   rq   :  s    "
zImageFile.load_preparec                 C  s   d S rK   r   rO   r   r   r   r|   B  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)r1   rd   r„   Zn_framesr@   Útell)rG   Úframer   r   r   r   Ú_seek_checkN  s    ÿûûú	zImageFile._seek_check)NN)r)   r*   r+   Ú__doc__r0   rP   rQ   rS   ra   rq   r|   r‡   Ú__classcell__r   r   rI   r   r-   i   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©rG   r   r   r   r   r<   f  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)Ú_loadrL   r   ra   ÚAssertionErrorrJ   Ú__dict__)rG   Úloaderr   Úimager   r   r   ra   j  s    
zStubImageFile.loadc                 C  s   d}t |ƒ‚dS )z (Hook) Find actual image loader.z+StubImageFile subclass must implement _loadNr‹   r   r   r   r   rŽ   v  s    zStubImageFile._loadN)r)   r*   r+   rˆ   r<   ra   rŽ   r   r   r   r   rŠ   ^  s   rŠ   c                   @  sZ   e Zd ZU dZdZdZded< 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.
    NzImage.Image | Noner’   r   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   rO   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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|dd‚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ƒ}|sDt|jƒdkrLd| _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   Fr   rh   rf   )Úfinishedr”   r   r'   Úminrc   rz   r’   r   ÚioÚBytesIOr   r8   r   rd   r3   rq   ru   rC   r5   rw   rn   )rG   r”   Úskipr   r€   r9   rn   ÚflagÚdÚoÚar   r   r   Úfeed‘  sR    	

zParser.feedc                 C  s   | S rK   r   rO   r   r   r   Ú	__enter__Ý  s    zParser.__enter__c                 G  s   |   ¡  d S rK   )rF   ©rG   r(   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.
        rZ   Nzimage was incompletezcannot parse this image)r   rŸ   r”   r–   r   r’   r˜   r™   ra   r   r8   )rG   r   r9   r   r   r   rF   ã  s    

zParser.close)r)   r*   r+   rˆ   Úincrementalr’   r,   r”   r   r'   r–   r•   rŸ   r    r¢   rF   r   r   r   r   r“   |  s   
Lr“   ÚNone)Úreturnc              
   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   rX   r   é   NÚflush)ra   rd   r¦   rr   r"   Úmaxr6   rD   rl   r¨   Ú_encode_tiler   r˜   ÚUnsupportedOperation)rn   r9   r3   ÚbufsizeÚfhÚexcr   r   r   Ú_save  s    	
$
r¯   zlist[_Tile]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´t|dd|‚W 5 |
 ¡  X qd S )Nr   r   Tr   )ri   r   Z_getencoderrC   r¦   rv   rw   rn   Ú	pushes_fdry   Úencode_to_pyfdÚencodeÚwriteÚencode_to_filer   )rn   r9   r3   r¬   r­   r®   Zencoder_namer%   r'   r(   r   Úerrcoder”   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   rZ   zTruncated File Readc                 s  s   | ]}t |ƒV  qd S rK   )rc   )r\   rœ   r   r   r   Ú	<genexpr>Y  s     z_safe_read.<locals>.<genexpr>)Ú	SAFEBLOCKrg   rc   r   r—   ÚappendÚsumÚjoin)r9   rD   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ÚyoffrO   r   r   r   r0   `  s    zPyCodecState.__init__c                 C  s    | j | j| j | j | j| j fS rK   )rÀ   rÁ   r¾   r¿   rO   r   r   r   r%   f  s    zPyCodecState.extentsN)r)   r*   r+   r0   r%   r   r   r   r   r½   _  s   r½   c                   @  s@   e Zd ZU ded< dd„ Zdd„ Zdd„ Zd	d
„ Zddd„ZdS )ÚPyCodeczIO[bytes] | NoneÚfdc                 G  s(   d | _ tƒ | _d | _|| _|  |¡ d S rK   )rn   r½   rR   rÃ   rC   Úinit)rG   rC   r(   r   r   r   r0   m  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)r(   r¡   r   r   r   rÄ   t  s    zPyCodec.initc                 C  s   dS )zT
        Override to perform codec specific cleanup

        :returns: None
        Nr   rO   r   r   r   rv   }  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Ã   )rG   rÃ   r   r   r   ry   …  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)rn   rD   rR   r¾   r¿   rÀ   rÁ   Ú
ValueError)rG   rn   r%   Zx0Zy0Úx1Úy1r   r   r   r   rw   Ž  s&    ÿþzPyCodec.setimage)N)	r)   r*   r+   r,   r0   rÄ   rv   ry   rw   r   r   r   r   rÂ   j  s   
		rÂ   c                   @  s:   e Zd ZdZdZedd„ ƒZdd„ Zddd	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 rK   )Ú	_pulls_fdrO   r   r   r   rx   ¾  s    zPyDecoder.pulls_fdc                 C  s   d}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`.
        zunavailable in base decoderNr‹   )rG   Úbufferr   r   r   r   rz   Â  s    	zPyDecoder.decodeNÚbytesr¤   )r”   r¥   c                 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
        rU   r   znot enough image datar   zcannot decode image dataN)	rC   r   ru   rw   rn   rR   r%   rz   rÅ   )rG   r”   Úrawmoderœ   r   r   r   r   r   Ú
set_as_rawÎ  s    

zPyDecoder.set_as_raw)N)	r)   r*   r+   rˆ   rÉ   Úpropertyrx   rz   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 rK   )Ú
_pushes_fdrO   r   r   r   r°   ð  s    zPyEncoder.pushes_fdc                 C  s   d}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`.
        zunavailable in base encoderNr‹   )rG   r¬   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³   )rG   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³   )rG   r­   r¬   rµ   ÚstatusÚbufr   r   r   r´     s    	zPyEncoder.encode_to_fileN)
r)   r*   r+   rˆ   rÐ   rÎ   r°   r²   r±   r´   r   r   r   r   rÏ   æ  s   
rÏ   )r   )N)"Ú
__future__r   r˜   rs   rA   re   Útypingr   r   r   Ú r   Z
_deprecater   Z_utilr	   r6   r·   r{   r   r   r   r"   r#   r-   rŠ   r“   r¯   rª   r¼   r½   rÂ   rÈ   rÏ   r   r   r   r   Ú<module>   sB   û
 v 
#J2