<[c@sddlmZmZmZmZmZddlmZmZ m Z m Z ddl Z dZdZdejfdYZid d 6d d 6d d 6Zed Zd ZdZdZdZedZdZdZdZeZdZdZdZdZ dZ!dZ"e#e#dZ$ddfdZ%ej&ej'eeej(ej'eej)ej'eej*ej'dej+ej'ddS( i(tImaget ImageFilet ImagePalettet ImageChopst ImageSequence(ti8ti16leto8to16leiNs0.9cCs|d dkS(NitGIF87atGIF89a(R R ((tprefix((s2/tmp/pip-build-SKekGm/pillow/PIL/GifImagePlugin.pyt_accept&st GifImageFilecBsteZdZdZeZd ZdZdZ e dZ e dZ dZ dZdZd ZRS( tGIFsCompuserve GIFcCs>|jjd}|r:t|r:|jjt|SdS(Ni(tfptreadRtNone(tselfts((s2/tmp/pip-build-SKekGm/pillow/PIL/GifImagePlugin.pytdata6scCs|jjd}|d dkr1tdn|d |jd}xtdt |dD]}}|dt||ko.t||d ko.t||dknst j d|}||_ |_ PqqWn|j|_|jj|_d|_d|_|jddS(Ni iR R snot a GIF filetversionii iiii t backgroundiiitRGB(R R (RRt SyntaxErrortinfoti16t_sizettileRtrangetlenRtrawtglobal_palettetpalettet_GifImageFile__fpttellt_GifImageFile__rewindRt _n_framest _is_animatedt_seek(RRtflagstbitstpti((s2/tmp/pip-build-SKekGm/pillow/PIL/GifImagePlugin.pyt_open<s*#  Q    cCs|jdkrz|j}y(x!trA|j|jdq!WWn$tk ri|jd|_nX|j|n|jS(Ni(R%RR#tTruetseektEOFError(Rtcurrent((s2/tmp/pip-build-SKekGm/pillow/PIL/GifImagePlugin.pytn_framesZs   cCs|jdkr|jdk r3|jdk|_q|j}y|jdt|_Wntk rut|_nX|j|n|jS(Ni(R&RR%R#R.R-R/tFalse(RR0((s2/tmp/pip-build-SKekGm/pillow/PIL/GifImagePlugin.pyt is_animatedfs     cCs|j|sdS||jkr2|jdn|j}x`t|jd|dD]D}y|j|WqVtk r|j|tdqVXqVWdS(Niisno more images in GIF file(t _seek_checkt_GifImageFile__frameR'RR/R.(Rtframet last_frametf((s2/tmp/pip-build-SKekGm/pillow/PIL/GifImagePlugin.pyR.ws !  cCsn|dkrdd|_d|_ddddg|_d|_|jj|jd|_d|_ n|j sz|j n||jdkrt d|n||_g|_ |j|_|jr|jj|jx|jrqWd|_n|jr|j j|j|jnddlm}||j|_i}xtr(|jjd}| st|dkrxPqI|dkr|jjd}|j}t|dkr)t|d}|d@rt|d |d |_nt|jjd} |jj|_d|| | | f|j| | ffg|_ PqIqIWy|j dkrGd|_nU|j dkr~tjjd |j|j d!|_n|j r|j j|_n|jr|j!|j|j|_nWnt"t#fk rnX|j st$nxYd d dddgD]B}||kr,|||j |psR!N(Rt isinstancetbytest bytearraytlistRt itertoolstchaint from_iterabletzipR!RTRJt getpaletteRt _get_optimizet remap_palette(RJR!Rtsource_palettetused_palette_colors((s2/tmp/pip-build-SKekGm/pillow/PIL/GifImagePlugin.pyt_normalize_paletteTs*        cCst|t}t|||j}x't||jD]}|j|q7Wd}t|rm|dB}nt||d|dt|f|_t j ||dd|j dt |j fg|jddS(Nii@iRBs(ii(ii(RoR-R~t encoderinfot_get_global_headertwritet get_interlacet_write_local_headert encoderconfigRt_saveRPRiRT(RJRR!tim_outRR(((s2/tmp/pip-build-SKekGm/pillow/PIL/GifImagePlugin.pyt_write_single_frame|s  cCs|jjdd}|jjdd}g}d}xtj|g|jjdgD]}}xttj|D]c}t|j}t |||j}|jj} t |t t fr||| dO} |sd}n|j dt d t d t | t |t |t dnd |jkrdt |jd kod knr|j dt d t t |jd |jd t dnd|jkrR|jd} |j dt d t ddt dt dt | t dn|jj d} | rt|} t| } | r|dB}|| B}qn|j dt |dt |dt |jdt |jdt || r| r|j t| n|j t ddS(NR<R=i iRiiR;iiR>iiR@i s NETSCAPE2.0iRiRAi(R2RRStintR-RzRtindexRLRRRto16RRt_get_color_table_sizeRPt_get_header_palette(RRJRR(ttransparent_color_existsR<R}R=Rt packed_flagtnumber_of_loopsRt palette_bytestcolor_table_size((s2/tmp/pip-build-SKekGm/pillow/PIL/GifImagePlugin.pyRs`      5)/ 9   A cCsddl}ddlm}m}m}m}|j}t|d} |jdkrt|j d#} |d|gd| d| WdQXndd |g} dg} t|j d@} || d|d| } || d | j d| d| }WdQX| j j | j }|r2||| n|j }|rV||| nWdQXy|j |Wntk rnXdS( Ni(tPopent check_calltPIPEtCalledProcessErrortwbRtppmtogiftstdouttstderrtppmquantt256tstdin(tost subprocessRRRRt_dumptopenRTtdevnullRtclosetwaittunlinktOSError(RJRRRRRRRtfileR8Rt quant_cmdt togif_cmdt quant_proct togif_proctretcode((s2/tmp/pip-build-SKekGm/pillow/PIL/GifImagePlugin.pyt _save_netpbm>s0 " %     cCs|jdkr|r|jddrtp9|jdk}|sX|j|jd krg}x6t|jD]"\}}|rq|j|qqqqW|st|dkrt |t|kr|SqndS( sL Palette optimization is a potentially expensive operation. This function determines if the palette should be optimized using some heuristics, then returns the list of palette entries in use. :param im: Image object :param info: encoderinfo :returns: list of indexes of palette entries in use, or None RCRDRiiiN(RCRDi( RTRt_FORCE_OPTIMIZEtwidththeightt enumeratet histogramRRtmax(RJRtoptimiseR}R+tcount((s2/tmp/pip-build-SKekGm/pillow/PIL/GifImagePlugin.pyRzos ' cCsTddl}t|j|jt|ddd}|dkrPd}n|S(Niiiii(tmathRtceiltlogR(RRR((s2/tmp/pip-build-SKekGm/pillow/PIL/GifImagePlugin.pyRs  /  cCsOt|}d|>t|d}|dkrK|tdd|7}n|S(s Returns the palette, null padded to the next power of 2 (*3) bytes suitable for direct inclusion in the GIF header :param palette_bytes: Unpadded palette bytes, in RGBRGB form :returns: Null padded palette iii(RRR(RRtactual_target_size_diff((s2/tmp/pip-build-SKekGm/pillow/PIL/GifImagePlugin.pyRs   cCs |jjS(s Gets the palette for inclusion in the gif header :param im: Image object :returns: Bytes, len<=768 suitable for inclusion in gif header (R!(RJ((s2/tmp/pip-build-SKekGm/pillow/PIL/GifImagePlugin.pyRscCs?d}xddddgD]w}|r||kr|dkrM||dks|dkrdt||kozdkn rqnd }PqqW|jjd d krd }nt|}t|}d |kr|d nd}d |t|jdt|jdt|d t|tdt|gS(s2Return a list of strings representing a GIF headert87aR<R=R@R>iiit89aRRRi( RRRRRRRPRR(RJRRt extensionKeyRRR((s2/tmp/pip-build-SKekGm/pillow/PIL/GifImagePlugin.pyRs& '    cCskz]||_t|||dtj||dd|jdt|jfg|jdWd|`XdS(NiRBs(ii(RRRRRPRiRTR(RRRtparams((s2/tmp/pip-build-SKekGm/pillow/PIL/GifImagePlugin.pyRs cCst||}|dkr$i}nd|krSd|jkrS|jd|dsB("     (  A   L . #  (