a
    xdzv                     @   sn  d dl Z d dlZd dlZd dlZddlmZmZmZmZm	Z	 ddl
mZ ddl
mZ ddl
mZ dd ZG d	d
 d
ejZddddZd6ddZdd Zdd Zdd Zdd Zd7ddZdd Zdd Zdd  ZdZd!d" Zd#d$ Zd%d& Zd'd( Z d)d* Z!d+d, Z"d-d. Z#d8d/d0Z$d9d2d3Z%e&ej'ee e(ej'e e)ej'e e*ej'd4 e+ej'd5 dS ):    N   )Image
ImageChops	ImageFileImagePaletteImageSequence)i16le)o8)o16lec                 C   s   | d d dv S )N   )s   GIF87as   GIF89a )prefixr   r   J/var/www/html/Ranjet/env/lib/python3.9/site-packages/PIL/GifImagePlugin.py_accept)   s    r   c                       st   e Zd ZdZdZdZdZdd Zdd Ze	d	d
 Z
e	dd Zdd Zdd Z fddZdd Zdd Z  ZS )GifImageFileZGIFzCompuserve GIFFNc                 C   s,   | j d}|r(|d r(| j |d S d S )Nr   r   )fpread)selfsr   r   r   data:   s    zGifImageFile.datac                 C   s  | j d}t|std|d d | jd< t|dt|df| _g | _|d }|d@ d }|d	@ r|d
 | jd< | j d|> }tdt	|dD ]X}|d ||   kr||d    kr||d  ksn t
d|}| | _| _ qq| j | _| j  | _d | _d | _| d d S )N   znot a GIF filer   version   
      r         
background   r      RGB)r   r   r   SyntaxErrorinfoi16_sizetilerangelenr   rawglobal_palettepalette_GifImageFile__fptell_GifImageFile__rewind	_n_frames_is_animated_seek)r   r   flagsbitspir   r   r   _open@   s*    8zGifImageFile._openc                 C   s\   | j d u rV|  }z| |  d  qW n  tyJ   |  d | _ Y n0 | | | j S )Nr   )r.   r,   seekEOFErrorr   currentr   r   r   n_frames^   s    

zGifImageFile.n_framesc                 C   sh   | j d u rb| jd ur"| jdk| _ n@|  }z| d d| _ W n tyV   d| _ Y n0 | | | j S )Nr   TF)r/   r.   r,   r6   r7   r8   r   r   r   is_animatedj   s    




zGifImageFile.is_animatedc                 C   s   |  |sd S || jk r0|dkr&d | _| d | j}t| jd |d D ]L}z| | W qJ ty } z | | td|W Y d }~qJd }~0 0 qJd S )Nr   r   zno more images in GIF file)Z_seek_check_GifImageFile__frameimr0   r&   r7   r6   )r   frameZ
last_framefer   r   r   r6   {   s    



zGifImageFile.seekc                 C   s  |dkr:d| _ d | _g d| _d| _| j| j d| _n| jrH| 	  || jd krdt
d| || _g | _| j| _| j r| j| j  |  rqd| _ | jr| j| j| j ddlm} || j| _i }d }d }| jd}|r|dkrq&q|dkr&| jd}|  }|d d	krl|d }|d@ r<|d
 }t|dd |d< d|@ }	|	d? }	|	r|	| _n|d dkr|rd|v r|d  |7  < n||d< |  }qzqnd|d dkr|| j f|d< |d d dkr|  }t|d
