
     fa                       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	 d2dZd3d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d4d5d%Zd6d7d(Zd) Z G d* d+          Z  G d, d-          Z! G d. d/e!          Z" G d0 d1e!          Z#dS )8    )annotationsN)IOAny
NamedTuple   )Image)	deprecate)is_pathi   i   Fzimage buffer overrun errorzdecoding errorzunknown errorzbad configurationzout of memory error)ierrorintencoderboolreturnOSErrorc                   	 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 )Nr   decoderz error z when writingreadingz image file)r   coregetcodecstatusAttributeErrorERRORSgetr   )r   r   msgs      P/var/www/api.educacionweb.es/myenv/lib/python3.11/site-packages/PIL/ImageFile.py_get_oserrorr    D   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"   O   s:    
G    ue
,
,
,,    c                    | d         S )N    )ts    r   	_tilesortr+   Y   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_nametuple[int, int, int, int]extentsr   offsetztuple[Any, ...] | str | NoneargsN)__name__
__module____qualname____annotations__r)   r&   r   r-   r-   ^   s<         OOO&&&&KKK&&&&&&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.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)selfrF   rG   vr   	__class__s        r   r=   zImageFile.__init__m   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r   
str | Nonec                    | j         r| j         S | j        6t          j                            | j                                                  S d S N)r?   formatr   MIMEr   upperrT   s    r   get_format_mimetypezImageFile.get_format_mimetype   sE     	('';":>>$+"3"3"5"5666tr&   c                X    g | _         t                                          |           d S rY   )r@   r<   __setstate__)rT   staterV   s     r   r`   zImageFile.__setstate__   s(    	U#####r&   Nonec                T    | j         r| j                                         d| _        dS )zCheck file integrityN)rH   rF   rS   r]   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   rh   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maprG   lenhasattrsys	load_readr   rF   read	load_seekseek
isinstancer.   rP   	_MAPMODESmmaprE   filenoACCESS_READrQ   r   
map_bufferimpalettedirtyImportErrorload_preparesortr+   tile_prefix	itertoolsgroupby_getdecoderrB   setimagepulls_fdsetfddecoderD   rJ   rN   r   LOAD_TRUNCATED_IMAGEScleanuprA   load_endrH   !_close_exclusive_fp_after_loadingrS   r    )rT   r   pixeluse_mmaprA   rz   r|   decoder_namer1   r2   r3   r   rF   err_codeprefixr   bsens                       r   rt   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   rP   rQ   r   r   newrt   r]   s    r   r   zImageFile.load_prepare<  sx    w 	;$',$)33tw|ty7P7PjnnTY	::DG9KT""""" r&   c                    d S rY   r)   r]   s    r   r   zImageFile.load_endD      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>   rw   r   n_framesrM   tell)rT   framer   s      r   _seek_checkzImageFile._seek_checkP  sg    DO## T;//	 $ 59N4JT]T_<<< 5C3--yy{{e##r&   )NN)r   rW   r   rb   )r4   r5   r6   __doc__r=   r^   r`   rd   rt   r   r   r   __classcell__)rV   s   @r   r9   r9   j   s        44. . . . . .`   $ $ $ $ $   I& I& I&V# # # #   $ $ $ $ $ $ $r&   r9   c                  8    e Zd Zd	dZej        d
d            ZdS )StubHandlerr   StubImageFiler   rb   c                    d S rY   r)   rT   r   s     r   rE   zStubHandler.opena  s    r&   Image.Imagec                    d S rY   r)   r   s     r   rt   zStubHandler.loadd  r   r&   N)r   r   r   rb   )r   r   r   r   )r4   r5   r6   rE   abcabstractmethodrt   r)   r&   r   r   r   `  sL            	     r&   r   c                  (    e Zd ZdZd	dZd Zd
dZdS )r   z
    Base class for stub image loaders.

    A stub loader is an image loader that can identify files of a
    certain format, but relies on external code to load the file.
    r   rb   c                $    d}t          |          )Nz+StubImageFile subclass must implement _openNotImplementedErrorrT   r   s     r   rI   zStubImageFile._openq  s    ;!#&&&r&   c                    |                                  }|d| j         d}t          |          |                    |           }|J |j        | _        |j        | _        |                                S )Nzcannot find loader for this z file)_loadrZ   r   rt   rV   __dict__)rT   loaderr   images       r   rt   zStubImageFile.loadu  so    >CCCCC#,,D!!   zz||r&   StubHandler | Nonec                $    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   )r4   r5   r6   r   rI   rt   r   r)   r&   r   r   r   i  sU         ' ' ' '
 
 
' ' ' ' ' 'r&   r   c                  X    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dS )Parserzj
    Incremental image parser.  This class implements the standard
    feed/close consumer interface.
    NzImage.Image | Noner   r   r   rb   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)datar]   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%   r{   ry   )finishedr   r   r2   minrv   r   r   r    ioBytesIOr   rE   rw   r@   r   r   rP   rB   r   r   r   )rT   r   skipr   r   rF   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 rY   r)   r]   s    r   	__enter__zParser.__enter__  s    r&   r3   objectc                .    |                                   d S rY   )rS   rT   r3   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   rE   rt   )rT   r   rF   s      r   rS   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r   )r3   r   r   rb   )r4   r5   r6   r   incrementalr   r7   r   r   r2   r   r   r   r   r   rS   r)   r&   r   r   r     s          
 K $E$$$$DGFH9 9 9 9J  J  J X         r&   r   rb   c           	        |                                   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)   rj   r      Nflush)rt   rw   r   r   r+   maxrC   rQ   r   r   _encode_tiler   r   UnsupportedOperation)r   rF   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%   )r|   r   _getencoderrP   r   r   r   	pushes_fdr   encode_to_pyfdencodewriteencode_to_filer    r   )r   rF   r@   r   r   r   encoder_namer1   r2   r3   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 rY   )rv   )rn   r   s     r   	<genexpr>z_safe_read.<locals>.<genexpr>d  s(      
 
 a3q66
 
 
 
 
 
 r&   )	SAFEBLOCKrz   rv   r   r   appendsumjoin)rF   rQ   r   r   remaining_sizeblocks         r   
_safe_readr   G  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dZddZdS )	PyCodecStater   rb   c                >    d| _         d| _        d| _        d| _        d S )Nr   )xsizeysizexoffyoffr]   s    r   r=   zPyCodecState.__init__k  s"    

				r&   r0   c                V    | j         | j        | j         | j        z   | j        | j        z   fS rY   )r   r   r   r   r]   s    r   r1   zPyCodecState.extentsq  s'    y$)TY%;TY=SSSr&   Nr   )r   r0   )r4   r5   r6   r=   r1   r)   r&   r   r   r   j  sB           T T T T T 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d	S )PyCodeczIO[bytes] | Nonefdc                    d | _         t                      | _        d | _        || _        |                     |           d S rY   )r   r   ra   r  rP   init)rT   rP   r3   s      r   r=   zPyCodec.__init__x  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)r3   r   s     r   r  zPyCodec.init  s     			r&   r   rb   c                    dS )zT
        Override to perform codec specific cleanup

        :returns: None
        Nr)   r]   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  )rT   r  s     r   r   zPyCodec.setfd  s     r&   Nr1    tuple[int, int, int, int] | Nonec                :   || _         |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   rQ   ra   r   r   r   r   
ValueError)rT   r   r1   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&   r   rY   )r1   r  r   rb   )	r4   r5   r6   r7   r=   r  r   r   r   r)   r&   r   r  r  u  s|                  #" #" #" #" #" #" #"r&   r  c                  @    e Zd ZdZdZedd            Zd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>`
    Fr   r   c                    | j         S rY   )	_pulls_fdr]   s    r   r   zPyDecoder.pulls_fd  s
    ~r&   bufferbytestuple[int, int]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   )rT   r  r   s      r   r   zPyDecoder.decode       ,!#&&&r&   Nr   rb   c                b   |s| j         }t          j        | j         d|          }| j        J |                    | 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
        rg   Nr   znot enough image datar   zcannot decode image data)	rP   r   r   r   r   ra   r1   r   r
  )rT   r   rawmoder   r   r   s         r   
