Commit 15ac5a3d authored by Damien George's avatar Damien George

extmod/modframebuf: Change int to unsigned int in format methods args.

These args are already bounds checked and clipped, and using unsigned ints
can be more efficient.  It also eliminates possible issues and compiler
warnings with shifting of signed integers.
Signed-off-by: default avatarDamien George <damien@micropython.org>
parent 33f10381
...@@ -45,9 +45,9 @@ typedef struct _mp_obj_framebuf_t { ...@@ -45,9 +45,9 @@ typedef struct _mp_obj_framebuf_t {
STATIC const mp_obj_type_t mp_type_framebuf; STATIC const mp_obj_type_t mp_type_framebuf;
#endif #endif
typedef void (*setpixel_t)(const mp_obj_framebuf_t *, int, int, uint32_t); typedef void (*setpixel_t)(const mp_obj_framebuf_t *, unsigned int, unsigned int, uint32_t);
typedef uint32_t (*getpixel_t)(const mp_obj_framebuf_t *, int, int); typedef uint32_t (*getpixel_t)(const mp_obj_framebuf_t *, unsigned int, unsigned int);
typedef void (*fill_rect_t)(const mp_obj_framebuf_t *, int, int, int, int, uint32_t); typedef void (*fill_rect_t)(const mp_obj_framebuf_t *, unsigned int, unsigned int, unsigned int, unsigned int, uint32_t);
typedef struct _mp_framebuf_p_t { typedef struct _mp_framebuf_p_t {
setpixel_t setpixel; setpixel_t setpixel;
...@@ -66,25 +66,25 @@ typedef struct _mp_framebuf_p_t { ...@@ -66,25 +66,25 @@ typedef struct _mp_framebuf_p_t {
// Functions for MHLSB and MHMSB // Functions for MHLSB and MHMSB
STATIC void mono_horiz_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) { STATIC void mono_horiz_setpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, uint32_t col) {
size_t index = (x + y * fb->stride) >> 3; size_t index = (x + y * fb->stride) >> 3;
int offset = fb->format == FRAMEBUF_MHMSB ? x & 0x07 : 7 - (x & 0x07); unsigned int offset = fb->format == FRAMEBUF_MHMSB ? x & 0x07 : 7 - (x & 0x07);
((uint8_t *)fb->buf)[index] = (((uint8_t *)fb->buf)[index] & ~(0x01 << offset)) | ((col != 0) << offset); ((uint8_t *)fb->buf)[index] = (((uint8_t *)fb->buf)[index] & ~(0x01 << offset)) | ((col != 0) << offset);
} }
STATIC uint32_t mono_horiz_getpixel(const mp_obj_framebuf_t *fb, int x, int y) { STATIC uint32_t mono_horiz_getpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y) {
size_t index = (x + y * fb->stride) >> 3; size_t index = (x + y * fb->stride) >> 3;
int offset = fb->format == FRAMEBUF_MHMSB ? x & 0x07 : 7 - (x & 0x07); unsigned int offset = fb->format == FRAMEBUF_MHMSB ? x & 0x07 : 7 - (x & 0x07);
return (((uint8_t *)fb->buf)[index] >> (offset)) & 0x01; return (((uint8_t *)fb->buf)[index] >> (offset)) & 0x01;
} }
STATIC void mono_horiz_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) { STATIC void mono_horiz_fill_rect(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, unsigned int w, unsigned int h, uint32_t col) {
int reverse = fb->format == FRAMEBUF_MHMSB; unsigned int reverse = fb->format == FRAMEBUF_MHMSB;
int advance = fb->stride >> 3; unsigned int advance = fb->stride >> 3;
while (w--) { while (w--) {
uint8_t *b = &((uint8_t *)fb->buf)[(x >> 3) + y * advance]; uint8_t *b = &((uint8_t *)fb->buf)[(x >> 3) + y * advance];
int offset = reverse ? x & 7 : 7 - (x & 7); unsigned int offset = reverse ? x & 7 : 7 - (x & 7);
for (int hh = h; hh; --hh) { for (unsigned int hh = h; hh; --hh) {
*b = (*b & ~(0x01 << offset)) | ((col != 0) << offset); *b = (*b & ~(0x01 << offset)) | ((col != 0) << offset);
b += advance; b += advance;
} }
...@@ -94,21 +94,21 @@ STATIC void mono_horiz_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int ...@@ -94,21 +94,21 @@ STATIC void mono_horiz_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int
// Functions for MVLSB format // Functions for MVLSB format
STATIC void mvlsb_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) { STATIC void mvlsb_setpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, uint32_t col) {
size_t index = (y >> 3) * fb->stride + x; size_t index = (y >> 3) * fb->stride + x;
uint8_t offset = y & 0x07; uint8_t offset = y & 0x07;
((uint8_t *)fb->buf)[index] = (((uint8_t *)fb->buf)[index] & ~(0x01 << offset)) | ((col != 0) << offset); ((uint8_t *)fb->buf)[index] = (((uint8_t *)fb->buf)[index] & ~(0x01 << offset)) | ((col != 0) << offset);
} }
STATIC uint32_t mvlsb_getpixel(const mp_obj_framebuf_t *fb, int x, int y) { STATIC uint32_t mvlsb_getpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y) {
return (((uint8_t *)fb->buf)[(y >> 3) * fb->stride + x] >> (y & 0x07)) & 0x01; return (((uint8_t *)fb->buf)[(y >> 3) * fb->stride + x] >> (y & 0x07)) & 0x01;
} }
STATIC void mvlsb_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) { STATIC void mvlsb_fill_rect(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, unsigned int w, unsigned int h, uint32_t col) {
while (h--) { while (h--) {
uint8_t *b = &((uint8_t *)fb->buf)[(y >> 3) * fb->stride + x]; uint8_t *b = &((uint8_t *)fb->buf)[(y >> 3) * fb->stride + x];
uint8_t offset = y & 0x07; uint8_t offset = y & 0x07;
for (int ww = w; ww; --ww) { for (unsigned int ww = w; ww; --ww) {
*b = (*b & ~(0x01 << offset)) | ((col != 0) << offset); *b = (*b & ~(0x01 << offset)) | ((col != 0) << offset);
++b; ++b;
} }
...@@ -118,18 +118,18 @@ STATIC void mvlsb_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, in ...@@ -118,18 +118,18 @@ STATIC void mvlsb_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, in
// Functions for RGB565 format // Functions for RGB565 format
STATIC void rgb565_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) { STATIC void rgb565_setpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, uint32_t col) {
((uint16_t *)fb->buf)[x + y * fb->stride] = col; ((uint16_t *)fb->buf)[x + y * fb->stride] = col;
} }
STATIC uint32_t rgb565_getpixel(const mp_obj_framebuf_t *fb, int x, int y) { STATIC uint32_t rgb565_getpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y) {
return ((uint16_t *)fb->buf)[x + y * fb->stride]; return ((uint16_t *)fb->buf)[x + y * fb->stride];
} }
STATIC void rgb565_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) { STATIC void rgb565_fill_rect(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, unsigned int w, unsigned int h, uint32_t col) {
uint16_t *b = &((uint16_t *)fb->buf)[x + y * fb->stride]; uint16_t *b = &((uint16_t *)fb->buf)[x + y * fb->stride];
while (h--) { while (h--) {
for (int ww = w; ww; --ww) { for (unsigned int ww = w; ww; --ww) {
*b++ = col; *b++ = col;
} }
b += fb->stride - w; b += fb->stride - w;
...@@ -138,7 +138,7 @@ STATIC void rgb565_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, i ...@@ -138,7 +138,7 @@ STATIC void rgb565_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, i
// Functions for GS2_HMSB format // Functions for GS2_HMSB format
STATIC void gs2_hmsb_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) { STATIC void gs2_hmsb_setpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, uint32_t col) {
uint8_t *pixel = &((uint8_t *)fb->buf)[(x + y * fb->stride) >> 2]; uint8_t *pixel = &((uint8_t *)fb->buf)[(x + y * fb->stride) >> 2];
uint8_t shift = (x & 0x3) << 1; uint8_t shift = (x & 0x3) << 1;
uint8_t mask = 0x3 << shift; uint8_t mask = 0x3 << shift;
...@@ -146,15 +146,15 @@ STATIC void gs2_hmsb_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_ ...@@ -146,15 +146,15 @@ STATIC void gs2_hmsb_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_
*pixel = color | (*pixel & (~mask)); *pixel = color | (*pixel & (~mask));
} }
STATIC uint32_t gs2_hmsb_getpixel(const mp_obj_framebuf_t *fb, int x, int y) { STATIC uint32_t gs2_hmsb_getpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y) {
uint8_t pixel = ((uint8_t *)fb->buf)[(x + y * fb->stride) >> 2]; uint8_t pixel = ((uint8_t *)fb->buf)[(x + y * fb->stride) >> 2];
uint8_t shift = (x & 0x3) << 1; uint8_t shift = (x & 0x3) << 1;
return (pixel >> shift) & 0x3; return (pixel >> shift) & 0x3;
} }
STATIC void gs2_hmsb_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) { STATIC void gs2_hmsb_fill_rect(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, unsigned int w, unsigned int h, uint32_t col) {
for (int xx = x; xx < x + w; xx++) { for (unsigned int xx = x; xx < x + w; xx++) {
for (int yy = y; yy < y + h; yy++) { for (unsigned int yy = y; yy < y + h; yy++) {
gs2_hmsb_setpixel(fb, xx, yy, col); gs2_hmsb_setpixel(fb, xx, yy, col);
} }
} }
...@@ -162,7 +162,7 @@ STATIC void gs2_hmsb_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, ...@@ -162,7 +162,7 @@ STATIC void gs2_hmsb_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w,
// Functions for GS4_HMSB format // Functions for GS4_HMSB format
STATIC void gs4_hmsb_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) { STATIC void gs4_hmsb_setpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, uint32_t col) {
uint8_t *pixel = &((uint8_t *)fb->buf)[(x + y * fb->stride) >> 1]; uint8_t *pixel = &((uint8_t *)fb->buf)[(x + y * fb->stride) >> 1];
if (x % 2) { if (x % 2) {
...@@ -172,7 +172,7 @@ STATIC void gs4_hmsb_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_ ...@@ -172,7 +172,7 @@ STATIC void gs4_hmsb_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_
} }
} }
STATIC uint32_t gs4_hmsb_getpixel(const mp_obj_framebuf_t *fb, int x, int y) { STATIC uint32_t gs4_hmsb_getpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y) {
if (x % 2) { if (x % 2) {
return ((uint8_t *)fb->buf)[(x + y * fb->stride) >> 1] & 0x0f; return ((uint8_t *)fb->buf)[(x + y * fb->stride) >> 1] & 0x0f;
} }
...@@ -180,16 +180,16 @@ STATIC uint32_t gs4_hmsb_getpixel(const mp_obj_framebuf_t *fb, int x, int y) { ...@@ -180,16 +180,16 @@ STATIC uint32_t gs4_hmsb_getpixel(const mp_obj_framebuf_t *fb, int x, int y) {
return ((uint8_t *)fb->buf)[(x + y * fb->stride) >> 1] >> 4; return ((uint8_t *)fb->buf)[(x + y * fb->stride) >> 1] >> 4;
} }
STATIC void gs4_hmsb_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) { STATIC void gs4_hmsb_fill_rect(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, unsigned int w, unsigned int h, uint32_t col) {
col &= 0x0f; col &= 0x0f;
uint8_t *pixel_pair = &((uint8_t *)fb->buf)[(x + y * fb->stride) >> 1]; uint8_t *pixel_pair = &((uint8_t *)fb->buf)[(x + y * fb->stride) >> 1];
uint8_t col_shifted_left = col << 4; uint8_t col_shifted_left = col << 4;
uint8_t col_pixel_pair = col_shifted_left | col; uint8_t col_pixel_pair = col_shifted_left | col;
int pixel_count_till_next_line = (fb->stride - w) >> 1; unsigned int pixel_count_till_next_line = (fb->stride - w) >> 1;
bool odd_x = (x % 2 == 1); bool odd_x = (x % 2 == 1);
while (h--) { while (h--) {
int ww = w; unsigned int ww = w;
if (odd_x && ww > 0) { if (odd_x && ww > 0) {
*pixel_pair = (*pixel_pair & 0xf0) | col; *pixel_pair = (*pixel_pair & 0xf0) | col;
...@@ -213,16 +213,16 @@ STATIC void gs4_hmsb_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, ...@@ -213,16 +213,16 @@ STATIC void gs4_hmsb_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w,
// Functions for GS8 format // Functions for GS8 format
STATIC void gs8_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) { STATIC void gs8_setpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, uint32_t col) {
uint8_t *pixel = &((uint8_t *)fb->buf)[(x + y * fb->stride)]; uint8_t *pixel = &((uint8_t *)fb->buf)[(x + y * fb->stride)];
*pixel = col & 0xff; *pixel = col & 0xff;
} }
STATIC uint32_t gs8_getpixel(const mp_obj_framebuf_t *fb, int x, int y) { STATIC uint32_t gs8_getpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y) {
return ((uint8_t *)fb->buf)[(x + y * fb->stride)]; return ((uint8_t *)fb->buf)[(x + y * fb->stride)];
} }
STATIC void gs8_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) { STATIC void gs8_fill_rect(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, unsigned int w, unsigned int h, uint32_t col) {
uint8_t *pixel = &((uint8_t *)fb->buf)[(x + y * fb->stride)]; uint8_t *pixel = &((uint8_t *)fb->buf)[(x + y * fb->stride)];
while (h--) { while (h--) {
memset(pixel, col, w); memset(pixel, col, w);
...@@ -240,11 +240,11 @@ STATIC mp_framebuf_p_t formats[] = { ...@@ -240,11 +240,11 @@ STATIC mp_framebuf_p_t formats[] = {
[FRAMEBUF_MHMSB] = {mono_horiz_setpixel, mono_horiz_getpixel, mono_horiz_fill_rect}, [FRAMEBUF_MHMSB] = {mono_horiz_setpixel, mono_horiz_getpixel, mono_horiz_fill_rect},
}; };
static inline void setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) { static inline void setpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y, uint32_t col) {
formats[fb->format].setpixel(fb, x, y, col); formats[fb->format].setpixel(fb, x, y, col);
} }
static inline uint32_t getpixel(const mp_obj_framebuf_t *fb, int x, int y) { static inline uint32_t getpixel(const mp_obj_framebuf_t *fb, unsigned int x, unsigned int y) {
return formats[fb->format].getpixel(fb, x, y); return formats[fb->format].getpixel(fb, x, y);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment