--- fract/mandelzoom.cgi 2001/02/24 21:36:47 1.1 +++ fract/mandelzoom.cgi 2001/06/19 22:27:27 1.9 @@ -1,64 +1,149 @@ #!/usr/bin/python -import cgi, Image, ImageDraw, sys -from string import atoi +import cgi, Image, ImageDraw, sys, math, signal, errno, os +from string import atoi, atof + +def handler(signum, frame): + raise os.error, (errno.ETIME, "Timer expired") + +signal.alarm(55) +signal.signal(signal.SIGALRM, handler) + +# This is to get backtrace output +sys.stderr = sys.stdout + +# Uncomment this to get the backtrace more readable +#print "Content-Type: text/plain" +#print "" form= cgi.FieldStorage() # Image size if form.has_key('width'): - width=atoi(form['width'].value) + width= atoi(form['width'].value) else: - width= 570 + width= 480 if form.has_key('height'): - height=atoi(form['height'].value) + height= atoi(form['height'].value) else: - height= 570 + height= 480 xmax, ymax = width-1, height-1 # Coordinate maximums +# Complex number in center of image is (cx+cy*j) +if form.has_key('cx'): + cx= atof(form['cx'].value) +else: + cx= 0.0 + +if form.has_key('cy'): + cy= atof(form['cy'].value) +else: + cy= 0.0 + +if form.has_key('diag'): + diag= atof(form['diag'].value) +else: + diag= math.sqrt(32) # sqrt(4**2 + 4**2) + debug= form.has_key('debug') -if debug: - del form['debug'] if form.has_key('iter'): - maxiter=atoi(form['iter'].value) - del form['iter'] + maxiter= atoi(form['iter'].value) else: maxiter= 270 -sys.stderr = sys.stdout -print "Content-Type: text/plain" -print "" - -# If type==html, then output an HTML page, not an image -if form.has_key('type') and form['type'].value == "html": - del form['type'] +# If type!=image, then output an HTML page, not an image +if not (form.has_key('type') and form['type'].value == "image"): print """Content-Type: text/html -Mandel Set Zoomer +Mandelbrot Set Zoomer -

Mandel Set Zoomer

+

Mandelbrot Set Zoomer

- - +""" + print '

' % (str(width)) + print 'Zoom: ' + print """Width: + Height: Iterations: - Debug mode: """ - for key in form.keys(): - print '' % (key, form[key].value) - - print """

+ Debug mode: ' + else: + print '>' + print '' + for var in (("diag", diag), ("cx", cx), ("cy", cy), ("owidth", width), + ("oheight", height)): + print '' % var + print '' + + print """

+After changing any settings, don't forget to change the "Zoom" setting +to "Pan" if you don't want to zoom when applying them. """ sys.exit(0) -c1, c2 = (-2+2j), (2-2j) # Corner coordinates +# Figure out c1 and c2 from width, height, diag, cx, and cy. +# Diagonal in pixels +diagp= math.sqrt(width**2 + height**2) +# Scale between pixels and coordinates +scale= diagp/diag +x= (width/2.0)/scale +y= (height/2.0)/scale + +c1= cx - x + (cy - y) * (0+1j) +c2= cx + x + (cy + y) * (0+1j) + +#print width, height, diag, cx, cy, diagp, scale, x, y, c1, c2 +#sys.exit(0) + +#c1, c2 = (-2+2j), (2-2j) # Corner coordinates # Force c1 to be upper left and c2 to be lower right c1, c2= complex(min(c1.real, c2.real), max(c1.imag, c2.imag)), \ @@ -170,7 +255,13 @@ else: yto= yorig # End at x axis -drawrect((0, yfrom), (xmax, yto)) +try: + drawrect((0, yfrom), (xmax, yto)) +except os.error, the_error: + if the_error[0] != errno.ETIME: + raise os.error, the_error + +signal.alarm(0) print "Content-type: image/png" print