
    0-Ph-                         d 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	m
Z
mZ  ej        e          Zdad Z G d de	          Zd	ZdS )
a   Read/Write SWF files.

Backend: internal

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.

    N)BytesIO   )Formatread_n_bytesimage_as_uintc                      ddl ma t          S )N   )_swf) r
        S/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/imageio/plugins/swf.pyload_libr   <   s    Kr   c                   j    e Zd ZdZd Zd Z G d dej                  Z G d dej                  ZdS )		SWFFormatzSee :mod:`imageio.plugins.swf`c                 X    |j         dd                             dd          }|dv rdS d S )Nr      asciiignore)FWSCWST)
firstbytesdecode)selfrequesttmps      r   	_can_readzSWFFormat._can_readF   s;     1%,,Wh??.  4 ! r   c                 &    |j         | j        v rdS d S )NT)	extension
extensions)r   r   s     r   
_can_writezSWFFormat._can_writeK   s    //4 0/r   c                   :    e Zd Zd
dZd Zd Zd Zd Zd Zd Z	d	S )SWFFormat.ReaderFc                    t           st                       t          |          | _        | j                                        | _        | j        j        dd                             dd          }|dk    rnj|dk    rU| j        	                                }|d d         t          j        |dd                    z   }t          |          | _        nt          d          	 | j                            d           d	| _        n,# t           $ r d
| _        |                     d           Y nw xY wt                               |                     d          d          }d|dz  z   }|dz  }|dz  r|dz  }t'          |          }|                     |dz              g | _        | j        s	 	 |                                 \  }}}}	| j                                        }
|rNt/          |                     d          dd                    }|dk    r| j                            |
|||	f           | j                            |
|z              # t2          $ r Y d S w xY w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zlib
decompressr   IOErrorseek_streaming_mode	Exception_fp_readbits2intint_imlocs_read_one_tagtellordappend
IndexError)r   loopr   bbnbitsLrectisimageszeTL1locformats               r   _openzSWFFormat.Reader._openR   sq    


!$ZZDN|,,..DH ,)!A#.55gxHHCe||X]]__Vdobf555"2;;HIII!a   ',$$ ! ! !'+$a     ! MM$--"2"2A66E	MECKEqy 
JJEMM%!)$$$ DL' 1.2.@.@.B.B+a"hmmoo" G%(q)9)9!"")=%>%>F%{{ $ 3 3S#q"4E F F FcCi0001 "   DD s%   "!D &D-,D-+B!I 
IIc                 ,    t          | j        |          S N)r   r+   )r   ns     r   r3   zSWFFormat.Reader._fp_read   s    !,,,r   c                     d S rH   r   r   s    r   _closezSWFFormat.Reader._close       Dr   c                 P    | j         rt          j        S t          | j                  S rH   )r1   npinflenr6   rK   s    r   _get_lengthzSWFFormat.Reader._get_length   s$    # )v4<(((r   c                 N   |dk     rt          d          | j        sL| j        r| j        r|t	          | j                  z  }|t	          | j                  k    rt          d          | j        rU	 |                                 \  }}}}|                     |          }|r#t                              |d||          }||i fS T| j        |         \  }}}}| j	        
                    |           |                     |          }t                              |d||          }|i fS )Nr   zIndex in swf file must be > 0zIndex out of bounds)r;   r1   r)   r6   rQ   r7   r3   r
   read_pixelsr+   r0   )	r   indexr@   rA   rB   rC   r=   imrD   s	            r   	_get_datazSWFFormat.Reader._get_data   s<   qyy !@AAA' <> 6dl 6!C$5$55EC----$%:;;;# **.*<*<*>*>'GS!Rs++B *!--b!Q;;>#%r6M* #',u"5S!Rc"""]]3''%%b!Q332vr   c                 >   |                      d          }|st          d          t                              |          \  }}}|st	          d          d}|dz
  }|dk    rt          d          |dv rd}n |dv rt
                              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)rY      #   Z   z&Ignoring JPEG image: cannot read JPEG.)r3   r;   r
   get_type_and_lenRuntimeErrorloggerwarning)r   headrB   rC   L2r@   rA   s          r   r7   zSWFFormat.Reader._read_one_tag   s     ==##D = !;<<< --d33IAr2 L"#JKKK Gq&C Avv !;<<<ho%%GHHHH CB&&r   c                     i S rH   r   )r   rU   s     r   _get_meta_datazSWFFormat.Reader._get_meta_data   s    Ir   N)F)
__name__
__module____qualname__rF   r3   rL   rR   rW   r7   rf   r   r   r   Readerr#   Q   s        9	 9	 9	 9	v	- 	- 	-	 	 		) 	) 	)	 	 	:"	' "	' "	'H	 	 	 	 	r   rj   c                   4    e Zd ZddZd Zd Zd Zd Zd	 Zd
S )SWFFormat.Writer   TFc                 x   t           st                       t          |          | _        t	          |          | _        t	          |          | _        t	          |          | _        | j        	                                | _
        d| _        d| _        | j        r!| j
        | _        t                      | _
        d S d S )Nr   )d   ro   )r
   r   r5   _arg_fpsr(   r)   	_arg_html_arg_compressr   r*   r+   _framecounter
