home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / pippy-0.6beta-src.tar.gz / pippy-0.6beta-src.tar / pippy-0.6beta-src / src / Modules / svmodule.c < prev    next >
C/C++ Source or Header  |  2000-12-21  |  23KB  |  1,077 lines

  1. /**********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* SV module -- interface to the Indigo video board */
  33.  
  34. /* WARNING! This module is for hardware that we don't have any more,
  35.    so it hasn't been tested.  It has been converted to the new coding
  36.    style, and it is possible that this conversion has broken something
  37.    -- user beware! */
  38.  
  39. #include <sys/time.h>
  40. #include <svideo.h>
  41. #include "Python.h"
  42. #include "compile.h"
  43. #include "yuv.h"        /* for YUV conversion functions */
  44.  
  45. typedef struct {
  46.     PyObject_HEAD
  47.     SV_nodeP ob_svideo;
  48.     svCaptureInfo ob_info;
  49. } svobject;
  50.  
  51. typedef struct {
  52.     PyObject_HEAD
  53.     void *ob_capture;
  54.     int ob_mustunlock;
  55.     svCaptureInfo ob_info;
  56.     svobject *ob_svideo;
  57. } captureobject;
  58.  
  59. static PyObject *SvError;        /* exception sv.error */
  60.  
  61. static PyObject *newcaptureobject Py_PROTO((svobject *, void *, int));
  62.  
  63. /* Set a SV-specific error from svideo_errno and return NULL */
  64. static PyObject *
  65. sv_error()
  66. {
  67.     PyErr_SetString(SvError, svStrerror(svideo_errno));
  68.     return NULL;
  69. }
  70.  
  71. static PyObject *
  72. svc_conversion(self, args, function, factor)
  73.     captureobject *self;
  74.     PyObject *args;
  75.     void (*function)();
  76.     float factor;
  77. {
  78.     PyObject *output;
  79.     int invert;
  80.     char* outstr;
  81.  
  82.     if (!PyArg_Parse(args, "i", &invert))
  83.         return NULL;
  84.  
  85.     if (!(output = PyString_FromStringAndSize(
  86.         NULL,
  87.         (int)(self->ob_info.width * self->ob_info.height * factor))))
  88.     {
  89.         return NULL;
  90.     }
  91.     if (!(outstr = PyString_AsString(output))) {
  92.         Py_DECREF(output);
  93.         return NULL;
  94.     }
  95.  
  96.     (*function)((boolean)invert, self->ob_capture,
  97.             outstr,
  98.             self->ob_info.width, self->ob_info.height);
  99.  
  100.     return output;
  101. }
  102.  
  103. /*
  104.  * 3 functions to convert from Starter Video YUV 4:1:1 format to
  105.  * Compression Library 4:2:2 Duplicate Chroma format.
  106.  */
  107. static PyObject *
  108. svc_YUVtoYUV422DC(self, args)
  109.     captureobject *self;
  110.     PyObject *args;
  111. {
  112.     if (self->ob_info.format != SV_YUV411_FRAMES) {
  113.         PyErr_SetString(SvError, "data has bad format");
  114.         return NULL;
  115.     }
  116.     return svc_conversion(self, args, yuv_sv411_to_cl422dc, 2.0);
  117. }
  118.  
  119. static PyObject *
  120. svc_YUVtoYUV422DC_quarter(self, args)
  121.     captureobject *self;
  122.     PyObject *args;
  123. {
  124.     if (self->ob_info.format != SV_YUV411_FRAMES) {
  125.         PyErr_SetString(SvError, "data has bad format");
  126.         return NULL;
  127.     }
  128.     return svc_conversion(self, args,
  129.                   yuv_sv411_to_cl422dc_quartersize, 0.5);
  130. }
  131.  
  132. static PyObject *
  133. svc_YUVtoYUV422DC_sixteenth(self, args)
  134.     captureobject *self;
  135.     PyObject *args;
  136. {
  137.     if (self->ob_info.format != SV_YUV411_FRAMES) {
  138.         PyErr_SetString(SvError, "data has bad format");
  139.         return NULL;
  140.     }
  141.     return svc_conversion(self, args,
  142.                   yuv_sv411_to_cl422dc_sixteenthsize, 0.125);
  143. }
  144.  
  145. static PyObject *
  146. svc_YUVtoRGB(self, args)
  147.     captureobject *self;
  148.     PyObject *args;
  149. {
  150.     switch (self->ob_info.format) {
  151.     case SV_YUV411_FRAMES:
  152.     case SV_YUV411_FRAMES_AND_BLANKING_BUFFER:
  153.         break;
  154.     default:
  155.         PyErr_SetString(SvError, "data had bad format");
  156.         return NULL;
  157.     }
  158.     return svc_conversion(self, args, svYUVtoRGB, (float) sizeof(long));
  159. }
  160.  
  161. static PyObject *
  162. svc_RGB8toRGB32(self, args)
  163.     captureobject *self;
  164.     PyObject *args;
  165. {
  166.     if (self->ob_info.format != SV_RGB8_FRAMES) {
  167.         PyErr_SetString(SvError, "data has bad format");
  168.         return NULL;
  169.     }
  170.     return svc_conversion(self, args, svRGB8toRGB32, (float) sizeof(long));
  171. }
  172.  
  173. static PyObject *
  174. svc_InterleaveFields(self, args)
  175.     captureobject *self;
  176.     PyObject *args;
  177. {
  178.     if (self->ob_info.format != SV_RGB8_FRAMES) {
  179.         PyErr_SetString(SvError, "data has bad format");
  180.         return NULL;
  181.     }
  182.     return svc_conversion(self, args, svInterleaveFields, 1.0);
  183. }
  184.  
  185. static PyObject *
  186. svc_GetFields(self, args)
  187.     captureobject *self;
  188.     PyObject *args;
  189. {
  190.     PyObject *f1 = NULL;
  191.     PyObject *f2 = NULL;
  192.     PyObject *ret = NULL;
  193.     int fieldsize;
  194.     char* obcapture;
  195.  
  196.     if (self->ob_info.format != SV_RGB8_FRAMES) {
  197.         PyErr_SetString(SvError, "data has bad format");
  198.         return NULL;
  199.     }
  200.  
  201.     fieldsize = self->ob_info.width * self->ob_info.height / 2;
  202.     obcapture = (char*)self->ob_capture;
  203.     
  204.     if (!(f1 = PyString_FromStringAndSize(obcapture, fieldsize)))
  205.         goto finally;
  206.     if (!(f2 = PyString_FromStringAndSize(obcapture + fieldsize,
  207.                           fieldsize)))
  208.         goto finally;
  209.     ret = Py_BuildValue("(OO)", f1, f2);
  210.  
  211.   finally:
  212.     Py_XDECREF(f1);
  213.     Py_XDECREF(f2);
  214.     return ret;
  215. }
  216.     
  217. static PyObject *
  218. svc_UnlockCaptureData(self, args)
  219.     captureobject *self;
  220.     PyObject *args;
  221. {
  222.     if (!PyArg_Parse(args, ""))
  223.         return NULL;
  224.  
  225.     if (!self->ob_mustunlock) {
  226.         PyErr_SetString(SvError, "buffer should not be unlocked");
  227.         return NULL;
  228.     }
  229.  
  230.     if (svUnlockCaptureData(self->ob_svideo->ob_svideo, self->ob_capture))
  231.         return sv_error();
  232.  
  233.     self->ob_mustunlock = 0;
  234.  
  235.     Py_INCREF(Py_None);
  236.     return Py_None;
  237. }
  238.  
  239. #ifdef USE_GL
  240. #include <gl.h>
  241.  
  242. static PyObject *
  243. svc_lrectwrite(self, args)
  244.     captureobject *self;
  245.     PyObject *args;
  246. {
  247.     Screencoord x1, x2, y1, y2;
  248.  
  249.     if (!PyArg_Parse(args, "(hhhh)", &x1, &x2, &y1, &y2))
  250.         return NULL;
  251.  
  252.     lrectwrite(x1, x2, y1, y2, (unsigned long *) self->ob_capture);
  253.  
  254.     Py_INCREF(Py_None);
  255.     return Py_None;
  256. }
  257. #endif
  258.  
  259. static PyObject *
  260. svc_writefile(self, args)
  261.     captureobject *self;
  262.     PyObject *args;
  263. {
  264.     PyObject *file;
  265.     int size;
  266.     FILE* fp;
  267.  
  268.     if (!PyArg_Parse(args, "O", &file))
  269.         return NULL;
  270.  
  271.     if (!PyFile_Check(file)) {
  272.         PyErr_SetString(SvError, "not a file object");
  273.         return NULL;
  274.     }
  275.  
  276.     if (!(fp = PyFile_AsFile(file)))
  277.         return NULL;
  278.  
  279.     size = self->ob_info.width * self->ob_info.height;
  280.  
  281.     if (fwrite(self->ob_capture, sizeof(long), size, fp) != size) {
  282.         PyErr_SetString(SvError, "writing failed");
  283.         return NULL;
  284.     }
  285.  
  286.     Py_INCREF(Py_None);
  287.     return Py_None;
  288. }
  289.  
  290. static PyObject *
  291. svc_FindVisibleRegion(self, args)
  292.     captureobject *self;
  293.     PyObject *args;
  294. {
  295.     void *visible;
  296.     int width;
  297.  
  298.     if (!PyArg_Parse(args, ""))
  299.         return NULL;
  300.  
  301.     if (svFindVisibleRegion(self->ob_svideo->ob_svideo,
  302.                 self->ob_capture, &visible,
  303.                 self->ob_info.width))
  304.         return sv_error();
  305.  
  306.     if (visible == NULL) {
  307.         PyErr_SetString(SvError, "data in wrong format");
  308.         return NULL;
  309.     }
  310.  
  311.     return newcaptureobject(self->ob_svideo, visible, 0);
  312. }
  313.  
  314. static PyMethodDef capture_methods[] = {
  315.     {"YUVtoRGB",        (PyCFunction)svc_YUVtoRGB},
  316.     {"RGB8toRGB32",        (PyCFunction)svc_RGB8toRGB32},
  317.     {"InterleaveFields",    (PyCFunction)svc_InterleaveFields},
  318.     {"UnlockCaptureData",    (PyCFunction)svc_UnlockCaptureData},
  319.     {"FindVisibleRegion",    (PyCFunction)svc_FindVisibleRegion},
  320.     {"GetFields",        (PyCFunction)svc_GetFields},
  321.     {"YUVtoYUV422DC",    (PyCFunction)svc_YUVtoYUV422DC},
  322.     {"YUVtoYUV422DC_quarter",(PyCFunction)svc_YUVtoYUV422DC_quarter},
  323.     {"YUVtoYUV422DC_sixteenth",(PyCFunction)svc_YUVtoYUV422DC_sixteenth},
  324. #ifdef USE_GL
  325.     {"lrectwrite",        (PyCFunction)svc_lrectwrite},
  326. #endif
  327.     {"writefile",        (PyCFunction)svc_writefile},
  328.     {NULL,            NULL}         /* sentinel */
  329. };
  330.  
  331. static void
  332. capture_dealloc(self)
  333.     captureobject *self;
  334. {
  335.     if (self->ob_capture != NULL) {
  336.         if (self->ob_mustunlock)
  337.             (void)svUnlockCaptureData(self->ob_svideo->ob_svideo,
  338.                           self->ob_capture);
  339.         self->ob_capture = NULL;
  340.         Py_DECREF(self->ob_svideo);
  341.         self->ob_svideo = NULL;
  342.     }
  343.     PyMem_DEL(self);
  344. }
  345.  
  346. static PyObject *
  347. capture_getattr(self, name)
  348.     svobject *self;
  349.     char *name;
  350. {
  351.     return Py_FindMethod(capture_methods, (PyObject *)self, name);
  352. }
  353.  
  354. PyTypeObject Capturetype = {
  355.     PyObject_HEAD_INIT(&PyType_Type)
  356.     0,                /*ob_size*/
  357.     "capture",            /*tp_name*/
  358.     sizeof(captureobject),        /*tp_size*/
  359.     0,                /*tp_itemsize*/
  360.     /* methods */
  361.     (destructor)capture_dealloc,    /*tp_dealloc*/
  362.     0,                /*tp_print*/
  363.     (getattrfunc)capture_getattr,    /*tp_getattr*/
  364.     0,                /*tp_setattr*/
  365.     0,                /*tp_compare*/
  366.     0,                /*tp_repr*/
  367. };
  368.  
  369. static PyObject *
  370. newcaptureobject(self, ptr, mustunlock)
  371.     svobject *self;
  372.     void *ptr;
  373.     int mustunlock;
  374. {
  375.     captureobject *p;
  376.  
  377.     p = PyObject_NEW(captureobject, &Capturetype);
  378.     if (p == NULL)
  379.         return NULL;
  380.     p->ob_svideo = self;
  381.     Py_INCREF(self);
  382.     p->ob_capture = ptr;
  383.     p->ob_mustunlock = mustunlock;
  384.     p->ob_info = self->ob_info;
  385.     return (PyObject *) p;
  386. }
  387.  
  388. static PyObject *
  389. sv_GetCaptureData(self, args)
  390.     svobject *self;
  391.     PyObject *args;
  392. {
  393.     void *ptr;
  394.     long fieldID;
  395.     PyObject *res, *c;
  396.  
  397.     if (!PyArg_Parse(args, ""))
  398.         return NULL;
  399.  
  400.     if (svGetCaptureData(self->ob_svideo, &ptr, &fieldID))
  401.         return sv_error();
  402.  
  403.     if (ptr == NULL) {
  404.         PyErr_SetString(SvError, "no data available");
  405.         return NULL;
  406.     }
  407.  
  408.     c = newcaptureobject(self, ptr, 1);
  409.     if (c == NULL)
  410.         return NULL;
  411.     res = Py_BuildValue("(Oi)", c, fieldID);
  412.     Py_DECREF(c);
  413.     return res;
  414. }
  415.  
  416. static PyObject *
  417. sv_BindGLWindow(self, args)
  418.     svobject *self;
  419.     PyObject *args;
  420. {
  421.     long wid;
  422.     int mode;
  423.  
  424.     if (!PyArg_Parse(args, "(ii)", &wid, &mode))
  425.         return NULL;
  426.  
  427.     if (svBindGLWindow(self->ob_svideo, wid, mode))
  428.         return sv_error();
  429.  
  430.     Py_INCREF(Py_None);
  431.     return Py_None;
  432. }
  433.  
  434. static PyObject *
  435. sv_EndContinuousCapture(self, args)
  436.     svobject *self;
  437.     PyObject *args;
  438. {
  439.  
  440.     if (!PyArg_Parse(args, ""))
  441.         return NULL;
  442.  
  443.     if (svEndContinuousCapture(self->ob_svideo))
  444.         return sv_error();
  445.  
  446.     Py_INCREF(Py_None);
  447.     return Py_None;
  448. }
  449.  
  450. static PyObject *
  451. sv_IsVideoDisplayed(self, args)
  452.     svobject *self;
  453.     PyObject *args;
  454. {
  455.     int v;
  456.  
  457.     if (!PyArg_Parse(args, ""))
  458.         return NULL;
  459.  
  460.     v = svIsVideoDisplayed(self->ob_svideo);
  461.     if (v == -1)
  462.         return sv_error();
  463.  
  464.     return PyInt_FromLong((long) v);
  465. }
  466.  
  467. static PyObject *
  468. sv_OutputOffset(self, args)
  469.     svobject *self;
  470.     PyObject *args;
  471. {
  472.     int x_offset;
  473.     int y_offset;
  474.  
  475.     if (!PyArg_Parse(args, "(ii)", &x_offset, &y_offset))
  476.         return NULL;
  477.  
  478.     if (svOutputOffset(self->ob_svideo, x_offset, y_offset))
  479.         return sv_error();
  480.  
  481.     Py_INCREF(Py_None);
  482.     return Py_None;
  483. }
  484.  
  485. static PyObject *
  486. sv_PutFrame(self, args)
  487.     svobject *self;
  488.     PyObject *args;
  489. {
  490.     char *buffer;
  491.  
  492.     if (!PyArg_Parse(args, "s", &buffer))
  493.         return NULL;
  494.  
  495.     if (svPutFrame(self->ob_svideo, buffer))
  496.         return sv_error();
  497.  
  498.     Py_INCREF(Py_None);
  499.     return Py_None;
  500. }
  501.  
  502. static PyObject *
  503. sv_QuerySize(self, args)
  504.     svobject *self;
  505.     PyObject *args;
  506. {
  507.     int w;
  508.     int h;
  509.     int rw;
  510.     int rh;
  511.  
  512.     if (!PyArg_Parse(args, "(ii)", &w, &h))
  513.         return NULL;
  514.  
  515.     if (svQuerySize(self->ob_svideo, w, h, &rw, &rh))
  516.         return sv_error();
  517.  
  518.     return Py_BuildValue("(ii)", (long) rw, (long) rh);
  519. }
  520.  
  521. static PyObject *
  522. sv_SetSize(self, args)
  523.     svobject *self;
  524.     PyObject *args;
  525. {
  526.     int w;
  527.     int h;
  528.  
  529.     if (!PyArg_Parse(args, "(ii)", &w, &h))
  530.         return NULL;
  531.  
  532.     if (svSetSize(self->ob_svideo, w, h))
  533.         return sv_error();
  534.  
  535.     Py_INCREF(Py_None);
  536.     return Py_None;
  537. }
  538.  
  539. static PyObject *
  540. sv_SetStdDefaults(self, args)
  541.     svobject *self;
  542.     PyObject *args;
  543. {
  544.  
  545.     if (!PyArg_Parse(args, ""))
  546.         return NULL;
  547.  
  548.     if (svSetStdDefaults(self->ob_svideo))
  549.         return sv_error();
  550.  
  551.     Py_INCREF(Py_None);
  552.     return Py_None;
  553. }
  554.  
  555. static PyObject *
  556. sv_UseExclusive(self, args)
  557.     svobject *self;
  558.     PyObject *args;
  559. {
  560.     boolean onoff;
  561.     int mode;
  562.  
  563.     if (!PyArg_Parse(args, "(ii)", &onoff, &mode))
  564.         return NULL;
  565.  
  566.     if (svUseExclusive(self->ob_svideo, onoff, mode))
  567.         return sv_error();
  568.  
  569.     Py_INCREF(Py_None);
  570.     return Py_None;
  571. }
  572.  
  573. static PyObject *
  574. sv_WindowOffset(self, args)
  575.     svobject *self;
  576.     PyObject *args;
  577. {
  578.     int x_offset;
  579.     int y_offset;
  580.  
  581.     if (!PyArg_Parse(args, "(ii)", &x_offset, &y_offset))
  582.         return NULL;
  583.  
  584.     if (svWindowOffset(self->ob_svideo, x_offset, y_offset))
  585.         return sv_error();
  586.  
  587.     Py_INCREF(Py_None);
  588.     return Py_None;
  589. }
  590.  
  591. static PyObject *
  592. sv_CaptureBurst(self, args)
  593.     svobject *self;
  594.     PyObject *args;
  595. {
  596.     int bytes, i;
  597.     svCaptureInfo info;
  598.     void *bitvector = NULL;
  599.     PyObject *videodata = NULL;
  600.     PyObject *bitvecobj = NULL;
  601.     PyObject *res = NULL;
  602.     static PyObject *evenitem, *odditem;
  603.  
  604.     if (!PyArg_Parse(args, "(iiiii)", &info.format,
  605.              &info.width, &info.height,
  606.              &info.size, &info.samplingrate))
  607.         return NULL;
  608.  
  609.     switch (info.format) {
  610.     case SV_RGB8_FRAMES:
  611.         bitvector = malloc(SV_BITVEC_SIZE(info.size));
  612.         break;
  613.     case SV_YUV411_FRAMES_AND_BLANKING_BUFFER:
  614.         break;
  615.     default:
  616.         PyErr_SetString(SvError, "illegal format specified");
  617.         return NULL;
  618.     }
  619.  
  620.     if (svQueryCaptureBufferSize(self->ob_svideo, &info, &bytes)) {
  621.         res = sv_error();
  622.         goto finally;
  623.     }
  624.  
  625.     if (!(videodata = PyString_FromStringAndSize(NULL, bytes)))
  626.         goto finally;
  627.  
  628.     /* XXX -- need to do something about the bitvector */
  629.     {
  630.         char* str = PyString_AsString(videodata);
  631.         if (!str)
  632.             goto finally;
  633.         
  634.         if (svCaptureBurst(self->ob_svideo, &info, str, bitvector)) {
  635.             res = sv_error();
  636.             goto finally;
  637.         }
  638.     }
  639.  
  640.     if (bitvector) {
  641.         if (evenitem == NULL) {
  642.             if (!(evenitem = PyInt_FromLong(0)))
  643.                 goto finally;
  644.         }
  645.         if (odditem == NULL) {
  646.             if (!(odditem = PyInt_FromLong(1)))
  647.                 goto finally;
  648.         }
  649.         if (!(bitvecobj = PyTuple_New(2 * info.size)))
  650.             goto finally;
  651.  
  652.         for (i = 0; i < 2 * info.size; i++) {
  653.             int sts;
  654.  
  655.             if (SV_GET_FIELD(bitvector, i) == SV_EVEN_FIELD) {
  656.                 Py_INCREF(evenitem);
  657.                 sts = PyTuple_SetItem(bitvecobj, i, evenitem);
  658.             } else {
  659.                 Py_INCREF(odditem);
  660.                 sts = PyTuple_SetItem(bitvecobj, i, odditem);
  661.             }
  662.             if (sts < 0)
  663.                 goto finally;
  664.         }
  665.     } else {
  666.         bitvecobj = Py_None;
  667.         Py_INCREF(Py_None);
  668.     }
  669.  
  670.     res = Py_BuildValue("((iiiii)OO)", info.format,
  671.                 info.width, info.height,
  672.                 info.size, info.samplingrate,
  673.                 videodata, bitvecobj);
  674.  
  675.   finally:
  676.     if (bitvector)
  677.         free(bitvector);
  678.  
  679.     Py_XDECREF(videodata);
  680.     Py_XDECREF(bitvecobj);
  681.     return res;
  682. }
  683.  
  684. static PyObject *
  685. sv_CaptureOneFrame(self, args)
  686.     svobject *self;
  687.     PyObject *args;
  688. {
  689.     svCaptureInfo info;
  690.     int format, width, height;
  691.     int bytes;
  692.     PyObject *videodata = NULL;
  693.     PyObject *res = NULL;
  694.     char *str;
  695.     
  696.     if (!PyArg_Parse(args, "(iii)", &format, &width, &height))
  697.         return NULL;
  698.  
  699.     info.format = format;
  700.     info.width = width;
  701.     info.height = height;
  702.     info.size = 0;
  703.     info.samplingrate = 0;
  704.     if (svQueryCaptureBufferSize(self->ob_svideo, &info, &bytes))
  705.         return sv_error();
  706.  
  707.     if (!(videodata = PyString_FromStringAndSize(NULL, bytes)))
  708.         return NULL;
  709.     
  710.     str = PyString_AsString(videodata);
  711.     if (!str)
  712.         goto finally;
  713.  
  714.     if (svCaptureOneFrame(self->ob_svideo, format, &width, &height, str)) {
  715.         res = sv_error();
  716.         goto finally;
  717.     }
  718.  
  719.     res = Py_BuildValue("(iiO)", width, height, videodata);
  720.  
  721.   finally:
  722.     Py_XDECREF(videodata);
  723.     return res;
  724. }
  725.  
  726. static PyObject *
  727. sv_InitContinuousCapture(self, args)
  728.     svobject *self;
  729.     PyObject *args;
  730. {
  731.     svCaptureInfo info;
  732.  
  733.     if (!PyArg_Parse(args, "(iiiii)", &info.format,
  734.              &info.width, &info.height,
  735.              &info.size, &info.samplingrate))
  736.         return NULL;
  737.  
  738.     if (svInitContinuousCapture(self->ob_svideo, &info))
  739.         return sv_error();
  740.  
  741.     self->ob_info = info;
  742.  
  743.     return Py_BuildValue("(iiiii)", info.format, info.width, info.height,
  744.                  info.size, info.samplingrate);
  745. }
  746.  
  747. static PyObject *
  748. sv_LoadMap(self, args)
  749.     svobject *self;
  750.     PyObject *args;
  751. {
  752.     PyObject *rgb;
  753.     PyObject *res = NULL;
  754.     rgb_tuple *mapp = NULL;
  755.     int maptype;
  756.     int i, j;                 /* indices */
  757.  
  758.     if (!PyArg_Parse(args, "(iO)", &maptype, &rgb))
  759.         return NULL;
  760.  
  761.     if (!PyList_Check(rgb) || PyList_Size(rgb) != 256) {
  762.         PyErr_BadArgument();
  763.         return NULL;
  764.     }
  765.  
  766.     if (!(mapp = PyMem_NEW(rgb_tuple, 256)))
  767.         return PyErr_NoMemory();
  768.  
  769.     for (i = 0; i < 256; i++) {
  770.         PyObject* v = PyList_GetItem(rgb, i);
  771.         if (!v)
  772.             goto finally;
  773.  
  774.         if (!PyTuple_Check(v) || PyTuple_Size(v) != 3) {
  775.             PyErr_BadArgument();
  776.             goto finally;
  777.         }
  778.         for (j = 0; j < 3; j++) {
  779.             PyObject* cell = PyTuple_GetItem(v, j);
  780.             if (!cell)
  781.                 goto finally;
  782.  
  783.             if (!PyInt_Check(cell)) {
  784.                 PyErr_BadArgument();
  785.                 goto finally;
  786.             }
  787.             switch (j) {
  788.             case 0: mapp[i].red = PyInt_AsLong(cell); break;
  789.             case 1: mapp[i].blue = PyInt_AsLong(cell); break;
  790.             case 2: mapp[i].green = PyInt_AsLong(cell); break;
  791.             }
  792.             if (PyErr_Occurred())
  793.                 goto finally;
  794.         }
  795.     }
  796.  
  797.     if (svLoadMap(self->ob_svideo, maptype, mapp)) {
  798.         res = sv_error();
  799.         goto finally;
  800.     }
  801.  
  802.     Py_INCREF(Py_None);
  803.     res = Py_None;
  804.  
  805.   finally:
  806.     PyMem_DEL(mapp);
  807.     return res;
  808. }
  809.         
  810. static PyObject *
  811. sv_CloseVideo(self, args)
  812.     svobject *self;
  813.     PyObject *args;
  814. {
  815.     if (!PyArg_Parse(args, ""))
  816.         return NULL;
  817.  
  818.     if (svCloseVideo(self->ob_svideo))
  819.         return sv_error();
  820.  
  821.     self->ob_svideo = NULL;
  822.     Py_INCREF(Py_None);
  823.     return Py_None;
  824. }
  825.  
  826. static PyObject *
  827. doParams(self, args, func, modified)
  828.     svobject *self;
  829.     PyObject *args;
  830.     int (*func)(SV_nodeP, long *, int);
  831.     int modified;
  832. {
  833.     PyObject *list;
  834.     PyObject *res = NULL;
  835.     long *PVbuffer = NULL;
  836.     long length;
  837.     int i;
  838.     
  839.     if (!PyArg_Parse(args, "O", &list))
  840.         return NULL;
  841.  
  842.     if (!PyList_Check(list)) {
  843.         PyErr_BadArgument();
  844.         return NULL;
  845.     }
  846.  
  847.     if ((length = PyList_Size(list)) < 0)
  848.         return NULL;
  849.  
  850.     PVbuffer = PyMem_NEW(long, length);
  851.     if (PVbuffer == NULL)
  852.         return PyErr_NoMemory();
  853.  
  854.     for (i = 0; i < length; i++) {
  855.         PyObject *v = PyList_GetItem(list, i);
  856.         if (!v)
  857.             goto finally;
  858.  
  859.         if (!PyInt_Check(v)) {
  860.             PyErr_BadArgument();
  861.             goto finally;
  862.         }
  863.         PVbuffer[i] = PyInt_AsLong(v);
  864.         /* can't just test the return value, because what if the
  865.            value was -1?!
  866.         */
  867.         if (PVbuffer[i] == -1 && PyErr_Occurred())
  868.             goto finally;
  869.     }
  870.  
  871.     if ((*func)(self->ob_svideo, PVbuffer, length)) {
  872.         res = sv_error();
  873.         goto finally;
  874.     }
  875.  
  876.     if (modified) {
  877.         for (i = 0; i < length; i++) {
  878.             PyObject* v = PyInt_FromLong(PVbuffer[i]);
  879.             if (!v || PyList_SetItem(list, i, v) < 0)
  880.                 goto finally;
  881.         }
  882.     }
  883.  
  884.     Py_INCREF(Py_None);
  885.     res = Py_None;
  886.  
  887.   finally:
  888.     PyMem_DEL(PVbuffer);
  889.     return res;
  890. }
  891.  
  892. static PyObject *
  893. sv_GetParam(self, args)
  894.     PyObject *self, *args;
  895. {
  896.     return doParams(self, args, svGetParam, 1);
  897. }
  898.  
  899. static PyObject *
  900. sv_GetParamRange(self, args)
  901.     PyObject *self, *args;
  902. {
  903.     return doParams(self, args, svGetParamRange, 1);
  904. }
  905.  
  906. static PyObject *
  907. sv_SetParam(self, args)
  908.     PyObject *self, *args;
  909. {
  910.     return doParams(self, args, svSetParam, 0);
  911. }
  912.  
  913. static PyMethodDef svideo_methods[] = {
  914.     {"BindGLWindow",    (PyCFunction)sv_BindGLWindow},
  915.     {"EndContinuousCapture",(PyCFunction)sv_EndContinuousCapture},
  916.     {"IsVideoDisplayed",    (PyCFunction)sv_IsVideoDisplayed},
  917.     {"OutputOffset",    (PyCFunction)sv_OutputOffset},
  918.     {"PutFrame",        (PyCFunction)sv_PutFrame},
  919.     {"QuerySize",        (PyCFunction)sv_QuerySize},
  920.     {"SetSize",        (PyCFunction)sv_SetSize},
  921.     {"SetStdDefaults",    (PyCFunction)sv_SetStdDefaults},
  922.     {"UseExclusive",    (PyCFunction)sv_UseExclusive},
  923.     {"WindowOffset",    (PyCFunction)sv_WindowOffset},
  924.     {"InitContinuousCapture",(PyCFunction)sv_InitContinuousCapture},
  925.     {"CaptureBurst",    (PyCFunction)sv_CaptureBurst},
  926.     {"CaptureOneFrame",    (PyCFunction)sv_CaptureOneFrame},
  927.     {"GetCaptureData",    (PyCFunction)sv_GetCaptureData},
  928.     {"CloseVideo",        (PyCFunction)sv_CloseVideo},
  929.     {"LoadMap",        (PyCFunction)sv_LoadMap},
  930.     {"GetParam",        (PyCFunction)sv_GetParam},
  931.     {"GetParamRange",    (PyCFunction)sv_GetParamRange},
  932.     {"SetParam",        (PyCFunction)sv_SetParam},
  933.     {NULL,            NULL}         /* sentinel */
  934. };
  935.  
  936. static PyObject *
  937. sv_conversion(self, args, function, inputfactor, factor)
  938.     PyObject *self, *args;
  939.     void (*function)();
  940.     int inputfactor;
  941.     float factor;
  942. {
  943.     int invert, width, height, inputlength;
  944.     char *input, *str;
  945.     PyObject *output;
  946.  
  947.     if (!PyArg_Parse(args, "(is#ii)", &invert,
  948.              &input, &inputlength, &width, &height))
  949.         return NULL;
  950.  
  951.     if (width * height * inputfactor > inputlength) {
  952.         PyErr_SetString(SvError, "input buffer not long enough");
  953.         return NULL;
  954.     }
  955.  
  956.     if (!(output = PyString_FromStringAndSize(NULL,
  957.                           (int)(width * height * factor))))
  958.         return NULL;
  959.  
  960.     str = PyString_AsString(output);
  961.     if (!str) {
  962.         Py_DECREF(output);
  963.         return NULL;
  964.     }
  965.     (*function)(invert, input, str, width, height);
  966.  
  967.     return output;
  968. }
  969.  
  970. static PyObject *
  971. sv_InterleaveFields(self, args)
  972.     PyObject *self, *args;
  973. {
  974.     return sv_conversion(self, args, svInterleaveFields, 1, 1.0);
  975. }
  976.  
  977. static PyObject *
  978. sv_RGB8toRGB32(self, args)
  979.     PyObject *self, *args;
  980. {
  981.     return sv_conversion(self, args, svRGB8toRGB32, 1, (float) sizeof(long));
  982. }
  983.  
  984. static PyObject *
  985. sv_YUVtoRGB(self, args)
  986.     PyObject *self, *args;
  987. {
  988.     return sv_conversion(self, args, svYUVtoRGB, 2, (float) sizeof(long));
  989. }
  990.  
  991. static void
  992. svideo_dealloc(self)
  993.     svobject *self;
  994. {
  995.     if (self->ob_svideo != NULL)
  996.         (void) svCloseVideo(self->ob_svideo);
  997.     PyMem_DEL(self);
  998. }
  999.  
  1000. static PyObject *
  1001. svideo_getattr(self, name)
  1002.     svobject *self;
  1003.     char *name;
  1004. {
  1005.     return Py_FindMethod(svideo_methods, (PyObject *)self, name);
  1006. }
  1007.  
  1008. PyTypeObject Svtype = {
  1009.     PyObject_HEAD_INIT(&PyType_Type)
  1010.     0,            /*ob_size*/
  1011.     "sv",            /*tp_name*/
  1012.     sizeof(svobject),    /*tp_size*/
  1013.     0,            /*tp_itemsize*/
  1014.     /* methods */
  1015.     (destructor)svideo_dealloc, /*tp_dealloc*/
  1016.     0,            /*tp_print*/
  1017.     (getattrfunc)svideo_getattr, /*tp_getattr*/
  1018.     0,            /*tp_setattr*/
  1019.     0,            /*tp_compare*/
  1020.     0,            /*tp_repr*/
  1021. };
  1022.  
  1023. static PyObject *
  1024. newsvobject(svp)
  1025.     SV_nodeP svp;
  1026. {
  1027.     svobject *p;
  1028.  
  1029.     p = PyObject_NEW(svobject, &Svtype);
  1030.     if (p == NULL)
  1031.         return NULL;
  1032.     p->ob_svideo = svp;
  1033.     p->ob_info.format = 0;
  1034.     p->ob_info.size = 0;
  1035.     p->ob_info.width = 0;
  1036.     p->ob_info.height = 0;
  1037.     p->ob_info.samplingrate = 0;
  1038.     return (PyObject *) p;
  1039. }
  1040.  
  1041. static PyObject *
  1042. sv_OpenVideo(self, args)
  1043.     PyObject *self, *args;
  1044. {
  1045.     SV_nodeP svp;
  1046.  
  1047.     if (!PyArg_Parse(args, ""))
  1048.         return NULL;
  1049.  
  1050.     svp = svOpenVideo();
  1051.     if (svp == NULL)
  1052.         return sv_error();
  1053.  
  1054.     return newsvobject(svp);
  1055. }
  1056.  
  1057. static PyMethodDef sv_methods[] = {
  1058.     {"InterleaveFields",    (PyCFunction)sv_InterleaveFields},
  1059.     {"RGB8toRGB32",        (PyCFunction)sv_RGB8toRGB32},
  1060.     {"YUVtoRGB",        (PyCFunction)sv_YUVtoRGB},
  1061.     {"OpenVideo",        (PyCFunction)sv_OpenVideo},
  1062.     {NULL,            NULL}    /* Sentinel */
  1063. };
  1064.  
  1065. void
  1066. initsv()
  1067. {
  1068.     PyObject *m, *d;
  1069.  
  1070.     m = Py_InitModule("sv", sv_methods);
  1071.     d = PyModule_GetDict(m);
  1072.  
  1073.     SvError = PyErr_NewException("sv.error", NULL, NULL);
  1074.     if (SvError == NULL || PyDict_SetItemString(d, "error", SvError) != 0)
  1075.         return;
  1076. }
  1077.