blob: 30a631c3fbbd24015a770d18b0607d06cd9e000f [file] [log] [blame]
Sjoerd Mullenderc4315491992-09-23 14:53:00 +00001/***********************************************************
2Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
25
26/* Cl objects */
27
28#include <cl.h>
29#include "allobjects.h"
30#include "modsupport.h" /* For getargs() etc. */
31#include "ceval.h" /* For call_object() */
32
33typedef struct {
34 OB_HEAD
35 int ob_isCompressor; /* Compressor or Decompressor */
36 CL_CompressorHdl ob_compressorHdl;
37 long ob_dataMaxSize;
38 object *ob_callbackFunc;
39 object *ob_callbackID;
40 object *ob_data;
41} clobject;
42
43extern typeobject Cltype; /* Really static, forward */
44
45#define is_clobject(v) ((v)->ob_type == &Cltype)
46
47static object *
48cl_Compress(self, args)
49 clobject *self;
50 object *args;
51{
52 object *data, *res;
53 long frameIndex, numberOfFrames, dataSize, result;
54
55 if (!getargs(args, "(ii)", &frameIndex, &numberOfFrames))
56 return NULL;
57
58 data = newsizedstringobject(NULL,
59 numberOfFrames * self->ob_dataMaxSize);
60 if (data == NULL)
61 return NULL;
62
63 dataSize = numberOfFrames * self->ob_dataMaxSize;
64
65 result = clCompress(self->ob_compressorHdl, frameIndex, numberOfFrames,
66 &dataSize, (void *) getstringvalue(data));
67
68 if (dataSize < numberOfFrames * self->ob_dataMaxSize)
69 if (resizestring(&data, dataSize))
70 return NULL;
71
72 res = mkvalue("(iO)", result, data);
73 DECREF(data);
74 return res;
75}
76
77static object *
78cl_Decompress(self, args)
79 clobject *self;
80 object *args;
81{
82 object *data, *res;
83 long frameIndex, numberOfFrames, result;
84
85 if (!getargs(args, "(ii)", &frameIndex, &numberOfFrames))
86 return NULL;
87
88 data = newsizedstringobject(NULL,
89 numberOfFrames * self->ob_dataMaxSize);
90 if (data == NULL)
91 return NULL;
92
93 result = clDecompress(self->ob_compressorHdl, frameIndex,
94 numberOfFrames, (void *) getstringvalue(data));
95
96 res = mkvalue("(iO)", result, data);
97 DECREF(data);
98 return res;
99}
100
101static object *
102cl_GetCompressorInfo(self, args)
103 clobject *self;
104 object *args;
105{
106 long result, infoSize;
107 void *info;
108 object *infoObject, *res;
109
110 if (!getnoarg(args))
111 return NULL;
112
113 result = clGetCompressorInfo(self->ob_compressorHdl, &infoSize, &info);
114
115 infoObject = newsizedstringobject((char *) info, infoSize);
116
117 res = mkvalue("(iO)", result, infoObject);
118 DECREF(infoObject);
119 return res;
120}
121
122static object *
123cl_GetDefault(self, args)
124 clobject *self;
125 object *args;
126{
127 long initial, result;
128
129 if (!getargs(args, "i", &initial))
130 return NULL;
131
132 result = clGetDefault(self->ob_compressorHdl, initial);
133
134 return newintobject(result);
135}
136
137static object *
138cl_GetMinMax(self, args)
139 clobject *self;
140 object *args;
141{
142 long param, min, max;
143
144 if (!getargs(args, "i", &param))
145 return NULL;
146
147 clGetMinMax(self->ob_compressorHdl, param, &min, &max);
148
149 return mkvalue("(ii)", min, max);
150}
151
152static object *
153cl_GetName(self, args)
154 clobject *self;
155 object *args;
156{
157 long descriptor;
158 char *name;
159
160 if (!getargs(args, "i", &descriptor))
161 return NULL;
162
163 name = clGetName(self->ob_compressorHdl, descriptor);
164
165 return newstringobject(name);
166}
167
168static object *
169doParams(self, args, func, modified)
170 clobject *self;
171 object *args;
172 void (*func)(CL_CompressorHdl, long *, long);
173 int modified;
174{
175 object *list, *v;
176 long *PVbuffer;
177 long length;
178 int i;
179
180 if (!getargs(args, "O", &list))
181 return NULL;
182 if (!is_listobject(list)) {
183 err_badarg();
184 return NULL;
185 }
186 length = getlistsize(list);
187 PVbuffer = NEW(long, length);
188 if (PVbuffer == NULL)
189 return err_nomem();
190 for (i = 0; i < length; i++) {
191 v = getlistitem(list, i);
192 if (!is_intobject(v)) {
193 DEL(PVbuffer);
194 err_badarg();
195 return NULL;
196 }
197 PVbuffer[i] = getintvalue(v);
198 }
199
200 (*func)(self->ob_compressorHdl, PVbuffer, length);
201
202 if (modified) {
203 for (i = 0; i < length; i++)
204 setlistitem(list, i, newintobject(PVbuffer[i]));
205 }
206
207 DEL(PVbuffer);
208
209 INCREF(None);
210 return None;
211}
212
213static object *
214cl_GetParams(self, args)
215 object *self, *args;
216{
217 return doParams(self, args, clGetParams, 1);
218}
219
220static object *
221cl_SetParams(self, args)
222 object *self, *args;
223{
224 return doParams(self, args, clSetParams, 0);
225}
226
227static object *
228cl_QueryParams(self, args)
229 clobject *self;
230 object *args;
231{
232 long bufferlength;
233 long *PVbuffer;
234 object *list;
235 int i;
236
237 if (!getnoarg(args))
238 return NULL;
239
240 bufferlength = clQueryParams(self->ob_compressorHdl, 0, 0);
241
242 PVbuffer = NEW(long, bufferlength);
243 if (PVbuffer == NULL)
244 return err_nomem();
245
246 bufferlength = clQueryParams(self->ob_compressorHdl, PVbuffer,
247 bufferlength);
248
249 list = newlistobject(bufferlength);
250 if (list == NULL) {
251 DEL(PVbuffer);
252 return NULL;
253 }
254
255 for (i = 0; i < bufferlength; i++)
256 setlistitem(list, i, newintobject(PVbuffer[i]));
257
258 DEL(PVbuffer);
259
260 return list;
261}
262
263static struct methodlist compressor_methods[] = {
264 {"Compress", cl_Compress},
265 {"GetCompressorInfo", cl_GetCompressorInfo},
266 {"GetDefault", cl_GetDefault},
267 {"GetMinMax", cl_GetMinMax},
268 {"GetName", cl_GetName},
269 {"GetParams", cl_GetParams},
270 {"QueryParams", cl_QueryParams},
271 {"SetParams", cl_SetParams},
272 {NULL, NULL} /* sentinel */
273};
274
275static struct methodlist decompressor_methods[] = {
276 {"Decompress", cl_Decompress},
277 {"GetDefault", cl_GetDefault},
278 {"GetMinMax", cl_GetMinMax},
279 {"GetName", cl_GetName},
280 {"GetParams", cl_GetParams},
281 {"QueryParams", cl_QueryParams},
282 {"SetParams", cl_SetParams},
283 {NULL, NULL} /* sentinel */
284};
285
286static void
287cl_dealloc(self)
288 clobject *self;
289{
290 if (self->ob_compressorHdl) {
291 if (self->ob_isCompressor)
292 clCloseCompressor(self->ob_compressorHdl);
293 else
294 clCloseDecompressor(self->ob_compressorHdl);
295 }
296 XDECREF(self->ob_callbackFunc);
297 XDECREF(self->ob_callbackID);
298 XDECREF(self->ob_data);
299 DEL(self);
300}
301
302static object *
303cl_getattr(self, name)
304 clobject *self;
305 char *name;
306{
307 if (self->ob_isCompressor)
308 return findmethod(compressor_methods, (object *)self, name);
309 else
310 return findmethod(decompressor_methods, (object *) self, name);
311}
312
313static typeobject Cltype = {
314 OB_HEAD_INIT(&Typetype)
315 0, /*ob_size*/
316 "cl", /*tp_name*/
317 sizeof(clobject), /*tp_size*/
318 0, /*tp_itemsize*/
319 /* methods */
320 cl_dealloc, /*tp_dealloc*/
321 0, /*tp_print*/
322 cl_getattr, /*tp_getattr*/
323 0, /*tp_setattr*/
324 0, /*tp_compare*/
325 0, /*tp_repr*/
326 0, /*tp_as_number*/
327 0, /*tp_as_sequence*/
328 0, /*tp_as_mapping*/
329};
330
331static long
332GetFrame(callbackID, frameIndex, numberOfFrames, data)
333 void *callbackID;
334 long frameIndex;
335 long numberOfFrames;
336 void **data;
337{
338 object *args;
339 clobject *self = (clobject *) callbackID;
340 object *result;
341
342 args = newtupleobject(3);
343 if (args == NULL)
344 return FAILURE;
345
346 XINCREF(self->ob_callbackID);
347 settupleitem(args, 0, self->ob_callbackID);
348 settupleitem(args, 1, newintobject(frameIndex));
349 settupleitem(args, 2, newintobject(numberOfFrames));
350
351 if (err_occurred()) {
352 XDECREF(self->ob_callbackID);
353 return FAILURE;
354 }
355
356 result = call_object(self->ob_callbackFunc, args);
357 DECREF(args);
358 if (result == NULL)
359 return FAILURE;
360
361 if (!is_stringobject(result)) {
362 DECREF(result);
363 return FAILURE;
364 }
365
366 XDECREF(self->ob_data);
367 self->ob_data = result;
368
369 *data = (void *) getstringvalue(result);
370
371 return SUCCESS;
372}
373
374static long
375GetData(callbackID, frameIndex, numberOfFrames, dataSize, data)
376 void *callbackID;
377 long frameIndex;
378 long numberOfFrames;
379 long *dataSize;
380 void **data;
381{
382 object *args, *result;
383 clobject *self = (clobject *) callbackID;
384
385 args = newtupleobject(3);
386 if (args == NULL)
387 return FAILURE;
388
389 XINCREF(self->ob_callbackID);
390 settupleitem(args, 0, self->ob_callbackID);
391 settupleitem(args, 1, newintobject(frameIndex));
392 settupleitem(args, 2, newintobject(numberOfFrames));
393
394 if (err_occurred()) {
395 XDECREF(self->ob_callbackID);
396 return FAILURE;
397 }
398
399 result = call_object(self->ob_callbackFunc, args);
400 DECREF(args);
401 if (result == NULL)
402 return FAILURE;
403
404 if (!is_stringobject(result)) {
405 DECREF(result);
406 return FAILURE;
407 }
408
409 XDECREF(self->ob_data);
410 self->ob_data = result;
411
412 *dataSize = getstringsize(result);
413 *data = (void *) getstringvalue(result);
414
415 return SUCCESS;
416}
417
418static object *
419cl_OpenCompressor(self, args)
420 object *self, *args;
421{
422 CL_CompressionFormat compressionFormat;
423 long qualityFactor;
424 object *GetFrameCBPtr;
425 object *callbackID;
426 clobject *new;
427 long result;
428 object *res;
429
430 if (!getargs(args, "((iiiiiiiiii)iOO)",
431 &compressionFormat.width,
432 &compressionFormat.height,
433 &compressionFormat.frameSize,
434 &compressionFormat.dataMaxSize,
435 &compressionFormat.originalFormat,
436 &compressionFormat.components,
437 &compressionFormat.bitsPerComponent,
438 &compressionFormat.frameRate,
439 &compressionFormat.numberOfFrames,
440 &compressionFormat.compressionScheme,
441 &qualityFactor, &GetFrameCBPtr, &callbackID))
442 return NULL;
443
444 new = NEWOBJ(clobject, &Cltype);
445 if (new == 0)
446 return NULL;
447
448 result = clOpenCompressor(&compressionFormat, qualityFactor, GetFrame,
449 (void *) new, &new->ob_compressorHdl);
450
451 new->ob_isCompressor = 1;
452 new->ob_callbackFunc = GetFrameCBPtr;
453 XINCREF(new->ob_callbackFunc);
454 if (callbackID == NULL)
455 callbackID = None;
456 new->ob_callbackID = callbackID;
457 INCREF(new->ob_callbackID);
458 new->ob_data = NULL;
459 new->ob_dataMaxSize = compressionFormat.dataMaxSize;
460
461 res = mkvalue("(iO)", result, new);
462 DECREF(new);
463 return res;
464}
465
466static object *
467cl_OpenDecompressor(self, args)
468 object *self, *args;
469{
470 CL_CompressionFormat compressionFormat;
471 long infoSize;
472 void *info;
473 object *GetDataCBPtr;
474 object *callbackID;
475 clobject *new;
476 long result;
477 object *res;
478
479 if (!getargs(args, "(s#OO)", &info, &infoSize, &GetDataCBPtr,
480 &callbackID))
481 return NULL;
482
483 new = NEWOBJ(clobject, &Cltype);
484 if (new == 0)
485 return NULL;
486
487 result = clOpenDecompressor(&compressionFormat, infoSize, info,
488 GetData, (void *) new,
489 &new->ob_compressorHdl);
490
491 new->ob_isCompressor = 0;
492 new->ob_callbackFunc = GetDataCBPtr;
493 XINCREF(new->ob_callbackFunc);
494 if (callbackID == NULL)
495 callbackID = None;
496 new->ob_callbackID = callbackID;
497 XINCREF(new->ob_callbackID);
498 new->ob_data = NULL;
499
500 res = mkvalue("(iO(iiiiiiiiii))", result, new,
501 compressionFormat.width,
502 compressionFormat.height,
503 compressionFormat.frameSize,
504 compressionFormat.dataMaxSize,
505 compressionFormat.originalFormat,
506 compressionFormat.components,
507 compressionFormat.bitsPerComponent,
508 compressionFormat.frameRate,
509 compressionFormat.numberOfFrames,
510 compressionFormat.compressionScheme);
511 if (res == NULL) {
512 XDECREF(new->ob_callbackFunc);
513 XDECREF(new->ob_callbackID);
514 }
515
516 DECREF(new);
517 return res;
518}
519
520static object *
521cl_AddParam(self, args)
522 object *self, *args;
523{
524 char *name;
525 long type, min, max, initial, paramID, result;
526
527 if (!getargs(args, "(siiii)", &name, &type, &min, &max, &initial))
528 return NULL;
529
530 result = clAddParam(name, type, min, max, initial, &paramID);
531
532 return mkvalue("(ii)", result, paramID);
533}
534
535static struct methodlist cl_methods[] = {
536 {"AddParam", cl_AddParam},
537 {"OpenCompressor", cl_OpenCompressor},
538 {"OpenDecompressor", cl_OpenDecompressor},
539 {NULL, NULL} /* Sentinel */
540};
541
542void
543initcl()
544{
545 (void) initmodule("cl", cl_methods);
546}