o
    ÒèFhe_  ã                   @  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d1d2d!d"„Zd3d4d%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 )5é    )Ú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  sh   zt j | ¡}W n ty   t | ¡}Y nw |s$|rdnd› d| › �}|d|r*dnd› d�7 }t|ƒS )NÚencoderÚdecoderz error z when ÚwritingÚreadingz image file)r   ÚcoreÚgetcodecstatusÚAttributeErrorÚERRORSÚgetÚOSError)Úerrorr   Úmsg© r   úF/var/www/html/loop/nvenv/lib/python3.10/site-packages/PIL/ImageFile.pyÚ_get_oserrorC   s   ÿr   c                 C  s   t dddd� t| dd�‚)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ÚstrÚ
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	|ƒr)t
|dƒ| _|| _d| _n	|| _|| _d | _z8z|  ¡  W n tttttjfyP } zt|ƒ|‚d }~ww | jrb| jd dksb| jd dkrhd}t|ƒ‚W d S  tyz   | jry| 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<   Úvr   ©Ú	__class__r   r   r2   l   sJ   
û
€ù"	þ
üzImageFile.__init__c                 C  s,   | j r| j S | jd urtj | j ¡ ¡S d S ©N)r4   Úformatr   ÚMIMEr   Úupper©rI   r   r   r   Úget_format_mimetypeœ   s
   
ÿzImageFile.get_format_mimetypec                   s   g | _ tƒ  |¡ d S rM   )r5   r1   Ú__setstate__)rI   ÚstaterK   r   r   rS   ¢   s   zImageFile.__setstate__c                 C  s   | j r| j ¡  d| _dS )zCheck file integrityN)r=   r;   rH   rQ   r   r   r   Úverify¦   s   

zImageFile.verifyc                 C  s¾  | j du rd}t|ƒ‚tj | ¡}| j s|S d| _| jo"t| j ƒdk}|o*ttdƒ }d}z| j	}d}W n t
yA   | jj}Y nw z| j}d}W n t
yV   | jj}Y nw |rç| j d \}}}	}
t|
tƒrl|
ddf}
|dkrçt|
ƒdkrç|
d | jkrç|
d tjv rçzSddl}t| jƒ�}|j| ¡ d|jd	�| _W d  ƒ n1 s¥w   Y  |	| jd |
d   | j ¡ krÀd
}t|ƒ‚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%¡}zw| &| j|¡ |j'�rJ| (| j¡ | )d¡d }nW|}	 z|| j*ƒ}W n  t+t,j-f�yt } zt.�riW Y d}~n8d}t|ƒ|‚d}~ww |�sˆt.�r|n%dt|ƒ› d�}t|ƒ‚|| }| )|¡\}}|dk �r™n||d… }�qMW | /¡  �q| /¡  w 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Ú_Útilesr   r   r   Ú
<listcomp>û   s    
ÿÿz"ImageFile.load.<locals>.<listcomp>c                 S  s   | d | d | d fS )Nr   r   rX   r   )r5   r   r   r   Ú<lambda>þ   s    z ImageFile.load.<locals>.<lambda>Tzimage file is truncatedzimage file is truncated (z bytes not processed)r    )6r5   r   r   ÚloadÚmapr<   ÚlenÚhasattrÚsysÚ	load_readr   r;   ÚreadÚ	load_seekÚseekÚ
isinstancer%   rE   Ú	_MAPMODESÚmmapr:   ÚfilenoÚACCESS_READrF   r   Ú
map_bufferÚimÚpaletteÚdirtyÚImportErrorÚload_prepareÚsortr#   Útile_prefixÚ	itertoolsÚgroupbyÚ_getdecoderr7   ÚsetimageÚpulls_fdÚsetfdÚdecoder9   r?   rC   r   ÚLOAD_TRUNCATED_IMAGESÚcleanupr6   Úload_endr=   Ú!_close_exclusive_fp_after_loadingrH   r   )rI   r   ÚpixelÚuse_mmapr6   ri   rk   Údecoder_namer'   r)   r*   rn   r;   Úerr_codeÚprefixr   ÚbÚsÚeÚnr   r   r   rc   ¯   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)rr   rE   rF   r   r   Únewrc   rQ   r   r   r   rv   :  s
   "
