3

hzv                 @   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d5ddZdd Zdd Zdd Zdd Zd6d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#d7d/d0Z$d9d1d2Z%ej&ej'ee ej(ej'e ej)ej'e ej*ej'd3 ej+ej'd4 dS ):    N   )Image
ImageChops	ImageFileImagePaletteImageSequence)i16le)o8)o16lec             C   s   | d d dkS )N      GIF87a   GIF89a)r   r    )prefixr   r   R/var/www/html/enquirykeeper_venv/lib/python3.6/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 jd}|r(|d r(| j j|d S d S )Nr   r   )fpread)selfsr   r   r   data:   s    zGifImageFile.datac             C   s   | j 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 jd|> }xltdt	|dD ]X}|d ||   ko||d    ko||d  kn  st
jd|}| | _| _P qW | j | _| j j | _d | _d | _| j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*    :zGifImageFile._openc             C   sb   | j d kr\| j }yx| j| j d  qW W n" tk
rP   | j d | _ Y nX | j| | j S )Nr   )r0   r.   seekEOFError)r   currentr   r   r   n_frames^   s    

zGifImageFile.n_framesc             C   sj   | j d krd| jd k	r"| jdk| _ nB| j }y| jd d| _ W n tk
rX   d| _ Y nX | j| | j S )Nr   TF)r1   r0   r.   r8   r9   )r   r:   r   r   r   is_animatedj   s    




