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

Annotation of /fract/julia3.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations)
Sat Feb 24 21:33:54 2001 UTC (23 years, 8 months ago) by teddy
Branch: MAIN
Changes since 1.1: +10 -1 lines
Get size from CGI variable.

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

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26