
    }fe_                       d dl m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 Zd Zd Z G d de	          Z G d dej                  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!dS )/    )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                            |           }n*# t          $ r t                              |           }Y nw xY w|s|rdnd d|  }|d|rdnd dz  }t          |          S )Nencoderdecoderz error z when writingreadingz image file)r   coregetcodecstatusAttributeErrorERRORSgetOSError)errorr   msgs      F/var/www/rtfcompare/venv/lib/python3.11/site-packages/PIL/ImageFile.py_get_oserrorr   C   s     j''..      jj  E%49DDUDDD7IIiDDDDC3<<s   " $A	A	c                H    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   N   s:    
G    ue
,
,
,,    c                    | d         S )N    )ts    r   	_tilesortr(   X   s    Q4Kr#   c                  8    e Zd ZU ded<   ded<   ded<   ded<   d	S )
_Tilestr
codec_nameztuple[int, int, int, int]extentsintoffsetztuple[Any, ...] | str | NoneargsN)__name__
__module____qualname____annotations__r&   r#   r   r*   r*   ]   s<         OOO&&&&KKK&&&&&&r#   r*   c                  R     e Zd ZdZd fd	Zd Z fdZd Zd Zd Z	d	 Z
d
 Z xZS )	ImageFilez*Base class for image file format handlers.Nc                   t                                                       d| _        d | _        d | _        	 d| _        d| _        t          | _        t          |          r$t          |d          | _        || _        d| _        n|| _        || _        d | _        	 	 |                                  n@# t          t           t"          t$          t&          j        f$ r}t+          |          |d }~ww xY w| j        r"| j        d         dk    s| j        d         dk    rd}t+          |          d S # t0          $ r" | j        r| j                                          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)selfrC   rD   vr   	__class__s        r   r:   zImageFile.__init__l   sm   #	7'2;; 
	&2tnnDGDM!%D DG$DM!%D		,

 , , , "!nn!+, 9 '	! 1 1TYq\Q5F5F5!#&&& 6G5F  	 	 	!  		s*   B1 0D- 1(C.C))C..=D- -,Ec                    | j         r| j         S | j        6t          j                            | j                                                  S d S N)r<   formatr   MIMEr   upperrQ   s    r   get_format_mimetypezImageFile.get_format_mimetype   sH     	('';":>>$+"3"3"5"5666 #"r#   c                X    g | _         t                                          |           d S rU   )r=   r9   __setstate__)rQ   staterS   s     r   r\   zImageFile.__setstate__   s(    	U#####r#   c                T    | j         r| j                                         d| _        dS )zCheck file integrityN)rE   rC   rP   rY   s    r   verifyzImageFile.verify   s)    
  	GMMOOOr#   c                	   | j         d}t          |          t          j                            |           }| j         s|S d| _        | j        ot          | j                   dk    }|ot          t          d           }d}	 | j	        }d}n# t          $ r | j        j        }Y nw xY w	 | j        }d}n# t          $ r | j        j        }Y nw xY w|r| j         d         \  }}}	}
t          |
t                     r|
ddf}
|dk    rQt          |
          dk    r=|
d         | j        k    r+|
d         t          j        v r	 ddl}t)          | j                  5 }|                    |                                d|j        	          | _        ddd           n# 1 swxY w Y   |	| j        d         |
d         z  z   | j                                        k    rd
}t          |          t          j                            | j        | j        ||	|
          | _        d}| j        rd| j        _        n$# t          t          t:          f$ r
 d| _        Y nw xY w|                                  d}| j        s| j                             t@                     	 | j!        }n# t          $ r d}Y nw xY wd tE          j#        | j         d           D             | _         | j         D ]f\  }}}	}
 ||	           t          j$        | j        ||
