o
    \"<fR                     @   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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 ZG dd dZG d d! d!ZdS )#    N   )Image)isPathi   i   Fzimage buffer overrun errorzdecoding errorzunknown errorzbad configurationzout of memory error)iic                 C   sH   zt j| }W n ty   t| }Y nw |sd|  }t|d )Nzdecoder error z when reading image file)r   coregetcodecstatusAttributeErrorERRORSgetOSError)errormessage r   D/var/www/html/kck/venv/lib/python3.10/site-packages/PIL/ImageFile.pyraise_oserror<   s   
r   c                 C   s   t dt t| S )Nzdraise_ioerror is deprecated and will be removed in Pillow 9 (2022-01-02). Use raise_oserror instead.)warningswarnDeprecationWarningr   )r   r   r   r   raise_ioerrorF   s
   r   c                 C   s   | d S )N   r   )tr   r   r   	_tilesortO      r   c                       sR   e Zd ZdZd fdd	Z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 | _z/z|   W n tttttjfyP } zt||d }~ww | jr[| jd dkr_tdW d S  tyq   | jrp| 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(   v	__class__r   r   r   \   sH   

	
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r5   r   r   r   get_format_mimetype   s
   
zImageFile.get_format_mimetypec                 C   s   | j r| j  d| _dS )zCheck file integrityN)r)   r'   r4   r=   r   r   r   verify   s   

zImageFile.verifyc                 C   sL  | j du r	tdtj| }| j s|S d| _| jo t| j dk}|o(ttd }d}z| j	}d}W n t
y?   | jj}Y nw z| j}d}W n t
yT   | jj}Y nw |r| j d \}}}}	|dkrt|	dkr|	d | jkr|	d tjv rz=ddl}
t| j}|
j| d|
jd	| _W d   n1 sw   Y  tj| j| j|||	| _d}| jrd| j_W n t
ttfy   d| _Y nw |   d
}| jsv| j jtd z| j}W n t
y   d}Y nw | j D ]\}}}}	t | j||	| j!}zv|| |"| j| |j#r|$| j |%d\}}nS|}	 z|| j&}W n t't(j)fy@ } zt*r7W Y d}~n4td|d}~ww |sRt*rHn#tdt| d|| }|%|\}}|dk rcn||d }qW |+  q|+  w g | _ || _,| -  | j.r| j/r| j0  d| _| jst*s|dk rt1| tj| S )z"Load image data based on tile listNzcannot load this imager   pypy_version_infor   Fraw   )accessr   key    Tzimage file is truncatedzimage file is truncated (z bytes not processed))2r!   r   r   loadmapr(   lenhasattrsys	load_readr
   r'   read	load_seekseekr1   	_MAPMODESmmapr&   filenoACCESS_READr   
map_bufferr2   impalettedirtyImportErrorload_preparesortr   tile_prefix_getdecoderr#   setimagepulls_fdsetfddecoder%   r+   r/   r   LOAD_TRUNCATED_IMAGEScleanupr"   load_endr)   !_close_exclusive_fp_after_loadingr4   r   )r5   pixeluse_mmapr"   rM   rO   decoder_nameextentsoffsetargsrQ   r'   err_codeprefixdecoderstatusbsenr   r   r   rG      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)rU   r1   r2   r   r   newrG   r=   r   r   r   rY     s
   "
zImageFile.load_preparec                 C   s   d S r9   r   r=   r   r   r   rc     s   zImageFile.load_endc                 C   sB   || j k st| dr| jd u s|| j| j  krtd|  |kS )N	_n_framesz attempt to seek outside sequence)r   rJ   ru   n_framesr.   tell)r5   framer   r   r   _seek_check*  s   

zImageFile._seek_check)NN)__name__
__module____qualname____doc__r   r>   r?   rG   rY   rc   ry   __classcell__r   r   r7   r   r   Y   s    /	|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      t d)Nz+StubImageFile subclass must implement _openNotImplementedErrorr=   r   r   r   r*   A  s   zStubImageFile._openc                 C   sL   |   }|d u rtd| j d|| }|d usJ |j| _|j| _d S )Nzcannot find loader for this z file)_loadr   r:   rG   r8   __dict__)r5   loaderimager   r   r   rG   D  s   
zStubImageFile.loadc                 C   r   )z (Hook) Find actual image loader.z+StubImageFile subclass must implement _loadr   r=   r   r   r   r   N  r   zStubImageFile._loadN)rz   r{   r|   r}   r*   rG   r   r   r   r   r   r   9  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)datar=   r   r   r   reset`  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 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   rN   rL   )finishedr   rm   ri   minrI   r`   r   r   ioBytesIOr   r&   r   rJ   r!   rY   r\   r1   r#   r]   rU   )r5   r   skiprr   rq   r'   rU   flagdoar   r   r   feedh  sX   	



