1 # SPDX-FileCopyrightText: 2020 Melissa LeBlanc-Williams for Adafruit Industries
3 # SPDX-License-Identifier: MIT
7 ================================================================================
11 **Software and Dependencies:**
14 https://github.com/adafruit/Adafruit_Blinka/releases
16 * Author(s): Melissa LeBlanc-Williams
21 from typing import Union, Optional, Tuple
22 from circuitpython_typing import WriteableBuffer
23 from ._bitmap import Bitmap
24 from ._colorconverter import ColorConverter
25 from ._ondiskbitmap import OnDiskBitmap
26 from ._shape import Shape
27 from ._palette import Palette
28 from ._structs import (
33 from ._colorspace import Colorspace
34 from ._area import Area
36 __version__ = "0.0.0+auto.0"
37 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
41 # pylint: disable=too-many-instance-attributes, too-many-statements
42 """Position a grid of tiles sourced from a bitmap and pixel_shader combination. Multiple
43 grids can share bitmaps and pixel shaders.
45 A single tile grid is also known as a Sprite.
50 bitmap: Union[Bitmap, OnDiskBitmap, Shape],
52 pixel_shader: Union[ColorConverter, Palette],
55 tile_width: Optional[int] = None,
56 tile_height: Optional[int] = None,
57 default_tile: int = 0,
61 """Create a TileGrid object. The bitmap is source for 2d pixels. The pixel_shader is
62 used to convert the value and its location to a display native pixel color. This may
63 be a simple color palette lookup, a gradient, a pattern or a color transformer.
65 tile_width and tile_height match the height of the bitmap by default.
67 if not isinstance(bitmap, (Bitmap, OnDiskBitmap, Shape)):
68 raise ValueError("Unsupported Bitmap type")
70 bitmap_width = bitmap.width
71 bitmap_height = bitmap.height
73 if pixel_shader is not None and not isinstance(
74 pixel_shader, (ColorConverter, Palette)
76 raise ValueError("Unsupported Pixel Shader type")
77 self._pixel_shader = pixel_shader
78 if isinstance(self._pixel_shader, ColorConverter):
79 self._pixel_shader._rgba = True # pylint: disable=protected-access
80 self._hidden_tilegrid = False
81 self._hidden_by_parent = False
82 self._rendered_hidden = False
83 self._name = "Tilegrid"
86 self._width_in_tiles = width
87 self._height_in_tiles = height
88 self._transpose_xy = False
93 if tile_width is None or tile_width == 0:
94 tile_width = bitmap_width
95 if tile_height is None or tile_width == 0:
96 tile_height = bitmap_height
97 if tile_width < 1 or tile_height < 1:
98 raise ValueError("Tile width and height must be greater than 0")
99 if bitmap_width % tile_width != 0:
100 raise ValueError("Tile width must exactly divide bitmap width")
101 self._tile_width = tile_width
102 if bitmap_height % tile_height != 0:
103 raise ValueError("Tile height must exactly divide bitmap height")
104 self._tile_height = tile_height
105 if not 0 <= default_tile <= 255:
106 raise ValueError("Default Tile is out of range")
107 self._pixel_width = width * tile_width
108 self._pixel_height = height * tile_height
109 self._tiles = bytearray(
110 (self._width_in_tiles * self._height_in_tiles) * [default_tile]
112 self._in_group = False
113 self._absolute_transform = None
114 self._current_area = Area(0, 0, self._pixel_width, self._pixel_height)
115 self._dirty_area = Area(0, 0, 0, 0)
116 self._previous_area = Area(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF)
118 self._full_change = True
119 self._partial_change = True
120 self._bitmap_width_in_tiles = bitmap_width // tile_width
121 self._tiles_in_bitmap = self._bitmap_width_in_tiles * (
122 bitmap_height // tile_height
125 def _update_transform(self, absolute_transform):
126 """Update the parent transform and child transforms"""
127 self._in_group = absolute_transform is not None
128 self._absolute_transform = absolute_transform
129 if self._absolute_transform is not None:
131 self._update_current_x()
132 self._update_current_y()
134 def _update_current_x(self):
135 if self._transpose_xy:
136 width = self._pixel_height
138 width = self._pixel_width
140 absolute_transform = (
142 if self._absolute_transform is None
143 else self._absolute_transform
146 if absolute_transform.transpose_xy:
147 self._current_area.y1 = (
148 absolute_transform.y + absolute_transform.dy * self._x
150 self._current_area.y2 = absolute_transform.y + absolute_transform.dy * (
153 if self._current_area.y2 < self._current_area.y1:
154 self._current_area.y1, self._current_area.y2 = (
155 self._current_area.y2,
156 self._current_area.y1,
159 self._current_area.x1 = (
160 absolute_transform.x + absolute_transform.dx * self._x
162 self._current_area.x2 = absolute_transform.x + absolute_transform.dx * (
165 if self._current_area.x2 < self._current_area.x1:
166 self._current_area.x1, self._current_area.x2 = (
167 self._current_area.x2,
168 self._current_area.x1,
171 def _update_current_y(self):
172 if self._transpose_xy:
173 height = self._pixel_width
175 height = self._pixel_height
177 absolute_transform = (
179 if self._absolute_transform is None
180 else self._absolute_transform
183 if absolute_transform.transpose_xy:
184 self._current_area.x1 = (
185 absolute_transform.x + absolute_transform.dx * self._y
187 self._current_area.x2 = absolute_transform.x + absolute_transform.dx * (
190 if self._current_area.x2 < self._current_area.x1:
191 self._current_area.x1, self._current_area.x2 = (
192 self._current_area.x2,
193 self._current_area.x1,
196 self._current_area.y1 = (
197 absolute_transform.y + absolute_transform.dy * self._y
199 self._current_area.y2 = absolute_transform.y + absolute_transform.dy * (
202 if self._current_area.y2 < self._current_area.y1:
203 self._current_area.y1, self._current_area.y2 = (
204 self._current_area.y2,
205 self._current_area.y1,
208 def _shade(self, pixel_value):
209 if isinstance(self._pixel_shader, Palette):
210 return self._pixel_shader[pixel_value]
211 if isinstance(self._pixel_shader, ColorConverter):
212 return self._pixel_shader.convert(pixel_value)
215 def _apply_palette(self, image):
217 self._pixel_shader._get_palette() # pylint: disable=protected-access
220 def _add_alpha(self, image):
221 alpha = self._bitmap._image.copy().convert( # pylint: disable=protected-access
225 self._pixel_shader._get_alpha_palette() # pylint: disable=protected-access
227 image.putalpha(alpha.convert("L"))
231 colorspace: Colorspace,
233 mask: WriteableBuffer,
234 buffer: WriteableBuffer,
236 """Draw onto the image"""
237 # pylint: disable=too-many-locals,too-many-branches,too-many-statements
239 # If no tiles are present we have no impact
242 if tiles is None or len(tiles) == 0:
245 if self._hidden_tilegrid or self._hidden_by_parent:
247 overlap = Area() # area, current_area, overlap
248 if not area.compute_overlap(self._current_area, overlap):
251 # print("Checking", area.x1, area.y1, area.x2, area.y2)
252 # print("Overlap", overlap.x1, overlap.y1, overlap.x2, overlap.y2)
254 if self._bitmap.width <= 0 or self._bitmap.height <= 0:
258 y_stride = area.width()
260 flip_x = self._flip_x
261 flip_y = self._flip_y
262 if self._transpose_xy != self._absolute_transform.transpose_xy:
263 flip_x, flip_y = flip_y, flip_x
266 if (self._absolute_transform.dx < 0) != flip_x:
267 start += (area.x2 - area.x1 - 1) * x_stride
269 if (self._absolute_transform.dy < 0) != flip_y:
270 start += (area.y2 - area.y1 - 1) * y_stride
273 # Track if this layer finishes filling in the given area. We can ignore any remaining
274 # layers at that point.
275 full_coverage = area == overlap
278 area.transform_within(
279 flip_x != (self._absolute_transform.dx < 0),
280 flip_y != (self._absolute_transform.dy < 0),
281 self.transpose_xy != self._absolute_transform.transpose_xy,
287 start_x = transformed.x1 - self._current_area.x1
288 end_x = transformed.x2 - self._current_area.x1
289 start_y = transformed.y1 - self._current_area.y1
290 end_y = transformed.y2 - self._current_area.y1
292 if (self._absolute_transform.dx < 0) != flip_x:
293 x_shift = area.x2 - overlap.x2
295 x_shift = overlap.x1 - area.x1
296 if (self._absolute_transform.dy < 0) != flip_y:
297 y_shift = area.y2 - overlap.y2
299 y_shift = overlap.y1 - area.y1
301 # This untransposes x and y so it aligns with bitmap rows
302 if self._transpose_xy != self._absolute_transform.transpose_xy:
303 x_stride, y_stride = y_stride, x_stride
304 x_shift, y_shift = y_shift, x_shift
306 pixels_per_byte = 8 // colorspace.depth
308 input_pixel = InputPixelStruct()
309 output_pixel = OutputPixelStruct()
310 for input_pixel.y in range(start_y, end_y):
312 start + (input_pixel.y - start_y + y_shift) * y_stride
314 local_y = input_pixel.y // self._absolute_transform.scale
315 for input_pixel.x in range(start_x, end_x):
316 # Compute the destination pixel in the buffer and mask based on the transformations
318 row_start + (input_pixel.x - start_x + x_shift) * x_stride
321 # Check the mask first to see if the pixel has already been set
322 if mask[offset // 8] & (1 << (offset % 8)):
324 local_x = input_pixel.x // self._absolute_transform.scale
326 (local_y // self._tile_height + self._top_left_y)
327 % self._height_in_tiles
328 ) * self._width_in_tiles + (
329 local_x // self._tile_width + self._top_left_x
330 ) % self._width_in_tiles
331 input_pixel.tile = tiles[tile_location]
332 input_pixel.tile_x = (
333 input_pixel.tile % self._bitmap_width_in_tiles
334 ) * self._tile_width + local_x % self._tile_width
335 input_pixel.tile_y = (
336 input_pixel.tile // self._bitmap_width_in_tiles
337 ) * self._tile_height + local_y % self._tile_height
339 output_pixel.pixel = 0
340 input_pixel.pixel = 0
342 # We always want to read bitmap pixels by row first and then transpose into
343 # the destination buffer because most bitmaps are row associated.
344 if isinstance(self._bitmap, (Bitmap, Shape, OnDiskBitmap)):
345 input_pixel.pixel = (
346 self._bitmap._get_pixel( # pylint: disable=protected-access
347 input_pixel.tile_x, input_pixel.tile_y
351 output_pixel.opaque = True
352 if self._pixel_shader is None:
353 output_pixel.pixel = input_pixel.pixel
354 elif isinstance(self._pixel_shader, Palette):
355 self._pixel_shader._get_color( # pylint: disable=protected-access
356 colorspace, input_pixel, output_pixel
358 elif isinstance(self._pixel_shader, ColorConverter):
359 self._pixel_shader._convert( # pylint: disable=protected-access
360 colorspace, input_pixel, output_pixel
363 if not output_pixel.opaque:
364 full_coverage = False
366 mask[offset // 32] |= 1 << (offset % 32)
367 if colorspace.depth == 16:
374 elif colorspace.depth == 32:
381 elif colorspace.depth == 8:
382 buffer[offset] = output_pixel.pixel & 0xFF
383 elif colorspace.depth < 8:
384 # Reorder the offsets to pack multiple rows into
385 # a byte (meaning they share a column).
386 if not colorspace.pixels_in_byte_share_row:
388 row = offset // width
390 # Dividing by pixels_per_byte does truncated division
391 # even if we multiply it back out
393 col * pixels_per_byte
394 + (row // pixels_per_byte) * pixels_per_byte * width
395 + (row % pixels_per_byte)
397 shift = (offset % pixels_per_byte) * colorspace.depth
398 if colorspace.reverse_pixels_in_byte:
399 # Reverse the shift by subtracting it from the leftmost shift
400 shift = (pixels_per_byte - 1) * colorspace.depth - shift
401 buffer[offset // pixels_per_byte] |= output_pixel.pixel << shift
405 def _finish_refresh(self):
406 first_draw = self._previous_area.x1 == self._previous_area.x2
407 hidden = self._hidden_tilegrid or self._hidden_by_parent
408 if not first_draw and hidden:
409 self._previous_area.x2 = self._previous_area.x1
410 elif self._moved or first_draw:
411 self._current_area.copy_into(self._previous_area)
414 self._full_change = False
415 self._partial_change = False
416 if isinstance(self._pixel_shader, (Palette, ColorConverter)):
417 self._pixel_shader._finish_refresh() # pylint: disable=protected-access
418 if isinstance(self._bitmap, (Bitmap, Shape)):
419 self._bitmap._finish_refresh() # pylint: disable=protected-access
421 def _get_refresh_areas(self, areas: list[Area]) -> None:
422 # pylint: disable=invalid-name, too-many-branches, too-many-statements
423 first_draw = self._previous_area.x1 == self._previous_area.x2
424 hidden = self._hidden_tilegrid or self._hidden_by_parent
426 # Check hidden first because it trumps all other changes
428 self._rendered_hidden = True
430 areas.append(self._previous_area)
432 if self._moved and not first_draw:
433 self._previous_area.union(self._current_area, self._dirty_area)
434 if self._dirty_area.size() < 2 * self._pixel_width * self._pixel_height:
435 areas.append(self._dirty_area)
437 areas.append(self._current_area)
438 areas.append(self._previous_area)
441 tail = areas[-1] if areas else None
442 # If we have an in-memory bitmap, then check it for modifications
443 if isinstance(self._bitmap, Bitmap):
444 self._bitmap._get_refresh_areas(areas) # pylint: disable=protected-access
445 refresh_area = areas[-1] if areas else None
446 if refresh_area != tail:
447 # Special case a TileGrid that shows a full bitmap and use its
448 # dirty area. Copy it to ours so we can transform it.
449 if self._tiles_in_bitmap == 1:
450 refresh_area.copy_into(self._dirty_area)
451 self._partial_change = True
453 self._full_change = True
454 elif isinstance(self._bitmap, Shape):
455 self._bitmap._get_refresh_areas(areas) # pylint: disable=protected-access
456 refresh_area = areas[-1] if areas else None
457 if refresh_area != tail:
458 refresh_area.copy_into(self._dirty_area)
459 self._partial_change = True
461 self._full_change = self._full_change or (
462 isinstance(self._pixel_shader, (Palette, ColorConverter))
463 and self._pixel_shader._needs_refresh # pylint: disable=protected-access
465 if self._full_change or first_draw:
466 areas.append(self._current_area)
469 if self._partial_change:
472 if self._absolute_transform.transpose_xy:
474 x1 = self._dirty_area.x1
475 x2 = self._dirty_area.x2
477 x1 = self._pixel_width - x1
478 x2 = self._pixel_width - x2
479 y1 = self._dirty_area.y1
480 y2 = self._dirty_area.y2
482 y1 = self._pixel_height - y1
483 y2 = self._pixel_height - y2
484 if self._transpose_xy != self._absolute_transform.transpose_xy:
487 self._dirty_area.x1 = (
488 self._absolute_transform.x + self._absolute_transform.dx * (x + x1)
490 self._dirty_area.y1 = (
491 self._absolute_transform.y + self._absolute_transform.dy * (y + y1)
493 self._dirty_area.x2 = (
494 self._absolute_transform.x + self._absolute_transform.dx * (x + x2)
496 self._dirty_area.y2 = (
497 self._absolute_transform.y + self._absolute_transform.dy * (y + y2)
499 if self._dirty_area.y2 < self._dirty_area.y1:
500 self._dirty_area.y1, self._dirty_area.y2 = (
504 if self._dirty_area.x2 < self._dirty_area.x1:
505 self._dirty_area.x1, self._dirty_area.x2 = (
509 areas.append(self._dirty_area)
511 def _set_hidden(self, hidden: bool) -> None:
512 self._hidden_tilegrid = hidden
513 self._rendered_hidden = False
515 self._full_change = True
517 def _set_hidden_by_parent(self, hidden: bool) -> None:
518 self._hidden_by_parent = hidden
519 self._rendered_hidden = False
521 self._full_change = True
523 def _get_rendered_hidden(self) -> bool:
524 return self._rendered_hidden
526 def _set_all_tiles(self, tile_index: int) -> None:
527 """Set all tiles to the given tile index"""
528 if tile_index >= self._tiles_in_bitmap:
529 raise ValueError("Tile index out of bounds")
530 self._tiles = bytearray(
531 (self._width_in_tiles * self._height_in_tiles) * [tile_index]
533 self._full_change = True
535 def _set_tile(self, x: int, y: int, tile_index: int) -> None:
536 self._tiles[y * self._width_in_tiles + x] = tile_index
538 if not self._partial_change:
539 tile_area = self._dirty_area
541 tile_area = temp_area
542 top_x = (x - self._top_left_x) % self._width_in_tiles
544 top_x += self._width_in_tiles
545 tile_area.x1 = top_x * self._tile_width
546 tile_area.x2 = tile_area.x1 + self._tile_width
547 top_y = (y - self._top_left_y) % self._height_in_tiles
549 top_y += self._height_in_tiles
550 tile_area.y1 = top_y * self._tile_height
551 tile_area.y2 = tile_area.y1 + self._tile_height
553 if self._partial_change:
554 self._dirty_area.union(temp_area, self._dirty_area)
556 self._partial_change = True
558 def _set_top_left(self, x: int, y: int) -> None:
561 self._full_change = True
564 def hidden(self) -> bool:
565 """True when the TileGrid is hidden. This may be False even
566 when a part of a hidden Group."""
567 return self._hidden_tilegrid
570 def hidden(self, value: bool):
571 if not isinstance(value, (bool, int)):
572 raise ValueError("Expecting a boolean or integer value")
574 self._set_hidden(value)
578 """X position of the left edge in the parent."""
582 def x(self, value: int):
583 if not isinstance(value, int):
584 raise TypeError("X should be a integer type")
588 if self._absolute_transform is not None:
589 self._update_current_x()
593 """Y position of the top edge in the parent."""
597 def y(self, value: int):
598 if not isinstance(value, int):
599 raise TypeError("Y should be a integer type")
603 if self._absolute_transform is not None:
604 self._update_current_y()
607 def flip_x(self) -> bool:
608 """If true, the left edge rendered will be the right edge of the right-most tile."""
612 def flip_x(self, value: bool):
613 if not isinstance(value, bool):
614 raise TypeError("Flip X should be a boolean type")
615 if self._flip_x != value:
617 self._full_change = True
620 def flip_y(self) -> bool:
621 """If true, the top edge rendered will be the bottom edge of the bottom-most tile."""
625 def flip_y(self, value: bool):
626 if not isinstance(value, bool):
627 raise TypeError("Flip Y should be a boolean type")
628 if self._flip_y != value:
630 self._full_change = True
633 def transpose_xy(self) -> bool:
634 """If true, the TileGrid's axis will be swapped. When combined with mirroring, any 90
635 degree rotation can be achieved along with the corresponding mirrored version.
637 return self._transpose_xy
640 def transpose_xy(self, value: bool) -> None:
641 if not isinstance(value, bool):
642 raise TypeError("Transpose XY should be a boolean type")
643 if self._transpose_xy != value:
644 self._transpose_xy = value
645 if self._pixel_width == self._pixel_height:
646 self._full_change = True
648 self._update_current_x()
649 self._update_current_y()
653 def pixel_shader(self) -> Union[ColorConverter, Palette]:
654 """The pixel shader of the tilegrid."""
655 return self._pixel_shader
658 def pixel_shader(self, new_pixel_shader: Union[ColorConverter, Palette]) -> None:
659 if not isinstance(new_pixel_shader, ColorConverter) and not isinstance(
660 new_pixel_shader, Palette
663 "Unsupported Type: new_pixel_shader must be ColorConverter or Palette"
666 self._pixel_shader = new_pixel_shader
667 self._full_change = True
670 def bitmap(self) -> Union[Bitmap, OnDiskBitmap, Shape]:
671 """The Bitmap, OnDiskBitmap, or Shape that is assigned to this TileGrid"""
675 def bitmap(self, new_bitmap: Union[Bitmap, OnDiskBitmap, Shape]) -> None:
677 not isinstance(new_bitmap, Bitmap)
678 and not isinstance(new_bitmap, OnDiskBitmap)
679 and not isinstance(new_bitmap, Shape)
682 "Unsupported Type: new_bitmap must be Bitmap, OnDiskBitmap, or Shape"
686 new_bitmap.width != self.bitmap.width
687 or new_bitmap.height != self.bitmap.height
689 raise ValueError("New bitmap must be same size as old bitmap")
691 self._bitmap = new_bitmap
692 self._full_change = True
694 def _extract_and_check_index(self, index):
695 if isinstance(index, (tuple, list)):
698 index = y * self._width_in_tiles + x
699 elif isinstance(index, int):
700 x = index % self._width_in_tiles
701 y = index // self._width_in_tiles
703 x > self._width_in_tiles
704 or y > self._height_in_tiles
705 or index >= len(self._tiles)
707 raise ValueError("Tile index out of bounds")
710 def __getitem__(self, index: Union[Tuple[int, int], int]) -> int:
711 """Returns the tile index at the given index. The index can either be
712 an x,y tuple or an int equal to ``y * width + x``'.
714 x, y = self._extract_and_check_index(index)
715 return self._tiles[y * self._width_in_tiles + x]
717 def __setitem__(self, index: Union[Tuple[int, int], int], value: int) -> None:
718 """Sets the tile index at the given index. The index can either be
719 an x,y tuple or an int equal to ``y * width + x``.
721 x, y = self._extract_and_check_index(index)
722 if not 0 <= value <= 255:
723 raise ValueError("Tile value out of bounds")
724 self._set_tile(x, y, value)
727 def width(self) -> int:
729 return self._width_in_tiles
732 def height(self) -> int:
733 """Height in tiles"""
734 return self._height_in_tiles
737 def tile_width(self) -> int:
738 """Width of each tile in pixels"""
739 return self._tile_width
742 def tile_height(self) -> int:
743 """Height of each tile in pixels"""
744 return self._tile_height