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