/[cvs]/fract/mandelzoom.cgi
ViewVC logotype

Contents of /fract/mandelzoom.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations)
Sun Feb 25 20:12:25 2001 UTC (23 years, 8 months ago) by teddy
Branch: MAIN
Changes since 1.3: +3 -3 lines
Interim commit.

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

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26