o
    e_                     @  s6  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 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d/dd Zd0d1d#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 )2    )annotationsN)Any
NamedTuple   )Image)	deprecate)is_pathi   i   Fzimage buffer overrun errorzdecoding errorzunknown errorzbad configurationzout of memory error)ic                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   @/var/www/html/venv/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   zIt 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encoder_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   r1   l   sJ   

"	
zImageFile.__init__c                 C  s,   | j r| j S | jd urtj| j S d S N)r3   formatr   MIMEr   upperrH   r   r   r   get_format_mimetype   s
   
zImageFile.get_format_mimetypec                   s   g | _ t | d S rL   )r4   r0   __setstate__)rH   staterJ   r   r   rR      s   zImageFile.__setstate__c                 C  s   | j r| j  d| _dS )zCheck file integrityN)r<   r:   rG   rP   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rt|
dkr|
d | jkr|
d tjv rzSddl}t| j}|j| d|jd	| _W d   n1 sw   Y  |	| jd |
d   | j krd
}t|tj| j| j||	|
| _d}| jrd| 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 |st.r|n%dt| d}t||| }|)|\}}|dk rn||d }qMW |/  q|/  w g | _ || _0| 1  | j2r| j3r| j4  d| _| jst.s|dk rt5|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   rW   r   )r4   r   r   r   <lambda>   s    z ImageFile.load.<locals>.<lambda>Tzimage file is truncatedzimage file is truncated (z bytes not processed)r   )6r4   r   r   loadmapr;   lenhasattrsys	load_readr   r:   read	load_seekseek
isinstancer$   rD   	_MAPMODESmmapr9   filenoACCESS_READrE   r   
map_bufferimpalettedirtyImportErrorload_preparesortr"   tile_prefix	itertoolsgroupby_getdecoderr6   setimagepulls_fdsetfddecoder8   r>   rB   r   LOAD_TRUNCATED_IMAGEScleanupr5   load_endr<   !_close_exclusive_fp_after_loadingrG   r   )rH   r   pixeluse_mmapr5   rh   rj   decoder_namer&   r(   r)   rm   r:   err_codeprefixr   bsenr   r   r   rb      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)rq   rD   rE   r   r   newrb   rP   r   r   r   ru   :  s
   "
zImageFile.load_preparec                 C  s   d S rL   r   rP   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)r2   re   r   n_framesrA   tell)rH   framer   r   r   r   _seek_checkN  s   

zImageFile._seek_check)NN)r*   r+   r,   __doc__r1   rQ   rR   rT   rb   ru   r   r   __classcell__r   r   rJ   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rH   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)_loadrM   r   rb   rK   __dict__)rH   loaderr   imager   r   r   rb   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=   rb   r   r   r   r   r   r   ^  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)datarP   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 sw   Y  W n
 ty   Y dS w t|dpt|d}|st|jdkrd| _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   ri   rg   )finishedr   r   r(   minrd   r~   r   r   ioBytesIOr   r9   r   re   r4   ru   rz   rD   r6   r{   rq   )rH   r   skipr   r   r:   rq   flagdoar   r   r   feed  sX   	


zParser.feedc                 C  s   | S rL   r   rP   r   r   r   	__enter__  s   zParser.__enter__c                 G  s   |    d S rL   )rG   rH   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   r9   rb   )rH   r   r:   r   r   r   rG     s$   


zParser.close)r*   r+   r,   r   incrementalr   r   r   r(   r   r   r   r   r   rG   r   r   r   r   r   |  s    L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 )zHelper 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   rY   r      Nflush)rb   re   r   rv   r"   maxr7   rE   rn   r   _encode_tiler   r   UnsupportedOperation)rq   r:   r4   bufsizefhexcr   r   r   _save  s    	

r   r4   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   )rj   r   _getencoderrD   r   r{   rq   	pushes_fdr}   encode_to_pyfdencodewriteencode_to_filer   r   )rq   r:   r4   r   r   r   r%   r&   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 rL   )rd   )r]   r   r   r   r   	<genexpr>Y  s    z_safe_read.<locals>.<genexpr>)	SAFEBLOCKrh   rd   r   r   appendsumjoin)r:   rE   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yoffrP   r   r   r   r1   `  s   
zPyCodecState.__init__c                 C  s    | j | j| j | j | j| j fS rL   )r   r   r   r   rP   r   r   r   r&   f  s    zPyCodecState.extentsN)r*   r+   r,   r1   r&   r   r   r   r   r   _  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 rL   )rq   r   rS   fdrD   init)rH   rD   r)   r   r   r   r1   k  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   r     
zPyCodec.initc                 C  s   dS )zT
        Override to perform codec specific cleanup

        :returns: None
        Nr   rP   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   )rH   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)rq   rE   rS   r   r   r   r   
ValueError)rH   rq   r&   x0y0x1y1r   r   r   r   r{     s$   zPyCodec.setimagerL   )r*   r+   r,   r1   r   r   r}   r{   r   r   r   r   r   j  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 rL   )	_pulls_fdrP   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   )rH   bufferr   r   r   r   r~        	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
        rV   r   znot enough image datar   zcannot decode image dataN)	rD   r   rz   r{   rq   rS   r&   r~   r   )rH   r   rawmoder   r   r   r   r   r   
set_as_raw  s   

zPyDecoder.set_as_rawrL   )	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   rL   )
_pushes_fdrP   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   )rH   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   )rH   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   )rH   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   rL   )r4   r   )!
__future__r   r   rx   rB   rf   typingr   r    r   
_deprecater   _utilr   r7   r   r   r   r   r   r"   r#   r.   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sH   
 v 

#H2