Skip to content

Commit 7c18538

Browse files
authored
Merge pull request #58 from FoamyGuy/tilegrid_inflator
adding tilegrid_inflator
2 parents 18ef776 + eaabf2d commit 7c18538

File tree

6 files changed

+131
-0
lines changed

6 files changed

+131
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

docs/api.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@
1212

1313
.. automodule:: adafruit_imageload.bmp.indexed
1414
:members:
15+
16+
.. automodule:: adafruit_imageload.tilegrid_inflator
17+
:members:

docs/examples.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,12 @@ Loads image that is fetched using adafruit_request
1515
.. literalinclude:: ../examples/imageload_from_web.py
1616
:caption: examples/imageload_from_web.py
1717
:linenos:
18+
19+
Inflate TileGrid test
20+
---------------------
21+
22+
Load 3x3 spritesheet and inflate it to a larger sized TileGrid
23+
24+
.. literalinclude:: ../examples/imageload_tilegrid_inflator_simpletest.py
25+
:caption: examples/imageload_tilegrid_inflator_simpletest.py
26+
:linenos:
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import board
5+
import displayio
6+
import adafruit_imageload
7+
from adafruit_imageload.tilegrid_inflator import inflate_tilegrid
8+
9+
image, palette = adafruit_imageload.load("images/castle_spritesheet.bmp")
10+
tile_grid = inflate_tilegrid(bmp_obj=image, bmp_palette=palette, target_size=(10, 8))
11+
12+
group = displayio.Group()
13+
group.append(tile_grid)
14+
board.DISPLAY.show(group)
15+
16+
while True:
17+
pass
1.28 KB
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT

0 commit comments

Comments
 (0)