B
    •%Z\V0  ã               @   sª   d Z ddlmZmZmZ ddlZddlZddlZddlm	Z	 ddl
ZddlmZ ddlmZmZmZ e e¡Zdadd	„ ZG d
d„ deƒZdZeddddƒZe e¡ dS )z9 SWF plugin. Most of the actual work is done in _swf.py.
é    )Úabsolute_importÚprint_functionÚdivisionN)ÚBytesIOé   )Úformats)ÚFormatÚread_n_bytesÚimage_as_uintc               C   s   ddl ma tS )Né   )Ú_swf)Ú r   © r   r   ú2lib/python3.7/site-packages/imageio/plugins/swf.pyÚload_lib   s    r   c               @   sD   e Zd ZdZdd„ Zdd„ ZG dd„ dejƒZG dd	„ d	ejƒZd
S )Ú	SWFFormata<   Shockwave flash (SWF) is a media format designed for rich and
    interactive animations. This plugin makes use of this format to
    store a series of images in a lossless format with good compression
    (zlib). The resulting images can be shown as an animation using
    a flash player (such as the browser).
    
    SWF stores images in RGBA format. RGB or grayscale images are
    automatically converted. SWF does not support meta data.
    
    Parameters for reading
    ----------------------
    loop : bool
        If True, the video will rewind as soon as a frame is requested
        beyond the last frame. Otherwise, IndexError is raised. Default False.
    
    Parameters for saving
    ---------------------
    fps : int
        The speed to play the animation. Default 12.
    loop : bool
        If True, add a tag to the end of the file to play again from
        the first frame. Most flash players will then play the movie
        in a loop. Note that the imageio SWF Reader does not check this
        tag. Default True.
    html : bool
        If the output is a file on the file system, write an html file
        (in HTML5) that shows the animation. Default False.
    compress : bool
        Whether to compress the swf file. Default False. You probably don't
        want to use this. This does not decrease the file size since
        the images are already compressed. It will result in slower
        read and write time. The only purpose of this feature is to
        create compressed SWF files, so that we can test the
        functionality to read them.
    c             C   s:   |j d | jd kr6|jdd…  dd¡}|dkr6dS d S )	Nr   ú?r   é   ÚasciiÚignore)ÚFWSÚCWST)ÚmodeÚmodesÚ
firstbytesÚdecode)ÚselfÚrequestÚtmpr   r   r   Ú	_can_readE   s    zSWFFormat._can_readc             C   s(   |j d | jd kr$|j| jkr$dS d S )Nr   r   T)r   r   Ú	extensionÚ
extensions)r   r   r   r   r   Ú
_can_writeK   s    zSWFFormat._can_writec               @   sF   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dS )zSWFFormat.ReaderFc             C   s¨  t s
tƒ  t|ƒ| _| j ¡ | _| jjdd…  dd¡}|dkrBnD|dkr~| j 	¡ }|d d… t
 |dd … ¡ }t|ƒ| _ntdƒ‚y| j d¡ d	| _W n$ tk
rÀ   d
| _|  d¡ Y nX t  |  d¡d¡}d|d  }|d }|d rø|d7 }t|ƒ}|  |d ¡ g | _| js¤ypxj|  ¡ \}}}}	| j ¡ }
|rtt|  d¡dd … ƒ}|dkrt| j |
|||	f¡ | j |
| ¡ q W W n tk
r¢   Y nX d S )Nr   r   r   r   r   r   é   z(This does not look like a valid SWF fileFTr   é   é   g       @r   )r   r   ÚboolÚ	_arg_loopr   Úget_fileÚ_fpr   r   ÚreadÚzlibZ
decompressr   ÚIOErrorÚseekÚ_streaming_modeÚ	ExceptionÚ_fp_readZbits2intÚintÚ_imlocsÚ_read_one_tagÚtellÚordÚappendÚ
IndexError)r   Úloopr   ÚbbZnbitsZLrectÚisimageÚszeÚTÚL1ÚlocÚformatr   r   r   Ú_openS   sL    




zSWFFormat.Reader._openc             C   s   t | j|ƒS )N)r	   r)   )r   Únr   r   r   r0   Ž   s    zSWFFormat.Reader._fp_readc             C   s   d S )Nr   )r   r   r   r   Ú_close‘   s    zSWFFormat.Reader._closec             C   s   | j rtjS t| jƒS d S )N)r.   ÚnpÚinfÚlenr2   )r   r   r   r   Ú_get_length”   s    zSWFFormat.Reader._get_lengthc       	      C   sÖ   |dk rt dƒ‚| jsF| jr0| jr0|t| jƒ }|t| jƒkrFt dƒ‚| jr’x„|  ¡ \}}}}|  |¡}|rNt |d||¡}|d k	rN|i fS qNW n@| j| \}}}}| j	 
|¡ |  |¡}t |d||¡}|i fS d S )Nr   zIndex in swf file must be > 0zIndex out of bounds)r7   r.   r'   r2   rE   r3   r0   r   Zread_pixelsr)   r-   )	r   Úindexr:   r;   r<   r=   r9   Úimr>   r   r   r   Ú	_get_dataš   s(    

