TCAX 字幕特效制作工具官方论坛 | ASS | TCAS | Python | Aegisub | Lua

 找回密码
 新人加入
查看: 2531|回复: 0

[其他代码] def tcaxPy_Main的C语言源码定义【来源:py.c】 [复制链接]

Rank: 4

发表于 2022-3-31 02:29:29 |显示全部楼层
本帖最后由 Seekladoom 于 2022-3-31 02:29 编辑

def tcaxPy_Main的C语言源码定义
  1. void **tcaxpy_script_func_main(const PY_pTcaxPy pTcaxPy, int iLine, int iText, int nTexts, int start, int end, int timeDiff, int time, int x, int y, int advance, const wchar_t *text) {
  2.     PyObject *pyArgs;          /* params to user tcax py script's tcaxPy_Main function */
  3.     PyObject *pyReturnedBuf;   /* the value returned from tcaxPy_Main function */
  4.     void    **pBufToReturn;    /* value return to tcax module */
  5.     PyObject *pyAssList;
  6.     PyObject *pyTcasList;
  7.     pyArgs = PyTuple_New(11);
  8.     PyTuple_SetItem(pyArgs, 0, PyLong_FromLong(iLine));
  9.     PyTuple_SetItem(pyArgs, 1, PyLong_FromLong(iText));
  10.     PyTuple_SetItem(pyArgs, 2, PyLong_FromLong(nTexts));
  11.     PyTuple_SetItem(pyArgs, 3, PyLong_FromLong(start));
  12.     PyTuple_SetItem(pyArgs, 4, PyLong_FromLong(end));
  13.     PyTuple_SetItem(pyArgs, 5, PyLong_FromLong(timeDiff));
  14.     PyTuple_SetItem(pyArgs, 6, PyLong_FromLong(time));
  15.     PyTuple_SetItem(pyArgs, 7, PyLong_FromLong(x));
  16.     PyTuple_SetItem(pyArgs, 8, PyLong_FromLong(y));
  17.     PyTuple_SetItem(pyArgs, 9, PyLong_FromLong(advance));
  18.     PyTuple_SetItem(pyArgs, 10, PyUnicode_FromUnicode((const Py_UNICODE *)text, wcslen(text)));
  19.     pyReturnedBuf = PyObject_CallObject(pTcaxPy->pyMainFunc, pyArgs);
  20.     Py_CLEAR(pyArgs);
  21.     if (!pyReturnedBuf) {
  22.         PyErr_Print();
  23.         /* PyErr_Clear(); */
  24.         return NULL;
  25.     }
  26.     if (!PyTuple_Check(pyReturnedBuf)) {
  27.         Py_CLEAR(pyReturnedBuf);
  28.         /* fwprintf_s(stderr, L"Error: return value from py script is invalid\n"); */
  29.         return NULL;
  30.     }
  31.     pBufToReturn = (void **)malloc(4 * sizeof(void *));
  32.     pyAssList = PyTuple_GET_ITEM(pyReturnedBuf, 0);
  33.     if (PyList_Check(pyAssList)) {
  34.         int i, count, size, offset;
  35.         wchar_t *assBuf;
  36.         wchar_t **pAssLine;
  37.         int     *assLineSize;
  38.         count = PyList_GET_SIZE(pyAssList);
  39.         if (0 == count) {
  40.             pBufToReturn[0] = (void *)0;
  41.             pBufToReturn[1] = NULL;
  42.         } else {
  43.             assLineSize = (int *)malloc(count * sizeof(int));
  44.             pAssLine = (wchar_t **)malloc(count * sizeof(wchar_t *));
  45.             size = 0;
  46.             for (i = 0; i < count; i ++) {
  47.                 pAssLine[i] = (wchar_t *)PyUnicode_AS_UNICODE(PyList_GET_ITEM(pyAssList, i));
  48.                 assLineSize[i] = wcslen(pAssLine[i]);
  49.                 size += assLineSize[i];
  50.             }
  51.             assBuf = (wchar_t *)malloc((size + 1) * sizeof(wchar_t));
  52.             offset = 0;
  53.             for (i = 0; i < count; i ++) {
  54.                 memcpy(assBuf + offset, pAssLine[i], assLineSize[i] * sizeof(wchar_t));
  55.                 offset += assLineSize[i];
  56.             }
  57.             assBuf[size] = L'\0';
  58.             free(pAssLine);
  59.             free(assLineSize);
  60.             pBufToReturn[0] = (void *)size;
  61.             pBufToReturn[1] = (void *)assBuf;
  62.         }
  63.     } else {
  64.         pBufToReturn[0] = (void *)0;
  65.         pBufToReturn[1] = NULL;
  66.     }
  67.     pyTcasList = PyTuple_GET_ITEM(pyReturnedBuf, 1);
  68.     if (PyList_Check(pyTcasList)) {
  69.         int i, count;
  70.         PyObject *pyTcasItem;
  71.         short x, y;
  72.         unsigned long RGB;
  73.         unsigned char alpha;
  74.         unsigned long *tcasBuf;
  75.         count = PyList_GET_SIZE(pyTcasList);
  76.         if (0 == count) {
  77.             pBufToReturn[2] = (void *)0;
  78.             pBufToReturn[3] = NULL;
  79.         }
  80.         else {
  81.             /* tcas buffer from user tcax py script: (Start, End, Layer_Type_Pair, PosX, PosY, RGB, Alpha) */
  82.             /* a raw tcas chunk defined in TCAS file format specification: (Start, End, Layer_Type_Pair, Pos, RGBA) */
  83.             tcasBuf = (unsigned long *)malloc(count * 5 * sizeof(unsigned long));
  84.             for (i = 0; i < count; i ++) {
  85.                 pyTcasItem = PyList_GET_ITEM(pyTcasList, i);
  86.                 x = (short)PyLong_AsLong(PyTuple_GET_ITEM(pyTcasItem, 3));
  87.                 y = (short)PyLong_AsLong(PyTuple_GET_ITEM(pyTcasItem, 4));
  88.                 RGB = (unsigned long)PyLong_AsUnsignedLong(PyTuple_GET_ITEM(pyTcasItem, 5));
  89.                 alpha = (unsigned char)PyLong_AsLong(PyTuple_GET_ITEM(pyTcasItem, 6));
  90.                 tcasBuf[5 * i + 0] = (long)PyLong_AsLong(PyTuple_GET_ITEM(pyTcasItem, 0));    /* Start */
  91.                 tcasBuf[5 * i + 1] = (long)PyLong_AsLong(PyTuple_GET_ITEM(pyTcasItem, 1));    /* End */
  92.                 tcasBuf[5 * i + 2] = (unsigned long)PyLong_AsUnsignedLong(PyTuple_GET_ITEM(pyTcasItem, 2));    /* Layer_Type_Pair */
  93.                 tcasBuf[5 * i + 3] = (x | ((unsigned long)y << 16));
  94.                 tcasBuf[5 * i + 4] = (RGB | ((unsigned long)alpha << 24));
  95.             }
  96.             pBufToReturn[2] = (void *)count;
  97.             pBufToReturn[3] = tcasBuf;
  98.         }
  99.     } else {
  100.         pBufToReturn[2] = (void *)0;
  101.         pBufToReturn[3] = NULL;
  102.     }
  103.     Py_CLEAR(pyReturnedBuf);
  104.     return pBufToReturn;
  105. }
复制代码
您需要登录后才可以回帖 登录 | 新人加入

GitHub|TCAX 主页

GMT+8, 2024-3-28 22:51

Powered by Discuz! X2

© 2001-2011 Comsenz Inc.

回顶部
RealH