Skip to content

Commit a9afa0d

Browse files
committed
Move input checks to shared-module, update docstrings
1 parent 85f0f07 commit a9afa0d

File tree

2 files changed

+31
-47
lines changed

2 files changed

+31
-47
lines changed

shared-bindings/bitmaptools/__init__.c

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ MP_DEFINE_CONST_FUN_OBJ_KW(bitmaptools_rotozoom_obj, 0, bitmaptools_obj_rotozoom
255255
//| :param bitmap dest_bitmap: Destination bitmap that will be written into
256256
//| :param int x1: x-pixel position of the first corner of the rectangular fill region
257257
//| :param int y1: y-pixel position of the first corner of the rectangular fill region
258-
//| :param int x2: x-pixel position of the second corner of the rectangular fill region
259-
//| :param int y2: y-pixel position of the second corner of the rectangular fill region
258+
//| :param int x2: x-pixel position of the second corner of the rectangular fill region (exclusive)
259+
//| :param int y2: y-pixel position of the second corner of the rectangular fill region (exclusive)
260260
//| :param int value: Bitmap palette index that will be written into the rectangular
261261
//| fill region in the destination bitmap"""
262262
//| ...
@@ -289,40 +289,6 @@ STATIC mp_obj_t bitmaptools_obj_fill_region(size_t n_args, const mp_obj_t *pos_a
289289
int16_t x2 = args[ARG_x2].u_int;
290290
int16_t y2 = args[ARG_y2].u_int;
291291

292-
// Ensure x1 < x2 and y1 < y2
293-
if (x1 > x2) {
294-
int16_t temp=x2;
295-
x2=x1;
296-
x1=temp;
297-
}
298-
if (y1 > y2) {
299-
int16_t temp=y2;
300-
y2=y1;
301-
y1=temp;
302-
}
303-
304-
// constrain to bitmap dimensions
305-
if (x1 < 0) {
306-
x1 = 0;
307-
} else if (x1 > destination->width) {
308-
x1 = destination->width;
309-
}
310-
if (x2 < 0) {
311-
x2 = 0;
312-
} else if (x2 > destination->width) {
313-
x2 = destination->width;
314-
}
315-
if (y1 < 0) {
316-
y1 = 0;
317-
} else if (y1 > destination->height) {
318-
y1 = destination->height;
319-
}
320-
if (y2 < 0) {
321-
y2 = 0;
322-
} else if (y2 > destination->height) {
323-
y2 = destination->height;
324-
}
325-
326292
common_hal_bitmaptools_fill_region(destination, x1, y1, x2, y2, value);
327293

328294
return mp_const_none;

shared-module/bitmaptools/__init__.c

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@ void common_hal_bitmaptools_rotozoom(displayio_bitmap_t *self, int16_t ox, int16
175175
}
176176
}
177177

178+
int16_t constrain(int16_t input, int16_t min, int16_t max) {
179+
// constrain the input between the min and max values
180+
if (input < min) {
181+
return min;
182+
}
183+
if (input > max) {
184+
return max;
185+
}
186+
return input;
187+
}
188+
178189
void common_hal_bitmaptools_fill_region(displayio_bitmap_t *destination,
179190
int16_t x1, int16_t y1,
180191
int16_t x2, int16_t y2,
@@ -187,6 +198,24 @@ void common_hal_bitmaptools_fill_region(displayio_bitmap_t *destination,
187198
mp_raise_RuntimeError(translate("Read-only object"));
188199
}
189200

201+
// Ensure x1 < x2 and y1 < y2
202+
if (x1 > x2) {
203+
int16_t temp=x2;
204+
x2=x1;
205+
x1=temp;
206+
}
207+
if (y1 > y2) {
208+
int16_t temp=y2;
209+
y2=y1;
210+
y1=temp;
211+
}
212+
213+
// constrain to bitmap dimensions
214+
x1 = constrain(x1, 0, destination->width);
215+
x2 = constrain(x2, 0, destination->width);
216+
y1 = constrain(y1, 0, destination->height);
217+
y2 = constrain(y2, 0, destination->height);
218+
190219
// update the dirty rectangle
191220
displayio_bitmap_set_dirty_area(destination, x1, y1, x2, y2);
192221

@@ -198,17 +227,6 @@ void common_hal_bitmaptools_fill_region(displayio_bitmap_t *destination,
198227
}
199228
}
200229

201-
int16_t constrain(int16_t input, int16_t min, int16_t max) {
202-
// constrain the input between the min and max values
203-
if (input < min) {
204-
return min;
205-
}
206-
if (input > max) {
207-
return max;
208-
}
209-
return input;
210-
}
211-
212230
void common_hal_bitmaptools_draw_line(displayio_bitmap_t *destination,
213231
int16_t x0, int16_t y0,
214232
int16_t x1, int16_t y1,

0 commit comments

Comments
 (0)