Пример #1
0
// Takes one argument: a contiguous buffer object.  Modifies it in place.
static PyObject *
_convert_argb2rgba(PyObject *self, PyObject *args)
{
    PyObject *ret = NULL;
    Py_buffer view;

    if (!PyArg_ParseTuple(args, "s*", &view))
        return NULL;
    if (!PyBuffer_IsContiguous(&view, 'A')) {
        PyErr_SetString(PyExc_ValueError, "Argument is not contiguous");
        goto DONE;
    }
    if (view.readonly) {
        PyErr_SetString(PyExc_ValueError, "Argument is not writable");
        goto DONE;
    }
    if (view.len % 4) {
        PyErr_SetString(PyExc_ValueError, "Argument has invalid size");
        goto DONE;
    }

    Py_BEGIN_ALLOW_THREADS
    argb2rgba(view.buf, view.len);
    Py_END_ALLOW_THREADS

    Py_INCREF(Py_None);
    ret = Py_None;

DONE:
    PyBuffer_Release(&view);
    return ret;
}
Пример #2
0
// refresh image
PyObject *Image_refresh (PyImage *self, PyObject *args)
{
	Py_buffer buffer;
	bool done = true;
	char *mode = NULL;
	double ts = -1.0;
	unsigned int format;

	memset(&buffer, 0, sizeof(buffer));
	if (PyArg_ParseTuple(args, "|s*sd:refresh", &buffer, &mode, &ts)) {
		if (buffer.buf) {
			// a target buffer is provided, verify its format
			if (buffer.readonly) {
				PyErr_SetString(PyExc_TypeError, "Buffers passed in argument must be writable");
			}
			else if (!PyBuffer_IsContiguous(&buffer, 'C')) {
				PyErr_SetString(PyExc_TypeError, "Buffers passed in argument must be contiguous in memory");
			}
			else if (((intptr_t)buffer.buf & 3) != 0) {
				PyErr_SetString(PyExc_TypeError, "Buffers passed in argument must be aligned to 4 bytes boundary");
			}
			else {
				// ready to get the image into our buffer
				try {
					if (mode == NULL || !strcmp(mode, "RGBA"))
						format = GL_RGBA;
					else if (!strcmp(mode, "BGRA"))
						format = GL_BGRA;
					else
						THRWEXCP(InvalidImageMode,S_OK);

					done = self->m_image->loadImage((unsigned int *)buffer.buf, buffer.len, format, ts);
				}
				catch (Exception & exp) {
					exp.report();
				}
			}
			PyBuffer_Release(&buffer);
			if (PyErr_Occurred()) {
				return NULL;
			}
		}
	}
	else {
		return NULL;
	}

	self->m_image->refresh();
	if (done)
		Py_RETURN_TRUE;
	Py_RETURN_FALSE;
}
Пример #3
0
/* feed some data to the reader and does one of:
 *  1) returns a tuple (bytes_consumed, messages, error_pending), or
 *  2) throws an exception
 *
 * If a stream error is seen, but some messages were parsed OK,
 * then an exception is not immediately thrown and the parsed
 * messages are returned with error_pending = True. The caller
 * should call feed again (after consuming the given number of
 * bytes) to get the exception.
 *
 * Internal errors (e.g. out of memory) are thrown immediately.
 */
static PyObject *modesreader_feed(modesreader *self, PyObject *args, PyObject *kwds)
{
    Py_buffer buffer;
    PyObject *rv = NULL;
    int max_messages = 0;
    static char *kwlist[] = { "buffer", "max_messages", NULL };

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "y*|i", kwlist, &buffer, &max_messages))
        return NULL;

    if (buffer.itemsize != 1) {
        PyErr_SetString(PyExc_ValueError, "buffer itemsize is not 1");
        goto out;
    }

    if (!PyBuffer_IsContiguous(&buffer, 'C')) {
        PyErr_SetString(PyExc_ValueError, "buffer is not contiguous");
        goto out;
    }

    switch (self->decoder_mode) {
    case DECODER_NONE:
        PyErr_SetString(PyExc_NotImplementedError, "decoder mode is None, no decoder type selected");
        break;

    case DECODER_BEAST:
    case DECODER_RADARCAPE:
    case DECODER_RADARCAPE_EMULATED:
        rv = feed_beast(self, &buffer, max_messages);
        break;

    case DECODER_AVR:
    case DECODER_AVRMLAT:
        rv = feed_avr(self, &buffer, max_messages);
        break;

    case DECODER_SBS:
        rv = feed_sbs(self, &buffer, max_messages);
        break;

    default:
        PyErr_Format(PyExc_AssertionError, "decoder somehow got into illegal mode %d", (int)self->decoder_mode);
        break;
    }

 out:
    PyBuffer_Release(&buffer);
    return rv;
}
Пример #4
0
static PyObject * p_inject (PyObject *self, PyObject *args)
{
  pcap_t * ppcap;
  Py_buffer pbuf;
  if (!PyArg_ParseTuple(args, "ls*", (long int*)&ppcap, &pbuf)) return NULL;
  if (!PyBuffer_IsContiguous(&pbuf, 'C'))
  {
    PyBuffer_Release(&pbuf);
    return PyErr_Format(PyExc_RuntimeError, "Buffer not contiguous");
  }
#ifdef WIN32
  int rv = pcap_sendpacket(ppcap, pbuf.buf, pbuf.len);
  rv = rv ? 0 : len;
#else
  int rv = pcap_inject(ppcap, pbuf.buf, pbuf.len);
#endif
  PyBuffer_Release(&pbuf);
  return Py_BuildValue("i", rv);
}