Skip to content

adding tilegrid_inflator #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions adafruit_imageload/tilegrid_inflator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
`adafruit_imageload.tilegrid_inflator`
====================================================
Use a 3x3 spritesheet to inflate a larger grid of tiles, duplicating the center rows and
columns as many times as needed to reach a target size.
* Author(s): Tim Cocks
"""

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"

import displayio
import adafruit_imageload


def inflate_tilegrid(
bmp_path=None,
target_size=(3, 3),
tile_size=None,
transparent_index=None,
bmp_obj=None,
bmp_palette=None,
):
"""
inflate a TileGrid of ``target_size`` in tiles from a 3x3 spritesheet by duplicating
the center rows and columns.
:param Optional[str] bmp_path: filepath to the 3x3 spritesheet bitmap file
:param Optional[tuple] target_size: desired size in tiles (target_width, target_height)
:param Optional[tuple] tile_size: size of the tiles in the 3x3 spritesheet. If
None is used it will equally divide the width and height of the Bitmap by 3.
:param Optional[Union[tuple, int]] transparent_index: a single index within the palette to
make transparent, or a tuple of multiple indexes to make transparent
:param Optional[OnDiskBitmap] bmp_obj: Already loaded 3x3 spritesheet in an OnDiskBitmap
:param Optional[Palette] bmp_palette: Already loaded spritesheet Palette
"""

# pylint: disable=too-many-arguments, too-many-locals, too-many-branches

if bmp_path is None and (bmp_obj is None and bmp_palette is None):
raise AttributeError("Must pass either bmp_path or bmp_obj and bmp_palette")

if bmp_path is not None:
image, palette = adafruit_imageload.load(bmp_path)
else:
image = bmp_obj
palette = bmp_palette

if transparent_index is not None:
if isinstance(transparent_index, tuple):
for index in transparent_index:
palette.make_transparent(index)
elif isinstance(transparent_index, int):
palette.make_transparent(transparent_index)

if tile_size is None:
tile_width = image.width // 3
tile_height = image.height // 3
else:
tile_width = tile_size[0]
tile_height = tile_size[1]

target_width = target_size[0]
target_height = target_size[1]

tile_grid = displayio.TileGrid(
image,
pixel_shader=palette,
height=target_height,
width=target_width,
tile_width=tile_width,
tile_height=tile_height,
)

# corners
tile_grid[0, 0] = 0 # upper left
tile_grid[tile_grid.width - 1, 0] = 2 # upper right
tile_grid[0, tile_grid.height - 1] = 6 # lower left
tile_grid[tile_grid.width - 1, tile_grid.height - 1] = 8 # lower right

for x in range(target_size[0] - 2):
tile_grid[x + 1, 0] = 1
tile_grid[x + 1, tile_grid.height - 1] = 7

for y in range(target_size[1] - 2):
tile_grid[0, y + 1] = 3
tile_grid[tile_grid.width - 1, y + 1] = 5

for y in range(target_size[1] - 2):
for x in range(target_size[0] - 2):
tile_grid[x + 1, y + 1] = 4

return tile_grid
3 changes: 3 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@

.. automodule:: adafruit_imageload.bmp.indexed
:members:

.. automodule:: adafruit_imageload.tilegrid_inflator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always forget to do this, nice!

:members:
9 changes: 9 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ Loads image that is fetched using adafruit_request
.. literalinclude:: ../examples/imageload_from_web.py
:caption: examples/imageload_from_web.py
:linenos:

Inflate TileGrid test
---------------------

Load 3x3 spritesheet and inflate it to a larger sized TileGrid

.. literalinclude:: ../examples/imageload_tilegrid_inflator_simpletest.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooo, I haven't seen this directive, I'll have to remember this one.

:caption: examples/imageload_tilegrid_inflator_simpletest.py
:linenos:
17 changes: 17 additions & 0 deletions examples/imageload_tilegrid_inflator_simpletest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries
# SPDX-License-Identifier: MIT

import board
import displayio
import adafruit_imageload
from adafruit_imageload.tilegrid_inflator import inflate_tilegrid

image, palette = adafruit_imageload.load("images/castle_spritesheet.bmp")
tile_grid = inflate_tilegrid(bmp_obj=image, bmp_palette=palette, target_size=(10, 8))

group = displayio.Group()
group.append(tile_grid)
board.DISPLAY.show(group)

while True:
pass
Binary file added examples/images/castle_spritesheet.bmp
Binary file not shown.
2 changes: 2 additions & 0 deletions examples/images/castle_spritesheet.bmp.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT