2019-12-11 10:45:45 -07:00
|
|
|
#include <string.h>
|
|
|
|
#define PY_SSIZE_T_CLEAN
|
|
|
|
#include <Python.h>
|
2019-12-11 10:57:07 -07:00
|
|
|
#include "scode.h"
|
2019-12-11 14:19:51 -07:00
|
|
|
#include "log.h"
|
2019-12-11 10:45:45 -07:00
|
|
|
#include "gfxobj.h"
|
|
|
|
#include "bitmap.h"
|
|
|
|
#include "ep_types.h"
|
|
|
|
|
|
|
|
static PyMethodDef BitmapMethods[] = {
|
|
|
|
{NULL, NULL, 0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject *bitmap_get_width(BitmapObject *self, void *closure)
|
|
|
|
{
|
|
|
|
if (!(self->pbmp))
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "bad bitmap object");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyLong_FromUnsignedLong(self->pbmp->width);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *bitmap_get_height(BitmapObject *self, void *closure)
|
|
|
|
{
|
|
|
|
if (!(self->pbmp))
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "bad bitmap object");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyLong_FromUnsignedLong(self->pbmp->height);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyGetSetDef BitmapProperties[] = {
|
|
|
|
{"width", (getter)bitmap_get_width, NULL,
|
|
|
|
"Width of this bitmap", NULL},
|
|
|
|
{"height", (getter)bitmap_get_height, NULL,
|
|
|
|
"Height of this bitmap", NULL},
|
|
|
|
{NULL, NULL, NULL, NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static int bitmap_init(BitmapObject *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
2019-12-11 14:03:11 -07:00
|
|
|
static char *kwlist[] = { "stock", "width", "height", NULL };
|
|
|
|
const char *stock;
|
2019-12-11 10:45:45 -07:00
|
|
|
int width = 0, height = 0;
|
|
|
|
|
2019-12-11 14:19:51 -07:00
|
|
|
Log(LDEBUG, "Bitmap init entry");
|
2019-12-11 14:06:26 -07:00
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|$sii", kwlist, &stock, &width, &height))
|
2019-12-11 10:45:45 -07:00
|
|
|
return -1;
|
|
|
|
|
2019-12-11 14:03:11 -07:00
|
|
|
if (stock)
|
|
|
|
{
|
2019-12-11 14:19:51 -07:00
|
|
|
Log(LDEBUG, "stock bitmap");
|
2019-12-11 14:03:11 -07:00
|
|
|
self->pbmp = _BMP_GetStock(stock);
|
|
|
|
if (!(self->pbmp))
|
|
|
|
{
|
|
|
|
PyErr_Format(PyExc_RuntimeError, "no such stock bitmap: '%s'", stock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2019-12-11 10:45:45 -07:00
|
|
|
{
|
2019-12-11 14:19:51 -07:00
|
|
|
Log(LDEBUG, "basic bitmap");
|
2019-12-11 14:03:11 -07:00
|
|
|
width = MAX(1, width);
|
|
|
|
height = MAX(1, height);
|
|
|
|
self->pbmp = BMP_Create(width, height, NULL);
|
|
|
|
if (!(self->pbmp))
|
|
|
|
{
|
2019-12-11 10:45:45 -07:00
|
|
|
PyErr_SetString(PyExc_RuntimeError, "unable to create bitmap");
|
|
|
|
return -1;
|
2019-12-11 14:03:11 -07:00
|
|
|
}
|
2019-12-11 10:45:45 -07:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bitmap_dealloc(BitmapObject *self)
|
|
|
|
{
|
|
|
|
if (self->pbmp)
|
|
|
|
BMP_Delete(self->pbmp);
|
|
|
|
Py_TYPE(self)->tp_free((PyObject *)self);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyTypeObject BitmapType = {
|
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
|
|
|
.tp_name = "upiwin.Bitmap",
|
|
|
|
.tp_doc = "Bitmap object",
|
|
|
|
.tp_basicsize = sizeof(BitmapObject),
|
|
|
|
.tp_itemsize = 0,
|
|
|
|
.tp_flags = Py_TPFLAGS_DEFAULT,
|
|
|
|
.tp_new = PyType_GenericNew,
|
|
|
|
.tp_init = (initproc)bitmap_init,
|
|
|
|
.tp_dealloc = (destructor)bitmap_dealloc,
|
|
|
|
.tp_methods = BitmapMethods,
|
|
|
|
.tp_getset = BitmapProperties,
|
|
|
|
};
|
|
|
|
|
|
|
|
HRESULT Epython_register_bitmap(PyObject *module)
|
|
|
|
{
|
|
|
|
if (PyType_Ready(&BitmapType) < 0)
|
|
|
|
return E_FAIL;
|
|
|
|
Py_INCREF(&BitmapType);
|
|
|
|
if (PyModule_AddObject(module, "Bitmap", (PyObject *)(&BitmapType)) < 0)
|
|
|
|
{
|
|
|
|
Py_DECREF(&BitmapType);
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *Epython_wrap_bitmap(PBITMAP pbmp)
|
|
|
|
{
|
|
|
|
PyObject *rc = NULL, *args, *kwargs;
|
|
|
|
BitmapObject *pbitmapobj;
|
|
|
|
|
|
|
|
args = PyTuple_New(0);
|
|
|
|
if (args)
|
|
|
|
{
|
|
|
|
kwargs = PyDict_New();
|
|
|
|
if (kwargs)
|
|
|
|
{
|
2019-12-11 14:19:51 -07:00
|
|
|
Log(LDEBUG, "gonna create it");
|
2019-12-11 10:45:45 -07:00
|
|
|
rc = PyType_GenericNew(&BitmapType, args, kwargs);
|
2019-12-11 14:19:51 -07:00
|
|
|
Log(LDEBUG, "created it");
|
2019-12-11 10:45:45 -07:00
|
|
|
if (rc)
|
|
|
|
{
|
2019-12-11 10:59:14 -07:00
|
|
|
pbitmapobj = (BitmapObject *)rc;
|
2019-12-11 14:19:51 -07:00
|
|
|
Log(LDEBUG, "gonna delete bitmap");
|
2019-12-11 14:21:27 -07:00
|
|
|
if (pbitmapobj->pbmp)
|
|
|
|
BMP_Delete(pbitmapobj->pbmp);
|
2019-12-11 14:19:51 -07:00
|
|
|
Log(LDEBUG, "deleted bitmap");
|
2019-12-11 10:45:45 -07:00
|
|
|
pbitmapobj->pbmp = pbmp;
|
|
|
|
}
|
|
|
|
Py_DECREF(kwargs);
|
|
|
|
}
|
|
|
|
Py_DECREF(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rc)
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "unable to create bitmap");
|
|
|
|
return rc;
|
|
|
|
}
|