From ed9fd9320c97124480a7043032dfb9044d7b4074 Mon Sep 17 00:00:00 2001 From: Amy Bowersox Date: Wed, 11 Dec 2019 14:03:11 -0700 Subject: [PATCH] added stock bitmap handling and support for it in the demo script --- scripts/demo1.py | 10 ++++++++++ src/Makefile | 2 +- src/bitmap.h | 2 ++ src/ep_bitmap.c | 25 +++++++++++++++++++------ src/stockobj.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 7 deletions(-) create mode 100755 src/stockobj.c diff --git a/scripts/demo1.py b/scripts/demo1.py index 5a1c37e..3c3c5cd 100755 --- a/scripts/demo1.py +++ b/scripts/demo1.py @@ -6,7 +6,14 @@ WHITE = upiwin.rgb(255, 255, 255) LTGRAY = upiwin.rgb(204, 204, 204) YELLOW = upiwin.rgb(255, 255, 0) +# Get the stock bitmaps. +bmp_freehand = upiwin.Bitmap(stock='freehand') +bmp_line = upiwin.Bitmap(stock='line') +bmp_rect = upiwin.Bitmap(stock='rect') +bmp_fillrect = upiwin.Bitmap(stock='fillrect') + hdc = upiwin.DevCtxt(type='screen') +hdc_bits = upiwin.DevCtxt(type='memory') # divide the screen into "drawing" and "command" areas screen_rect = hdc.get_clip_rect() @@ -100,6 +107,9 @@ hdc.rectangle(cmd2_rect[0], cmd2_rect[1], cmd2_rect[2], cmd2_rect[3]) hdc.rectangle(cmd3_rect[0], cmd3_rect[1], cmd3_rect[2], cmd3_rect[3]) hdc.rectangle(cmd4_rect[0], cmd4_rect[1], cmd4_rect[2], cmd4_rect[3]) +hdc_bits.select_object(bmp_freehand) +hdc.bitblt(cmd1_rect[0] + 6, cmd1_rect[1] + 6, cmd1_rect[0] + 54, cmd1_rect[1] + 54, hdc_bits, 0, 0, 0) + # Main message loop msg = {} while upiwin.get_message(msg): diff --git a/src/Makefile b/src/Makefile index 99774fa..b8289c8 100644 --- a/src/Makefile +++ b/src/Makefile @@ -4,7 +4,7 @@ SPLASHSCREEN=splash-vmwcblk.png OBJS=main.o sysinput.o ep_init.o ep_upiwin.o ep_backlight.o ep_msg.o ep_graphics.o ep_devctxt.o ep_bitmap.o \ ep_upiwin_tmp.o ep_util.o fbinit.o rect.o gfxobj.o devctxt.o dc_screen.o fontengine.o \ - bitmap.o fbprimitive.o log.o gpio.o msg_queue.o time_func.o config.o \ + bitmap.o stockobj.o fbprimitive.o log.o gpio.o msg_queue.o time_func.o config.o \ i_freehand.o i_line.o i_rect.o i_fillrect.o splash.o LIBS=-lpython3.7m -lcrypt -lfreetype -lbcm2835 -lpthread -ldl -lutil -lm CFLAGS=-I/usr/include/python3.7m -I/usr/include/freetype2 -I/usr/include/libpng16 \ diff --git a/src/bitmap.h b/src/bitmap.h index f7500b5..4a306e2 100755 --- a/src/bitmap.h +++ b/src/bitmap.h @@ -16,4 +16,6 @@ typedef struct tagBITMAP { extern PBITMAP BMP_Create(INT32 width, INT32 height, const void *bits); extern void BMP_Delete(PBITMAP pbmp); +extern PBITMAP _BMP_GetStock(PCSTR name); + #endif /* __BITMAP_H_INCLUDED */ \ No newline at end of file diff --git a/src/ep_bitmap.c b/src/ep_bitmap.c index abb2406..dfeef62 100755 --- a/src/ep_bitmap.c +++ b/src/ep_bitmap.c @@ -40,19 +40,32 @@ static PyGetSetDef BitmapProperties[] = { static int bitmap_init(BitmapObject *self, PyObject *args, PyObject *kwds) { - static char *kwlist[] = { "width", "height", NULL }; + static char *kwlist[] = { "stock", "width", "height", NULL }; + const char *stock; int width = 0, height = 0; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "$ii", kwlist, &width, &height)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "$sii", kwlist, &stock, &width, &height)) return -1; - width = MAX(1, width); - height = MAX(1, height); - self->pbmp = BMP_Create(width, height, NULL); - if (!(self->pbmp)) + if (stock) { + self->pbmp = _BMP_GetStock(stock); + if (!(self->pbmp)) + { + PyErr_Format(PyExc_RuntimeError, "no such stock bitmap: '%s'", stock); + return -1; + } + } + else + { + width = MAX(1, width); + height = MAX(1, height); + self->pbmp = BMP_Create(width, height, NULL); + if (!(self->pbmp)) + { PyErr_SetString(PyExc_RuntimeError, "unable to create bitmap"); return -1; + } } return 0; } diff --git a/src/stockobj.c b/src/stockobj.c new file mode 100755 index 0000000..272d390 --- /dev/null +++ b/src/stockobj.c @@ -0,0 +1,46 @@ +#include +#include +#include "wintype.h" +#include "bitmap.h" + +/* references to the icon data */ +extern uint8_t _binary_i_freehand_bin_start[]; +extern uint8_t _binary_i_freehand_bin_end; +extern uint8_t _binary_i_freehand_bin_size; + +extern uint8_t _binary_i_line_bin_start[]; +extern uint8_t _binary_i_line_bin_end; +extern uint8_t _binary_i_line_bin_size; + +extern uint8_t _binary_i_rect_bin_start[]; +extern uint8_t _binary_i_rect_bin_end; +extern uint8_t _binary_i_rect_bin_size; + +extern uint8_t _binary_i_fillrect_bin_start[]; +extern uint8_t _binary_i_fillrect_bin_end; +extern uint8_t _binary_i_fillrect_bin_size; + +typedef struct tagSTOCKBITMAPDESC { + PCSTR name; + INT32 width; + INT32 height; + const void *data; +} STOCKBITMAPDESC; + +static const STOCKBITMAPDESC stock_bitmaps = { + {"freehand", 48, 48, _binary_i_freehand_bin_start }, + {"line", 48, 48, _binary_i_line_bin_start }, + {"rect", 48, 48, _binary_i_rect_bin_start }, + {"fillrect", 48, 48, _binary_i_fillrect_bin_start }, + {NULL, 0, 0, NULL } +}; + +PBITMAP _BMP_GetStock(PCSTR name) +{ + INT32 i; + + for (i = 0; stock_bitmaps[i].name; ++i) + if (strcmp(name, stock_bitmaps[i].name) == 0) + return BMP_Create(stock_bitmaps[i].width, stock_bitmaps[i].height, stock_bitmaps[i].data); + return NULL; +}