8000 displayio_bitmap_set_dirty_area: rewrite in terms of displayio_area · kmatch98/circuitpython@36d608a · GitHub
[go: up one dir, main page]

Skip to content

Commit 36d608a

Browse files
committed
displayio_bitmap_set_dirty_area: rewrite in terms of displayio_area
.. simplifying code in the process. For instance, now fill_region uses area routines to order and constrain its coordinates. Happily, this change also frees a modest amount of code space.
1 parent 3b506f0 commit 36d608a

File tree

3 files changed

+26
-65
lines changed

3 files changed

+26
-65
lines changed

shared-module/bitmaptools/__init__.c

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -240,30 +240,18 @@ void common_hal_bitmaptools_fill_region(displayio_bitmap_t *destination,
240240
mp_raise_RuntimeError(translate("Read-only object"));
241241
}
242242

243-
// Ensure x1 < x2 and y1 < y2
244-
if (x1 > x2) {
245-
int16_t temp = x2;
246-
x2 = x1;
247-
x1 = temp;
248-
}
249-
if (y1 > y2) {
250-
int16_t temp = y2;
251-
y2 = y1;
252-
y1 = temp;
253-
}
243+
displayio_area_t area = { x1, y1, x2, y2 };
244+
displayio_area_canon(&area);
254245

255-
// constrain to bitmap dimensions
256-
x1 = constrain(x1, 0, destination->width);
257-
x2 = constrain(x2, 0, destination->width);
258-
y1 = constrain(y1, 0, destination->height);
259-
y2 = constrain(y2, 0, destination->height);
246+
displayio_area_t bitmap_area = { 0, 0, destination->width, destination->height };
247+
displayio_area_compute_overlap(&area, &bitmap_area, &area);
260248

261249
// update the dirty rectangle
262-
displayio_bitmap_set_dirty_area(destination, x1, y1, x2, y2);
250+
displayio_bitmap_set_dirty_area(destination, &area);
263251

264252
int16_t x, y;
265-
for (x = x1; x < x2; x++) {
266-
for (y = y1; y < y2; y++) {
253+
for (x = area.x1; x < area.x2; x++) {
254+
for (y = area.y1; y < area.y2; y++) {
267255
displayio_bitmap_write_pixel(destination, x, y, value);
268256
}
269257
}
@@ -298,13 +286,11 @@ void common_hal_bitmaptools_draw_line(displayio_bitmap_t *destination,
298286
ybb0 = y1;
299287
ybb1 = y0 + 1;
300288
}
289+
displayio_area_t area = { xbb0, ybb0, xbb1, ybb1 };
290+
displayio_area_t bitmap_area = { 0, 0, destination->width, destination->height };
291+
displayio_area_compute_overlap(&area, &bitmap_area, &area);
301292

302-
xbb0 = constrain(xbb0, 0, destination->width);
303-
xbb1 = constrain(xbb1, 0, destination->width);
304-
ybb0 = constrain(ybb0, 0, destination->height);
305-
ybb1 = constrain(ybb1, 0, destination->height);
306-
307-
displayio_bitmap_set_dirty_area(destination, xbb0, ybb0, xbb1, ybb1);
293+
displayio_bitmap_set_dirty_area(destination, &area);
308294

309295
int16_t temp, x, y;
310296

@@ -401,7 +387,8 @@ void common_hal_bitmaptools_arrayblit(displayio_bitmap_t *self, void *data, int
401387
}
402388
}
403389
}
404-
displayio_bitmap_set_dirty_area(self, x1, y1, x2, y2);
390+
displayio_area_t area = { x1, y1, x2, y2 };
391+
displayio_bitmap_set_dirty_area(self, &area);
405392
}
406393

407394
void common_hal_bitmaptools_readinto(displayio_bitmap_t *self, pyb_file_obj_t *file, int element_size, int bits_per_pixel, bool reverse_pixels_in_element, bool swap_bytes) {
@@ -486,5 +473,6 @@ void common_hal_bitmaptools_readinto(displayio_bitmap_t *self, pyb_file_obj_t *f
486473
}
487474
}
488475

