B ¥.¦^ýPã@s¾ddlZddlZddlZddlmZddlmZdZdZdZ dd d d d d œZ dd„Z dd„Z Gdd„dejƒZ Gdd„de ƒZGdd„dƒZd dd„Zdd„ZGdd„dƒZGdd„dƒZdS)!éNé)ÚImage)ÚisPathiiFzimage buffer overrun errorzdecoding errorz unknown errorzbad configurationzout of memory error)éÿÿÿÿéþÿÿÿéýÿÿÿiøÿÿÿi÷ÿÿÿcCsLytj |¡}Wntk r.t |¡}YnX|sŒs°            zImageFile.loadcCsP|jr"|jj|jks"|jj|jkr6tj |j|j¡|_|jdkrLtj |¡dS)NÚP)rFr+r,rrÚnewr>)r/rrrrJs" zImageFile.load_preparecCsdS)Nr)r/rrrrSszImageFile.load_endcCsB||jks.t|dƒr|jdks6||j|jkr6tdƒ‚| ¡|kS)NÚ _n_framesz attempt to seek outside sequence)rr@r_Zn_framesr(Útell)r/ÚframerrrÚ _seek_check&s  zImageFile._seek_check)NN) Ú__name__Ú __module__Ú __qualname__Ú__doc__rr4r5r>rJrSrbÚ __classcell__rr)r1rrMs-  rc@s(eZdZdZdd„Zdd„Zdd„ZdS) Ú 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. cCs tdƒ‚dS)Nz+StubImageFile subclass must implement _open)ÚNotImplementedError)r/rrrr$=szStubImageFile._opencCsH| ¡}|dkrtd|jƒ‚| |¡}|dk s4t‚|j|_|j|_dS)Nz#cannot find loader for this %s file)Ú_loadr r2r>ÚAssertionErrorr1Ú__dict__)r/ÚloaderÚimagerrrr>@s  zStubImageFile.loadcCs tdƒ‚dS)z (Hook) Find actual image loader.z+StubImageFile subclass must implement _loadN)ri)r/rrrrjJszStubImageFile._loadN)rcrdrerfr$r>rjrrrrrh5s rhc@sPeZdZdZdZdZdZdZdZdZ dd„Z dd„Z dd „Z d d „Z d d „ZdS)ÚParserzj Incremental image parser. This class implements the standard feed/close consumer interface. NrcCs|jdkstdƒ‚dS)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. Nzcannot reuse parsers)Údatark)r/rrrÚreset\sz Parser.resetc CsÈ|jr dS|jdkr||_n |j||_|jrÎ|jdkrztt|jƒ|jƒ}|j|d…|_|j||_|jdksv|jszdS|j |j¡\}}|dkr¼d|_d|_|dkr¸d|_t|ƒndS|j|d…|_nö|jrÖnîy&t   |j¡�}t   |¡}WdQRXWnt k �rYn²Xt|dƒ�p(t|dƒ}|�s@t|jƒdk�rHd|_nv| ¡|jd\}}} } g|_t  |j|| |j¡|_|j |j|¡| |_|jt|jƒk�r¾|j|jd…|_d|_||_dS)z¦ (Consumer) Feed data to the parser. :param data: A string buffer. :exception IOError: If the parser failed to parse the image file. NrrrDrB)ÚfinishedrprXrUÚminr?rPrnrÚioÚBytesIOrr r r@rrJrLr+rrMrF) r/rpÚskipr\Úer!rFÚflagÚdÚoÚarrrÚfeeddsR      z Parser.feedcCs|S)Nr)r/rrrÚ __enter__´szParser.__enter__cGs | ¡dS)N)r.)r/rVrrrÚ__exit__·szParser.__exit__c Csz|jr*| d¡d|_|_|js*tdƒ‚|js8tdƒ‚|jrtt |j¡�$}zt  |¡|_Wd|j  ¡XWdQRX|jS)a (Consumer) Close the stream. :returns: An image object. :exception IOError: If the parser failed to parse the image file either because it cannot be identified or cannot be decoded. r=Nzimage was incompletezcannot parse this image) rXr|rprrr rnrtrurr r>)r/r!rrrr.ºs   z Parser.close)rcrdrerfÚ incrementalrnrprXrUrrrqr|r}r~r.rrrrroOsProc CsÞ| ¡t|dƒsd|_|jtd�tt||jddƒ}|tj krP|  ¡dSy|  ¡}|  ¡WnÈt t jfk �r,xª|D]¢\}}}}t |j|||j¡}|dkr´| |¡| |j|¡|jrà| |¡| ¡\} } n&x$| |¡\} } } | | ¡| râPqâW| dk�rtd| ƒ‚| ¡q‚WYnšXx–|D]Ž\}}}}t |j|||j¡}|dk�rh| |¡| |j|¡|j�r–| |¡| ¡\} } n | ||¡} | dk�r¸td| ƒ‚| ¡�q4Wt|dƒ�rÚ|  ¡dS) z«Helper 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)r<réNz(encoder error %d when writing image fileÚflush)r>r@r€rKrÚmaxrr,rAÚstdoutr‚rHr rtÚUnsupportedOperationrZ _getencoderr+rErMrFZ pushes_fdrOZencode_to_pyfdÚencodeÚwriter rRZencode_to_file) rFr!rÚbufsizeÚfhrwrZrzr{Úlr[ryrrrÚ_saveÚsT                 r‹cCsf|dkr dS|tkr| |¡Sg}x8|dkrZ| t|tƒ¡}|sBP| |¡|t|ƒ8}q$Wd |¡S)ao 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 read method. :param size: Number of bytes to read. :returns: A string containing up to size bytes of data. rr=)Ú SAFEBLOCKrCrsÚappendr?Újoin)r!r,rpÚblockrrrÚ _safe_reads    r�c@seZdZdd„Zdd„ZdS)Ú PyCodecStatecCsd|_d|_d|_d|_dS)Nr)ÚxsizeÚysizeÚxoffÚyoff)r/rrrr1szPyCodecState.__init__cCs |j|j|j|j|j|jfS)N)r”r•r’r“)r/rrrrT7szPyCodecState.extentsN)rcrdrerrTrrrrr‘0sr‘c@s\eZdZdZdZdd„Zdd„Zedd„ƒZd d „Z d d „Z d d„Z ddd„Z ddd„Z dS)Ú PyDecoderzÉ Python implementation of a format decoder. Override this class and add the decoding logic in the `decode` method. See :ref:`Writing Your Own File Decoder in Python` FcGs(d|_tƒ|_d|_||_| |¡dS)N)rFr‘ÚstateÚfdr+Úinit)r/r+rVrrrrEs zPyDecoder.__init__cCs ||_dS)zš Override to perform decoder specific initialization :param args: Array of args items from the tile entry :returns: None N)rV)r/rVrrrr™LszPyDecoder.initcCs|jS)N)Ú _pulls_fd)r/rrrrNUszPyDecoder.pulls_fdcCs 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 <0 for the bytes consumed. Err codes are from `ERRORS` N)ri)r/ÚbufferrrrrPYs zPyDecoder.decodecCsdS)zV Override to perform decoder specific cleanup :returns: None Nr)r/rrrrRdszPyDecoder.cleanupcCs ||_dS)z� Called from ImageFile to set the python file-like object :param fd: A python file-like object :returns: None N)r˜)r/r˜rrrrOlszPyDecoder.setfdNcCsÖ||_|r|\}}}}n d\}}}}|dkrJ|dkrJ|jj\|j_|j_n(||j_||j_|||j_|||j_|jjdksŠ|jjdkr’tdƒ‚|jj|jj|jjdksÊ|jj|jj|jjdkrÒtdƒ‚dS)zú Called from ImageFile to set the core output image for the decoder :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)rFr,r—r’r“r”r•Ú ValueError)r/rFrTZx0Zy0Úx1Úy1rrrrMus    zPyDecoder.setimagecCsd|s |j}t |jd|¡}| |j|j ¡¡| |¡}|ddkrLtdƒ‚|ddkr`tdƒ‚dS)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 r7rznot enough image datarzcannot decode image dataN) r+rrLrMrFr—rTrPrœ)r/rpÚrawmoderyr[rrrÚ set_as_raw˜s    zPyDecoder.set_as_raw)N)N)rcrdrerfršrr™ÚpropertyrNrPrRrOrMr rrrrr–;s   #r–)r)rtr)rAÚrZ_utilrrrŒrQr rrrrhror‹r�r‘r–rrrrÚs.   i >