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

Annotation of /fract/mandel.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Sat Feb 24 21:35:53 2001 UTC (23 years, 2 months ago) by teddy
Branch: MAIN
CVS Tags: HEAD
New mandelbrot CGI.

1 teddy 1.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    
22     if form.has_key('iter'):
23     maxiter=atoi(form['iter'].value)
24     else:
25     maxiter= 270
26    
27     c1, c2 = (-2+2j), (2-2j) # Corner coordinates
28    
29     # Force c1 to be upper left and c2 to be lower right
30     c1, c2= complex(min(c1.real, c2.real), max(c1.imag, c2.imag)), \
31     complex(max(c1.real, c2.real), min(c1.imag, c2.imag))
32    
33     mandel= Image.new("L", (width, height))
34     draw= ImageDraw.Draw(mandel)
35    
36     def drawrect((x1, y1), (x2, y2)):
37     "Draw a rectangle defined by the corners (x1, y1) and (x2, y2)."
38     color= plot(x1, y1) # Plot the upper left corner
39     # Should we cut this rectangle and recurse into the pieces?
40     cut= 0
41     ## Plot the edges and at the same time check if they are all the
42     ## same color.
43     # Upper edge (left to right)
44     x, y= x1+1, y1
45     while x<=x2:
46     if color<>plot(x, y):
47     cut= 1
48     x= x+1
49     # Right edge (top to bottom)
50     x, y= x2, y1+1
51     while y<=y2:
52     if color<>plot(x, y):
53     cut= 1
54     y= y+1
55     # Bottom edge (right to left)
56     x, y= x2-1, y2
57     while x>=x1:
58     if color<>plot(x, y):
59     cut= 1
60     x= x-1
61     # Left edge (bottom to top)
62     x, y= x1, y2-1
63     while y>y1:
64     if color<>plot(x, y):
65     cut= 1
66     y= y-1
67     # If our rectangle is too small to have an inside, we have now
68     # plotted every pixel in it.
69     if x2-x1<=1 or y2-y1<=1:
70     return
71     if cut: # If we're cutting the rectangle
72     if x2-x1 > y2-y1: # If it's wider than high
73     # Cut vertically
74     xc= ((x2-x1)/2)+x1
75     drawrect((x1, y1), (xc, y2))
76     drawrect((xc, y1), (x2, y2))
77     else: # if it's higher than wide
78     # Cut horizontally
79     yc= ((y2-y1)/2)+y1
80     drawrect((x1, y1), (x2, yc))
81     drawrect((x1, yc), (x2, y2))
82     else:
83     # If we're not cutting, it was the same color along the edge
84     if not debug:
85     # Fill in the rectangle
86     draw.rectangle([x1, y1, x2, y2], fill= color)
87     if yorig >= 0: # The x axis is visible on image
88     # Fill in the mirror rectangle
89     # Mirror y1 and y2 over yorig, but keep within image
90     y1m= max(0, min(2*yorig-y1, ymax))
91     y2m= max(0, min(2*yorig-y2, ymax))
92     draw.rectangle([x1, y1m, x2, y2m], fill= color)
93    
94     def constant(x, y):
95     creal= (float(x)/xmax)*(c2.real - c1.real)+c1.real
96     cimag= (float(y)/ymax)*(c2.imag - c1.imag)+c1.imag
97     return complex(creal, cimag)
98    
99     def plot(x, y):
100     c= constant(x, y)
101     z= i= 0
102     color= mandel.getpixel((x, y))
103     if color<>0:
104     return color
105     while i<maxiter and abs(z)<2:
106     z= z**2+c
107     i= i+1
108     color= 16*i%256
109     if i>=maxiter or color==0:
110     if debug:
111     color= 255
112     else:
113     color= 1
114     try:
115     mandel.putpixel((x, y), color)
116     if yorig >= 0: # The x axis is visible on image
117     ym= 2*yorig-y # ym is y mirrored over yorig
118     if ym >= 0 and ym <= ymax:
119     # ym is on the image
120     mandel.putpixel((x, ym), color)
121     except IndexError, the_error:
122     print "coord", x, y, xm, ym
123     raise IndexError, the_error
124     return color
125    
126     # Initial default values
127     yfrom= 0
128     yto= ymax
129     yorig= -1 # The pixel coordinate of the x axis; negative means not on image
130    
131     # Check if we should do mirroring
132     if c1.imag > 0 and c2.imag < 0: # the x axis is visible
133     yorig= int((-c1.imag/(c2.imag-c1.imag))*height) # pixel pos. of x axis
134     if c1.imag < abs(c2.imag): # the x axis is closer to top than bottom
135     yfrom= yorig # Begin at x axis
136     else:
137     yto= yorig # End at x axis
138    
139     drawrect((0, yfrom), (xmax, yto))
140    
141     print "Content-type: image/png"
142     print
143     mandel.save(sys.stdout, "PNG")

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26