zGifImageFile.is_animatedc             C   s   | j |sd S || jk r0|dkr&d | _| jd | j}xbt| jd |d D ]J}y| j| W qL tk
r } z| j| td|W Y d d }~X qLX qLW d S )Nr   r   zno more images in GIF file)Z_seek_check_GifImageFile__frameimr2   r(   r9   r8   )r   frameZ
last_framefer   r   r   r8   {   s    



zGifImageFile.seekc             C   s  |dkr>d| _ d | _ddddg| _d"| _| jj| j d| _n| jrL| j	  || jd krht
d| || _g | _| j| _| j r| jj| j  x| j rqW d| _ | jr| jj| j| j ddlm} || j| _i }d }d }xZ| jjd}| p|dkrP q|dkr>| jjd}| j }|d dkrz|d }|d@ rJ|d }t|dd	 |d
< d|@ }	|	d? }	|	r*|	| _n|d dkrx8|rd|kr|d  |7  < n||d< | j }qW qnd|d dkr*|| jj f|d< |d d dkr*| j }t|dkr*|d dkr*t|d|d< x| j r:q,W q|dkr| j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jd| jjd|> | _| jjdd }| jj | _ P qqW y| jdk rTd | _n| jdkr| j\}
}}}||
 || f}tj| | jjd|}|d kr| jjdd}tjj d||| _n`| jr| j!| j| j| _nD|d k	r"| j\}
}}}||
 || f}tj| tjj d||| _W n t"k
r:   Y nX |d k	rd#}|d k	rn|dkrj|| jd< n|}d |
|||f| j |||ffg| _nt#x<d$D ]4}||kr|| | j|< n|| jkr| j|= qW d!| _$| jrd| _$d S )%Nr   r   zcannot seek to frame )copy   ;   !   r    r   duration   r!      comment   	extensionr   s   NETSCAPE2.0loop   ,	      r   r   @   r   r   r"   transparencyr   PgifLrU   )rF   rI   rK   rL   )%Z_GifImageFile__offsetZdisposeZdispose_extentr=   r-   r8   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AttributeErrorr9   mode)r   r?   rB   r$   Zframe_transparency	interlacer   blockr3   Zdispose_bitsZx0Zy0x1y1r4   Zdispose_sizecolorrQ   kr   r   r   r2      s    



 "










zGifImageFile._seekc                s@   | j  r.d| jkr.tjj| j| j| jd | _ tt| j	  d S )NrQ   )
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=   )r   r   r   r   r.   I  s    zGifImageFile.tellc             C   sB   z4y| j | jkr| j j  W n tk
r0   Y nX W d d | _ X d S )N)r-   r   closer]   )r   r   r   r   
_close__fpL  s    
zGifImageFile._close__fp)__name__
__module____qualname__formatformat_descriptionZ!_close_exclusive_fp_after_loadingr+   r   r7   propertyr;   r<   r8   r2   rf   r.   ri   __classcell__r   r   )rg   r   r   2   s    9r   rT   rR   )1rT   rR   Fc             C   sp   | j tkr| j  | S tj| j dkrf|r\d}| jrJt| jj d d }| jdtj	|dS | jdS | j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    rR   )r,   colorsrT   )
r^   RAWMODErV   r   Zgetmodebaser,   r)   getdataconvertZADAPTIVE)r>   Zinitial_callZpalette_sizer   r   r   _normalize_mode]  s    

rw   c       	      C   s~  d}|r>t |tttfr(t|dd }t |tjr>t|j}| jdkrb|s| jjddd }n*|s|tdd t	dD }tjd|d| _|rRg }x`t	dt
|d	D ]L}t|||d	  }y| jj| }W n tk
r   d}Y nX |j| qW xLt|D ]@\}}|dkrx*t	t
|D ]}||kr"|||< P q"W qW | j|} n t| |}|dk	rr| j||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   rR   r"   c             s   s   | ]}|d  V  qdS )r    Nr   ).0r6   r   r   r   	<genexpr>  s    z%_normalize_palette.<locals>.<genexpr>)r,   r   r    )
isinstancebytes	bytearraylistr   r,   r^   r>   Z
getpaletter(   r)   tuplers   KeyErrorappend	enumerateZremap_palette_get_optimize)	r>   r,   r$   Zsource_paletteused_palette_colorsr6   Zsource_colorindexjr   r   r   _normalize_palette{  sB    






r   c             C   s   t | d}x$|jj D ]\}}| jj|| qW t||| j}xt|| jD ]}|j| qLW d}t| rr|dB }t	|| d| dt| f|_
tj||dd| j dt|j fg |jd d S )	NTr   rP   r   rS       )r   r   )r   r   )rw   r$   itemsencoderinfo
setdefaultr   _get_global_headerwriteget_interlace_write_local_headerZencoderconfigr   _saverX   rt   r^   )r>   r   r,   Zim_outrd   vr   r3   r   r   r   _write_single_frame  s    
$r   c          	   C   s  | j jd| jjd}| j jd| jjd}g }d}d }xtj| g| j jdg D ]}xtj|D ]}	t|	j }	|dkrx$|	jj	 D ]\}
}| j j
|
| qW t|	|| j }	| j j }t|ttfr|| |d< t|ttfr|| |d< |d7 }|r|d }|jddkrp|d krj| j jd| jjdd}t|	|}tjd|	j|}|j|d d	 j |}n|d	 }t|	t|krtj|	|}ntj|	jd
|jd
}|j }|s|rh|d d  |d 7  < qhnd }|j|	||d qhW qTW t|dkrx|D ]}|d	 }	|d sPx"t|	|d D ]}|j| q6W d}n0|sbd|d d< |	j|d }	|d d d }t||	||d  qW dS d| j krt| j d ttfrt | j d | j d< d S )NrF   disposalr   Zappend_imagesr   r!   rQ   rR   r>   r"   r   )r>   bboxr   r   Tinclude_color_tablerU   )r   r   r   )r   r   )!r   rZ   r$   	itertoolschainr   Iteratorrw   rB   r   r   r   rz   r}   r~   _get_backgroundr   newrX   Z
putpaletter,   _get_palette_bytesr   Zsubtract_modulorv   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  sp    "




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 ksd| jkr,| j jd| jjd}nd }| j jdd| j d< | sXt| || rdt| || |jd t|dr|j  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 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}y|j d }W n tk
r&   Y nJX t|}d}t||j }|d k	rpy|j|}W n tk
rn   d}Y nX d|j krt|j d d }nd}t|j jdd}|s|dks|r|rdnd}	|	|d	> O }	|sd}| jd
td td t|	 t	| t| td  d|j krdt
|j d kr| jd
td  |j d }
t|
tr`|
j }
x@tdt
|
dD ],}|
||d  }| jtt
||  qrW | jtd d|j kr|j d }| jd
td td d td td t	| td  |j jd}|r<t|}t|}|r<|dB }||B }| jdt	|d  t	|d  t	|jd  t	|jd  t|  |r|r| jt| | jtd d S )NFrQ   TrF   r   r   r   r   r!   rD   rE   rO   rI   rH   rJ   rL   r   s   NETSCAPE2.0r    r   r   rM   r   )r   r   intr   r   rW   rZ   r   r	   o16r)   rz   strencoder(   r   _get_color_table_sizerX   _get_header_palette)r   r>   r   r3   Ztransparent_color_existsrQ   r   rF   r   Zpacked_flagrI   r6   ZsubblockZnumber_of_loopsr   palette_bytescolor_table_sizer   r   r   r   2  sb    

6 

:Br   c       
      C   s   | j  }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j	  |j
 }	|	rtj|	||j
 }	|	rtj|	|W d Q R X W d ytj| W n tk
r   Y nX X d S )Nwbr"   Zppmtogif)stdoutstderrZppmquantZ256)stdinr   r   )_dumpopenr^   
subprocess
check_callDEVNULLPopenPIPEr   rh   waitCalledProcessErrorosunlinkOSError)
r>   r   r   tempfiler@   Z	quant_cmdZ	togif_cmdZ
quant_procZ
togif_procretcoder   r   r   _save_netpbm  s4    



r   c             C   s   | j dkr|r|jddrtp&| j dk}|s<| j| j d	k rg }x&t| j D ]\}}|rN|j| qNW |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
    rR   rT   r   r   i   r   N)rR   rT   i   )
r^   rZ   _FORCE_OPTIMIZEwidthheightr   Z	histogramr   r)   rY   )r>   r$   Zoptimiser   r6   countr   r   r   r     s    r   c             C   s:   | sdS t | dk rdS tjtjt | d dd S d S )Nr   rN   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   sb   d}|r^|}t |tr^y| jj|| }W n4 tk
r\ } zt|dkrJdS  W Y d d }~X nX |S )Nr   z$cannot allocate more than 256 colors)rz   r~   r,   ZgetcolorrW   r   )r>   ZinfoBackgroundr   rA   r   r   r   r     s    
r   c             C   s   d}xrdD ]V}|r
||kr
|dkr.|| dks
|dkrZdt ||   koPdkn   rZq
d	}P q
W | jjd
d	krxd	}t| |j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   87arQ   rF   rL   rI   r   r   rJ   s   89ar   r   s   GIFr   )rQ   rF   rL   rI   )
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   sR   zF||_ t| ||d tj|| dd|j dt|j fg | jd W d |` X d S )Nr   rS   r   )r   r   )r   r   r   r   rX   rt   r^   r   )r   r   r   paramsr   r   r   r   A  s     r   c             C   sd   t | |}|dkri }d|kr6d| jk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   c             K   s0   G dd d}| j   | }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 j| d S )N)r   r   )r   r   r   r   r   r     s    z getdata.<locals>.Collector.writeN)rj   rk   rl   r   r   r   r   r   r   	Collector  s   r   )rV   r   r   )r>   r   r   r   r   r   r   r   ru   s  s
    ru   z.gifz	image/gif)F)F)NNr   r   )r   ),r   r   r   r    r   r   r   r   r   _binaryr   r%   r	   r
   r   r   r   rt   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ru   Zregister_openrm   Zregister_saveZregister_save_allZregister_extensionZregister_mimer   r   r   r   <module>   sH   	  *
5N

T2%

'

!