a
    ¥Q•hó[  ã                   @   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   sP   zt j | ¡}W n ty,   t | ¡}Y n0 |s<d| › �}|d7 }t|ƒ‚d S )Nzdecoder error z when reading image file)r   ÚcoreÚgetcodecstatusÚAttributeErrorÚERRORSÚgetÚOSError)ÚerrorÚmsg© r   úF/var/www/sistema_ama/venv/lib/python3.9/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 | _zzz|  ¡  W n: tttttjfyª } zt|ƒ|‚W Y d }~n
d }~0 0 | jrÎ| jd dksÎ| jd dkrÚd}t|ƒ‚W n& t�y   | jrü| j ¡  ‚ Y n0 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 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oDt| j ƒdk}|oTttdƒ }d}z| j	}d}W n t
y‚   | jj}Y n0 z| j}d}W n t
y¬   | jj}Y n0 |�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 �sB0    Y  |	| jd |
d   | j ¡ k�rrt‚tj | j| j||	|
¡| _d}| j�r d| j_W n  t
ttf�yÂ   d| _Y n0 |  ¡  d
}| j�sj| j jtd� z
| j}W n t
�y   d}Y n0 dd„ t  !| j dd„ ¡D ƒ| _ | j D �]8\}}}	}
||	ƒ t "| j||
| j#¡}�z | $| j|¡ |j%�rŽ| &| j¡ | 'd¡d }nÂ|}z|| j(ƒ}W nP t)t*j+f�yð } z0t,�rÎW Y d}~�qPnd}t|ƒ|‚W Y d}~n
d}~0 0 |�st,�r�qPndt|ƒ› d�}t|ƒ‚|| }| '|¡\}}|dk �r@�qP||d… }�q’W | -¡  n
| -¡  0 �q.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   Z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-   Ú	_MAPMODESÚmmapr"   ÚfilenoZACCESS_READr.   r	   Ú
map_bufferÚimÚpaletteÚdirtyÚImportErrorÚload_prepareÚsortr   Z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   ZpixelZuse_mmapr   rO   rQ   Údecoder_nameÚextentsÚoffsetÚargsrS   r#   Zerr_codeÚprefixÚdecoderÚbÚsÚeÚnr   r   r   rI   ›   sÄ    
ÿ
þýü8"ÿ


ÿþÿ ÿÿ

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)rV   r-   r.   r   r	   ÚnewrI   r9   r   r   r   rZ   $  s    "
zImageFile.load_preparec                 C   s   d S r5   r   r9   r   r   r   re   ,  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   rL   rs   Zn_framesr*   Útell)r1   Úframer   r   r   r   Ú_seek_check8  s    ÿûûú	zImageFile._seek_check)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r:   r;   r=   rI   rZ   re   rv   Ú__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   s   d}t |ƒ‚d S )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   rI   r4   Ú__dict__)r1   Ú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 _loadNr}   r   r   r   r   r€   `  s    zStubImageFile._loadN)rw   rx   ry   rz   r&   rI   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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 d  ƒ n1 �s
0    Y  W n t�y*   Y n²0 t|dƒ�p@t|dƒ}|�sXt|jƒdk�r`d| _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…   rl   ri   ÚminrK   rb   rƒ   r   ÚioÚBytesIOr   r"   r   rL   r   rZ   r^   r-   r   r_   rV   )r1   r…   Úskiprp   ro   r#   rV   ÚflagÚdÚoÚar   r   r   Úfeed{  sR    	


.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   rj   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¡�8}zt 	|¡| _W | j 
¡  n| j 
¡  0 W d  ƒ n1 s�0    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.
        rC   Nzimage was incompletezcannot parse this image)rl   r�   r…   r‡   r   rƒ   r‰   rŠ   r   r"   rI   )r1   r   r#   r   r   r   r0   Î  s    

6zParser.close)rw   rx   ry   rz   Úincrementalrƒ   r…   rl   ri   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yš } zt
| |||d|ƒ W Y d}~n
d}~0 0 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•   r[   r   Úmaxr    r.   rT   r—   Ú_encode_tiler   r‰   ÚUnsupportedOperation)rV   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 |
 ¡  q|
 ¡  0 qd S )Nr   r   zencoder error z when writing image file)rQ   r   Ú_getencoderr-   r•   r_   rV   Ú	pushes_fdra   Úencode_to_pyfdÚencodeÚwriteÚencode_to_filer   rd   )rV   r#   r   r›   rœ   r�   ro   rm   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 r5   )rK   )rE   r�   r   r   r   Ú	<genexpr>E  rC   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Ú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   rh   R  s    zPyCodecState.extentsN)rw   rx   ry   r   rh   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   )rV   r®   r<   Úfdr-   Úinit)r1   r-   rj   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)rj   r’   r   r   r   rµ   ^  s    zPyCodec.initc                 C   s   dS )zT
        Override to perform codec specific cleanup

        :returns: None
        Nr   r9   r   r   r   rd   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´   )r1   r´   r   r   r   ra   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)rV   r.   r<   r¯   r°   r±   r²   Ú
ValueError)r1   rV   rh   Úx0Úy0Úx1Úy1r   r   r   r   r_   x  s&    ÿþzPyCodec.setimage)N)rw   rx   ry   r   rµ   rd   ra   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 r5   )Ú	_pulls_fdr9   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`.
        Nr}   )r1   Úbufferr   r   r   rb   ¬  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_   rV   r<   rh   rb   r¶   )r1   r…   Úrawmoder�   rn   r   r   r   r   Ú
set_as_raw·  s    

zPyDecoder.set_as_raw)N)	rw   rx   ry   rz   r¼   Úpropertyr`   rb   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 r5   )Ú
_pushes_fdr9   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`.
        Nr}   )r1   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£   )r1   Ú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£   )r1   rœ   r›   r¦   ÚstatusÚbufr   r   r   r¤   ÷  s    	zPyEncoder.encode_to_fileN)
rw   rx   ry   rz   rÂ   rÀ   r    r¢   r¡   r¤   r   r   r   r   rÁ   Ï  s   
rÁ   )r   )N)r‰   r\   r+   rM   Ú r   Ú_utilr   r    r¨   rc   r   r   r   r   r|   r„   rž   r™   r­   r®   r³   r»   rÁ   r   r   r   r   Ú<module>   s8   û
 t 

#H1