zParser.feedc                 C   s   | S r9   r   r=   r   r   r   	__enter__  s   zParser.__enter__c                 G   s   |    d S r9   )r4   r5   rj   r   r   r   __exit__  s   zParser.__exit__c              	   C   s   | j r| d d | _| _ | jstd| jstd| jrKt| j}zt	|| _W | j
  n| j
  w W d   | jS 1 sFw   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.
        rF   Nzimage was incompletezcannot parse this image)rm   r   r   r   r   r   r   r   r   r&   rG   )r5   r'   r   r   r   r4     s    


zParser.close)rz   r{   r|   r}   incrementalr   r   rm   ri   r   r   r   r   r   r4   r   r   r   r   r   S  s    Pr   c              
   C   s  |    t| dsd| _|jtd tt|| jd d }z|tj	kp(|tj	j
k}W n ttfy7   d}Y nw |r@|  dS z
| }|  W nj ttjfy } z[|D ]R\}}}	}
t| j||
| j}|	dkrp||	 || j| |jr|| | \}}n	 ||\}}}|| |rnq|dk rtd	| d
||  qWW Y d}~nMd}~ww |D ]E\}}}	}
t| j||
| j}|	dkr||	 || j| |jr|| | \}}n|||}|dk rtd	| d
|  qt|dr	|  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   rD   r      FNTzencoder error z when writing image fileflush)rG   rJ   r   rZ   r   maxr$   r2   rK   stdoutbufferr   r
   r   rR   r   UnsupportedOperationr   _getencoderr1   rO   r]   rU   	pushes_fdr_   encode_to_pyfdencodewriterb   encode_to_file)rU   r'   r!   bufsizer   fhexcrq   ro   r   r   lrp   r   r   r   r   _save  sh   	







r   c                 C   s   |dkrdS |t kr| |}t||k rtd|S g }|dkr;| t|t }|s,n|| |t|8 }|dks!tdd |D |k rJtd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   rF   zTruncated File Readc                 s   s    | ]}t |V  qd S r9   )rI   ).0r   r   r   r   	<genexpr>;  s    z_safe_read.<locals>.<genexpr>)	SAFEBLOCKrM   rI   r   r   appendsumjoin)r'   r2   r   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yoffr=   r   r   r   r   A  s   
zPyCodecState.__init__c                 C   s    | j | j| j | j | j| j fS r9   )r   r   r   r   r=   r   r   r   rh   G  s    zPyCodecState.extentsN)rz   r{   r|   r   rh   r   r   r   r   r   @  s    r   c                   @   s\   e Zd ZdZdZdd Zdd Zedd Zd	d
 Z	dd Z
dd Zd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 Decoder in Python<file-decoders-py>`
    Fc                 G   s(   d | _ t | _d | _|| _| | d S r9   )rU   r   statefdr1   init)r5   r1   rj   r   r   r   r   U  s
   zPyDecoder.__init__c                 C   
   || _ dS )z
        Override to perform decoder specific initialization

        :param args: Array of args items from the tile entry
        :returns: None
        N)rj   r   r   r   r   r   \     
zPyDecoder.initc                 C   s   | j S r9   )	_pulls_fdr=   r   r   r   r^   e  s   zPyDecoder.pulls_fdc                 C   s   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 <0 for the bytes consumed.
            Err codes are from :data:`.ImageFile.ERRORS`.
        r   )r5   r   r   r   r   r`   i  s   	zPyDecoder.decodec                 C   s   dS )zV
        Override to perform decoder specific cleanup

        :returns: None
        Nr   r=   r   r   r   rb   t  s   zPyDecoder.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   )r5   r   r   r   r   r_   |  r   zPyDecoder.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rItd| jj| jj | j jd kse| jj| jj | j jd kritddS )z
        Called from ImageFile to set the core output image for the decoder

        :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)rU   r2   r   r   r   r   r   
ValueError)r5   rU   rh   x0y0x1y1r   r   r   r]     s    zPyDecoder.setimagec                 C   sd   |s| j }t| j d|}|| j| j  ||}|d dkr&td|d dkr0td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
        rA   r   znot enough image datar   zcannot decode image dataN)	r1   r   r\   r]   rU   r   rh   r`   r   )r5   r   rawmoder   rp   r   r   r   
set_as_raw  s   

zPyDecoder.set_as_rawr9   )rz   r{   r|   r}   r   r   r   propertyr^   r`   rb   r_   r]   r   r   r   r   r   r   K  s    	

	#r   )r   )r   r/   rK   r    r   _utilr   r$   r   ra   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s:   
	
 a 
B 