Skip to content

Commit 2e17368

Browse files
committed
bitmaptools: Introduce new validators for coordinate pairs
Several of these routines use coordinate pairs, and jpeg decoding will soon. Introduce a validator just for this use.
1 parent d0c51a4 commit 2e17368

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

shared-bindings/bitmaptools/__init__.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,27 @@
4343
#include "extmod/vfs_posix.h"
4444
#endif
4545

46+
// This assigns to [0] and [2] because the order is x1, y1, x2, y2
47+
STATIC void bitmaptools_validate_coord_range(int16_t out[3], const mp_arg_val_t in[3], int lim, const qstr what[2]) {
48+
out[0] = mp_arg_validate_int_range(mp_arg_validate_type_int(in[0].u_obj, what[0]), 0, lim - 1, what[0]);
49+
if (in[2].u_obj == mp_const_none) {
50+
out[2] = lim;
51+
} else {
52+
out[2] = mp_arg_validate_int_range(mp_arg_validate_type_int(in[2].u_obj, what[1]), out[0] + 1, lim, what[1]);
53+
}
54+
}
55+
56+
bitmaptools_rect_t bitmaptools_validate_coord_range_pair(const mp_arg_val_t in[4], int width, int height) {
57+
static const qstr x_names[] = {MP_QSTR_x1, MP_QSTR_x2};
58+
static const qstr y_names[] = {MP_QSTR_y1, MP_QSTR_y2};
59+
60+
bitmaptools_rect_t rect;
61+
bitmaptools_validate_coord_range(&rect.arr[0], in, width, x_names);
62+
bitmaptools_validate_coord_range(&rect.arr[1], in + 1, height, y_names);
63+
return rect;
64+
}
65+
66+
4667
//| """Collection of bitmap manipulation tools
4768
//|
4869
//| .. note:: If you're looking for information about displaying bitmaps on

shared-bindings/bitmaptools/__init__.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "shared-bindings/displayio/__init__.h"
3333
#include "shared-module/displayio/Palette.h"
3434
#include "py/obj.h"
35+
#include "py/runtime.h"
3536
#include "extmod/vfs_fat.h"
3637

3738
typedef enum {
@@ -88,4 +89,21 @@ void common_hal_bitmaptools_dither(displayio_bitmap_t *dest_bitmap, displayio_bi
8889
void common_hal_bitmaptools_alphablend(displayio_bitmap_t *destination, displayio_bitmap_t *source1, displayio_bitmap_t *source2, displayio_colorspace_t colorspace, mp_float_t factor1, mp_float_t factor2,
8990
bitmaptools_blendmode_t blendmode, uint32_t skip_source1_index, bool skip_source1_index_none, uint32_t skip_source2_index, bool skip_source2_index_none);
9091

92+
typedef struct {
93+
union {
94+
struct {
95+
int16_t x1, y1, x2, y2;
96+
};
97+
int16_t arr[4];
98+
};
99+
} bitmaptools_rect_t;
100+
101+
#define ARGS_X1_Y1_X2_Y2 ARG_x1, ARG_y1, ARG_x2, ARG_y2
102+
#define ALLOWED_ARGS_X1_Y1_X2_Y2(if_required1, if_required2) \
103+
{MP_QSTR_x1, if_required1 | MP_ARG_OBJ, {.u_obj = MP_ROM_INT(0)}}, \
104+
{MP_QSTR_y1, if_required2 | MP_ARG_OBJ, {.u_obj = MP_ROM_INT(0)}}, \
105+
{MP_QSTR_x2, if_required1 | MP_ARG_OBJ, {.u_obj = MP_ROM_NONE}}, \
106+
{MP_QSTR_y2, if_required2 | MP_ARG_OBJ, {.u_obj = MP_ROM_NONE}}
107+
108+
bitmaptools_rect_t bitmaptools_validate_coord_range_pair(const mp_arg_val_t in[4], int width, int height);
91109
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BITMAPTOOLS__INIT__H

0 commit comments

Comments
 (0)