<[c@sddlZddlZddlZddlZddlmZmZmZddlm Z m Z m Z mZmZddlmZdZejeZejdjZdZidCdD6dEdF6dGdH6dIdJ6dKdL6dMdN6dOdP6dQdR6dSdT6dUdV6dWdX6dYdZ6d[d\6d]d^6d_d`6Zejd ZejZd!eZ d"Z!d d#Z"d$e#fd%YZ$d&e%fd'YZ&d(e#fd)YZ'd*e$fd+YZ(d,Z)d-ejfd.YZ*i dad 6dbd06dcd 6ddd6ded 6dfd6dgd6dhd6did6djd6dkd6dld6dmd6Z+d<Z,d=e#fd>YZ-e,d?Z.d@Z/ej0e*j1e*e)ej2e*j1e.ej3e*j1dAej4e*j1dBdS(niNi(tImaget ImageFilet ImagePalette(ti8ti16beti32beto16beto32be(tpy3s0.9s\w\w\w\wsPNG  t1itLsL;2isL;4iitIsI;16BitRGBsRGB;16BtPsP;1isP;2sP;4tLAtRGBAsLA;16BisRGBA;16Bs^**$i@cCs:tj}|j|t}|jr6tdn|S(NsDecompressed Data Too Large(tzlibt decompressobjt decompresstMAX_TEXT_CHUNKtunconsumed_tailt ValueError(tstdobjt plaintext((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyt_safe_zlib_decompressRs   cCstj||d@S(NI(Rtcrc32(tdatatseed((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyt_crc32Zst ChunkStreamcBseeZdZdZdZdZdZdZdZdZ dZ d d Z RS( cCs||_g|_dS(N(tfptqueue(tselfR((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyt__init__cs cCsd}|jr:|jj\}}}|jj|n7|jjd}|d}|jj}t|}t|st j st dt |qn|||fS(s.Fetch a new chunk. Returns header information.iisbroken PNG file (chunk %s)N( tNoneR tpopRtseektreadttellti32tis_cidRtLOAD_TRUNCATED_IMAGESt SyntaxErrortrepr(R!tcidtpostlengthR((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyR&hs     cCs|S(N((R!((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyt __enter__{scGs|jdS(N(tclose(R!targs((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyt__exit__~scCsd|_|_|_dS(N(R#R tcrcR(R!((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyR1scCs|jj|||fdS(N(R tappend(R!R-R.R/((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pytpushscCs9tjd|||t|d|jd||S(s"Call the appropriate chunk handlersSTREAM %r %s %stchunk_tascii(tloggertdebugtgetattrtdecode(R!R-R.R/((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pytcallscCstjr5t|dd?d@r5|j||dSyPt|t|}t|jjd}||krtd|nWn$t j k rtd|nXdS(sRead and verify checksumiiiNis+broken PNG file (bad header checksum in %r)s+broken PNG file (incomplete checksum in %r)( RR*Rtcrc_skipRR(RR&R+tstructterror(R!R-Rtcrc1tcrc2((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyR4s! cCs|jjddS(s3Read checksum. Used if the C module is not presentiN(RR&(R!R-R((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyR>stIENDcCsg}xtry|j\}}}Wn tjk rJtdnX||kr[Pn|j|tj|j||j |q W|S(Nstruncated PNG file( tTrueR&R?R@tIOErrorR4Rt _safe_readRR5(R!tendchunktcidsR-R.R/((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pytverifys  ( t__name__t __module__R"R&R0R3R1R6R=R4R>RI(((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyRas         tiTXtcBseZdZedZRS(sq Subclass of string to allow iTXt chunks to look like strings while keeping their extra information cCs(tj||}||_||_|S(s :param cls: the class to use when creating the instance :param text: value for this key :param lang: language code :param tkey: UTF-8 version of the key name (tstrt__new__tlangttkey(tclsttextRORPR!((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyRNs   (RJRKt__doc__t staticmethodRN(((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyRLstPngInfocBs>eZdZdZdZddedZedZRS(s< PNG chunk container (for use with save(pnginfo=)) cCs g|_dS(N(tchunks(R!((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyR"scCs|jj||fdS(sAppends an arbitrary chunk. Use with caution. :param cid: a byte string, 4 bytes long. :param data: a byte string of the encoded data N(RVR5(R!R-R((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pytaddstcCst|ts$|jdd}nt|tsH|jdd}nt|tsl|jdd}nt|ts|jdd}n|r|jd|d|d|dtj|n(|jd|d|d|d|dS( sAppends an iTXt chunk. :param key: latin-1 encodable text key name :param value: value for this key :param lang: language code :param tkey: UTF-8 version of the key name :param zip: compression flag slatin-1tstrictsutf-8RLsssN(t isinstancetbytestencodeRWRtcompress(R!tkeytvalueRORPtzip((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pytadd_itxts   cCst|tr1|j|||j|jd|St|tsy|jdd}Wqtk r||j||d|SXnt|ts|jdd}n|r|jd|dt j |n|jd|d|dS( sAppends a text chunk. :param key: latin-1 encodable text key name :param value: value for this key, text or an :py:class:`PIL.PngImagePlugin.iTXt` instance :param zip: compression flag R`slatin-1RYtzTXtsttEXtsN( RZRLRaRORPR[R\t UnicodeErrorRWRR](R!R^R_R`((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pytadd_texts " $(RJRKRSR"RWtFalseRaRe(((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyRUs   t PngStreamcBseZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZRS(cCsStj||i|_i|_d|_d|_d|_d|_d|_ dS(Ni(ii( RR"tim_infotim_texttim_sizeR#tim_modetim_tilet im_palettet text_memory(R!R((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyR" s      cCs8|j|7_|jtkr4td|jndS(Ns7Too much memory used in text chunks: %s>MAX_TEXT_MEMORY(RntMAX_TEXT_MEMORYR(R!tchunklen((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pytcheck_text_memory.scCstj|j|}|jd}tjd|| tjdt||t||}|dkrtd|nyt||d}Wn?t k rtj rd}qnt j k rd}nX||jd<|S(NssiCCP profile name %rsCompression method %sis+Unknown compression method %s in iCCP chunkit icc_profile(RRFRtfindR9R:RR+RRR*R#RR@Rh(R!R.R/Rtit comp_methodRr((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyt chunk_iCCP4s$       cCstj|j|}t|t|df|_y4tt|dt|df\|_|_WnnXt|drd|j d%dIicss|]}|dVqdS(gj@N((t.0telt((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pys st chromaticity(RRFRR?tunpacktlenttupleRh(R!R.R/Rtraw_vals((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyt chunk_cHRMs cCs,tj|j|}t||jd<|S(Ntsrgb(RRFRRRh(R!R.R/R((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyt chunk_sRGBscCstj|j|}t|t|d}}t|d}|dkrt|ddt|ddf}||jd}nA|jrsttt|jjdddd}nd}|dkrd}n0|dkrd}n|dkrd}nd }|d krd ||f}qn|jjd t|jjd d |jjdd |jjddf|_ yt |\}}Wn!t k rht d|nX|j t||dt|jdt|jd|ddddddddg}|jjd|jjd} | r(d} | dtj| } ||d| |jdn|jjd} | rdd d!d"g} xe| jD]W\}} ||kr|j||||| q\|| kr\|||| q\q\Wn|jdkr#d|d}|jjd#| }x t||kr|d7}qW||d$|n|jjd%|jjd%d}|sY|dkr|jdkrd|}t|tr||d&|| q~tdtd'|}d(|d}||d&|| q|jd)krtdtd*|}||d&t|q|jd#kr`|\}}}||d&t|t|t|qd%|jkrt d+qnZ|jdkr|jjd,kr|jjd,d-}d|}||d&|| n|jjd.}|r<||d/tt|dd0d1tt|dd0d1d2n|jjd} | rd3d4g}xF| jD]8\}} ||krj|j||||| qjqjWntj|t ||d5d8|jd|fg||d6dt!|d7r|j"ndS(9NR tbitsiiiiiiis%s;%dtoptimizetcompress_levelit compress_typet dictionaryRXscannot write mode %s as PNGtIHDRistcHRMtgAMAtsBITtsRGBttIMERrs ICC ProfilestiCCPtpnginfotsPLTRLRcRbR tPLTERttRNSisR is%cannot use transparency for this modeRtARtpHYsg F%u?g?stbKGDthISTR`RCtflush(ii(#Rt encoderinfoRtmaxRRtgetdataRRft encoderconfigt _OUTMODEStKeyErrorRERRRtsizeRRR]tremoveRVtimt getpaletteR#RZR[to16tgetpalettemodeRRt_saveRthasattrR(RRtfilenameRRtcolorsRRRVticctnameRRtchunks_multiple_allowedR-tpalette_byte_numbert palette_bytesRt alpha_bytestalphatredtgreentblueR((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyRs   2           !     -$      cKsYdtfdY}d}|}z ||_t||d|Wd|`X|jS(s4Return a list of PNG chunks representing this image.t collectorcBs eZgZdZdZRS(cSsdS(N((R!R((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyRIscSs|jj|dS(N(RR5(R!R((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyR5Ls(RJRKRRR5(((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyRFs cWsDdj|}tt|t|}|j|||fdS(NRX(RRRR5(RR-RR4((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyR5OsN(tobjectRRR#R(RtparamsRR5R((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyt getchunksCs   s.pngs image/png(R R (ii(R sL;2(ii(R sL;4(ii(R R (ii(R sI;16B(ii(R R (ii(R sRGB;16B(ii(R sP;1(ii(R sP;2(ii(R sP;4(ii(R R (ii(RR(ii(RsLA;16B(ii(RR(ii(RsRGBA;16B(ii(R s(sL;1s(sL;2s(sL;4s(R s(Rs(sI;16Bs(sP;1s(sP;2s(sP;4s(R s(R s(Rs(5tloggingtreRR?RXRRRt_binaryRRRRR(RRRRt_utilRt __version__t getLoggerRJR9tcompileRR)RRxRt SAFEBLOCKRRoRRRRRMRLRURgRRRRRRRt register_openRt register_savetregister_extensiont register_mime(((s2/tmp/pip-build-SKekGm/pillow/PIL/PngImagePlugin.pyt"sv    (     [M m