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

Contents of /fract/mandelzoom.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Sat Feb 24 21:36:47 2001 UTC (23 years, 8 months ago) by teddy
Branch: MAIN
Doesn't work yet.  Whatever.

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

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26