3 |
import cgi, Image, ImageDraw, sys, math |
import cgi, Image, ImageDraw, sys, math |
4 |
from string import atoi, atof |
from string import atoi, atof |
5 |
|
|
6 |
|
# This is to get backtrace output |
7 |
sys.stderr = sys.stdout |
sys.stderr = sys.stdout |
8 |
|
|
9 |
|
# Uncomment this to get the backtrace more readable |
10 |
|
#print "Content-Type: text/plain" |
11 |
|
#print "" |
12 |
|
|
13 |
form= cgi.FieldStorage() |
form= cgi.FieldStorage() |
14 |
|
|
15 |
# Image size |
# Image size |
16 |
if form.has_key('width'): |
if form.has_key('width'): |
17 |
width= atoi(form['width'].value) |
width= atoi(form['width'].value) |
18 |
else: |
else: |
19 |
width= 570 |
width= 480 |
20 |
if form.has_key('height'): |
if form.has_key('height'): |
21 |
height= atoi(form['height'].value) |
height= atoi(form['height'].value) |
22 |
else: |
else: |
23 |
height= 570 |
height= 480 |
24 |
|
|
25 |
xmax, ymax = width-1, height-1 # Coordinate maximums |
xmax, ymax = width-1, height-1 # Coordinate maximums |
26 |
|
|
28 |
if form.has_key('cx'): |
if form.has_key('cx'): |
29 |
cx= atof(form['cx'].value) |
cx= atof(form['cx'].value) |
30 |
else: |
else: |
31 |
cx= 0 |
cx= 0.0 |
32 |
|
|
33 |
if form.has_key('cy'): |
if form.has_key('cy'): |
34 |
cy= atof(form['cy'].value) |
cy= atof(form['cy'].value) |
35 |
else: |
else: |
36 |
cy= 0 |
cy= 0.0 |
37 |
|
|
38 |
if form.has_key('diag'): |
if form.has_key('diag'): |
39 |
diag= atof(form['diag'].value) |
diag= atof(form['diag'].value) |
40 |
else: |
else: |
41 |
diag= math.sqrt(8) |
diag= math.sqrt(32) # sqrt(4**2 + 4**2) |
42 |
|
|
43 |
debug= form.has_key('debug') |
debug= form.has_key('debug') |
44 |
|
|
47 |
else: |
else: |
48 |
maxiter= 270 |
maxiter= 270 |
49 |
|
|
|
#print "Content-Type: text/plain" |
|
|
#print "" |
|
|
|
|
50 |
# If type!=image, then output an HTML page, not an image |
# If type!=image, then output an HTML page, not an image |
51 |
if not (form.has_key('type') and form['type'].value == "image"): |
if not (form.has_key('type') and form['type'].value == "image"): |
52 |
print """Content-Type: text/html |
print """Content-Type: text/html |
53 |
|
|
54 |
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> |
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> |
55 |
<HTML><HEAD> |
<HTML><HEAD> |
56 |
<TITLE>Mandel Set Zoomer</TITLE> |
<TITLE>Mandelbrot Set Zoomer</TITLE> |
57 |
</HEAD> |
</HEAD> |
58 |
<BODY> |
<BODY> |
59 |
<H1>Mandel Set Zoomer</H1> |
<H1>Mandelbrot Set Zoomer</H1> |
60 |
|
|
61 |
<FORM ACTION="mandelzoom.cgi" METHOD=GET> |
<FORM ACTION="mandelzoom.cgi" METHOD=GET> |
62 |
|
|
68 |
ds="&debug=on" |
ds="&debug=on" |
69 |
else: |
else: |
70 |
ds="" |
ds="" |
71 |
|
if form.has_key('image.x') and form.has_key('image.y'): |
72 |
|
# Adjust cx and cy |
73 |
|
ix= atof(form['image.x'].value) |
74 |
|
iy= atof(form['image.y'].value) |
75 |
|
owidth= atof(form['owidth'].value) |
76 |
|
oheight= atof(form['oheight'].value) |
77 |
|
diagp= math.sqrt(owidth**2 + oheight**2) |
78 |
|
scale= diagp/diag |
79 |
|
cx= (ix/scale) + (cx - (owidth / (scale*2))) |
80 |
|
cy= ((oheight-iy)/scale) + (cy - (oheight / (scale*2))) |
81 |
if form.has_key('zoom'): |
if form.has_key('zoom'): |
82 |
zoom=atoi(form['zoom'].value) |
zoom=atof(form['zoom'].value) |
83 |
diag=diag/zoom |
diag=diag/zoom |
84 |
else: |
else: |
85 |
# If no zoom provided, don't actually zoom |
# If no zoom provided, don't actually zoom |
86 |
zoom= 2 |
zoom= 2.0 |
87 |
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)), |
88 |
print 'ALIGN=BOTTOM HEIGHT="%s"' % (str(height)), |
print 'ALIGN=BOTTOM HEIGHT="%s"' % (str(height)), |
89 |
print 'WIDTH="%s"><P>' % (str(width)) |
print 'WIDTH="%s"><P>' % (str(width)) |
90 |
print 'Zoom: <SELECT NAME="zoom">' |
print 'Zoom: <SELECT NAME="zoom">' |
91 |
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]: |
92 |
print '<OPTION', |
print '<OPTION', |
93 |
if zv==zoom: |
if str(zv)==str(zoom): |
94 |
print 'SELECTED', |
print 'SELECTED', |
95 |
print 'VALUE="%s"' % (str(zv)) |
print 'VALUE="%s"' % (str(zv)) |
96 |
if zv < 0: |
if zv == 1: |
97 |
print '>÷%s' % (str(-zv)) |
print '>Pan' |
98 |
|
elif zv < 1: |
99 |
|
print '>Out ÷%s' % (str(1/zv)) |
100 |
else: |
else: |
101 |
print '>×%s' % (str(zv)) |
print '>In ×%s' % (str(zv)) |
102 |
print '</SELECT>' |
print '</SELECT>' |
103 |
print """Width: <INPUT TYPE=TEXT NAME="width" MAXLENGTH="4" SIZE="3" |
print """Width: <INPUT TYPE=TEXT NAME="width" MAXLENGTH="4" SIZE="3" |
104 |
VALUE="%s"> |
VALUE="%s"> |
106 |
VALUE="%s"> |
VALUE="%s"> |
107 |
Iterations: <INPUT TYPE=TEXT NAME="iter" MAXLENGTH="4" SIZE="3" |
Iterations: <INPUT TYPE=TEXT NAME="iter" MAXLENGTH="4" SIZE="3" |
108 |
VALUE="%s"> |
VALUE="%s"> |
109 |
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)), |
110 |
if debug: |
if debug: |
111 |
print 'CHECKED>' |
print 'CHECKED>' |
112 |
else: |
else: |
113 |
print '>' |
print '>' |
114 |
for key in form.keys(): |
print '<INPUT TYPE=SUBMIT VALUE="Apply">' |
115 |
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), |
116 |
print '<INPUT TYPE=HIDDEN NAME="%s" VALUE="%s">' % (key, form[key].value) |
("oheight", height)): |
117 |
|
print '<INPUT TYPE=HIDDEN NAME="%s" VALUE="%s">' % var |
118 |
print '<INPUT TYPE=HIDDEN NAME=type VALUE="html">' |
print '<INPUT TYPE=HIDDEN NAME=type VALUE="html">' |
119 |
|
|
120 |
print """</FORM> |
print """</FORM> |
121 |
</BODY></HTML>""" |
</BODY></HTML>""" |
122 |
sys.exit(0) |
sys.exit(0) |
123 |
|
|
124 |
c1, c2 = (-2+2j), (2-2j) # Corner coordinates |
# Figure out c1 and c2 from width, height, diag, cx, and cy. |
125 |
|
# Diagonal in pixels |
126 |
|
diagp= math.sqrt(width**2 + height**2) |
127 |
|
# Scale between pixels and coordinates |
128 |
|
scale= diagp/diag |
129 |
|
x= (width/2.0)/scale |
130 |
|
y= (height/2.0)/scale |
131 |
|
|
132 |
|
c1= cx - x + (cy - y) * (0+1j) |
133 |
|
c2= cx + x + (cy + y) * (0+1j) |
134 |
|
|
135 |
|
#print width, height, diag, cx, cy, diagp, scale, x, y, c1, c2 |
136 |
|
#sys.exit(0) |
137 |
|
|
138 |
|
#c1, c2 = (-2+2j), (2-2j) # Corner coordinates |
139 |
|
|
140 |
# Force c1 to be upper left and c2 to be lower right |
# Force c1 to be upper left and c2 to be lower right |
141 |
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)), \ |