a
    xdR                     @   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   sL   zt j| }W n ty,   t| }Y n0 |s<d|  }t|d d S )Nzdecoder error z when reading image file)r   coreZgetcodecstatusAttributeErrorERRORSgetOSError)errormessage r   E/var/www/html/Ranjet/env/lib/python3.9/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   s    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	|rPt
|d| _|| _d| _n|| _|| _d | _zhz|   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rtd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&   v	__class__r   r   r   \   s>    
 
zImageFile.__init__c                 C   s,   | j r| j S | jd ur(tj| j S d S N)r   formatr   ZMIMEr   upperr3   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%   r2   r:   r   r   r   verify   s    
zImageFile.verifyc                 C   sz  | j du rtdtj| }| j s(|S d| _| jo@t| j dk}|oPt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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 s>0    Y  tj| j| j|||	| _d}| jrvd| j_W n  t
ttfy   d| _Y n0 |   d
}| js| j jtd z
| j}W n t
y   d}Y n0 | j D ]0\}}}}	t | j||	| j!}z || |"| j| |j#rF|$| j |%d\}}n|}z|| j&}W nL t't(j)fy } z,t*rW Y d}~q n
td|W Y d}~n
d}~0 0 |st*rq ntdt| d|| }|%|\}}|dk rq ||d }qJW |+  n
|+  0 qg | _ || _,| -  | j.rH| j/rH| j0  d| _| jsnt*sn|dk rnt1| tj| S )z"Load image data based on tile listNzcannot load this imager   pypy_version_infor   Fraw   )accessr   key    z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seekr/   Z	_MAPMODESmmapr$   filenoZACCESS_READr   Z
map_bufferr0   imZpaletteZdirtyImportErrorload_preparesortr   Ztile_prefix_getdecoderr!   setimagepulls_fdsetfddecoder#   r)   r-   r   LOAD_TRUNCATED_IMAGEScleanupr    load_endr'   Z!_close_exclusive_fp_after_loadingr2   r   )r3   ZpixelZuse_mmapr    rJ   rL   Zdecoder_nameextentsoffsetargsrM   r%   Zerr_codeprefixdecoderstatusbsenr   r   r   rD      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)rO   r/   r0   r   r   newrD   r:   r   r   r   rQ     s    "
zImageFile.load_preparec                 C   s   d S r7   r   r:   r   r   r   rZ     s    zImageFile.load_endc                 C   sB   || j k s.t| dr| jd u s6|| j| j  kr6td|  |kS )N	_n_framesz attempt to seek outside sequence)r   rG   rg   Zn_framesr,   tell)r3   framer   r   r   _seek_check*  s    	zImageFile._seek_check)NN)__name__
__module____qualname____doc__r   r;   r<   rD   rQ   rZ   rj   __classcell__r   r   r5   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   s   t dd S )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s8J |j| _|j| _d S )Nzcannot find loader for this z file)_loadr   r8   rD   r6   __dict__)r3   loaderimager   r   r   rD   D  s    
zStubImageFile.loadc                 C   s   t ddS )z (Hook) Find actual image loader.z+StubImageFile subclass must implement _loadNrq   r:   r   r   r   rs   N  s    zStubImageFile._loadN)rk   rl   rm   rn   r(   rD   rs   r   r   r   r   rp   9  s   
rp   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r| jdkrztt| j| j}| j|d | _| j| | _| jdksv| jszdS | j| j\}}|dk rd| _d| _ |dk rd| _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 n0 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   rK   rI   )finishedrx   r_   r\   minrF   rW   rv   r   ioBytesIOr   r$   r   rG   r   rQ   rS   r/   r!   rT   rO   )r3   rx   skiprd   rc   r%   rO   flagdoar   r   r   feedh  sR    	


.zParser.feedc                 C   s   | S r7   r   r:   r   r   r   	__enter__  s    zParser.__enter__c                 G   s   |    d S r7   )r2   r3   r]   r   r   r   __exit__  s    zParser.__exit__c              	   C   s   | j r*| d d | _| _ | js*td| js8td| jrt| j8}zt	|| _W | j
  n| j
  0 W d   n1 s0    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)r_   r   rx   rz   r   rv   r|   r}   r   r$   rD   )r3   r%   r   r   r   r2     s    

6zParser.close)rk   rl   rm   rn   incrementalrv   rx   r_   r\   rz   ry   r   r   r   r2   r   r   r   r   rw   S  s   Prw   c              
   C   s$  |    t| dsd| _|jtd tt|| jd d }z|tj	kpP|tj	j
k}W n ttfyn   d}Y n0 |r|  dS z| }|  W n ttjfyr } z|D ]\}}}	}
t| j||
| j}|	dkr||	 || j| |jr|| | \}}n(||\}}}|| |rq8q|dk rTtd| d	||  qW Y d}~nd}~0 0 |D ]\}}}	}
t| j||
| j}|	dkr||	 || j| |jr|| | \}}n|||}|dk r td| d	|  qxt|d
r |  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   rA   r      FNzencoder error z when writing image fileflush)rD   rG   r   rR   r   maxr"   r0   rH   stdoutbufferr   r	   r   rN   r|   UnsupportedOperationr   Z_getencoderr/   rL   rT   rO   Z	pushes_fdrV   Zencode_to_pyfdencodewriterY   Zencode_to_file)rO   r%   r   bufsizer   fhexcrc   ra   r   r   lrb   r   r   r   r   _save  sZ    	





 



r   c                 C   s   |dkrdS |t kr6| |}t||k r2td|S g }|dkrp| t|t }|sXqp|| |t|8 }q:tdd |D |k rt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   rC   zTruncated File Readc                 s   s   | ]}t |V  qd S r7   )rF   ).0r   r   r   r   	<genexpr>;  rC   z_safe_read.<locals>.<genexpr>)	SAFEBLOCKrJ   rF   r   r{   appendsumjoin)r%   r0   rx   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 r7   )r   r   r   r   r:   r   r   r   r[   G  s    zPyCodecState.extentsN)rk   rl   rm   r   r[   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 r7   )rO   r   statefdr/   init)r3   r/   r]   r   r   r   r   U  s
    zPyDecoder.__init__c                 C   s
   || _ dS )z
        Override to perform decoder specific initialization

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

        :returns: None
        Nr   r:   r   r   r   rY   t  s    zPyDecoder.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   )r3   r   r   r   r   rV   |  s    zPyDecoder.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rtd| jj| jj | j jd ks| jj| jj | j jd krt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)rO   r0   r   r   r   r   r   
ValueError)r3   rO   r[   Zx0Zy0x1y1r   r   r   rT     s"    zPyDecoder.setimagec                 C   sd   |s
| j }t| j d|}|| j| j  ||}|d dkrLtd|d dkr`t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
        r>   r   znot enough image datar   zcannot decode image dataN)	r/   r   rS   rT   rO   r   r[   rW   r   )r3   rx   rawmoder   rb   r   r   r   
set_as_raw  s    

zPyDecoder.set_as_raw)N)N)rk   rl   rm   rn   r   r   r   propertyrU   rW   rY   rV   rT   r   r   r   r   r   r   K  s   	
	
#r   )r   )r|   r-   rH   r    r   Z_utilr   r"   r   rX   r
   r   r   r   r   rp   rw   r   r   r   r   r   r   r   r   <module>   s4   
	
 a 
B 