|
| 1 | +# SPDX-FileCopyrightText: 2024 Channing Ramos |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +""" |
| 6 | +`adafruit_imageload.jpg` |
| 7 | +==================================================== |
| 8 | +
|
| 9 | +Load a JPG into a bitmap by calling the jpegio class. |
| 10 | +
|
| 11 | +* Author(s): Channing Ramos |
| 12 | +
|
| 13 | +""" |
| 14 | + |
| 15 | +# A separate try for jpegio. Not every board supports it and this import may fail. |
| 16 | +# If that happens an ImportError with a proper message needs to be raised |
| 17 | +try: |
| 18 | + from jpegio import JpegDecoder |
| 19 | +except ImportError: |
| 20 | + print("jpegio not supported on this board.") |
| 21 | + |
| 22 | +try: |
| 23 | + from io import BufferedReader |
| 24 | + from typing import Tuple, Optional |
| 25 | + from .displayio_types import BitmapConstructor |
| 26 | +except ImportError: |
| 27 | + pass |
| 28 | + |
| 29 | +from displayio import Bitmap, ColorConverter, Colorspace |
| 30 | + |
| 31 | +__version__ = "0.0.0+auto.0" |
| 32 | +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git" |
| 33 | + |
| 34 | + |
| 35 | +def load( |
| 36 | + file: BufferedReader, |
| 37 | + *, |
| 38 | + bitmap: BitmapConstructor, |
| 39 | +) -> Tuple[Bitmap, Optional[ColorConverter]]: |
| 40 | + """ |
| 41 | + Loads a JPG image from the open ''file''. |
| 42 | + The JPG must be a Baseline JPG, Progressive and Lossless JPG formats are not supported. |
| 43 | +
|
| 44 | + Returns tuple of bitmap object and ColorConverter object. |
| 45 | +
|
| 46 | + :param io.BufferedReader file: Open file handle or compatible (like 'io.BytesIO') |
| 47 | + :param object bitmap: Type to store bitmap data. |
| 48 | + Must have API similar to 'displayio.Bitmap'. Will be skipped if None. |
| 49 | + Will be skipped if None. |
| 50 | + """ |
| 51 | + decoder = JpegDecoder() |
| 52 | + width, height = decoder.open(file) |
| 53 | + bitmap_obj = bitmap(width, height, 65535) |
| 54 | + decoder.decode(bitmap_obj) |
| 55 | + |
| 56 | + return bitmap_obj, ColorConverter(input_colorspace=Colorspace.RGB565_SWAPPED) |
0 commit comments