diff options
author | Barry Warsaw <barry@python.org> | 1996-12-13 01:24:29 (GMT) |
---|---|---|
committer | Barry Warsaw <barry@python.org> | 1996-12-13 01:24:29 (GMT) |
commit | f630f6b93deeca767404d936c548c16c9fb8d561 (patch) | |
tree | fbc0b0b93b05002bd9a4116ac2fe9e17007e221c /Modules/svmodule.c | |
parent | e3c0170ba0fe56e7ebde3f51b909bc752e77986d (diff) | |
download | cpython-f630f6b93deeca767404d936c548c16c9fb8d561.zip cpython-f630f6b93deeca767404d936c548c16c9fb8d561.tar.gz cpython-f630f6b93deeca767404d936c548c16c9fb8d561.tar.bz2 |
Renamed, and scrutinized for missed potential error conditions.
Alas, I don't have an Indigo, so I could not even compile this.
Diffstat (limited to 'Modules/svmodule.c')
-rw-r--r-- | Modules/svmodule.c | 752 |
1 files changed, 411 insertions, 341 deletions
diff --git a/Modules/svmodule.c b/Modules/svmodule.c index 3cf97f7..f4e66df 100644 --- a/Modules/svmodule.c +++ b/Modules/svmodule.c @@ -33,57 +33,63 @@ PERFORMANCE OF THIS SOFTWARE. #include <sys/time.h> #include <svideo.h> -#include "allobjects.h" -#include "import.h" -#include "modsupport.h" +#include "Python.h" #include "compile.h" -#include "ceval.h" #include "yuv.h" /* for YUV conversion functions */ typedef struct { - OB_HEAD + PyObject_HEAD SV_nodeP ob_svideo; svCaptureInfo ob_info; } svobject; typedef struct { - OB_HEAD + PyObject_HEAD void *ob_capture; int ob_mustunlock; svCaptureInfo ob_info; svobject *ob_svideo; } captureobject; -static object *SvError; /* exception sv.error */ +static PyObject *SvError; /* exception sv.error */ -static object *newcaptureobject PROTO((svobject *, void *, int)); +static PyObject *newcaptureobject Py_PROTO((svobject *, void *, int)); /* Set a SV-specific error from svideo_errno and return NULL */ -static object * +static PyObject * sv_error() { - err_setstr(SvError, svStrerror(svideo_errno)); + PyErr_SetString(SvError, svStrerror(svideo_errno)); return NULL; } -static object * +static PyObject * svc_conversion(self, args, function, factor) captureobject *self; - object *args; + PyObject *args; void (*function)(); float factor; { - object *output; + PyObject *output; int invert; + char* outstr; - if (!getargs(args, "i", &invert)) + if (!PyArg_Parse(args, "i", &invert)) return NULL; - output = newsizedstringobject(NULL, (int) (self->ob_info.width * self->ob_info.height * factor)); - if (output == NULL) + if (!(output = PyString_FromStringAndSize( + NULL, + (int)(self->ob_info.width * self->ob_info.height * factor)))) + { return NULL; + } + if (!(outstr = PyString_AsString(output))) { + Py_DECREF(output); + return NULL; + } - (*function)((boolean) invert, self->ob_capture, getstringvalue(output), + (*function)((boolean)invert, self->ob_capture, + outstr, self->ob_info.width, self->ob_info.height); return output; @@ -93,114 +99,126 @@ svc_conversion(self, args, function, factor) * 3 functions to convert from Starter Video YUV 4:1:1 format to * Compression Library 4:2:2 Duplicate Chroma format. */ -static object * +static PyObject * svc_YUVtoYUV422DC(self, args) captureobject *self; - object *args; + PyObject *args; { if (self->ob_info.format != SV_YUV411_FRAMES) { - err_setstr(SvError, "data has bad format"); + PyErr_SetString(SvError, "data has bad format"); return NULL; } return svc_conversion(self, args, yuv_sv411_to_cl422dc, 2.0); } -static object * +static PyObject * svc_YUVtoYUV422DC_quarter(self, args) captureobject *self; - object *args; + PyObject *args; { if (self->ob_info.format != SV_YUV411_FRAMES) { - err_setstr(SvError, "data has bad format"); + PyErr_SetString(SvError, "data has bad format"); return NULL; } - return svc_conversion(self, args, yuv_sv411_to_cl422dc_quartersize, 0.5); + return svc_conversion(self, args, + yuv_sv411_to_cl422dc_quartersize, 0.5); } -static object * +static PyObject * svc_YUVtoYUV422DC_sixteenth(self, args) captureobject *self; - object *args; + PyObject *args; { if (self->ob_info.format != SV_YUV411_FRAMES) { - err_setstr(SvError, "data has bad format"); + PyErr_SetString(SvError, "data has bad format"); return NULL; } - return svc_conversion(self, args, yuv_sv411_to_cl422dc_sixteenthsize, 0.125); + return svc_conversion(self, args, + yuv_sv411_to_cl422dc_sixteenthsize, 0.125); } -static object * +static PyObject * svc_YUVtoRGB(self, args) captureobject *self; - object *args; + PyObject *args; { switch (self->ob_info.format) { case SV_YUV411_FRAMES: case SV_YUV411_FRAMES_AND_BLANKING_BUFFER: break; default: - err_setstr(SvError, "data had bad format"); + PyErr_SetString(SvError, "data had bad format"); return NULL; } return svc_conversion(self, args, svYUVtoRGB, (float) sizeof(long)); } -static object * +static PyObject * svc_RGB8toRGB32(self, args) captureobject *self; - object *args; + PyObject *args; { if (self->ob_info.format != SV_RGB8_FRAMES) { - err_setstr(SvError, "data has bad format"); + PyErr_SetString(SvError, "data has bad format"); return NULL; } return svc_conversion(self, args, svRGB8toRGB32, (float) sizeof(long)); } -static object * +static PyObject * svc_InterleaveFields(self, args) captureobject *self; - object *args; + PyObject *args; { if (self->ob_info.format != SV_RGB8_FRAMES) { - err_setstr(SvError, "data has bad format"); + PyErr_SetString(SvError, "data has bad format"); return NULL; } return svc_conversion(self, args, svInterleaveFields, 1.0); } -static object * +static PyObject * svc_GetFields(self, args) captureobject *self; - object *args; + PyObject *args; { - object *f1, *f2, *ret; + PyObject *f1 = NULL; + PyObject *f2 = NULL; + PyObject *ret = NULL; int fieldsize; + char* obcapture; if (self->ob_info.format != SV_RGB8_FRAMES) { - err_setstr(SvError, "data has bad format"); + PyErr_SetString(SvError, "data has bad format"); return NULL; } fieldsize = self->ob_info.width * self->ob_info.height / 2; - f1 = newsizedstringobject((char *) self->ob_capture, fieldsize); - f2 = newsizedstringobject((char *) self->ob_capture + fieldsize, fieldsize); - ret = mkvalue("(OO)", f1, f2); - XDECREF(f1); - XDECREF(f2); + obcapture = (char*)self->ob_capture; + + if (!(f1 = PyString_FromStringAndSize(obcapture, fieldsize))) + goto finally; + if (!(f2 = PyString_FromStringAndSize(obcapture + fieldsize, + fieldsize))) + goto finally; + ret = Py_BuildValue("(OO)", f1, f2); + + finally: + Py_XDECREF(f1); + Py_XDECREF(f2); return ret; } -static object * +static PyObject * svc_UnlockCaptureData(self, args) captureobject *self; - object *args; + PyObject *args; { - if (!getnoarg(args)) + if (!PyArg_Parse(args, "")) return NULL; if (!self->ob_mustunlock) { - err_setstr(SvError, "buffer should not be unlocked"); + PyErr_SetString(SvError, "buffer should not be unlocked"); return NULL; } @@ -209,93 +227,99 @@ svc_UnlockCaptureData(self, args) self->ob_mustunlock = 0; - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } #ifdef USE_GL #include <gl.h> -static object * +static PyObject * svc_lrectwrite(self, args) captureobject *self; - object *args; + PyObject *args; { Screencoord x1, x2, y1, y2; - if (!getargs(args, "(hhhh)", &x1, &x2, &y1, &y2)) + if (!PyArg_Parse(args, "(hhhh)", &x1, &x2, &y1, &y2)) return NULL; lrectwrite(x1, x2, y1, y2, (unsigned long *) self->ob_capture); - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } #endif -static object * +static PyObject * svc_writefile(self, args) captureobject *self; - object *args; + PyObject *args; { - object *file; + PyObject *file; int size; + FILE* fp; - if (!getargs(args, "O", &file)) + if (!PyArg_Parse(args, "O", &file)) return NULL; - if (!is_fileobject(file)) { - err_setstr(SvError, "not a file object"); + if (!PyFile_Check(file)) { + PyErr_SetString(SvError, "not a file object"); return NULL; } + if (!(fp = PyFile_AsFile(file))) + return NULL; + size = self->ob_info.width * self->ob_info.height; - if (fwrite(self->ob_capture, sizeof(long), size, getfilefile(file)) != size) { - err_setstr(SvError, "writing failed"); + if (fwrite(self->ob_capture, sizeof(long), size, fp) != size) { + PyErr_SetString(SvError, "writing failed"); return NULL; } - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } -static object * +static PyObject * svc_FindVisibleRegion(self, args) captureobject *self; - object *args; + PyObject *args; { void *visible; int width; - if (!getnoarg(args)) + if (!PyArg_Parse(args, "")) return NULL; - if (svFindVisibleRegion(self->ob_svideo->ob_svideo, self->ob_capture, &visible, self->ob_info.width)) + if (svFindVisibleRegion(self->ob_svideo->ob_svideo, + self->ob_capture, &visible, + self->ob_info.width)) return sv_error(); if (visible == NULL) { - err_setstr(SvError, "data in wrong format"); + PyErr_SetString(SvError, "data in wrong format"); return NULL; } return newcaptureobject(self->ob_svideo, visible, 0); } -static struct methodlist capture_methods[] = { - {"YUVtoRGB", (method)svc_YUVtoRGB}, - {"RGB8toRGB32", (method)svc_RGB8toRGB32}, - {"InterleaveFields", (method)svc_InterleaveFields}, - {"UnlockCaptureData", (method)svc_UnlockCaptureData}, - {"FindVisibleRegion", (method)svc_FindVisibleRegion}, - {"GetFields", (method)svc_GetFields}, - {"YUVtoYUV422DC", (method)svc_YUVtoYUV422DC}, - {"YUVtoYUV422DC_quarter",(method)svc_YUVtoYUV422DC_quarter}, - {"YUVtoYUV422DC_sixteenth",(method)svc_YUVtoYUV422DC_sixteenth}, +static PyMethodDef capture_methods[] = { + {"YUVtoRGB", (PyCFunction)svc_YUVtoRGB}, + {"RGB8toRGB32", (PyCFunction)svc_RGB8toRGB32}, + {"InterleaveFields", (PyCFunction)svc_InterleaveFields}, + {"UnlockCaptureData", (PyCFunction)svc_UnlockCaptureData}, + {"FindVisibleRegion", (PyCFunction)svc_FindVisibleRegion}, + {"GetFields", (PyCFunction)svc_GetFields}, + {"YUVtoYUV422DC", (PyCFunction)svc_YUVtoYUV422DC}, + {"YUVtoYUV422DC_quarter",(PyCFunction)svc_YUVtoYUV422DC_quarter}, + {"YUVtoYUV422DC_sixteenth",(PyCFunction)svc_YUVtoYUV422DC_sixteenth}, #ifdef USE_GL - {"lrectwrite", (method)svc_lrectwrite}, + {"lrectwrite", (PyCFunction)svc_lrectwrite}, #endif - {"writefile", (method)svc_writefile}, + {"writefile", (PyCFunction)svc_writefile}, {NULL, NULL} /* sentinel */ }; @@ -305,24 +329,25 @@ capture_dealloc(self) { if (self->ob_capture != NULL) { if (self->ob_mustunlock) - (void) svUnlockCaptureData(self->ob_svideo->ob_svideo, self->ob_capture); + (void)svUnlockCaptureData(self->ob_svideo->ob_svideo, + self->ob_capture); self->ob_capture = NULL; - DECREF(self->ob_svideo); + Py_DECREF(self->ob_svideo); self->ob_svideo = NULL; } - DEL(self); + PyMem_DEL(self); } -static object * +static PyObject * capture_getattr(self, name) svobject *self; char *name; { - return findmethod(capture_methods, (object *)self, name); + return Py_FindMethod(capture_methods, (PyObject *)self, name); } -typeobject Capturetype = { - OB_HEAD_INIT(&Typetype) +PyTypeObject Capturetype = { + PyObject_HEAD_INIT(&PyType_Type) 0, /*ob_size*/ "capture", /*tp_name*/ sizeof(captureobject), /*tp_size*/ @@ -336,7 +361,7 @@ typeobject Capturetype = { 0, /*tp_repr*/ }; -static object * +static PyObject * newcaptureobject(self, ptr, mustunlock) svobject *self; void *ptr; @@ -344,233 +369,236 @@ newcaptureobject(self, ptr, mustunlock) { captureobject *p; - p = NEWOBJ(captureobject, &Capturetype); + p = PyObject_NEW(captureobject, &Capturetype); if (p == NULL) return NULL; p->ob_svideo = self; - INCREF(self); + Py_INCREF(self); p->ob_capture = ptr; p->ob_mustunlock = mustunlock; p->ob_info = self->ob_info; - return (object *) p; + return (PyObject *) p; } -static object * +static PyObject * sv_GetCaptureData(self, args) svobject *self; - object *args; + PyObject *args; { void *ptr; long fieldID; - object *res, *c; + PyObject *res, *c; - if (!getnoarg(args)) + if (!PyArg_Parse(args, "")) return NULL; if (svGetCaptureData(self->ob_svideo, &ptr, &fieldID)) return sv_error(); if (ptr == NULL) { - err_setstr(SvError, "no data available"); + PyErr_SetString(SvError, "no data available"); return NULL; } c = newcaptureobject(self, ptr, 1); if (c == NULL) return NULL; - res = mkvalue("(Oi)", c, fieldID); - DECREF(c); + res = Py_BuildValue("(Oi)", c, fieldID); + Py_DECREF(c); return res; } -static object * +static PyObject * sv_BindGLWindow(self, args) svobject *self; - object *args; + PyObject *args; { long wid; int mode; - if (!getargs(args, "(ii)", &wid, &mode)) + if (!PyArg_Parse(args, "(ii)", &wid, &mode)) return NULL; if (svBindGLWindow(self->ob_svideo, wid, mode)) return sv_error(); - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } -static object * +static PyObject * sv_EndContinuousCapture(self, args) svobject *self; - object *args; + PyObject *args; { - if (!getnoarg(args)) + if (!PyArg_Parse(args, "")) return NULL; if (svEndContinuousCapture(self->ob_svideo)) return sv_error(); - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } -static object * +static PyObject * sv_IsVideoDisplayed(self, args) svobject *self; - object *args; + PyObject *args; { int v; - if (!getnoarg(args)) + if (!PyArg_Parse(args, "")) return NULL; v = svIsVideoDisplayed(self->ob_svideo); if (v == -1) return sv_error(); - return newintobject((long) v); + return PyInt_FromLong((long) v); } -static object * +static PyObject * sv_OutputOffset(self, args) svobject *self; - object *args; + PyObject *args; { int x_offset; int y_offset; - if (!getargs(args, "(ii)", &x_offset, &y_offset)) + if (!PyArg_Parse(args, "(ii)", &x_offset, &y_offset)) return NULL; if (svOutputOffset(self->ob_svideo, x_offset, y_offset)) return sv_error(); - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } -static object * +static PyObject * sv_PutFrame(self, args) svobject *self; - object *args; + PyObject *args; { char *buffer; - if (!getargs(args, "s", &buffer)) + if (!PyArg_Parse(args, "s", &buffer)) return NULL; if (svPutFrame(self->ob_svideo, buffer)) return sv_error(); - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } -static object * +static PyObject * sv_QuerySize(self, args) svobject *self; - object *args; + PyObject *args; { int w; int h; int rw; int rh; - if (!getargs(args, "(ii)", &w, &h)) + if (!PyArg_Parse(args, "(ii)", &w, &h)) return NULL; if (svQuerySize(self->ob_svideo, w, h, &rw, &rh)) return sv_error(); - return mkvalue("(ii)", (long) rw, (long) rh); + return Py_BuildValue("(ii)", (long) rw, (long) rh); } -static object * +static PyObject * sv_SetSize(self, args) svobject *self; - object *args; + PyObject *args; { int w; int h; - if (!getargs(args, "(ii)", &w, &h)) + if (!PyArg_Parse(args, "(ii)", &w, &h)) return NULL; if (svSetSize(self->ob_svideo, w, h)) return sv_error(); - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } -static object * +static PyObject * sv_SetStdDefaults(self, args) svobject *self; - object *args; + PyObject *args; { - if (!getnoarg(args)) + if (!PyArg_Parse(args, "")) return NULL; if (svSetStdDefaults(self->ob_svideo)) return sv_error(); - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } -static object * +static PyObject * sv_UseExclusive(self, args) svobject *self; - object *args; + PyObject *args; { boolean onoff; int mode; - if (!getargs(args, "(ii)", &onoff, &mode)) + if (!PyArg_Parse(args, "(ii)", &onoff, &mode)) return NULL; if (svUseExclusive(self->ob_svideo, onoff, mode)) return sv_error(); - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } -static object * +static PyObject * sv_WindowOffset(self, args) svobject *self; - object *args; + PyObject *args; { int x_offset; int y_offset; - if (!getargs(args, "(ii)", &x_offset, &y_offset)) + if (!PyArg_Parse(args, "(ii)", &x_offset, &y_offset)) return NULL; if (svWindowOffset(self->ob_svideo, x_offset, y_offset)) return sv_error(); - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } -static object * +static PyObject * sv_CaptureBurst(self, args) svobject *self; - object *args; + PyObject *args; { int bytes, i; svCaptureInfo info; void *bitvector = NULL; - object *videodata, *bitvecobj, *res; - static object *evenitem, *odditem; - - if (!getargs(args, "(iiiii)", &info.format, &info.width, &info.height, - &info.size, &info.samplingrate)) + PyObject *videodata = NULL; + PyObject *bitvecobj = NULL; + PyObject* *res = NULL; + static PyObject *evenitem, *odditem; + + if (!PyArg_Parse(args, "(iiiii)", &info.format, + &info.width, &info.height, + &info.size, &info.samplingrate)) return NULL; switch (info.format) { @@ -580,89 +608,88 @@ sv_CaptureBurst(self, args) case SV_YUV411_FRAMES_AND_BLANKING_BUFFER: break; default: - err_setstr(SvError, "illegal format specified"); + PyErr_SetString(SvError, "illegal format specified"); return NULL; } if (svQueryCaptureBufferSize(self->ob_svideo, &info, &bytes)) { - if (bitvector) - free(bitvector); - return sv_error(); + res = sv_error(); + goto finally; } - videodata = newsizedstringobject(NULL, bytes); - if (videodata == NULL) { - if (bitvector) - free(bitvector); - return NULL; - } + if (!(videodata = PyString_FromStringAndSize(NULL, bytes))) + goto finally; /* XXX -- need to do something about the bitvector */ - if (svCaptureBurst(self->ob_svideo, &info, getstringvalue(videodata), - bitvector)) { - if (bitvector) - free(bitvector); - DECREF(videodata); - return sv_error(); + { + char* str = PyString_AsString(videodata); + if (!str) + goto finally; + + if (svCaptureBurst(self->ob_svideo, &info, str, bitvector)) { + res = sv_error(); + goto finally; + } } if (bitvector) { if (evenitem == NULL) { - evenitem = newintobject(0); - if (evenitem == NULL) { - free(bitvector); - DECREF(videodata); - return NULL; - } + if (!(evenitem = PyInt_FromLong(0))) + goto finally; } if (odditem == NULL) { - odditem = newintobject(1); - if (odditem == NULL) { - free(bitvector); - DECREF(videodata); - return NULL; - } - } - bitvecobj = newtupleobject(2 * info.size); - if (bitvecobj == NULL) { - free(bitvecobj); - DECREF(videodata); - return NULL; + if (!(odditem = PyInt_FromLong(1))) + goto finally; } + if (!(bitvecobj = PyTuple_New(2 * info.size))) + goto finally; + for (i = 0; i < 2 * info.size; i++) { + int sts; + if (SV_GET_FIELD(bitvector, i) == SV_EVEN_FIELD) { - INCREF(evenitem); - settupleitem(bitvecobj, i, evenitem); + Py_INCREF(evenitem); + sts = PyTuple_SetItem(bitvecobj, i, evenitem); } else { - INCREF(odditem); - settupleitem(bitvecobj, i, odditem); + Py_INCREF(odditem); + sts = PyTuple_SetItem(bitvecobj, i, odditem); } + if (sts < 0) + goto finally; } - free(bitvector); } else { - bitvecobj = None; - INCREF(None); + bitvecobj = Py_None; + Py_INCREF(Py_None); } - res = mkvalue("((iiiii)OO)", info.format, info.width, info.height, - info.size, info.samplingrate, videodata, bitvecobj); - DECREF(videodata); - DECREF(bitvecobj); + res = Py_BuildValue("((iiiii)OO)", info.format, + info.width, info.height, + info.size, info.samplingrate, + videodata, bitvecobj); + + finally: + if (bitvector) + free(bitvector); + + Py_XDECREF(videodata); + Py_XDECREF(bitvecobj); return res; } -static object * +static PyObject * sv_CaptureOneFrame(self, args) svobject *self; - object *args; + PyObject *args; { svCaptureInfo info; int format, width, height; int bytes; - object *videodata, *res; + PyObject *videodata = NULL; + PyObject *res = NULL; - if (!getargs(args, "(iii)", &format, &width, &height)) + if (!PyArg_Parse(args, "(iii)", &format, &width, &height)) return NULL; + info.format = format; info.width = width; info.height = height; @@ -670,29 +697,37 @@ sv_CaptureOneFrame(self, args) info.samplingrate = 0; if (svQueryCaptureBufferSize(self->ob_svideo, &info, &bytes)) return sv_error(); - videodata = newsizedstringobject(NULL, bytes); - if (videodata == NULL) + + if (!(videodata = PyString_FromStringAndSize(NULL, bytes))) return NULL; - if (svCaptureOneFrame(self->ob_svideo, format, &width, &height, - getstringvalue(videodata))) { - DECREF(videodata); - return sv_error(); + + { + char* str = PyString_AsString(videodata); + if (!str) + goto finally; + + if (svCaptureOneFrame(self->ob_svideo, format, &width, &height, str)) { + res = sv_error(); + goto finally; } - res = mkvalue("(iiO)", width, height, videodata); - DECREF(videodata); + res = Py_BuildValue("(iiO)", width, height, videodata); + + finally: + Py_XDECREF(videodata); return res; } -static object * +static PyObject * sv_InitContinuousCapture(self, args) svobject *self; - object *args; + PyObject *args; { svCaptureInfo info; - if (!getargs(args, "(iiiii)", &info.format, &info.width, &info.height, - &info.size, &info.samplingrate)) + if (!PyArg_Parse(args, "(iiiii)", &info.format, + &info.width, &info.height, + &info.size, &info.samplingrate)) return NULL; if (svInitContinuousCapture(self->ob_svideo, &info)) @@ -700,215 +735,250 @@ sv_InitContinuousCapture(self, args) self->ob_info = info; - return mkvalue("(iiiii)", info.format, info.width, info.height, - info.size, info.samplingrate); + return Py_BuildValue("(iiiii)", info.format, info.width, info.height, + info.size, info.samplingrate); } -static object * +static PyObject * sv_LoadMap(self, args) svobject *self; - object *args; + PyObject *args; { - object *rgb, *v, *cell; - rgb_tuple *mapp; + PyObject *rgb; + PyObject *res = NULL; + rgb_tuple *mapp = NULL; int maptype; - int i, j; /* indices */ + int i, j; /* indices */ - if (!getargs(args, "(iO)", &maptype, &rgb)) + if (!PyArg_Parse(args, "(iO)", &maptype, &rgb)) return NULL; - if (!is_listobject(rgb) || getlistsize(rgb) != 256) { - err_badarg(); + + if (!PyList_Check(rgb) || PyList_Size(rgb) != 256) { + PyErr_BadArgument(); return NULL; } - mapp = NEW(rgb_tuple, 256); - if (mapp == NULL) - return err_nomem(); + + if (!(mapp = PyMem_NEW(rgb_tuple, 256))) + return PyErr_NoMemory(); + for (i = 0; i < 256; i++) { - v = getlistitem(rgb, i); - if (!is_tupleobject(v) || gettuplesize(v) != 3) { - DEL(mapp); - err_badarg(); - return NULL; + PyObject* v = PyList_GetItem(rgb, i); + if (!v) + goto finally; + + if (!PyTuple_Check(v) || PyTuple_Size(v) != 3) { + PyErr_BadArgument(); + goto finally; } for (j = 0; j < 3; j++) { - cell = gettupleitem(v, j); - if (!is_intobject(cell)) { - DEL(mapp); - err_badarg(); - return NULL; + PyObject* cell = PyTuple_GetItem(v, j); + if (!cell) + goto finally; + + if (!PyInt_Check(cell)) { + PyErr_BadArgument(); + goto finally; } switch (j) { - case 0: mapp[i].red = getintvalue(cell); break; - case 1: mapp[i].blue = getintvalue(cell); break; - case 2: mapp[i].green = getintvalue(cell); break; + case 0: mapp[i].red = PyInt_AsLong(cell); break; + case 1: mapp[i].blue = PyInt_AsLong(cell); break; + case 2: mapp[i].green = PyInt_AsLong(cell); break; } + if (PyErr_Occurred()) + goto finally; } } if (svLoadMap(self->ob_svideo, maptype, mapp)) { - DEL(mapp); - return sv_error(); + res = sv_error(); + goto finally; } - DEL(mapp); + Py_INCREF(Py_None); + res = Py_None; - INCREF(None); - return None; + finally: + PyMem_DEL(mapp); + return res; } -static object * +static PyObject * sv_CloseVideo(self, args) svobject *self; - object *args; + PyObject *args; { - if (!getnoarg(args)) + if (!PyArg_Parse(args, "")) return NULL; if (svCloseVideo(self->ob_svideo)) return sv_error(); - self->ob_svideo = NULL; - INCREF(None); - return None; + self->ob_svideo = NULL; + Py_INCREF(Py_None); + return Py_None; } -static object * +static PyObject * doParams(self, args, func, modified) svobject *self; - object *args; + PyObject *args; int (*func)(SV_nodeP, long *, int); int modified; { - object *list, *v; - long *PVbuffer; + PyObject *list; + PyObject *res = NULL; + long *PVbuffer = NULL; long length; int i; - if (!getargs(args, "O", &list)) + if (!PyArg_Parse(args, "O", &list)) return NULL; - if (!is_listobject(list)) { - err_badarg(); + + if (!PyList_Check(list)) { + PyErr_BadArgument(); return NULL; } - length = getlistsize(list); - PVbuffer = NEW(long, length); + + if ((length = PyList_Size(list)) < 0) + return NULL; + + PVbuffer = PyMem_NEW(long, length); if (PVbuffer == NULL) - return err_nomem(); + return PyErr_NoMemory(); + for (i = 0; i < length; i++) { - v = getlistitem(list, i); - if (!is_intobject(v)) { - DEL(PVbuffer); - err_badarg(); - return NULL; + PyObject *v = PyList_GetItem(list, i); + if (!v) + goto finally; + + if (!PyInt_Check(v)) { + PyErr_BadArgument(); + goto finally; } - PVbuffer[i] = getintvalue(v); + PVbuffer[i] = PyInt_AsLong(v); + /* can't just test the return value, because what if the + value was -1?! + */ + if (PVbuffer[i] == -1 && PyErr_Occurred()) + goto finally; } if ((*func)(self->ob_svideo, PVbuffer, length)) { - DEL(PVbuffer); - return sv_error(); + res = sv_error(); + goto finally; } if (modified) { - for (i = 0; i < length; i++) - setlistitem(list, i, newintobject(PVbuffer[i])); + for (i = 0; i < length; i++) { + PyObject* v = PyInt_FromLong(PVbuffer[i]); + if (!v || PyList_SetItem(list, i, v) < 0) + goto finally; + } } - DEL(PVbuffer); + Py_INCREF(Py_None); + res = Py_None; - INCREF(None); - return None; + finally: + PyMem_DEL(PVbuffer); + return res; } -static object * +static PyObject * sv_GetParam(self, args) - object *self, *args; + PyObject *self, *args; { return doParams(self, args, svGetParam, 1); } -static object * +static PyObject * sv_GetParamRange(self, args) - object *self, *args; + PyObject *self, *args; { return doParams(self, args, svGetParamRange, 1); } -static object * +static PyObject * sv_SetParam(self, args) - object *self, *args; + PyObject *self, *args; { return doParams(self, args, svSetParam, 0); } -static struct methodlist svideo_methods[] = { - {"BindGLWindow", (method)sv_BindGLWindow}, - {"EndContinuousCapture",(method)sv_EndContinuousCapture}, - {"IsVideoDisplayed", (method)sv_IsVideoDisplayed}, - {"OutputOffset", (method)sv_OutputOffset}, - {"PutFrame", (method)sv_PutFrame}, - {"QuerySize", (method)sv_QuerySize}, - {"SetSize", (method)sv_SetSize}, - {"SetStdDefaults", (method)sv_SetStdDefaults}, - {"UseExclusive", (method)sv_UseExclusive}, - {"WindowOffset", (method)sv_WindowOffset}, - {"InitContinuousCapture",(method)sv_InitContinuousCapture}, - {"CaptureBurst", (method)sv_CaptureBurst}, - {"CaptureOneFrame", (method)sv_CaptureOneFrame}, - {"GetCaptureData", (method)sv_GetCaptureData}, - {"CloseVideo", (method)sv_CloseVideo}, - {"LoadMap", (method)sv_LoadMap}, - {"GetParam", (method)sv_GetParam}, - {"GetParamRange", (method)sv_GetParamRange}, - {"SetParam", (method)sv_SetParam}, +static PyMethodDef svideo_methods[] = { + {"BindGLWindow", (PyCFunction)sv_BindGLWindow}, + {"EndContinuousCapture",(PyCFunction)sv_EndContinuousCapture}, + {"IsVideoDisplayed", (PyCFunction)sv_IsVideoDisplayed}, + {"OutputOffset", (PyCFunction)sv_OutputOffset}, + {"PutFrame", (PyCFunction)sv_PutFrame}, + {"QuerySize", (PyCFunction)sv_QuerySize}, + {"SetSize", (PyCFunction)sv_SetSize}, + {"SetStdDefaults", (PyCFunction)sv_SetStdDefaults}, + {"UseExclusive", (PyCFunction)sv_UseExclusive}, + {"WindowOffset", (PyCFunction)sv_WindowOffset}, + {"InitContinuousCapture",(PyCFunction)sv_InitContinuousCapture}, + {"CaptureBurst", (PyCFunction)sv_CaptureBurst}, + {"CaptureOneFrame", (PyCFunction)sv_CaptureOneFrame}, + {"GetCaptureData", (PyCFunction)sv_GetCaptureData}, + {"CloseVideo", (PyCFunction)sv_CloseVideo}, + {"LoadMap", (PyCFunction)sv_LoadMap}, + {"GetParam", (PyCFunction)sv_GetParam}, + {"GetParamRange", (PyCFunction)sv_GetParamRange}, + {"SetParam", (PyCFunction)sv_SetParam}, {NULL, NULL} /* sentinel */ }; -static object * +static PyObject * sv_conversion(self, args, function, inputfactor, factor) - object *self, *args; + PyObject *self, *args; void (*function)(); int inputfactor; float factor; { int invert, width, height, inputlength; - char *input; - object *output; + char *input, *str; + PyObject *output; - if (!getargs(args, "(is#ii)", &invert, &input, &inputlength, &width, &height)) + if (!PyArg_Parse(args, "(is#ii)", &invert, + &input, &inputlength, &width, &height)) return NULL; if (width * height * inputfactor > inputlength) { - err_setstr(SvError, "input buffer not long enough"); + PyErr_SetString(SvError, "input buffer not long enough"); return NULL; } - output = newsizedstringobject(NULL, (int) (width * height * factor)); - if (output == NULL) + if (!(output = PyString_FromStringAndSize(NULL, + (int)(width * height * factor)))) return NULL; - (*function)(invert, input, getstringvalue(output), width, height); + str = PyString_AsString(output); + if (!str) { + Py_DECREF(output); + return NULL; + } + (*function)(invert, input, str, width, height); return output; } -static object * +static PyObject * sv_InterleaveFields(self, args) - object *self, *args; + PyObject *self, *args; { return sv_conversion(self, args, svInterleaveFields, 1, 1.0); } -static object * +static PyObject * sv_RGB8toRGB32(self, args) - object *self, *args; + PyObject *self, *args; { return sv_conversion(self, args, svRGB8toRGB32, 1, (float) sizeof(long)); } -static object * +static PyObject * sv_YUVtoRGB(self, args) - object *self, *args; + PyObject *self, *args; { return sv_conversion(self, args, svYUVtoRGB, 2, (float) sizeof(long)); } @@ -919,19 +989,19 @@ svideo_dealloc(self) { if (self->ob_svideo != NULL) (void) svCloseVideo(self->ob_svideo); - DEL(self); + PyMem_DEL(self); } -static object * +static PyObject * svideo_getattr(self, name) svobject *self; char *name; { - return findmethod(svideo_methods, (object *)self, name); + return Py_FindMethod(svideo_methods, (PyObject *)self, name); } -typeobject Svtype = { - OB_HEAD_INIT(&Typetype) +PyTypeObject Svtype = { + PyObject_HEAD_INIT(&PyType_Type) 0, /*ob_size*/ "sv", /*tp_name*/ sizeof(svobject), /*tp_size*/ @@ -945,13 +1015,13 @@ typeobject Svtype = { 0, /*tp_repr*/ }; -static object * +static PyObject * newsvobject(svp) SV_nodeP svp; { svobject *p; - p = NEWOBJ(svobject, &Svtype); + p = PyObject_NEW(svobject, &Svtype); if (p == NULL) return NULL; p->ob_svideo = svp; @@ -960,16 +1030,16 @@ newsvobject(svp) p->ob_info.width = 0; p->ob_info.height = 0; p->ob_info.samplingrate = 0; - return (object *) p; + return (PyObject *) p; } -static object * +static PyObject * sv_OpenVideo(self, args) - object *self, *args; + PyObject *self, *args; { SV_nodeP svp; - if (!getnoarg(args)) + if (!PyArg_Parse(args, "")) return NULL; svp = svOpenVideo(); @@ -979,23 +1049,23 @@ sv_OpenVideo(self, args) return newsvobject(svp); } -static struct methodlist sv_methods[] = { - {"InterleaveFields", (method)sv_InterleaveFields}, - {"RGB8toRGB32", (method)sv_RGB8toRGB32}, - {"YUVtoRGB", (method)sv_YUVtoRGB}, - {"OpenVideo", (method)sv_OpenVideo}, +static PyMethodDef sv_methods[] = { + {"InterleaveFields", (PyCFunction)sv_InterleaveFields}, + {"RGB8toRGB32", (PyCFunction)sv_RGB8toRGB32}, + {"YUVtoRGB", (PyCFunction)sv_YUVtoRGB}, + {"OpenVideo", (PyCFunction)sv_OpenVideo}, {NULL, NULL} /* Sentinel */ }; void initsv() { - object *m, *d; + PyObject *m, *d; - m = initmodule("sv", sv_methods); - d = getmoduledict(m); + m = Py_InitModule("sv", sv_methods); + d = PyModule_GetDict(m); - SvError = newstringobject("sv.error"); - if (SvError == NULL || dictinsert(d, "error", SvError) != 0) - fatal("can't define sv.error"); + SvError = PyString_FromString("sv.error"); + if (SvError == NULL || PyDict_SetItemString(d, "error", SvError) != 0) + Py_FatalError("can't define sv.error"); } |