-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFeatures_Speedtest.py
412 lines (389 loc) · 14.2 KB
/
Features_Speedtest.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
notice = """
Feature and speed test
for a Pure Python graphics library
that saves to a bitmap
-----------------------------------
| Copyright 2022 by Joel C. Alcarez |
| [joelalcarez1975@gmail.com] |
|-----------------------------------|
| We make absolutely no warranty |
| of any kind, expressed or implied |
|-----------------------------------|
| The primary author and any |
| any subsequent code contributors |
| shall not be liable in any event |
| for incidental or consequential |
| damages in connection with, or |
| arising out from the use of this |
| code in current form or with any |
| modifications. |
|-----------------------------------|
| Contact primary author |
| if you plan to use this |
| in a commercial product at |
| joelalcarez1975@gmail.com |
|-----------------------------------|
| Educational or hobby use is |
| highly encouraged... |
| have fun coding ! |
|-----------------------------------|
| This graphics library outputs |
| to a bitmap file. |
-----------------------------------
"""
from pythonbmp.BITMAPlib import(
addvect,
adjustcolordicttopal,
beziercurve,
bottomrightcoord,
bspline,
bsplinevert,
centercoord,
conevertandsurface,
convertselection2BMP,
copyrect,
cubevert,
cylindervertandsurface,
decahedvertandsurface,
drawvec,
fillbackgroundwithgrad,
filledgradrect,
filledrect,
font8x14,
font8x8,
font8x8thin,
font8x14thin,
font8x8sanserif,
font8x14sanserif,
getcolorname2RGBdict,
getdefaultlumrange,
getIFSparams,
getRGBfactors,
getshapesidedict,
gradcircle,
gradellipse,
gradthickcircle,
gradthickellipserot,
gradthickroundline,
gradvert,
hexahedravert,
icosahedvertandsurface,
IFS,
loadBMP,
mandelbrot,
fractaldomainparamdict,
newBMP,
octahedravert,
pasterect,
piechart,
plot3Dsolid,
plot8bitpatternastext,
plotbmpastext,
plotfilledflower,
plotlines,
plotpoly,
plotreversestring,
plotstring,
plotstringsideway,
plotstringupsidedown,
plotstringvertical,
rectangle,
regpolygonvert,
RGB2int,
rotvec3D,
saveBMP,
spherevertandsurface,
spiralcontrolpointsvert,
surfplot3Dvertandsurface,
tetrahedravert,
trans,
XYaxis,
XYscatterplot
)
import subprocess as proc
from time import(
process_time_ns as _time_ns
)
from random import randint
from os import path
def elaspedtimeinseconds(inittime):
return (_time_ns() - inittime) / 1000000000
def hhmmsselaspedtime(inittime):
secs, ns = \
divmod((_time_ns() - inittime), 1000000000)
mins, secs = divmod(secs, 60)
hrs, mins = divmod(mins, 60)
return (((f'{str(hrs).zfill(2)}:' + str(mins).zfill(2)) + ':') +
str(secs).zfill(2) + '.') + str(ns)
def main():
rootdir = path.dirname(__file__)
print(notice)
demostart = _time_ns()
starttime = _time_ns()
mx = 1024
my = 768
bmp = newBMP(mx, my, 24)
print('New bitmap in ' + hhmmsselaspedtime(starttime))
maxpt = bottomrightcoord(bmp)
cenpt = centercoord(bmp) #bitmap dependent coords
c = getcolorname2RGBdict()
cf = getRGBfactors()
lum = getdefaultlumrange() #color info
adjustcolordicttopal(bmp, c)
starttime = _time_ns()
fillbackgroundwithgrad(bmp,
lum['maxasc'], cf['blue'], 0)
print('Background gradient test done in ',
hhmmsselaspedtime(starttime))
starttime = _time_ns()
rectangle(bmp, 1, 1,
maxpt[0] - 1,
maxpt[1] - 1,
c['white'])
filledgradrect(bmp,
395, 5, 955, 41,
lum['upperdesc'],
cf['orange'], 1)
filledrect(bmp,
395, 44, 955, 55,
c['darkblue'])
print('Rectangle tests done in ',
hhmmsselaspedtime(starttime))
starttime = _time_ns()
hc = spiralcontrolpointsvert(
350, 180, 5, 1.1, 5)#we will use this later to make smooth spiral
gradthickroundline(bmp,
[800, 620], # end point 1
[800, 600], # end point 2
7, # thickness
lum['upperdesc'], # how bright the gradient
cf['darkred']) # color of the gradient
plotlines(bmp, hc, c['yellow'])
cp = [50, 150]#all arrows must point outward from cp or bug report please
drawvec(bmp, cp, [50, 100], 0, c['white'])#color dict more readable than color int
drawvec(bmp, cp, [100, 150], 0, c['green'])#but if you want to use color int
drawvec(bmp, cp, [50, 200], 0, c['red'])#the functions will work too
drawvec(bmp, cp, [5, 150], 0, c['blue'])#up to the color supported
drawvec(bmp, cp, [75, 175], 0, c['yellow'])# by the loaded bitmap file
drawvec(bmp, cp, [25, 125], 0, c['magenta'])
drawvec(bmp, cp, [25, 175], 0, c['orange'])
drawvec(bmp, cp, [75, 125], 0, c['gray'])
print('Line tests done in ',
hhmmsselaspedtime(starttime))
starttime = _time_ns()
fnt = font8x8
strtest = '\x01abcdefghijklmnopqrstuvwxyz\n0123456789\'":;.,?!~`@#$%^&()[]{}_*+-/=<>\nABCDEFGHIJKLMNOPQRSTUVWXYZ'
plotstring(bmp, 400, 10,
'My Python GL test',
4, 1, 0, c['lightgray'],
fnt)
plotstring(bmp, 400, 45,
'Copyright 2021 by Joel C. Alcarez (joelalcarez1975@gmail.com)',1,1,0,c['brightwhite'],fnt)
fnt = font8x8sanserif
plotstring(bmp, 300, 64,
strtest, 1, 0, 0,
c['brightgreen'],
fnt)
fnt = font8x8
plotstringupsidedown(bmp,
10, 737,
strtest, 1, 0, 0,
c['brightgreen'],
fnt)
plotreversestring(bmp,
300, 100,
strtest, 1, 0, 0,
c['brightgreen'],
fnt)
fnt = font8x14sanserif
plotstringvertical(bmp,
970, 30,
'Matrix text',
2, 0, 0, c['green'],
fnt)
plotstringsideway(bmp, 970, 730,
strtest, 1, 0, 0,
c['white'], fnt)
print('Text tests done in ',
hhmmsselaspedtime(starttime))
starttime = _time_ns()
#be careful with these variables
# or the object goes offscreen
d = 200 # distance of the observer
# from the screen
tvect = [0, 0, 100] #3D translation vector
sd = getshapesidedict()
pts = tetrahedravert(80)
shapes = [[[trans(cubevert(30),
pts[3]),
sd["cube"]],
cf["darkblue"], True,
c['black']],
[[trans(tetrahedravert(30),
pts[2]),
sd["tetrahedra"]],
cf["darkred"], True,
c['white']],
[[trans(octahedravert(20),
pts[1]),
sd["octahedra"]],
cf["yellow"], True,
c['darkgray']],
[[trans(hexahedravert(30),
pts[0]),
sd["hexahedra"]],
cf["darkgreen"], True,
c['darkgreen']]]
for s in shapes:
plot3Dsolid(bmp,
s[0], True,
s[1], s[2], s[3],
rotvec3D(10, 5, 5),
tvect, d,
addvect(cenpt, [-160, -10]))
plot3Dsolid(bmp,
decahedvertandsurface(25),
True, cf['brightred'], False,
0, rotvec3D(7, 77, 20),
tvect, d, addvect(cenpt, [280, -250]))
plot3Dsolid(bmp,
icosahedvertandsurface(25),
True, cf['brightwhite'], False,
0, rotvec3D(70, 7, 20),
tvect, d, addvect(cenpt, [+60, -130]))
plot3Dsolid(bmp,
spherevertandsurface([5, 0, 0], 60, 10),
True, cf['brightwhite'], False,
0, rotvec3D(190, 145, 70),
tvect, d, addvect(cenpt, [300, -50]))
plot3Dsolid(bmp,
cylindervertandsurface([1,0,0], 20, 10, 5),
True, cf['brightyellow'], True,
RGB2int(20, 20, 0), rotvec3D(60, 74, 72),
tvect, d, addvect(cenpt,[-200, -50]))
plot3Dsolid(bmp,
conevertandsurface([1, 0, 0], 20, 15, 5),
True, cf['brightorange'],
False, RGB2int(20, 20, 0),
rotvec3D(6, 67 ,2),
tvect, d, addvect(cenpt, [-300, -150]))
fnxy = lambda x, y: x & y
plot3Dsolid(bmp,
surfplot3Dvertandsurface (
-35, -35, 35, 35, 5, fnxy),
True, cf['brightcyan'],
True, 0, rotvec3D(20, 67, 30),
tvect, d, addvect(cenpt, [-420, -25]))
print('3D tests done in ',
hhmmsselaspedtime(starttime))
starttime = _time_ns()
mandelpar = fractaldomainparamdict()
mandelbrot(bmp, 10, 600, 130, 735,
mandelpar['maxeqdim'],
cf['brightgreen'], 255)
p = getIFSparams()#same here add more to this dict
IFS(bmp, p['fern'],
170, 600, 270, 730, 12, 12, 30, 30,
c['lightgreen'], 10000)
IFS(bmp, p['tree'],
270, 600, 370, 730, 100, 100, 0, 0,
c['brown'], 10000)
IFS(bmp,p['cantortree'],
370, 600, 450, 730, 100, 100, 0, 0,
c['lightcyan'], 10000)
IFS(bmp, p['sierpinskitriangle'],
450, 600, 670, 730, 100, 100, 0, 0,
c['cyan'], 10000)
print('Fractal tests done in ',
hhmmsselaspedtime(starttime))
p = regpolygonvert(250, 80, 40, 6, 0)
starttime = _time_ns()
beziercurve(bmp, p, 3, c['brightorange'])
beziercurve(bmp, hc, 0, c['lightred'])
print('Bezier curve tests done in ',
hhmmsselaspedtime(starttime) )
starttime = _time_ns() #bspline follow control points better than bezier
bspline(bmp, p, 0, c['red'], True, True)
bspline(bmp, hc, 0, c['brightwhite'], True, True)
gradvert(bmp,
bsplinevert(
spiralcontrolpointsvert(
165, 135, 7, 1.2, 3),
False,
False),
5,
lum['upperdesc'],
cf['brightwhite'])
print('Bspline tests done in ',
hhmmsselaspedtime(starttime) )
starttime = _time_ns()
gradcircle(bmp, 900, 140, 30,
lum['maxdesc'], cf['brightyellow'])
gradthickcircle(bmp, 900, 550, 40, 8,
lum['upperdesc'], cf['lightred'])
print('Circle tests done in ',
hhmmsselaspedtime(starttime))
starttime = _time_ns()
gradellipse(bmp, 750, 550, 20, 40,
lum['upperdesc'], cf['brightorange'])
gradthickellipserot(bmp, 790, 700, 50, 30, 45, 5,
lum['upperdesc'], cf['yellow'])
print('Ellipse tests done in ',
hhmmsselaspedtime(starttime))
starttime = _time_ns()
pdata = []#[[20,c['red']],[30,c['brightyellow']],...]
for color in c:
pdata.append([1,c[color]])
piedata = piechart(bmp, 75, 540, 45, pdata)
print('Arc and pie chart tests done in ',
hhmmsselaspedtime(starttime))
starttime = _time_ns()
plotpoly(bmp, p, c['brightcyan'])
print('Polygon tests done in ',
hhmmsselaspedtime(starttime))
starttime = _time_ns()
plotfilledflower(bmp, 40, 40, 30, 5, 45,
lum['maxasc'], cf['brightyellow'])
print('Filled flower test done in ',
hhmmsselaspedtime(starttime))
starttime = _time_ns() #[x,y,rad=constant for sample can be variable,isfilled]
XYdata = []#[[45,45,5,c['brightcyan'],True],[100,60,5,c['brightgreen'],True],..]
for color in c:
XYdata.append(
[randint(20, 140),
randint(30,70),
5, c[color], True])
XYdata.append(
[randint(20, 140),
randint(30, 70),
5, c[color], False])
XYcoordinfo = XYaxis(bmp,
[172, 598],[40, 40],
[666, 398],[20, 30],
[10, 10],
c['brightwhite'],
c['gray'],
True,
c['darkgreen'])
XYscatterplot(bmp, XYdata, XYcoordinfo,
True, c['green'])
print('XY scatterplot test done in ',
hhmmsselaspedtime(starttime))
starttime = _time_ns()
buff = copyrect(bmp, 3, 3, 80, 80)
pasterect(bmp, buff, 858, 611)
print('Copy paste done in ',
hhmmsselaspedtime(starttime))
starttime = _time_ns()
file = 'Hello_speedtest.bmp' # some random filename
saveBMP(file, bmp) # dump byte array to file
print('Saved ' + file + ' in ',
hhmmsselaspedtime(starttime))
print('Demo done in ',
hhmmsselaspedtime(demostart))
print('\nAll done close mspaint to finish')
ret = proc.call('mspaint ' + file) # replace with another editor if Unix
print('\nThe')
print(plot8bitpatternastext([0x00,0x00,0x38,0x6C,0x6C,0x38,0x76,0xDC,0xCC,0xCC,0x76,0x00,0x00,0x00],'&',' '))
if __name__ == "__main__":
main()