
    ga                       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mZm	Z	m
Z
 ddlm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 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-d.dZd  Z G d! d"      Z  G d# d$      Z! G d% d&e!      Z" G d' d(e!      Z#y)/    )annotationsN)IOAny
NamedTuple   )Image)	deprecate)is_pathi   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      B/var/www/openai/venv/lib/python3.12/site-packages/PIL/ImageFile.py_get_oserrorr   D   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   O   s%    
G ue
,,    c                    | d   S )N    )ts    r   	_tilesortr(   Y   s    Q4Kr#   c                  6    e Zd ZU ded<   ded<   ded<   ded<   y	)
_Tilestr
codec_nametuple[int, int, int, int]extentsintoffsetztuple[Any, ...] | str | NoneargsN)__name__
__module____qualname____annotations__r&   r#   r   r*   r*   ^   s    O&&K
&&r#   r*   c                  Z     e Zd ZdZd
 fd	ZddZ fdZddZd ZddZ	ddZ
d	 Z xZS )	ImageFilez*Base class for image file format handlers.c                ~   t         |           d| _        d | _        d | _        	 d| _        d| _        t        | _        t        |      r t        |d      | _        || _        d| _        n|| _        || _        d | _        	 	 | j                          | j,                  r$| j.                  d   dk  s| j.                  d   dk  rd}t+        |      y # t        t         t"        t$        t&        j(                  f$ r}t+        |      |d }~ww xY w# t0        $ r( | j                  r| j                  j3                           w xY w)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)selfrD   rE   vr   	__class__s        r   r;   zImageFile.__init__m   s'   #	7'2;2tnDGDM!%D DG$DM!%D		,

 99		! 1TYYq\Q5F5!#&& 6G  , "!n!+,  	!!		s*   C =D (D7DDD 1D<c                    | j                   r| j                   S | j                  7t        j                  j	                  | j                  j                               S y N)r=   formatr   MIMEr   upperrR   s    r   get_format_mimetypezImageFile.get_format_mimetype   sD    ''';;"::>>$++"3"3"566r#   c                2    g | _         t        | 	  |       y rV   )r>   r:   __setstate__)rR   staterT   s     r   r]   zImageFile.__setstate__   s    	U#r#   c                ^    | j                   r| j                  j                          d| _        y)zCheck file integrityN)rF   rD   rQ   rZ   s    r   verifyzImageFile.verify   s!    
 GGMMOr#   c                D
   | j                   d}t        |      t        j                  j                  |       }| j                   s|S d| _        | j
                  xr t        | j                         dk(  }|xr t        t        d       }d}	 | j                  }d}	 | j                  }d}|rU| j                   d   \  }}}	}
t        |
t               r|
ddf}
|dk(  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       |	| j.                  d   |
d   z  z   | j                  j/                         kD  rd
}t        |      t        j0                  j3                  | j                  | j.                  ||	|
      | _        d}| j6                  rd| j6                  _        | j=                          d}| j                  s| j                   j?                  t@               	 | jB                  }tE        jF                  | j                   d       D cg c]  \  }}tI        |      d    c}}| _         | j                   D ]  \  }}}	}
 ||	       t        jJ                  | j"                  ||
