Skip to content

Commit 8f5d3fd

Browse files
committed
pybricks.parameters.Image: Drop support for Image("_screen_")
There is no need to load image from a string, and there is a more pythonic way to get the screen Image object from the hub object. Refs: pybricks/support#2154
1 parent f4a5229 commit 8f5d3fd

File tree

1 file changed

+26
-38
lines changed

1 file changed

+26
-38
lines changed

pybricks/parameters/pb_type_image.c

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -69,47 +69,35 @@ static mp_obj_t pb_type_Image_make_new(const mp_obj_type_t *type,
6969

7070
pb_type_Image_obj_t *self;
7171

72-
if (mp_obj_is_type(source_in, &pb_type_Image)) {
73-
pb_type_Image_obj_t *source = MP_OBJ_TO_PTR(source_in);
74-
if (!mp_obj_is_true(sub_in)) {
75-
// Copy.
76-
int width = source->image.width;
77-
int height = source->image.height;
78-
79-
void *buf = umm_malloc(width * height * sizeof(uint8_t));
80-
if (!buf) {
81-
mp_raise_type(&mp_type_MemoryError);
82-
}
83-
84-
self = mp_obj_malloc_with_finaliser(pb_type_Image_obj_t, &pb_type_Image);
85-
self->owner = MP_OBJ_NULL;
86-
self->is_display = false;
87-
pbio_image_init(&self->image, buf, width, height, width);
88-
pbio_image_draw_image(&self->image, &source->image, 0, 0);
89-
} else {
90-
// Sub-image.
91-
mp_int_t x1 = pb_obj_get_int(x1_in);
92-
mp_int_t y1 = pb_obj_get_int(y1_in);
93-
mp_int_t x2 = x2_in == mp_const_none ? source->image.width - 1 : pb_obj_get_int(x2_in);
94-
mp_int_t y2 = y2_in == mp_const_none ? source->image.height - 1 : pb_obj_get_int(y2_in);
95-
self = mp_obj_malloc(pb_type_Image_obj_t, &pb_type_Image);
96-
self->owner = source_in;
97-
self->is_display = false;
98-
int width = x2 - x1 + 1;
99-
int height = y2 - y1 + 1;
100-
pbio_image_init_sub(&self->image, &source->image, x1, y1, width, height);
72+
pb_assert_type(source_in, &pb_type_Image);
73+
pb_type_Image_obj_t *source = MP_OBJ_TO_PTR(source_in);
74+
if (!mp_obj_is_true(sub_in)) {
75+
// Copy.
76+
int width = source->image.width;
77+
int height = source->image.height;
78+
79+
void *buf = umm_malloc(width * height * sizeof(uint8_t));
80+
if (!buf) {
81+
mp_raise_type(&mp_type_MemoryError);
10182
}
102-
} else if (mp_obj_equal(source_in, MP_OBJ_NEW_QSTR(MP_QSTR__screen_))) {
103-
// Screen.
104-
self = mp_obj_malloc(pb_type_Image_obj_t, &pb_type_Image);
83+
84+
self = mp_obj_malloc_with_finaliser(pb_type_Image_obj_t, &pb_type_Image);
10585
self->owner = MP_OBJ_NULL;
106-
self->is_display = true;
107-
self->image = *pbdrv_display_get_image();
108-
} else if (mp_obj_is_str(source_in)) {
109-
// TODO: load from image "files".
110-
mp_raise_NotImplementedError(MP_ERROR_TEXT("'_screen_' is the only supported source string"));
86+
self->is_display = false;
87+
pbio_image_init(&self->image, buf, width, height, width);
88+
pbio_image_draw_image(&self->image, &source->image, 0, 0);
11189
} else {
112-
mp_raise_TypeError(MP_ERROR_TEXT("source must be Image or str"));
90+
// Sub-image.
91+
mp_int_t x1 = pb_obj_get_int(x1_in);
92+
mp_int_t y1 = pb_obj_get_int(y1_in);
93+
mp_int_t x2 = x2_in == mp_const_none ? source->image.width - 1 : pb_obj_get_int(x2_in);
94+
mp_int_t y2 = y2_in == mp_const_none ? source->image.height - 1 : pb_obj_get_int(y2_in);
95+
self = mp_obj_malloc(pb_type_Image_obj_t, &pb_type_Image);
96+
self->owner = source_in;
97+
self->is_display = false;
98+
int width = x2 - x1 + 1;
99+
int height = y2 - y1 + 1;
100+
pbio_image_init_sub(&self->image, &source->image, x1, y1, width, height);
113101
}
114102

115103
return MP_OBJ_FROM_PTR(self);

0 commit comments

Comments
 (0)