62
62
//| ...
63
63
STATIC mp_obj_t displayio_bitmap_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * all_args ) {
64
64
mp_arg_check_num (n_args , n_kw , 3 , 3 , false);
65
- uint32_t width = mp_obj_get_int (all_args [0 ]);
66
- uint32_t height = mp_obj_get_int (all_args [1 ]);
67
- uint32_t value_count = mp_obj_get_int (all_args [2 ]);
65
+ uint32_t width = mp_arg_validate_int_range ( mp_obj_get_int (all_args [0 ]), 1 , 32767 , MP_QSTR_width );
66
+ uint32_t height = mp_arg_validate_int_range ( mp_obj_get_int (all_args [1 ]), 1 , 32767 , MP_QSTR_height );
67
+ uint32_t value_count = mp_arg_validate_int_range ( mp_obj_get_int (all_args [2 ]), 1 , 65535 , MP_QSTR_value_count );
68
68
uint32_t bits = 1 ;
69
69
70
- if (value_count == 0 ) {
71
- mp_raise_ValueError (translate ("value_count must be > 0" ));
72
- }
73
70
while ((value_count - 1 ) >> bits ) {
74
71
if (bits < 8 ) {
75
72
bits <<= 1 ;
@@ -154,28 +151,24 @@ STATIC mp_obj_t bitmap_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t val
154
151
uint16_t x = 0 ;
155
152
uint16_t y = 0 ;
156
153
if (mp_obj_is_small_int (index_obj )) {
157
- mp_int_t i = MP_OBJ_SMALL_INT_VALUE (index_obj );
154
+ mp_int_t i = mp_arg_validate_int_min ( MP_OBJ_SMALL_INT_VALUE (index_obj ), 0 , MP_QSTR_index );
158
155
uint16_t width = common_hal_displayio_bitmap_get_width (self );
159
156
x = i % width ;
160
157
y = i / width ;
161
158
} else {
162
159
mp_obj_t * items ;
163
160
mp_obj_get_array_fixed_n (index_obj , 2 , & items );
164
- x = mp_obj_get_int (items [0 ]);
165
- y = mp_obj_get_int (items [1 ]);
166
- if (x >= common_hal_displayio_bitmap_get_width (self ) || y >= common_hal_displayio_bitmap_get_height (self )) {
167
- mp_raise_IndexError (translate ("pixel coordinates out of bounds" ));
168
- }
161
+ x = mp_arg_validate_int_range (mp_obj_get_int (items [0 ]), 0 , self -> width - 1 , MP_QSTR_x );
162
+ y = mp_arg_validate_int_range (mp_obj_get_int (items [1 ]), 0 , self -> height - 1 , MP_QSTR_y );
169
163
}
170
164
171
165
if (value_obj == MP_OBJ_SENTINEL ) {
172
166
// load
173
167
return MP_OBJ_NEW_SMALL_INT (common_hal_displayio_bitmap_get_pixel (self , x , y ));
174
168
} else {
175
- mp_uint_t value = (mp_uint_t )mp_obj_get_int (value_obj );
176
- if ((value >> common_hal_displayio_bitmap_get_bits_per_value (self )) != 0 ) {
177
- mp_raise_ValueError (translate ("pixel value requires too many bits" ));
178
- }
169
+ mp_uint_t value = (mp_uint_t )mp_arg_validate_int_range (
170
+ mp_obj_get_int (value_obj ), 0 ,
171
+ (UINT32_MAX >> (32 - common_hal_displayio_bitmap_get_bits_per_value (self ))), MP_QSTR_value );
179
172
common_hal_displayio_bitmap_set_pixel (self , x , y , value );
180
173
}
181
174
return mp_const_none ;
@@ -226,8 +219,9 @@ STATIC mp_obj_t displayio_bitmap_obj_blit(size_t n_args, const mp_obj_t *pos_arg
226
219
displayio_bitmap_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
227
220
check_for_deinit (self );
228
221
229
- int16_t x = args [ARG_x ].u_int ;
230
- int16_t y = args [ARG_y ].u_int ;
222
+ // Check x,y are within self (target) bitmap boundary
223
+ int16_t x = mp_arg_validate_int_range (args [ARG_x ].u_int , 0 , self -> width - 1 , MP_QSTR_x );
224
+ int16_t y = mp_arg_validate_int_range (args [ARG_y ].u_int , 0 , self -> height - 1 , MP_QSTR_y );
231
225
232
226
displayio_bitmap_t * source = mp_arg_validate_type (args [ARG_source ].u_obj , & displayio_bitmap_type , MP_QSTR_source_bitmap );
233
227
@@ -237,32 +231,21 @@ STATIC mp_obj_t displayio_bitmap_obj_blit(size_t n_args, const mp_obj_t *pos_arg
237
231
mp_raise_ValueError (translate ("source palette too large" ));
238
232
}
239
233
240
- int16_t x1 = args [ARG_x1 ].u_int ;
241
- int16_t y1 = args [ARG_y1 ].u_int ;
234
+ // Check x1,y1,x2,y2 are within source bitmap boundary
235
+ int16_t x1 = mp_arg_validate_int_range (args [ARG_x1 ].u_int , 0 , source -> width - 1 , MP_QSTR_x1 );
236
+ int16_t y1 = mp_arg_validate_int_range (args [ARG_y1 ].u_int , 0 , source -> height - 1 , MP_QSTR_y1 );
242
237
int16_t x2 , y2 ;
243
238
// if x2 or y2 is None, then set as the maximum size of the source bitmap
244
239
if (args [ARG_x2 ].u_obj == mp_const_none ) {
245
240
x2 = source -> width ;
246
241
} else {
247
- x2 = mp_obj_get_int (args [ARG_x2 ].u_obj );
242
+ x2 = mp_arg_validate_int_range ( mp_obj_get_int (args [ARG_x2 ].u_obj ), 0 , source -> width , MP_QSTR_x2 );
248
243
}
249
244
// int16_t y2;
250
245
if (args [ARG_y2 ].u_obj == mp_const_none ) {
251
246
y2 = source -> height ;
252
247
} else {
253
- y2 = mp_obj_get_int (args [ARG_y2 ].u_obj );
254
- }
255
-
256
- // Check x,y are within self (target) bitmap boundary
257
- if ((x < 0 ) || (y < 0 ) || (x > self -> width ) || (y > self -> height )) {
258
- mp_raise_ValueError (translate ("out of range of target" ));
259
- }
260
- // Check x1,y1,x2,y2 are within source bitmap boundary
261
- if ((x1 < 0 ) || (x1 > source -> width ) ||
262
- (y1 < 0 ) || (y1 > source -> height ) ||
263
- (x2 < 0 ) || (x2 > source -> width ) ||
264
- (y2 < 0 ) || (y2 > source -> height )) {
265
- mp_raise_ValueError (translate ("out of range of source" ));
248
+ y2 = mp_arg_validate_int_range (mp_obj_get_int (args [ARG_y2 ].u_obj ), 0 , source -> height , MP_QSTR_y2 );
266
249
}
267
250
268
251
// Ensure x1 < x2 and y1 < y2
@@ -301,10 +284,7 @@ STATIC mp_obj_t displayio_bitmap_obj_fill(mp_obj_t self_in, mp_obj_t value_obj)
301
284
displayio_bitmap_t * self = MP_OBJ_TO_PTR (self_in );
302
285
check_for_deinit (self );
303
286
304
- mp_uint_t value = (mp_uint_t )mp_obj_get_int (value_obj );
305
- if ((value >> common_hal_displayio_bitmap_get_bits_per_value (self )) != 0 ) {
306
- mp_raise_ValueError (translate ("pixel value requires too many bits" ));
307
- }
287
+ mp_uint_t value = (mp_uint_t )mp_arg_validate_int_range (mp_obj_get_int (value_obj ), 0 ,(1u << common_hal_displayio_bitmap_get_bits_per_value (self )) - 1 ,MP_QSTR_value );
308
288
common_hal_displayio_bitmap_fill (self , value );
309
289
310
290
return mp_const_none ;
0 commit comments