ó
;c›]c           @` 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 j e ƒ Z e a d „  Z d	 e f d
 „  ƒ  YZ d Z e d d d d ƒ Z e j e ƒ d S(   s9    SWF plugin. Most of the actual work is done in _swf.py.
i    (   t   absolute_importt   print_functiont   divisionN(   t   BytesIOi   (   t   formats(   t   Formatt   read_n_bytest   image_as_uintc           C` s   d d l  m a t S(   Ni   (   t   _swf(   t    R   (    (    (    s2   lib/python2.7/site-packages/imageio/plugins/swf.pyt   load_lib   s    t	   SWFFormatc           B` sR   e  Z d  Z d „  Z d „  Z d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ RS(   s<   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` sM   | j  d |  j d k rI | j d d !j d d ƒ } | d	 k rI t Sn  d  S(
   Ni   t   ?i    i   t   asciit   ignoret   FWSt   CWS(   R   R   (   t   modet   modest
   firstbytest   decodet   True(   t   selft   requestt   tmp(    (    s2   lib/python2.7/site-packages/imageio/plugins/swf.pyt	   _can_readE   s    c         C` s7   | j  d |  j d k r3 | j |  j k r3 t Sn  d  S(   Ni   R   (   R   R   t	   extensiont
   extensionsR   (   R   R   (    (    s2   lib/python2.7/site-packages/imageio/plugins/swf.pyt
   _can_writeK   s    t   Readerc           B` sJ   e  Z e d  „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   c         C` s'  t  s t ƒ  n  t | ƒ |  _ |  j j ƒ  |  _ |  j j d d !j d d ƒ } | d k r_ nT | d k r§ |  j j	 ƒ  } | d  t
 j | d ƒ } t | ƒ |  _ n t d ƒ ‚ y |  j j d ƒ t |  _ Wn' t k
 rù t |  _ |  j d ƒ n Xt  j |  j d	 ƒ d
 ƒ } d
 | d } | d } | d	 rD| d	 7} n  t | ƒ } |  j | d ƒ g  |  _ |  j s#y™ x’ t r
|  j ƒ  \ } } } }	 |  j j ƒ  }
 | rót |  j d ƒ d ƒ } | d
 k ró|  j j |
 | | |	 f ƒ qón  |  j j |
 | ƒ qyWWq#t k
 rq#Xn  d  S(   Ni    i   R   R   R   R   i   s(   This does not look like a valid SWF filei   i   i   g       @i   (   R   R
   t   boolt	   _arg_loopR   t   get_filet   _fpR   R   t   readt   zlibt
   decompressR   t   IOErrort   seekt   Falset   _streaming_modet	   ExceptionR   t   _fp_readt   bits2intt   intt   _imlocst   _read_one_tagt   tellt   ordt   appendt
   IndexError(   R   t   loopR   t   bbt   nbitst   Lrectt   isimaget   szet   Tt   L1t   loct   format(    (    s2   lib/python2.7/site-packages/imageio/plugins/swf.pyt   _openS   sL    
	

			"c         C` s   t  |  j | ƒ S(   N(   R   R!   (   R   t   n(    (    s2   lib/python2.7/site-packages/imageio/plugins/swf.pyR*   Ž   s    c         C` s   d  S(   N(    (   R   (    (    s2   lib/python2.7/site-packages/imageio/plugins/swf.pyt   _close‘   s    c         C` s!   |  j  r t j St |  j ƒ Sd  S(   N(   R(   t   npt   inft   lenR-   (   R   (    (    s2   lib/python2.7/site-packages/imageio/plugins/swf.pyt   _get_length”   s    	c   	      C` sH  | d k  r t  d ƒ ‚ n  |  j ss |  j rL |  j rL | t |  j ƒ } n  | t |  j ƒ k rs t  d ƒ ‚ qs n  |  j rê xÅ t ræ |  j ƒ  \ } } } } |  j | ƒ } | r t j	 | d | | ƒ } | d  k	 rã | i  f Sq q WnZ |  j | \ } } } } |  j j | ƒ |  j | ƒ } t j	 | d | | ƒ } | i  f Sd  S(   Ni    s   Index in swf file must be > 0s   Index out of bounds(   R2   R(   R   R-   RB   R   R.   R*   R   t   read_pixelst   NoneR!   R&   (	   R   t   indexR7   R8   R9   R:   R4   t   imR;   (    (    s2   lib/python2.7/site-packages/imageio/plugins/swf.pyt	   _get_dataš   s(    			c         C` s½   |  j  d ƒ } | s$ t d ƒ ‚ n  t j | ƒ \ } } } | sQ t d ƒ ‚ n  t } | d } | d k r| t d ƒ ‚ n1 | d k r‘ t } n | d k r­ t j d
 ƒ n  | | | | f S(   s—    
            Return (True, loc, size, T, L1) if an image that we can read.
            Return (False, loc, size, T, L1) if any other tag.
            i   s   Reached end of swf movies%   Invalid tag length, could not proceedi    i   i$   i   i#   iZ   s&   Ignoring JPEG image: cannot read JPEG.(   i   i$   (   i   i   i#   iZ   (	   R*   R2   R   t   get_type_and_lent   RuntimeErrorR'   R   t   loggert   warning(   R   t   headR9   R:   t   L2R7   R8   (    (    s2   lib/python2.7/site-packages/imageio/plugins/swf.pyR.   ·   s    
	c         C` s   i  S(   N(    (   R   RF   (    (    s2   lib/python2.7/site-packages/imageio/plugins/swf.pyt   _get_meta_dataÛ   s    (
   t   __name__t
   __module__R'   R=   R*   R?   RC   RH   R.   RO   (    (    (    s2   lib/python2.7/site-packages/imageio/plugins/swf.pyR   R   s   ;					$t   Writerc           B` sJ   e  Z d  e e e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   i   c         C` s˜   t  s t ƒ  n  t | ƒ |  _ t | ƒ |  _ t | ƒ |  _ t | ƒ |  _ |  j j	 ƒ  |  _
 d |  _ d |  _ |  j r” |  j
 |  _ t ƒ  |  _
 n  d  S(   Ni    id   (   id   id   (   R   R
   R,   t   _arg_fpsR   R   t	   _arg_htmlt   _arg_compressR   R    R!   t   _framecountert
   _framesizet   _fp_realR   (   R   t   fpsR3   t   htmlt   compress(    (    s2   lib/python2.7/site-packages/imageio/plugins/swf.pyR=   á   s    
			c   
      C` s—  |  j  ƒ  |  j j ƒ  } |  j j |  j ƒ |  j j t j |  j ƒ ƒ |  j	 r¯ |  j j
 ƒ  } |  j |  _ |  j j | d  ƒ |  j j t j | d ƒ ƒ |  j j ƒ  } n  |  j j d ƒ |  j j t j | ƒ ƒ d  |  _ |  j r“t j j |  j j ƒ r“t j j |  j j ƒ \ } } t j j | | d  d ƒ } |  j \ } } t | | | | f } t | d ƒ  }	 |	 j | j d ƒ ƒ Wd  QXn  d  S(   Ni   i   iüÿÿÿs   .htmlt   wbs   utf-8(   t	   _completeR!   R/   R&   t   _location_to_save_nframest   writeR   t
   int2uint16RV   RU   t   getvalueRX   R#   R[   t
   int2uint32RE   RT   t   ost   patht   isfileR   t   filenamet   splitt   joinRW   t   HTMLt   opent   encode(
   R   R8   R4   t   dirnamet   fnameRf   t   wt   hRZ   t   f(    (    s2   lib/python2.7/site-packages/imageio/plugins/swf.pyR?   ó   s(    
		!c         C` s0  | |  _  d } | d |  j j d ƒ 7} | d j d ƒ 7} | t j d ƒ 7} | d j d ƒ 7} | t j ƒ  j d | d d | d ƒ j ƒ  7} | t j d ƒ t j | ƒ 7} t | ƒ |  _	 | d	 j d ƒ 7} |  j
 j | ƒ t j ƒ  t j d d d ƒ f } x$ | D] } |  j
 j | j ƒ  ƒ qWd  S(
   NR	   t   FCR   t   WSi   t   0000i    i   t   00(   RW   RU   Rk   R   t	   int2uint8t   Tagt   make_rect_recordt   tobytesRB   R^   R!   R_   t   FileAttributesTagt   SetBackgroundTagt   get_tag(   R   t	   framesizeRY   R4   t   taglistt   tag(    (    s2   lib/python2.7/site-packages/imageio/plugins/swf.pyt   _write_header  s    	- !c         C` sg   |  j  s |  j d |  j ƒ n  |  j sJ |  j j t j d ƒ j ƒ  ƒ n  |  j j d j	 d ƒ ƒ d  S(   Ni
   t   stops     R   (   i
   i
   (
   RV   R   RS   R   R!   R_   R   t   DoActionTagR{   Rk   (   R   (    (    s2   lib/python2.7/site-packages/imageio/plugins/swf.pyR]   $  s
    		"c   
      C` s6  | j  d k rD | j d d k rD | d  d  … d  d  … d f } n  t | d d ƒ} | j d | j d f } t } |  j d k r¡ t } |  j | |  j ƒ n  t j	 | ƒ } t j
 | j d | ƒ } t j d | j d | ƒ} t j ƒ  } x0 | | | | g D] }	 |  j j |	 j ƒ  ƒ qW|  j d 7_ d  S(	   Ni   iÿÿÿÿi   i    t   bitdepthi   t   move(   i    i    (   t   ndimt   shapeR   R'   RV   R   R   RS   R   t	   BitmapTagt   ShapeTagt   idt   PlaceObjectTagt   ShowFrameTagR!   R_   R{   (
   R   RG   t   metat   wht   isfirstframet   bmt   sht   pot   sfR~   (    (    s2   lib/python2.7/site-packages/imageio/plugins/swf.pyt   _append_data.  s    ""c         C` s   d  S(   N(    (   R   R‹   (    (    s2   lib/python2.7/site-packages/imageio/plugins/swf.pyt   set_meta_dataD  s    (
   RP   RQ   R   R'   R=   R?   R   R]   R’   R“   (    (    (    s2   lib/python2.7/site-packages/imageio/plugins/swf.pyRR   à   s   			
	(   RP   RQ   t   __doc__R   R   R   R   RR   (    (    (    s2   lib/python2.7/site-packages/imageio/plugins/swf.pyR       s
   #		Žs   
<!DOCTYPE html>
<html>
<head>
    <title>Show Flash animation %s</title>
</head>
<body>
    <embed width="%i" height="%i" src="%s">
</html>
t   swfs   Shockwave flashs   .swft   I(   R”   t
   __future__R    R   R   Rc   R#   t   loggingt   ioR   t   numpyR@   R	   R   t   coreR   R   R   t	   getLoggerRP   RK   RE   R   R
   R   Ri   R<   t
   add_format(    (    (    s2   lib/python2.7/site-packages/imageio/plugins/swf.pyt   <module>   s(   	ÿ 2	