|
1 | 1 | from PIL import Image
|
2 | 2 | from . import constants as CONSTANTS
|
| 3 | +import threading |
3 | 4 |
|
4 | 5 | # TileGrid implementation loosely based on the
|
5 | 6 | # displayio.TileGrid class in Adafruit CircuitPython
|
|
21 | 22 | bmp_img = img.load()
|
22 | 23 |
|
23 | 24 |
|
| 25 | +def func(x, y): |
| 26 | + return x + y |
| 27 | + |
| 28 | + |
24 | 29 | class TileGrid:
|
25 | 30 | def __init__(
|
26 | 31 | self,
|
@@ -82,22 +87,59 @@ def draw(self, x, y, scale):
|
82 | 87 | # appropriate scale on the global bmp_img
|
83 | 88 | x = self.x * scale + x
|
84 | 89 | y = self.y * scale + y
|
85 |
| - for i in range(self.tile_height): |
86 |
| - for j in range(self.tile_width): |
| 90 | + |
| 91 | + if self.tile_height > 1 and self.tile_width > 1: |
| 92 | + y_mid = int(self.tile_height / 2) |
| 93 | + x_mid = int(self.tile_width / 2) |
| 94 | + thread_1 = threading.Thread( |
| 95 | + target=self.draw_group, args=(x, y, 0, y_mid, 0, x_mid, scale,), |
| 96 | + ) |
| 97 | + thread_2 = threading.Thread( |
| 98 | + target=self.draw_group, |
| 99 | + args=(x, y, 0, y_mid, x_mid, self.tile_width, scale), |
| 100 | + ) |
| 101 | + thread_3 = threading.Thread( |
| 102 | + target=self.draw_group, |
| 103 | + args=(x, y, y_mid, self.tile_height, 0, x_mid, scale), |
| 104 | + ) |
| 105 | + thread_4 = threading.Thread( |
| 106 | + target=self.draw_group, |
| 107 | + args=(x, y, y_mid, self.tile_height, x_mid, self.tile_width, scale,), |
| 108 | + ) |
| 109 | + thread_1.start() |
| 110 | + thread_2.start() |
| 111 | + thread_3.start() |
| 112 | + thread_4.start() |
| 113 | + |
| 114 | + thread_1.join() |
| 115 | + thread_2.join() |
| 116 | + thread_3.join() |
| 117 | + thread_4.join() |
| 118 | + else: |
| 119 | + self.draw_group( |
| 120 | + x, y, 0, self.tile_height, 0, self.tile_width, scale, |
| 121 | + ) |
| 122 | + |
| 123 | + def draw_group(self, x, y, y_start, y_end, x_start, x_end, scale): |
| 124 | + # return |
| 125 | + for i in range(y_start, y_end): |
| 126 | + for j in range(x_start, x_end): |
87 | 127 | self.fill_pixel(i, j, x, y, scale)
|
88 | 128 |
|
89 | 129 | # helper method for drawing pixels on bmp_img
|
90 | 130 | # given the src, offset, and scale
|
91 | 131 | def fill_pixel(self, i, j, x, y, scale):
|
92 |
| - for i_new in range(scale): |
93 |
| - for j_new in range(scale): |
94 |
| - try: |
95 |
| - if x + (j * scale) + j_new >= 0 and y + (i * scale) + i_new >= 0: |
96 |
| - if not self.pixel_shader._Palette__contents[ |
97 |
| - self.bitmap[j, i] |
98 |
| - ].transparent: |
99 |
| - bmp_img[ |
100 |
| - x + (j * scale) + j_new, y + (i * scale) + i_new, |
101 |
| - ] = self.pixel_shader[self.bitmap[j, i]] |
102 |
| - except IndexError: |
103 |
| - continue |
| 132 | + |
| 133 | + curr_val = self.bitmap[j, i] |
| 134 | + transparent = self.pixel_shader._Palette__contents[curr_val].transparent |
| 135 | + if not transparent: |
| 136 | + x_offset = x + (j * scale) |
| 137 | + y_offset = y + (i * scale) |
| 138 | + x_max = min(x_offset + scale, 240) |
| 139 | + y_max = min(y_offset + scale, 240) |
| 140 | + |
| 141 | + curr_colour = self.pixel_shader[curr_val] |
| 142 | + for new_y in range(y_offset, y_max): |
| 143 | + for new_x in range(x_offset, x_max): |
| 144 | + if curr_val != bmp_img[new_x, new_y]: |
| 145 | + bmp_img[new_x, new_y] = curr_colour |
0 commit comments