| jL                        }	 |jO                  | j4                  |       |jP                  r0|jS                  | j                         |jU                  d      d   }n^|}	 	  || jV                        }|s!t^        rn?dt        |       d}t        |      ||z   }|jU                  |      \  }}|dk  rn||d }[|ja                           g | _         || _1        | je                          | jf                  r&| jh                  r| j                  jk                          d| _        | j                  st^        s|dk  rtm        |d      t        j                  j                  |       S # t        $ r | j                  j                  }Y w xY w# t        $ r | j                  j                  }Y w xY w# 1 sw Y   /xY w# t        t        t:        f$ r d| _        Y w xY w# t        $ r d}Y {w xY wc c}}w # tX        tZ        j\                  f$ r}t^        rY d}~ed}t        |      |d}~ww xY w# |ja                          w xY w)z"Load image data based on tile listNzcannot load this imager   pypy_version_infor   Fraw   )accesszbuffer is not large enoughr   keyr#   c                    | d   | d   | d   fS )Nr   r   rd   r&   )r>   s    r   <lambda>z ImageFile.load.<locals>.<lambda>   s    T!Wd1gtAw,Gr#   r   zimage file is truncatedzimage file is truncated (z bytes not processed)r"   )7r>   r   r   loadmaprE   lenhasattrsys	load_readr   rD   read	load_seekseek
isinstancer+   rN   	_MAPMODESmmaprC   filenoACCESS_READrO   r   
map_bufferimpalettedirtyImportErrorload_preparesortr(   tile_prefix	itertoolsgroupbylist_getdecoderr@   setimagepulls_fdsetfddecoderB   rH   rL   r   LOAD_TRUNCATED_IMAGEScleanupr?   load_endrF   !_close_exclusive_fp_after_loadingrQ   r   )rR   r   pixeluse_mmapr?   rp   rr   decoder_namer.   r0   r1   ru   rD   err_codeprefix_tilesr   bsens                         r   rj   zImageFile.load   s    99*C#,  &yyL==8S^q%8EGC1D$E E	 >>DH	 >>DH 26))A,/L'64$$a|%INGtyy(Gu.$dmm,#'99RYY[!DDTDT9#U -		!tAw 66H:%cl*#jj33$))\64DG  !H ||-.* 	xxIINNyN))) !* 1 1IIG!!HAu 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 8AT 	 $"H"HGGMMOxx 5(Q,x77{{%%e  	 77<<D	   	 77<<D	 $ -, '= $#DH$ " * %/#= >#8$)*CC*1#,A$=>2 OO%s   P? Q% R ,2RB#R R: 1SAT8S
AT?Q"!Q"%RRRR R76R7:S	S	T
+T1T7TT

TTc                x   | j                   rF| j                   j                  | j                  k7  s#| j                   j                  | j                  k7  r9t        j                  j                  | j                  | j                        | _         | j                  dk(  r t        j                  j                  |        y y )NP)ry   rN   rO   r   r   newrj   rZ   s    r   r}   zImageFile.load_prepare<  sp    ww$'',,$))3tww||tyy7PjjnnTYY		:DG99KKT" r#   c                     y rV   r&   rZ   s    r   r   zImageFile.load_endD      r#   c                    || j                   k  s4t        | d      r| j                  )|| j                  | j                   z   k\  rd}t	        |      | j                         |k7  S )N	_n_framesz attempt to seek outside sequence)r<   rm   r   n_framesrK   tell)rR   framer   s      r   _seek_checkzImageFile._seek_checkP  sZ    DOO# T;/DNN4JT]]T__<< 5C3-yy{e##r#   )NN)returnz
str | Noner   None)r2   r3   r4   __doc__r;   r[   r]   r`   rj   r}   r   r   __classcell__)rT   s   @r   r7   r7   j   s1    4.`$I&V#$r#   r7   c                  :    e Zd ZddZej
                  dd       Zy)StubHandlerc                     y rV   r&   rR   ry   s     r   rC   zStubHandler.opena  s    r#   c                     y rV   r&   r   s     r   rj   zStubHandler.loadd  r   r#   N)ry   StubImageFiler   r   )ry   r   r   zImage.Image)r2   r3   r4   rC   abcabstractmethodrj   r&   r#   r   r   r   `  s!     	 r#   r   c                  &    e Zd ZdZddZ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rR   r   s     r   rG   zStubImageFile._openq  s    ;!#&&r#   c                    | j                         }|d| j                   d}t        |      |j                  |       }|J |j                  | _        |j
                  | _        |j                         S )Nzcannot find loader for this z file)_loadrW   r   rj   rT   __dict__)rR   loaderr   images       r   rj   zStubImageFile.loadu  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   zStubHandler | None)r2   r3   r4   r   rG   rj   r   r&   r#   r   r   r   i  s    '
'r#   r   c                  V    e Zd ZU dZdZdZded<   dZdZdZ	dZ
ddZd Zd Zdd	Zd
 Zy)Parserzj
    Incremental image parser.  This class implements the standard
    feed/close consumer interface.
    NzImage.Image | 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)datarZ   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"   rq   ro   )finishedr   r   r0   minrl   r   r   r   ioBytesIOr   rC   rm   r>   r}   r   rN   r@   r   ry   r   )rR   r   skipr   r   rD   ry   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 rV   r&   rZ   s    r   	__enter__zParser.__enter__  s    r#   c                $    | j                          y rV   )rQ   rR   r1   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   rC   rj   )rR   r   rD   s      r   rQ   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   )r1   objectr   r   )r2   r3   r4   r   incrementalr   r5   r   r   r0   r   r   r   r   r   rQ   r&   r#   r   r   r     sH    
 K $E$DG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&   rf   r      Nflush)rj   rm   r   r~   r(   maxrA   rO   rv   r   _encode_tiler   r   UnsupportedOperation)ry   rD   r>   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                0   |D ]  \  }}}}	|dkD  r|j                  |       t        j                  | j                  ||	| j                        }
	 |
