o
    svXjó[  ã                   @   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   sL   zt j | ¡}W n ty   t | ¡}Y nw |sd| › �}|d7 }t|ƒ‚)Nzdecoder error z when reading image file)r   ÚcoreÚgetcodecstatusÚAttributeErrorÚERRORSÚgetÚOSError)ÚerrorÚmsg© r   úJ/var/www/html/myproject/venv/lib/python3.10/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	|ƒ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   r   X   sJ   
û
€ù"	þ
ü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                    s   g | _ tƒ  |¡ d S r5   )r   r   Ú__setstate__)r1   Ústater3   r   r   r;   Ž   s   zImageFile.__setstate__c                 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   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 \}}}	}
|dkrÙt|
ƒdkrÙ|
d | jkrÙ|
d tjv rÙzOddl}t| jƒ�}|j| ¡ d|jd	�| _W d  ƒ n1 s›w   Y  |	| jd |
d   | j ¡ kr²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%�r;| &| j¡ | 'd¡d }nW|}	 z|| j(ƒ}W n  t)t*j+f�ye } zt,�rZW Y d}~n8d}t|ƒ|‚d}~ww |�syt,�rmn%dt|ƒ› d�}t|ƒ‚|| }| '|¡\}}|dk �rŠn||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.   r	   Ú
map_bufferÚ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   r   ÚpixelÚuse_mmapr   rQ   rS   Údecoder_nameÚextentsÚoffsetÚargsrU   r#   Úerr_codeÚprefixÚdecoderÚbÚsÚeÚnr   r   r   rK   ›   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)rY   r-   r.   r   r	   ÚnewrK   r9   r   r   r   r]   $  s
   "
ÿzImageFile.load_preparec                 C   s   d S r5   r   r9   r   r   r   ri   ,  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)r   rN   rz   Ún_framesr*   Útell)r1   Úframer   r   r   r   Ú_seek_check8  s   
ü
zImageFile._seek_check)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r:   r;   r=   rK   r]   ri   r~   Ú__classcell__r   r   r3   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   ó   d}t |ƒ‚)Nz+StubImageFile subclass must implement _open©ÚNotImplementedError©r1   r   r   r   r   r&   P  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)Ú_loadr6   r   rK   r4   Ú__dict__)r1   Úloaderr   Úimager   r   r   rK   T  s   
zStubImageFile.loadc                 C   r…   )z (Hook) Find actual image loader.z+StubImageFile subclass must implement _loadr†   rˆ   r   r   r   r‰   `  s   zStubImageFile._loadN)r   r€   r�   r‚   r&   rK   r‰   r   r   r   r   r„   H  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   Úresets  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   rR   rP   )ÚfinishedrŽ   rs   ro   ÚminrM   rf   rŒ   r   ÚioÚBytesIOr   r"   r   rN   r   r]   rb   r-   r   rc   rY   )r1   rŽ   Úskiprw   rv   r#   rY   ÚflagÚdÚoÚar   r   r   Úfeed{  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   rp   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.
        rD   Nzimage was incompletezcannot parse this image)rs   r™   rŽ   r�   r   rŒ   r’   r“   r   r"   rK   )r1   r   r#   r   r   r   r0   Î  s$   


üûzParser.close)r   r€   r�   r‚   ÚincrementalrŒ   rŽ   rs   ro   r�   r�   r™   rš   rœ   r0   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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   rB   r   é   NÚflush)rK   rN   rž   r^   r   Úmaxr    r.   rV   r    Ú_encode_tiler   r’   ÚUnsupportedOperation)rY   r#   r   ÚbufsizeÚfhÚexcr   r   r   Ú_saveð  s    	
€ÿ
ÿr§   c              	   C   sØ   |D ]g\}}}}	|dkr|  |¡ t | j||	| j¡}
zI|
 | j|¡ |
jr2|
 |¡ |
 	¡ d }n|rJ	 |
 
|¡dd … \}}| |¡ |rHnq5n|
 ||¡}|dk r_d|› d�}t|ƒ|‚W |
 ¡  q|
 ¡  w d S )Nr   r   Tzencoder error z when writing image file)rS   r   Ú_getencoderr-   rž   rc   rY   Ú	pushes_fdre   Úencode_to_pyfdÚencodeÚwriteÚencode_to_filer   rh   )rY   r#   r   r¤   r¥   r¦   rv   rt   r—   r˜   ÚencoderÚerrcoderŽ   r   r   r   r   r¢     s0   


ü
þè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   rD   zTruncated File Readc                 s   s   � | ]}t |ƒV  qd S r5   )rM   )rF   r–   r   r   r   Ú	<genexpr>E  s   € z_safe_read.<locals>.<genexpr>)Ú	SAFEBLOCKrQ   rM   r   r‘   ÚappendÚsumÚjoin)r#   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   L  s   
zPyCodecState.__init__c                 C   s    | j | j| j | j | j| j fS r5   )r»   r¼   r¹   rº   r9   r   r   r   rn   R  s    zPyCodecState.extentsN)r   r€   r�   r   rn   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 r5   )rY   r¸   r<   Úfdr-   Úinit)r1   r-   rp   r   r   r   r   W  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)rp   r›   r   r   r   r¿   ^  ó   
zPyCodec.initc                 C   s   dS )zT
        Override to perform codec specific cleanup

        :returns: None
        Nr   r9   r   r   r   rh   g  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¾   )r1   r¾   r   r   r   re   o  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)rY   r.   r<   r¹   rº   r»   r¼   Ú
ValueError)r1   rY   rn   Úx0Úy0Úx1Úy1r   r   r   r   rc   x  s$   ýzPyCodec.setimager5   )r   r€   r�   r   r¿   rh   re   rc   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   ó   | j S r5   )Ú	_pulls_fdr9   r   r   r   rd   ¨  ó   zPyDecoder.pulls_fdc                 C   ó   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`.
        r†   )r1   Úbufferr   r   r   rf   ¬  ó   	zPyDecoder.decodeNc                 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
        r?   r   znot enough image datar   zcannot decode image dataN)	r-   r   rb   rc   rY   r<   rn   rf   rÂ   )r1   rŽ   Úrawmoder–   ru   r   r   r   r   Ú
set_as_raw·  s   

þzPyDecoder.set_as_rawr5   )	r   r€   r�   r‚   rÉ   Úpropertyrd   rf   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È   r5   )Ú
_pushes_fdr9   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`.
        r†   )r1   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¬   )r1   Ú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¬   )r1   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   r5   )r’   r`   r+   rO   Ú r   Ú_utilr   r    r±   rg   r   r   r   r   r„   r�   r§   r¢   r·   r¸   r½   rÇ   rÑ   r   r   r   r   Ú<module>   s>   û
 t 

#H1