Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The |
| 3 | 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 not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
| 25 | |
| 26 | /* Cl objects */ |
| 27 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 28 | #include <stdarg.h> |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 29 | #include <cl.h> |
| 30 | #include "allobjects.h" |
| 31 | #include "modsupport.h" /* For getargs() etc. */ |
| 32 | #include "ceval.h" /* For call_object() */ |
| 33 | |
| 34 | typedef struct { |
| 35 | OB_HEAD |
| 36 | int ob_isCompressor; /* Compressor or Decompressor */ |
| 37 | CL_CompressorHdl ob_compressorHdl; |
| 38 | long ob_dataMaxSize; |
| 39 | object *ob_callbackFunc; |
| 40 | object *ob_callbackID; |
| 41 | object *ob_data; |
| 42 | } clobject; |
| 43 | |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 44 | #define CheckCompressor(self) if ((self)->ob_compressorHdl == NULL) { \ |
| 45 | err_setstr(RuntimeError, "(de)compressor not active"); \ |
| 46 | return NULL; \ |
| 47 | } |
| 48 | |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 49 | extern typeobject Cltype; /* Really static, forward */ |
| 50 | |
| 51 | #define is_clobject(v) ((v)->ob_type == &Cltype) |
| 52 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 53 | static object *ClError; /* exception cl.error */ |
| 54 | |
| 55 | static int error_handler_called = 0; |
| 56 | |
| 57 | static void |
| 58 | cl_ErrorHandler(long errnum, const char *fmt, ...) |
| 59 | { |
| 60 | va_list ap; |
| 61 | char errbuf[BUFSIZ]; /* hopefully big enough */ |
| 62 | |
| 63 | error_handler_called = 1; |
| 64 | va_start(ap, fmt); |
| 65 | vsprintf(errbuf, fmt, ap); |
| 66 | va_end(ap); |
| 67 | err_setstr(ClError, errbuf); |
| 68 | } |
| 69 | |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 70 | static object * |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 71 | cl_CloseCompressor(self, args) |
| 72 | clobject *self; |
| 73 | object *args; |
| 74 | { |
| 75 | CheckCompressor(self); |
| 76 | |
| 77 | if (!getnoarg(args)) |
| 78 | return NULL; |
| 79 | |
| 80 | if (clCloseCompressor(self->ob_compressorHdl) == FAILURE) { |
| 81 | if (!error_handler_called) |
| 82 | err_setstr(ClError, "CloseCompressor failed"); |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | self->ob_compressorHdl = NULL; |
| 87 | |
| 88 | INCREF(None); |
| 89 | return None; |
| 90 | } |
| 91 | |
| 92 | static object * |
| 93 | cl_CloseDecompressor(self, args) |
| 94 | clobject *self; |
| 95 | object *args; |
| 96 | { |
| 97 | CheckCompressor(self); |
| 98 | |
| 99 | if (!getnoarg(args)) |
| 100 | return NULL; |
| 101 | |
| 102 | if (clCloseDecompressor(self->ob_compressorHdl) == FAILURE) { |
| 103 | if (!error_handler_called) |
| 104 | err_setstr(ClError, "CloseDecompressor failed"); |
| 105 | return NULL; |
| 106 | } |
| 107 | |
| 108 | self->ob_compressorHdl = NULL; |
| 109 | |
| 110 | INCREF(None); |
| 111 | return None; |
| 112 | } |
| 113 | |
| 114 | static object * |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 115 | cl_Compress(self, args) |
| 116 | clobject *self; |
| 117 | object *args; |
| 118 | { |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 119 | object *data; |
| 120 | long frameIndex, numberOfFrames, dataSize; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 121 | |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 122 | CheckCompressor(self); |
| 123 | |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 124 | if (!getargs(args, "(ii)", &frameIndex, &numberOfFrames)) |
| 125 | return NULL; |
| 126 | |
| 127 | data = newsizedstringobject(NULL, |
| 128 | numberOfFrames * self->ob_dataMaxSize); |
| 129 | if (data == NULL) |
| 130 | return NULL; |
| 131 | |
| 132 | dataSize = numberOfFrames * self->ob_dataMaxSize; |
| 133 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 134 | error_handler_called = 0; |
| 135 | if (clCompress(self->ob_compressorHdl, frameIndex, numberOfFrames, |
| 136 | &dataSize, (void *) getstringvalue(data)) == FAILURE) { |
| 137 | DECREF(data); |
| 138 | if (!error_handler_called) |
| 139 | err_setstr(ClError, "compress failed"); |
| 140 | return NULL; |
| 141 | } |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 142 | |
| 143 | if (dataSize < numberOfFrames * self->ob_dataMaxSize) |
| 144 | if (resizestring(&data, dataSize)) |
| 145 | return NULL; |
| 146 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 147 | return data; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static object * |
| 151 | cl_Decompress(self, args) |
| 152 | clobject *self; |
| 153 | object *args; |
| 154 | { |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 155 | object *data; |
| 156 | long frameIndex, numberOfFrames; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 157 | |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 158 | CheckCompressor(self); |
| 159 | |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 160 | if (!getargs(args, "(ii)", &frameIndex, &numberOfFrames)) |
| 161 | return NULL; |
| 162 | |
| 163 | data = newsizedstringobject(NULL, |
| 164 | numberOfFrames * self->ob_dataMaxSize); |
| 165 | if (data == NULL) |
| 166 | return NULL; |
| 167 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 168 | error_handler_called = 0; |
| 169 | if (clDecompress(self->ob_compressorHdl, frameIndex, numberOfFrames, |
| 170 | (void *) getstringvalue(data)) == FAILURE) { |
| 171 | DECREF(data); |
| 172 | if (!error_handler_called) |
| 173 | err_setstr(ClError, "decompress failed"); |
| 174 | return NULL; |
| 175 | } |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 176 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 177 | return data; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static object * |
| 181 | cl_GetCompressorInfo(self, args) |
| 182 | clobject *self; |
| 183 | object *args; |
| 184 | { |
| 185 | long result, infoSize; |
| 186 | void *info; |
| 187 | object *infoObject, *res; |
| 188 | |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 189 | CheckCompressor(self); |
| 190 | |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 191 | if (!getnoarg(args)) |
| 192 | return NULL; |
| 193 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 194 | error_handler_called = 0; |
| 195 | if (clGetCompressorInfo(self->ob_compressorHdl, &infoSize, &info) == FAILURE) { |
| 196 | if (!error_handler_called) |
| 197 | err_setstr(ClError, "getcompressorinfo failed"); |
| 198 | return NULL; |
| 199 | } |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 200 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 201 | return newsizedstringobject((char *) info, infoSize); |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static object * |
| 205 | cl_GetDefault(self, args) |
| 206 | clobject *self; |
| 207 | object *args; |
| 208 | { |
| 209 | long initial, result; |
| 210 | |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 211 | CheckCompressor(self); |
| 212 | |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 213 | if (!getargs(args, "i", &initial)) |
| 214 | return NULL; |
| 215 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 216 | error_handler_called = 0; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 217 | result = clGetDefault(self->ob_compressorHdl, initial); |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 218 | if (error_handler_called) |
| 219 | return NULL; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 220 | |
| 221 | return newintobject(result); |
| 222 | } |
| 223 | |
| 224 | static object * |
| 225 | cl_GetMinMax(self, args) |
| 226 | clobject *self; |
| 227 | object *args; |
| 228 | { |
| 229 | long param, min, max; |
| 230 | |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 231 | CheckCompressor(self); |
| 232 | |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 233 | if (!getargs(args, "i", ¶m)) |
| 234 | return NULL; |
| 235 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 236 | error_handler_called = 0; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 237 | clGetMinMax(self->ob_compressorHdl, param, &min, &max); |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 238 | if (error_handler_called) |
| 239 | return NULL; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 240 | |
| 241 | return mkvalue("(ii)", min, max); |
| 242 | } |
| 243 | |
| 244 | static object * |
| 245 | cl_GetName(self, args) |
| 246 | clobject *self; |
| 247 | object *args; |
| 248 | { |
| 249 | long descriptor; |
| 250 | char *name; |
| 251 | |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 252 | CheckCompressor(self); |
| 253 | |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 254 | if (!getargs(args, "i", &descriptor)) |
| 255 | return NULL; |
| 256 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 257 | error_handler_called = 0; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 258 | name = clGetName(self->ob_compressorHdl, descriptor); |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 259 | if (error_handler_called) |
| 260 | return NULL; |
| 261 | if (name == NULL) { |
| 262 | err_setstr(ClError, "getname failed"); |
| 263 | return NULL; |
| 264 | } |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 265 | |
| 266 | return newstringobject(name); |
| 267 | } |
| 268 | |
| 269 | static object * |
| 270 | doParams(self, args, func, modified) |
| 271 | clobject *self; |
| 272 | object *args; |
| 273 | void (*func)(CL_CompressorHdl, long *, long); |
| 274 | int modified; |
| 275 | { |
| 276 | object *list, *v; |
| 277 | long *PVbuffer; |
| 278 | long length; |
| 279 | int i; |
| 280 | |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 281 | CheckCompressor(self); |
| 282 | |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 283 | if (!getargs(args, "O", &list)) |
| 284 | return NULL; |
| 285 | if (!is_listobject(list)) { |
| 286 | err_badarg(); |
| 287 | return NULL; |
| 288 | } |
| 289 | length = getlistsize(list); |
| 290 | PVbuffer = NEW(long, length); |
| 291 | if (PVbuffer == NULL) |
| 292 | return err_nomem(); |
| 293 | for (i = 0; i < length; i++) { |
| 294 | v = getlistitem(list, i); |
| 295 | if (!is_intobject(v)) { |
| 296 | DEL(PVbuffer); |
| 297 | err_badarg(); |
| 298 | return NULL; |
| 299 | } |
| 300 | PVbuffer[i] = getintvalue(v); |
| 301 | } |
| 302 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 303 | error_handler_called = 0; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 304 | (*func)(self->ob_compressorHdl, PVbuffer, length); |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 305 | if (error_handler_called) |
| 306 | return NULL; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 307 | |
| 308 | if (modified) { |
| 309 | for (i = 0; i < length; i++) |
| 310 | setlistitem(list, i, newintobject(PVbuffer[i])); |
| 311 | } |
| 312 | |
| 313 | DEL(PVbuffer); |
| 314 | |
| 315 | INCREF(None); |
| 316 | return None; |
| 317 | } |
| 318 | |
| 319 | static object * |
| 320 | cl_GetParams(self, args) |
| 321 | object *self, *args; |
| 322 | { |
| 323 | return doParams(self, args, clGetParams, 1); |
| 324 | } |
| 325 | |
| 326 | static object * |
| 327 | cl_SetParams(self, args) |
| 328 | object *self, *args; |
| 329 | { |
| 330 | return doParams(self, args, clSetParams, 0); |
| 331 | } |
| 332 | |
| 333 | static object * |
| 334 | cl_QueryParams(self, args) |
| 335 | clobject *self; |
| 336 | object *args; |
| 337 | { |
| 338 | long bufferlength; |
| 339 | long *PVbuffer; |
| 340 | object *list; |
| 341 | int i; |
| 342 | |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 343 | CheckCompressor(self); |
| 344 | |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 345 | if (!getnoarg(args)) |
| 346 | return NULL; |
| 347 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 348 | error_handler_called = 0; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 349 | bufferlength = clQueryParams(self->ob_compressorHdl, 0, 0); |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 350 | if (error_handler_called) |
| 351 | return NULL; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 352 | |
| 353 | PVbuffer = NEW(long, bufferlength); |
| 354 | if (PVbuffer == NULL) |
| 355 | return err_nomem(); |
| 356 | |
| 357 | bufferlength = clQueryParams(self->ob_compressorHdl, PVbuffer, |
| 358 | bufferlength); |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 359 | if (error_handler_called) { |
| 360 | DEL(PVbuffer); |
| 361 | return NULL; |
| 362 | } |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 363 | |
| 364 | list = newlistobject(bufferlength); |
| 365 | if (list == NULL) { |
| 366 | DEL(PVbuffer); |
| 367 | return NULL; |
| 368 | } |
| 369 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 370 | for (i = 0; i < bufferlength; i++) { |
| 371 | if (i & 1) |
| 372 | setlistitem(list, i, newintobject(PVbuffer[i])); |
| 373 | else |
| 374 | setlistitem(list, i, newstringobject((char *) PVbuffer[i])); |
| 375 | } |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 376 | |
| 377 | DEL(PVbuffer); |
| 378 | |
| 379 | return list; |
| 380 | } |
| 381 | |
| 382 | static struct methodlist compressor_methods[] = { |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 383 | {"CloseCompressor", cl_CloseCompressor}, |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 384 | {"Compress", cl_Compress}, |
| 385 | {"GetCompressorInfo", cl_GetCompressorInfo}, |
| 386 | {"GetDefault", cl_GetDefault}, |
| 387 | {"GetMinMax", cl_GetMinMax}, |
| 388 | {"GetName", cl_GetName}, |
| 389 | {"GetParams", cl_GetParams}, |
| 390 | {"QueryParams", cl_QueryParams}, |
| 391 | {"SetParams", cl_SetParams}, |
| 392 | {NULL, NULL} /* sentinel */ |
| 393 | }; |
| 394 | |
| 395 | static struct methodlist decompressor_methods[] = { |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 396 | {"CloseDecompressor", cl_CloseDecompressor}, |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 397 | {"Decompress", cl_Decompress}, |
| 398 | {"GetDefault", cl_GetDefault}, |
| 399 | {"GetMinMax", cl_GetMinMax}, |
| 400 | {"GetName", cl_GetName}, |
| 401 | {"GetParams", cl_GetParams}, |
| 402 | {"QueryParams", cl_QueryParams}, |
| 403 | {"SetParams", cl_SetParams}, |
| 404 | {NULL, NULL} /* sentinel */ |
| 405 | }; |
| 406 | |
| 407 | static void |
| 408 | cl_dealloc(self) |
| 409 | clobject *self; |
| 410 | { |
| 411 | if (self->ob_compressorHdl) { |
| 412 | if (self->ob_isCompressor) |
| 413 | clCloseCompressor(self->ob_compressorHdl); |
| 414 | else |
| 415 | clCloseDecompressor(self->ob_compressorHdl); |
| 416 | } |
| 417 | XDECREF(self->ob_callbackFunc); |
| 418 | XDECREF(self->ob_callbackID); |
| 419 | XDECREF(self->ob_data); |
| 420 | DEL(self); |
| 421 | } |
| 422 | |
| 423 | static object * |
| 424 | cl_getattr(self, name) |
| 425 | clobject *self; |
| 426 | char *name; |
| 427 | { |
| 428 | if (self->ob_isCompressor) |
| 429 | return findmethod(compressor_methods, (object *)self, name); |
| 430 | else |
| 431 | return findmethod(decompressor_methods, (object *) self, name); |
| 432 | } |
| 433 | |
| 434 | static typeobject Cltype = { |
| 435 | OB_HEAD_INIT(&Typetype) |
| 436 | 0, /*ob_size*/ |
| 437 | "cl", /*tp_name*/ |
| 438 | sizeof(clobject), /*tp_size*/ |
| 439 | 0, /*tp_itemsize*/ |
| 440 | /* methods */ |
| 441 | cl_dealloc, /*tp_dealloc*/ |
| 442 | 0, /*tp_print*/ |
| 443 | cl_getattr, /*tp_getattr*/ |
| 444 | 0, /*tp_setattr*/ |
| 445 | 0, /*tp_compare*/ |
| 446 | 0, /*tp_repr*/ |
| 447 | 0, /*tp_as_number*/ |
| 448 | 0, /*tp_as_sequence*/ |
| 449 | 0, /*tp_as_mapping*/ |
| 450 | }; |
| 451 | |
| 452 | static long |
| 453 | GetFrame(callbackID, frameIndex, numberOfFrames, data) |
| 454 | void *callbackID; |
| 455 | long frameIndex; |
| 456 | long numberOfFrames; |
| 457 | void **data; |
| 458 | { |
| 459 | object *args; |
| 460 | clobject *self = (clobject *) callbackID; |
| 461 | object *result; |
| 462 | |
| 463 | args = newtupleobject(3); |
| 464 | if (args == NULL) |
| 465 | return FAILURE; |
| 466 | |
| 467 | XINCREF(self->ob_callbackID); |
| 468 | settupleitem(args, 0, self->ob_callbackID); |
| 469 | settupleitem(args, 1, newintobject(frameIndex)); |
| 470 | settupleitem(args, 2, newintobject(numberOfFrames)); |
| 471 | |
| 472 | if (err_occurred()) { |
| 473 | XDECREF(self->ob_callbackID); |
| 474 | return FAILURE; |
| 475 | } |
| 476 | |
| 477 | result = call_object(self->ob_callbackFunc, args); |
| 478 | DECREF(args); |
| 479 | if (result == NULL) |
| 480 | return FAILURE; |
| 481 | |
| 482 | if (!is_stringobject(result)) { |
| 483 | DECREF(result); |
| 484 | return FAILURE; |
| 485 | } |
| 486 | |
| 487 | XDECREF(self->ob_data); |
| 488 | self->ob_data = result; |
| 489 | |
| 490 | *data = (void *) getstringvalue(result); |
| 491 | |
| 492 | return SUCCESS; |
| 493 | } |
| 494 | |
| 495 | static long |
| 496 | GetData(callbackID, frameIndex, numberOfFrames, dataSize, data) |
| 497 | void *callbackID; |
| 498 | long frameIndex; |
| 499 | long numberOfFrames; |
| 500 | long *dataSize; |
| 501 | void **data; |
| 502 | { |
| 503 | object *args, *result; |
| 504 | clobject *self = (clobject *) callbackID; |
| 505 | |
| 506 | args = newtupleobject(3); |
| 507 | if (args == NULL) |
| 508 | return FAILURE; |
| 509 | |
| 510 | XINCREF(self->ob_callbackID); |
| 511 | settupleitem(args, 0, self->ob_callbackID); |
| 512 | settupleitem(args, 1, newintobject(frameIndex)); |
| 513 | settupleitem(args, 2, newintobject(numberOfFrames)); |
| 514 | |
| 515 | if (err_occurred()) { |
| 516 | XDECREF(self->ob_callbackID); |
| 517 | return FAILURE; |
| 518 | } |
| 519 | |
| 520 | result = call_object(self->ob_callbackFunc, args); |
| 521 | DECREF(args); |
| 522 | if (result == NULL) |
| 523 | return FAILURE; |
| 524 | |
| 525 | if (!is_stringobject(result)) { |
| 526 | DECREF(result); |
| 527 | return FAILURE; |
| 528 | } |
| 529 | |
| 530 | XDECREF(self->ob_data); |
| 531 | self->ob_data = result; |
| 532 | |
| 533 | *dataSize = getstringsize(result); |
| 534 | *data = (void *) getstringvalue(result); |
| 535 | |
| 536 | return SUCCESS; |
| 537 | } |
| 538 | |
| 539 | static object * |
| 540 | cl_OpenCompressor(self, args) |
| 541 | object *self, *args; |
| 542 | { |
| 543 | CL_CompressionFormat compressionFormat; |
| 544 | long qualityFactor; |
| 545 | object *GetFrameCBPtr; |
| 546 | object *callbackID; |
| 547 | clobject *new; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 548 | |
| 549 | if (!getargs(args, "((iiiiiiiiii)iOO)", |
| 550 | &compressionFormat.width, |
| 551 | &compressionFormat.height, |
| 552 | &compressionFormat.frameSize, |
| 553 | &compressionFormat.dataMaxSize, |
| 554 | &compressionFormat.originalFormat, |
| 555 | &compressionFormat.components, |
| 556 | &compressionFormat.bitsPerComponent, |
| 557 | &compressionFormat.frameRate, |
| 558 | &compressionFormat.numberOfFrames, |
| 559 | &compressionFormat.compressionScheme, |
| 560 | &qualityFactor, &GetFrameCBPtr, &callbackID)) |
| 561 | return NULL; |
| 562 | |
| 563 | new = NEWOBJ(clobject, &Cltype); |
| 564 | if (new == 0) |
| 565 | return NULL; |
| 566 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 567 | new->ob_compressorHdl = NULL; |
| 568 | new->ob_callbackFunc = NULL; |
| 569 | new->ob_callbackID = NULL; |
| 570 | new->ob_data = NULL; |
| 571 | |
| 572 | error_handler_called = 0; |
| 573 | if (clOpenCompressor(&compressionFormat, qualityFactor, GetFrame, |
| 574 | (void *) new, &new->ob_compressorHdl) == FAILURE) { |
| 575 | DECREF(new); |
| 576 | if (!error_handler_called) |
| 577 | err_setstr(ClError, "opencompressor failed"); |
| 578 | return NULL; |
| 579 | } |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 580 | |
| 581 | new->ob_isCompressor = 1; |
| 582 | new->ob_callbackFunc = GetFrameCBPtr; |
| 583 | XINCREF(new->ob_callbackFunc); |
| 584 | if (callbackID == NULL) |
| 585 | callbackID = None; |
| 586 | new->ob_callbackID = callbackID; |
| 587 | INCREF(new->ob_callbackID); |
| 588 | new->ob_data = NULL; |
| 589 | new->ob_dataMaxSize = compressionFormat.dataMaxSize; |
| 590 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 591 | return new; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | static object * |
| 595 | cl_OpenDecompressor(self, args) |
| 596 | object *self, *args; |
| 597 | { |
| 598 | CL_CompressionFormat compressionFormat; |
| 599 | long infoSize; |
| 600 | void *info; |
| 601 | object *GetDataCBPtr; |
| 602 | object *callbackID; |
| 603 | clobject *new; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 604 | object *res; |
| 605 | |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 606 | if (!getargs(args, "((iiiiiiiiii)s#OO)", |
| 607 | &compressionFormat.width, |
| 608 | &compressionFormat.height, |
| 609 | &compressionFormat.frameSize, |
| 610 | &compressionFormat.dataMaxSize, |
| 611 | &compressionFormat.originalFormat, |
| 612 | &compressionFormat.components, |
| 613 | &compressionFormat.bitsPerComponent, |
| 614 | &compressionFormat.frameRate, |
| 615 | &compressionFormat.numberOfFrames, |
| 616 | &compressionFormat.compressionScheme, |
| 617 | &info, &infoSize, &GetDataCBPtr, &callbackID)) |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 618 | return NULL; |
| 619 | |
| 620 | new = NEWOBJ(clobject, &Cltype); |
| 621 | if (new == 0) |
| 622 | return NULL; |
| 623 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 624 | new->ob_compressorHdl = NULL; |
| 625 | new->ob_callbackFunc = NULL; |
| 626 | new->ob_callbackID = NULL; |
| 627 | new->ob_data = NULL; |
| 628 | |
| 629 | error_handler_called = 0; |
| 630 | if (clOpenDecompressor(&compressionFormat, infoSize, info, GetData, |
| 631 | (void *) new, &new->ob_compressorHdl) == FAILURE) { |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 632 | new->ob_compressorHdl = NULL; /* just in case... */ |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 633 | DECREF(new); |
| 634 | if (!error_handler_called) |
| 635 | err_setstr(ClError, "opendecompressor failed"); |
| 636 | return NULL; |
| 637 | } |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 638 | |
| 639 | new->ob_isCompressor = 0; |
| 640 | new->ob_callbackFunc = GetDataCBPtr; |
| 641 | XINCREF(new->ob_callbackFunc); |
| 642 | if (callbackID == NULL) |
| 643 | callbackID = None; |
| 644 | new->ob_callbackID = callbackID; |
| 645 | XINCREF(new->ob_callbackID); |
| 646 | new->ob_data = NULL; |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 647 | new->ob_dataMaxSize = compressionFormat.dataMaxSize; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 648 | |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 649 | return new; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | static object * |
| 653 | cl_AddParam(self, args) |
| 654 | object *self, *args; |
| 655 | { |
| 656 | char *name; |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 657 | long type, min, max, initial, paramID; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 658 | |
| 659 | if (!getargs(args, "(siiii)", &name, &type, &min, &max, &initial)) |
| 660 | return NULL; |
| 661 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 662 | error_handler_called = 0; |
| 663 | if (clAddParam(name, type, min, max, initial, ¶mID) == FAILURE) { |
| 664 | if (!error_handler_called) |
| 665 | err_setstr(ClError, "addparam failed"); |
| 666 | return NULL; |
| 667 | } |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 668 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 669 | return newintobject(paramID); |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | static struct methodlist cl_methods[] = { |
| 673 | {"AddParam", cl_AddParam}, |
| 674 | {"OpenCompressor", cl_OpenCompressor}, |
| 675 | {"OpenDecompressor", cl_OpenDecompressor}, |
| 676 | {NULL, NULL} /* Sentinel */ |
| 677 | }; |
| 678 | |
| 679 | void |
| 680 | initcl() |
| 681 | { |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 682 | object *m, *d; |
| 683 | |
| 684 | m = initmodule("cl", cl_methods); |
| 685 | d = getmoduledict(m); |
| 686 | |
| 687 | ClError = newstringobject("cl.error"); |
| 688 | if (ClError == NULL || dictinsert(d, "error", ClError) != 0) |
| 689 | fatal("can't define cl.error"); |
| 690 | |
| 691 | (void) clSetErrorHandler(cl_ErrorHandler); |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 692 | } |