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

Contents of /fract/mandelzoom.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations)
Sun Feb 25 05:49:07 2001 UTC (23 years, 9 months ago) by teddy
Branch: MAIN
Changes since 1.1: +67 -19 lines
More code.  `cx' and `cy' doesn't work yet, but that should be about
it before writing the code to figure out `c1' and `c2' from `diag',
`cx' and `cy'.

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

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26