kr|d dkrt|d|d< |  r$qq|dkr| jd}t|dt|d }
}|
t|d |t|d  }}|| jd ks|| jd krt|| jd t|| jd f| _|
|||f| _|d }|d@ dk}|d@ r|d@ d }td| jd
|> | _| jdd }| j | _ q&qqz| jdk r<d | _n| jdkr| j\}
}}}||
 || f}t| | jd|}|d u r| jd d}tj d!||| _n`| jr| !| j| j| _nD|d ur
| j\}
}}}||
 || f}t| tj d!||| _W n t"y    Y n0 |d urvd}|d urT|dkrP|| jd< n|}d"|
|||f| j |||ffg| _nt#d#D ]4}||v r|| | j|< n|| jv r~| j|= q~d$| _$| jrd!| _$d S )%Nr   )r   r   r   r   r   zcannot seek to frame )copy   ;   !   r   r   duration   r      comment   	extensionr      NETSCAPE2.0loop   ,	      r   r   @   r   r   r    transparencyr   Pgif)rF   rI   rK   rM   L)%Z_GifImageFile__offsetZdisposeZdispose_extentr<   r+   r6   r-   Zdisposal_methodr%   load
ValueErrorr   r   r=   ZpasterB   r)   r*   r   r#   r,   r'   sizemaxr$   r   r(   r   Z_decompression_bomb_checkr"   getcorefillZ_cropAttributeErrorr7   mode)r   r>   rB   r"   Zframe_transparency	interlacer   blockr1   Zdispose_bitsZx0Zy0x1y1r2   Zdispose_sizecolorrR   kr   r   r   r0      s    




 "











zGifImageFile._seekc                    s>   | j s,d| jv r,tj| j| j| jd | _ tt| 	  d S )NrR   )
r=   r"   r   r[   r\   r^   rX   superr   load_preparer   	__class__r   r   rf   C  s    zGifImageFile.load_preparec                 C   s   | j S N)r<   rg   r   r   r   r,   I  s    zGifImageFile.tellc                 C   sF   z8z| j | jkr| j   W n ty.   Y n0 W d | _ nd | _ 0 d S rj   )r+   r   closer]   rg   r   r   r   
_close__fpL  s    zGifImageFile._close__fp)__name__
__module____qualname__formatformat_descriptionZ!_close_exclusive_fp_after_loadingr)   r   r5   propertyr:   r;   r6   r0   rf   r,   rl   __classcell__r   r   rh   r   r   2   s    

 9r   rU   rS   )1rU   rS   Fc                 C   sp   | j tv r|   | S t| j dkrf|r\d}| jrJt| j d d }| jdtj	|dS | dS | dS )a  
    Takes an image (or frame), returns an image in a mode that is appropriate
    for saving in a Gif.

    It may return the original image, or it may return an image converted to
    palette or 'L' mode.

    UNDONE: What is the point of mucking with the initial call palette, for
    an image that shouldn't have a palette, or it would be a mode 'P' and
    get returned in the RAWMODE clause.

    :param im: Image object
    :param initial_call: Default false, set to true for a single frame.
    :returns: Image object
    r       r   r   rS   )r*   colorsrU   )
r^   RAWMODErV   r   Zgetmodebaser*   r'   getdataconvertZADAPTIVE)r=   Zinitial_callZpalette_sizer   r   r   _normalize_mode]  s    

rz   c           	   	   C   sn  d}|r>t |tttfr(t|dd }t |tjr>t|j}| jdkrb|s| jddd }n*|s|tdd t	dD }tjd|d| _|rBg }t	dt
|d	D ]J}t|||d	  }z| jj| }W n ty   d}Y n0 || qt|D ]:\}}|du rt	t
|D ]}||vr|||<  qqq| |} n t| |}|durb| ||S || j_| S )
at  
    Normalizes the palette for image.
      - Sets the palette to the incoming palette, if provided.
      - Ensures that there's a palette for L mode images
      - Optimizes the palette if necessary/desired.

    :param im: Image object
    :param palette: bytes object containing the source palette, or ....
    :param info: encoderinfo
    :returns: Image object
    Ni   rS   r    c                 s   s   | ]}|d  V  qdS )r   Nr   ).0r4   r   r   r   	<genexpr>      z%_normalize_palette.<locals>.<genexpr>r*   r   r   )
isinstancebytes	bytearraylistr   r*   r^   r=   Z
getpaletter&   r'   tuplerv   KeyErrorappend	enumerateZremap_palette_get_optimize)	r=   r*   r"   Zsource_paletteused_palette_colorsr4   Zsource_colorindexjr   r   r   _normalize_palette{  sB    






r   c              	   C   s   t | d}|j D ]\}}| j|| qt||| j}t|| jD ]}|| qFd}t| rj|dB }t	|| d| dt| f|_
t||dd| j dt|j fg |d d S )NTr   rQ   r   r   r   rT       )rz   r"   itemsencoderinfo
setdefaultr   _get_global_headerwriteget_interlace_write_local_headerZencoderconfigr   _saverX   rw   r^   )r=   r   r*   Zim_outrd   vr   r1   r   r   r   _write_single_frame  s    
$r   c              	   C   s  | j d| jd}| j d| jd}g }d}d }t| g| j dg D ]}t|D ]}	t|	 }	|dkr|	j	 D ]\}
}| j 
|
| qt|	|| j }	| j  }t|ttfr|| |d< t|ttfr|| |d< |d7 }|r|d }|ddkrd|d u r^| j d| jdd	}t|	|}td
|	j|}||d d j |}n|d }t|	t|krt|	|}nt|	d|d}| }|s|r`|d d  |d 7  < q`nd }||	||d q`qPt|dkr|D ]}|d }	|d s:t|	|d D ]}|| q"d}n0|sLd|d d< |	|d }	|d d d }t||	||d  qdS d| j v rt| j d ttfrt | j d | j d< d S )NrF   disposalr   Zappend_imagesr   rA   r   rR   )r   r   r   rS   r=   r    r   )r=   bboxr   r   r   Tinclude_color_table)!r   rZ   r"   	itertoolschainr   Iteratorrz   rB   r   r   r   r   r   r   _get_backgroundr   newrX   Z
putpaletter*   _get_palette_bytesr   Zsubtract_modulory   Zgetbboxr   r'   r   r   Zcrop_write_frame_datasum)r=   r   r*   rF   r   Z	im_framesZframe_countZbackground_imZ
imSequenceim_framerd   r   r   previousrc   r   Zbase_imdeltar   Z
frame_datar   offsetr   r   r   _write_multiple_frames  sv    



