a
    Agf                  	   @  s  d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	m
Z
mZmZmZ ddlmZ ddlmZ ddlmZ e
rddlmZ d	Zd
ZdZddddddZddddddZdddddZdddddZG dd deZG d d! d!ejZG d"d# d#ZG d$d% d%eZ G d&d' d'Z!d@d(d)d*dd+d,d-d.Z"dAd(d)d*dd/d0d+d1d2d3Z#d)dd4d5d6d7Z$G d8d9 d9Z%G d:d; d;Z&G d<d= d=e&Z'G d>d? d?e&Z(dS )B    )annotationsN)IOTYPE_CHECKINGAny
NamedTuplecast   )Image)	deprecate)is_path)StrOrBytesPathi   i   Fzimage buffer overrun errorzdecoding errorzunknown errorzbad configurationzout of memory error)iintboolOSError)errorencoderreturnc                C  sh   zt j| }W n ty,   t| }Y n0 |sH|r:dnd d|  }|d|rTdnd d7 }t|S )Nr   decoderz error z when writingreadingz image file)r	   coreZgetcodecstatusAttributeErrorERRORSgetr   )r   r   msg r   L/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/PIL/ImageFile.py_get_oserrorH   s    r!   )r   r   c                 C  s   t dddd t| ddd S )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"   S   s    r"   _Tile)tr   c                 C  s   | d S )N   r   )r'   r   r   r    	_tilesort]   s    r)   c                   @  s6   e Zd ZU ded< ded< dZded< dZd	ed
< dS )r&   strZ
codec_name tuple[int, int, int, int] | Noneextentsr   r   offsetNztuple[Any, ...] | str | Noneargs)__name__
__module____qualname____annotations__r-   r.   r   r   r   r    r&   b   s   
c                      s   e Zd ZdZd!dddd fddZdd	d
dZdd	ddZddd fddZdd	ddZdd	ddZ	dd	ddZ
dd	ddZddddd Z  ZS )"	ImageFilez*Base class for image file format handlers.NzStrOrBytesPath | IO[bytes]zstr | bytes | NoneNone)fpfilenamer   c                   s&  t    d| _d | _g | _d| _d| _t| _t	|rVt
|d| _t|| _d| _n(ttt || _|d urr|nd| _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rd}t|W n( ty    | jr| j   Y n0 d S )	Nr   r   r   rbT Fznot identified by this driver)super__init__
_min_framecustom_mimetypetilereadonlydecoderconfigMAXBLOCKdecodermaxblockr   openr5   osfspathr6   _exclusive_fpr   r   bytes_open
IndexError	TypeErrorKeyErrorEOFErrorstructr   SyntaxErrormodesizeBaseExceptionclose)selfr5   r6   vr   	__class__r   r    r:   q   s@    
 "
zImageFile.__init__r   c                 C  s   d S Nr   rR   r   r   r    rG      s    zImageFile._open
str | Nonec                 C  s,   | j r| j S | jd ur(tj| j S d S rW   )r<   formatr	   ZMIMEr   upperrX   r   r   r    get_format_mimetype   s
    
zImageFile.get_format_mimetypez	list[Any])stater   c                   s   g | _ t | d S rW   )r=   r9   __setstate__)rR   r]   rT   r   r    r^      s    zImageFile.__setstate__c                 C  s   | j r| j  d| _dS )zCheck file integrityN)rE   r5   rQ   rX   r   r   r    verify   s    
zImageFile.verifyImage.core.PixelAccess | Nonec                 C  s  | j s| jdu rd}t|tj| }| j s2|S d| _| joJt| j dk}|oZtt	d }d}t| drv| j
}d}n| jj}t| dr| j}d}n| jj}|r| j d \}}}	}
t|
tr|
ddf}
|d	krt|
trt|
d
kr|
d | jkr|
d tjv rzddl}t| j(}|j| d|jd| _W d   n1 sP0    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0 |    d}| jsh| j j!t"d t#| dd}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 } z0t0rW Y d}~qNnd}t||W Y d}~n
d}~0 0 |st0rqNndt| d}t||| }|+|\}}|dk r>qN||d }qW |1  n
|1  0 q,g | _ || _2| 3  | j4r| j5r| j6  d| _| jst0s|dk rt7|ddtj| S )z"Load image data based on tile listNzcannot load this imager   pypy_version_infor   	load_readF	load_seekraw   )accesszbuffer is not large enoughr   keyZtile_prefix    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   re   r   )r=   r   r   r    <lambda>  ri   z ImageFile.load.<locals>.<lambda>zimage file is truncatedzimage file is truncated (z bytes not processed)r%   )8r=   _imr   r	   loadmapr6   lenhasattrsysrb   r5   readrc   seek
isinstancer*   tuplerN   Z	_MAPMODESmmaprB   filenoZACCESS_READrO   r   Z
map_bufferimZpaletteZdirtyr   ImportErrorload_preparesortr)   getattr	itertoolsgroupby_getdecoderr?   setimagepulls_fdsetfddecoderA   rH   rL   r   LOAD_TRUNCATED_IMAGEScleanupr>   load_endrE   Z!_close_exclusive_fp_after_loadingrQ   r!   )rR   r   ZpixelZuse_mmapr>   ru   rv   Zdecoder_namer,   r-   r.   ry   r5   Zerr_codeprefixr   bsenr   r   r    rp      s    