zSWFFormat.Reader._get_datac             C   s~   |   d¡}|stdƒ‚t |¡\}}}|s2tdƒ‚d}|d }|dkrPtdƒ‚n"|dkr^d}n|dkrrt d	¡ n ||||fS )
z— 
            Return (True, loc, size, T, L1) if an image that we can read.
            Return (False, loc, size, T, L1) if any other tag.
            é   zReached end of swf moviez%Invalid tag length, could not proceedFr   )é   é$   T)rJ   é   é#   éZ   z&Ignoring JPEG image: cannot read JPEG.)r0   r7   r   Zget_type_and_lenÚRuntimeErrorÚloggerZwarning)r   Úheadr<   r=   ZL2r:   r;   r   r   r   r3   ·   s    

zSWFFormat.Reader._read_one_tagc             C   s   i S )Nr   )r   rG   r   r   r   Ú_get_meta_dataÛ   s    zSWFFormat.Reader._get_meta_dataN)F)
Ú__name__Ú
__module__Ú__qualname__r@   r0   rB   rF   rI   r3   rS   r   r   r   r   ÚReaderR   s   
;$rW   c               @   s>   e Zd Zddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ ZdS )zSWFFormat.Writeré   TFc             C   sd   t s
tƒ  t|ƒ| _t|ƒ| _t|ƒ| _t|ƒ| _| j 	¡ | _
d| _d| _| jr`| j
| _tƒ | _
d S )Nr   )éd   rY   )r   r   r1   Ú_arg_fpsr&   r'   Ú	_arg_htmlÚ_arg_compressr   r(   r)   Ú_framecounterÚ
_framesizeÚ_fp_realr   )r   Úfpsr8   ÚhtmlÚcompressr   r   r   r@   á   s    



zSWFFormat.Writer._openc       
   	   C   s4  |   ¡  | j ¡ }| j | j¡ | j t | j¡¡ | j	r„| j 
¡ }| j| _| j |d d… ¡ | j t |dd … ¡¡ | j ¡ }| j d¡ | j t |¡¡ d | _| jr0tj | jj¡r0tj | jj¡\}}tj ||d d… d ¡}| j\}}t||||f }t|dƒ}	|	 | d¡¡ W d Q R X d S )Nr#   r%   éüÿÿÿz.htmlÚwbzutf-8)Ú	_completer)   r4   r-   Ú_location_to_save_nframesÚwriter   Z
int2uint16r]   r\   Úgetvaluer_   r+   rb   Z
int2uint32r[   ÚosÚpathÚisfiler   ÚfilenameÚsplitÚjoinr^   ÚHTMLÚopenÚencode)
r   r;   r9   ÚdirnameÚfnamerl   ÚwÚhra   Úfr   r   r   rB   ó   s(    



zSWFFormat.Writer._closec             C   sà   || _ d}|d| j  d¡7 }|d d¡7 }|t d¡7 }|d d¡7 }|t ¡  d|d d|d ¡ ¡ 7 }|t d¡t |¡ 7 }t|ƒ| _	|d	 d¡7 }| j
 |¡ t ¡ t ddd¡f}x|D ]}| j
 | ¡ ¡ qÄW d S )
Nó    ZFCr   ZWSr#   Z0000r   r   Z00)r^   r\   rq   r   Z	int2uint8ZTagZmake_rect_recordÚtobytesrE   rf   r)   rg   ZFileAttributesTagZSetBackgroundTagÚget_tag)r   Z	framesizer`   r9   ZtaglistÚtagr   r   r   Ú_write_header  s    "

zSWFFormat.Writer._write_headerc             C   sF   | j s|  d| j¡ | js0| j t d¡ ¡ ¡ | j d 	d¡¡ d S )N)é
   r|   Ústopz  r   )
r]   r{   rZ   r'   r)   rg   r   ZDoActionTagry   rq   )r   r   r   r   re   $  s
    zSWFFormat.Writer._completec       
      C   sÜ   |j dkr.|jd dkr.|d d …d d …df }t|dd}|jd |jd f}d}| jdkrnd}|  || j¡ t |¡}t |j	d	|¡}tj
d|j	| d
}t ¡ }x$||||gD ]}	| j |	 ¡ ¡ q²W |  jd7  _d S )Nr   éÿÿÿÿr   r   r#   )ZbitdepthFT)r   r   )Zmove)ÚndimÚshaper
   r]   r{   rZ   r   Z	BitmapTagZShapeTagÚidZPlaceObjectTagZShowFrameTagr)   rg   ry   )
r   rH   ÚmetaZwhZisfirstframeZbmZshZpoZsfrz   r   r   r   Ú_append_data.  s    

zSWFFormat.Writer._append_datac             C   s   d S )Nr   )r   r‚   r   r   r   Úset_meta_dataD  s    zSWFFormat.Writer.set_meta_dataN)rX   TFF)	rT   rU   rV   r@   rB   r{   re   rƒ   r„   r   r   r   r   ÚWriterà   s   

r…   N)	rT   rU   rV   Ú__doc__r   r"   r   rW   r…   r   r   r   r   r       s   # r   z
<!DOCTYPE html>
<html>
<head>
    <title>Show Flash animation %s</title>
</head>
<body>
    <embed width="%i" height="%i" src="%s">
</html>
ZswfzShockwave flashz.swfÚI)r†   Z
__future__r   r   r   ri   r+   ZloggingÚior   ZnumpyrC   r   r   Zcorer   r	   r
   Z	getLoggerrT   rQ   r   r   r   ro   r?   Z
add_formatr   r   r   r   Ú<module>   s*   
  3