set_as_rawzPyDecoder.set_as_raw  s      	 iGdi88w"""	

47DJ..00111HHTNNQ4199)CS//!Q4199,CS//! 9r&   r   r   )r  r  r   r  rY   )r   r  r   rb   )	r4   r5   r6   r   r  propertyr   r   r  r)   r&   r   r  r    sp          I   X
' 
' 
' 
'" " " " " " "r&   r  c                  D    e Zd ZdZdZedd            Zdd	Zd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>`
    Fr   r   c                    | j         S rY   )
_pushes_fdr]   s    r   r   zPyEncoder.pushes_fd  s
    r&   r   r   tuple[int, int, bytes]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   )rT   r   r   s      r   r   zPyEncoder.encode   r  r&   r  c                    | j         sdS |                     d          \  }}}|r#| 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   )rT   bytes_consumedr   r   s       r   r   zPyEncoder.encode_to_pyfd  s^     ~ 	5(,A% 	 7&&&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   )rT   r   r   r   statusbufs         r   r   zPyEncoder.encode_to_file  s_     ll#';;w#7#7 FGSzzVWW&&& ll r&   Nr  )r   r   r   r!  )r   r  )
r4   r5   r6   r   r   r  r   r   r   r   r)   r&   r   r  r    sz          J   X
' 
' 
' 
'' ' ' '     r&   r  )r   r   r   r   r   r   )r   r   r   r   )r   r   rY   )r@   r   )$
__future__r   r   r   r   rN   rx   typingr   r   r    r   
_deprecater	   _utilr
   rC   r   r   r   r    r"   r+   r-   r9   r   r   r   r   r   r   r   r  r  r  r)   r&   r   <module>r-     s  : # " " " " " 



 				      



 & & & & & & & & & &       ! ! ! ! ! !      	  N 	%
 
   - - - -  
' ' ' ' 'J ' ' 's$ s$ s$ s$ s$ s$ s$ s$l       ' ' ' ' '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0" 0" 0" 0" 0" 0" 0" 0"f8 8 8 8 8 8 8 8 8 8r&   