1 |
#!/usr/bin/python |
#!/usr/bin/python |
2 |
|
|
3 |
import cgi, Image, ImageDraw, sys, math |
import cgi, Image, ImageDraw, sys, math, signal, errno, os |
4 |
from string import atoi, atof |
from string import atoi, atof |
5 |
|
|
6 |
|
def handler(signum, frame): |
7 |
|
signal.alarm(0) |
8 |
|
raise os.error, (errno.ETIME, "Timer expired") |
9 |
|
|
10 |
|
signal.alarm(55) |
11 |
|
signal.signal(signal.SIGALRM, handler) |
12 |
|
|
13 |
|
# This is to get backtrace output |
14 |
sys.stderr = sys.stdout |
sys.stderr = sys.stdout |
15 |
|
|
16 |
|
# Uncomment this to get the backtrace more readable |
17 |
|
#print "Content-Type: text/plain" |
18 |
|
#print "" |
19 |
|
|
20 |
form= cgi.FieldStorage() |
form= cgi.FieldStorage() |
21 |
|
|
22 |
# Image size |
# Image size |
23 |
if form.has_key('width'): |
if form.has_key('width'): |
24 |
width= atoi(form['width'].value) |
width= atoi(form['width'].value) |
25 |
else: |
else: |
26 |
width= 570 |
width= 480 |
27 |
if form.has_key('height'): |
if form.has_key('height'): |
28 |
height= atoi(form['height'].value) |
height= atoi(form['height'].value) |
29 |
else: |
else: |
30 |
height= 570 |
height= 480 |
31 |
|
|
32 |
xmax, ymax = width-1, height-1 # Coordinate maximums |
xmax, ymax = width-1, height-1 # Coordinate maximums |
33 |
|
|
35 |
if form.has_key('cx'): |
if form.has_key('cx'): |
36 |
cx= atof(form['cx'].value) |
cx= atof(form['cx'].value) |
37 |
else: |
else: |
38 |
cx= 0 |
cx= 0.0 |
39 |
|
|
40 |
if form.has_key('cy'): |
if form.has_key('cy'): |
41 |
cy= atof(form['cy'].value) |
cy= atof(form['cy'].value) |
42 |
else: |
else: |
43 |
cy= 0 |
cy= 0.0 |
44 |
|
|
45 |
|
# The length of the diagonal line from corner to corner. |
46 |
|
# Used as a measure of image size independent of aspect ratio. |
47 |
if form.has_key('diag'): |
if form.has_key('diag'): |
48 |
diag= atof(form['diag'].value) |
diag= atof(form['diag'].value) |
49 |
else: |
else: |
50 |
diag= math.sqrt(8) |
diag= math.sqrt(32) # sqrt(4**2 + 4**2) |
51 |
|
|
52 |
debug= form.has_key('debug') |
debug= form.has_key('debug') |
53 |
|
|
56 |
else: |
else: |
57 |
maxiter= 270 |
maxiter= 270 |
58 |
|
|
|
#print "Content-Type: text/plain" |
|
|
#print "" |
|
|
|
|
59 |
# If type!=image, then output an HTML page, not an image |
# If type!=image, then output an HTML page, not an image |
60 |
if not (form.has_key('type') and form['type'].value == "image"): |
if not (form.has_key('type') and form['type'].value == "image"): |
61 |
print """Content-Type: text/html |
print """Content-Type: text/html |
62 |
|
|
63 |
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> |
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> |
64 |
<HTML><HEAD> |
<HTML><HEAD> |
65 |
<TITLE>Mandel Set Zoomer</TITLE> |
<TITLE>Mandelbrot Set Zoomer</TITLE> |
66 |
</HEAD> |
</HEAD> |
67 |
<BODY> |
<BODY> |
68 |
<H1>Mandel Set Zoomer</H1> |
<H1>Mandelbrot Set Zoomer</H1> |
69 |
|
|
70 |
<FORM ACTION="mandelzoom.cgi" METHOD=GET> |
<FORM ACTION="mandelzoom.cgi" METHOD=GET> |
71 |
|
|
77 |
ds="&debug=on" |
ds="&debug=on" |
78 |
else: |
else: |
79 |
ds="" |
ds="" |
80 |
|
# If the user has selected a specific point on the image |
81 |
|
if form.has_key('image.x') and form.has_key('image.y'): |
82 |
|
# Adjust cx and cy |
83 |
|
ix= atof(form['image.x'].value) # Intermediate value |
84 |
|
iy= atof(form['image.y'].value) # Intermediate value |
85 |
|
owidth= atof(form['owidth'].value) |
86 |
|
oheight= atof(form['oheight'].value) |
87 |
|
diagp= math.sqrt(owidth**2 + oheight**2) # Intermediate value |
88 |
|
scale= diagp/diag # Intermediate value |
89 |
|
cx= (ix/scale) + (cx - (owidth / (scale*2))) |
90 |
|
cy= ((oheight-iy)/scale) + (cy - (oheight / (scale*2))) |
91 |
if form.has_key('zoom'): |
if form.has_key('zoom'): |
92 |
zoom=atoi(form['zoom'].value) |
zoom=atof(form['zoom'].value) |
93 |
diag=diag/zoom |
diag=diag/zoom |
94 |
else: |
else: |
95 |
# If no zoom provided, don't actually zoom |
# If no zoom provided, don't actually zoom, but keep the |
96 |
zoom= 2 |
# default of ×2 zoom. |
97 |
|
zoom= 2.0 |
98 |
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 '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)), |
99 |
print 'ALIGN=BOTTOM HEIGHT="%s"' % (str(height)), |
print 'ALIGN=BOTTOM HEIGHT="%s"' % (str(height)), |
100 |
print 'WIDTH="%s"><P>' % (str(width)) |
print 'WIDTH="%s"><P>' % (str(width)) |
101 |
print 'Zoom: <SELECT NAME="zoom">' |
print 'Zoom: <SELECT NAME="zoom">' |
102 |
for zv in [-5, -3, -2, 1, 2, 3, 5]: |
for zv in [1.0/5, 1.0/3, 1.0/2, 1.0/1.5, 1.0, 1.5, 2.0, 3.0, 5.0]: |
103 |
print '<OPTION', |
print '<OPTION', |
104 |
if zv==zoom: |
if str(zv)==str(zoom): |
105 |
print 'SELECTED', |
print 'SELECTED', |
106 |
print 'VALUE="%s"' % (str(zv)) |
print 'VALUE="%s"' % (str(zv)) |
107 |
if zv < 0: |
if zv == 1: |
108 |
print '>÷%s' % (str(-zv)) |
print '>Pan' |
109 |
|
elif zv < 1: |
110 |
|
print '>Out ÷%s' % (str(1/zv)) |
111 |
else: |
else: |
112 |
print '>×%s' % (str(zv)) |
print '>In ×%s' % (str(zv)) |
113 |
print '</SELECT>' |
print '</SELECT>' |
114 |
print """Width: <INPUT TYPE=TEXT NAME="width" MAXLENGTH="4" SIZE="3" |
print """Width: <INPUT TYPE=TEXT NAME="width" MAXLENGTH="4" SIZE="3" |
115 |
VALUE="%s"> |
VALUE="%s"> |
117 |
VALUE="%s"> |
VALUE="%s"> |
118 |
Iterations: <INPUT TYPE=TEXT NAME="iter" MAXLENGTH="4" SIZE="3" |
Iterations: <INPUT TYPE=TEXT NAME="iter" MAXLENGTH="4" SIZE="3" |
119 |
VALUE="%s"> |
VALUE="%s"> |
120 |
Debug mode: <INPUT TYPE=CHECKBOX NAME="debug" """ % (str(width), str(height), str(maxiter)) |
Debug mode: <INPUT TYPE=CHECKBOX NAME="debug" """ % (str(width), str(height), str(maxiter)), |
121 |
if debug: |
if debug: |
122 |
print 'CHECKED>' |
print 'CHECKED>' |
123 |
else: |
else: |
124 |
print '>' |
print '>' |
125 |
for key in form.keys(): |
print '<INPUT TYPE=SUBMIT VALUE="Apply">' |
126 |
if key not in ["zoom", "type", "debug", "image.x", "image.y", "iter", "height", "width"]: |
for var in (("diag", diag), ("cx", cx), ("cy", cy), ("owidth", width), |
127 |
print '<INPUT TYPE=HIDDEN NAME="%s" VALUE="%s">' % (key, form[key].value) |
("oheight", height)): |
128 |
|
print '<INPUT TYPE=HIDDEN NAME="%s" VALUE="%s">' % var |
129 |
print '<INPUT TYPE=HIDDEN NAME=type VALUE="html">' |
print '<INPUT TYPE=HIDDEN NAME=type VALUE="html">' |
130 |
|
|
131 |
print """</FORM> |
print """</FORM><P> |
132 |
|
After changing any settings, don't forget to change the "Zoom" setting |
133 |
|
to "Pan" if you don't want to zoom when applying them. |
134 |
</BODY></HTML>""" |
</BODY></HTML>""" |
135 |
|
# Just exit, don't calculate an image. |
136 |
sys.exit(0) |
sys.exit(0) |
137 |
|
|
138 |
c1, c2 = (-2+2j), (2-2j) # Corner coordinates |
# c1 and c2 are the coordinates of two opposite corners. |
139 |
|
# Figure out c1 and c2 from width, height, diag, cx, and cy. |
140 |
|
|
141 |
|
# Diagonal in pixels |
142 |
|
diagp= math.sqrt(width**2 + height**2) |
143 |
|
# Scale between pixels and coordinates |
144 |
|
scale= diagp/diag |
145 |
|
# half the width/height in real numbers; intermediate values |
146 |
|
x= (width/2.0)/scale |
147 |
|
y= (height/2.0)/scale |
148 |
|
|
149 |
|
# The coordinates of two opposite corners |
150 |
|
c1= cx - x + (cy - y) * (0+1j) |
151 |
|
c2= cx + x + (cy + y) * (0+1j) |
152 |
|
|
153 |
|
#print width, height, diag, cx, cy, diagp, scale, x, y, c1, c2 |
154 |
|
#sys.exit(0) |
155 |
|
|
156 |
# Force c1 to be upper left and c2 to be lower right |
# Force c1 to be upper left and c2 to be lower right |
157 |
c1, c2= complex(min(c1.real, c2.real), max(c1.imag, c2.imag)), \ |
c1, c2= complex(min(c1.real, c2.real), max(c1.imag, c2.imag)), \ |
158 |
complex(max(c1.real, c2.real), min(c1.imag, c2.imag)) |
complex(max(c1.real, c2.real), min(c1.imag, c2.imag)) |
159 |
|
|
160 |
|
# New image object |
161 |
mandel= Image.new("L", (width, height)) |
mandel= Image.new("L", (width, height)) |
162 |
|
# shortcut to the "Draw" method on the image object |
163 |
draw= ImageDraw.Draw(mandel) |
draw= ImageDraw.Draw(mandel) |
164 |
|
|
165 |
def drawrect((x1, y1), (x2, y2)): |
def drawrect((x1, y1), (x2, y2)): |
221 |
draw.rectangle([x1, y1m, x2, y2m], fill= color) |
draw.rectangle([x1, y1m, x2, y2m], fill= color) |
222 |
|
|
223 |
def constant(x, y): |
def constant(x, y): |
224 |
|
"Return complex number from pixel coordinates x and y." |
225 |
creal= (float(x)/xmax)*(c2.real - c1.real)+c1.real |
creal= (float(x)/xmax)*(c2.real - c1.real)+c1.real |
226 |
cimag= (float(y)/ymax)*(c2.imag - c1.imag)+c1.imag |
cimag= (float(y)/ymax)*(c2.imag - c1.imag)+c1.imag |
227 |
return complex(creal, cimag) |
return complex(creal, cimag) |
266 |
else: |
else: |
267 |
yto= yorig # End at x axis |
yto= yorig # End at x axis |
268 |
|
|
269 |
drawrect((0, yfrom), (xmax, yto)) |
try: |
270 |
|
drawrect((0, yfrom), (xmax, yto)) |
271 |
|
except os.error, the_error: |
272 |
|
if the_error[0] != errno.ETIME: |
273 |
|
raise os.error, the_error |
274 |
|
|
275 |
|
signal.alarm(0) |
276 |
|
|
277 |
print "Content-type: image/png" |
print "Content-type: image/png" |
278 |
print |
print |