| j%                  }	 |&                    | j        |           |j'        r6|(                    | j                   |)                    d          d         }n|}	 	  || j*                  }n<# tV          tX          j-        f$ r#}t\          rY d}~nud}t          |          |d}~ww xY w|s*t\          rnQdt          |           d}t          |          ||z   }|)                    |          \  }}|dk     rn||d         }|/                                 O# |/                                 w xY wg | _         || _0        | 1                                 | j2        r | j3        r| j        4                                 d| _        | j        st\          s|dk     rtk          |d          t          j                            |           S )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                >    g | ]\  }}t          |          d          S )r   )list).0_tiless      r   
<listcomp>z"ImageFile.load.<locals>.<listcomp>   s6       Au UB  r#   c                0    | d         | d         | d         fS )Nr   r   rc   r&   )r=   s    r   <lambda>z ImageFile.load.<locals>.<lambda>   s    T!Wd1gtAw,G r#   Tzimage file is truncatedzimage file is truncated (z bytes not processed)r"   )6r=   r   r   loadmaprD   lenhasattrsys	load_readr   rC   read	load_seekseek
isinstancer+   rM   	_MAPMODESmmaprB   filenoACCESS_READrN   r   
map_bufferimpalettedirtyImportErrorload_preparesortr(   tile_prefix	itertoolsgroupby_getdecoderr?   setimagepulls_fdsetfddecoderA   rG   rK   r   LOAD_TRUNCATED_IMAGEScleanupr>   load_endrE   !_close_exclusive_fp_after_loadingrP   r   )rQ   r   pixeluse_mmapr>   ru   rw   decoder_namer-   r/   r0   rz   rC   err_codeprefixr   bsens                       r   ro   zImageFile.load   s    9*C#,,  &&y 	L=8S^^q%8EGC1D$E$E E	 >DHH 	  	  	 7<DDD	 	 >DHH 	  	  	 7<DDD	   	$26)A,/L'64$$$ $a|%%IINNGty((Gu..$KKKdm,, V#'99RYY[[!DDT9#U#UV V V V V V V V V V V V V V V	!tAw 66HH:%cll*#j33$)\64 DG  !H | /-.*&= $ $ $#DHHH$ 	x 9	&INNyN))))!     ) 1IGG! !  DI 8<y (& (&3gvtV+I|T43E #&$$TWg666' &dg...#*>>##6#6q#9"&>$(D)=$>$>$.#= > > >#8 !>$)EEEE*CC*1#,,A$=> $% 7#8 !7$))J,/FF)J )J )J %( +2#,,$6 !AA*1..*;*;KAx 1uu % !!""A3&8 OO%%%%GOO%%%%	  	$"H 	GMMOOOx 	8 5 	8(Q,,x7777{%%%s   	B B%$B%)	B3 3CCH2 5FH2 FH2 !F"BH2 2IIJ J,+J,AP/1NP/N;N6P/$N66N;;AP//Qc                   | j         r*| j         j        | j        k    s| j         j        | j        k    r/t          j                            | j        | j                  | _         | j        dk    r!t          j                            |            d S d S )NP)r~   rM   rN   r   r   newro   rY   s    r   r   zImageFile.load_prepare:  sx    w 	;$',$)33tw|ty7P7PjnnTY	::DG9KT""""" r#   c                    d S rU   r&   rY   s    r   r   zImageFile.load_endB  s    r#   c                    || j         k     s*t          | d          r| j        $|| j        | j         z   k    rd}t	          |          |                                 |k    S )N	_n_framesz attempt to seek outside sequence)r;   rr   r   n_framesrJ   tell)rQ   framer   s      r   _seek_checkzImageFile._seek_checkN  sg    DO## T;//	 $ 59N4JT]T_<<< 5C3--yy{{e##r#   )NN)r1   r2   r3   __doc__r:   rZ   r\   r_   ro   r   r   r   __classcell__)rS   s   @r   r6   r6   i   s        44. . . . . .`7 7 7$ $ $ $ $  I& I& I&V# # #  $ $ $ $ $ $ $r#   r6   c                  $    e Zd ZdZd Zd Z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                $    d}t          |          )Nz+StubImageFile subclass must implement _openNotImplementedErrorrQ   r   s     r   rF   zStubImageFile._openf  s    ;!#&&&r#   c                    |                                  }|d| j         d}t          |          |                    |           }|J |j        | _        |j        | _        |                                S )Nzcannot find loader for this z file)_loadrV   r   ro   rS   __dict__)rQ   loaderr   images       r   ro   zStubImageFile.loadj  so    >CCC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._loadv  s    ;!#&&&r#   N)r1   r2   r3   r   rF   ro   r   r&   r#   r   r   r   ^  sK         ' ' '
 
 
' ' ' ' 'r#   r   c                  T    e Zd ZU dZdZdZded<   dZdZdZ	dZ
d Zd Zd Zd	 Zd
 ZdS )Parserzj
    Incremental image parser.  This class implements the standard
    feed/close consumer interface.
    NzImage.Image | Noner   r   c                (    | j         
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)datarY   s    r   resetzParser.reset  s!     y  "8     r#   c                   | j         rdS | j        || _        n| j        |z   | _        | j        r| j        dk    r^t	          t          | j                  | j                  }| j        |d         | _        | j        |z
  | _        | j        dk    s| j        sdS | j                            | j                  \  }}|dk     r.d| _        d| _         |dk     rd| _        t          |d          dS | j        |d         | _        dS | j        rdS 	 t          j
        | j                  5 }t          j        |          }ddd           n# 1 swxY w Y   t          |d          pt          |d          }|st          |j                  dk    rd| _        n|                                 |j        d         \  }}}	}
