Skip to content

Commit 18b60df

Browse files
authored
Merge pull request #1 from adafruit/master
Merge from adafruit
2 parents 21755ff + 32f1768 commit 18b60df

File tree

12 files changed

+147
-14
lines changed

12 files changed

+147
-14
lines changed

.github/workflows/build.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,9 @@ jobs:
5555
- name: Build docs
5656
working-directory: docs
5757
run: sphinx-build -E -W -b html . _build/html
58+
- name: Build Python package
59+
run: |
60+
pip install --upgrade setuptools wheel twine readme_renderer testresources
61+
python setup.py sdist
62+
python setup.py bdist_wheel --universal
63+
twine check dist/*

.github/workflows/test.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Run Tests
2+
3+
on: [pull_request, push]
4+
5+
jobs:
6+
test:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- name: Set up Python 3.6
10+
uses: actions/setup-python@v1
11+
with:
12+
python-version: 3.6
13+
- name: Versions
14+
run: |
15+
python3 --version
16+
- name: Checkout Current Repo
17+
uses: actions/checkout@v1
18+
with:
19+
submodules: true
20+
- name: Install pytest
21+
run: pip install pytest
22+
- name: Install locally
23+
run: pip install .
24+
- name: Run tests
25+
run: pytest

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ bundles
66
.idea/
77
venv/
88
.vscode
9+
.DS_STORE

README.rst

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Introduction
66
:alt: Documentation Status
77

88
.. image:: https://img.shields.io/discord/327254708534116352.svg
9-
:target: https://discord.gg/nBQh6qu
9+
:target: https://adafru.it/discord
1010
:alt: Discord
1111

1212
.. image:: https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad/workflows/Build%20CI/badge.svg
@@ -17,10 +17,30 @@ This library decodes an image file into new bitmap and palette objects of the pr
1717
designed to load code needed during decoding as needed. This is meant to minimize the memory
1818
overhead of the decoding code.
1919

20+
Only certain types of bitmaps work with this library, and they often have to be exported in specific ways. To find out what types are supported and how to make them, see `this learn guide page.
21+
<https://learn.adafruit.com/creating-your-first-tilemap-game-with-circuitpython/indexed-bmp-graphics>`_
22+
2023
Usage Example
2124
=============
2225

23-
.. literalinclude:: ../examples/imageload_simpletest.py
26+
.. code-block:: python
27+
28+
import board
29+
import displayio
30+
import adafruit_imageload
31+
32+
image, palette = adafruit_imageload.load(
33+
"images/4bit.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette
34+
)
35+
tile_grid = displayio.TileGrid(image, pixel_shader=palette)
36+
37+
group = displayio.Group()
38+
group.append(tile_grid)
39+
board.DISPLAY.show(group)
40+
41+
while True:
42+
pass
43+
2444
2545
Contributing
2646
============

adafruit_imageload/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ def load(filename, *, bitmap=None, palette=None):
4343
palette is the desired pallete type. The constructor should take the number of colors and
4444
support assignment to indices via [].
4545
"""
46+
if not bitmap or not palette:
47+
try:
48+
# use displayio if available
49+
import displayio
50+
51+
if not bitmap:
52+
bitmap = displayio.Bitmap
53+
if not palette:
54+
palette = displayio.Palette
55+
except ModuleNotFoundError:
56+
# meh, we tried
57+
pass
58+
4659
with open(filename, "rb") as file:
4760
header = file.read(3)
4861
file.seek(0)

adafruit_imageload/bmp/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ def load(file, *, bitmap=None, palette=None):
5050
# print(bmp_header_length)
5151
file.seek(0x12) # Width of the bitmap in pixels
5252
width = int.from_bytes(file.read(4), "little")
53-
height = int.from_bytes(file.read(4), "little")
53+
try:
54+
height = int.from_bytes(file.read(4), "little")
55+
except OverflowError as error:
56+
raise NotImplementedError(
57+
"Negative height BMP files are not supported on builds without longint"
58+
) from error
5459
file.seek(0x1C) # Number of bits per pixel
5560
color_depth = int.from_bytes(file.read(2), "little")
5661
file.seek(0x1E) # Compression type

adafruit_imageload/bmp/indexed.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
__version__ = "0.0.0-auto.0"
3333
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
3434

35+
import sys
36+
3537

3638
def load(
3739
file,
@@ -71,9 +73,12 @@ def load(
7173
while colors > 2 ** minimum_color_depth:
7274
minimum_color_depth *= 2
7375

74-
# convert unsigned int to signed int when height is negative
75-
if height > 0x7FFFFFFF:
76-
height = height - 4294967296
76+
if sys.maxsize > 1073741823:
77+
# pylint: disable=import-outside-toplevel
78+
from .negative_height_check import negative_height_check
79+
80+
# convert unsigned int to signed int when height is negative
81+
height = negative_height_check(height)
7782
bitmap = bitmap(width, abs(height), colors)
7883
file.seek(data_start)
7984
line_size = width // (8 // color_depth)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
Check for negative height on the BMP.
3+
Seperated into it's own file to support builds
4+
without longint.
5+
"""
6+
7+
8+
def negative_height_check(height):
9+
"""Check the height return modified if negative."""
10+
if height > 0x7FFFFFFF:
11+
return height - 4294967296
12+
return height

examples/imageload_simpletest.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
import displayio
33
import adafruit_imageload
44

5-
image, palette = adafruit_imageload.load(
6-
"images/4bit.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette
7-
)
5+
image, palette = adafruit_imageload.load("images/4bit.bmp")
6+
87
tile_grid = displayio.TileGrid(image, pixel_shader=palette)
98

109
group = displayio.Group()

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
1+
Adafruit-Blinka
2+
adafruit-blinka-displayio

setup.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""A setuptools based setup module.
2+
See:
3+
https://packaging.python.org/en/latest/distributing.html
4+
https://github.com/pypa/sampleproject
5+
"""
6+
7+
from setuptools import setup, find_packages
8+
9+
# To use a consistent encoding
10+
from codecs import open
11+
from os import path
12+
13+
here = path.abspath(path.dirname(__file__))
14+
15+
# Get the long description from the README file
16+
with open(path.join(here, "README.rst"), encoding="utf-8") as f:
17+
long_description = f.read()
18+
19+
setup(
20+
name="adafruit-circuitpython-imageload",
21+
use_scm_version=True,
22+
setup_requires=["setuptools_scm"],
23+
description="Displays text using CircuitPython's displayio.",
24+
long_description=long_description,
25+
long_description_content_type="text/x-rst",
26+
# The project's main homepage.
27+
url="https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad",
28+
# Author details
29+
author="Adafruit Industries",
30+
author_email="[email protected]",
31+
install_requires=["Adafruit-Blinka", "adafruit-blinka-displayio",],
32+
# Choose your license
33+
license="MIT",
34+
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
35+
classifiers=[
36+
"Development Status :: 3 - Alpha",
37+
"Intended Audience :: Developers",
38+
"Topic :: Software Development :: Libraries",
39+
"Topic :: System :: Hardware",
40+
"License :: OSI Approved :: MIT License",
41+
"Programming Language :: Python :: 3",
42+
"Programming Language :: Python :: 3.4",
43+
"Programming Language :: Python :: 3.5",
44+
],
45+
# What does your project relate to?
46+
keywords="adafruit blinka circuitpython micropython bitmap fonts text display tft lcd displayio imageload image",
47+
# You can just specify the packages manually here if your project is
48+
# simple. Or you can use find_packages().
49+
packages=["adafruit_imageload"],
50+
)

setup.py.disabled

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)