
    Bwgf                       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
r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e      Z G d dej                        Z G d d      Z G d de      Z  G d d      Z!d-d.dZ"	 d/	 	 	 	 	 	 	 	 	 	 	 	 	 d0d Z#d1d!Z$ G d" d#      Z% G d$ d%      Z& G d& d'e&      Z' G d( d)e&      Z(y)2    )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)ic                   	 t         j                  j                  |       }|s|rdnd d|  }|d|rdnd dz  }t        |      S # t        $ r t        j                  |       }Y Ew xY w)Nencoderdecoderz error z when writingreadingz image file)r
   coregetcodecstatusAttributeErrorERRORSgetOSError)errorr   msgs      D/var/www/horilla/myenv/lib/python3.12/site-packages/PIL/ImageFile.py_get_oserrorr    H   sv     jj''. %95WUGDVIi8DDC3<   jj s   A A'&A'c                8    t        ddd       t        | d      )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   s    r   r"   r"   S   s%    
G ue
,,    _Tilec                    | d   S )N    )ts    r   	_tilesortr,   ]   s    Q4Kr&   c                  >    e Zd ZU ded<   ded<   dZded<   dZd	ed
<   y)r'   str
codec_name tuple[int, int, int, int] | Noneextentsr   intoffsetNztuple[Any, ...] | str | Noneargs)__name__
__module____qualname____annotations__r3   r4   r*   r&   r   r'   r'   b   s!    O--FCO)-D
&-r&   c                  v     e Zd ZdZ	 d	 	 	 	 	 d fdZddZddZd fdZddZddZ	ddZ
dd	Zdd
Z xZS )	ImageFilez*Base class for image file format handlers.c                   t         |           d| _        d | _        g | _        	 d| _        d| _        t        | _        t        |      r3t        |d      | _        t        j                  |      | _        d| _        n.t!        t"        t$           |      | _        ||nd| _        d| _        	 	 | j'                          | j6                  r$| j8                  d   dk  s| j8                  d   dk  rd}t5        |      y # t(        t*        t,        t.        t0        j2                  f$ r}t5        |      |d }~ww xY w# t:        $ r( | j                  r| j                  j=                           w xY w)	Nr   r	   r*   rbT Fznot identified by this driver)super__init__
_min_framecustom_mimetypetilereadonlydecoderconfigMAXBLOCKdecodermaxblockr   openfposfspathfilename_exclusive_fpr   r   bytes_open
IndexError	TypeErrorKeyErrorEOFErrorstructr   SyntaxErrormodesizeBaseExceptionclose)selfrH   rK   vr   	__class__s        r   r?   zImageFile.__init__q   sC    	+/!#	*.0'2;2tnDGIIbMDM!%D 2e9b)DG(0(<H"DM!&D		,

 99		! 1TYYq\Q5F5!#&& 6G  , "!n!+,  	!!		s*   -C; ==D7 ;(D4#D//D44D7 71E(c                     y Nr*   rY   s    r   rN   zImageFile._open       r&   c                    | j                   r| j                   S | j                  7t        j                  j	                  | j                  j                               S y r]   )rA   formatr
   MIMEr   upperr^   s    r   get_format_mimetypezImageFile.get_format_mimetype   sD    ''';;"::>>$++"3"3"566r&   c                2    g | _         t        | 	  |       y r]   )rB   r>   __setstate__)rY   stater[   s     r   rf   zImageFile.__setstate__   s    	U#r&   c                ^    | j                   r| j                  j                          d| _        y)zCheck file integrityN)rL   rH   rX   r^   s    r   verifyzImageFile.verify   s!    
 GGMMOr&   c                J
   | j                   s| j                  d}t        |      t        j                  j	                  |       }| j                   s|S d| _        | j                  xr t        | j                         dk(  }|xr t        t        d       }d}t        | d      r| j                  }d}n| j                  j                  }t        | d      r| j                  }d}n| j                  j                  }|rf| j                   d   \  }}}	}
t        |
t               r|
ddf}
|d	k(  r7t        |
t"              r&t        |
      d
k\  r|
d   | j$                  k(  r|
d   t        j&                  v r	 ddl}t+        | j                        5 }|j)                  |j-                         d|j.                        | _        ddd       |	| j0                  d   |
d   z  z   | j
                  j1                         kD  rd}t        |      t        j2                  j5                  | j
                  | j0                  ||	|
      | _        d}| j8                  rd| j8                  _        | jA                          d}| j
                  s| j                   jC                  tD               tG        | dd      }tI        jJ                  | j                   d       D cg c]  \  }}tM        |      d    c}}| _         | j                   D ]  \  }}}	}
 ||	       t        jN                  | j$                  ||
| jP                        }	 |jS                  | j6                  |       |jT                  r0|jW                  | j                         |jY                  d      d   }n^|}	 	  || jZ                        }|s!tb        rn?dt        |       d}t        |      ||z   }|jY                  |      \  }}|dk  rn||d }[|je                           g | _         || _3        | ji                          | jj                  r&| jl                  r| j                  jo                          d| _        | j
                  stb        s|dk  rtq        |d      t        j                  j	                  |       S # 1 sw Y   xY w# t<        t        t>        f$ r d| _        Y iw xY wc c}}w # t\        t^        j`                  f$ r}tb        rY d}~d}t        |      |d}~ww xY w# |je                          w xY w)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   keytile_prefixr&   c                    | d   | d   | d   fS )Nr   r	   ro   r*   )rB   s    r   <lambda>z ImageFile.load.<locals>.<lambda>  s    T!Wd1gtAw,G r&   r   zimage file is truncatedzimage file is truncated (z bytes not processed)r%   )9rB   _imr   r
   loadmaprK   lenhasattrsysrl   rH   readrm   seek
isinstancer.   tuplerU   	_MAPMODESmmaprG   filenoACCESS_READrV   r   
map_bufferimpalettedirtyr   ImportErrorload_preparesortr,   getattr	itertoolsgroupbylist_getdecoderrD   setimagepulls_fdsetfddecoderF   rO   rS   r   LOAD_TRUNCATED_IMAGEScleanuprC   load_endrL   !_close_exclusive_fp_after_loadingrX   r    )rY   r   pixeluse_mmaprC   r|   r}   decoder_namer1   r3   r4   r   rH   err_codeprefix_tilesr   bsens                         r   rw   zImageFile.load   sn    yyTXX-*C#,  &yyL%)==8S^q%8EGC1D$E E 4%>>DH77<<D4%>>DH77<<D26))A,/L'64$$a|%tU+INGtyy(Gu.$dmm, V#'99RYY[!DDTDT9#UV		!tAw 66H:%cl*#jj33$))\64DG  !H ||-.* 	xxIINNyN) T=#6F
 !* 1 1IIG!Au UBDI 8<yy (&3gvtV++II|T43E3E#&$$TWWg6''dgg.#*>>##6q#9"">$()=)=$> $%#8$)),,/F83H)J %( +2#,$6 !AA*1..*;KAx 1u % !!"A3 #8 OO%Q(&T 	 $"H"HGGMMOxx 5(Q,x77{{%%oV V '= $#DH$* %/#= >#8$)*CC*1#,A$=>2 OO%so   4R- 2R ?B#R- S=ATS+AT R*%R- -SST.T4T:TTTT"c                    | j                   9t        j                  j                  | j                  | j
                        | _        | j                  dk(  r t        j                  j                  |        y y )NP)rv   r
   r   newrU   rV   r   rw   r^   s    r   r   zImageFile.load_prepareC  sM    88jjnnTYY		:DG99KKT" r&   c                     y r]   r*   r^   s    r   r   zImageFile.load_endK      r&   c                    || j                   k  s4t        | d      r| j                  )|t        | d      | j                   z   k\  rd}t	        |      | j                         |k7  S )N	_n_framesn_framesz attempt to seek outside sequence)r@   rz   r   r   rR   tell)rY   framer   s      r   _seek_checkzImageFile._seek_checkW  s]    DOO# T;/DNN4JWT:6HH 5C3-yy{e##r&   r]   )rH   zStrOrBytesPath | IO[bytes]rK   zstr | bytes | NonereturnNoner   r   )r   
str | None)rg   z	list[Any]r   r   r   zImage.core.PixelAccess | None)r   r2   r   bool)r5   r6   r7   __doc__r?   rN   rd   rf   ri   rw   r   r   r   __classcell__)r[   s   @r   r:   r:   n   sU    4 NR0,08J0	0d$G&R#$r&   r:   c                  :    e Zd ZddZej
                  dd       Zy)StubHandlerc                     y r]   r*   rY   r   s     r   rG   zStubHandler.openh  r_   r&   c                     y r]   r*   r   s     r   rw   zStubHandler.loadk  r   r&   N)r   StubImageFiler   r   )r   r   r   Image.Image)r5   r6   r7   rG   abcabstractmethodrw   r*   r&   r   r   r   g  s!     	 r&   r   c                  (    e Zd ZdZddZddZddZy)	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.
    c                    d}t        |      )Nz+StubImageFile subclass must implement _openNotImplementedErrorrY   r   s     r   rN   zStubImageFile._openx  s    ;!#&&r&   c                    | j                         }|d| j                   d}t        |      |j                  |       }|J |j                  | _        |j
                  | _        |j                         S )Nzcannot find loader for this z file)_loadra   r   rw   r[   __dict__)rY   loaderr   images       r   rw   zStubImageFile.load|  sj    >0UCC#,D!   zz|r&   c                    d}t        |      )z (Hook) Find actual image loader.z+StubImageFile subclass must implement _loadr   r   s     r   r   zStubImageFile._load  s    ;!#&&r&   Nr   r   )r   zStubHandler | None)r5   r6   r7   r   rN   rw   r   r*   r&   r   r   r   p  s    '
'r&   r   c                  p    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
ZddZddZddZddZy)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   c                *    | j                   J d       y)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   r^   s    r   resetzParser.reset  s     yy 8"88 r&   c                T   | j                   ry| j                  || _        n| j                  |z   | _        | j                  r| j                  dkD  rmt	        t        | j                        | j                        }| j                  |d | _        | j                  |z
  | _        | j                  dkD  s| j                  sy| j                  j                  | j                        \  }}|dk  r(d| _        d| _         |dk  rd| _        t        |d      y| j                  |d | _        y| j                  ry	 t        j                  | j                        5 }t        j                  |      }ddd       t        d      xs t        |d      }|st        |j                        dk7  rd| _        || _        y|j                          |j                  d   \  }}}	}
g |_        t        j                   |j"                  ||
|j$                        | _        | j                  j'                  |j(                  |       |	| _        | j                  t        | j                        k  r%| j                  | j                  d | _        d| _        || _        y# 1 sw Y   %xY w# t*        $ r Y yw xY w)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%   rm   rl   )finishedr   r   r3   minry   r   r   r    ioBytesIOr
   rG   rz   rB   r   r   rU   rD   r   r   r   )rY   r   skipr   r   rH   r   flagdoas              r   feedzParser.feed  s(    ==99DI		D(DI <<{{Q3tyy>4;;7 IIde,	"kkD0;;?$))<<&&tyy1DAq1u 	 !q5!%DJ&q%88 		!"DIZZ  ZZ		* (bBB(
 r;/K72{3K3rww<1,"&DK  
 OO%!#JAq!Q BG#(#4#4RWWaBDTDT#UDLLL))"%%3 #$DK{{c$))n4$(IIdkkm$<	&'
/( ( s*   6J J+J JJ 	J'&J'c                    | S r]   r*   r^   s    r   	__enter__zParser.__enter__  s    r&   c                $    | j                          y r]   )rX   rY   r4   s     r   __exit__zParser.__exit__  s    

r&   c                ,   | j                   r8| j                  d       dx| _        | _         | j                  sd}t	        |      | j
                  sd}t	        |      | j                  rjt        j                  | j                        5 }	 t        j                  |      | _        | j
                  j                          	 ddd       | j
                  S | j
                  S # | j
                  j                          w xY w# 1 sw Y   | j
                  S xY w)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   r   r   r   r   r   r   r
   rG   rw   )rY   r   rH   s      r   rX   zParser.close  s     <<IIcN'++DI==,cl"zz+C#,99 DII& &"&!&BDJJJOO%	&
 zztzz JJOO%	&
 zzs$   	C?C %C? C<<C??Dr   )r   rM   r   r   )r   r   )r4   objectr   r   )r   r   )r5   r6   r7   r   incrementalr   r8   r   r   r3   r   r   r   r   r   rX   r*   r&   r   r   r     sS    
 K $E$D,<@G9@FH9J Xr&   r   c           	        | j                          t        | d      sd| _        |j                  t               t        t        || j                  d   dz        }	 |j                         }|j                          t        | ||||       t        |d      r|j                          yy# t        t        j                  f$ r}t        | |||d|       Y d}~Ld}~ww xY w)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*   rq   r      Nflush)rw   rz   r   r   r,   maxrE   rV   r   r   _encode_tiler   r   UnsupportedOperation)r   rH   rB   bufsizefhexcs         r   _saver     s     GGI2'II)I
 (GRWWQZ!^4G7YY[

RT7B/ r7

  B334 7RT7D#667s   /B) )CCCc                <   |D ]  \  }}}}	|dkD  r|j                  |       t        j                  | j                  ||	| j                        }
	 |
j                  | j                  |       |
j                  r%|
j                  |       |
j                         d   }nE|r-	 |
j                  |      dd  \  }}|j                  |       |rn,|J |
j                  ||      }|dk  rt        |d      |	 |
j                           y # |
j                          w xY w)Nr   r	   Tr%   )r}   r
   _getencoderrU   r   r   r   	pushes_fdr   encode_to_pyfdencodewriteencode_to_filer    r   )r   rH   rB   r   r   r   encoder_namer1   r3   r4   r   errcoder   s                r   r   r   3  s    04 +gvtA:GGFO##BGG\4AQAQR	RUUG,  b!!00215(/w(?(C"!	  >)>%44RAG{"7D9sB  OO10 OOs   B%D		Dc                   |dk  ry|t         k  r.| j                  |      }t        |      |k  rd}t        |      |S g }|}|dkD  rG| j                  t	        |t                     }|sn%|j                  |       |t        |      z  }|dkD  rGt        d |D              |k  rd}t        |      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              3  2   K   | ]  }t        |        y wr]   )ry   ).0blocks     r   	<genexpr>z_safe_read.<locals>.<genexpr>s  s     
*%3u:
*s   )	SAFEBLOCKr|   ry   r   r   appendsumjoin)rH   rV   r   r   blocksremaining_sizer   s          r   
_safe_readr  V  s     qyywwt}t9t'C#,FN
1
NI67e#e*$ 1
 
*6
**T1#cl88Fr&   c                      e Zd ZddZddZy)PyCodecStatec                <    d| _         d| _        d| _        d| _        y )Nr   )xsizeysizexoffyoffr^   s    r   r?   zPyCodecState.__init__z  s    

		r&   c                    | j                   | j                  | j                   | j                  z   | j                  | j                  z   fS r]   )r  r  r  r  r^   s    r   r1   zPyCodecState.extents  s3    yy$))TYY%;TYY=SSSr&   Nr   )r   ztuple[int, int, int, int])r5   r6   r7   r?   r1   r*   r&   r   r  r  y  s    Tr&   r  c                  N    e Zd ZU ded<   d	dZd
dZddZddZ	 d	 	 	 	 	 ddZy)PyCodeczIO[bytes] | Nonefdc                n    d | _         t               | _        d | _        || _        | j                  |       y r]   )r   r  rg   r  rU   init)rY   rU   r4   s      r   r?   zPyCodec.__init__  s+    15!^
			$r&   c                    || _         y)z
        Override to perform codec specific initialization

        :param args: Tuple of arg items from the tile entry
        :returns: None
        N)r4   r   s     r   r  zPyCodec.init  s     	r&   c                     y)zT
        Override to perform codec specific cleanup

        :returns: None
        Nr*   r^   s    r   r   zPyCodec.cleanup  s     	r&   c                    || _         y)z
        Called from ImageFile to set the Python file-like object

        :param fd: A Python file-like object
        :returns: None
        N)r  )rY   r  s     r   r   zPyCodec.setfd  s     r&   Nc                    || _         |r|\  }}}}nd\  }}}}|dk(  r=|dk(  r8| j                   j                  \  | j                  _        | j                  _        nJ|| j                  _        || j                  _        ||z
  | j                  _        ||z
  | j                  _        | j                  j                  dk  s| j                  j                  dk  rd}t        |      | j                  j                  | j                  j
                  z   | j                   j                  d   kD  sG| j                  j                  | j                  j                  z   | j                   j                  d   kD  rd}t        |      y)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   rV   rg   r  r  r  r  
ValueError)rY   r   r1   x0y0x1y1r   s           r   r   zPyCodec.setimage  s,    &RR+RR7rQw15.DJJdjj. DJJO DJJO!BwDJJ!BwDJJ::q DJJ$4$4$9+CS/! JJtzz.a@zz$**//1DGGLLOC4CS/! Dr&   )rU   r.   r4   r   r   r   )r4   tuple[Any, ...]r   r   r   )r  	IO[bytes]r   r   r]   )r   zImage.core.ImagingCorer1   r0   r   r   )	r5   r6   r7   r8   r?   r  r   r   r   r*   r&   r   r  r    sB     59'""'" 2'" 
	'"r&   r  c                  H    e Zd ZdZdZedd       ZddZ	 d		 	 	 	 	 	 	 d
dZy)	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>`
    Fc                    | j                   S r]   )	_pulls_fdr^   s    r   r   zPyDecoder.pulls_fd  s    ~~r&   c                    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`.
        zunavailable in base decoderr   )rY   bufferr   s      r   r   zPyDecoder.decode       ,!#&&r&   Nc                ^   |s| j                   }t        j                  | j                   d||      }| j                  J |j	                  | j                  | j
                  j                                |j                  |      }|d   dk\  rd}t        |      |d   dk7  rd}t        |      y)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
        rn   Nr   znot enough image datar	   zcannot decode image data)	rU   r
   r   r   r   rg   r1   r   r  )rY   r   rawmodeextrar   r   r   s          r   
set_as_rawzPyDecoder.set_as_raw  s     iiGdii?ww"""	

477DJJ..01HHTNQ419)CS/!Q419,CS/! r&   r   r   )r  z$bytes | Image.SupportsArrayInterfacer   tuple[int, int])Nr*   )r   rM   r"  r   r#  r  r   r   )	r5   r6   r7   r   r  propertyr   r   r$  r*   r&   r   r  r    sQ     I 
' QS""$.">M"	"r&   r  c                  >    e Zd ZdZdZedd       Zd	dZd
dZddZ	y)	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>`
    Fc                    | j                   S r]   )
_pushes_fdr^   s    r   r   zPyEncoder.pushes_fd  s    r&   c                    d}t        |      )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 encoderr   )rY   r   r   s      r   r   zPyEncoder.encode  r   r&   c                    | j                   sy| j                  d      \  }}}|r)| j                  J | 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   )r   r   r  r   )rY   bytes_consumedr   r   s       r   r   zPyEncoder.encode_to_pyfd"  sP     ~~(,A%77&&&GGMM$w&&r&   c                    d}|dk(  r9| j                  |      \  }}}|dkD  rt        j                  |||d        |dk(  r9|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   rI   r   )rY   r   r   r   statusbufs         r   r   zPyEncoder.encode_to_file2  sP     l#';;w#7 FGSzS\* l r&   Nr%  )r   r2   r   ztuple[int, int, bytes])r   r&  )r   r2   r   r2   r   r2   )
r5   r6   r7   r   r+  r'  r   r   r   r   r*   r&   r   r)  r)    s/     J 
'' r&   r)  )r   r2   r   r   r   r   )r   r2   r   r   )r+   r'   r   r2   )r   )
r   r   rH   r  rB   list[_Tile]r   r2   r   r   r]   )r   r   rH   r  rB   r2  r   r2   r   z
int | Noner   zBaseException | Noner   r   )rH   r  rV   r2   r   rM   ))
__future__r   r   r   r   rI   rS   r{   typingr   r   r   r   r   r=   r
   
_deprecater   _utilr   _typingr   rE   r   r   r   r    r"   r,   r'   r:   r   r   r   r   r   r  r  r  r  r)  r*   r&   r   <module>r8     s=  : # 
 	  	  
 ; ;  ! '	  N 	%
-
.J .v$ v$r 'I '<C CRD !%      	 
 	  
  
 F FT TK" K"\3" 3"l8 8r&   