2013-11-30 17:41:38 +01:00
|
|
|
// Python include
|
|
|
|
#include <Python.h>
|
|
|
|
|
2013-11-26 21:38:24 +01:00
|
|
|
// stl includes
|
2013-11-27 20:15:29 +01:00
|
|
|
#include <iostream>
|
2013-11-26 21:38:24 +01:00
|
|
|
#include <sstream>
|
2016-10-14 20:59:03 +02:00
|
|
|
#include <cmath>
|
2013-11-26 21:38:24 +01:00
|
|
|
|
2013-11-28 14:29:31 +01:00
|
|
|
// Qt includes
|
|
|
|
#include <QDateTime>
|
2016-09-13 11:51:16 +02:00
|
|
|
#include <QFile>
|
2016-09-21 22:01:50 +02:00
|
|
|
#include <Qt>
|
|
|
|
#include <QLinearGradient>
|
|
|
|
#include <QConicalGradient>
|
|
|
|
#include <QRadialGradient>
|
|
|
|
#include <QRect>
|
2013-11-28 14:29:31 +01:00
|
|
|
|
2013-11-26 21:38:24 +01:00
|
|
|
// effect engin eincludes
|
2013-11-24 16:10:48 +01:00
|
|
|
#include "Effect.h"
|
2016-06-23 13:48:49 +02:00
|
|
|
#include <utils/Logger.h>
|
2016-09-21 22:01:50 +02:00
|
|
|
#include <hyperion/Hyperion.h>
|
2013-11-24 16:10:48 +01:00
|
|
|
|
2013-11-28 14:15:52 +01:00
|
|
|
// Python method table
|
|
|
|
PyMethodDef Effect::effectMethods[] = {
|
2017-02-16 07:33:58 +01:00
|
|
|
{"setColor" , Effect::wrapSetColor , METH_VARARGS, "Set a new color for the leds."},
|
|
|
|
{"setImage" , Effect::wrapSetImage , METH_VARARGS, "Set a new image to process and determine new led colors."},
|
|
|
|
{"abort" , Effect::wrapAbort , METH_NOARGS, "Check if the effect should abort execution."},
|
|
|
|
{"imageShow" , Effect::wrapImageShow , METH_NOARGS, "set current effect image to hyperion core."},
|
2016-09-21 22:01:50 +02:00
|
|
|
{"imageCanonicalGradient", Effect::wrapImageCanonicalGradient, METH_VARARGS, ""},
|
2017-02-16 07:33:58 +01:00
|
|
|
{"imageRadialGradient" , Effect::wrapImageRadialGradient , METH_VARARGS, ""},
|
|
|
|
{"imageSolidFill" , Effect::wrapImageSolidFill , METH_VARARGS, ""},
|
|
|
|
{"imageDrawLine" , Effect::wrapImageDrawLine , METH_VARARGS, ""},
|
|
|
|
{"imageDrawRect" , Effect::wrapImageDrawRect , METH_VARARGS, ""},
|
|
|
|
{"imageSetPixel" , Effect::wrapImageSetPixel , METH_VARARGS, "set pixel color of image"},
|
|
|
|
{"imageGetPixel" , Effect::wrapImageGetPixel , METH_VARARGS, "get pixel color of image"},
|
2013-11-26 21:38:24 +01:00
|
|
|
{NULL, NULL, 0, NULL}
|
|
|
|
};
|
|
|
|
|
2014-02-26 18:10:17 +01:00
|
|
|
#if PY_MAJOR_VERSION >= 3
|
|
|
|
// create the hyperion module
|
|
|
|
struct PyModuleDef Effect::moduleDef = {
|
2014-03-01 19:28:57 +01:00
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
"hyperion", /* m_name */
|
|
|
|
"Hyperion module", /* m_doc */
|
|
|
|
-1, /* m_size */
|
|
|
|
Effect::effectMethods, /* m_methods */
|
|
|
|
NULL, /* m_reload */
|
|
|
|
NULL, /* m_traverse */
|
|
|
|
NULL, /* m_clear */
|
|
|
|
NULL, /* m_free */
|
2014-02-26 18:10:17 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
PyObject* Effect::PyInit_hyperion()
|
|
|
|
{
|
2014-03-01 19:28:57 +01:00
|
|
|
return PyModule_Create(&moduleDef);
|
2014-02-26 18:10:17 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
void Effect::PyInit_hyperion()
|
|
|
|
{
|
2014-03-01 19:28:57 +01:00
|
|
|
Py_InitModule("hyperion", effectMethods);
|
2014-02-26 18:10:17 +01:00
|
|
|
}
|
|
|
|
#endif
|
2013-11-26 21:38:24 +01:00
|
|
|
|
2014-02-26 18:10:17 +01:00
|
|
|
void Effect::registerHyperionExtensionModule()
|
|
|
|
{
|
2014-03-01 19:28:57 +01:00
|
|
|
PyImport_AppendInittab("hyperion", &PyInit_hyperion);
|
2014-02-26 18:10:17 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
Effect::Effect(PyThreadState * mainThreadState, int priority, int timeout, const QString & script, const QString & name, const QJsonObject & args)
|
2016-08-08 00:17:00 +02:00
|
|
|
: QThread()
|
|
|
|
, _mainThreadState(mainThreadState)
|
|
|
|
, _priority(priority)
|
|
|
|
, _timeout(timeout)
|
|
|
|
, _script(script)
|
2016-09-13 11:51:16 +02:00
|
|
|
, _name(name)
|
2016-08-08 00:17:00 +02:00
|
|
|
, _args(args)
|
|
|
|
, _endTime(-1)
|
|
|
|
, _interpreterThreadState(nullptr)
|
|
|
|
, _abortRequested(false)
|
|
|
|
, _imageProcessor(ImageProcessorFactory::getInstance().newImageProcessor())
|
|
|
|
, _colors()
|
2013-11-24 16:10:48 +01:00
|
|
|
{
|
2013-12-08 12:46:33 +01:00
|
|
|
_colors.resize(_imageProcessor->getLedCount(), ColorRgb::BLACK);
|
|
|
|
|
2014-04-30 22:53:05 +02:00
|
|
|
// disable the black border detector for effects
|
2016-07-15 10:28:12 +02:00
|
|
|
_imageProcessor->enableBlackBorderDetector(false);
|
2016-09-21 22:01:50 +02:00
|
|
|
|
|
|
|
// init effect image for image based effects, size is based on led layout
|
|
|
|
_imageSize = Hyperion::getInstance()->getLedGridSize();
|
|
|
|
_image = new QImage(_imageSize, QImage::Format_ARGB32_Premultiplied);
|
|
|
|
_image->fill(Qt::black);
|
|
|
|
_painter = new QPainter(_image);
|
2014-04-30 22:53:05 +02:00
|
|
|
|
2013-11-26 21:38:24 +01:00
|
|
|
// connect the finished signal
|
|
|
|
connect(this, SIGNAL(finished()), this, SLOT(effectFinished()));
|
2016-09-13 11:51:16 +02:00
|
|
|
Q_INIT_RESOURCE(EffectEngine);
|
2013-11-24 16:10:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Effect::~Effect()
|
|
|
|
{
|
2016-09-21 22:01:50 +02:00
|
|
|
delete _painter;
|
|
|
|
delete _image;
|
2013-11-24 16:10:48 +01:00
|
|
|
}
|
|
|
|
|
2013-11-26 21:38:24 +01:00
|
|
|
void Effect::run()
|
|
|
|
{
|
2014-03-01 19:28:57 +01:00
|
|
|
// switch to the main thread state and acquire the GIL
|
|
|
|
PyEval_RestoreThread(_mainThreadState);
|
2014-02-26 18:10:17 +01:00
|
|
|
|
2013-11-26 21:38:24 +01:00
|
|
|
// Initialize a new thread state
|
|
|
|
_interpreterThreadState = Py_NewInterpreter();
|
2013-11-27 20:15:29 +01:00
|
|
|
|
2014-03-01 19:28:57 +01:00
|
|
|
// import the buildtin Hyperion module
|
|
|
|
PyObject * module = PyImport_ImportModule("hyperion");
|
2014-02-26 18:10:17 +01:00
|
|
|
|
2014-03-01 19:28:57 +01:00
|
|
|
// add a capsule containing 'this' to the module to be able to retrieve the effect from the callback function
|
|
|
|
PyObject_SetAttrString(module, "__effectObj", PyCapsule_New(this, nullptr, nullptr));
|
2013-11-29 23:22:49 +01:00
|
|
|
|
2013-11-28 16:26:32 +01:00
|
|
|
// add ledCount variable to the interpreter
|
|
|
|
PyObject_SetAttrString(module, "ledCount", Py_BuildValue("i", _imageProcessor->getLedCount()));
|
2013-11-26 21:38:24 +01:00
|
|
|
|
2016-09-21 22:01:50 +02:00
|
|
|
// add imageWidth variable to the interpreter
|
|
|
|
PyObject_SetAttrString(module, "imageWidth", Py_BuildValue("i", _imageSize.width()));
|
|
|
|
|
|
|
|
// add imageHeight variable to the interpreter
|
|
|
|
PyObject_SetAttrString(module, "imageHeight", Py_BuildValue("i", _imageSize.height()));
|
|
|
|
|
2013-11-30 00:28:04 +01:00
|
|
|
// add a args variable to the interpreter
|
2013-11-30 17:41:38 +01:00
|
|
|
PyObject_SetAttrString(module, "args", json2python(_args));
|
2013-11-26 21:38:24 +01:00
|
|
|
|
2014-03-01 19:28:57 +01:00
|
|
|
// decref the module
|
|
|
|
Py_XDECREF(module);
|
2013-11-30 00:28:04 +01:00
|
|
|
|
2013-11-28 14:29:31 +01:00
|
|
|
// Set the end time if applicable
|
|
|
|
if (_timeout > 0)
|
|
|
|
{
|
|
|
|
_endTime = QDateTime::currentMSecsSinceEpoch() + _timeout;
|
|
|
|
}
|
|
|
|
|
2013-11-26 21:38:24 +01:00
|
|
|
// Run the effect script
|
2016-09-13 11:51:16 +02:00
|
|
|
QFile file (_script);
|
|
|
|
QByteArray python_code;
|
|
|
|
if (file.open(QIODevice::ReadOnly))
|
2013-11-30 00:28:04 +01:00
|
|
|
{
|
2016-09-13 11:51:16 +02:00
|
|
|
python_code = file.readAll();
|
2013-11-30 00:28:04 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-13 11:51:16 +02:00
|
|
|
Error(Logger::getInstance("EFFECTENGINE"), "Unable to open script file %s", _script.toUtf8().constData());
|
|
|
|
}
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
if (!python_code.isEmpty())
|
|
|
|
{
|
|
|
|
PyRun_SimpleString(python_code.constData());
|
2013-11-30 00:28:04 +01:00
|
|
|
}
|
2013-11-26 21:38:24 +01:00
|
|
|
|
|
|
|
// Clean up the thread state
|
|
|
|
Py_EndInterpreter(_interpreterThreadState);
|
|
|
|
_interpreterThreadState = nullptr;
|
|
|
|
PyEval_ReleaseLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Effect::getPriority() const
|
|
|
|
{
|
|
|
|
return _priority;
|
|
|
|
}
|
|
|
|
|
2013-11-30 15:54:47 +01:00
|
|
|
bool Effect::isAbortRequested() const
|
|
|
|
{
|
|
|
|
return _abortRequested;
|
|
|
|
}
|
|
|
|
|
2013-11-26 21:38:24 +01:00
|
|
|
void Effect::abort()
|
|
|
|
{
|
|
|
|
_abortRequested = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Effect::effectFinished()
|
|
|
|
{
|
|
|
|
emit effectFinished(this);
|
|
|
|
}
|
2013-11-28 14:15:52 +01:00
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
PyObject *Effect::json2python(const QJsonValue &jsonData) const
|
|
|
|
{
|
|
|
|
switch (jsonData.type())
|
2013-11-30 17:41:38 +01:00
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
case QJsonValue::Null:
|
|
|
|
return Py_BuildValue("");
|
|
|
|
case QJsonValue::Undefined:
|
|
|
|
return Py_BuildValue("");
|
|
|
|
case QJsonValue::Double:
|
2013-11-30 17:41:38 +01:00
|
|
|
{
|
2016-10-14 20:59:03 +02:00
|
|
|
if (std::rint(jsonData.toDouble()) != jsonData.toDouble())
|
|
|
|
{
|
|
|
|
return Py_BuildValue("d", jsonData.toDouble());
|
|
|
|
}
|
|
|
|
return Py_BuildValue("i", jsonData.toInt());
|
2013-11-30 17:41:38 +01:00
|
|
|
}
|
2016-10-09 22:22:17 +02:00
|
|
|
case QJsonValue::Bool:
|
|
|
|
return Py_BuildValue("i", jsonData.toBool() ? 1 : 0);
|
|
|
|
case QJsonValue::String:
|
|
|
|
return Py_BuildValue("s", jsonData.toString().toUtf8().constData());
|
|
|
|
case QJsonValue::Object:
|
2013-11-30 17:41:38 +01:00
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
PyObject * dict= PyDict_New();
|
|
|
|
QJsonObject objectData = jsonData.toObject();
|
|
|
|
for (QJsonObject::iterator i = objectData.begin(); i != objectData.end(); ++i)
|
|
|
|
{
|
|
|
|
PyObject * obj = json2python(*i);
|
|
|
|
PyDict_SetItemString(dict, i.key().toStdString().c_str(), obj);
|
|
|
|
Py_XDECREF(obj);
|
|
|
|
}
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
case QJsonValue::Array:
|
|
|
|
{
|
|
|
|
QJsonArray arrayData = jsonData.toArray();
|
|
|
|
PyObject * list = PyList_New(arrayData.size());
|
|
|
|
int index = 0;
|
|
|
|
for (QJsonArray::iterator i = arrayData.begin(); i != arrayData.end(); ++i, ++index)
|
|
|
|
{
|
|
|
|
PyObject * obj = json2python(*i);
|
|
|
|
PyList_SetItem(list, index, obj);
|
|
|
|
Py_XDECREF(obj);
|
|
|
|
}
|
|
|
|
return list;
|
2013-11-30 17:41:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(false);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-11-28 14:15:52 +01:00
|
|
|
PyObject* Effect::wrapSetColor(PyObject *self, PyObject *args)
|
|
|
|
{
|
2013-11-29 23:22:49 +01:00
|
|
|
// get the effect
|
2014-03-01 19:28:57 +01:00
|
|
|
Effect * effect = getEffect();
|
2013-11-29 23:22:49 +01:00
|
|
|
|
|
|
|
// check if we have aborted already
|
|
|
|
if (effect->_abortRequested)
|
|
|
|
{
|
|
|
|
return Py_BuildValue("");
|
|
|
|
}
|
|
|
|
|
|
|
|
// determine the timeout
|
|
|
|
int timeout = effect->_timeout;
|
|
|
|
if (timeout > 0)
|
|
|
|
{
|
|
|
|
timeout = effect->_endTime - QDateTime::currentMSecsSinceEpoch();
|
|
|
|
|
|
|
|
// we are done if the time has passed
|
|
|
|
if (timeout <= 0)
|
|
|
|
{
|
|
|
|
return Py_BuildValue("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the number of arguments
|
|
|
|
int argCount = PyTuple_Size(args);
|
|
|
|
if (argCount == 3)
|
|
|
|
{
|
|
|
|
// three seperate arguments for red, green, and blue
|
|
|
|
ColorRgb color;
|
|
|
|
if (PyArg_ParseTuple(args, "bbb", &color.red, &color.green, &color.blue))
|
|
|
|
{
|
2013-12-08 12:46:33 +01:00
|
|
|
std::fill(effect->_colors.begin(), effect->_colors.end(), color);
|
2016-10-10 18:29:54 +02:00
|
|
|
effect->setColors(effect->_priority, effect->_colors, timeout, false, hyperion::COMP_EFFECT);
|
2013-11-29 23:22:49 +01:00
|
|
|
return Py_BuildValue("");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (argCount == 1)
|
|
|
|
{
|
|
|
|
// bytearray of values
|
|
|
|
PyObject * bytearray = nullptr;
|
|
|
|
if (PyArg_ParseTuple(args, "O", &bytearray))
|
|
|
|
{
|
|
|
|
if (PyByteArray_Check(bytearray))
|
|
|
|
{
|
|
|
|
size_t length = PyByteArray_Size(bytearray);
|
|
|
|
if (length == 3 * effect->_imageProcessor->getLedCount())
|
|
|
|
{
|
|
|
|
char * data = PyByteArray_AS_STRING(bytearray);
|
2013-12-08 12:46:33 +01:00
|
|
|
memcpy(effect->_colors.data(), data, length);
|
2016-10-10 18:29:54 +02:00
|
|
|
effect->setColors(effect->_priority, effect->_colors, timeout, false, hyperion::COMP_EFFECT);
|
2013-11-29 23:22:49 +01:00
|
|
|
return Py_BuildValue("");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "Length of bytearray argument should be 3*ledCount");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "Argument is not a bytearray");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "Function expect 1 or 3 arguments");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// error
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "Unknown error");
|
|
|
|
return nullptr;
|
2013-11-28 14:15:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PyObject* Effect::wrapSetImage(PyObject *self, PyObject *args)
|
|
|
|
{
|
2013-11-29 23:53:58 +01:00
|
|
|
// get the effect
|
2014-03-01 19:28:57 +01:00
|
|
|
Effect * effect = getEffect();
|
2013-11-29 23:22:49 +01:00
|
|
|
|
|
|
|
// check if we have aborted already
|
|
|
|
if (effect->_abortRequested)
|
|
|
|
{
|
|
|
|
return Py_BuildValue("");
|
|
|
|
}
|
|
|
|
|
2013-11-29 23:53:58 +01:00
|
|
|
// determine the timeout
|
|
|
|
int timeout = effect->_timeout;
|
|
|
|
if (timeout > 0)
|
|
|
|
{
|
|
|
|
timeout = effect->_endTime - QDateTime::currentMSecsSinceEpoch();
|
|
|
|
|
|
|
|
// we are done if the time has passed
|
|
|
|
if (timeout <= 0)
|
|
|
|
{
|
|
|
|
return Py_BuildValue("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// bytearray of values
|
|
|
|
int width, height;
|
|
|
|
PyObject * bytearray = nullptr;
|
|
|
|
if (PyArg_ParseTuple(args, "iiO", &width, &height, &bytearray))
|
|
|
|
{
|
|
|
|
if (PyByteArray_Check(bytearray))
|
|
|
|
{
|
|
|
|
int length = PyByteArray_Size(bytearray);
|
|
|
|
if (length == 3 * width * height)
|
|
|
|
{
|
|
|
|
Image<ColorRgb> image(width, height);
|
|
|
|
char * data = PyByteArray_AS_STRING(bytearray);
|
2013-12-08 12:46:33 +01:00
|
|
|
memcpy(image.memptr(), data, length);
|
2013-11-29 23:53:58 +01:00
|
|
|
|
2013-12-08 12:46:33 +01:00
|
|
|
effect->_imageProcessor->process(image, effect->_colors);
|
2016-10-10 18:29:54 +02:00
|
|
|
effect->setColors(effect->_priority, effect->_colors, timeout, false, hyperion::COMP_EFFECT);
|
2013-11-29 23:53:58 +01:00
|
|
|
return Py_BuildValue("");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-19 10:54:15 +02:00
|
|
|
PyErr_SetString(PyExc_RuntimeError, "Length of bytearray argument should be 3*width*height");
|
2013-11-29 23:53:58 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "Argument 3 is not a bytearray");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// error
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "Unknown error");
|
|
|
|
return nullptr;
|
2013-11-28 14:15:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PyObject* Effect::wrapAbort(PyObject *self, PyObject *)
|
|
|
|
{
|
2014-03-01 19:28:57 +01:00
|
|
|
Effect * effect = getEffect();
|
2013-11-29 23:22:49 +01:00
|
|
|
|
2013-11-28 14:29:31 +01:00
|
|
|
// Test if the effect has reached it end time
|
|
|
|
if (effect->_timeout > 0 && QDateTime::currentMSecsSinceEpoch() > effect->_endTime)
|
|
|
|
{
|
|
|
|
effect->_abortRequested = true;
|
|
|
|
}
|
2013-11-29 23:22:49 +01:00
|
|
|
|
2013-12-08 12:46:33 +01:00
|
|
|
return Py_BuildValue("i", effect->_abortRequested ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2016-09-21 22:01:50 +02:00
|
|
|
|
|
|
|
PyObject* Effect::wrapImageShow(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
Effect * effect = getEffect();
|
|
|
|
|
|
|
|
// determine the timeout
|
|
|
|
int timeout = effect->_timeout;
|
|
|
|
if (timeout > 0)
|
|
|
|
{
|
|
|
|
timeout = effect->_endTime - QDateTime::currentMSecsSinceEpoch();
|
|
|
|
|
|
|
|
// we are done if the time has passed
|
|
|
|
if (timeout <= 0)
|
|
|
|
{
|
|
|
|
return Py_BuildValue("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int width = effect->_imageSize.width();
|
|
|
|
int height = effect->_imageSize.height();
|
|
|
|
QImage * qimage = effect->_image;
|
|
|
|
|
|
|
|
Image<ColorRgb> image(width, height);
|
|
|
|
QByteArray binaryImage;
|
|
|
|
|
|
|
|
for (int i = 0; i <qimage->height(); ++i)
|
|
|
|
{
|
|
|
|
const QRgb * scanline = reinterpret_cast<const QRgb *>(qimage->scanLine(i));
|
|
|
|
for (int j = 0; j < qimage->width(); ++j)
|
|
|
|
{
|
|
|
|
binaryImage.append((char) qRed(scanline[j]));
|
|
|
|
binaryImage.append((char) qGreen(scanline[j]));
|
|
|
|
binaryImage.append((char) qBlue(scanline[j]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(image.memptr(), binaryImage.data(), binaryImage.size());
|
|
|
|
effect->_imageProcessor->process(image, effect->_colors);
|
2016-10-10 18:29:54 +02:00
|
|
|
effect->setColors(effect->_priority, effect->_colors, timeout, false, hyperion::COMP_EFFECT);
|
2016-09-21 22:01:50 +02:00
|
|
|
|
|
|
|
return Py_BuildValue("");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject* Effect::wrapImageCanonicalGradient(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
Effect * effect = getEffect();
|
|
|
|
|
|
|
|
int argCount = PyTuple_Size(args);
|
|
|
|
PyObject * bytearray = nullptr;
|
|
|
|
int centerX, centerY, angle;
|
|
|
|
int startX = 0;
|
|
|
|
int startY = 0;
|
|
|
|
int width = effect->_imageSize.width();
|
|
|
|
int height = effect->_imageSize.height();
|
|
|
|
|
|
|
|
bool argsOK = false;
|
|
|
|
|
|
|
|
if ( argCount == 8 && PyArg_ParseTuple(args, "iiiiiiiO", &startX, &startY, &width, &height, ¢erX, ¢erY, &angle, &bytearray) )
|
|
|
|
{
|
2017-02-16 07:33:58 +01:00
|
|
|
argsOK = true;
|
2016-09-21 22:01:50 +02:00
|
|
|
}
|
|
|
|
if ( argCount == 4 && PyArg_ParseTuple(args, "iiiO", ¢erX, ¢erY, &angle, &bytearray) )
|
|
|
|
{
|
2017-02-16 07:33:58 +01:00
|
|
|
argsOK = true;
|
2016-09-21 22:01:50 +02:00
|
|
|
}
|
|
|
|
angle = std::max(std::min(angle,360),0);
|
|
|
|
|
|
|
|
if (argsOK)
|
|
|
|
{
|
|
|
|
if (PyByteArray_Check(bytearray))
|
|
|
|
{
|
2016-10-01 21:34:30 +02:00
|
|
|
const int length = PyByteArray_Size(bytearray);
|
|
|
|
const unsigned arrayItemLength = 5;
|
|
|
|
if (length % arrayItemLength == 0)
|
2016-09-21 22:01:50 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
QPainter * painter = effect->_painter;
|
|
|
|
QRect myQRect(startX,startY,width,height);
|
|
|
|
QConicalGradient gradient(QPoint(centerX,centerY), angle );
|
|
|
|
char * data = PyByteArray_AS_STRING(bytearray);
|
|
|
|
|
2016-10-01 21:34:30 +02:00
|
|
|
for (int idx=0; idx<length; idx+=arrayItemLength)
|
2016-09-21 22:01:50 +02:00
|
|
|
{
|
|
|
|
gradient.setColorAt(
|
|
|
|
((uint8_t)data[idx])/255.0,
|
|
|
|
QColor(
|
|
|
|
(uint8_t)(data[idx+1]),
|
|
|
|
(uint8_t)(data[idx+2]),
|
2016-10-01 21:34:30 +02:00
|
|
|
(uint8_t)(data[idx+3]),
|
|
|
|
(uint8_t)(data[idx+4])
|
2016-09-21 22:01:50 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
painter->fillRect(myQRect, gradient);
|
|
|
|
|
|
|
|
return Py_BuildValue("");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-01 21:34:30 +02:00
|
|
|
PyErr_SetString(PyExc_RuntimeError, "Length of bytearray argument should multiple of 5");
|
2016-09-21 22:01:50 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "Argument 8 is not a bytearray");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject* Effect::wrapImageRadialGradient(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
Effect * effect = getEffect();
|
|
|
|
|
|
|
|
int argCount = PyTuple_Size(args);
|
|
|
|
PyObject * bytearray = nullptr;
|
|
|
|
int centerX, centerY, radius, focalX, focalY, focalRadius;
|
|
|
|
int startX = 0;
|
|
|
|
int startY = 0;
|
|
|
|
int width = effect->_imageSize.width();
|
|
|
|
int height = effect->_imageSize.height();
|
|
|
|
|
|
|
|
bool argsOK = false;
|
|
|
|
|
|
|
|
if ( argCount == 11 && PyArg_ParseTuple(args, "iiiiiiiiiiO", &startX, &startY, &width, &height, ¢erX, ¢erY, &radius, &focalX, &focalY, &focalRadius, &bytearray) )
|
|
|
|
{
|
|
|
|
argsOK = true;
|
|
|
|
}
|
|
|
|
if ( argCount == 8 && PyArg_ParseTuple(args, "iiiiiiiO", &startX, &startY, &width, &height, ¢erX, ¢erY, &radius, &bytearray) )
|
|
|
|
{
|
|
|
|
argsOK = true;
|
|
|
|
focalX = centerX;
|
|
|
|
focalY = centerY;
|
|
|
|
focalRadius = radius;
|
|
|
|
}
|
|
|
|
if ( argCount == 7 && PyArg_ParseTuple(args, "iiiiiiO", ¢erX, ¢erY, &radius, &focalX, &focalY, &focalRadius, &bytearray) )
|
|
|
|
{
|
2017-02-16 07:33:58 +01:00
|
|
|
argsOK = true;
|
2016-09-21 22:01:50 +02:00
|
|
|
}
|
|
|
|
if ( argCount == 4 && PyArg_ParseTuple(args, "iiiO", ¢erX, ¢erY, &radius, &bytearray) )
|
|
|
|
{
|
|
|
|
argsOK = true;
|
|
|
|
focalX = centerX;
|
|
|
|
focalY = centerY;
|
|
|
|
focalRadius = radius;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argsOK)
|
|
|
|
{
|
|
|
|
if (PyByteArray_Check(bytearray))
|
|
|
|
{
|
|
|
|
int length = PyByteArray_Size(bytearray);
|
|
|
|
if (length % 4 == 0)
|
|
|
|
{
|
|
|
|
|
|
|
|
QPainter * painter = effect->_painter;
|
|
|
|
QRect myQRect(startX,startY,width,height);
|
|
|
|
QRadialGradient gradient(QPoint(centerX,centerY), std::max(radius,0) );
|
|
|
|
char * data = PyByteArray_AS_STRING(bytearray);
|
|
|
|
|
|
|
|
for (int idx=0; idx<length; idx+=4)
|
|
|
|
{
|
|
|
|
gradient.setColorAt(
|
|
|
|
((uint8_t)data[idx])/255.0,
|
|
|
|
QColor(
|
|
|
|
(uint8_t)(data[idx+1]),
|
|
|
|
(uint8_t)(data[idx+2]),
|
|
|
|
(uint8_t)(data[idx+3])
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
//gradient.setSpread(QGradient::ReflectSpread);
|
|
|
|
painter->fillRect(myQRect, gradient);
|
|
|
|
|
|
|
|
return Py_BuildValue("");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "Length of bytearray argument should multiple of 4");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "Last argument is not a bytearray");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-01 21:34:30 +02:00
|
|
|
PyObject* Effect::wrapImageSolidFill(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
Effect * effect = getEffect();
|
|
|
|
|
|
|
|
int argCount = PyTuple_Size(args);
|
|
|
|
int r, g, b;
|
|
|
|
int a = 255;
|
|
|
|
int startX = 0;
|
|
|
|
int startY = 0;
|
|
|
|
int width = effect->_imageSize.width();
|
|
|
|
int height = effect->_imageSize.height();
|
|
|
|
|
|
|
|
bool argsOK = false;
|
|
|
|
|
|
|
|
if ( argCount == 8 && PyArg_ParseTuple(args, "iiiiiiii", &startX, &startY, &width, &height, &r, &g, &b, &a) )
|
|
|
|
{
|
2017-02-16 07:33:58 +01:00
|
|
|
argsOK = true;
|
2016-10-01 21:34:30 +02:00
|
|
|
}
|
|
|
|
if ( argCount == 7 && PyArg_ParseTuple(args, "iiiiiii", &startX, &startY, &width, &height, &r, &g, &b) )
|
|
|
|
{
|
2017-02-16 07:33:58 +01:00
|
|
|
argsOK = true;
|
2016-10-01 21:34:30 +02:00
|
|
|
}
|
|
|
|
if ( argCount == 4 && PyArg_ParseTuple(args, "iiii",&r, &g, &b, &a) )
|
|
|
|
{
|
2017-02-16 07:33:58 +01:00
|
|
|
argsOK = true;
|
2016-10-01 21:34:30 +02:00
|
|
|
}
|
|
|
|
if ( argCount == 3 && PyArg_ParseTuple(args, "iii",&r, &g, &b) )
|
|
|
|
{
|
2017-02-16 07:33:58 +01:00
|
|
|
argsOK = true;
|
2016-10-01 21:34:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argsOK)
|
|
|
|
{
|
|
|
|
QRect myQRect(startX,startY,width,height);
|
|
|
|
effect->_painter->fillRect(myQRect, QColor(r,g,b,a));
|
|
|
|
return Py_BuildValue("");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 22:01:50 +02:00
|
|
|
|
2017-02-16 07:33:58 +01:00
|
|
|
PyObject* Effect::wrapImageDrawLine(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
Effect * effect = getEffect();
|
|
|
|
|
|
|
|
int argCount = PyTuple_Size(args);
|
|
|
|
int r, g, b;
|
|
|
|
int a = 255;
|
|
|
|
int startX = 0;
|
|
|
|
int startY = 0;
|
|
|
|
int thick = 1;
|
|
|
|
int endX = effect->_imageSize.width();
|
|
|
|
int endY = effect->_imageSize.height();
|
|
|
|
|
|
|
|
bool argsOK = false;
|
|
|
|
|
|
|
|
if ( argCount == 9 && PyArg_ParseTuple(args, "iiiiiiiii", &startX, &startY, &endX, &endY, &thick, &r, &g, &b, &a) )
|
|
|
|
{
|
|
|
|
argsOK = true;
|
|
|
|
}
|
|
|
|
if ( argCount == 8 && PyArg_ParseTuple(args, "iiiiiiii", &startX, &startY, &endX, &endY, &thick, &r, &g, &b) )
|
|
|
|
{
|
|
|
|
argsOK = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argsOK)
|
|
|
|
{
|
|
|
|
QRect myQRect(startX, startY, endX, endY);
|
|
|
|
QPen oldPen = effect->_painter->pen();
|
|
|
|
QPen newPen(QColor(r,g,b,a));
|
|
|
|
newPen.setWidth(thick);
|
|
|
|
effect->_painter->setPen(newPen);
|
|
|
|
effect->_painter->drawLine(startX, startY, endX, endY);
|
|
|
|
effect->_painter->setPen(oldPen);
|
|
|
|
|
|
|
|
return Py_BuildValue("");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject* Effect::wrapImageDrawRect(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
Effect * effect = getEffect();
|
|
|
|
|
|
|
|
int argCount = PyTuple_Size(args);
|
|
|
|
int r, g, b;
|
|
|
|
int a = 255;
|
|
|
|
int startX = 0;
|
|
|
|
int startY = 0;
|
|
|
|
int thick = 1;
|
|
|
|
int width = effect->_imageSize.width();
|
|
|
|
int height = effect->_imageSize.height();
|
|
|
|
|
|
|
|
bool argsOK = false;
|
|
|
|
|
|
|
|
if ( argCount == 9 && PyArg_ParseTuple(args, "iiiiiiiii", &startX, &startY, &width, &height, &thick, &r, &g, &b, &a) )
|
|
|
|
{
|
|
|
|
argsOK = true;
|
|
|
|
}
|
|
|
|
if ( argCount == 8 && PyArg_ParseTuple(args, "iiiiiiii", &startX, &startY, &width, &height, &thick, &r, &g, &b) )
|
|
|
|
{
|
|
|
|
argsOK = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argsOK)
|
|
|
|
{
|
|
|
|
QRect myQRect(startX,startY,width,height);
|
|
|
|
QPen oldPen = effect->_painter->pen();
|
|
|
|
QPen newPen(QColor(r,g,b,a));
|
|
|
|
newPen.setWidth(thick);
|
|
|
|
effect->_painter->setPen(newPen);
|
|
|
|
effect->_painter->drawRect(startX, startY, width, height);
|
|
|
|
effect->_painter->setPen(oldPen);
|
|
|
|
|
|
|
|
return Py_BuildValue("");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject* Effect::wrapImageSetPixel(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
Effect * effect = getEffect();
|
|
|
|
|
|
|
|
int argCount = PyTuple_Size(args);
|
|
|
|
int r, g, b, x, y;
|
|
|
|
|
|
|
|
bool argsOK = false;
|
|
|
|
|
|
|
|
if ( argCount == 5 && PyArg_ParseTuple(args, "iiiii", &x, &y, &r, &g, &b ) )
|
|
|
|
{
|
|
|
|
argsOK = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argsOK)
|
|
|
|
{
|
|
|
|
effect->_image->setPixel(x,y,qRgb(r,g,b));
|
|
|
|
|
|
|
|
return Py_BuildValue("");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject* Effect::wrapImageGetPixel(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
Effect * effect = getEffect();
|
|
|
|
|
|
|
|
int argCount = PyTuple_Size(args);
|
|
|
|
int x, y;
|
|
|
|
|
|
|
|
bool argsOK = false;
|
|
|
|
|
|
|
|
if ( argCount == 2 && PyArg_ParseTuple(args, "ii", &x, &y) )
|
|
|
|
{
|
|
|
|
argsOK = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argsOK)
|
|
|
|
{
|
|
|
|
QRgb rgb = effect->_image->pixel(x,y);
|
|
|
|
|
|
|
|
return Py_BuildValue("iii",qRed(rgb),qGreen(rgb),qBlue(rgb));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-26 18:10:17 +01:00
|
|
|
Effect * Effect::getEffect()
|
2013-12-08 12:46:33 +01:00
|
|
|
{
|
2014-03-01 19:28:57 +01:00
|
|
|
// extract the module from the runtime
|
|
|
|
PyObject * module = PyObject_GetAttrString(PyImport_AddModule("__main__"), "hyperion");
|
|
|
|
|
|
|
|
if (!PyModule_Check(module))
|
|
|
|
{
|
|
|
|
// something is wrong
|
|
|
|
Py_XDECREF(module);
|
2016-06-23 13:48:49 +02:00
|
|
|
Error(Logger::getInstance("EFFECTENGINE"), "Unable to retrieve the effect object from the Python runtime");
|
2014-03-01 19:28:57 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve the capsule with the effect
|
|
|
|
PyObject * effectCapsule = PyObject_GetAttrString(module, "__effectObj");
|
|
|
|
Py_XDECREF(module);
|
|
|
|
|
|
|
|
if (!PyCapsule_CheckExact(effectCapsule))
|
|
|
|
{
|
|
|
|
// something is wrong
|
|
|
|
Py_XDECREF(effectCapsule);
|
2016-06-23 13:48:49 +02:00
|
|
|
Error(Logger::getInstance("EFFECTENGINE"), "Unable to retrieve the effect object from the Python runtime");
|
2014-03-01 19:28:57 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the effect from the capsule
|
|
|
|
Effect * effect = reinterpret_cast<Effect *>(PyCapsule_GetPointer(effectCapsule, nullptr));
|
|
|
|
Py_XDECREF(effectCapsule);
|
|
|
|
return effect;
|
2013-11-28 14:29:31 +01:00
|
|
|
}
|