j                  | j                  |       |
j                  r%|
j                  |       |
j                         d   }nA|r-	 |
j                  |      dd  \  }}|j                  |       |rn,|
j                  ||      }|dk  rt        |d      |	 |
j                           y # |
j                          w xY w)Nr   r   Tr"   )rr   r   _getencoderrN   r   r   ry   	pushes_fdr   encode_to_pyfdencodewriteencode_to_filer   r   )ry   rD   r>   r   r   r   encoder_namer.   r0   r1   r   errcoder   s                r   r   r   ,  s   /3+gvtA:GGFO##BGG\4AQAQR	RUUG,  b!!00215(/w(?(C"!	  &44RAG{"7D9sB  OO/ 04. 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rV   )rl   ).0r   s     r   	<genexpr>z_safe_read.<locals>.<genexpr>d  s     
 4a3q64s   )	SAFEBLOCKrp   rl   r   r   appendsumjoin)rD   rO   r   r   remaining_sizeblocks         r   
_safe_readr   G  s     qyywwt}t9t'C#,DN
1
NI67E#e*$ 1
 
 4
  4'#cl88D>r#   c                      e Zd ZddZddZy)PyCodecStatec                <    d| _         d| _        d| _        d| _        y )Nr   )xsizeysizexoffyoffrZ   s    r   r;   zPyCodecState.__init__k  s    

		r#   c                    | j                   | j                  | j                   | j                  z   | j                  | j                  z   fS rV   )r   r   r   r   rZ   s    r   r.   zPyCodecState.extentsq  s3    yy$))TYY%;TYY=SSSr#   Nr   )r   r-   )r2   r3   r4   r;   r.   r&   r#   r   r   r   j  s    Tr#   r   c                  <    e Zd ZU ded<   d Zd Zd	dZd Zd
ddZy)PyCodeczIO[bytes] | Nonefdc                n    d | _         t               | _        d | _        || _        | j                  |       y rV   )ry   r   r^   r   rN   init)rR   rN   r1   s      r   r;   zPyCodec.__init__x  s+    !^
			$r#   c                    || _         y)z
        Override to perform codec specific initialization

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

        :returns: None
        Nr&   rZ   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   )rR   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)ry   rO   r^   r   r   r   r   
ValueError)rR   ry   r.   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#   r   rV   )r.   z tuple[int, int, int, int] | Noner   r   )	r2   r3   r4   r5   r;   r   r   r   r   r&   r#   r   r   r   u  s!    #"r#   r   c                  8    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 rV   )	_pulls_fdrZ   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   )rR   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)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
        rc   Nr   znot enough image datar   zcannot decode image data)	rN   r   r   ry   r   r^   r.   r   r  )rR   r   rawmoder   r   r   s         r   
set_as_rawzPyDecoder.set_as_raw  s     iiGdii8ww"""	

477DJJ..01HHTNQ419)CS/!Q419,CS/! r#   r   bool)r  bytesr   tuple[int, int]rV   )r   r  r   r   )	r2   r3   r4   r   r
  propertyr   r   r  r&   r#   r   r  r    s*     I 
'"r#   r  c                  <    e Zd ZdZdZedd       Zd	dZd
dZ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 rV   )
_pushes_fdrZ   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   )rR   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   )rR   bytes_consumedr   r   s       r   r   zPyEncoder.encode_to_pyfd  sP     ~~(,A%77&&&GGMM$w&&r#   c                |    d}|dk(  r4| j                  |      \  }}}|dkD  r|j                  ||d        |dk(  r4|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   r   )rR   r   r   r   statusbufs         r   r   zPyEncoder.encode_to_file  sN     l#';;w#7 FGSzVW& l r#   Nr  )r   r/   r   ztuple[int, int, bytes])r   r  )
r2   r3   r4   r   r  r  r   r   r   r   r&   r#   r   r  r    s/     J 
'' r#   r  )r   r/   r   r  r   r   )r   r/   r   r   )r   r   rV   )r>   zlist[_Tile])$
__future__r   r   r   r   rL   rn   typingr   r   r    r   
_deprecater	   _utilr
   rA   r   r   r   r   r   r(   r*   r7   r   r   r   r   r   r   r   r   r  r  r&   r#   r   <module>r%     s   : # 
 	   
 & &  ! 	  N 	%
-
'J 's$ s$l 'I '<C CR86 FT TG" G"T0" 0"f8 8r#   