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