r   c                 C   s   t | ||dd d S )NT)save_all)r   )r=   r   filenamer   r   r   	_save_all  s    r   c                 C   s   d| j v sd| jv r,| j d| jd}nd }| j dd| j d< |rTt| ||s`t| || |d t|dr||  d S )Nr*   optimizeTrC   flush)r   r"   rZ   r   r   r   hasattrr   )r=   r   r   r   r*   r   r   r   r     s    

r   c                 C   s$   | j dd}t| jdk r d}|S )Nr_   r      r   )r   rZ   minrX   )r=   r_   r   r   r   r   (  s    r   c                 C   s  d}z|j d }W n ty$   Y nH0 t|}d}t||j }|d urlz||}W n tyj   d}Y n0 d|j v rt|j d d }nd}t|j dd}|s|dks|r|rdnd}	|	|d	> O }	|sd}| d
td td t|	 t	| t| td  d|j v rdt
|j d kr| d
td  |j d }
t|
tr\|
 }
tdt
|
dD ],}|
||d  }| tt
||  ql| td d|j v r|j d }| d
td td d td td t	| td  |j d}|r4t|}t|}|r4|dB }||B }| dt	|d  t	|d  t	|jd  t	|jd  t|  |r|r| t| | td d S )NFrR   TrF   r   r   r   r   r   rD   rE   rP   rI   rH   rJ   rM   r   rL   r   r   r   rN   r   )r   r   intr   r   rW   rZ   r   r	   o16r'   r   strencoder&   r   _get_color_table_sizerX   _get_header_palette)r   r=   r   r1   Ztransparent_color_existsrR   r   rF   r   Zpacked_flagrI   r4   ZsubblockZnumber_of_loopsr   palette_bytescolor_table_sizer   r   r   r   2  s    


 




r   c           
      C   s  |   }zt|d}| jdkr8tjd|g|tjd ntdd|g}dg}tj|tjtjd}tj||j|tjd}|j	  |
 }	|	rt|	||
 }	|	rt|	|W d    n1 s0    Y  W zt| W n ty   Y n0 n&zt| W n ty   Y n0 0 d S )Nwbr    Zppmtogif)stdoutstderrZppmquantZ256)stdinr   r   )_dumpopenr^   
subprocess
check_callDEVNULLPopenPIPEr   rk   waitCalledProcessErrorosunlinkOSError)
r=   r   r   tempfiler?   Z	quant_cmdZ	togif_cmdZ
quant_procZ
togif_procretcoder   r   r   _save_netpbm  sB    



,r   c                 C   s   | j dv r|r|ddrtp&| j dk}|s<| j| j dk rg }t|  D ]\}}|rL|| qL|st|dkrt	|t|kr|S dS )aL  
    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
    )rS   rU   r   r   rU   i   r   N)
r^   rZ   _FORCE_OPTIMIZEwidthheightr   Z	histogramr   r'   rY   )r=   r"   Zoptimiser   r4   countr   r   r   r     s    
r   c                 C   s:   | sdS t | dk rdS ttt | d dd S d S )Nr   rO   r   r   r   )r'   mathceillog)r   r   r   r   r     s
    r   c                 C   s<   t | }d|> t| d  }|dkr8| tdd | 7 } | S )z
    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
    r   r   r   )r   r'   r	   )r   r   Zactual_target_size_diffr   r   r   r     s
    r   c                 C   s   | j j S )z
    Gets the palette for inclusion in the gif header

    :param im: Image object
    :returns: Bytes, len<=768 suitable for inclusion in gif header
    r~   )r=   r   r   r   r     s    r   c              
   C   sn   d}|rj|}t |trjz| j|| }W n@ tyh } z(t|dkrRW Y d }~dS  W Y d }~n
d }~0 0 |S )Nr   z$cannot allocate more than 256 colors)r   r   r*   ZgetcolorrW   r   )r=   ZinfoBackgroundr   r@   r   r   r   r     s    
r   c                 C   s   d}dD ]T}|r||v r|dkr,|| dks|dkrTdt ||   krPdksTq qd} qrq| jd	dkrrd}t| |d
}t| }t|}d| t| jd  t| jd  t|d t|td t	|gS )z2Return a list of strings representing a GIF headers   87a)rR   rF   rM   rI   rF   r   rI   r   rJ   s   89ar   r   s   GIFr   )
r'   r"   rZ   r   r   r   r   rX   r	   r   )r=   r"   r   ZextensionKeyr   r   r   r   r   r   r     s:    
r   c              	   C   sV   zJ||_ t| ||d t|| dd|j dt|j fg | d W |` n|` 0 d S )Nr   rT   r   r   )r   r   r   r   rX   rw   r^   r   )r   r   r   paramsr   r   r   r   A  s    r   c                 C   sd   t | |}|du ri }d|vr6d| jv r6| jd |d< t| ||}|j| _|j| _t| |}||fS )a  
    Legacy Method to get Gif data from image.

    Warning:: May modify image data.

    :param im: Image object
    :param palette: bytes object containing the source palette, or ....
    :param info: encoderinfo
    :returns: tuple of(list of header items, optimized palette)

    Nr   )r   r"   r   r*   r=   r   )r=   r*   r"   r   Zim_modheaderr   r   r   	getheaderU  s    

r   r   c                 K   s0   G dd d}|    | }t|| || |jS )a  
    Legacy Method

    Return a list of strings representing this image.
    The first string is a local image header, the rest contains
    encoded image data.

    :param im: Image object
    :param offset: Tuple of (x, y) pixels. Defaults to (0,0)
    :param \**params: E.g. duration or other encoder info parameters
    :returns: List of Bytes containing gif encoded frame data

    c                   @   s   e Zd Zg Zdd ZdS )zgetdata.<locals>.Collectorc                 S   s   | j | d S rj   )r   r   )r   r   r   r   r   r     s    z getdata.<locals>.Collector.writeN)rm   rn   ro   r   r   r   r   r   r   	Collector  s   r   )rV   r   r   )r=   r   r   r   r   r   r   r   rx   s  s
    rx   z.gifz	image/gif)F)F)NN)r   ),r   r   r   r    r   r   r   r   r   _binaryr   r#   r	   r
   r   r   r   rw   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rx   Zregister_openrp   Zregister_saveZregister_save_allZregister_extensionZregister_mimer   r   r   r   <module>   sH   	  *
5N

T2%

'

!