Faster method to create a checkerboard pattern in a memory image view The image is written tile by tile, using a buffer to write the tile values at once.
Faster method to create a checkerboard pattern in a memory image view The image is written tile by tile, using a buffer to write the tile values at once.
1import iolink
2
3from array import array
4
5import timeit
6
7
10def checkerBoard_Fast(sideLength):
11 shape = iolink.VectorXu64(sideLength, sideLength)
12 dt = iolink.DataTypeId.UINT8
13
14
15 image = iolink.ImageViewFactory.allocate(shape, dt)
16
17 tileCount = 10
18
19
20 blackValue = 0
21 whiteValue = 255
22
23
24 tileSize = shape / tileCount
25 regionTile = iolink.RegionXu64.create_full_region(tileSize)
26
27
28
29 blackBuf = array('B', [blackValue] * regionTile.element_count)
30 whiteBuf = array('B', [whiteValue] * regionTile.element_count)
31
32
33 for tileX in range(0, tileCount):
34 for tileY in range(0, tileCount):
35
36 origin = iolink.VectorXu64(tileX, tileY) * tileSize
37 regionTile = iolink.RegionXu64(origin, tileSize)
38
39 if ( (tileX + tileY) % 2 == 0):
40 image.write_region(regionTile, blackBuf)
41 else:
42 image.write_region(regionTile, whiteBuf)
43
44 return image
45
46start = timeit.default_timer()
47img = checkerBoard_Fast(1000)
48stop = timeit.default_timer()
49print('CheckerBoard fast method created in: ', stop - start, "s")
50
51print("Generated ImageView:", str(img))
52
53
55
56
57
58
59
62
63
64
65
66
67print("SUCCESS")