3

hR              
   @   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d)dd*di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 error   zdecoding error   zunknown error   zbad configuration	   zout of memory errorc             C   sN   yt jj| }W n tk
r.   tj| }Y nX |s>d|  }t|d d S )Nzdecoder error z when reading image file)r   coreZgetcodecstatusAttributeErrorERRORSgetOSError)errormessage r   M/var/www/html/enquirykeeper_venv/lib/python3.6/site-packages/PIL/ImageFile.pyraise_oserror<   s    
r   c             C   s   t j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 )Nr   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  j  d| _d | _d | _d| _f | _t| _t	|rPt
|d| _|| _d| _n|| _|| _d | _yhy| j  W n8 tttttjfk
r } zt||W Y d d }~X nX | j s| jd dkrtdW n& tk
r   | jr| jj   Y nX d S )Nr   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 k	r(tjj| 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j  d| _dS )zCheck file integrityN)r'   r%   r2   )r3   r   r   r   verify   s    
zImageFile.verifyc          >   C   sX  | j dkrtdtjj| }| j s(|S d| _| jo@t| j dk}|oPttd }d}y| j	}d}W n t
k
r   | jj}Y nX y| j}d}W n t
k
r   | jj}Y nX |r| j d \}}}}	|dkot|	dko|	d | jko|	d tjkryhddl}
t| j}|
j|j d|
jd	| _W dQ R X tjj| j| j|||	| _d}| jr^d| j_W n" t
ttfk
r   d| _Y nX | j  d}| js| j jtd
 y
| j}W n t
k
r   d}Y nX x$| j D ]\}}}}	tj | j||	| j!}z|| |j"| j| |j#r4|j$| j |j%d\}}n|}xy|| j&}W n> t't(j)fk
r } zt*rlP n
td|W Y dd}~X nX |st*rP ntdt| d|| }|j%|\}}|dk rP ||d }q:W W d|j+  X qW g | _ || _,| j-  | j.r"| j/r"| jj0  d| _| j rLt* rL|dk rLt1| tjj| S )z"Load image data based on tile listNzcannot load this imager   pypy_version_infor   Frawr   )access)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    rF   rH   Zdecoder_nameextentsoffsetargsrI   r%   Zerr_codeprefixdecoderstatusbsenr   r   r   r@      s    
"



zImageFile.loadc             C   sR   | j  s$| j j| jks$| j j| jkr8tjj| j| j| _ | jdkrNtjj|  d S )NP)rK   r/   r0   r   r	   newr@   )r3   r   r   r   rM     s    $
zImageFile.load_preparec             C   s   d S )Nr   )r3   r   r   r   rV     s    zImageFile.load_endc             C   sD   || j k s0t| do| jd k r8|| j| j  kr8td| j |kS )N	_n_framesz attempt to seek outside sequence)r   rC   rc   Zn_framesr,   tell)r3   framer   r   r   _seek_check*  s
    
zImageFile._seek_check)NN)__name__
__module____qualname____doc__r   r8   r9   r@   rM   rV   rf   __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)NotImplementedError)r3   r   r   r   r(   A  s    zStubImageFile._openc             C   sL   | j  }|d kr"td| j d|j| }|d k	s8t|j| _|j| _d S )Nzcannot find loader for this z file)_loadr   r6   r@   AssertionErrorr5   __dict__)r3   loaderimager   r   r   r@   D  s    
zStubImageFile.loadc             C   s   t ddS )z (Hook) Find actual image loader.z+StubImageFile subclass must implement _loadN)rm   )r3   r   r   r   rn   N  s    zStubImageFile._loadN)rg   rh   ri   rj   r(   r@   rn   r   r   r   r   rl   9  s   
rl   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kst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)dataro   )r3   r   r   r   reset`  s    zParser.resetc             C   s  | j r
dS | jdkr|| _n| j| | _| jr| jdkr|tt| j| j}| j|d | _| j| | _| jdksx| j r|dS | jj| j\}}|dk rd| _d| _ |dk rd| _t| ndS | j|d | _n| jrny&t	j
| j}tj|}W dQ R X W n tk
r   Y nX t|dp*t|d}|sBt|jdkrJd| _nv|j  |jd \}}}	}
g |_tj|j||
|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   rG   rE   )finishedrt   r[   rX   minrB   rS   rr   r   ioBytesIOr   r$   r   rC   r   rM   rO   r/   r!   rP   rK   )r3   rt   skipr`   r_   r%   rK   flagdoar   r   r   feedh  sR    	