g |_        t          j        |j        ||
|j                  | _        | j                            |j        |           |	| _        | j        t          | j                  k    r | j        | j        d         | _        d| _        || _        dS # t*          $ r Y dS 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"   rv   rt   )finishedr   r   r/   minrq   r   r   r   ioBytesIOr   rB   rr   r=   r   r   rM   r?   r   r~   r   )rQ   r   skipr   r   rC   r~   flagdoas              r   feedzParser.feed  s    = 	F9DII	D(DI < 8	 {Q3ty>>4;77 Idee,	"kD0;??$)?F<&&ty11DAq1uu 	 !q55!%DJ&q%8888 F	!""DIIIZ  	  D Z	** (bBB( ( ( ( ( ( ( ( ( ( ( ( ( ( (
 r;//K72{3K3K (3rw<<1,,"&DKK OO%%%!#JAq!Q BG#(#4RWaBDT#U#UDLL))"%333 #$DK{c$)nn44$(Idkmm$<	&'


+    s6   I 'E<I EI EI 
I#"I#c                    | S rU   r&   rY   s    r   	__enter__zParser.__enter__  s    r#   c                .    |                                   d S rU   )rP   rQ   r0   s     r   __exit__zParser.__exit__  s    

r#   c                   | j         r;|                     d           dx| _        | _         | j        sd}t	          |          | j        sd}t	          |          | j        rt          j        | j                  5 }	 t          j	        |          | _        | j        
                                 n# | j        
                                 w xY w	 ddd           n# 1 swxY w 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.
        r#   Nzimage was incompletezcannot parse this image)r   r   r   r   r   r   r   r   r   rB   ro   )rQ   r   rC   s      r   rP   zParser.close  s    < 	#IIcNNN'++DI= #,cll"z 	+C#,,9 	& DI&& &"&!&BDJJOO%%%%DJOO%%%%%	& & & & & & & & & & & & & & &
 zs*   ;C=B0C0CCC"C)r1   r2   r3   r   incrementalr   r4   r   r   r/   r   r   r   r   r   rP   r&   r#   r   r   r   |  s          
 K $E$$$$DGFH9 9 9J  J  J X        r#   r   returnNonec           	        |                                   t          | d          sd| _        |                    t                     t          t          || j        d         dz            }	 |                                }|	                                 t          | ||||           n7# t          t          j        f$ r}t          | |||d|           Y d}~nd}~ww xY wt          |d          r|	                                 dS 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&   re   r      Nflush)ro   rr   r   r   r(   maxr@   rN   r{   r   _encode_tiler   r   UnsupportedOperation)r~   rC   r=   bufsizefhexcs         r   _saver     s    GGIII2'' II)I
 (GRWQZ!^44G7YY[[



RT7B////B34 7 7 7RT7D#666666667r7 





 s   ,;B( (C>CCr=   list[_Tile]c                n   |D ]0\  }}}}	|dk    r|                     |           t          j        | j        ||	| j                  }
	 |
                    | j        |           |
j        r0|
                    |           |
	                                d         }nS|r;	 |

                    |          dd          \  }}|                    |           |rn9n|
                    ||          }|dk     rt          |d          |	 |
                                 # |
                                 w xY wd S )Nr   r   Tr"   )rw   r   _getencoderrM   r   r   r~   	pushes_fdr   encode_to_pyfdencodewriteencode_to_filer   r   )r~   rC   r=   r   r   r   encoder_namer-   r/   r0   r   errcoder   s                r   r   r   !  s_   /3  +gvtA::GGFOOO#BG\4AQRR	RUG,,,  Bb!!!!002215 	B"(/w(?(?(C" "!	" " &44RAAG{{"7D999sB  OOGOO/ s   B=DD2c                   |dk    rdS |t           k    r;|                     |          }t          |          |k     rd}t          |          |S g }|}|dk    rX|                     t	          |t                               }|sn-|                    |           |t          |          z  }|dk    Xt          d |D                       |k     rd}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   r#   zTruncated File Readc              3  4   K   | ]}t          |          V  d S rU   )rq   )ri   r   s     r   	<genexpr>z_safe_read.<locals>.<genexpr>Y  s(      
 
 a3q66
 
 
 
 
 
 r#   )	SAFEBLOCKru   rq   r   r   appendsumjoin)rC   rN   r   r   remaining_sizeblocks         r   
_safe_readr   <  s    qyysywwt}}t99t'C#,,DN
1

NI6677 	E#e**$ 1

 
 
 4
 
 
   4''#cll88D>>r#   c                      e Zd Zd Zd ZdS )PyCodecStatec                >    d| _         d| _        d| _        d| _        d S )Nr   )xsizeysizexoffyoffrY   s    r   r:   zPyCodecState.__init__`  s"    

				r#   c                V    | j         | j        | j         | j        z   | j        | j        z   fS rU   )r   r   r   r   rY   s    r   r-   zPyCodecState.extentsf  s'    y$)TY%;TY=SSSr#   N)r1   r2   r3   r:   r-   r&   r#   r   r   r   _  s7          T T T T Tr#   r   c                  :    e Zd ZU ded<   d Zd Zd Zd Zd	dZdS )
PyCodeczIO[bytes] | Nonefdc                    d | _         t                      | _        d | _        || _        |                     |           d S rU   )r~   r   r]   r   rM   init)rQ   rM   r0   s      r   r:   zPyCodec.__init__m  s5    !^^
			$r#   c                    || _         dS )z
        Override to perform codec specific initialization

        :param args: Array of args items from the tile entry
        :returns: None
        N)r0   r   s     r   r   zPyCodec.initt  s     			r#   c                    dS )zT
        Override to perform codec specific cleanup

        :returns: None
        Nr&   rY   s    r   r   zPyCodec.cleanup}  s	     	r#   c                    || _         dS )z
        Called from ImageFile to set the Python file-like object

        :param fd: A Python file-like object
        :returns: None
        N)r   )rQ   r   s     r   r   zPyCodec.setfd  s     r#   Nc                :   || _         |r|\  }}}}nd\  }}}}|dk    r*|dk    r$| j         j        \  | j        _        | j        _        n6|| 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         k    s-| j        j        | j        j        z   | j         j        d         k    rd}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~   rN   r]   r   r   r   r   
