
    th[                     H   d dl Z d dlZd dlZd dl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 G d dej                  Z G d de          Z G d d          Zd!dZ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 )#    N   )Image)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d|  }|dz  }t          |          )Nzdecoder error z when reading image file)r   coregetcodecstatusAttributeErrorERRORSgetOSError)errormsgs     a/var/www/html/mycamper/aliexpress-site/backend/venv/lib/python3.11/site-packages/PIL/ImageFile.pyraise_oserrorr   @   sz     j''..      jj  '&u&&%%C
#,,s   " $A	A	c                     | d         S )N    )ts    r   	_tilesortr   K   s    Q4K    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)selfr)   r*   vr   	__class__s        r   r    zImageFile.__init__X   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r7   s    r   get_format_mimetypezImageFile.get_format_mimetype   sH     	('';":>>$+"3"3"5"5666 #"r   c                 X    g | _         t                                          |           d S r;   )r#   r   __setstate__)r7   stater9   s     r   rB   zImageFile.__setstate__   s(    	U#####r   c                 T    | j         r| j                                         d| _        dS )zCheck file integrityN)r+   r)   r6   r?   s    r   verifyzImageFile.verify   s)    
  	GMMOOOr   c                 l	   | 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         \  }}}	}
|dk    rGt          |
          dk    r3|
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t          t          j                            | j        | j        ||	|
          | _        d}| j        rd| j        _        n$# t          t          t6          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 tA          j!        | j         d           D             | _         | j         D ]f\  }}}	}
 ||	           t          j"        | j        ||
