1 |
teddy |
1.1 |
#!/usr/bin/python |
2 |
|
|
|
3 |
teddy |
1.2 |
import cgi, Image, ImageDraw, sys, math |
4 |
|
|
from string import atoi, atof |
5 |
|
|
|
6 |
teddy |
1.3 |
# This is to get backtrace output |
7 |
teddy |
1.2 |
sys.stderr = sys.stdout |
8 |
teddy |
1.1 |
|
9 |
teddy |
1.3 |
# Uncomment this to get the backtrace more readable |
10 |
|
|
#print "Content-Type: text/plain" |
11 |
|
|
#print "" |
12 |
|
|
|
13 |
teddy |
1.1 |
form= cgi.FieldStorage() |
14 |
|
|
|
15 |
|
|
# Image size |
16 |
|
|
if form.has_key('width'): |
17 |
teddy |
1.2 |
width= atoi(form['width'].value) |
18 |
teddy |
1.1 |
else: |
19 |
teddy |
1.3 |
width= 480 |
20 |
teddy |
1.1 |
if form.has_key('height'): |
21 |
teddy |
1.2 |
height= atoi(form['height'].value) |
22 |
teddy |
1.1 |
else: |
23 |
teddy |
1.3 |
height= 480 |
24 |
teddy |
1.1 |
|
25 |
|
|
xmax, ymax = width-1, height-1 # Coordinate maximums |
26 |
|
|
|
27 |
teddy |
1.2 |
# Complex number in center of image is (cx+cy*j) |
28 |
|
|
if form.has_key('cx'): |
29 |
|
|
cx= atof(form['cx'].value) |
30 |
|
|
else: |
31 |
teddy |
1.6 |
cx= 0.0 |
32 |
teddy |
1.2 |
|
33 |
|
|
if form.has_key('cy'): |
34 |
|
|
cy= atof(form['cy'].value) |
35 |
|
|
else: |
36 |
teddy |
1.6 |
cy= 0.0 |
37 |
teddy |
1.2 |
|
38 |
|
|
if form.has_key('diag'): |
39 |
|
|
diag= atof(form['diag'].value) |
40 |
|
|
else: |
41 |
teddy |
1.5 |
diag= math.sqrt(32) # sqrt(4**2 + 4**2) |
42 |
teddy |
1.2 |
|
43 |
teddy |
1.1 |
debug= form.has_key('debug') |
44 |
|
|
|
45 |
|
|
if form.has_key('iter'): |
46 |
teddy |
1.2 |
maxiter= atoi(form['iter'].value) |
47 |
teddy |
1.1 |
else: |
48 |
teddy |
1.5 |
maxiter= 270 |
49 |
teddy |
1.1 |
|
50 |
teddy |
1.2 |
# If type!=image, then output an HTML page, not an image |
51 |
|
|
if not (form.has_key('type') and form['type'].value == "image"): |
52 |
teddy |
1.1 |
print """Content-Type: text/html |
53 |
|
|
|
54 |
|
|
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> |
55 |
|
|
<HTML><HEAD> |
56 |
teddy |
1.6 |
<TITLE>Mandelbrot Set Zoomer</TITLE> |
57 |
teddy |
1.1 |
</HEAD> |
58 |
|
|
<BODY> |
59 |
teddy |
1.6 |
<H1>Mandelbrot Set Zoomer</H1> |
60 |
teddy |
1.1 |
|
61 |
|
|
<FORM ACTION="mandelzoom.cgi" METHOD=GET> |
62 |
|
|
|
63 |
teddy |
1.2 |
<!-- implies TYPE=SUBMIT -->""" |
64 |
|
|
print '<INPUT TYPE=IMAGE NAME="image"', |
65 |
|
|
print 'WIDTH="%s"' % (str(width)), |
66 |
|
|
print 'HEIGHT="%s"' % (str(height)), |
67 |
|
|
if debug: |
68 |
|
|
ds="&debug=on" |
69 |
|
|
else: |
70 |
|
|
ds="" |
71 |
teddy |
1.6 |
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 |
teddy |
1.7 |
diagp= math.sqrt(owidth**2 + oheight**2) |
78 |
teddy |
1.6 |
scale= diagp/diag |
79 |
teddy |
1.7 |
cx= (ix/scale) + (cx - (owidth / (scale*2))) |
80 |
|
|
cy= ((oheight-iy)/scale) + (cy - (oheight / (scale*2))) |
81 |
teddy |
1.2 |
if form.has_key('zoom'): |
82 |
teddy |
1.5 |
zoom=atof(form['zoom'].value) |
83 |
teddy |
1.2 |
diag=diag/zoom |
84 |
|
|
else: |
85 |
|
|
# If no zoom provided, don't actually zoom |
86 |
teddy |
1.6 |
zoom= 2.0 |
87 |
teddy |
1.2 |
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)), |
89 |
|
|
print 'WIDTH="%s"><P>' % (str(width)) |
90 |
|
|
print 'Zoom: <SELECT NAME="zoom">' |
91 |
teddy |
1.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 |
teddy |
1.2 |
print '<OPTION', |
93 |
teddy |
1.5 |
if str(zv)==str(zoom): |
94 |
teddy |
1.2 |
print 'SELECTED', |
95 |
|
|
print 'VALUE="%s"' % (str(zv)) |
96 |
teddy |
1.3 |
if zv == 1: |
97 |
|
|
print '>Pan' |
98 |
|
|
elif zv < 1: |
99 |
teddy |
1.6 |
print '>Out ÷%s' % (str(1/zv)) |
100 |
teddy |
1.2 |
else: |
101 |
teddy |
1.3 |
print '>In ×%s' % (str(zv)) |
102 |
teddy |
1.2 |
print '</SELECT>' |
103 |
|
|
print """Width: <INPUT TYPE=TEXT NAME="width" MAXLENGTH="4" SIZE="3" |
104 |
|
|
VALUE="%s"> |
105 |
|
|
Height: <INPUT TYPE=TEXT NAME="height" MAXLENGTH="4" SIZE="3" |
106 |
|
|
VALUE="%s"> |
107 |
teddy |
1.1 |
Iterations: <INPUT TYPE=TEXT NAME="iter" MAXLENGTH="4" SIZE="3" |
108 |
|
|
VALUE="%s"> |
109 |
teddy |
1.3 |
Debug mode: <INPUT TYPE=CHECKBOX NAME="debug" """ % (str(width), str(height), str(maxiter)), |
110 |
teddy |
1.2 |
if debug: |
111 |
|
|
print 'CHECKED>' |
112 |
|
|
else: |
113 |
|
|
print '>' |
114 |
teddy |
1.3 |
print '<INPUT TYPE=SUBMIT VALUE="Apply">' |
115 |
teddy |
1.6 |
for var in (("diag", diag), ("cx", cx), ("cy", cy), ("owidth", width), |
116 |
|
|
("oheight", height)): |
117 |
teddy |
1.5 |
print '<INPUT TYPE=HIDDEN NAME="%s" VALUE="%s">' % var |
118 |
teddy |
1.2 |
print '<INPUT TYPE=HIDDEN NAME=type VALUE="html">' |
119 |
teddy |
1.1 |
|
120 |
teddy |
1.8 |
print """</FORM><P> |
121 |
|
|
After changing any settings, don't forget to change the "Zoom" setting |
122 |
|
|
to "Pan" if you don't want to zoom when applying them. |
123 |
teddy |
1.1 |
</BODY></HTML>""" |
124 |
|
|
sys.exit(0) |
125 |
|
|
|
126 |
teddy |
1.5 |
# Figure out c1 and c2 from width, height, diag, cx, and cy. |
127 |
|
|
# Diagonal in pixels |
128 |
|
|
diagp= math.sqrt(width**2 + height**2) |
129 |
|
|
# Scale between pixels and coordinates |
130 |
|
|
scale= diagp/diag |
131 |
|
|
x= (width/2.0)/scale |
132 |
|
|
y= (height/2.0)/scale |
133 |
|
|
|
134 |
teddy |
1.6 |
c1= cx - x + (cy - y) * (0+1j) |
135 |
|
|
c2= cx + x + (cy + y) * (0+1j) |
136 |
teddy |
1.5 |
|
137 |
|
|
#print width, height, diag, cx, cy, diagp, scale, x, y, c1, c2 |
138 |
|
|
#sys.exit(0) |
139 |
|
|
|
140 |
|
|
#c1, c2 = (-2+2j), (2-2j) # Corner coordinates |
141 |
teddy |
1.1 |
|
142 |
|
|
# Force c1 to be upper left and c2 to be lower right |
143 |
|
|
c1, c2= complex(min(c1.real, c2.real), max(c1.imag, c2.imag)), \ |
144 |
|
|
complex(max(c1.real, c2.real), min(c1.imag, c2.imag)) |
145 |
|
|
|
146 |
|
|
mandel= Image.new("L", (width, height)) |
147 |
|
|
draw= ImageDraw.Draw(mandel) |
148 |
|
|
|
149 |
|
|
def drawrect((x1, y1), (x2, y2)): |
150 |
|
|
"Draw a rectangle defined by the corners (x1, y1) and (x2, y2)." |
151 |
|
|
color= plot(x1, y1) # Plot the upper left corner |
152 |
|
|
# Should we cut this rectangle and recurse into the pieces? |
153 |
|
|
cut= 0 |
154 |
|
|
## Plot the edges and at the same time check if they are all the |
155 |
|
|
## same color. |
156 |
|
|
# Upper edge (left to right) |
157 |
|
|
x, y= x1+1, y1 |
158 |
|
|
while x<=x2: |
159 |
|
|
if color<>plot(x, y): |
160 |
|
|
cut= 1 |
161 |
|
|
x= x+1 |
162 |
|
|
# Right edge (top to bottom) |
163 |
|
|
x, y= x2, y1+1 |
164 |
|
|
while y<=y2: |
165 |
|
|
if color<>plot(x, y): |
166 |
|
|
cut= 1 |
167 |
|
|
y= y+1 |
168 |
|
|
# Bottom edge (right to left) |
169 |
|
|
x, y= x2-1, y2 |
170 |
|
|
while x>=x1: |
171 |
|
|
if color<>plot(x, y): |
172 |
|
|
cut= 1 |
173 |
|
|
x= x-1 |
174 |
|
|
# Left edge (bottom to top) |
175 |
|
|
x, y= x1, y2-1 |
176 |
|
|
while y>y1: |
177 |
|
|
if color<>plot(x, y): |
178 |
|
|
cut= 1 |
179 |
|
|
y= y-1 |
180 |
|
|
# If our rectangle is too small to have an inside, we have now |
181 |
|
|
# plotted every pixel in it. |
182 |
|
|
if x2-x1<=1 or y2-y1<=1: |
183 |
|
|
return |
184 |
|
|
if cut: # If we're cutting the rectangle |
185 |
|
|
if x2-x1 > y2-y1: # If it's wider than high |
186 |
|
|
# Cut vertically |
187 |
|
|
xc= ((x2-x1)/2)+x1 |
188 |
|
|
drawrect((x1, y1), (xc, y2)) |
189 |
|
|
drawrect((xc, y1), (x2, y2)) |
190 |
|
|
else: # if it's higher than wide |
191 |
|
|
# Cut horizontally |
192 |
|
|
yc= ((y2-y1)/2)+y1 |
193 |
|
|
drawrect((x1, y1), (x2, yc)) |
194 |
|
|
drawrect((x1, yc), (x2, y2)) |
195 |
|
|
else: |
196 |
|
|
# If we're not cutting, it was the same color along the edge |
197 |
|
|
if not debug: |
198 |
|
|
# Fill in the rectangle |
199 |
|
|
draw.rectangle([x1, y1, x2, y2], fill= color) |
200 |
|
|
if yorig >= 0: # The x axis is visible on image |
201 |
|
|
# Fill in the mirror rectangle |
202 |
|
|
# Mirror y1 and y2 over yorig, but keep within image |
203 |
|
|
y1m= max(0, min(2*yorig-y1, ymax)) |
204 |
|
|
y2m= max(0, min(2*yorig-y2, ymax)) |
205 |
|
|
draw.rectangle([x1, y1m, x2, y2m], fill= color) |
206 |
|
|
|
207 |
|
|
def constant(x, y): |
208 |
|
|
creal= (float(x)/xmax)*(c2.real - c1.real)+c1.real |
209 |
|
|
cimag= (float(y)/ymax)*(c2.imag - c1.imag)+c1.imag |
210 |
|
|
return complex(creal, cimag) |
211 |
|
|
|
212 |
|
|
def plot(x, y): |
213 |
|
|
c= constant(x, y) |
214 |
|
|
z= i= 0 |
215 |
|
|
color= mandel.getpixel((x, y)) |
216 |
|
|
if color<>0: |
217 |
|
|
return color |
218 |
|
|
while i<maxiter and abs(z)<2: |
219 |
|
|
z= z**2+c |
220 |
|
|
i= i+1 |
221 |
|
|
color= 16*i%256 |
222 |
|
|
if i>=maxiter or color==0: |
223 |
|
|
if debug: |
224 |
|
|
color= 255 |
225 |
|
|
else: |
226 |
|
|
color= 1 |
227 |
|
|
try: |
228 |
|
|
mandel.putpixel((x, y), color) |
229 |
|
|
if yorig >= 0: # The x axis is visible on image |
230 |
|
|
ym= 2*yorig-y # ym is y mirrored over yorig |
231 |
|
|
if ym >= 0 and ym <= ymax: |
232 |
|
|
# ym is on the image |
233 |
|
|
mandel.putpixel((x, ym), color) |
234 |
|
|
except IndexError, the_error: |
235 |
|
|
print "coord", x, y, xm, ym |
236 |
|
|
raise IndexError, the_error |
237 |
|
|
return color |
238 |
|
|
|
239 |
|
|
# Initial default values |
240 |
|
|
yfrom= 0 |
241 |
|
|
yto= ymax |
242 |
|
|
yorig= -1 # The pixel coordinate of the x axis; negative means not on image |
243 |
|
|
|
244 |
|
|
# Check if we should do mirroring |
245 |
|
|
if c1.imag > 0 and c2.imag < 0: # the x axis is visible |
246 |
|
|
yorig= int((-c1.imag/(c2.imag-c1.imag))*height) # pixel pos. of x axis |
247 |
|
|
if c1.imag < abs(c2.imag): # the x axis is closer to top than bottom |
248 |
|
|
yfrom= yorig # Begin at x axis |
249 |
|
|
else: |
250 |
|
|
yto= yorig # End at x axis |
251 |
|
|
|
252 |
|
|
drawrect((0, yfrom), (xmax, yto)) |
253 |
|
|
|
254 |
|
|
print "Content-type: image/png" |
255 |
|
|
print |
256 |
|
|
mandel.save(sys.stdout, "PNG") |