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