_framesize_fp_realr   )r   fpsr<   htmlcompresss        r   rF   zSWFFormat.Writer._open   s     


HHDM!$ZZDN!$ZZDN!%hD|,,..DH!"D(DO ! % $"99% %r   c                    |                                   | j                                        }| j                            | j                   | j                            t                              | j                             | j	        r| j        
                                }| j        | _        | j                            |d d                    | j                            t          j        |dd                               | j                                        }| j                            d           | j                            t                              |                     d | _        | j        rt           j                            | j        j                  rt           j                            | j        j                  \  }}t           j                            ||d d         dz             }| j        \  }}t0          ||||fz  }t3          |d          5 }	|	                    |                    d                     d d d            d S # 1 swxY w Y   d S d S d S )Nr%   r'   z.htmlwbzutf-8)	_completer+   r8   r0   _location_to_save_nframeswriter
   
int2uint16rs   rr   getvalueru   r-   rx   
int2uint32rq   ospathisfiler   filenamesplitjoinrt   HTMLopenencode)
r   rA   r=   dirnamefnamer   whrw   fs
             r   rL   zSWFFormat.Writer._close   s3   NN(--//CHMM$8999HNN4??4+=>>???! &X&&((=r"1"v&&&t}RV44555hmmooHMM!HNN4??3//000DH ~ 2"'..1F"G"G 2!#t|/D!E!E7<<ssg1EFF1uaE22(D)) 2QGGDKK001112 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 22 2 2 2s   !)IIIc                 `   || _         d}|d| j                                     d          z  }|d                    d          z  }|t                              d          z  }|d                    d          z  }|t                                                              d|d         d|d                                                   z  }|t                              d          t                              |          z   z  }t          |          | _	        |d	                    d          z  }| j
                            |           t                                          t                              ddd          f}|D ].}| j
                            |                                           /d S )
Nr   FCr   WSr%   0000r   r	   00)rt   rr   r   r
   	int2uint8Tagmake_rect_recordtobytesrQ   r}   r+   r~   FileAttributesTagSetBackgroundTagget_tag)r   	framesizerv   r=   taglisttags         r   _write_headerzSWFFormat.Writer._write_header  sp   'DOB$t)*11':::B$++g&&&B$..###B&--(((B

++Ay|Q	!MMUUWWB $..##dnnS&9&999B-0WWD*$++g&&&BHNN2 ,,..0E0EaA0N0NNG . .s{{}}----. .r   c                 :   | j         s|                     d| j                   | j        sD| j                            t                              d                                                     | j                            d	                    d                     d S )N)
   r   stopz  r   )
rs   r   rp   r)   r+   r~   r
   DoActionTagr   r   rK   s    r   r|   zSWFFormat.Writer._complete#  s    % <""8T];;;> Ct//77??AABBBHNN:,,W5566666r   c                 ~   |j         dk    r |j        d         dk    r|d d d d df         }t          |d          }|j        d         |j        d         f}d}| j        dk    rd}|                     || j                   t                              |          }t                              |j	        d	|          }t          
                    d|j	        | 
          }t                                          }||||fD ].}	| j                            |	                                           /| xj        dz  c_        d S )Nr   r	   r   r%   )bitdepthFT)r   r   )move)ndimshaper   rs   r   rp   r
   	BitmapTagShapeTagidPlaceObjectTagShowFrameTagr+   r~   r   )
r   rV   metawhisfirstframebmshposfr   s
             r   _append_datazSWFFormat.Writer._append_data-  s5   w!|| 1 1111a[rA...B!bhqk)B L!Q&&#""2t}555##Brufb11B$$Q5E$GGB""$$BBB' . .s{{}}----!#r   c                     d S rH   r   )r   r   s     r   set_meta_datazSWFFormat.Writer.set_meta_dataC  rM   r   N)rm   TFF)	rg   rh   ri   rF   rL   r   r|   r   r   r   r   r   Writerrl      ss        	% 	% 	% 	%$	2 	2 	28	. 	. 	.*	7 	7 	7	$ 	$ 	$,	 	 	 	 	r   r   N)	rg   rh   ri   __doc__r   r!   r   rj   r   r   r   r   r   r   C   s        ((  
  J J J J J J J J\e e e e e e e e e er   r   z
<!DOCTYPE html>
<html>
<head>
    <title>Show Flash animation %s</title>
</head>
<body>
    <embed width="%i" height="%i" src="%s">
</html>
)r   r   r-   loggingior   numpyrO   corer   r   r   	getLoggerrg   ra   r
   r   r   r   r   r   r   <module>r      s   ' 'R 
			             6 6 6 6 6 6 6 6 6 6 
	8	$	$  A A A A A A A AH	r   