o �J�hai�@s|ddlmZddlZddlZddlZddlZddlZddlZddlm Z m Z m Z m Z m Z ddlmZddlmZddlmZe rFddlmZd Zd Zd Z d d dddd�Z dEdd�ZdFdd�ZdGdd�ZGd d�de �ZGd!d"�d"ej�ZGd#d$�d$�ZGd%d&�d&e�Z Gd'd(�d(�Z!dHdId1d2�Z" dJdKd7d8�Z#dLd;d<�Z$Gd=d>�d>�Z%Gd?d@�d@�Z&GdAdB�dBe&�Z'GdCdD�dDe&�Z(dS)M�)� annotationsN)�IO� TYPE_CHECKING�Any� NamedTuple�cast�)�Image)� deprecate)�is_path)�StrOrBytesPathiiFzimage buffer overrun errorzdecoding errorz unknown errorzbad configurationzout of memory error)��������������������i�����error�int�encoder�bool�return�OSErrorcCshztj�|�}Wntyt�|�}Ynw|s$|rdnd�d|��}|d|r*dnd�d�7}t|�S)Nr�decoderz error z when �writing�readingz image file)r �core�getcodecstatus�AttributeError�ERRORS�getr)rr�msg�r �KC:\pinokio\api\whisper-webui.git\app\env\lib\site-packages\PIL\ImageFile.py� _get_oserrorHs �r"cCstdddd�t|dd��)N� raise_oserror� z�It is only useful for translating error codes returned by a codec's decode() method, which ImageFile already does automatically.)�actionF�r)r r")rr r r!r#Ss � r#�t�_TilecCs|dS)N�r )r'r r r!� _tilesort]sr*c@s.eZdZUded<ded<ded<ded<d S) r(�str� codec_name� tuple[int, int, int, int] | None�extentsr�offsetztuple[Any, ...] | str | None�argsN)�__name__� __module__� __qualname__�__annotations__r r r r!r(bs  cszeZdZdZ d"d#�fd d � Zd$d d �Zd%dd�Zd&�fdd� Zd$dd�Zd'dd�Z d$dd�Z d$dd�Z d(d d!�Z �Z S))� ImageFilez*Base class for image file format handlers.N�fp�StrOrBytesPath | IO[bytes]�filename�str | bytes | Noner�Nonec st���d|_d|_g|_ d|_d|_t|_t |�r0t |d�|_ t j �t �|��|_d|_nttt|�|_ |dur>|nd|_d|_z8z|��Wntttttjfyb}zt|�|�d}~ww|jrt|jddkst|jddkrzd}t|��WdSty�|jr�|j � ��w) Nrrr �rbT�Fznot identified by this driver)!�super�__init__� _min_frame�custom_mimetype�tile�readonly� decoderconfig�MAXBLOCK�decodermaxblockr �openr6�os�path�realpath�fspathr8� _exclusive_fprr�bytes�_open� IndexError� TypeError�KeyError�EOFError�structr� SyntaxError�mode�size� BaseException�close)�selfr6r8�vr�� __class__r r!r>qsJ    � ��" �  �zImageFile.__init__cC�dS�Nr �rXr r r!rM��zImageFile._open� str | NonecCs,|jr|jS|jdurtj�|j���SdSr])r@�formatr �MIMEr�upperr^r r r!�get_format_mimetype�s  zImageFile.get_format_mimetype�state� list[Any]csg|_t��|�dSr])rAr=� __setstate__)rXrerZr r!rg�szImageFile.__setstate__cCs|jr|j��d|_dS)zCheck file integrityN)rKr6rWr^r r r!�verify�s  zImageFile.verify�Image.core.PixelAccess | Nonec Cs�|js|jdurd}t|��tj�|�}|js|Sd|_|jo%t|j�dk}|o-tt d� }d}t|d�r;|j }d}n|j j }t|d�rJ|j }d}n|j j}|r�|jd\}}} } t| t�rc| ddf} |d kr�t| t�r�t| �d kr�| d|jkr�| dtjvr�zSddl} t|j��} | j| ��d| jd �|_Wd�n1s�wY| |jd| d|j��kr�d }t|��tj�|j|j|| | �|_d}|jr�d|j_Wntttfy�d|_Ynw|� �d } |j�s�|jj!t"d�t#|dd�}dd�t$�%|jdd��D�|_|jD]�\}}} } || �t�&|j|| |j'�}zw|�(|j|�|j)�r:|�*|j �|�+d�d} nW|} z||j,�}Wn t-t.j/f�yd}zt0�rYWYd}~n8d}t|�|�d}~ww|�sxt0�rln%dt|��d�}t|��||}|�+|�\}} |dk�r�n||d�}�q=W|�1��q |�1�wg|_||_2|�3�|j4�r�|j5�r�|j �6�d|_ |j�s�t0�s�| dk�r�t7| dd��tj�|�S)z"Load image data based on tile listNzcannot load this imagerZpypy_version_infor� load_readF� load_seek�raw�)�accesszbuffer is not large enoughr��keyZ tile_prefix�cSsg|] \}}t|�d�qS)r )�list)�.0�_Ztilesr r r!� <listcomp>s ��z"ImageFile.load.<locals>.<listcomp>cSs|d|d|dfS)Nrrrmr )rAr r r!�<lambda>sz ImageFile.load.<locals>.<lambda>Tzimage file is truncatedzimage file is truncated (z bytes not processed)r&)8rA�_imrr �load�mapr8�len�hasattr�sysrjr6�readrk�seek� isinstancer+�tuplerT� _MAPMODES�mmaprF�fileno� ACCESS_READrUr� map_buffer�im�palette�dirtyr� ImportError� load_prepare�sortr*�getattr� itertools�groupby� _getdecoderrC�setimage�pulls_fd�setfd�decoderErNrRr�LOAD_TRUNCATED_IMAGES�cleanuprB�load_endrK�!_close_exclusive_fp_after_loadingrWr")rXrZpixelZuse_mmaprBr}r~� decoder_namer.r/r0r�r6Zerr_code�prefixr�b�s�e�nr r r!rx�s�     �  � �� �  �� �   ����  ��   zImageFile.loadcCs<|jdurtj�|j|j�|_|jdkrtj�|�dSdS)N�P)rwr r�newrTrUr�rxr^r r r!r�Cs  �zImageFile.load_preparecCr\r]r r^r r r!r�K�zImageFile.load_end�framerrcCsJ||jkst|d�r|jdus|t|d�|jkrd}t|��|��|kS)N� _n_frames�n_framesz attempt to seek outside sequence)r?r{r�r�rQ�tell)rXr�rr r r!� _seek_checkWs �  zImageFile._seek_checkr])r6r7r8r9rr:�rr:)rr`)rerfrr:�rri)r�rrr)r1r2r3�__doc__r>rMrdrgrhrxr�r�r�� __classcell__r r rZr!r5ns� 2     r5c@s&eZdZd dd�Zejd dd ��Zd S) � StubHandlerr�� StubImageFilerr:cCr\r]r �rXr�r r r!rFhr_zStubHandler.open� Image.ImagecCr\r]r r�r r r!rxkr�zStubHandler.loadN)r�r�rr:)r�r�rr�)r1r2r3rF�abc�abstractmethodrxr r r r!r�gs r�c@s.eZdZdZd dd�Zddd�Zdd d �Zd 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. rr:cC� d}t|��)Nz+StubImageFile subclass must implement _open��NotImplementedError�rXrr r r!rMxszStubImageFile._openricCsT|��}|durd|j�d�}t|��|�|�}|dusJ�|j|_|j|_|��S)Nzcannot find loader for this z file)�_loadrarrxr[�__dict__)rX�loaderr�imager r r!rx|s  zStubImageFile.load�StubHandler | NonecCr�)z (Hook) Find actual image loader.z+StubImageFile subclass must implement _loadr�r�r r r!r��szStubImageFile._loadNr�r�)rr�)r1r2r3r�rMrxr�r r r r!r�ps    r�c@steZdZUdZdZdZded<dZded<dZded<d Z d Z dd d �Z ddd�Z ddd�Z ddd�Zddd�ZdS)�Parserzj Incremental image parser. This class implements the standard feed/close consumer interface. NzImage.Image | Noner�z bytes | None�dataz,Image.core.ImagingDecoder | PyDecoder | Nonerrrr:cCs|jdus Jd��dS)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. Nzcannot reuse parsers)r�r^r r r!�reset�sz Parser.resetrLc Cs�|jrdS|jdur||_n|j||_|jri|jdkr=tt|j�|j�}|j|d�|_|j||_|jdks;|js=dS|j�|j�\}}|dkr_d|_d|_|dkr]d|_t|dd��dS|j|d�|_dS|jrndSzt � |j�� }t � |�}Wd�n1s�wYWn t y�YdSwt|d�p�t|d�}|s�t|j�dkr�d|_n:|��|jd\}}} } g|_t �|j|| |j�|_|j�|j|�| |_|jt|j�kr�|j|jd�|_d|_||_dS)z� (Consumer) Feed data to the parser. :param data: A string buffer. :exception OSError: If the parser failed to parse the image file. NrrFr&rkrj)�finishedr�rr/�minrzr�r�r"�io�BytesIOr rFrr{rAr�r�rTrCr�r�) rXr��skipr�r�r6r��flag�d�o�ar r r!�feed�sX       �� � z Parser.feedcCs|Sr]r r^r r r!� __enter__�r_zParser.__enter__r0�objectcGs |��dSr])rW�rXr0r r r!�__exit__�s zParser.__exit__r�c Cs�|jr|�d�d|_|_|jsd}t|��|js d}t|��|jrOt�|j��}z t� |�|_W|j� �n|j� �wWd�|jS1sJwY|jS)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. rqNzimage was incompletezcannot parse this image) rr�r�r�rr�r�r�r rFrx)rXrr6r r r!rW�s$   ��z Parser.closer�)r�rLrr:)rr�)r0r�rr:)rr�)r1r2r3r�Z incrementalr�r4r�rr/r�r�r�r�r�rWr r r r!r��s       L r�r�r�r6� IO[bytes]rA� list[_Tile]�bufsizer:c Cs�|��t|d�s d|_|jtd�tt||jdd�}z|��}|� �t |||||�Wnt t j fyM}zt ||||d|�WYd}~nd}~wwt|d�rY|� �dSdS)z�Helper 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 ror�N�flush)rxr{r�r�r*�maxrDrUr�r�� _encode_tilerr��UnsupportedOperation)r�r6rAr��fh�excr r r!�_saves   ��  �r�r�� int | Noner��BaseException | Nonec Cs�|D]i\}}}} |dkr|�|�t�|j|| |j�} zK| �|j|�| jr2| �|�| � �d} n$|rJ | � |�dd�\} } |� | �| rHnq5n |dusPJ�| � ||�} | dkrat | dd�|�W| ��q| ��wdS)NrrTr&)r~r � _getencoderrTr�r�r�� pushes_fdr��encode_to_pyfd�encode�write�encode_to_filer"r�) r�r6rAr�r�r�� encoder_namer.r/r0r�errcoder�r r r!r�3s0   �  ��r�rUrLcCs�|dkrdS|tkr|�|�}t|�|krd}t|��|Sg}|}|dkr?|�t|t��}|s0n|�|�|t|�8}|dks%tdd�|D��|krPd}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 rrqzTruncated File Readcss�|]}t|�VqdSr])rz)rs�blockr r r!� <genexpr>ss�z_safe_read.<locals>.<genexpr>)� SAFEBLOCKr}rzrr��append�sum�join)r6rUr�r�blocksZremaining_sizer�r r r!� _safe_readVs*     � r�c@s eZdZd dd�Zd dd�ZdS) � PyCodecStaterr:cCsd|_d|_d|_d|_dS)Nr)�xsize�ysize�xoff�yoffr^r r r!r>zs zPyCodecState.__init__�tuple[int, int, int, int]cCs |j|j|j|j|j|jfSr])r�r�r�r�r^r r r!r.�s zPyCodecState.extentsNr�)rr�)r1r2r3r>r.r r r r!r�ys r�c@sLeZdZUded<dd d �Zdd d �Zddd�Zddd�Z dddd�ZdS) �PyCodeczIO[bytes] | None�fdrTr+r0rrr:cGs(d|_t�|_d|_||_|�|�dSr])r�r�rer�rT�init)rXrTr0r r r!r>�s zPyCodec.__init__�tuple[Any, ...]cC� ||_dS)z� Override to perform codec specific initialization :param args: Tuple of arg items from the tile entry :returns: None N)r0r�r r r!r��� z PyCodec.initcCsdS)zT Override to perform codec specific cleanup :returns: None Nr r^r r r!r��szPyCodec.cleanupr�cCr�)z� Called from ImageFile to set the Python file-like object :param fd: A Python file-like object :returns: None N)r�)rXr�r r r!r��r�z PyCodec.setfdNr��Image.core.ImagingCorer.r-cCs�||_|r |\}}}}nd\}}}}|dkr%|dkr%|jj\|j_|j_n||j_||j_|||j_|||j_|jjdksE|jjdkrKd}t|��|jj|jj|jjdksg|jj|jj|jjdkrmd}t|��dS)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 )rrrrrzSize cannot be negativerz Tile cannot extend outside imageN)r�rUrer�r�r�r�� ValueError)rXr�r.�x0�y0�x1�y1rr r r!r��s$   �zPyCodec.setimage)rTr+r0rrr:)r0r�rr:r�)r�r�rr:r])r�r�r.r-rr:) r1r2r3r4r>r�r�r�r�r r r r!r��s     �r�c@s:eZdZdZdZeddd��Zdd d �Z dddd�Zd 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>` FrrcC�|jSr])� _pulls_fdr^r r r!r���zPyDecoder.pulls_fd�buffer�$bytes | Image.SupportsArrayInterface�tuple[int, int]cC� 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`. zunavailable in base decoderr�)rXr�rr r r!r��� zPyDecoder.decodeNr r�rL�rawmoder`�extrar�r:cCs||s|j}t�|jd||�}|jdusJ�|�|j|j���|�|�}|ddkr0d}t|��|ddkr<d}t|��dS)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 rlNrznot enough image datarzcannot decode image data) rTr r�r�r�rer.r�r�)rXr�rrr�r�rr r r!� set_as_raw�s    �zPyDecoder.set_as_raw�rr)r�r�rr)Nr )r�rLrr`rr�rr:) r1r2r3r�r��propertyr�r�rr r r r!r��s   �r�c@s@eZdZdZdZeddd��Zdd d �Zdd d�Zddd�Z dS)� 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>` FrrcCr�r])� _pushes_fdr^r r r!r�r�zPyEncoder.pushes_fdr�r�tuple[int, int, bytes]cCr)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`. zunavailable in base encoderr�)rXr�rr r r!r�rzPyEncoder.encodercCs@|jsdS|�d�\}}}|r|jdusJ�|j�|�||fS)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`. )rrrN)r�r�r�r�)rX�bytes_consumedr�r�r r r!r�"s zPyEncoder.encode_to_pyfdr�cCsDd}|dkr |�|�\}}}|dkrt�|||d��|dks|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`. rN)r�rGr�)rXr�r�r��status�bufr r r!r�2s �zPyEncoder.encode_to_fileNr)r�rrr )rr)r�rr�rrr) r1r2r3r�r rr�r�r�r�r r r r!rs   r)rrrrrr)rrrr)r'r(rr)r) r�r�r6r�rAr�r�rrr:r])r�r�r6r�rAr�r�rr�r�r�r�rr:)r6r�rUrrrL))� __future__rr�r�r�rGrRr|�typingrrrrrr<r � _deprecater �_utilr �_typingr rDr�r�rr"r#r*r(r5r�r�r�r�r�r�r�r�r�rr r r r!�<module>sT     �  z  " � ## N6
Memory