-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmandelbrot.py
executable file
·370 lines (312 loc) · 10.3 KB
/
mandelbrot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/bin/env python
""" The mandelbrot application is building a colorized Mandelbrot set image
"""
#############################################################################
#
# The mandelbrot application is building a colorized Mandelbrot set fractal
# images
#
# Copyright 2011 by DIRAC Project
# http://diracgrid.org
#
#############################################################################
#############################################################################
#
# Helper classes and functions
#
#############################################################################
####################################################
# BmpImage class derived from:
# py_kohn_bmp - Copyright 2007 by Michael Kohn
# http://www.mikekohn.net/
# mike@mikekohn.net
####################################################
class BmpImage:
def __init__(self,filename,width,height,depth):
self.out = 0
self.width = width
self.height = height
self.depth = depth
self.bytes = 0
self.xpos = 0
self.width_bytes = width*depth
if (self.width_bytes%4)!=0:
self.width_bytes=self.width_bytes+(4-(self.width_bytes%4))
self.out=open(filename,"wb")
self.out.write("BM") # magic number
if depth==1:
self.write_int((self.width_bytes*height)+54+1024)
else:
self.write_int((self.width_bytes*height)+54)
self.write_word(0)
self.write_word(0)
if depth==1:
self.write_int(54+1024)
else:
self.write_int(54)
self.write_int(40) # header_size
self.write_int(width) # width
self.write_int(height) # height
self.write_word(1) # planes
self.write_word(depth*8) # bits per pixel
self.write_int(0) # compression
self.write_int(self.width_bytes*height*depth) # image_size
self.write_int(0) # biXPelsperMetre
self.write_int(0) # biYPelsperMetre
if depth==1:
self.write_int(256) # colors used
self.write_int(256) # colors important
for c in range(256):
self.out.write('%c' % c)
self.out.write('%c' % c)
self.out.write('%c' % c)
self.out.write('%c' % 0)
else:
self.write_int(0) # colors used - 0 since 24 bit
self.write_int(0) # colors important - 0 since 24 bit
def write_int(self,n):
str_out='%c%c%c%c' % ((n&255),(n>>8)&255,(n>>16)&255,(n>>24)&255)
self.out.write(str_out)
def write_word(self,n):
str_out='%c%c' % ((n&255),(n>>8)&255)
self.out.write(str_out)
def write_pixel_bw(self,y):
self.out.write(str("%c" % y))
self.xpos=self.xpos+1
if self.xpos==self.width:
while self.xpos<self.width_bytes:
self.out.write(str("%c" % 0))
self.xpos=self.xpos+1
self.xpos=0
def write_pixel(self,red,green,blue):
self.out.write(str("%c" % (blue&255)))
self.out.write(str("%c" % (green&255)))
self.out.write(str("%c" % (red&255)))
self.xpos=self.xpos+1
if self.xpos==self.width:
self.xpos=self.xpos*3
while self.xpos<self.width_bytes:
self.out.write(str("%c" % 0))
self.xpos=self.xpos+1
self.xpos=0
def close(self):
self.out.close()
def mandelbrot(c,maxCount = 100,limit=2):
""" The main Mandelbrot set engine
"""
z=complex(0,0)
count = 0
while count < maxCount:
z=(z*z)+c
#if checkCardioid(z):
# return maxCount
if abs(z)>limit: break
count += 1
return count
def checkCardioid(c):
""" Check getting to the Mandelbrot set area to stop iterations
"""
x = c.real
y = c.imag
y2 = y*y
q = (x-0.25)*(x-0.25) + y2
qq = q*(q+(x-0.25))
if qq < 0.25*y2:
print "exit 1"
print x,y,y2,q,qq,0.25*y2
return 1
if (x+1)*(x+1)+y2 < 0.0625:
print "exit 2"
print (x+1)*(x+1)+y2
return 1
return 0
##########################################################
#
# Various color generators
#
##########################################################
def getColorRange(count,maxIter):
black = [0,0,0]
mincolor = [255.,255.,0.]
maxcolor = [0.,0.,255.]
color = [0,0,0]
if count < maxIter:
color = []
for i in range(3):
c = (maxcolor[i]*count + mincolor[i]*(maxIter-count))/maxIter
color.append(int(c))
return color
colorDict = {}
def getColorRandom(count,maxIter):
global colorDict
import md5
key = "randomname"
myMD5 = md5.md5()
if not count in colorDict:
myMD5.update( str(count)+key )
hexstring = myMD5.hexdigest()
color = [int('0x'+hexstring[:2],0),int('0x'+hexstring[2:4],0),int('0x'+hexstring[4:6],0)]
colorDict[count] = color
else:
color = colorDict[count]
return color
def getColorSlider(count,maxIter):
global colorDict
if count == maxIter:
return [0,0,0]
if not count in colorDict:
color = []
color.append(int(255./maxIter*count))
color.append(int(255./maxIter*count))
color.append(int(255. - 255./maxIter*count))
colorDict[count] = color
else:
color = colorDict[count]
return color
def getColorSin(count,maxIter):
global colorDict,cFactor,cPhase,cDelta
if count == maxIter:
return [0,0,0]
if not count in colorDict:
arg = count*cFactor
c1 = int(127.*(sin(arg+cPhase) + 1.))
c2 = int(127.*(sin(arg+cPhase+cDelta) + 1.))
c3 = int(127.*(sin(arg+cPhase+2.*cDelta) + 1.))
color = [c1,c2,c3]
colorDict[count] = color
else:
color = colorDict[count]
return color
greyDict = {}
def getGreyLevel(count,maxIter, cFactor=0.02, cPhase=1.):
global greyDict
if count == maxIter:
return 0
if not count in greyDict:
arg = count*cFactor
grey = int(127.*(sin(arg+cPhase) + 1.))
greyDict[count] = grey
else:
grey = greyDict[count]
return grey
###################################################################
#
# Start of the main body
#
###################################################################
import time
import sys
import getopt
from math import sin
def usage():
print """
The mandelbrot program is creating an image of a Mandelbrot set and its vicinity in
the given range of the C parameter. For more information about the Mandelbrot set
see http://en.wikipedia.org/Mandelbrot.
Usage:
mandelbrot [options] [<output_file>]
Options:
-X, --cx - the real part of the C parameter in the center of the image, default = -0.5
-Y, --cy - the imaginary part of the C parameter in the center of the image, default = 0.0
-P, --precision - the step size of the C parameter increment per pixel of the image, default = 0.01
-M, --max_iterations - the maximum number of the mandelbrot algorithm iterations, default = 100
-W, --width - image width in pixels, default = 300
-H, --height - image height in pixels, default = 300
-B, --bw - force black and white image, default is a color image
-F, --color_factor - color palette parameter defining how quickly the colors are changing, the value
should be in the range 0.<x<1.0, default = 0.02
-S, --color_phase - a magic color palette parameter, default = 1.0
-D, --color_delta - yet another magic color palette parameter, default = 1.0
-h, --help - print this usage info
"""
if __name__ == '__main__':
# Get the command line options first
options = ['width=','height=','cx=','cy=','precision=', 'start_line', 'n_lines', 'max_iterations=',
'color_factor=','color_phase=','color_delta=','bw','help']
optlist,args = getopt.getopt(sys.argv[1:], 'W:H:X:Y:P:L:N:M:F:S:D:Bh',options)
# 4K 3840 x 2160
# 8K 7680 x 4320
# 16K 15360 x 8640
(image_width, image_height) = (7680, 4320)
centerX = -0.5
centerY = 0.0
precision = .01
cFactor = 0.02
cPhase = 1.
cDelta = 1.
maxIter = 500
output = 'out.bmp'
bwImage = False
start_line = 0
n_lines = image_height
for o,v in optlist:
if o in ['-W','--width']:
image_width = int(v)
if o in ['-H','--height']:
image_height = int(v)
if o in ['-X','--cx']:
centerX = float(v)
if o in ['-Y','--cy']:
centerY = float(v)
if o in ['-P','--precision']:
precision = float(v)
if o in ['-L','--start_line']:
start_line = int(v)*200
if o in ['-N','--n_lines']:
n_lines = int(v)
if o in ['-F','--color_factor']:
cFactor = float(v)
if o in ['-S','--color_phase']:
cPhase = float(v)
if o in ['-D','--color_delta']:
cDelta = float(v)
if o in ['-M','--max_iterations']:
maxIter = int(v)
if o in ['-B','--bw']:
bwImage = True
if o in ['-h','--help']:
usage()
sys.exit(0)
#if args:
#output = args[0]
output = 'data_'+str(start_line)+'.bmp'
#centerX = -0.46490
#centerY = -.56480
#precision = .000002
start_real = centerX - image_width*precision/2.
start_imag = centerY - image_height*precision/2.
end_real = centerX + image_width*precision/2.
end_imag = centerY + image_height*precision/2.
depth = 3
if bwImage:
depth = 1
my_bmp=BmpImage(output,image_width,image_height,depth)
inc_real=(end_real-start_real)/image_width
inc_imag=(end_imag-start_imag)/image_height
start=complex(start_real,start_imag)
end=complex(end_real,end_imag)
start = time.time()
print "max iterations: %s, precision: %s, C: %f+%fj" % (maxIter, precision, centerX,centerY)
buff=list()
# for y in range(image_height):
y_i = start_line
y_f = min(start_line + n_lines, image_height)
print(y_i, y_f)
for y in range(y_i, y_f):
print('%d\r'%y)
dump='%d' % y
for x in range(image_width):
c = complex(start_real+(inc_real*x),start_imag+(inc_imag*y))
count = mandelbrot(c,maxIter)
dump+=' %d' % count
if bwImage:
grey = getGreyLevel(count,maxIter)
my_bmp.write_pixel_bw(grey)
else:
color = getColorSin(count,maxIter)
my_bmp.write_pixel(*color)
#print count, color, c
buff.append(dump+'\n')
print 'Image generation time %.2f' % (time.time()-start)
my_bmp.close()
open('data_%d.txt'%start_line,'w').writelines(buff)