489-
displayio_bitmap_set_dirty_area(self, 0, 0, self->width, self->height);
476+
displayio_area_t a = {0, 0, self->width, self->height};
477+
displayio_bitmap_set_dirty_area(self, &a);
490478
}

shared-module/displayio/Bitmap.c

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -105,41 +105,12 @@ uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *self, int16_t
105105
return 0;
106106
}
107107

108-
void displayio_bitmap_set_dirty_area(displayio_bitmap_t *self, int16_t x1, int16_t y1, int16_t x2, int16_t y2) {
109-
// Update the bitmap's dirty region with the rectangle bounded by (x1,y1) and (x2, y2)
110-
111-
// Arrange x1 < x2, y1 < y2
112-
if (x1 > x2) {
113-
int16_t temp = x1;
114-
x1 = x2;
115-
x2 = temp;
116-
}
117-
if (y1 > y2) {
118-
int16_t temp = y1;
119-
y1 = y2;
120-
y2 = temp;
121-
}
122-
123-
// Update the dirty area.
124-
if (self->dirty_area.x1 == self->dirty_area.x2) {
125-
self->dirty_area.x1 = x1;
126-
self->dirty_area.x2 = x2;
127-
self->dirty_area.y1 = y1;
128-
self->dirty_area.y2 = y2;
129-
} else {
130-
if (x1 < self->dirty_area.x1) {
131-
self->dirty_area.x1 = x1;
132-
}
133-
if (x2 > self->dirty_area.x2) {
134-
self->dirty_area.x2 = x2;
135-
}
136-
if (y1 < self->dirty_area.y1) {
137-
self->dirty_area.y1 = y1;
138-
}
139-
if (y2 > F438 self->dirty_area.y2) {
140-
self->dirty_area.y2 = y2;
141-
}
142-
}
108+
void displayio_bitmap_set_dirty_area(displayio_bitmap_t *self, const displayio_area_t *dirty_area) {
109+
displayio_area_t area = *dirty_area;
110+
displayio_area_canon(&area);
111+
displayio_area_union(&area, &self->dirty_area, &area);
112+
displayio_area_t bitmap_area = {0, 0, self->width, self->height};
113+
displayio_area_compute_overlap(&area, &bitmap_area, &self->dirty_area);
143114
}
144115

145116
void displayio_bitmap_write_pixel(displayio_bitmap_t *self, int16_t x, int16_t y, uint32_t value) {
@@ -189,7 +160,8 @@ void common_hal_displayio_bitmap_blit(displayio_bitmap_t *self, int16_t x, int16
189160
dirty_y_max = self->height;
190161
}
191162

192-
displayio_bitmap_set_dirty_area(self, x, y, dirty_x_max, dirty_y_max);
163+
displayio_area_t a = { x, y, dirty_x_max, dirty_y_max};
164+
displayio_bitmap_set_dirty_area(self, &a);
193165

194166
bool x_reverse = false;
195167
bool y_reverse = false;
@@ -231,7 +203,8 @@ void common_hal_displayio_bitmap_set_pixel(displayio_bitmap_t *self, int16_t x,
231203
}
232204

233205
// update the dirty region
234-
displayio_bitmap_set_dirty_area(self, x, y, x + 1, y + 1);
206+
displayio_area_t a = {x, y, x + 1, y + 1};
207+
displayio_bitmap_set_dirty_area(self, &a);
235208

236209
// write the pixel
237210
displayio_bitmap_write_pixel(self, x, y, value);

shared-module/displayio/Bitmap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ typedef struct {
4949

5050
void displayio_bitmap_finish_refresh(displayio_bitmap_t *self);
5151
displayio_area_t *displayio_bitmap_get_refresh_areas(displayio_bitmap_t *self, displayio_area_t *tail);
52-
void displayio_bitmap_set_dirty_area(displayio_bitmap_t *self, int16_t x1, int16_t y1, int16_t x2, int16_t y2);
52+
void displayio_bitmap_set_dirty_area(displayio_bitmap_t *self, const displayio_area_t *area);
5353
void displayio_bitmap_write_pixel(displayio_bitmap_t *self, int16_t x, int16_t y, uint32_t value);
5454

5555
#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_BITMAP_H

0 commit comments

Comments
 (0)
0