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