8"
 

zImageFile.loadc                 C  s8   | j d u rtj| j| j| _| jdkr4tj|  d S )NP)ro   r	   r   newrN   rO   r{   rp   rX   r   r   r    r}   C  s    

zImageFile.load_preparec                 C  s   d S rW   r   rX   r   r   r    r   K  s    zImageFile.load_endr   r   )framer   c                 C  sJ   || j k s2t| dr| jd u s>|t| d| j  kr>d}t||  |kS )N	_n_framesZn_framesz attempt to seek outside sequence)r;   rs   r   r   rK   tell)rR   r   r   r   r   r    _seek_checkW  s    	zImageFile._seek_check)N)r/   r0   r1   __doc__r:   rG   r\   r^   r_   rp   r}   r   r   __classcell__r   r   rT   r    r3   n   s    2	 
r3   c                   @  s2   e Zd ZdddddZejdddddZd	S )
StubHandlerStubImageFiler4   )r{   r   c                 C  s   d S rW   r   rR   r{   r   r   r    rB   h  s    zStubHandler.openImage.Imagec                 C  s   d S rW   r   r   r   r   r    rp   k  s    zStubHandler.loadN)r/   r0   r1   rB   abcabstractmethodrp   r   r   r   r    r   g  s   r   c                   @  s:   e Zd ZdZddddZddddZd	dd
dZdS )r   z
    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.
    r4   rV   c                 C  s   d}t |d S )Nz+StubImageFile subclass must implement _openNotImplementedErrorrR   r   r   r   r    rG   x  s    zStubImageFile._openr`   c                 C  sT   |   }|d u r&d| j d}t||| }|d us<J |j| _|j| _| S )Nzcannot find loader for this z file)_loadrZ   r   rp   rU   __dict__)rR   loaderr   imager   r   r    rp   |  s    
zStubImageFile.loadzStubHandler | Nonec                 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)r/   r0   r1   r   rG   rp   r   r   r   r   r    r   p  s   r   c                   @  s   e Zd ZU dZdZdZded< dZded< dZded< d	Z	d	Z
d
dddZdd
dddZd dddZdd
dddZddddZdS )Parserzj
    Incremental image parser.  This class implements the standard
    feed/close consumer interface.
    NzImage.Image | Noner   zbytes | Nonedataz,Image.core.ImagingDecoder | PyDecoder | Noner   r   r4   rV   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)r   rX   r   r   r    reset  s    zParser.resetrF   )r   r   c                 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|dd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pDt|d}|s\t|jdkrdd| _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   Fr%   rc   rb   )finishedr   r   r-   minrr   r   r   r!   ioBytesIOr	   rB   r   rs   r=   r}   r   rN   r?   r   r{   )rR   r   skipr   r   r5   r{   flagdoar   r   r    feed  sR    	

.zParser.feedc                 C  s   | S rW   r   rX   r   r   r    	__enter__  s    zParser.__enter__objectr.   r   c                 G  s   |    d S rW   )rQ   rR   r.   r   r   r    __exit__  s    zParser.__exit__r   c              	   C  s   | j r.| d d | _| _ | js.d}t|| js@d}t|| 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.
        ri   Nzimage was incompletezcannot parse this image)r   r   r   r   r   r   r   r   r	   rB   rp   )rR   r   r5   r   r   r    rQ     s    

6zParser.close)r/   r0   r1   r   incrementalr   r2   r   r   r-   r   r   r   r   r   rQ   r   r   r   r    r     s   
Lr   r   	IO[bytes]zlist[_Tile]r4   )r{   r5   r=   bufsizer   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 )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   rg   r      Nflush)rp   rs   r   r~   r)   maxr@   rO   rz   r   _encode_tiler   r   UnsupportedOperation)r{   r5   r=   r   fhexcr   r   r    _save  s    	
(
r   z
int | NonezBaseException | None)r{   r5   r=   r   r   r   r   c              	   C  s   |D ]\}}}}	|dkr"| | t| j||	| j}
z|
| j| |
jrd|
| |
	 d }nF|r|

|dd  \}}|| |rhqqhn|d usJ |
||}|dk rt|dd|W |
  q|
  0 qd S )Nr   r   Tr%   )rv   r	   Z_getencoderrN   r   r   r{   	pushes_fdr   encode_to_pyfdencodewriteencode_to_filer!   r   )r{   r5   r=   r   r   r   Zencoder_namer,   r-   r.   r   errcoder   r   r   r    r   3  s&    


r   rF   )r5   rO   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 rd}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   ri   zTruncated File Readc                 s  s   | ]}t |V  qd S rW   )rr   )rk   blockr   r   r    	<genexpr>s  ri   z_safe_read.<locals>.<genexpr>)	SAFEBLOCKru   rr   r   r   appendsumjoin)r5   rO   r   r   blocksZremaining_sizer   r   r   r    
_safe_readV  s(    

r   c                   @  s(   e Zd ZddddZddddZdS )	PyCodecStater4   rV   c                 C  s   d| _ d| _d| _d| _d S )Nr   )xsizeysizexoffyoffrX   r   r   r    r:   z  s    zPyCodecState.__init__ztuple[int, int, int, int]c                 C  s    | j | j| j | j | j| j fS rW   )r   r   r   r   rX   r   r   r    r,     s    zPyCodecState.extentsN)r/   r0   r1   r:   r,   r   r   r   r    r   y  s   r   c                   @  sj   e Zd ZU ded< ddddddZd	dd
ddZddddZdddddZdddddddZdS )PyCodeczIO[bytes] | Nonefdr*   r   r4   )rN   r.   r   c                 G  s(   d | _ t | _d | _|| _| | d S rW   )r{   r   r]   r   rN   init)rR   rN   r.   r   r   r    r:     s
    zPyCodec.__init__tuple[Any, ...]r   c                 C  s
   || _ dS )z
        Override to perform codec specific initialization

        :param args: Tuple of arg items from the tile entry
        :returns: None
        N)r.   r   r   r   r    r     s    zPyCodec.initrV   c                 C  s   dS )zT
        Override to perform codec specific cleanup

        :returns: None
        Nr   rX   r   r   r    r     s    zPyCodec.cleanupr   )r   r   c                 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   )rR   r   r   r   r    r     s    zPyCodec.setfdNzImage.core.ImagingCorer+   )r{   r,   r   c                 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rd}t|| jj| jj | j jd ks| jj| jj | j jd krd}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)r{   rO   r]   r   r   r   r   
ValueError)rR   r{   r,   Zx0Zy0x1y1r   r   r   r    r     s&    zPyCodec.setimage)N)	r/   r0   r1   r2   r:   r   r   r   r   r   r   r   r    r     s   
	 r   c                   @  sL   e Zd ZdZdZeddddZddd	d
dZddddd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>`
    Fr   rV   c                 C  s   | j S rW   )	_pulls_fdrX   r   r   r    r     s    zPyDecoder.pulls_fdz$bytes | Image.SupportsArrayInterfacetuple[int, int])bufferr   c                 C  s   d}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`.
        zunavailable in base decoderNr   )rR   r   r   r   r   r    r     s    	zPyDecoder.decodeNr   rF   rY   r   r4   )r   rawmodeextrar   c                 C  s|   |s
| j }t| j d||}| jdus*J || j| j  ||}|d dkr`d}t||d dkrxd}t|dS )aS  
        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
        :param extra: Extra arguments for the decoder.
        :returns: None
        rd   Nr   znot enough image datar   zcannot decode image data)	rN   r	   r   r{   r   r]   r,   r   r   )rR   r   r   r   r   r   r   r   r   r    