ValueError)rQ   r~   r-   x0y0x1y1r   s           r   r   zPyCodec.setimage  s     	,&RR+RR77rQww15.DJdj.. DJO DJO!BwDJ!BwDJ:q  DJ$4$9$9+CS//! Jtz.a@@z$*/1DGLOCC4CS//! DCr#   rU   )	r1   r2   r3   r4   r:   r   r   r   r   r&   r#   r   r   r   j  sr                 #" #" #" #" #" #"r#   r   c                  <    e Zd ZdZdZed             Z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 Codec in Python<file-codecs-py>`
    Fc                    | j         S rU   )	_pulls_fdrY   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   )rQ   bufferr   s      r   r   zPyDecoder.decode       ,!#&&&r#   Nr   bytesr   r   c                P   |s| j         }t          j        | j         d|          }|                    | j        | j                                                   |                    |          }|d         dk    rd}t          |          |d         dk    rd}t          |          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
        rb   r   znot enough image datar   zcannot decode image dataN)	rM   r   r   r   r~   r]   r-   r   r   )rQ   r   rawmoder   r   r   s         r   
set_as_rawzPyDecoder.set_as_raw  s      	 iGdi88	

47DJ..00111HHTNNQ4199)CS//!Q4199,CS//! 9r#   rU   )r   r  r   r   )	r1   r2   r3   r   r  propertyr   r   r	  r&   r#   r   r   r     sf          I  X
' 
' 
'" " " " " " "r#   r   c                  >    e Zd ZdZdZed             Zd Zd Z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>`
    Fc                    | j         S rU   )
_pushes_fdrY   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   )rQ   r   r   s      r   r   zPyEncoder.encode  r  r#   c                    | j         sdS |                     d          \  }}}|r| 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   )rQ   bytes_consumedr   r   s       r   r   zPyEncoder.encode_to_pyfd   sP     ~ 	5(,A% 	 GMM$w&&r#   c                    d}|dk    rB|                      |          \  }}}|dk    r|                    ||d                    |dk    B|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   )rQ   r   r   r   statusbufs         r   r   zPyEncoder.encode_to_file  s_     ll#';;w#7#7 FGSzzVWW&&& ll r#   N)
r1   r2   r3   r   r  r
  r   r   r   r   r&   r#   r   r  r    sk          J  X
' 
' 
'' ' '    r#   r  )r   )r   r   rU   )r=   r   )"
__future__r   r   r   rK   rs   typingr   r   r    r   
_deprecater	   _utilr
   r@   r   r   r   r   r   r(   r*   r6   r   r   r   r   r   r   r   r   r  r&   r#   r   <module>r     s  : # " " " " " 				      



 & & & & & & & & & &       ! ! ! ! ! !      	  N 	%
 
  - - -  
' ' ' ' 'J ' ' 'r$ r$ r$ r$ r$ r$ r$ r$j' ' ' ' 'I ' ' '<C C C C C C C CR    8    6     FT T T T T T T TG" G" G" G" G" G" G" G"T/" /" /" /" /" /" /" /"d7 7 7 7 7 7 7 7 7 7r#   