ÿzImageFile.load_preparec                 C  s   d S rM   r   rQ   r   r   r   r‚   B  s   zImageFile.load_endc                 C  sF   || j k st| dƒr| jd u s|| j| j  krd}t|ƒ‚|  ¡ |kS )NÚ	_n_framesz attempt to seek outside sequence)r3   rf   r�   Ún_framesrB   Útell)rI   Úframer   r   r   r   Ú_seek_checkN  s   
ü
zImageFile._seek_check)NN)r+   r,   r-   Ú__doc__r2   rR   rS   rU   rc   rv   r‚   r“   Ú__classcell__r   r   rK   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  ó   d}t |ƒ‚)Nz+StubImageFile subclass must implement _open©ÚNotImplementedError©rI   r   r   r   r   r>   f  s   zStubImageFile._openc                 C  sT   |   ¡ }|d u rd| j› d�}t|ƒ‚| | ¡}|d usJ ‚|j| _|j| _| ¡ S )Nzcannot find loader for this z file)Ú_loadrN   r   rc   rL   Ú__dict__)rI   Úloaderr   Úimager   r   r   rc   j  s   
zStubImageFile.loadc                 C  r—   )z (Hook) Find actual image loader.z+StubImageFile subclass must implement _loadr˜   rš   r   r   r   r›   v  s   zStubImageFile._loadN)r+   r,   r-   r”   r>   rc   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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)ÚdatarQ   r   r   r   Úreset‰  s   zParser.resetc                 C  sØ  | j rdS | jdu r|| _n| j| | _| jri| 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|dd�‚dS | j|d… | _dS | jrnd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   Fr    rj   rh   )Úfinishedr    r   r)   Úminre   r   rž   r   ÚioÚBytesIOr   r:   r   rf   r5   rv   r{   rE   r7   r|   rr   )rI   r    ÚskiprŒ   r‹   r;   rr   ÚflagÚdÚoÚar   r   r   Úfeed‘  sX   	

ÿ€ÿ
zParser.feedc                 C  s   | S rM   r   rQ   r   r   r   Ú	__enter__Ý  s   zParser.__enter__c                 G  s   |   ¡  d S rM   )rH   ©rI   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rOt | j¡�}zt 	|¡| _W | j 
¡  n| j 
¡  w W d  ƒ | jS 1 sJw   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.
        r\   Nzimage was incompletezcannot parse this image)r   r«   r    r¢   r   rž   r¤   r¥   r   r:   rc   )rI   r   r;   r   r   r   rH   ã  s$   


üûzParser.close)r+   r,   r-   r”   Úincrementalrž   r.   r    r   r)   r¢   r¡   r«   r¬   r®   rH   r   r   r   r   rŸ   |  s   
 LrŸ   ÚreturnÚNonec              
   C  s¶   |   ¡  t| dƒsd| _|jtd� tt|| jd d ƒ}z| ¡ }| 	¡  t
| ||||ƒ W n ttjfyM } zt
| |||d|ƒ W Y d}~nd}~ww t|dƒrY| 	¡  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   rZ   r   é   NÚflush)rc   rf   r²   rw   r#   Úmaxr8   rF   ro   r´   Ú_encode_tiler   r¤   ÚUnsupportedOperation)rr   r;   r5   ÚbufsizeÚfhÚexcr   r   r   Ú_save  s    	
€ÿ
ÿr»   r5   úlist[_Tile]c              	   C  sÐ   |D ]c\}}}}	|dkr|  |¡ t | j||	| j¡}
zE|
 | j|¡ |
jr2|
 |¡ |
 	¡ d }n|rJ	 |
 
