Skip to content

Commit ee5e716

Browse files
committed
bitmaptools: use stream API
this allows `readinto` to succeed in the unix port, where the VFS is not FAT
1 parent dca696d commit ee5e716

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

shared-bindings/bitmaptools/__init__.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ STATIC mp_obj_t bitmaptools_readinto(size_t n_args, const mp_obj_t *pos_args, mp
619619

620620
displayio_bitmap_t *bitmap = mp_arg_validate_type(args[ARG_bitmap].u_obj, &displayio_bitmap_type, MP_QSTR_bitmap);
621621

622-
pyb_file_obj_t *file = mp_arg_validate_type(args[ARG_file].u_obj, &mp_type_fileio, MP_QSTR_file);
622+
mp_obj_t *file = args[ARG_file].u_obj;
623623

624624
int element_size = args[ARG_element_size].u_int;
625625
if (element_size != 1 && element_size != 2 && element_size != 4) {

shared-bindings/bitmaptools/__init__.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void common_hal_bitmaptools_draw_line(displayio_bitmap_t *destination,
6464
int16_t x1, int16_t y1,
6565
uint32_t value);
6666

67-
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);
67+
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);
6868
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);
6969
void common_hal_bitmaptools_dither(displayio_bitmap_t *dest_bitmap, displayio_bitmap_t *source_bitmap, displayio_colorspace_t colorspace, bitmaptools_dither_algorithm_t algorithm);
7070

shared-module/bitmaptools/__init__.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
#include "shared-bindings/displayio/ColorConverter.h"
3131
#include "shared-module/displayio/Bitmap.h"
3232

33-
#include "py/runtime.h"
3433
#include "py/mperrno.h"
34+
#include "py/runtime.h"
35+
#include "py/stream.h"
3536

3637
#include <math.h>
3738
#include <stdlib.h>
@@ -513,9 +514,11 @@ void common_hal_bitmaptools_arrayblit(displayio_bitmap_t *self, void *data, int
513514
displayio_bitmap_set_dirty_area(self, &area);
514515
}
515516

516-
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) {
517+
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) {
517518
uint32_t mask = (1 << common_hal_displayio_bitmap_get_bits_per_value(self)) - 1;
518519

520+
const mp_stream_p_t *file_proto = mp_get_stream_raise(file, MP_STREAM_OP_READ);
521+
519522
displayio_area_t a = {0, 0, self->width, self->height, NULL};
520523
displayio_bitmap_set_dirty_area(self, &a);
521524

@@ -530,9 +533,14 @@ void common_hal_bitmaptools_readinto(displayio_bitmap_t *self, pyb_file_obj_t *f
530533
uint8_t *rowdata8 = (uint8_t *)rowdata32;
531534
const int y_draw = reverse_rows ? (self->height) - 1 - y : y;
532535

533-
UINT bytes_read = 0;
534-
if (f_read(&file->fp, rowdata32, rowsize, &bytes_read) != FR_OK || bytes_read != rowsize) {
535-
mp_raise_OSError(MP_EIO);
536+
537+
int error = 0;
538+
mp_uint_t bytes_read = file_proto->read(file, rowdata32, rowsize, &error);
539+
if (error) {
540+
mp_raise_OSError(error);
541+
}
542+
if (bytes_read != rowsize) {
543+
mp_raise_msg(&mp_type_EOFError, NULL);
536544
}
537545

538546
if (swap_bytes) {

0 commit comments

Comments
 (0)