6262//| ...
6363STATIC 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 ) {
6464 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 );
6868 uint32_t bits = 1 ;
6969
70- if (value_count == 0 ) {
71- mp_raise_ValueError (translate ("value_count must be > 0" ));
72- }
7370 while ((value_count - 1 ) >> bits ) {
7471 if (bits < 8 ) {
7572 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
154151 uint16_t x = 0 ;
155152 uint16_t y = 0 ;
156153 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 );
158155 uint16_t width = common_hal_displayio_bitmap_get_width (self );
159156 x = i % width ;
160157 y = i / width ;
161158 } else {
162159 mp_obj_t * items ;
163160 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 );
169163 }
170164
171165 if (value_obj == MP_OBJ_SENTINEL ) {
172166 // load
173167 return MP_OBJ_NEW_SMALL_INT (common_hal_displayio_bitmap_get_pixel (self , x , y ));
174168 } 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 );
179172 common_hal_displayio_bitmap_set_pixel (self , x , y , value );
180173 }
181174 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
226219 displayio_bitmap_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
227220 check_for_deinit (self );
228221
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 );
231225
232226 displayio_bitmap_t * source = mp_arg_validate_type (args [ARG_source ].u_obj , & displayio_bitmap_type , MP_QSTR_source_bitmap );
233227
@@ -237,32 +231,21 @@ STATIC mp_obj_t displayio_bitmap_obj_blit(size_t n_args, const mp_obj_t *pos_arg
237231 mp_raise_ValueError (translate ("source palette too large" ));
238232 }
239233
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 );
242237 int16_t x2 , y2 ;
243238 // if x2 or y2 is None, then set as the maximum size of the source bitmap
244239 if (args [ARG_x2 ].u_obj == mp_const_none ) {
245240 x2 = source -> width ;
246241 } 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 );
248243 }
249244 // int16_t y2;
250245 if (args [ARG_y2 ].u_obj == mp_const_none ) {
251246 y2 = source -> height ;
252247 } 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 );
266249 }
267250
268251 // 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)
301284 displayio_bitmap_t * self = MP_OBJ_TO_PTR (self_in );
302285 check_for_deinit (self );
303286
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 );
308288 common_hal_displayio_bitmap_fill (self , value );
309289
310290 return mp_const_none ;
0 commit comments