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 |
|
raise os.error, (errno.ETIME, "Timer expired") |
8 |
|
|
9 |
|
signal.alarm(55) |
10 |
|
signal.signal(signal.SIGALRM, handler) |
11 |
|
|
12 |
|
# This is to get backtrace output |
13 |
sys.stderr = sys.stdout |
sys.stderr = sys.stdout |
14 |
|
|
15 |
|
# Uncomment this to get the backtrace more readable |
16 |
|
#print "Content-Type: text/plain" |
17 |
|
#print "" |
18 |
|
|
19 |
form= cgi.FieldStorage() |
form= cgi.FieldStorage() |
20 |
|
|
21 |
# Image size |
# Image size |
22 |
if form.has_key('width'): |
if form.has_key('width'): |
23 |
width= atoi(form['width'].value) |
width= atoi(form['width'].value) |
24 |
else: |
else: |
25 |
width= 570 |
width= 480 |
26 |
if form.has_key('height'): |
if form.has_key('height'): |
27 |
height= atoi(form['height'].value) |
height= atoi(form['height'].value) |
28 |
else: |
else: |
29 |
height= 570 |
height= 480 |
30 |
|
|
31 |
xmax, ymax = width-1, height-1 # Coordinate maximums |
xmax, ymax = width-1, height-1 # Coordinate maximums |
32 |
|
|
34 |
if form.has_key('cx'): |
if form.has_key('cx'): |
35 |
cx= atof(form['cx'].value) |
cx= atof(form['cx'].value) |
36 |
else: |
else: |
37 |
cx= 0 |
cx= 0.0 |
38 |
|
|
39 |
if form.has_key('cy'): |
if form.has_key('cy'): |
40 |
cy= atof(form['cy'].value) |
cy= atof(form['cy'].value) |
41 |
else: |
else: |
42 |
cy= 0 |
cy= 0.0 |
43 |
|
|
44 |
if form.has_key('diag'): |
if form.has_key('diag'): |
45 |
diag= atof(form['diag'].value) |
diag= atof(form['diag'].value) |
46 |
else: |
else: |
47 |
diag= math.sqrt(8) |
diag= math.sqrt(32) # sqrt(4**2 + 4**2) |
48 |
|
|
49 |
debug= form.has_key('debug') |
debug= form.has_key('debug') |
50 |
|
|
53 |
else: |
else: |
54 |
maxiter= 270 |
maxiter= 270 |
55 |
|
|
|
#print "Content-Type: text/plain" |
|
|
#print "" |
|
|
|
|
56 |
# If type!=image, then output an HTML page, not an image |
# If type!=image, then output an HTML page, not an image |
57 |
if not (form.has_key('type') and form['type'].value == "image"): |
if not (form.has_key('type') and form['type'].value == "image"): |
58 |
print """Content-Type: text/html |
print """Content-Type: text/html |
59 |
|
|
60 |
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> |
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> |
61 |
<HTML><HEAD> |
<HTML><HEAD> |
62 |
<TITLE>Mandel Set Zoomer</TITLE> |
<TITLE>Mandelbrot Set Zoomer</TITLE> |
63 |
</HEAD> |
</HEAD> |
64 |
<BODY> |
<BODY> |
65 |
<H1>Mandel Set Zoomer</H1> |
<H1>Mandelbrot Set Zoomer</H1> |
66 |
|
|
67 |
<FORM ACTION="mandelzoom.cgi" METHOD=GET> |
<FORM ACTION="mandelzoom.cgi" METHOD=GET> |
68 |
|
|
74 |
ds="&debug=on" |
ds="&debug=on" |
75 |
else: |
else: |
76 |
ds="" |
ds="" |
77 |
|
if form.has_key('image.x') and form.has_key('image.y'): |
78 |
|
# Adjust cx and cy |
79 |
|
ix= atof(form['image.x'].value) |
80 |
|
iy= atof(form['image.y'].value) |
81 |
|
owidth= atof(form['owidth'].value) |
82 |
|
oheight= atof(form['oheight'].value) |
83 |
|
diagp= math.sqrt(owidth**2 + oheight**2) |
84 |
|
scale= diagp/diag |
85 |
|
cx= (ix/scale) + (cx - (owidth / (scale*2))) |
86 |
|
cy= ((oheight-iy)/scale) + (cy - (oheight / (scale*2))) |
87 |
if form.has_key('zoom'): |
if form.has_key('zoom'): |
88 |
zoom=atoi(form['zoom'].value) |
zoom=atof(form['zoom'].value) |
89 |
diag=diag/zoom |
diag=diag/zoom |
90 |
else: |
else: |
91 |
# If no zoom provided, don't actually zoom |
# If no zoom provided, don't actually zoom |
92 |
zoom= 2 |
zoom= 2.0 |
93 |
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)), |
94 |
print 'ALIGN=BOTTOM HEIGHT="%s"' % (str(height)), |
print 'ALIGN=BOTTOM HEIGHT="%s"' % (str(height)), |
95 |
print 'WIDTH="%s"><P>' % (str(width)) |
print 'WIDTH="%s"><P>' % (str(width)) |
96 |
print 'Zoom: <SELECT NAME="zoom">' |
print 'Zoom: <SELECT NAME="zoom">' |
97 |
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]: |
98 |
print '<OPTION', |
print '<OPTION', |
99 |
if zv==zoom: |
if str(zv)==str(zoom): |
100 |
print 'SELECTED', |
print 'SELECTED', |
101 |
print 'VALUE="%s"' % (str(zv)) |
print 'VALUE="%s"' % (str(zv)) |
102 |
if zv < 0: |
if zv == 1: |
103 |
print '>÷%s' % (str(-zv)) |
print '>Pan' |
104 |
|
elif zv < 1: |
105 |
|
print '>Out ÷%s' % (str(1/zv)) |
106 |
else: |
else: |
107 |
print '>×%s' % (str(zv)) |
print '>In ×%s' % (str(zv)) |
108 |
print '</SELECT>' |
print '</SELECT>' |
109 |
print """Width: <INPUT TYPE=TEXT NAME="width" MAXLENGTH="4" SIZE="3" |
print """Width: <INPUT TYPE=TEXT NAME="width" MAXLENGTH="4" SIZE="3" |
110 |
VALUE="%s"> |
VALUE="%s"> |
112 |
VALUE="%s"> |
VALUE="%s"> |
113 |
Iterations: <INPUT TYPE=TEXT NAME="iter" MAXLENGTH="4" SIZE="3" |
Iterations: <INPUT TYPE=TEXT NAME="iter" MAXLENGTH="4" SIZE="3" |
114 |
VALUE="%s"> |
VALUE="%s"> |
115 |
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)), |
116 |
if debug: |
if debug: |
117 |
print 'CHECKED>' |
print 'CHECKED>' |
118 |
else: |
else: |
119 |
print '>' |
print '>' |
120 |
for key in form.keys(): |
print '<INPUT TYPE=SUBMIT VALUE="Apply">' |
121 |
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), |
122 |
print '<INPUT TYPE=HIDDEN NAME="%s" VALUE="%s">' % (key, form[key].value) |
("oheight", height)): |
123 |
|
print '<INPUT TYPE=HIDDEN NAME="%s" VALUE="%s">' % var |
124 |
print '<INPUT TYPE=HIDDEN NAME=type VALUE="html">' |
print '<INPUT TYPE=HIDDEN NAME=type VALUE="html">' |
125 |
|
|
126 |
print """</FORM> |
print """</FORM><P> |
127 |
|
After changing any settings, don't forget to change the "Zoom" setting |
128 |
|
to "Pan" if you don't want to zoom when applying them. |
129 |
</BODY></HTML>""" |
</BODY></HTML>""" |
130 |
sys.exit(0) |
sys.exit(0) |
131 |
|
|
132 |
c1, c2 = (-2+2j), (2-2j) # Corner coordinates |
# Figure out c1 and c2 from width, height, diag, cx, and cy. |
133 |
|
# Diagonal in pixels |
134 |
|
diagp= math.sqrt(width**2 + height**2) |
135 |
|
# Scale between pixels and coordinates |
136 |
|
scale= diagp/diag |
137 |
|
x= (width/2.0)/scale |
138 |
|
y= (height/2.0)/scale |
139 |
|
|
140 |
|
c1= cx - x + (cy - y) * (0+1j) |
141 |
|
c2= cx + x + (cy + y) * (0+1j) |
142 |
|
|
143 |
|
#print width, height, diag, cx, cy, diagp, scale, x, y, c1, c2 |
144 |
|
#sys.exit(0) |
145 |
|
|
146 |
|
#c1, c2 = (-2+2j), (2-2j) # Corner coordinates |
147 |
|
|
148 |
# Force c1 to be upper left and c2 to be lower right |
# Force c1 to be upper left and c2 to be lower right |
149 |
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)), \ |
255 |
else: |
else: |
256 |
yto= yorig # End at x axis |
yto= yorig # End at x axis |
257 |
|
|
258 |
drawrect((0, yfrom), (xmax, yto)) |
try: |
259 |
|
drawrect((0, yfrom), (xmax, yto)) |
260 |
|
except os.error, the_error: |
261 |
|
if the_error[0] != errno.ETIME: |
262 |
|
raise os.error, the_error |
263 |
|
|
264 |
|
signal.alarm(0) |
265 |
|
|
266 |
print "Content-type: image/png" |
print "Content-type: image/png" |
267 |
print |
print |