a :jg!f @sddlmZddlZddlZddlZddlZddlZddlZddlm Z m Z m Z m Z m Z ddlmZddlmZddlmZe rddlmZd Zd Zd Zd d ddddZddddddZdddddZdddddZGddde ZGd d!d!ejZGd"d#d#ZGd$d%d%eZ Gd&d'd'Z!d@d(d)d*dd+d,d-d.Z"dAd(d)d*dd/d0d+d1d2d3Z#d)dd4d5d6d7Z$Gd8d9d9Z%Gd:d;d;Z&Gdd?d?e&Z(dS)B) annotationsN)IO TYPE_CHECKINGAny NamedTuplecast)Image) deprecate)is_path)StrOrBytesPathiiFzimage buffer overrun errorzdecoding errorz unknown errorzbad configurationzout of memory error)iintboolOSError)errorencoderreturncCshztj|}Wnty,t|}Yn0|sH|r:dndd|}|d|rTdndd7}t|S)Nrdecoderz error z when writingreadingz image file)r coreZgetcodecstatusAttributeErrorERRORSgetr)rrmsgr7/usr/local/lib/python3.9/site-packages/PIL/ImageFile.py _get_oserrorHs r!)rrcCstddddt|dddS)N raise_oserror zIt is only useful for translating error codes returned by a codec's decode() method, which ImageFile already does automatically.)actionFr)r r!)rrrr r"Ss r"_Tile)trcCs|dS)Nr)r'rrr _tilesort]sr)c@s.eZdZUded<ded<ded<ded<d S) r&strZ codec_name tuple[int, int, int, int] | Noneextentsroffsetztuple[Any, ...] | str | NoneargsN)__name__ __module__ __qualname____annotations__rrrr r&bs cseZdZdZd!ddddfdd Zdd d d Zd d d dZdddfdd Zdd ddZdd ddZ dd ddZ dd ddZ ddddd Z Z S)" ImageFilez*Base class for image file format handlers.NzStrOrBytesPath | IO[bytes]zstr | bytes | NoneNone)fpfilenamerc s.td|_d|_g|_d|_d|_t|_t |r^t |d|_ t j t ||_d|_n(ttt||_ |durz|nd|_d|_zzz |Wn:tttttjfy}zt||WYd}~n d}~00|jr|jddks|jddkrd}t|Wn(ty(|jr"|j Yn0dS) NrrrrbTFznot identified by this driver)!super__init__ _min_framecustom_mimetypetilereadonly decoderconfigMAXBLOCKdecodermaxblockr openr5ospathrealpathfspathr6 _exclusive_fprrbytes_open IndexError TypeErrorKeyErrorEOFErrorstructr SyntaxErrormodesize BaseExceptionclose)selfr5r6vr __class__rr r:qs@     "  zImageFile.__init__rcCsdSNrrTrrr rIszImageFile._open str | NonecCs,|jr |jS|jdur(tj|jSdSrY)r<formatr MIMErupperrZrrr get_format_mimetypes  zImageFile.get_format_mimetypez list[Any])statercsg|_t|dSrY)r=r9 __setstate__)rTr`rVrr raszImageFile.__setstate__cCs|jr|jd|_dS)zCheck file integrityN)rGr5rSrZrrr verifys zImageFile.verifyImage.core.PixelAccess | Nonec Cs|js|jdurd}t|tj|}|js2|Sd|_|joJt|jdk}|oZtt d }d}t|drv|j }d}n|j j }t|dr|j }d}n|j j}|r|jd\}}} } t| tr| ddf} |d krt| trt| d kr| d|jkr| dtjvrzddl} t|j(} | j| d| jd |_Wdn1sP0Y| |jd| d|jkrd }t|tj|j|j|| | |_d}|jrd|j_Wn tttfyd|_Yn0| d } |jsh|jj!t"dt#|dd}ddt$%|jddD|_|jD]8\}}} } || t&|j|| |j'}z|(|j||j)r|*|j |+dd} n|}z||j,}WnPt-t.j/fy}z0t0rWYd}~qNnd}t||WYd}~n d}~00|st0rqNndt|d}t|||}|+|\}} |dkr>qN||d}qW|1n |10q,g|_||_2|3|j4r|j5r|j 6d|_ |jst0s| dkrt7| ddtj|S)z"Load image data based on tile listNzcannot load this imagerpypy_version_infor load_readF load_seekraw)accesszbuffer is not large enoughrkeyZ tile_prefixcSsg|]\}}t|dqS)r )list).0_Ztilesrrr sz"ImageFile.load..cSs|d|d|dfS)Nrrrhr)r=rrr rlz ImageFile.load..zimage file is truncatedzimage file is truncated (z bytes not processed)r%)8r=_imrr loadmapr6lenhasattrsysrer5readrfseek isinstancer*tuplerPZ _MAPMODESmmaprBfileno ACCESS_READrQrZ map_bufferimpaletteZdirtyr ImportError load_preparesortr)getattr itertoolsgroupby _getdecoderr?setimagepulls_fdsetfddecoderArJrNrLOAD_TRUNCATED_IMAGEScleanupr>load_endrGZ!_close_exclusive_fp_after_loadingrSr!)rTrZpixelZuse_mmapr>rxryZ decoder_namer,r-r.r|r5Zerr_codeprefixrbsenrrr rss         8"          zImageFile.loadcCs8|jdurtj|j|j|_|jdkr4tj|dS)NP)rrr rnewrPrQrrsrZrrr rCs  zImageFile.load_preparecCsdSrYrrZrrr rKszImageFile.load_endrr)framercCsJ||jks2t|dr|jdus>|t|d|jkr>d}t|||kS)N _n_framesZn_framesz attempt to seek outside sequence)r;rvrrrMtell)rTrrrrr _seek_checkWs zImageFile._seek_check)N)r/r0r1__doc__r:rIr_rarbrsrrr __classcell__rrrVr r3ns2   r3c@s2eZdZdddddZejdddddZd S) StubHandler StubImageFiler4)rrcCsdSrYrrTrrrr rBhszStubHandler.open Image.ImagecCsdSrYrrrrr rskszStubHandler.loadN)r/r0r1rBabcabstractmethodrsrrrr rgsrc@s:eZdZdZddddZddddZd dd d Zd S) rz 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. r4rXcCsd}t|dS)Nz+StubImageFile subclass must implement _openNotImplementedErrorrTrrrr rIxszStubImageFile._openrccCsT|}|dur&d|jd}t|||}|dusread method. :param size: Number of bytes to read. :returns: A string containing size bytes of data. Raises an OSError if the file is truncated and the read cannot be completed rrlzTruncated File Readcss|]}t|VqdSrY)ru)rnblockrrr srlz_safe_read..) SAFEBLOCKrxrurrappendsumjoin)r5rQrrblocksZremaining_sizerrrr _safe_readVs(    rc@s(eZdZddddZddddZdS) PyCodecStater4rXcCsd|_d|_d|_d|_dS)Nr)xsizeysizexoffyoffrZrrr r:zszPyCodecState.__init__ztuple[int, int, int, int]cCs |j|j|j|j|j|jfSrY)rrrrrZrrr r,szPyCodecState.extentsN)r/r0r1r:r,rrrr rysrc@sjeZdZUded<ddddddZd dd d d Zdd ddZdddddZdddddddZdS)PyCodeczIO[bytes] | Nonefdr*rr4)rPr.rcGs(d|_t|_d|_||_||dSrY)rrr`rrPinit)rTrPr.rrr r:s zPyCodec.__init__tuple[Any, ...]rcCs ||_dS)z Override to perform codec specific initialization :param args: Tuple of arg items from the tile entry :returns: None N)r.rrrr rsz PyCodec.initrXcCsdS)zT Override to perform codec specific cleanup :returns: None NrrZrrr rszPyCodec.cleanupr)rrcCs ||_dS)z Called from ImageFile to set the Python file-like object :param fd: A Python file-like object :returns: None N)r)rTrrrr rsz PyCodec.setfdNzImage.core.ImagingCorer+)rr,rcCs||_|r|\}}}}n d\}}}}|dkrJ|dkrJ|jj\|j_|j_n(||j_||j_|||j_|||j_|jjdks|jjdkrd}t||jj|jj|jjdks|jj|jj|jjdkrd}t|dS)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 )rrrrrzSize cannot be negativerz Tile cannot extend outside imageN)rrQr`rrrr ValueError)rTrr,Zx0Zy0x1y1rrrr rs&   zPyCodec.setimage)N) r/r0r1r2r:rrrrrrrr rs   rc@sLeZdZdZdZeddddZddd d d ZddddddddZd 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` FrrXcCs|jSrY) _pulls_fdrZrrr rszPyDecoder.pulls_fdz$bytes | Image.SupportsArrayInterfacetuple[int, int])bufferrcCsd}t|dS)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`. zunavailable in base decoderNr)rTrrrrr rs zPyDecoder.decodeNrrHr[rr4)rrawmodeextrarcCs||s |j}t|jd||}|jdus*J||j|j||}|ddkr`d}t||ddkrxd}t|dS)aS Convenience method to set the internal image from a stream of raw data :param data: Bytes to be set :param rawmode: The rawmode to be used for the decoder. If not specified, it will default to the mode of the image :param extra: Extra arguments for the decoder. :returns: None rgNrznot enough image datarzcannot decode image data) rPr rrrr`r,rr)rTrrrrrrrrr set_as_raws    zPyDecoder.set_as_raw)Nr) r/r0r1rrpropertyrrrrrrr rs rc@sVeZdZdZdZeddddZddd d d Zd dd dZddddddZ dS) 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` FrrXcCs|jSrY) _pushes_fdrZrrr rszPyEncoder.pushes_fdrztuple[int, int, bytes])rrcCsd}t|dS)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`. zunavailable in base encoderNr)rTrrrrr rs zPyEncoder.encodercCs@|js dS|d\}}}|r8|jdus,J|j|||fS)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`. )rrrN)rrrr)rTZbytes_consumedrrrrr r"s zPyEncoder.encode_to_pyfd)rrrcCs>d}|dkr:||\}}}|dkrt|||dq|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`. rN)rrCr)rTrrrstatusbufrrr r2s zPyEncoder.encode_to_fileN) r/r0r1rrrrrrrrrrr rs r)r)N)) __future__rrrrrCrNrwtypingrrrrrr8r Z _deprecater Z_utilr Z_typingr r@rrrr!r"r)r&r3rrrrrrrrrrrrrr sN        z  "## N6