blendVideoTex.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * Copyright (c) 2006 The Zdeno Ash Miklas
  19. *
  20. * This source file is part of VideoTexture library
  21. *
  22. * Contributor(s):
  23. *
  24. * ***** END GPL LICENSE BLOCK *****
  25. */
  26. /** \file gameengine/VideoTexture/blendVideoTex.cpp
  27. * \ingroup bgevideotex
  28. */
  29. #include "EXP_PyObjectPlus.h"
  30. #include "KX_PythonInit.h"
  31. #include <RAS_IPolygonMaterial.h>
  32. //Old API
  33. //#include "TexPlayer.h"
  34. //#include "TexImage.h"
  35. //#include "TexFrameBuff.h"
  36. //#include "TexPlayerGL.h"
  37. #include "ImageBase.h"
  38. #include "VideoBase.h"
  39. #include "FilterBase.h"
  40. #include "Texture.h"
  41. #include "Exception.h"
  42. // access to IMB_BLEND_* constants
  43. extern "C"
  44. {
  45. #include "IMB_imbuf.h"
  46. };
  47. // get material id
  48. static PyObject *getMaterialID (PyObject *self, PyObject *args)
  49. {
  50. // parameters - game object with video texture
  51. PyObject *obj = NULL;
  52. // material name
  53. char * matName;
  54. // get parameters
  55. if (!PyArg_ParseTuple(args, "Os:materialID", &obj, &matName))
  56. return NULL;
  57. // get material id
  58. short matID = getMaterialID(obj, matName);
  59. // if material was not found, report errot
  60. if (matID < 0)
  61. {
  62. PyErr_SetString(PyExc_RuntimeError, "VideoTexture.materialID(ob, string): Object doesn't have material with given name");
  63. return NULL;
  64. }
  65. // return material ID
  66. return Py_BuildValue("h", matID);
  67. }
  68. // get last error description
  69. static PyObject *getLastError (PyObject *self, PyObject *args)
  70. {
  71. return PyUnicode_FromString(Exception::m_lastError.c_str());
  72. }
  73. // set log file
  74. static PyObject *setLogFile (PyObject *self, PyObject *args)
  75. {
  76. // get parameters
  77. if (!PyArg_ParseTuple(args, "s:setLogFile", &Exception::m_logFile))
  78. return Py_BuildValue("i", -1);
  79. // log file was loaded
  80. return Py_BuildValue("i", 0);
  81. }
  82. // image to numpy array
  83. static PyObject *imageToArray(PyObject *self, PyObject *args)
  84. {
  85. // parameter is Image object
  86. PyObject *pyImg;
  87. char *mode = NULL;
  88. if (!PyArg_ParseTuple(args, "O|s:imageToArray", &pyImg, &mode) || !pyImageTypes.in(Py_TYPE(pyImg)))
  89. {
  90. // if object is incorect, report error
  91. PyErr_SetString(PyExc_TypeError, "VideoTexture.imageToArray(image): The value must be a image source object");
  92. return NULL;
  93. }
  94. // get image structure
  95. PyImage * img = reinterpret_cast<PyImage*>(pyImg);
  96. return Image_getImage(img, mode);
  97. }
  98. // metody modulu
  99. static PyMethodDef moduleMethods[] =
  100. {
  101. {"materialID", getMaterialID, METH_VARARGS, "Gets object's Blender Material ID"},
  102. {"getLastError", getLastError, METH_NOARGS, "Gets last error description"},
  103. {"setLogFile", setLogFile, METH_VARARGS, "Sets log file name"},
  104. {"imageToArray", imageToArray, METH_VARARGS, "get buffer from image source, color channels are selectable"},
  105. {NULL} /* Sentinel */
  106. };
  107. #ifdef WITH_FFMPEG
  108. extern PyTypeObject VideoFFmpegType;
  109. extern PyTypeObject ImageFFmpegType;
  110. #endif
  111. #ifdef WITH_GAMEENGINE_DECKLINK
  112. extern PyTypeObject VideoDeckLinkType;
  113. extern PyTypeObject DeckLinkType;
  114. #endif
  115. extern PyTypeObject FilterBlueScreenType;
  116. extern PyTypeObject FilterGrayType;
  117. extern PyTypeObject FilterColorType;
  118. extern PyTypeObject FilterLevelType;
  119. extern PyTypeObject FilterNormalType;
  120. extern PyTypeObject FilterRGB24Type;
  121. extern PyTypeObject FilterRGBA32Type;
  122. extern PyTypeObject FilterBGR24Type;
  123. extern PyTypeObject ImageBuffType;
  124. extern PyTypeObject ImageMixType;
  125. static void registerAllTypes(void)
  126. {
  127. #ifdef WITH_FFMPEG
  128. pyImageTypes.add(&VideoFFmpegType, "VideoFFmpeg");
  129. pyImageTypes.add(&ImageFFmpegType, "ImageFFmpeg");
  130. #endif
  131. #ifdef WITH_GAMEENGINE_DECKLINK
  132. pyImageTypes.add(&VideoDeckLinkType, "VideoDeckLink");
  133. #endif
  134. pyImageTypes.add(&ImageBuffType, "ImageBuff");
  135. pyImageTypes.add(&ImageMixType, "ImageMix");
  136. pyImageTypes.add(&ImageRenderType, "ImageRender");
  137. pyImageTypes.add(&ImageMirrorType, "ImageMirror");
  138. pyImageTypes.add(&ImageViewportType, "ImageViewport");
  139. pyFilterTypes.add(&FilterBlueScreenType, "FilterBlueScreen");
  140. pyFilterTypes.add(&FilterGrayType, "FilterGray");
  141. pyFilterTypes.add(&FilterColorType, "FilterColor");
  142. pyFilterTypes.add(&FilterLevelType, "FilterLevel");
  143. pyFilterTypes.add(&FilterNormalType, "FilterNormal");
  144. pyFilterTypes.add(&FilterRGB24Type, "FilterRGB24");
  145. pyFilterTypes.add(&FilterRGBA32Type, "FilterRGBA32");
  146. pyFilterTypes.add(&FilterBGR24Type, "FilterBGR24");
  147. }
  148. PyDoc_STRVAR(VideoTexture_module_documentation,
  149. "Module that allows to play video files on textures in GameBlender."
  150. );
  151. static struct PyModuleDef VideoTexture_module_def = {
  152. PyModuleDef_HEAD_INIT,
  153. "VideoTexture", /* m_name */
  154. VideoTexture_module_documentation, /* m_doc */
  155. 0, /* m_size */
  156. moduleMethods, /* m_methods */
  157. 0, /* m_reload */
  158. 0, /* m_traverse */
  159. 0, /* m_clear */
  160. 0, /* m_free */
  161. };
  162. PyMODINIT_FUNC initVideoTexturePythonBinding(void)
  163. {
  164. PyObject *m;
  165. // initialize GL extensions
  166. //bgl::InitExtensions(0);
  167. // prepare classes
  168. registerAllTypes();
  169. registerAllExceptions();
  170. if (!pyImageTypes.ready())
  171. return NULL;
  172. if (!pyFilterTypes.ready())
  173. return NULL;
  174. if (PyType_Ready(&TextureType) < 0)
  175. return NULL;
  176. #ifdef WITH_GAMEENGINE_DECKLINK
  177. if (PyType_Ready(&DeckLinkType) < 0)
  178. return NULL;
  179. #endif
  180. m = PyModule_Create(&VideoTexture_module_def);
  181. PyDict_SetItemString(PySys_GetObject("modules"), VideoTexture_module_def.m_name, m);
  182. if (m == NULL)
  183. return NULL;
  184. // initialize classes
  185. pyImageTypes.reg(m);
  186. pyFilterTypes.reg(m);
  187. Py_INCREF(&TextureType);
  188. PyModule_AddObject(m, "Texture", (PyObject *)&TextureType);
  189. #ifdef WITH_GAMEENGINE_DECKLINK
  190. Py_INCREF(&DeckLinkType);
  191. PyModule_AddObject(m, "DeckLink", (PyObject *)&DeckLinkType);
  192. #endif
  193. PyModule_AddIntConstant(m, "SOURCE_ERROR", SourceError);
  194. PyModule_AddIntConstant(m, "SOURCE_EMPTY", SourceEmpty);
  195. PyModule_AddIntConstant(m, "SOURCE_READY", SourceReady);
  196. PyModule_AddIntConstant(m, "SOURCE_PLAYING", SourcePlaying);
  197. PyModule_AddIntConstant(m, "SOURCE_STOPPED", SourceStopped);
  198. PyModule_AddIntConstant(m, "IMB_BLEND_MIX", IMB_BLEND_MIX);
  199. PyModule_AddIntConstant(m, "IMB_BLEND_ADD", IMB_BLEND_ADD);
  200. PyModule_AddIntConstant(m, "IMB_BLEND_SUB", IMB_BLEND_SUB);
  201. PyModule_AddIntConstant(m, "IMB_BLEND_MUL", IMB_BLEND_MUL);
  202. PyModule_AddIntConstant(m, "IMB_BLEND_LIGHTEN", IMB_BLEND_LIGHTEN);
  203. PyModule_AddIntConstant(m, "IMB_BLEND_DARKEN", IMB_BLEND_DARKEN);
  204. PyModule_AddIntConstant(m, "IMB_BLEND_ERASE_ALPHA", IMB_BLEND_ERASE_ALPHA);
  205. PyModule_AddIntConstant(m, "IMB_BLEND_ADD_ALPHA", IMB_BLEND_ADD_ALPHA);
  206. PyModule_AddIntConstant(m, "IMB_BLEND_OVERLAY", IMB_BLEND_OVERLAY);
  207. PyModule_AddIntConstant(m, "IMB_BLEND_HARDLIGHT", IMB_BLEND_HARDLIGHT);
  208. PyModule_AddIntConstant(m, "IMB_BLEND_COLORBURN", IMB_BLEND_COLORBURN);
  209. PyModule_AddIntConstant(m, "IMB_BLEND_LINEARBURN", IMB_BLEND_LINEARBURN);
  210. PyModule_AddIntConstant(m, "IMB_BLEND_COLORDODGE", IMB_BLEND_COLORDODGE);
  211. PyModule_AddIntConstant(m, "IMB_BLEND_SCREEN", IMB_BLEND_SCREEN);
  212. PyModule_AddIntConstant(m, "IMB_BLEND_SOFTLIGHT", IMB_BLEND_SOFTLIGHT);
  213. PyModule_AddIntConstant(m, "IMB_BLEND_PINLIGHT", IMB_BLEND_PINLIGHT);
  214. PyModule_AddIntConstant(m, "IMB_BLEND_VIVIDLIGHT", IMB_BLEND_VIVIDLIGHT);
  215. PyModule_AddIntConstant(m, "IMB_BLEND_LINEARLIGHT", IMB_BLEND_LINEARLIGHT);
  216. PyModule_AddIntConstant(m, "IMB_BLEND_DIFFERENCE", IMB_BLEND_DIFFERENCE);
  217. PyModule_AddIntConstant(m, "IMB_BLEND_EXCLUSION", IMB_BLEND_EXCLUSION);
  218. PyModule_AddIntConstant(m, "IMB_BLEND_HUE", IMB_BLEND_HUE);
  219. PyModule_AddIntConstant(m, "IMB_BLEND_SATURATION", IMB_BLEND_SATURATION);
  220. PyModule_AddIntConstant(m, "IMB_BLEND_LUMINOSITY", IMB_BLEND_LUMINOSITY);
  221. PyModule_AddIntConstant(m, "IMB_BLEND_COLOR", IMB_BLEND_COLOR);
  222. PyModule_AddIntConstant(m, "IMB_BLEND_COPY", IMB_BLEND_COPY);
  223. PyModule_AddIntConstant(m, "IMB_BLEND_COPY_RGB", IMB_BLEND_COPY_RGB);
  224. PyModule_AddIntConstant(m, "IMB_BLEND_COPY_ALPHA", IMB_BLEND_COPY_ALPHA);
  225. // init last error description
  226. Exception::m_lastError = "";
  227. return m;
  228. }
  229. // registration to Image types, put here because of silly linker bug