--- fract/mandelzoom.cgi 2001/02/25 05:49:07 1.2 +++ fract/mandelzoom.cgi 2001/06/26 12:43:34 1.10 @@ -1,21 +1,33 @@ #!/usr/bin/python -import cgi, Image, ImageDraw, sys, math +import cgi, Image, ImageDraw, sys, math, signal, errno, os from string import atoi, atof +def handler(signum, frame): + signal.alarm(0) + 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) else: - width= 570 + width= 480 if form.has_key('height'): height= atoi(form['height'].value) else: - height= 570 + height= 480 xmax, ymax = width-1, height-1 # Coordinate maximums @@ -23,17 +35,19 @@ if form.has_key('cx'): cx= atof(form['cx'].value) else: - cx= 0 + cx= 0.0 if form.has_key('cy'): cy= atof(form['cy'].value) else: - cy= 0 + cy= 0.0 +# The length of the diagonal line from corner to corner. +# Used as a measure of image size independent of aspect ratio. if form.has_key('diag'): diag= atof(form['diag'].value) else: - diag= math.sqrt(8) + diag= math.sqrt(32) # sqrt(4**2 + 4**2) debug= form.has_key('debug') @@ -42,19 +56,16 @@ else: maxiter= 270 -#print "Content-Type: text/plain" -#print "" - # 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

@@ -66,25 +77,39 @@ ds="&debug=on" else: ds="" + # If the user has selected a specific point on the image + if form.has_key('image.x') and form.has_key('image.y'): + # Adjust cx and cy + ix= atof(form['image.x'].value) # Intermediate value + iy= atof(form['image.y'].value) # Intermediate value + owidth= atof(form['owidth'].value) + oheight= atof(form['oheight'].value) + diagp= math.sqrt(owidth**2 + oheight**2) # Intermediate value + scale= diagp/diag # Intermediate value + cx= (ix/scale) + (cx - (owidth / (scale*2))) + cy= ((oheight-iy)/scale) + (cy - (oheight / (scale*2))) if form.has_key('zoom'): - zoom=atoi(form['zoom'].value) + zoom=atof(form['zoom'].value) diag=diag/zoom else: - # If no zoom provided, don't actually zoom - zoom= 2 + # If no zoom provided, don't actually zoom, but keep the + # default of ×2 zoom. + zoom= 2.0 print 'SRC="mandelzoom.cgi?type=image&width=%s&height=%s&iter=%s%s&diag=%s&cx=%s&cy=%s"' % (str(width), str(height), str(maxiter), ds, str(diag), str(cx), str(cy)), print 'ALIGN=BOTTOM HEIGHT="%s"' % (str(height)), print 'WIDTH="%s">

' % (str(width)) print 'Zoom: ' print """Width: @@ -92,27 +117,49 @@ VALUE="%s"> Iterations: - Debug mode: ' else: print '>' - for key in form.keys(): - if key not in ["zoom", "type", "debug", "image.x", "image.y", "iter", "height", "width"]: - print '' % (key, form[key].value) + print '' + for var in (("diag", diag), ("cx", cx), ("cy", cy), ("owidth", width), + ("oheight", height)): + print '' % var print '' - 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. """ + # Just exit, don't calculate an image. sys.exit(0) -c1, c2 = (-2+2j), (2-2j) # Corner coordinates +# c1 and c2 are the coordinates of two opposite corners. +# 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 +# half the width/height in real numbers; intermediate values +x= (width/2.0)/scale +y= (height/2.0)/scale + +# The coordinates of two opposite corners +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) # 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)), \ complex(max(c1.real, c2.real), min(c1.imag, c2.imag)) +# New image object mandel= Image.new("L", (width, height)) +# shortcut to the "Draw" method on the image object draw= ImageDraw.Draw(mandel) def drawrect((x1, y1), (x2, y2)): @@ -174,6 +221,7 @@ draw.rectangle([x1, y1m, x2, y2m], fill= color) def constant(x, y): + "Return complex number from pixel coordinates x and y." creal= (float(x)/xmax)*(c2.real - c1.real)+c1.real cimag= (float(y)/ymax)*(c2.imag - c1.imag)+c1.imag return complex(creal, cimag) @@ -218,7 +266,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