From 0d75d6cb5b0928e5e0cf2d7b5087aaa9f5e2fec1 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Fri, 22 Apr 2022 16:53:23 -0500 Subject: [PATCH 1/4] adding tilegird_inflator --- adafruit_imageload/tilegrid_inflator.py | 100 ++++++++++++++++++++++++ docs/api.rst | 3 + 2 files changed, 103 insertions(+) create mode 100644 adafruit_imageload/tilegrid_inflator.py diff --git a/adafruit_imageload/tilegrid_inflator.py b/adafruit_imageload/tilegrid_inflator.py new file mode 100644 index 0000000..7f00735 --- /dev/null +++ b/adafruit_imageload/tilegrid_inflator.py @@ -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 string bmp_path: filepath to the 3x3 spritesheet bitmap file + :param 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 Union[tuple, int] transparent_index: a single index within the palette to + make transparent, or a tuple of multiple indexes to make transparent + :param OnDiskBitmap bmp_obj: Already loaded 3x3 spritesheet in an OnDiskBitmap + :param 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 diff --git a/docs/api.rst b/docs/api.rst index 5ef6ad1..0cf10aa 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -12,3 +12,6 @@ .. automodule:: adafruit_imageload.bmp.indexed :members: + +.. automodule:: adafruit_imageload.tilegrid_inflator + :members: From a4672c66897d717a31a10cca9af44fffb91639d9 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Sat, 23 Apr 2022 14:02:36 -0500 Subject: [PATCH 2/4] adding example for inflator --- .../imageload_tilegrid_inflator_simpletest.py | 17 +++++++++++++++++ examples/images/castle_spritesheet.bmp | Bin 0 -> 1310 bytes examples/images/castle_spritesheet.bmp.license | 2 ++ 3 files changed, 19 insertions(+) create mode 100644 examples/imageload_tilegrid_inflator_simpletest.py create mode 100644 examples/images/castle_spritesheet.bmp create mode 100644 examples/images/castle_spritesheet.bmp.license diff --git a/examples/imageload_tilegrid_inflator_simpletest.py b/examples/imageload_tilegrid_inflator_simpletest.py new file mode 100644 index 0000000..5024b6b --- /dev/null +++ b/examples/imageload_tilegrid_inflator_simpletest.py @@ -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 diff --git a/examples/images/castle_spritesheet.bmp b/examples/images/castle_spritesheet.bmp new file mode 100644 index 0000000000000000000000000000000000000000..587bef7aeb02a6d67ccee3488ac2acf3e11d0c00 GIT binary patch literal 1310 zcmb7EJ5Izf5FIC>qHz*xl=uW$c`InbZX{@Ekm$Gt1rnm*0$hO}F2Nx<5WF`NCrbzg z-WhxIGJf;MpX{zrk3`!m`X_idx|2fjXAJeRIYTY{OTG0X<&pi|zz#*^5io-o;+a84J;gwQAQ&?w{g`FK6=zP(OP(K%`VZ?0ZB>Of1f9XW z+txb|X9;DgYE%z!{u0hrAh9zX*{zXX*9W-B6kkqtZCjwNt`PZ?9_C%*4RP zriZKMs#^DpGyaMnX*BPOPO7){U+5gyA4*UaDmdhs^-Ma+&wbbRgc>hSr1UzS6uPQ!TH1HiWjGIk41GE$dJ1vR?9lMO7~{{LIQYT9EYO28m}!Z#x`f*~ zCozsAECWED>n+_QJEH;~Tq{qV$Y;ia6t}$KH3K&iXcFqNdyW30A9`GHcuyQ30khcQ Vhff1daeg=QjXn~uiLa{^`3BCNV*mgE literal 0 HcmV?d00001 diff --git a/examples/images/castle_spritesheet.bmp.license b/examples/images/castle_spritesheet.bmp.license new file mode 100644 index 0000000..a784acf --- /dev/null +++ b/examples/images/castle_spritesheet.bmp.license @@ -0,0 +1,2 @@ +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-License-Identifier: MIT From 0815e7863b5fdbd2190ce8b5ec79d0554701ad2c Mon Sep 17 00:00:00 2001 From: foamyguy Date: Sat, 23 Apr 2022 14:06:18 -0500 Subject: [PATCH 3/4] add example to docs --- docs/examples.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/examples.rst b/docs/examples.rst index 039d649..6dc236d 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -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 + :caption: examples/imageload_tilegrid_inflator_simpletest.py + :linenos: From eaabf2d771c22e718e0dcf59e7a485e7e15c847f Mon Sep 17 00:00:00 2001 From: foamyguy Date: Wed, 27 Apr 2022 18:51:25 -0500 Subject: [PATCH 4/4] fix str type and add Optionals for some arguments. --- adafruit_imageload/tilegrid_inflator.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/adafruit_imageload/tilegrid_inflator.py b/adafruit_imageload/tilegrid_inflator.py index 7f00735..2cb6aaf 100644 --- a/adafruit_imageload/tilegrid_inflator.py +++ b/adafruit_imageload/tilegrid_inflator.py @@ -32,14 +32,14 @@ def inflate_tilegrid( inflate a TileGrid of ``target_size`` in tiles from a 3x3 spritesheet by duplicating the center rows and columns. - :param string bmp_path: filepath to the 3x3 spritesheet bitmap file - :param tuple target_size: desired size in tiles (target_width, target_height) + :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 Union[tuple, int] transparent_index: a single index within the palette to + :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 OnDiskBitmap bmp_obj: Already loaded 3x3 spritesheet in an OnDiskBitmap - :param Palette bmp_palette: Already loaded spritesheet Palette + :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