set_as_raw  s    
zPyDecoder.set_as_raw)Nr   )	r/   r0   r1   r   r   propertyr   r   r   r   r   r   r    r     s    r   c                   @  sV   e Zd ZdZdZeddddZddd	d
dZddddZdddd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>`
    Fr   rV   c                 C  s   | j S rW   )
_pushes_fdrX   r   r   r    r     s    zPyEncoder.pushes_fdr   ztuple[int, int, bytes])r   r   c                 C  s   d}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`.
        zunavailable in base encoderNr   )rR   r   r   r   r   r    r     s    	zPyEncoder.encoder   c                 C  s@   | j s
dS | d\}}}|r8| jdus,J | 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   N)r   r   r   r   )rR   Zbytes_consumedr   r   r   r   r    r   "  s    zPyEncoder.encode_to_pyfd)r   r   r   c                 C  s>   d}|dkr:|  |\}}}|dkrt|||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   rC   r   )rR   r   r   r   statusbufr   r   r    r   2  s    	zPyEncoder.encode_to_fileN)
r/   r0   r1   r   r   r   r   r   r   r   r   r   r   r    r     s   r   )r   )N))
__future__r   r   r   r   rC   rL   rt   typingr   r   r   r   r   r8   r	   Z
_deprecater
   Z_utilr   _typingr   r@   r   r   r   r!   r"   r)   r&   r3   r   r   r   r   r   r   r   r   r   r   r   r   r   r    <module>   sN   
 z	 
" ##N6