1 | /*****************************************************************************************
|
---|
2 | * py_sail: A library that allows you to use SAIL from python (ie write SAGE apps with python)
|
---|
3 | *
|
---|
4 | * Copyright (C) 2007 Electronic Visualization Laboratory,
|
---|
5 | * University of Illinois at Chicago
|
---|
6 | *
|
---|
7 | * All rights reserved.
|
---|
8 | *
|
---|
9 | * Redistribution and use in source and binary forms, with or without
|
---|
10 | * modification, are permitted provided that the following conditions are met:
|
---|
11 | *
|
---|
12 | * * Redistributions of source code must retain the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer.
|
---|
14 | * * Redistributions in binary form must reproduce the above
|
---|
15 | * copyright notice, this list of conditions and the following disclaimer
|
---|
16 | * in the documentation and/or other materials provided with the distribution.
|
---|
17 | * * Neither the name of the University of Illinois at Chicago nor
|
---|
18 | * the names of its contributors may be used to endorse or promote
|
---|
19 | * products derived from this software without specific prior written permission.
|
---|
20 | *
|
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
22 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
24 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
---|
25 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
27 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
28 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
---|
29 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
---|
30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
32 | *
|
---|
33 | * Direct questions, comments etc about py_sail to www.evl.uic.edu/cavern/forum
|
---|
34 | *
|
---|
35 | * Author: Ratko Jagodic
|
---|
36 | *
|
---|
37 | *****************************************************************************************/
|
---|
38 |
|
---|
39 |
|
---|
40 | #include <Python.h>
|
---|
41 | #include <iostream>
|
---|
42 |
|
---|
43 | // sage headers
|
---|
44 | #include <sail.h>
|
---|
45 | #include <misc.h>
|
---|
46 |
|
---|
47 |
|
---|
48 |
|
---|
49 | using namespace std;
|
---|
50 |
|
---|
51 |
|
---|
52 | // SAGE Stuff
|
---|
53 | sail sageInf; // sail object
|
---|
54 | sailConfig scfg;
|
---|
55 | sageRect renderImageMap;
|
---|
56 | unsigned char *buffer = NULL;
|
---|
57 |
|
---|
58 |
|
---|
59 | // prototypes
|
---|
60 | static PyObject* initSail(PyObject *self, PyObject *args);
|
---|
61 | static PyObject* setBuffer(PyObject *self, PyObject *args);
|
---|
62 | static PyObject* checkMessages(PyObject *self, PyObject *args);
|
---|
63 | PyMODINIT_FUNC initpy_sail(void);
|
---|
64 |
|
---|
65 |
|
---|
66 |
|
---|
67 | //-----------------------------------------------------------------------
|
---|
68 |
|
---|
69 |
|
---|
70 |
|
---|
71 | // all the methods callable from Python have to be
|
---|
72 | // listed here
|
---|
73 | static PyMethodDef PySailMethods[] = {
|
---|
74 | {"initSail", initSail, METH_VARARGS,
|
---|
75 | "Initialize Sail Python interface."},
|
---|
76 |
|
---|
77 | {"setBuffer", setBuffer, METH_VARARGS,
|
---|
78 | "Receives the buffer from python."},
|
---|
79 |
|
---|
80 | {"checkMessages", checkMessages, METH_VARARGS,
|
---|
81 | "Checks for new sail messages from SAGE."},
|
---|
82 |
|
---|
83 | {NULL, NULL, 0, NULL} /* Sentinel */
|
---|
84 | };
|
---|
85 |
|
---|
86 |
|
---|
87 |
|
---|
88 | // this function is called when we do import py_sail from python
|
---|
89 | // it let's the python interpreter know about all the functions
|
---|
90 | // in this module available for calling
|
---|
91 | PyMODINIT_FUNC initpy_sail(void)
|
---|
92 | {
|
---|
93 | (void) Py_InitModule("py_sail", PySailMethods);
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 |
|
---|
98 | //-----------------------------------------------------------------------
|
---|
99 |
|
---|
100 |
|
---|
101 |
|
---|
102 | // initializes the sail object from python
|
---|
103 | static PyObject* initSail(PyObject *self, PyObject *args)
|
---|
104 | {
|
---|
105 | char *appName;
|
---|
106 | char confName[128];
|
---|
107 | int width, height;
|
---|
108 |
|
---|
109 | // parse the arguments
|
---|
110 | if (!PyArg_ParseTuple(args, "sii", &appName, &width, &height))
|
---|
111 | return NULL;
|
---|
112 |
|
---|
113 | // initialize sail and sage stuff
|
---|
114 | renderImageMap.left = 0.0;
|
---|
115 | renderImageMap.right = 1.0;
|
---|
116 | renderImageMap.bottom = 0.0;
|
---|
117 | renderImageMap.top = 1.0;
|
---|
118 | scfg.imageMap = renderImageMap;
|
---|
119 |
|
---|
120 | // the config name should be "appName.conf"
|
---|
121 | strcpy(confName, appName);
|
---|
122 | strcat(confName, ".conf");
|
---|
123 | fprintf(stderr, "\n\nSAIL CONFIG FILE BEING USED: %s", confName);
|
---|
124 |
|
---|
125 | scfg.init(confName);
|
---|
126 | scfg.setAppName(appName); // case sensitive
|
---|
127 | scfg.rank = 0;
|
---|
128 |
|
---|
129 | scfg.resX = width;
|
---|
130 | scfg.resY = height;
|
---|
131 |
|
---|
132 | scfg.pixFmt = PIXFMT_888;
|
---|
133 | scfg.rowOrd = TOP_TO_BOTTOM;
|
---|
134 | scfg.master = true;
|
---|
135 |
|
---|
136 | sageInf.init(scfg);
|
---|
137 | // return none
|
---|
138 | Py_INCREF(Py_None);
|
---|
139 | return Py_None;
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 |
|
---|
144 |
|
---|
145 | // receives the buffer and swaps it via sail object
|
---|
146 | static PyObject* setBuffer(PyObject *self, PyObject *args)
|
---|
147 | {
|
---|
148 | int bufSize;
|
---|
149 | unsigned char *pybuf = NULL;
|
---|
150 |
|
---|
151 | // parse the arguments
|
---|
152 | if (!PyArg_ParseTuple(args, "s#", &pybuf, &bufSize))
|
---|
153 | {
|
---|
154 | cout << "py_sail.so: ParseTuple failed..." << endl;
|
---|
155 | return NULL;
|
---|
156 | }
|
---|
157 |
|
---|
158 | // if something went wrong so just return None
|
---|
159 | if (pybuf == NULL)
|
---|
160 | {
|
---|
161 | cout << "\n\npy_sail.so: Buffer received from Python is NULL... returning without swapBuffer\n\n";
|
---|
162 | Py_INCREF(Py_None);
|
---|
163 | return Py_None;
|
---|
164 | }
|
---|
165 |
|
---|
166 | // finally, swap the buffer via sail
|
---|
167 | buffer = (unsigned char *) sageInf.getBuffer(); // get the new buffer from sage first
|
---|
168 | memcpy(buffer, pybuf, scfg.resX*scfg.resY*3);
|
---|
169 | sageInf.swapBuffer();
|
---|
170 |
|
---|
171 | // this function returns None
|
---|
172 | Py_INCREF(Py_None);
|
---|
173 | return Py_None;
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 |
|
---|
178 | /* checks for messages sent from SAGE
|
---|
179 | defined messages and their return values (to the python program):
|
---|
180 |
|
---|
181 | APP_QUIT -1
|
---|
182 |
|
---|
183 | */
|
---|
184 | static PyObject* checkMessages(PyObject *self, PyObject *args)
|
---|
185 | {
|
---|
186 | sageMessage msg;
|
---|
187 | int returnVal = 0;
|
---|
188 | if (sageInf.checkMsg(msg, false) > 0)
|
---|
189 | {
|
---|
190 | switch (msg.getCode()) {
|
---|
191 | case APP_QUIT : {
|
---|
192 | returnVal = -1;
|
---|
193 | break;
|
---|
194 | }
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | if (returnVal == 0)
|
---|
199 | {
|
---|
200 | Py_INCREF(Py_None);
|
---|
201 | return Py_None;
|
---|
202 | }
|
---|
203 | else
|
---|
204 | return Py_BuildValue("i", returnVal);
|
---|
205 | }
|
---|