|¡dd … \}}| |¡ |rHnq5n|
 ||¡}|dk r[t|dd�|‚W |
 ¡  q|
 ¡  w d S )Nr   r   Tr    )rk   r   Ú_getencoderrE   r²   r|   rr   Ú	pushes_fdr~   Úencode_to_pyfdÚencodeÚwriteÚencode_to_filer   r�   )rr   r;   r5   r¸   r¹   rº   Ú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 rd}t|ƒ‚|S g }|}|dkr?|  t|t ƒ¡}|s0n| |¡ |t|ƒ8 }|dks%tdd„ |D ƒƒ|k rPd}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   r\   zTruncated File Readc                 s  s   � | ]}t |ƒV  qd S rM   )re   )r^   r¨   r   r   r   Ú	<genexpr>Y  s   € z_safe_read.<locals>.<genexpr>)Ú	SAFEBLOCKri   re   r   r£   ÚappendÚsumÚjoin)r;   rF   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ÚyoffrQ   r   r   r   r2   `  s   
zPyCodecState.__init__c                 C  s    | j | j| j | j | j| j fS rM   )rÐ   rÑ   rÎ   rÏ   rQ   r   r   r   r'   f  s    zPyCodecState.extentsN)r+   r,   r-   r2   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 rM   )rr   rÍ   rT   rÓ   rE   Úinit)rI   rE   r*   r   r   r   r2   m  s
   zPyCodec.__init__c                 C  ó
   || _ 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  ó   
zPyCodec.initc                 C  s   dS )zT
        Override to perform codec specific cleanup

        :returns: None
        Nr   rQ   r   r   r   r�   }  s   zPyCodec.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Ó   )rI   rÓ   r   r   r   r~   …  rÖ   zPyCodec.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rKd}t|ƒ‚| jj| jj | j jd ksg| jj| jj | j jd krmd}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)rr   rF   rT   rÎ   rÏ   rÐ   rÑ   Ú
ValueError)rI   rr   r'   Úx0Úy0Úx1Úy1r   r   r   r   r|   Ž  s$   ýzPyCodec.setimagerM   )	r+   r,   r-   r.   r2   rÔ   r�   r~   r|   r   r   r   r   rÒ   j  s   
 		rÒ   c                   @  s4   e Zd ZdZdZedd„ ƒZdd„ Z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  ó   | j S rM   )Ú	_pulls_fdrQ   r   r   r   r}   ¾  ó   zPyDecoder.pulls_fdc                 C  ó   d}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 -1 for the bytes consumed.
            Err codes are from :data:`.ImageFile.ERRORS`.
        zunavailable in base decoderr˜   )rI   Úbufferr   r   r   r   r   Â  ó   	zPyDecoder.decodeNr    Úbytesr°   r±   c                 C  sl   |s| j }t | j d|¡}| | j| j ¡ ¡ | |¡}|d dkr(d}t|ƒ‚|d dkr4d}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
        rW   r   znot enough image datar   zcannot decode image dataN)	rE   r   r{   r|   rr   rT   r'   r   r×   )rI   r    Úrawmoder¨   rŠ   r   r   r   r   Ú
set_as_rawÎ  s   

þzPyDecoder.set_as_rawrM   )r    rã   r°   r±   )	r+   r,   r-   r”   rÞ   Úpropertyr}   r   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  rÝ   rM   )Ú
_pushes_fdrQ   r   r   r   r¾   ð  rß   zPyEncoder.pushes_fdc                 C  rà   )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 encoderr˜   )rI   r¸   r   r   r   r   rÀ   ô  râ   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Á   )rI   Úbytes_consumedrÄ   r    r   r   r   r¿      s   zPyEncoder.encode_to_pyfdc                 C  sB   d}|dkr|   |¡\}}}|dkr| ||d… ¡ |dks|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Á   )rI   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   )r°   r±   rM   )r5   r¼   )"Ú
__future__r   r¤   ry   rC   rg   Útypingr   r   r   Ú r   Ú
_deprecater   Ú_utilr	   r8   rÆ   r€   r   r   r   r#   r$   r/   r–   rŸ   r»   r¶   rÌ   rÍ   rÒ   rÜ   rç   r   r   r   r   Ú<module>   sH   û
 v 
#J2