summaryrefslogtreecommitdiff
path: root/shared-bindings
diff options
context:
space:
mode:
authorJeff Epler <jeff@adafruit.com>2021-03-18 18:41:35 -0500
committerGitHub <noreply@github.com>2021-03-18 18:41:35 -0500
commitd0125617fd9b2652faa1f3c9a88277304fcc66c4 (patch)
treea777183fc0c1fccd6ded2832389be0772ea03527 /shared-bindings
parent623ece2c73b1a7886695fc09bde82f79ae47b3d7 (diff)
parentc37a1f45f3f03652ec9b9e998e4c1b3a50b2303e (diff)
Merge pull request #4428 from kmatch98/bitmap-read-2
Add `reverse_rows` to speedy bitmaptools.readinto function
Diffstat (limited to 'shared-bindings')
-rw-r--r--shared-bindings/bitmaptools/__init__.c39
-rw-r--r--shared-bindings/bitmaptools/__init__.h2
2 files changed, 23 insertions, 18 deletions
diff --git a/shared-bindings/bitmaptools/__init__.c b/shared-bindings/bitmaptools/__init__.c
index bdd4d3e67..bfe4d346c 100644
--- a/shared-bindings/bitmaptools/__init__.c
+++ b/shared-bindings/bitmaptools/__init__.c
@@ -361,20 +361,20 @@ MP_DEFINE_CONST_FUN_OBJ_KW(bitmaptools_draw_line_obj, 0, bitmaptools_obj_draw_li
//| def arrayblit(bitmap: displayio.Bitmap, data: ReadableBuffer, x1: int=0, y1: int=0, x2: Optional[int]=None, y2: Optional[int]=None, skip_index:Optional[int]=None) -> None:
//| """Inserts pixels from ``data`` into the rectangle of width×height pixels with the upper left corner at ``(x,y)``
//|
-//| The values from ``data`` are taken modulo the number of color values
-//| avalable in the destination bitmap.
+//| The values from ``data`` are taken modulo the number of color values
+//| avalable in the destination bitmap.
//|
-//| If x1 or y1 are not specified, they are taken as 0. If x2 or y2
-//| are not specified, or are given as -1, they are taken as the width
-//| and height of the image.
+//| If x1 or y1 are not specified, they are taken as 0. If x2 or y2
+//| are not specified, or are given as -1, they are taken as the width
+//| and height of the image.
//|
-//| The coordinates affected by the blit are ``x1 <= x < x2`` and ``y1 <= y < y2``.
+//| The coordinates affected by the blit are ``x1 <= x < x2`` and ``y1 <= y < y2``.
//|
-//| ``data`` must contain at least as many elements as required. If it
-//| contains excess elements, they are ignored.
+//| ``data`` must contain at least as many elements as required. If it
+//| contains excess elements, they are ignored.
//|
-//| The blit takes place by rows, so the first elements of ``data`` go
-//| to the first row, the next elements to the next row, and so on.
+//| The blit takes place by rows, so the first elements of ``data`` go
+//| to the first row, the next elements to the next row, and so on.
//|
//| :param displayio.Bitmap bitmap: A writable bitmap
//| :param ReadableBuffer data: Buffer containing the source pixel values
@@ -436,13 +436,15 @@ STATIC mp_obj_t bitmaptools_arrayblit(size_t n_args, const mp_obj_t *pos_args, m
MP_DEFINE_CONST_FUN_OBJ_KW(bitmaptools_arrayblit_obj, 0, bitmaptools_arrayblit);
-//| def readinto(bitmap: displayio.Bitmap, file: typing.BinaryIO, bits_per_pixel: int, element_size: int = 1, reverse_pixels_in_element: bool = False, swap_bytes_in_element: bool = False) -> None:
-//| """Read from a binary file into a bitmap
-//| The file must be positioned so that it consists of ``bitmap.height`` rows of pixel data, where each row is the smallest multiple of ``element_size`` bytes that can hold ``bitmap.width`` pixels.
+//| def readinto(bitmap: displayio.Bitmap, file: typing.BinaryIO, bits_per_pixel: int, element_size: int = 1, reverse_pixels_in_element: bool = False, swap_bytes_in_element: bool = False, reverse_rows: bool = False) -> None:
+//| """Reads from a binary file into a bitmap.
//|
-//| The bytes in an element can be optionally swapped, and the pixels in an element can be reversed.
+//| The file must be positioned so that it consists of ``bitmap.height`` rows of pixel data, where each row is the smallest multiple of ``element_size`` bytes that can hold ``bitmap.width`` pixels.
//|
-//| This function doesn't parse image headers, but is useful to speed up loading of uncompressed image formats such as PCF glyph data.
+//| The bytes in an element can be optionally swapped, and the pixels in an element can be reversed. Also, the
+//| row loading direction can be reversed, which may be requires for loading certain bitmap files.
+//|
+//| This function doesn't parse image headers, but is useful to speed up loading of uncompressed image formats such as PCF glyph data.
//|
//| :param displayio.Bitmap bitmap: A writable bitmap
//| :param typing.BinaryIO file: A file opened in binary mode
@@ -450,12 +452,13 @@ MP_DEFINE_CONST_FUN_OBJ_KW(bitmaptools_arrayblit_obj, 0, bitmaptools_arrayblit);
//| :param int element_size: Number of bytes per element. Values of 1, 2, and 4 are supported, except that 24 ``bits_per_pixel`` requires 1 byte per element.
//| :param bool reverse_pixels_in_element: If set, the first pixel in a word is taken from the Most Signficant Bits; otherwise, it is taken from the Least Significant Bits.
//| :param bool swap_bytes_in_element: If the ``element_size`` is not 1, then reverse the byte order of each element read.
+//| :param bool reverse_rows: Reverse the direction of the row loading (required for some bitmap images).
//| """
//| ...
//|
STATIC mp_obj_t bitmaptools_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
- enum { ARG_bitmap, ARG_file, ARG_bits_per_pixel, ARG_element_size, ARG_reverse_pixels_in_element, ARG_swap_bytes_in_element };
+ enum { ARG_bitmap, ARG_file, ARG_bits_per_pixel, ARG_element_size, ARG_reverse_pixels_in_element, ARG_swap_bytes_in_element, ARG_reverse_rows };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_bitmap, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_file, MP_ARG_REQUIRED | MP_ARG_OBJ },
@@ -463,6 +466,7 @@ STATIC mp_obj_t bitmaptools_readinto(size_t n_args, const mp_obj_t *pos_args, mp
{ MP_QSTR_element_size, MP_ARG_INT, { .u_int = 1 } },
{ MP_QSTR_reverse_pixels_in_element, MP_ARG_BOOL, { .u_bool = false } },
{ MP_QSTR_swap_bytes_in_element, MP_ARG_BOOL, { .u_bool = false } },
+ { MP_QSTR_reverse_rows, MP_ARG_BOOL, { .u_bool = false } },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
@@ -503,8 +507,9 @@ STATIC mp_obj_t bitmaptools_readinto(size_t n_args, const mp_obj_t *pos_args, mp
bool reverse_pixels_in_element = args[ARG_reverse_pixels_in_element].u_bool;
bool swap_bytes_in_element = args[ARG_swap_bytes_in_element].u_bool;
+ bool reverse_rows = args[ARG_reverse_rows].u_bool;
- common_hal_bitmaptools_readinto(bitmap, file, element_size, bits_per_pixel, reverse_pixels_in_element, swap_bytes_in_element);
+ common_hal_bitmaptools_readinto(bitmap, file, element_size, bits_per_pixel, reverse_pixels_in_element, swap_bytes_in_element, reverse_rows);
return mp_const_none;
}
diff --git a/shared-bindings/bitmaptools/__init__.h b/shared-bindings/bitmaptools/__init__.h
index 6b80f98f5..fc1eb5906 100644
--- a/shared-bindings/bitmaptools/__init__.h
+++ b/shared-bindings/bitmaptools/__init__.h
@@ -51,7 +51,7 @@ void common_hal_bitmaptools_draw_line(displayio_bitmap_t *destination,
int16_t x1, int16_t y1,
uint32_t value);
-void common_hal_bitmaptools_readinto(displayio_bitmap_t *self, pyb_file_obj_t *file, int element_size, int bits_per_pixel, bool reverse_pixels_in_word, bool swap_bytes);
+void common_hal_bitmaptools_readinto(displayio_bitmap_t *self, pyb_file_obj_t *file, int element_size, int bits_per_pixel, bool reverse_pixels_in_word, bool swap_bytes, bool reverse_rows);
void common_hal_bitmaptools_arrayblit(displayio_bitmap_t *self, void *data, int element_size, int x1, int y1, int x2, int y2, bool skip_specified, uint32_t skip_index);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BITMAPTOOLS__INIT__H