-
Notifications
You must be signed in to change notification settings - Fork 21
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,6 @@ | |
|
||
.. automodule:: adafruit_imageload.bmp.indexed | ||
:members: | ||
|
||
.. automodule:: adafruit_imageload.tilegrid_inflator | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I always forget to do this, nice! |
||
:members: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.