Ditched '_find_SET()', since it was a no-value-added wrapper around
[python/dscho.git] / Modules / svmodule.c
blob75f20238e412499c590a4dd1e6c71459efbb385a
1 /**********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
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.
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.
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.
30 ******************************************************************/
32 /* SV module -- interface to the Indigo video board */
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! */
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 */
45 typedef struct {
46 PyObject_HEAD
47 SV_nodeP ob_svideo;
48 svCaptureInfo ob_info;
49 } svobject;
51 typedef struct {
52 PyObject_HEAD
53 void *ob_capture;
54 int ob_mustunlock;
55 svCaptureInfo ob_info;
56 svobject *ob_svideo;
57 } captureobject;
59 static PyObject *SvError; /* exception sv.error */
61 static PyObject *newcaptureobject Py_PROTO((svobject *, void *, int));
63 /* Set a SV-specific error from svideo_errno and return NULL */
64 static PyObject *
65 sv_error()
67 PyErr_SetString(SvError, svStrerror(svideo_errno));
68 return NULL;
71 static PyObject *
72 svc_conversion(self, args, function, factor)
73 captureobject *self;
74 PyObject *args;
75 void (*function)();
76 float factor;
78 PyObject *output;
79 int invert;
80 char* outstr;
82 if (!PyArg_Parse(args, "i", &invert))
83 return NULL;
85 if (!(output = PyString_FromStringAndSize(
86 NULL,
87 (int)(self->ob_info.width * self->ob_info.height * factor))))
89 return NULL;
91 if (!(outstr = PyString_AsString(output))) {
92 Py_DECREF(output);
93 return NULL;
96 (*function)((boolean)invert, self->ob_capture,
97 outstr,
98 self->ob_info.width, self->ob_info.height);
100 return output;
104 * 3 functions to convert from Starter Video YUV 4:1:1 format to
105 * Compression Library 4:2:2 Duplicate Chroma format.
107 static PyObject *
108 svc_YUVtoYUV422DC(self, args)
109 captureobject *self;
110 PyObject *args;
112 if (self->ob_info.format != SV_YUV411_FRAMES) {
113 PyErr_SetString(SvError, "data has bad format");
114 return NULL;
116 return svc_conversion(self, args, yuv_sv411_to_cl422dc, 2.0);
119 static PyObject *
120 svc_YUVtoYUV422DC_quarter(self, args)
121 captureobject *self;
122 PyObject *args;
124 if (self->ob_info.format != SV_YUV411_FRAMES) {
125 PyErr_SetString(SvError, "data has bad format");
126 return NULL;
128 return svc_conversion(self, args,
129 yuv_sv411_to_cl422dc_quartersize, 0.5);
132 static PyObject *
133 svc_YUVtoYUV422DC_sixteenth(self, args)
134 captureobject *self;
135 PyObject *args;
137 if (self->ob_info.format != SV_YUV411_FRAMES) {
138 PyErr_SetString(SvError, "data has bad format");
139 return NULL;
141 return svc_conversion(self, args,
142 yuv_sv411_to_cl422dc_sixteenthsize, 0.125);
145 static PyObject *
146 svc_YUVtoRGB(self, args)
147 captureobject *self;
148 PyObject *args;
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;
158 return svc_conversion(self, args, svYUVtoRGB, (float) sizeof(long));
161 static PyObject *
162 svc_RGB8toRGB32(self, args)
163 captureobject *self;
164 PyObject *args;
166 if (self->ob_info.format != SV_RGB8_FRAMES) {
167 PyErr_SetString(SvError, "data has bad format");
168 return NULL;
170 return svc_conversion(self, args, svRGB8toRGB32, (float) sizeof(long));
173 static PyObject *
174 svc_InterleaveFields(self, args)
175 captureobject *self;
176 PyObject *args;
178 if (self->ob_info.format != SV_RGB8_FRAMES) {
179 PyErr_SetString(SvError, "data has bad format");
180 return NULL;
182 return svc_conversion(self, args, svInterleaveFields, 1.0);
185 static PyObject *
186 svc_GetFields(self, args)
187 captureobject *self;
188 PyObject *args;
190 PyObject *f1 = NULL;
191 PyObject *f2 = NULL;
192 PyObject *ret = NULL;
193 int fieldsize;
194 char* obcapture;
196 if (self->ob_info.format != SV_RGB8_FRAMES) {
197 PyErr_SetString(SvError, "data has bad format");
198 return NULL;
201 fieldsize = self->ob_info.width * self->ob_info.height / 2;
202 obcapture = (char*)self->ob_capture;
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);
211 finally:
212 Py_XDECREF(f1);
213 Py_XDECREF(f2);
214 return ret;
217 static PyObject *
218 svc_UnlockCaptureData(self, args)
219 captureobject *self;
220 PyObject *args;
222 if (!PyArg_Parse(args, ""))
223 return NULL;
225 if (!self->ob_mustunlock) {
226 PyErr_SetString(SvError, "buffer should not be unlocked");
227 return NULL;
230 if (svUnlockCaptureData(self->ob_svideo->ob_svideo, self->ob_capture))
231 return sv_error();
233 self->ob_mustunlock = 0;
235 Py_INCREF(Py_None);
236 return Py_None;
239 #ifdef USE_GL
240 #include <gl.h>
242 static PyObject *
243 svc_lrectwrite(self, args)
244 captureobject *self;
245 PyObject *args;
247 Screencoord x1, x2, y1, y2;
249 if (!PyArg_Parse(args, "(hhhh)", &x1, &x2, &y1, &y2))
250 return NULL;
252 lrectwrite(x1, x2, y1, y2, (unsigned long *) self->ob_capture);
254 Py_INCREF(Py_None);
255 return Py_None;
257 #endif
259 static PyObject *
260 svc_writefile(self, args)
261 captureobject *self;
262 PyObject *args;
264 PyObject *file;
265 int size;
266 FILE* fp;
268 if (!PyArg_Parse(args, "O", &file))
269 return NULL;
271 if (!PyFile_Check(file)) {
272 PyErr_SetString(SvError, "not a file object");
273 return NULL;
276 if (!(fp = PyFile_AsFile(file)))
277 return NULL;
279 size = self->ob_info.width * self->ob_info.height;
281 if (fwrite(self->ob_capture, sizeof(long), size, fp) != size) {
282 PyErr_SetString(SvError, "writing failed");
283 return NULL;
286 Py_INCREF(Py_None);
287 return Py_None;
290 static PyObject *
291 svc_FindVisibleRegion(self, args)
292 captureobject *self;
293 PyObject *args;
295 void *visible;
296 int width;
298 if (!PyArg_Parse(args, ""))
299 return NULL;
301 if (svFindVisibleRegion(self->ob_svideo->ob_svideo,
302 self->ob_capture, &visible,
303 self->ob_info.width))
304 return sv_error();
306 if (visible == NULL) {
307 PyErr_SetString(SvError, "data in wrong format");
308 return NULL;
311 return newcaptureobject(self->ob_svideo, visible, 0);
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 */
331 static void
332 capture_dealloc(self)
333 captureobject *self;
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;
343 PyMem_DEL(self);
346 static PyObject *
347 capture_getattr(self, name)
348 svobject *self;
349 char *name;
351 return Py_FindMethod(capture_methods, (PyObject *)self, name);
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*/
369 static PyObject *
370 newcaptureobject(self, ptr, mustunlock)
371 svobject *self;
372 void *ptr;
373 int mustunlock;
375 captureobject *p;
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;
388 static PyObject *
389 sv_GetCaptureData(self, args)
390 svobject *self;
391 PyObject *args;
393 void *ptr;
394 long fieldID;
395 PyObject *res, *c;
397 if (!PyArg_Parse(args, ""))
398 return NULL;
400 if (svGetCaptureData(self->ob_svideo, &ptr, &fieldID))
401 return sv_error();
403 if (ptr == NULL) {
404 PyErr_SetString(SvError, "no data available");
405 return NULL;
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;
416 static PyObject *
417 sv_BindGLWindow(self, args)
418 svobject *self;
419 PyObject *args;
421 long wid;
422 int mode;
424 if (!PyArg_Parse(args, "(ii)", &wid, &mode))
425 return NULL;
427 if (svBindGLWindow(self->ob_svideo, wid, mode))
428 return sv_error();
430 Py_INCREF(Py_None);
431 return Py_None;
434 static PyObject *
435 sv_EndContinuousCapture(self, args)
436 svobject *self;
437 PyObject *args;
440 if (!PyArg_Parse(args, ""))
441 return NULL;
443 if (svEndContinuousCapture(self->ob_svideo))
444 return sv_error();
446 Py_INCREF(Py_None);
447 return Py_None;
450 static PyObject *
451 sv_IsVideoDisplayed(self, args)
452 svobject *self;
453 PyObject *args;
455 int v;
457 if (!PyArg_Parse(args, ""))
458 return NULL;
460 v = svIsVideoDisplayed(self->ob_svideo);
461 if (v == -1)
462 return sv_error();
464 return PyInt_FromLong((long) v);
467 static PyObject *
468 sv_OutputOffset(self, args)
469 svobject *self;
470 PyObject *args;
472 int x_offset;
473 int y_offset;
475 if (!PyArg_Parse(args, "(ii)", &x_offset, &y_offset))
476 return NULL;
478 if (svOutputOffset(self->ob_svideo, x_offset, y_offset))
479 return sv_error();
481 Py_INCREF(Py_None);
482 return Py_None;
485 static PyObject *
486 sv_PutFrame(self, args)
487 svobject *self;
488 PyObject *args;
490 char *buffer;
492 if (!PyArg_Parse(args, "s", &buffer))
493 return NULL;
495 if (svPutFrame(self->ob_svideo, buffer))
496 return sv_error();
498 Py_INCREF(Py_None);
499 return Py_None;
502 static PyObject *
503 sv_QuerySize(self, args)
504 svobject *self;
505 PyObject *args;
507 int w;
508 int h;
509 int rw;
510 int rh;
512 if (!PyArg_Parse(args, "(ii)", &w, &h))
513 return NULL;
515 if (svQuerySize(self->ob_svideo, w, h, &rw, &rh))
516 return sv_error();
518 return Py_BuildValue("(ii)", (long) rw, (long) rh);
521 static PyObject *
522 sv_SetSize(self, args)
523 svobject *self;
524 PyObject *args;
526 int w;
527 int h;
529 if (!PyArg_Parse(args, "(ii)", &w, &h))
530 return NULL;
532 if (svSetSize(self->ob_svideo, w, h))
533 return sv_error();
535 Py_INCREF(Py_None);
536 return Py_None;
539 static PyObject *
540 sv_SetStdDefaults(self, args)
541 svobject *self;
542 PyObject *args;
545 if (!PyArg_Parse(args, ""))
546 return NULL;
548 if (svSetStdDefaults(self->ob_svideo))
549 return sv_error();
551 Py_INCREF(Py_None);
552 return Py_None;
555 static PyObject *
556 sv_UseExclusive(self, args)
557 svobject *self;
558 PyObject *args;
560 boolean onoff;
561 int mode;
563 if (!PyArg_Parse(args, "(ii)", &onoff, &mode))
564 return NULL;
566 if (svUseExclusive(self->ob_svideo, onoff, mode))
567 return sv_error();
569 Py_INCREF(Py_None);
570 return Py_None;
573 static PyObject *
574 sv_WindowOffset(self, args)
575 svobject *self;
576 PyObject *args;
578 int x_offset;
579 int y_offset;
581 if (!PyArg_Parse(args, "(ii)", &x_offset, &y_offset))
582 return NULL;
584 if (svWindowOffset(self->ob_svideo, x_offset, y_offset))
585 return sv_error();
587 Py_INCREF(Py_None);
588 return Py_None;
591 static PyObject *
592 sv_CaptureBurst(self, args)
593 svobject *self;
594 PyObject *args;
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;
604 if (!PyArg_Parse(args, "(iiiii)", &info.format,
605 &info.width, &info.height,
606 &info.size, &info.samplingrate))
607 return NULL;
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;
620 if (svQueryCaptureBufferSize(self->ob_svideo, &info, &bytes)) {
621 res = sv_error();
622 goto finally;
625 if (!(videodata = PyString_FromStringAndSize(NULL, bytes)))
626 goto finally;
628 /* XXX -- need to do something about the bitvector */
630 char* str = PyString_AsString(videodata);
631 if (!str)
632 goto finally;
634 if (svCaptureBurst(self->ob_svideo, &info, str, bitvector)) {
635 res = sv_error();
636 goto finally;
640 if (bitvector) {
641 if (evenitem == NULL) {
642 if (!(evenitem = PyInt_FromLong(0)))
643 goto finally;
645 if (odditem == NULL) {
646 if (!(odditem = PyInt_FromLong(1)))
647 goto finally;
649 if (!(bitvecobj = PyTuple_New(2 * info.size)))
650 goto finally;
652 for (i = 0; i < 2 * info.size; i++) {
653 int sts;
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);
662 if (sts < 0)
663 goto finally;
665 } else {
666 bitvecobj = Py_None;
667 Py_INCREF(Py_None);
670 res = Py_BuildValue("((iiiii)OO)", info.format,
671 info.width, info.height,
672 info.size, info.samplingrate,
673 videodata, bitvecobj);
675 finally:
676 if (bitvector)
677 free(bitvector);
679 Py_XDECREF(videodata);
680 Py_XDECREF(bitvecobj);
681 return res;
684 static PyObject *
685 sv_CaptureOneFrame(self, args)
686 svobject *self;
687 PyObject *args;
689 svCaptureInfo info;
690 int format, width, height;
691 int bytes;
692 PyObject *videodata = NULL;
693 PyObject *res = NULL;
694 char *str;
696 if (!PyArg_Parse(args, "(iii)", &format, &width, &height))
697 return NULL;
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();
707 if (!(videodata = PyString_FromStringAndSize(NULL, bytes)))
708 return NULL;
710 str = PyString_AsString(videodata);
711 if (!str)
712 goto finally;
714 if (svCaptureOneFrame(self->ob_svideo, format, &width, &height, str)) {
715 res = sv_error();
716 goto finally;
719 res = Py_BuildValue("(iiO)", width, height, videodata);
721 finally:
722 Py_XDECREF(videodata);
723 return res;
726 static PyObject *
727 sv_InitContinuousCapture(self, args)
728 svobject *self;
729 PyObject *args;
731 svCaptureInfo info;
733 if (!PyArg_Parse(args, "(iiiii)", &info.format,
734 &info.width, &info.height,
735 &info.size, &info.samplingrate))
736 return NULL;
738 if (svInitContinuousCapture(self->ob_svideo, &info))
739 return sv_error();
741 self->ob_info = info;
743 return Py_BuildValue("(iiiii)", info.format, info.width, info.height,
744 info.size, info.samplingrate);
747 static PyObject *
748 sv_LoadMap(self, args)
749 svobject *self;
750 PyObject *args;
752 PyObject *rgb;
753 PyObject *res = NULL;
754 rgb_tuple *mapp = NULL;
755 int maptype;
756 int i, j; /* indices */
758 if (!PyArg_Parse(args, "(iO)", &maptype, &rgb))
759 return NULL;
761 if (!PyList_Check(rgb) || PyList_Size(rgb) != 256) {
762 PyErr_BadArgument();
763 return NULL;
766 if (!(mapp = PyMem_NEW(rgb_tuple, 256)))
767 return PyErr_NoMemory();
769 for (i = 0; i < 256; i++) {
770 PyObject* v = PyList_GetItem(rgb, i);
771 if (!v)
772 goto finally;
774 if (!PyTuple_Check(v) || PyTuple_Size(v) != 3) {
775 PyErr_BadArgument();
776 goto finally;
778 for (j = 0; j < 3; j++) {
779 PyObject* cell = PyTuple_GetItem(v, j);
780 if (!cell)
781 goto finally;
783 if (!PyInt_Check(cell)) {
784 PyErr_BadArgument();
785 goto finally;
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;
792 if (PyErr_Occurred())
793 goto finally;
797 if (svLoadMap(self->ob_svideo, maptype, mapp)) {
798 res = sv_error();
799 goto finally;
802 Py_INCREF(Py_None);
803 res = Py_None;
805 finally:
806 PyMem_DEL(mapp);
807 return res;
810 static PyObject *
811 sv_CloseVideo(self, args)
812 svobject *self;
813 PyObject *args;
815 if (!PyArg_Parse(args, ""))
816 return NULL;
818 if (svCloseVideo(self->ob_svideo))
819 return sv_error();
821 self->ob_svideo = NULL;
822 Py_INCREF(Py_None);
823 return Py_None;
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;
833 PyObject *list;
834 PyObject *res = NULL;
835 long *PVbuffer = NULL;
836 long length;
837 int i;
839 if (!PyArg_Parse(args, "O", &list))
840 return NULL;
842 if (!PyList_Check(list)) {
843 PyErr_BadArgument();
844 return NULL;
847 if ((length = PyList_Size(list)) < 0)
848 return NULL;
850 PVbuffer = PyMem_NEW(long, length);
851 if (PVbuffer == NULL)
852 return PyErr_NoMemory();
854 for (i = 0; i < length; i++) {
855 PyObject *v = PyList_GetItem(list, i);
856 if (!v)
857 goto finally;
859 if (!PyInt_Check(v)) {
860 PyErr_BadArgument();
861 goto finally;
863 PVbuffer[i] = PyInt_AsLong(v);
864 /* can't just test the return value, because what if the
865 value was -1?!
867 if (PVbuffer[i] == -1 && PyErr_Occurred())
868 goto finally;
871 if ((*func)(self->ob_svideo, PVbuffer, length)) {
872 res = sv_error();
873 goto finally;
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;
884 Py_INCREF(Py_None);
885 res = Py_None;
887 finally:
888 PyMem_DEL(PVbuffer);
889 return res;
892 static PyObject *
893 sv_GetParam(self, args)
894 PyObject *self, *args;
896 return doParams(self, args, svGetParam, 1);
899 static PyObject *
900 sv_GetParamRange(self, args)
901 PyObject *self, *args;
903 return doParams(self, args, svGetParamRange, 1);
906 static PyObject *
907 sv_SetParam(self, args)
908 PyObject *self, *args;
910 return doParams(self, args, svSetParam, 0);
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 */
936 static PyObject *
937 sv_conversion(self, args, function, inputfactor, factor)
938 PyObject *self, *args;
939 void (*function)();
940 int inputfactor;
941 float factor;
943 int invert, width, height, inputlength;
944 char *input, *str;
945 PyObject *output;
947 if (!PyArg_Parse(args, "(is#ii)", &invert,
948 &input, &inputlength, &width, &height))
949 return NULL;
951 if (width * height * inputfactor > inputlength) {
952 PyErr_SetString(SvError, "input buffer not long enough");
953 return NULL;
956 if (!(output = PyString_FromStringAndSize(NULL,
957 (int)(width * height * factor))))
958 return NULL;
960 str = PyString_AsString(output);
961 if (!str) {
962 Py_DECREF(output);
963 return NULL;
965 (*function)(invert, input, str, width, height);
967 return output;
970 static PyObject *
971 sv_InterleaveFields(self, args)
972 PyObject *self, *args;
974 return sv_conversion(self, args, svInterleaveFields, 1, 1.0);
977 static PyObject *
978 sv_RGB8toRGB32(self, args)
979 PyObject *self, *args;
981 return sv_conversion(self, args, svRGB8toRGB32, 1, (float) sizeof(long));
984 static PyObject *
985 sv_YUVtoRGB(self, args)
986 PyObject *self, *args;
988 return sv_conversion(self, args, svYUVtoRGB, 2, (float) sizeof(long));
991 static void
992 svideo_dealloc(self)
993 svobject *self;
995 if (self->ob_svideo != NULL)
996 (void) svCloseVideo(self->ob_svideo);
997 PyMem_DEL(self);
1000 static PyObject *
1001 svideo_getattr(self, name)
1002 svobject *self;
1003 char *name;
1005 return Py_FindMethod(svideo_methods, (PyObject *)self, name);
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*/
1023 static PyObject *
1024 newsvobject(svp)
1025 SV_nodeP svp;
1027 svobject *p;
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;
1041 static PyObject *
1042 sv_OpenVideo(self, args)
1043 PyObject *self, *args;
1045 SV_nodeP svp;
1047 if (!PyArg_Parse(args, ""))
1048 return NULL;
1050 svp = svOpenVideo();
1051 if (svp == NULL)
1052 return sv_error();
1054 return newsvobject(svp);
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 */
1065 void
1066 initsv()
1068 PyObject *m, *d;
1070 m = Py_InitModule("sv", sv_methods);
1071 d = PyModule_GetDict(m);
1073 SvError = PyErr_NewException("sv.error", NULL, NULL);
1074 if (SvError == NULL || PyDict_SetItemString(d, "error", SvError) != 0)
1075 return;