Merge pull request #5605 from jepler/bitmaptools-bugfixes
Bitmaptools bugfixes
This commit is contained in:
commit
387a8a46b3
|
@ -290,8 +290,8 @@ STATIC mp_obj_t bitmaptools_alphablend(size_t n_args, const mp_obj_t *pos_args,
|
|||
displayio_bitmap_t *source1 = MP_OBJ_TO_PTR(mp_arg_validate_type(args[ARG_source_bitmap_1].u_obj, &displayio_bitmap_type, MP_QSTR_source_bitmap_1)); // the first source bitmap
|
||||
displayio_bitmap_t *source2 = MP_OBJ_TO_PTR(mp_arg_validate_type(args[ARG_source_bitmap_2].u_obj, &displayio_bitmap_type, MP_QSTR_source_bitmap_2)); // the second source bitmap
|
||||
|
||||
mp_float_t factor1 = (args[ARG_factor_1].u_obj == mp_const_none) ? MICROPY_FLOAT_CONST(.5) : mp_obj_float_get(args[ARG_factor_1].u_obj);
|
||||
mp_float_t factor2 = (args[ARG_factor_2].u_obj == mp_const_none) ? 1 - factor1 : mp_obj_float_get(args[ARG_factor_2].u_obj);
|
||||
mp_float_t factor1 = (args[ARG_factor_1].u_obj == mp_const_none) ? MICROPY_FLOAT_CONST(.5) : mp_obj_get_float(args[ARG_factor_1].u_obj);
|
||||
mp_float_t factor2 = (args[ARG_factor_2].u_obj == mp_const_none) ? 1 - factor1 : mp_obj_get_float(args[ARG_factor_2].u_obj);
|
||||
|
||||
displayio_colorspace_t colorspace = (displayio_colorspace_t)cp_enum_value(&displayio_colorspace_type, args[ARG_colorspace].u_obj);
|
||||
|
||||
|
@ -619,7 +619,7 @@ STATIC mp_obj_t bitmaptools_readinto(size_t n_args, const mp_obj_t *pos_args, mp
|
|||
|
||||
displayio_bitmap_t *bitmap = mp_arg_validate_type(args[ARG_bitmap].u_obj, &displayio_bitmap_type, MP_QSTR_bitmap);
|
||||
|
||||
pyb_file_obj_t *file = mp_arg_validate_type(args[ARG_file].u_obj, &mp_type_fileio, MP_QSTR_file);
|
||||
mp_obj_t *file = args[ARG_file].u_obj;
|
||||
|
||||
int element_size = args[ARG_element_size].u_int;
|
||||
if (element_size != 1 && element_size != 2 && element_size != 4) {
|
||||
|
|
|
@ -64,7 +64,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, bool reverse_rows);
|
||||
void common_hal_bitmaptools_readinto(displayio_bitmap_t *self, mp_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);
|
||||
void common_hal_bitmaptools_dither(displayio_bitmap_t *dest_bitmap, displayio_bitmap_t *source_bitmap, displayio_colorspace_t colorspace, bitmaptools_dither_algorithm_t algorithm);
|
||||
|
||||
|
|
|
@ -30,8 +30,9 @@
|
|||
#include "shared-bindings/displayio/ColorConverter.h"
|
||||
#include "shared-module/displayio/Bitmap.h"
|
||||
|
||||
#include "py/runtime.h"
|
||||
#include "py/mperrno.h"
|
||||
#include "py/runtime.h"
|
||||
#include "py/stream.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -513,9 +514,11 @@ void common_hal_bitmaptools_arrayblit(displayio_bitmap_t *self, void *data, int
|
|||
displayio_bitmap_set_dirty_area(self, &area);
|
||||
}
|
||||
|
||||
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_element, bool swap_bytes, bool reverse_rows) {
|
||||
void common_hal_bitmaptools_readinto(displayio_bitmap_t *self, mp_obj_t *file, int element_size, int bits_per_pixel, bool reverse_pixels_in_element, bool swap_bytes, bool reverse_rows) {
|
||||
uint32_t mask = (1 << common_hal_displayio_bitmap_get_bits_per_value(self)) - 1;
|
||||
|
||||
const mp_stream_p_t *file_proto = mp_get_stream_raise(file, MP_STREAM_OP_READ);
|
||||
|
||||
displayio_area_t a = {0, 0, self->width, self->height, NULL};
|
||||
displayio_bitmap_set_dirty_area(self, &a);
|
||||
|
||||
|
@ -530,9 +533,14 @@ void common_hal_bitmaptools_readinto(displayio_bitmap_t *self, pyb_file_obj_t *f
|
|||
uint8_t *rowdata8 = (uint8_t *)rowdata32;
|
||||
const int y_draw = reverse_rows ? (self->height) - 1 - y : y;
|
||||
|
||||
UINT bytes_read = 0;
|
||||
if (f_read(&file->fp, rowdata32, rowsize, &bytes_read) != FR_OK || bytes_read != rowsize) {
|
||||
mp_raise_OSError(MP_EIO);
|
||||
|
||||
int error = 0;
|
||||
mp_uint_t bytes_read = file_proto->read(file, rowdata32, rowsize, &error);
|
||||
if (error) {
|
||||
mp_raise_OSError(error);
|
||||
}
|
||||
if (bytes_read != rowsize) {
|
||||
mp_raise_msg(&mp_type_EOFError, NULL);
|
||||
}
|
||||
|
||||
if (swap_bytes) {
|
||||
|
|
|
@ -78,7 +78,7 @@ static void write_word(gifio_gifwriter_t *self, uint16_t value) {
|
|||
|
||||
void shared_module_gifio_gifwriter_construct(gifio_gifwriter_t *self, mp_obj_t *file, int width, int height, displayio_colorspace_t colorspace, bool loop, bool dither, bool own_file) {
|
||||
self->file = file;
|
||||
self->file_proto = mp_proto_get_or_throw(MP_QSTR_protocol_stream, file);
|
||||
self->file_proto = mp_get_stream_raise(file, MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL);
|
||||
if (self->file_proto->is_text) {
|
||||
mp_raise_TypeError(translate("file must be a file opened in byte mode"));
|
||||
}
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
import ulab.numpy as np
|
||||
import displayio
|
||||
import bitmaptools
|
||||
|
||||
try:
|
||||
import struct
|
||||
except:
|
||||
import ustruct as struct
|
||||
|
||||
base_header = b"BMFX\x02\x00\x00\x00\x00\x00F\x00\x00\x008\x00\x00\x00@\x01\x00\x00\xf0\x00\x00\x00\x01\x00\x10\x00\x03\x00\x00\x00\x00X\x02\x00\xd7\r\x00\x00\xd7\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf8\x00\x00\xe0\x07\x00\x00\x1f\x00\x00\x00\x00\x00\x00\x00"
|
||||
|
||||
|
||||
def writebmp16(filename, bitmap):
|
||||
header = bytearray(base_header)
|
||||
header[18:26] = struct.pack("<II", bitmap.width, bitmap.height)
|
||||
with open(filename, "wb") as f:
|
||||
f.write(header)
|
||||
b = np.frombuffer(bitmap, dtype=np.uint16)
|
||||
for i in range(bitmap.height):
|
||||
j = (bitmap.height - i - 1) * bitmap.width
|
||||
f.write(b[j : j + bitmap.width])
|
||||
|
||||
|
||||
def loadbmp16(filename, width=320, height=240):
|
||||
"""This specialized routine loads 16bpp uncompressed bmp files with a
|
||||
70-byte header. It is not appropriate for generic bmp files."""
|
||||
|
||||
bitmap = displayio.Bitmap(width, height, 65536)
|
||||
with open(filename, "rb") as f:
|
||||
f.seek(70)
|
||||
bitmaptools.readinto(
|
||||
bitmap,
|
||||
f,
|
||||
bits_per_pixel=16,
|
||||
element_size=2,
|
||||
reverse_rows=True,
|
||||
)
|
||||
|
||||
return bitmap
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if "/" in __file__:
|
||||
here = __file__.rsplit("/", 1)[0]
|
||||
else:
|
||||
here = "."
|
||||
b = loadbmp16(here + "/minerva16.bmp")
|
||||
print(b[0, 0])
|
||||
print(b[160, 160])
|
||||
for i, p in enumerate(sorted(set(memoryview(b)))):
|
||||
print("%04x" % p, end="\n" if (i % 8) == 7 else " ")
|
||||
if i % 8 != 7:
|
||||
print()
|
|
@ -0,0 +1,32 @@
|
|||
import bitmaptools
|
||||
import displayio
|
||||
import _bmp16
|
||||
|
||||
if "/" in __file__:
|
||||
here = __file__.rsplit("/", 1)[0]
|
||||
else:
|
||||
here = "."
|
||||
|
||||
c = displayio.Colorspace.BGR565
|
||||
|
||||
b1 = _bmp16.loadbmp16(here + "/minerva16.bmp")
|
||||
b2 = _bmp16.loadbmp16(here + "/blinka16.bmp")
|
||||
b3 = displayio.Bitmap(320, 240, 65536)
|
||||
|
||||
for i in (
|
||||
0,
|
||||
1 / 64,
|
||||
3 / 64,
|
||||
3 / 32,
|
||||
3 / 16,
|
||||
0.5,
|
||||
1 - 3 / 16,
|
||||
1 - 3 / 32,
|
||||
1 - 3 / 64,
|
||||
1 - 1 / 64,
|
||||
1,
|
||||
):
|
||||
bitmaptools.alphablend(b3, b1, b2, c, i)
|
||||
_bmp16.writebmp16(f"blend-{i:.2f}.bmp", b3)
|
||||
bitmaptools.alphablend(b3, b1, b2, c, i, 0)
|
||||
_bmp16.writebmp16(f"fade-{i:.2f}.bmp", b3)
|
Binary file not shown.
After Width: | Height: | Size: 150 KiB |
|
@ -0,0 +1,31 @@
|
|||
import bitmaptools
|
||||
import displayio
|
||||
import _bmp16
|
||||
|
||||
if "/" in __file__:
|
||||
here = __file__.rsplit("/", 1)[0]
|
||||
else:
|
||||
here = "."
|
||||
|
||||
c = displayio.Colorspace.BGR565
|
||||
|
||||
b1 = _bmp16.loadbmp16(here + "/minerva16.bmp")
|
||||
b3 = displayio.Bitmap(320, 240, 65536)
|
||||
|
||||
for i in (
|
||||
0,
|
||||
1 / 64,
|
||||
3 / 64,
|
||||
3 / 32,
|
||||
3 / 16,
|
||||
0.5,
|
||||
1 - 3 / 16,
|
||||
1 - 3 / 32,
|
||||
1 - 3 / 64,
|
||||
1 - 1 / 64,
|
||||
1,
|
||||
):
|
||||
bitmaptools.dither(b3, b1, c)
|
||||
_bmp16.writebmp16(f"dither-atkinson.bmp", b3)
|
||||
bitmaptools.dither(b3, b1, c, bitmaptools.DitherAlgorithm.FloydStenberg)
|
||||
_bmp16.writebmp16(f"dither-floydstenberg.bmp", b3)
|
Binary file not shown.
After Width: | Height: | Size: 150 KiB |
Loading…
Reference in New Issue