zParser.feedc             C   s   | S )Nr   )r3   r   r   r   	__enter__  s    zParser.__enter__c             G   s   | j   d S )N)r2   )r3   rY   r   r   r   __exit__  s    zParser.__exit__c             C   sz   | j r*| jd d | _| _ | js*td| js8td| jrttj| j$}ztj	|| _W d| jj
  X W dQ R X | 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   rt   rv   r   rr   rx   ry   r   r$   r@   )r3   r%   r   r   r   r2     s    

zParser.close)rg   rh   ri   rj   incrementalrr   rt   r[   rX   rv   ru   r   r   r   r2   r   r   r   r   rs   S  s   Prs   c             C   s.  | j   t| dsf | _|jtd tt|| jd d }y|tj	kpP|tj	j
k}W n ttfk
rp   d}Y nX |r|j  dS y|j }|j  W n ttjfk
rx } zx|D ]\}}}	}
tj| j||
| j}|	dkr|j|	 |j| j| |jr|j| |j \}}n*x(|j|\}}}|j| |rP qW |dk r\td| d||j  qW W Y dd}~X nX x|D ]\}}}	}
tj| j||
| j}|	dkr|j|	 |j| j| |jr|j| |j \}}n|j||}|dk rtd| d|j  qW t|d	r*|j  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
    encoderconfig)r=   r      FNzencoder error z when writing image fileflush)r@   rC   r   rN   r   maxr"   r0   rD   stdoutbufferr   r
   r   rJ   rx   UnsupportedOperationr   Z_getencoderr/   rH   rP   rK   Z	pushes_fdrR   Zencode_to_pyfdencodewriterU   Zencode_to_file)rK   r%   r   bufsizer   fhexcr_   r]   r}   r~   lr^   r|   r   r   r   _save  s\    	









r   c             C   s   |dkrdS |t kr6| j|}t||k r2td|S g }x8|dkrr| jt|t }|sZP |j| |t|8 }q<W tdd |D |k rtddj|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 )N)rB   ).0r|   r   r   r   	<genexpr>;  s    z_safe_read.<locals>.<genexpr>)	SAFEBLOCKrF   rB   r   rw   appendsumjoin)r%   r0   rt   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yoff)r3   r   r   r   r   A  s    zPyCodecState.__init__c             C   s    | j | j| j | j | j| j fS )N)r   r   r   r   )r3   r   r   r   rW   G  s    zPyCodecState.extentsN)rg   rh   ri   r   rW   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 | _|| _| j| d S )N)rK   r   statefdr/   init)r3   r/   rY   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)rY   )r3   rY   r   r   r   r   \  s    zPyDecoder.initc             C   s   | j S )N)	_pulls_fd)r3   r   r   r   rQ   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`.
        N)rm   )r3   r   r   r   r   rS   i  s    	zPyDecoder.decodec             C   s   dS )zV
        Override to perform decoder specific cleanup

        :returns: None
        Nr   )r3   r   r   r   rU   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   rR   |  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   zSize cannot be negativer   z Tile cannot extend outside imageN)r   r   r   r   )rK   r0   r   r   r   r   r   
ValueError)r3   rK   rW   Zx0Zy0x1y1r   r   r   rP     s    zPyDecoder.setimagec             C   sd   |s
| j }tj| j d|}|j| j| j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   rO   rP   rK   r   rW   rS   r   )r3   rt   rawmoder|   r^   r   r   r   
set_as_raw  s    

zPyDecoder.set_as_raw)N)N)rg   rh   ri   rj   r   r   r   propertyrQ   rS   rU   rR   rP   r   r   r   r   r   r   K  s   		
#r   i   r?   ii)r   )rx   r-   rD   r    r   Z_utilr   r"   r   rT   r   r   r   r   r   rl   rs   r   r   r   r   r   r   r   r   <module>   s2   

	
 a 
B 