| j#                  }	 |$                    | j        |           |j%        r6|&                    | j                   |'                    d          d         }n|}	 	  || j(                  }n<# tR          tT          j+        f$ r#}tX          rY d}~nud}t          |          |d}~ww xY w|s*tX          rnQdt          |           d}t          |          ||z   }|'                    |          \  }}|dk     rn||d         }|-                                 O# |-                                 w xY wg | _         || _.        | /                                 | j0        r | j1        r| j        2                                 d| _        | j        stX          s|dk     rtg          |           t          j                            |           S )z"Load image data based on tile listNzcannot load this imager   pypy_version_infor   Fraw   )accessr   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   rI   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))4r#   r   r   loadmapr*   lenhasattrsys	load_readr   r)   read	load_seekseekr3   	_MAPMODESmmapr(   filenoACCESS_READr4   r   
map_bufferimpalettedirtyImportErrorload_preparesortr   tile_prefix	itertoolsgroupby_getdecoderr%   setimagepulls_fdsetfddecoder'   r-   r1   r   LOAD_TRUNCATED_IMAGEScleanupr$   load_endr+   !_close_exclusive_fp_after_loadingr6   r   )r7   r   pixeluse_mmapr$   r[   r]   decoder_nameextentsoffsetargsr_   r)   err_codeprefixdecoderbsens                       r   rU   zImageFile.load   sw    9*C#,,  &&y 	L=8S^^q%8EGC1D$E$E E	 >DHH 	  	  	 7<DDD	 	 >DHH 	  	  	 7<DDD	   	$26)A,/L'64%%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%#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 	$ 5 	$(Q,,(###{%%%s   	B B%$B%)	B3 3CC'H ?5F 4H  FH FBH H/.H/1I9 9JJ1APMPN4N;P NNAPP!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)rc   r3   r4   r   r   newrU   r?   s    r   rg   zImageFile.load_prepare$  sx    w 	;$',$)33tw|ty7P7PjnnTY	::DG9KT""""" r   c                     d S r;   r   r?   s    r   rs   zImageFile.load_end,  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!   rX   r   n_framesr0   tell)r7   framer   s      r   _seek_checkzImageFile._seek_check8  sg    DO## T;//	 $ 59N4JT]T_<<< 5C3--yy{{e##r   )NN)__name__
__module____qualname____doc__r    r@   rB   rE   rU   rg   rs   r   __classcell__)r9   s   @r   r   r   U   s        44. . . . . .`7 7 7$ $ $ $ $  G& G& G&R# # #  $ $ $ $ $ $ $r   r   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r7   r   s     r   r,   zStubImageFile._openP  s    ;!#&&&r   c                     |                                  }|d| j         d}t          |          |                    |           }|J |j        | _        |j        | _        |                                S )Nzcannot find loader for this z file)_loadr<   r   rU   r9   __dict__)r7   loaderr   images       r   rU   zStubImageFile.loadT  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._load`  s    ;!#&&&r   N)r   r   r   r   r,   rU   r   r   r   r   r   r   H  sK         ' ' '
 
 
' ' ' ' 'r   r   c                   H    e Zd ZdZdZdZ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.
    Nr   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.resets  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          |           n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   r\   rZ   )finishedr   r}   ry   minrW   rp   r   r   ioBytesIOr   r(   rX   r#   rg   rl   r3   r%   rm   rc   r   )r7   r   skipr   r   r)   rc   flagdoas              r   feedzParser.feed{  s    = 	F9DII	D(DI < 9	 {Q3ty>>4;77 Idee,	"kD0;??$)?F<&&ty11DAq1uu 	 !q55!%DJ!!$$$$ 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 r;   r   r?   s    r   	__enter__zParser.__enter__  s    r   c                 .    |                                   d S r;   )r6   r7   rz   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   r(   rU   )r7   r   r)   s      r   r6   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   r   r   r   incrementalr   r   r}   ry   r   r   r   r   r   r6   r   r   r   r   r   f  s         
 KEDGFH9 9 9K  K  K Z        r   r   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   rK   r      Nflush)rU   rX   r   rh   r   maxr&   r4   r`   r   _encode_tiler   r   UnsupportedOperation)rc   r)   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c                 v   |D ]4\  }}}}	|dk    r|                     |           t          j        | j        ||	| j                  }
	 |
                    | j        |           |
j        r0|
                    |           |
	                                d         }nS|r;	 |

                    |          dd          \  }}|                    |           |rn9n|
                    ||          }|dk     rd| d}t          |          |	 |
                                 # |
                                 w xY wd S )Nr   r   Tzencoder error z when writing image file)r]   r   _getencoderr3   r   rm   rc   	pushes_fdro   encode_to_pyfdencodewriteencode_to_filer   rr   )rc   r)   r#   r   r   r   r   r~   r   r   encodererrcoder   r   s                 r   r   r     sg     
1aq55GGAJJJ#BGQ23CDD	RUA&&&  Bb!!!!002215 	B"(/w(?(?(C" "!	" " &44RAAG{{HwHHHcll+  OOGOO1 s   CD  D6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 r;   )rW   )rO   r   s     r   	<genexpr>z_safe_read.<locals>.<genexpr>E  s(      
 
 a3q66
 
 
 
 
 
 r   )	SAFEBLOCKr[   rW   r   r   appendsumjoin)r)   r4   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yoffr?   s    r   r    zPyCodecState.__init__L  s"    

				r   c                 V    | j         | j        | j         | j        z   | j        | j        z   fS r;   )r   r   r   r   r?   s    r   rx   zPyCodecState.extentsR  s'    y$)TY%;TY=SSSr   N)r   r   r   r    rx   r   r   r   r   r   K  s7          T T T T Tr   r   c                   .    e Zd Zd Zd Zd Zd ZddZdS )PyCodecc                     d | _         t                      | _        d | _        || _        |                     |           d S r;   )rc   r   rC   fdr3   init)r7   r3   rz   s      r   r    zPyCodec.__init__W  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)rz   r   s     r   r   zPyCodec.init^  s     			r   c                     dS )zT
        Override to perform codec specific cleanup

        :returns: None
        Nr   r?   s    r   rr   zPyCodec.cleanupg  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   )r7   r   s     r   ro   zPyCodec.setfdo  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)rc   r4   rC   r   r   r   r   
ValueError)r7   rc   rx   x0y0x1y1r   s           r   rm   zPyCodec.setimagex  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;   )r   r   r   r    r   rr   ro   rm   r   r   r   r   r   V  sd                #" #" #" #" #" #"r   r   c                   :    e Zd ZdZdZed             Zd Z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 r;   )	_pulls_fdr?   s    r   rn   zPyDecoder.pulls_fd  s
    ~r   c                     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`.
        r   )r7   buffers     r   rp   zPyDecoder.decode       "###r   Nc                 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
        rH   r   znot enough image datar   zcannot decode image dataN)	r3   r   rl   rm   rc   rC   rx   rp   r   )r7   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   r;   )	r   r   r   r   r   propertyrn   rp   r   r   r   r   r   r     sa          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 r;   )
_pushes_fdr?   s    r   r   zPyEncoder.pushes_fd  s
    r   c                     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`.
        r   )r7   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   )r7   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   )r7   r   r   r   statusbufs         r   r   zPyEncoder.encode_to_file  s_     ll#';;w#7#7 FGSzzVWW&&& ll r   N)
r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     sk          J  X	$ 	$ 	$' ' '    r   r   )r   r;   )r   rj   r1   rY    r   _utilr   r&   r   rq   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>r      s-  < 
			      



            	  N 	%
 
    p$ p$ p$ p$ p$ p$ p$ p$f' ' ' ' 'I ' ' '<D D D D D D D DT   8   8     FT T T T T T T TE" E" E" E" E" E" E" E"P." ." ." ." ." ." ." ."b6 6 6 6 6 6 6 6 6 6r   