Skip to content

Merge from adafruit #1

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 20 commits into from
Oct 25, 2020
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
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,9 @@ jobs:
- name: Build docs
working-directory: docs
run: sphinx-build -E -W -b html . _build/html
- name: Build Python package
run: |
pip install --upgrade setuptools wheel twine readme_renderer testresources
python setup.py sdist
python setup.py bdist_wheel --universal
twine check dist/*
25 changes: 25 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Run Tests

on: [pull_request, push]

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Set up Python 3.6
uses: actions/setup-python@v1
with:
python-version: 3.6
- name: Versions
run: |
python3 --version
- name: Checkout Current Repo
uses: actions/checkout@v1
with:
submodules: true
- name: Install pytest
run: pip install pytest
- name: Install locally
run: pip install .
- name: Run tests
run: pytest
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ bundles
.idea/
venv/
.vscode
.DS_STORE
24 changes: 22 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Introduction
:alt: Documentation Status

.. image:: https://img.shields.io/discord/327254708534116352.svg
:target: https://discord.gg/nBQh6qu
:target: https://adafru.it/discord
:alt: Discord

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

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.
<https://learn.adafruit.com/creating-your-first-tilemap-game-with-circuitpython/indexed-bmp-graphics>`_

Usage Example
=============

.. literalinclude:: ../examples/imageload_simpletest.py
.. code-block:: python

import board
import displayio
import adafruit_imageload

image, palette = adafruit_imageload.load(
"images/4bit.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette
)
tile_grid = displayio.TileGrid(image, pixel_shader=palette)

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

while True:
pass


Contributing
============
Expand Down
13 changes: 13 additions & 0 deletions adafruit_imageload/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ def load(filename, *, bitmap=None, palette=None):
palette is the desired pallete type. The constructor should take the number of colors and
support assignment to indices via [].
"""
if not bitmap or not palette:
try:
# use displayio if available
import displayio

if not bitmap:
bitmap = displayio.Bitmap
if not palette:
palette = displayio.Palette
except ModuleNotFoundError:
# meh, we tried
pass

with open(filename, "rb") as file:
header = file.read(3)
file.seek(0)
Expand Down
7 changes: 6 additions & 1 deletion adafruit_imageload/bmp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ def load(file, *, bitmap=None, palette=None):
# print(bmp_header_length)
file.seek(0x12) # Width of the bitmap in pixels
width = int.from_bytes(file.read(4), "little")
height = int.from_bytes(file.read(4), "little")
try:
height = int.from_bytes(file.read(4), "little")
except OverflowError as error:
raise NotImplementedError(
"Negative height BMP files are not supported on builds without longint"
) from error
file.seek(0x1C) # Number of bits per pixel
color_depth = int.from_bytes(file.read(2), "little")
file.seek(0x1E) # Compression type
Expand Down
11 changes: 8 additions & 3 deletions adafruit_imageload/bmp/indexed.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"

import sys


def load(
file,
Expand Down Expand Up @@ -71,9 +73,12 @@ def load(
while colors > 2 ** minimum_color_depth:
minimum_color_depth *= 2

# convert unsigned int to signed int when height is negative
if height > 0x7FFFFFFF:
height = height - 4294967296
if sys.maxsize > 1073741823:
# pylint: disable=import-outside-toplevel
from .negative_height_check import negative_height_check

# convert unsigned int to signed int when height is negative
height = negative_height_check(height)
bitmap = bitmap(width, abs(height), colors)
file.seek(data_start)
line_size = width // (8 // color_depth)
Expand Down
12 changes: 12 additions & 0 deletions adafruit_imageload/bmp/negative_height_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""
Check for negative height on the BMP.
Seperated into it's own file to support builds
without longint.
"""


def negative_height_check(height):
"""Check the height return modified if negative."""
if height > 0x7FFFFFFF:
return height - 4294967296
return height
5 changes: 2 additions & 3 deletions examples/imageload_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
import displayio
import adafruit_imageload

image, palette = adafruit_imageload.load(
"images/4bit.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette
)
image, palette = adafruit_imageload.load("images/4bit.bmp")

tile_grid = displayio.TileGrid(image, pixel_shader=palette)

group = displayio.Group()
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@

Adafruit-Blinka
adafruit-blinka-displayio
50 changes: 50 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""A setuptools based setup module.
See:
https://packaging.python.org/en/latest/distributing.html
https://github.com/pypa/sampleproject
"""

from setuptools import setup, find_packages

# To use a consistent encoding
from codecs import open
from os import path

here = path.abspath(path.dirname(__file__))

# Get the long description from the README file
with open(path.join(here, "README.rst"), encoding="utf-8") as f:
long_description = f.read()

setup(
name="adafruit-circuitpython-imageload",
use_scm_version=True,
setup_requires=["setuptools_scm"],
description="Displays text using CircuitPython's displayio.",
long_description=long_description,
long_description_content_type="text/x-rst",
# The project's main homepage.
url="https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad",
# Author details
author="Adafruit Industries",
author_email="[email protected]",
install_requires=["Adafruit-Blinka", "adafruit-blinka-displayio",],
# Choose your license
license="MIT",
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
classifiers=[
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"Topic :: Software Development :: Libraries",
"Topic :: System :: Hardware",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
],
# What does your project relate to?
keywords="adafruit blinka circuitpython micropython bitmap fonts text display tft lcd displayio imageload image",
# You can just specify the packages manually here if your project is
# simple. Or you can use find_packages().
packages=["adafruit_imageload"],
)
4 changes: 0 additions & 4 deletions setup.py.disabled

This file was deleted.