diff --git a/adafruit_imageload/__init__.py b/adafruit_imageload/__init__.py index bd8cd98..3f632a6 100644 --- a/adafruit_imageload/__init__.py +++ b/adafruit_imageload/__init__.py @@ -47,6 +47,7 @@ def load( palette is the desired palette type. The constructor should take the number of colors and support assignment to indices via []. """ + # pylint: disable=too-many-branches if not bitmap or not palette: try: # use displayio if available @@ -89,4 +90,8 @@ def load( from . import png return png.load(file, bitmap=bitmap, palette=palette) + if header.startswith(b"\xff\xd8"): + from . import jpg + + return jpg.load(file, bitmap=bitmap) raise RuntimeError("Unsupported image format") diff --git a/adafruit_imageload/jpg.py b/adafruit_imageload/jpg.py new file mode 100644 index 0000000..9af3d60 --- /dev/null +++ b/adafruit_imageload/jpg.py @@ -0,0 +1,56 @@ +# SPDX-FileCopyrightText: 2024 Channing Ramos +# +# SPDX-License-Identifier: MIT + +""" +`adafruit_imageload.jpg` +==================================================== + +Load a JPG into a bitmap by calling the jpegio class. + +* Author(s): Channing Ramos + +""" + +# A separate try for jpegio. Not every board supports it and this import may fail. +# If that happens an ImportError with a proper message needs to be raised +try: + from jpegio import JpegDecoder +except ImportError: + print("jpegio not supported on this board.") + +try: + from io import BufferedReader + from typing import Tuple, Optional + from .displayio_types import BitmapConstructor +except ImportError: + pass + +from displayio import Bitmap, ColorConverter, Colorspace + +__version__ = "0.0.0+auto.0" +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git" + + +def load( + file: BufferedReader, + *, + bitmap: BitmapConstructor, +) -> Tuple[Bitmap, Optional[ColorConverter]]: + """ + Loads a JPG image from the open ''file''. + The JPG must be a Baseline JPG, Progressive and Lossless JPG formats are not supported. + + Returns tuple of bitmap object and ColorConverter object. + + :param io.BufferedReader file: Open file handle or compatible (like 'io.BytesIO') + :param object bitmap: Type to store bitmap data. + Must have API similar to 'displayio.Bitmap'. Will be skipped if None. + Will be skipped if None. + """ + decoder = JpegDecoder() + width, height = decoder.open(file) + bitmap_obj = bitmap(width, height, 65535) + decoder.decode(bitmap_obj) + + return bitmap_obj, ColorConverter(input_colorspace=Colorspace.RGB565_SWAPPED) diff --git a/examples/imageload_jpg_simpletest.py b/examples/imageload_jpg_simpletest.py new file mode 100644 index 0000000..dc4b8ef --- /dev/null +++ b/examples/imageload_jpg_simpletest.py @@ -0,0 +1,22 @@ +# SPDX-FileCopyrightText: 2024 Channing Ramos +# +# SPDX-License-Identifier: MIT + +""" +Basic JPG imageload example +""" + +import board +import displayio +import adafruit_imageload + +group = displayio.Group() +board.DISPLAY.root_group = group + +image, color_converter = adafruit_imageload.load("images/jpg_test.jpg") + +tile_grid = displayio.TileGrid(image, pixel_shader=color_converter) +group.append(tile_grid) + +while True: + pass diff --git a/examples/images/jpg_test.jpg b/examples/images/jpg_test.jpg new file mode 100644 index 0000000..7479826 Binary files /dev/null and b/examples/images/jpg_test.jpg differ diff --git a/examples/images/jpg_test.jpg.license b/examples/images/jpg_test.jpg.license new file mode 100644 index 0000000..d2f8702 --- /dev/null +++ b/examples/images/jpg_test.jpg.license @@ -0,0 +1,2 @@ +# SPDX-FileCopyrightText: 2024 Channing Ramos +# SPDX-License-Identifier: MIT