#!/usr/bin/python2.3

import SWF
SWF.verbose(0)
import Image
from math import sqrt,sin,cos

width = 512
height = 512

swf = SWF.create(version=6, bbox=(0,0,width,height), fps=25)
swf.tags += SWF.BackgroundColor(0,0,0)

stream = SWF.VideoStream(width=width, height=height, frames=65535)

startframe = stream.addBlackFrame()
swf.tags += startframe
swf.tags += SWF.PlaceObject(stream, depth=1, ratio=0)
swf.tags += SWF.ShowFrame()

line = [0]*stream.xblocks()
map = [line]*stream.yblocks()

import random

def mkPic(pos=0):
    rr=int(128+sin(pos/25.0+33)*127)
    gg=int(128+sin(pos/37.0+55)*127)
    bb=255-(rr+gg)/2
    rstr=""
    for y in xrange(0,8):
	xstr = ""
	for x in xrange(0,8): 
	    r = random.randint(0,rr)
	    g = random.randint(0,gg)
	    b = random.randint(0,bb) #255-(r+g)/2
	    for i in xrange(0,2):
		xstr+=str(chr(r))
		xstr+=str(chr(g))
		xstr+=str(chr(b))
	rstr += xstr+xstr
    return rstr

width = stream.xblocks()
height = stream.yblocks()
rstr=""
for i in xrange(1,1600):
    for y in xrange(0,height):
	line = [0]*stream.xblocks()
	for x in xrange(0,width):
	    mx = -x+width/2
	    my = -y+height/2
	    r = sqrt(mx*mx+my*my)
	    if r==0: 
		vx=vy=0
	    else: 
		if i>800:
		    vx = my/r
		    vy = -mx/r
		else:
		    vx = -my/r
		    vy = mx/r

	    mx/=2
	    my/=2
	    if mx>=0: mx+=1
	    if my>=0: my+=1
	    mx+= vx*8
	    my+= vy*8
	    line[x] = (mx+my*1j)
	map[y] = line

    x = random.randint(0,width/2)+width/4
    y = random.randint(0,height/2)+height/4
    if i%10==1:
	rstr=mkPic(i)
	map[width/2][height/2] = rstr
	map[width/2+1][height/2] = rstr
	map[width/2][height/2+1] = rstr
	map[width/2+1][height/2+1] = rstr

    dist = stream.addDistortionFrame(map)
    swf.tags += dist
    swf.tags += SWF.MoveObject(stream, depth=1, ratio=i)

    swf.tags += SWF.ShowFrame()

swf.save("tunnel_raw.swf")

