1
0
Fork 0
mirror of https://github.com/shlomif/PySolFC.git synced 2025-04-05 00:02:29 -04:00

trying to get a qpixmap from libkcardgame.

This commit is contained in:
Shlomi Fish 2019-04-28 14:09:18 +03:00
parent f62780c6fc
commit 5daf079d09
4 changed files with 107 additions and 1 deletions
kcardgame
pysollib/ui/tktile

7
kcardgame/Makefile Normal file
View file

@ -0,0 +1,7 @@
# ---- Link ---------------------------
kcardgame.so: kcardgame.o Makefile
gcc -shared -o kcardgame.so kcardgame.o `pkg-config --libs python3`
# ---- gcc C compile ------------------
kcardgame.o: kcardgame.cpp kcardgame.hpp Makefile
gcc `pkg-config --cflags Qt5Gui` -I build-kpat/libkcardgame/ -I kpat/libkcardgame/include -I kpat/libkcardgame/ -g -fPIC -c kcardgame.cpp -I /usr/include/python3.7m/ -I /usr/lib64/python3.7/site-packages/numpy/core/include/numpy

89
kcardgame/kcardgame.cpp Normal file
View file

@ -0,0 +1,89 @@
/* A file to test imorting C modules for handling arrays to Python */
#include "Python.h"
#include "arrayobject.h"
#include "kcardgame.hpp"
#include <math.h>
/* #### Globals #################################### */
/* ==== Set up the methods table ====================== */
static struct PyMethodDef _kcardgameMethods[] = {
{"np_kcardgame", np_kcardgame, METH_VARARGS, "kcardgame"},
{NULL, NULL, 0, NULL} /* Sentinel - marks the end of this structure */
};
static struct PyModuleDef _kcardgame_mod = {
PyModuleDef_HEAD_INIT,
"_kcardgame",
NULL,
-1,
_kcardgameMethods
};
/* ==== Initialize the C_test functions ====================== */
// Module name must be _kcardgame in compile and linked
PyMODINIT_FUNC
PyInit_kcardgame() {
PyObject* ret = PyModule_Create(&_kcardgame_mod);
import_array(); // Must be present for NumPy. Called first after above line.
return ret;
}
/* ==== Create 1D Carray from PyArray ======================
Assumes PyArray is contiguous in memory. */
static inline uint64_t *pyvector_to_Carrayptrs(PyArrayObject *arrayin) {
return (uint64_t *) arrayin->data; /* pointer to arrayin data as double */
}
int my_kcardgame(
const uint64_t *const cin,
const size_t n)
{
unsigned __int128 sum = 0;
/* Operate on the vectors */
for ( size_t i=0; i<n; i++) {
sum += cin[i];
}
return (int)(sum % 1000000007);
}
#include "KCardDeck"
#include "KCardTheme"
static void del_kcardgame(PyObject * obj)
{
delete (KCardDeck *)PyCapsule_GetPointer(obj, "KCardDeck");
}
static PyObject *np_kcardgame(PyObject *self, PyObject *args)
{
auto ret = new KCardDeck( KCardTheme(), nullptr);
return PyCapsule_New(ret, "KCardDeck", del_kcardgame);
}
/* ==== Square vector components & multiply by a float =========================
/* #### Vector Utility functions ######################### */
/* ==== Make a Python Array Obj. from a PyObject, ================
generates a double vector w/ contiguous memory which may be a new allocation if
the original was not a double type or contiguous
!! Must DECREF the object returned from this routine unless it is returned to the
caller of this routines caller using return PyArray_Return(obj) or
PyArray_BuildValue with the "N" construct !!!
*/
PyArrayObject *pyvector(PyObject *objin) {
return (PyArrayObject *) PyArray_ContiguousFromObject(objin,
NPY_DOUBLE, 1,1);
}
/* ==== Check that PyArrayObject is a double (Float) type and a vector ==============
return 1 if an error and raise exception */
int not_doublevector(PyArrayObject *vec) {
if (vec->descr->type_num != NPY_UINT64 || vec->nd != 1) {
PyErr_SetString(PyExc_ValueError,
"In not_doublevector: array must be of type uint644 and 1 dimensional (n).");
return 1; }
return 0;
}
/* #### Matrix Extensions ############################## */

10
kcardgame/kcardgame.hpp Normal file
View file

@ -0,0 +1,10 @@
/* Header to test of C modules for arrays for Python: C_test.c */
/* ==== Prototypes =================================== */
// .... Python callable Vector functions ..................
static PyObject *np_kcardgame(PyObject *self, PyObject *args);
/* .... C vector utility functions ..................*/
PyArrayObject *pyvector(PyObject *objin);
int not_doublevector(PyArrayObject *vec);

View file

@ -25,6 +25,7 @@ class SVGManager:
# Taken from https://stackoverflow.com/questions/44471795
# Under MIT License - thanks.
@pysnooper.snoop()
def pixbuf2image(self, pxb):
""" Convert GdkPixbuf.Pixbuf to PIL image """
data = pxb.get_pixels()
@ -37,7 +38,6 @@ class SVGManager:
img = Image.frombytes(mode, (w, h), data, "raw", mode, stride)
return img
@pysnooper.snoop()
def render_fragment(self, id_, width, height):
id__ = '#' + id_
"""docstring for render_"""