-
Notifications
You must be signed in to change notification settings - Fork 21
Add 16, 24, and 32-bit bmp support #71
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
3 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
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,93 @@ | ||
# SPDX-FileCopyrightText: 2018 Scott Shawcroft for Adafruit Industries | ||
# SPDX-FileCopyrightText: 2022-2023 Melissa LeBlanc-Williams | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
""" | ||
`adafruit_imageload.bmp.truecolor` | ||
==================================================== | ||
|
||
Load pixel colors into a bitmap from an truecolor BMP and return the correct colorconverter. | ||
|
||
* Author(s): Melissa LeBlanc-Williams | ||
|
||
""" | ||
|
||
import sys | ||
|
||
try: | ||
from typing import Tuple, Optional | ||
from io import BufferedReader | ||
from displayio import Bitmap | ||
from ..displayio_types import BitmapConstructor | ||
except ImportError: | ||
pass | ||
|
||
from displayio import ColorConverter, Colorspace | ||
|
||
__version__ = "0.0.0+auto.0" | ||
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git" | ||
|
||
|
||
def load( | ||
file: BufferedReader, | ||
width: int, | ||
height: int, | ||
data_start: int, | ||
color_depth: int, | ||
*, | ||
bitmap: Optional[BitmapConstructor] = None, | ||
) -> Tuple[Optional[Bitmap], Optional[ColorConverter]]: | ||
"""Loads truecolor bitmap data into bitmap and palette objects. Due to the 16-bit limit | ||
that the bitmap object can hold, colors will be converted to 16-bit RGB565 values. | ||
|
||
:param file file: The open bmp file | ||
:param int width: Image width in pixels | ||
:param int height: Image height in pixels | ||
:param int data_start: Byte location where the data starts (after headers) | ||
:param int color_depth: Number of bits used to store a value | ||
:param BitmapConstructor bitmap: a function that returns a displayio.Bitmap | ||
""" | ||
# pylint: disable=too-many-arguments,too-many-locals,too-many-branches | ||
converter_obj = None | ||
bitmap_obj = None | ||
if bitmap: | ||
# Set up a ColorConverter object and set appropriate colorspace | ||
# to convert from based on the color depth | ||
input_colorspace = Colorspace.RGB888 | ||
if color_depth == 16: | ||
input_colorspace = Colorspace.RGB555 | ||
converter_obj = ColorConverter(input_colorspace=input_colorspace) | ||
if sys.maxsize > 1073741823: | ||
makermelissa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# pylint: disable=import-outside-toplevel, relative-beyond-top-level | ||
from .negative_height_check import negative_height_check | ||
|
||
# convert unsigned int to signed int when height is negative | ||
height = negative_height_check(height) | ||
bitmap_obj = bitmap(width, abs(height), 65535) | ||
file.seek(data_start) | ||
line_size = width * (color_depth // 8) | ||
# Set the seek direction based on whether the height value is negative or positive | ||
if height > 0: | ||
makermelissa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
range1 = height - 1 | ||
range2 = -1 | ||
range3 = -1 | ||
else: | ||
range1 = 0 | ||
range2 = abs(height) | ||
range3 = 1 | ||
chunk = bytearray(line_size) | ||
for y in range(range1, range2, range3): | ||
file.readinto(chunk) | ||
bytes_per_pixel = color_depth // 8 | ||
offset = y * width | ||
|
||
for x in range(width): | ||
i = x * bytes_per_pixel | ||
if color_depth == 16: | ||
pixel = chunk[i] | chunk[i + 1] << 8 | ||
else: | ||
pixel = chunk[i + 2] << 16 | chunk[i + 1] << 8 | chunk[i] | ||
bitmap_obj[offset + x] = converter_obj.convert(pixel) | ||
|
||
return bitmap_obj, ColorConverter(input_colorspace=Colorspace.RGB565) |
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.