|
| 1 | +# SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +""" |
| 6 | +`adafruit_imageload.tilegrid_inflator` |
| 7 | +==================================================== |
| 8 | +
|
| 9 | +Use a 3x3 spritesheet to inflate a larger grid of tiles, duplicating the center rows and |
| 10 | +columns as many times as needed to reach a target size. |
| 11 | +
|
| 12 | +* Author(s): Tim Cocks |
| 13 | +
|
| 14 | +""" |
| 15 | + |
| 16 | +__version__ = "0.0.0-auto.0" |
| 17 | +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git" |
| 18 | + |
| 19 | +import displayio |
| 20 | +import adafruit_imageload |
| 21 | + |
| 22 | + |
| 23 | +def inflate_tilegrid( |
| 24 | + bmp_path=None, |
| 25 | + target_size=(3, 3), |
| 26 | + tile_size=None, |
| 27 | + transparent_index=None, |
| 28 | + bmp_obj=None, |
| 29 | + bmp_palette=None, |
| 30 | +): |
| 31 | + """ |
| 32 | + inflate a TileGrid of ``target_size`` in tiles from a 3x3 spritesheet by duplicating |
| 33 | + the center rows and columns. |
| 34 | +
|
| 35 | + :param Optional[str] bmp_path: filepath to the 3x3 spritesheet bitmap file |
| 36 | + :param Optional[tuple] target_size: desired size in tiles (target_width, target_height) |
| 37 | + :param Optional[tuple] tile_size: size of the tiles in the 3x3 spritesheet. If |
| 38 | + None is used it will equally divide the width and height of the Bitmap by 3. |
| 39 | + :param Optional[Union[tuple, int]] transparent_index: a single index within the palette to |
| 40 | + make transparent, or a tuple of multiple indexes to make transparent |
| 41 | + :param Optional[OnDiskBitmap] bmp_obj: Already loaded 3x3 spritesheet in an OnDiskBitmap |
| 42 | + :param Optional[Palette] bmp_palette: Already loaded spritesheet Palette |
| 43 | + """ |
| 44 | + |
| 45 | + # pylint: disable=too-many-arguments, too-many-locals, too-many-branches |
| 46 | + |
| 47 | + if bmp_path is None and (bmp_obj is None and bmp_palette is None): |
| 48 | + raise AttributeError("Must pass either bmp_path or bmp_obj and bmp_palette") |
| 49 | + |
| 50 | + if bmp_path is not None: |
| 51 | + image, palette = adafruit_imageload.load(bmp_path) |
| 52 | + else: |
| 53 | + image = bmp_obj |
| 54 | + palette = bmp_palette |
| 55 | + |
| 56 | + if transparent_index is not None: |
| 57 | + if isinstance(transparent_index, tuple): |
| 58 | + for index in transparent_index: |
| 59 | + palette.make_transparent(index) |
| 60 | + elif isinstance(transparent_index, int): |
| 61 | + palette.make_transparent(transparent_index) |
| 62 | + |
| 63 | + if tile_size is None: |
| 64 | + tile_width = image.width // 3 |
| 65 | + tile_height = image.height // 3 |
| 66 | + else: |
| 67 | + tile_width = tile_size[0] |
| 68 | + tile_height = tile_size[1] |
| 69 | + |
| 70 | + target_width = target_size[0] |
| 71 | + target_height = target_size[1] |
| 72 | + |
| 73 | + tile_grid = displayio.TileGrid( |
| 74 | + image, |
| 75 | + pixel_shader=palette, |
| 76 | + height=target_height, |
| 77 | + width=target_width, |
| 78 | + tile_width=tile_width, |
| 79 | + tile_height=tile_height, |
| 80 | + ) |
| 81 | + |
| 82 | + # corners |
| 83 | + tile_grid[0, 0] = 0 # upper left |
| 84 | + tile_grid[tile_grid.width - 1, 0] = 2 # upper right |
| 85 | + tile_grid[0, tile_grid.height - 1] = 6 # lower left |
| 86 | + tile_grid[tile_grid.width - 1, tile_grid.height - 1] = 8 # lower right |
| 87 | + |
| 88 | + for x in range(target_size[0] - 2): |
| 89 | + tile_grid[x + 1, 0] = 1 |
| 90 | + tile_grid[x + 1, tile_grid.height - 1] = 7 |
| 91 | + |
| 92 | + for y in range(target_size[1] - 2): |
| 93 | + tile_grid[0, y + 1] = 3 |
| 94 | + tile_grid[tile_grid.width - 1, y + 1] = 5 |
| 95 | + |
| 96 | + for y in range(target_size[1] - 2): |
| 97 | + for x in range(target_size[0] - 2): |
| 98 | + tile_grid[x + 1, y + 1] = 4 |
| 99 | + |
| 100 | + return tile_grid |
0 commit comments