@@ -270,8 +270,7 @@ static void fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, u
270
270
formats [fb -> format ].fill_rect (fb , x , y , xend - x , yend - y , col );
271
271
}
272
272
273
- static mp_obj_t framebuf_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * args_in ) {
274
- mp_arg_check_num (n_args , n_kw , 4 , 5 , false);
273
+ static mp_obj_t framebuf_make_new_helper (size_t n_args , const mp_obj_t * args_in , unsigned int buf_flags , mp_obj_framebuf_t * o ) {
275
274
276
275
mp_int_t width = mp_obj_get_int (args_in [1 ]);
277
276
mp_int_t height = mp_obj_get_int (args_in [2 ]);
@@ -318,13 +317,15 @@ static mp_obj_t framebuf_make_new(const mp_obj_type_t *type, size_t n_args, size
318
317
}
319
318
320
319
mp_buffer_info_t bufinfo ;
321
- mp_get_buffer_raise (args_in [0 ], & bufinfo , MP_BUFFER_WRITE );
320
+ mp_get_buffer_raise (args_in [0 ], & bufinfo , buf_flags );
322
321
323
322
if ((strides_required * stride + (height_required - strides_required ) * width_required ) * bpp / 8 > bufinfo .len ) {
324
323
mp_raise_ValueError (NULL );
325
324
}
326
325
327
- mp_obj_framebuf_t * o = mp_obj_malloc (mp_obj_framebuf_t , type );
326
+ if (o == NULL ) {
327
+ o = mp_obj_malloc (mp_obj_framebuf_t , (const mp_obj_type_t * )& mp_type_framebuf );
328
+ }
328
329
o -> buf_obj = args_in [0 ];
329
330
o -> buf = bufinfo .buf ;
330
331
o -> width = width ;
@@ -335,6 +336,11 @@ static mp_obj_t framebuf_make_new(const mp_obj_type_t *type, size_t n_args, size
335
336
return MP_OBJ_FROM_PTR (o );
336
337
}
337
338
339
+ static mp_obj_t framebuf_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * args_in ) {
340
+ mp_arg_check_num (n_args , n_kw , 4 , 5 , false);
341
+ return framebuf_make_new_helper (n_args , args_in , MP_BUFFER_WRITE , NULL );
342
+ }
343
+
338
344
static void framebuf_args (const mp_obj_t * args_in , mp_int_t * args_out , int n ) {
339
345
for (int i = 0 ; i < n ; ++ i ) {
340
346
args_out [i ] = mp_obj_get_int (args_in [i + 1 ]);
@@ -707,30 +713,45 @@ static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_poly_obj, 5, 6, framebuf_pol
707
713
708
714
#endif // MICROPY_PY_ARRAY
709
715
716
+ static void get_readonly_framebuffer (mp_obj_t arg , mp_obj_framebuf_t * rofb ) {
717
+ mp_obj_t fb = mp_obj_cast_to_native_base (arg , MP_OBJ_FROM_PTR (& mp_type_framebuf ));
718
+ if (fb != MP_OBJ_NULL ) {
719
+ * rofb = * (mp_obj_framebuf_t * )MP_OBJ_TO_PTR (fb );
720
+ } else {
721
+ // A tuple/list of the form: (buffer, width, height, format[, stride]).
722
+ size_t len ;
723
+ mp_obj_t * items ;
724
+ mp_obj_get_array (arg , & len , & items );
725
+ if (len < 4 || len > 5 ) {
726
+ mp_raise_ValueError (NULL );
727
+ }
728
+ framebuf_make_new_helper (len , items , MP_BUFFER_READ , rofb );
729
+ }
730
+ }
731
+
710
732
static mp_obj_t framebuf_blit (size_t n_args , const mp_obj_t * args_in ) {
711
733
mp_obj_framebuf_t * self = MP_OBJ_TO_PTR (args_in [0 ]);
712
- mp_obj_t source_in = mp_obj_cast_to_native_base (args_in [1 ], MP_OBJ_FROM_PTR (& mp_type_framebuf ));
713
- if (source_in == MP_OBJ_NULL ) {
714
- mp_raise_TypeError (NULL );
715
- }
716
- mp_obj_framebuf_t * source = MP_OBJ_TO_PTR (source_in );
734
+
735
+ mp_obj_framebuf_t source ;
736
+ get_readonly_framebuffer (args_in [1 ], & source );
717
737
718
738
mp_int_t x = mp_obj_get_int (args_in [2 ]);
719
739
mp_int_t y = mp_obj_get_int (args_in [3 ]);
720
740
mp_int_t key = -1 ;
721
741
if (n_args > 4 ) {
722
742
key = mp_obj_get_int (args_in [4 ]);
723
743
}
724
- mp_obj_framebuf_t * palette = NULL ;
744
+ mp_obj_framebuf_t palette ;
745
+ palette .buf = NULL ;
725
746
if (n_args > 5 && args_in [5 ] != mp_const_none ) {
726
- palette = MP_OBJ_TO_PTR ( mp_obj_cast_to_native_base ( args_in [5 ], MP_OBJ_FROM_PTR ( & mp_type_framebuf )) );
747
+ get_readonly_framebuffer ( args_in [5 ], & palette );
727
748
}
728
749
729
750
if (
730
751
(x >= self -> width ) ||
731
752
(y >= self -> height ) ||
732
- (- x >= source -> width ) ||
733
- (- y >= source -> height )
753
+ (- x >= source . width ) ||
754
+ (- y >= source . height )
734
755
) {
735
756
// Out of bounds, no-op.
736
757
return mp_const_none ;
@@ -741,15 +762,15 @@ static mp_obj_t framebuf_blit(size_t n_args, const mp_obj_t *args_in) {
741
762
int y0 = MAX (0 , y );
742
763
int x1 = MAX (0 , - x );
743
764
int y1 = MAX (0 , - y );
744
- int x0end = MIN (self -> width , x + source -> width );
745
- int y0end = MIN (self -> height , y + source -> height );
765
+ int x0end = MIN (self -> width , x + source . width );
766
+ int y0end = MIN (self -> height , y + source . height );
746
767
747
768
for (; y0 < y0end ; ++ y0 ) {
748
769
int cx1 = x1 ;
749
770
for (int cx0 = x0 ; cx0 < x0end ; ++ cx0 ) {
750
- uint32_t col = getpixel (source , cx1 , y1 );
751
- if (palette ) {
752
- col = getpixel (palette , col , 0 );
771
+ uint32_t col = getpixel (& source , cx1 , y1 );
772
+ if (palette . buf ) {
773
+ col = getpixel (& palette , col , 0 );
753
774
}
754
775
if (col != (uint32_t )key ) {
755
776
setpixel (self , cx0 , y0 , col );
0 commit comments