blob: 75542d1ecb2c3eb31f01ccc7138eab50437c621e [file] [log] [blame]
Sjoerd Mullenderc4315491992-09-23 14:53:00 +00001/***********************************************************
Guido van Rossumb6775db1994-08-01 11:34:53 +00002Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
Guido van Rossume5372401993-03-16 12:15:04 +00003Amsterdam, The Netherlands.
Sjoerd Mullenderc4315491992-09-23 14:53:00 +00004
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
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +000028#define CLDEBUG
29
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +000030#include <stdarg.h>
Sjoerd Mullenderc4315491992-09-23 14:53:00 +000031#include <cl.h>
32#include "allobjects.h"
33#include "modsupport.h" /* For getargs() etc. */
34#include "ceval.h" /* For call_object() */
35
36typedef struct {
37 OB_HEAD
38 int ob_isCompressor; /* Compressor or Decompressor */
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +000039 CL_Handle ob_compressorHdl;
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +000040 int *ob_paramtypes;
41 int ob_nparams;
Sjoerd Mullenderc4315491992-09-23 14:53:00 +000042} clobject;
43
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +000044static object *ClError; /* exception cl.error */
45
46static int error_handler_called = 0;
47
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +000048/*
49 * We want to use the function prototypes that are available in the C
50 * compiler on the SGI. Because of that, we need to declare the first
51 * argument of the compressor and decompressor methods as "object *",
52 * even though they are really "clobject *". Therefore we cast the
53 * argument to the proper type using this macro.
54 */
55#define SELF ((clobject *) self)
56
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +000057/********************************************************************
58 Utility routines.
59********************************************************************/
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +000060static void
Sjoerd Mullender3a997271993-02-04 16:43:28 +000061cl_ErrorHandler(CL_Handle handle, int code, const char *fmt, ...)
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +000062{
63 va_list ap;
64 char errbuf[BUFSIZ]; /* hopefully big enough */
Sjoerd Mullender384f2481992-09-29 16:43:43 +000065 char *p;
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +000066
Sjoerd Mullender384f2481992-09-29 16:43:43 +000067 if (err_occurred()) /* don't change existing error */
68 return;
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +000069 error_handler_called = 1;
70 va_start(ap, fmt);
71 vsprintf(errbuf, fmt, ap);
72 va_end(ap);
Sjoerd Mullender384f2481992-09-29 16:43:43 +000073 p = &errbuf[strlen(errbuf) - 1]; /* swat the line feed */
74 if (*p == '\n')
75 *p = 0;
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +000076 err_setstr(ClError, errbuf);
77}
78
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +000079/*
80 * This assumes that params are always in the range 0 to some maximum.
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +000081 */
82static int
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +000083param_type_is_float(clobject *self, int param)
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +000084{
Sjoerd Mullender3a997271993-02-04 16:43:28 +000085 int bufferlength;
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +000086 int ret;
87
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +000088 if (self->ob_paramtypes == NULL) {
89 error_handler_called = 0;
90 bufferlength = clQueryParams(self->ob_compressorHdl, 0, 0);
91 if (error_handler_called)
92 return -1;
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +000093
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +000094 self->ob_paramtypes = NEW(int, bufferlength);
95 if (self->ob_paramtypes == NULL)
96 return -1;
97 self->ob_nparams = bufferlength / 2;
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +000098
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +000099 (void) clQueryParams(self->ob_compressorHdl, self->ob_paramtypes, bufferlength);
100 if (error_handler_called) {
101 DEL(self->ob_paramtypes);
102 self->ob_paramtypes = NULL;
103 return -1;
104 }
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000105 }
106
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000107 if (param < 0 || param >= self->ob_nparams)
108 return -1;
109
110 if (self->ob_paramtypes[param*2 + 1] == CL_FLOATING_ENUM_VALUE ||
111 self->ob_paramtypes[param*2 + 1] == CL_FLOATING_RANGE_VALUE)
112 return 1;
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000113 else
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000114 return 0;
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000115}
116
117/********************************************************************
118 Single image compression/decompression.
119********************************************************************/
120static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000121cl_CompressImage(object *self, object *args)
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000122{
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000123 int compressionScheme, width, height, originalFormat;
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000124 float compressionRatio;
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000125 int frameBufferSize, compressedBufferSize;
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000126 char *frameBuffer;
127 object *compressedBuffer;
128
129 if (!getargs(args, "(iiiifs#)", &compressionScheme, &width, &height,
130 &originalFormat, &compressionRatio, &frameBuffer,
131 &frameBufferSize))
132 return NULL;
133
134 retry:
135 compressedBuffer = newsizedstringobject(NULL, frameBufferSize);
136 if (compressedBuffer == NULL)
137 return NULL;
138
139 compressedBufferSize = frameBufferSize;
140 error_handler_called = 0;
141 if (clCompressImage(compressionScheme, width, height, originalFormat,
142 compressionRatio, (void *) frameBuffer,
143 &compressedBufferSize,
144 (void *) getstringvalue(compressedBuffer))
145 == FAILURE) {
146 DECREF(compressedBuffer);
147 if (!error_handler_called)
148 err_setstr(ClError, "clCompressImage failed");
149 return NULL;
150 }
151
152 if (compressedBufferSize > frameBufferSize) {
153 frameBufferSize = compressedBufferSize;
154 DECREF(compressedBuffer);
155 goto retry;
156 }
157
158 if (compressedBufferSize < frameBufferSize)
159 if (resizestring(&compressedBuffer, compressedBufferSize))
160 return NULL;
161
162 return compressedBuffer;
163}
164
165static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000166cl_DecompressImage(object *self, object *args)
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000167{
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000168 int compressionScheme, width, height, originalFormat;
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000169 char *compressedBuffer;
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000170 int compressedBufferSize, frameBufferSize;
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000171 object *frameBuffer;
172
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000173 if (!getargs(args, "(iiiis#)", &compressionScheme, &width, &height,
174 &originalFormat, &compressedBuffer,
175 &compressedBufferSize))
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000176 return NULL;
177
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000178 frameBufferSize = width * height * CL_BytesPerPixel(originalFormat);
179
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000180 frameBuffer = newsizedstringobject(NULL, frameBufferSize);
181 if (frameBuffer == NULL)
182 return NULL;
183
184 error_handler_called = 0;
185 if (clDecompressImage(compressionScheme, width, height, originalFormat,
186 compressedBufferSize, compressedBuffer,
187 (void *) getstringvalue(frameBuffer)) == FAILURE) {
188 DECREF(frameBuffer);
189 if (!error_handler_called)
190 err_setstr(ClError, "clDecompressImage failed");
191 return NULL;
192 }
193
194 return frameBuffer;
195}
196
197/********************************************************************
198 Sequential compression/decompression.
199********************************************************************/
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000200#define CheckCompressor(self) if ((self)->ob_compressorHdl == NULL) { \
201 err_setstr(RuntimeError, "(de)compressor not active"); \
202 return NULL; \
203 }
204
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000205static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000206doClose(clobject *self, object *args, int (*close_func)(CL_Handle))
Sjoerd Mullender37f17b71992-09-25 10:28:20 +0000207{
208 CheckCompressor(self);
209
210 if (!getnoarg(args))
211 return NULL;
212
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000213 error_handler_called = 0;
Sjoerd Mullender384f2481992-09-29 16:43:43 +0000214 if ((*close_func)(self->ob_compressorHdl) == FAILURE) {
Sjoerd Mullender37f17b71992-09-25 10:28:20 +0000215 if (!error_handler_called)
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000216 err_setstr(ClError, "close failed");
Sjoerd Mullender37f17b71992-09-25 10:28:20 +0000217 return NULL;
218 }
219
220 self->ob_compressorHdl = NULL;
221
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000222 if (self->ob_paramtypes)
223 DEL(self->ob_paramtypes);
224 self->ob_paramtypes = NULL;
225
Sjoerd Mullender37f17b71992-09-25 10:28:20 +0000226 INCREF(None);
227 return None;
228}
229
230static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000231clm_CloseCompressor(object *self, object *args)
Sjoerd Mullender37f17b71992-09-25 10:28:20 +0000232{
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000233 return doClose(SELF, args, clCloseCompressor);
Sjoerd Mullender37f17b71992-09-25 10:28:20 +0000234}
235
236static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000237clm_CloseDecompressor(object *self, object *args)
Sjoerd Mullender384f2481992-09-29 16:43:43 +0000238{
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000239 return doClose(SELF, args, clCloseDecompressor);
Sjoerd Mullender384f2481992-09-29 16:43:43 +0000240}
241
242static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000243clm_Compress(object *self, object *args)
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000244{
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000245 int numberOfFrames;
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000246 int frameBufferSize, compressedBufferSize, size;
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000247 char *frameBuffer;
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +0000248 object *data;
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000249
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000250 CheckCompressor(SELF);
Sjoerd Mullender37f17b71992-09-25 10:28:20 +0000251
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000252 if (!getargs(args, "(is#)", &numberOfFrames, &frameBuffer, &frameBufferSize))
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000253 return NULL;
254
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000255 error_handler_called = 0;
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000256 size = clGetParam(SELF->ob_compressorHdl, CL_COMPRESSED_BUFFER_SIZE);
257 compressedBufferSize = size;
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000258 if (error_handler_called)
259 return NULL;
260
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000261 data = newsizedstringobject(NULL, size);
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000262 if (data == NULL)
263 return NULL;
264
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +0000265 error_handler_called = 0;
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000266 if (clCompress(SELF->ob_compressorHdl, numberOfFrames,
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000267 (void *) frameBuffer, &compressedBufferSize,
268 (void *) getstringvalue(data)) == FAILURE) {
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +0000269 DECREF(data);
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000270 if (!error_handler_called)
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +0000271 err_setstr(ClError, "compress failed");
272 return NULL;
273 }
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000274
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000275 if (compressedBufferSize < size)
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000276 if (resizestring(&data, compressedBufferSize))
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000277 return NULL;
278
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000279 if (compressedBufferSize > size) {
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000280 /* we didn't get all "compressed" data */
Sjoerd Mullender384f2481992-09-29 16:43:43 +0000281 DECREF(data);
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000282 err_setstr(ClError, "compressed data is more than fitted");
Sjoerd Mullender384f2481992-09-29 16:43:43 +0000283 return NULL;
284 }
285
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +0000286 return data;
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000287}
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000288
289static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000290clm_Decompress(object *self, object *args)
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000291{
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +0000292 object *data;
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000293 int numberOfFrames;
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000294 char *compressedData;
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000295 int compressedDataSize, dataSize;
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000296
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000297 CheckCompressor(SELF);
Sjoerd Mullender37f17b71992-09-25 10:28:20 +0000298
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000299 if (!getargs(args, "(is#)", &numberOfFrames, &compressedData,
300 &compressedDataSize))
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000301 return NULL;
302
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000303 error_handler_called = 0;
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000304 dataSize = clGetParam(SELF->ob_compressorHdl, CL_FRAME_BUFFER_SIZE);
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000305 if (error_handler_called)
306 return NULL;
307
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000308 data = newsizedstringobject(NULL, dataSize);
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000309 if (data == NULL)
310 return NULL;
311
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +0000312 error_handler_called = 0;
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000313 if (clDecompress(SELF->ob_compressorHdl, numberOfFrames,
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000314 compressedDataSize, (void *) compressedData,
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +0000315 (void *) getstringvalue(data)) == FAILURE) {
316 DECREF(data);
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000317 if (!error_handler_called)
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +0000318 err_setstr(ClError, "decompress failed");
319 return NULL;
320 }
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000321
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +0000322 return data;
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000323}
324
325static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000326doParams(clobject *self, object *args, int (*func)(CL_Handle, int *, int),
327 int modified)
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000328{
329 object *list, *v;
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000330 int *PVbuffer;
331 int length;
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000332 int i;
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000333 float number;
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000334
Sjoerd Mullender37f17b71992-09-25 10:28:20 +0000335 CheckCompressor(self);
336
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000337 if (!getargs(args, "O", &list))
338 return NULL;
339 if (!is_listobject(list)) {
340 err_badarg();
341 return NULL;
342 }
343 length = getlistsize(list);
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000344 PVbuffer = NEW(int, length);
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000345 if (PVbuffer == NULL)
346 return err_nomem();
347 for (i = 0; i < length; i++) {
348 v = getlistitem(list, i);
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000349 if (is_floatobject(v)) {
350 number = getfloatvalue(v);
351 PVbuffer[i] = CL_TypeIsInt(number);
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000352 } else if (is_intobject(v)) {
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000353 PVbuffer[i] = getintvalue(v);
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000354 if ((i & 1) &&
355 param_type_is_float(self, PVbuffer[i-1]) > 0) {
356 number = PVbuffer[i];
357 PVbuffer[i] = CL_TypeIsInt(number);
358 }
359 } else {
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000360 DEL(PVbuffer);
361 err_badarg();
362 return NULL;
363 }
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000364 }
365
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +0000366 error_handler_called = 0;
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000367 (*func)(self->ob_compressorHdl, PVbuffer, length);
Sjoerd Mullenderf64992e1993-08-03 15:11:36 +0000368 if (error_handler_called) {
369 DEL(PVbuffer);
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +0000370 return NULL;
Sjoerd Mullenderf64992e1993-08-03 15:11:36 +0000371 }
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000372
373 if (modified) {
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000374 for (i = 0; i < length; i++) {
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000375 if ((i & 1) &&
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000376 param_type_is_float(self, PVbuffer[i-1]) > 0) {
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000377 number = CL_TypeIsFloat(PVbuffer[i]);
378 v = newfloatobject(number);
379 } else
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000380 v = newintobject(PVbuffer[i]);
381 setlistitem(list, i, v);
382 }
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000383 }
384
385 DEL(PVbuffer);
386
387 INCREF(None);
388 return None;
389}
390
391static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000392clm_GetParams(object *self, object *args)
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000393{
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000394 return doParams(SELF, args, clGetParams, 1);
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000395}
396
397static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000398clm_SetParams(object *self, object *args)
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000399{
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000400 return doParams(SELF, args, clSetParams, 0);
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000401}
402
403static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000404do_get(clobject *self, object *args, int (*func)(CL_Handle, int))
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000405{
406 int paramID, value;
407 float fvalue;
408
409 CheckCompressor(self);
410
411 if (!getargs(args, "i", &paramID))
412 return NULL;
413
414 error_handler_called = 0;
415 value = (*func)(self->ob_compressorHdl, paramID);
416 if (error_handler_called)
417 return NULL;
418
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000419 if (param_type_is_float(self, paramID) > 0) {
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000420 fvalue = CL_TypeIsFloat(value);
421 return newfloatobject(fvalue);
422 }
423
424 return newintobject(value);
425}
426
427static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000428clm_GetParam(object *self, object *args)
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000429{
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000430 return do_get(SELF, args, clGetParam);
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000431}
432
433static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000434clm_GetDefault(object *self, object *args)
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000435{
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000436 return do_get(SELF, args, clGetDefault);
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000437}
438
439static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000440clm_SetParam(object *self, object *args)
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000441{
442 int paramID, value;
443 float fvalue;
444
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000445 CheckCompressor(SELF);
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000446
447 if (!getargs(args, "(ii)", &paramID, &value)) {
448 err_clear();
449 if (!getargs(args, "(if)", &paramID, &fvalue)) {
450 err_clear();
451 err_setstr(TypeError, "bad argument list (format '(ii)' or '(if)')");
452 return NULL;
453 }
454 value = CL_TypeIsInt(fvalue);
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000455 } else {
456 if (param_type_is_float(SELF, paramID) > 0) {
457 fvalue = value;
458 value = CL_TypeIsInt(fvalue);
459 }
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000460 }
461
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000462 error_handler_called = 0;
463 value = clSetParam(SELF->ob_compressorHdl, paramID, value);
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000464 if (error_handler_called)
465 return NULL;
466
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000467 if (param_type_is_float(SELF, paramID) > 0)
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000468 return newfloatobject(CL_TypeIsFloat(value));
469 else
470 return newintobject(value);
471}
472
473static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000474clm_GetParamID(object *self, object *args)
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000475{
476 char *name;
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000477 int value;
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000478
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000479 CheckCompressor(SELF);
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000480
481 if (!getargs(args, "s", &name))
482 return NULL;
483
484 error_handler_called = 0;
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000485 value = clGetParamID(SELF->ob_compressorHdl, name);
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000486 if (value == FAILURE) {
487 if (!error_handler_called)
488 err_setstr(ClError, "getparamid failed");
489 return NULL;
490 }
491
492 return newintobject(value);
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000493}
494
495static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000496clm_QueryParams(object *self, object *args)
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000497{
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000498 int bufferlength;
499 int *PVbuffer;
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000500 object *list;
501 int i;
502
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000503 CheckCompressor(SELF);
Sjoerd Mullender37f17b71992-09-25 10:28:20 +0000504
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000505 if (!getnoarg(args))
506 return NULL;
507
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +0000508 error_handler_called = 0;
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000509 bufferlength = clQueryParams(SELF->ob_compressorHdl, 0, 0);
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +0000510 if (error_handler_called)
511 return NULL;
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000512
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000513 PVbuffer = NEW(int, bufferlength);
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000514 if (PVbuffer == NULL)
515 return err_nomem();
516
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000517 bufferlength = clQueryParams(SELF->ob_compressorHdl, PVbuffer,
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000518 bufferlength);
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +0000519 if (error_handler_called) {
520 DEL(PVbuffer);
521 return NULL;
522 }
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000523
524 list = newlistobject(bufferlength);
525 if (list == NULL) {
526 DEL(PVbuffer);
527 return NULL;
528 }
529
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +0000530 for (i = 0; i < bufferlength; i++) {
531 if (i & 1)
532 setlistitem(list, i, newintobject(PVbuffer[i]));
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000533 else if (PVbuffer[i] == 0) {
534 INCREF(None);
535 setlistitem(list, i, None);
536 } else
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +0000537 setlistitem(list, i, newstringobject((char *) PVbuffer[i]));
538 }
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000539
540 DEL(PVbuffer);
541
542 return list;
543}
544
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000545static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000546clm_GetMinMax(object *self, object *args)
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000547{
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000548 int param, min, max;
549 float fmin, fmax;
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000550
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000551 CheckCompressor(SELF);
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000552
553 if (!getargs(args, "i", &param))
554 return NULL;
555
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000556 clGetMinMax(SELF->ob_compressorHdl, param, &min, &max);
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000557
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000558 if (param_type_is_float(SELF, param) > 0) {
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000559 fmin = CL_TypeIsFloat(min);
560 fmax = CL_TypeIsFloat(max);
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000561 return mkvalue("(ff)", fmin, fmax);
562 }
563
564 return mkvalue("(ii)", min, max);
565}
566
567static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000568clm_GetName(object *self, object *args)
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000569{
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000570 int param;
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000571 char *name;
572
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000573 CheckCompressor(SELF);
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000574
575 if (!getargs(args, "i", &param))
576 return NULL;
577
578 error_handler_called = 0;
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000579 name = clGetName(SELF->ob_compressorHdl, param);
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000580 if (name == NULL || error_handler_called) {
581 if (!error_handler_called)
582 err_setstr(ClError, "getname failed");
583 return NULL;
584 }
585
586 return newstringobject(name);
587}
588
589static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000590clm_QuerySchemeFromHandle(object *self, object *args)
591{
592 CheckCompressor(SELF);
593
594 if (!getnoarg(args))
595 return NULL;
596
597 return newintobject(clQuerySchemeFromHandle(SELF->ob_compressorHdl));
598}
599
600static object *
601clm_ReadHeader(object *self, object *args)
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000602{
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000603 char *header;
604 int headerSize;
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000605
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000606 CheckCompressor(SELF);
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000607
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000608 if (!getargs(args, "s#", &header, &headerSize))
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000609 return NULL;
610
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000611 return newintobject(clReadHeader(SELF->ob_compressorHdl,
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000612 headerSize, header));
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000613}
614
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000615static struct methodlist compressor_methods[] = {
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000616 {"close", clm_CloseCompressor}, /* alias */
Sjoerd Mullender384f2481992-09-29 16:43:43 +0000617 {"CloseCompressor", clm_CloseCompressor},
618 {"Compress", clm_Compress},
Sjoerd Mullender384f2481992-09-29 16:43:43 +0000619 {"GetDefault", clm_GetDefault},
620 {"GetMinMax", clm_GetMinMax},
621 {"GetName", clm_GetName},
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000622 {"GetParam", clm_GetParam},
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000623 {"GetParamID", clm_GetParamID},
Sjoerd Mullender384f2481992-09-29 16:43:43 +0000624 {"GetParams", clm_GetParams},
625 {"QueryParams", clm_QueryParams},
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000626 {"QuerySchemeFromHandle",clm_QuerySchemeFromHandle},
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000627 {"SetParam", clm_SetParam},
Sjoerd Mullender384f2481992-09-29 16:43:43 +0000628 {"SetParams", clm_SetParams},
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000629 {NULL, NULL} /* sentinel */
630};
631
632static struct methodlist decompressor_methods[] = {
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000633 {"close", clm_CloseDecompressor}, /* alias */
Sjoerd Mullender384f2481992-09-29 16:43:43 +0000634 {"CloseDecompressor", clm_CloseDecompressor},
635 {"Decompress", clm_Decompress},
636 {"GetDefault", clm_GetDefault},
637 {"GetMinMax", clm_GetMinMax},
638 {"GetName", clm_GetName},
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000639 {"GetParam", clm_GetParam},
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000640 {"GetParamID", clm_GetParamID},
Sjoerd Mullender384f2481992-09-29 16:43:43 +0000641 {"GetParams", clm_GetParams},
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000642 {"ReadHeader", clm_ReadHeader},
Sjoerd Mullender384f2481992-09-29 16:43:43 +0000643 {"QueryParams", clm_QueryParams},
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000644 {"QuerySchemeFromHandle",clm_QuerySchemeFromHandle},
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000645 {"SetParam", clm_SetParam},
Sjoerd Mullender384f2481992-09-29 16:43:43 +0000646 {"SetParams", clm_SetParams},
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000647 {NULL, NULL} /* sentinel */
648};
649
650static void
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000651cl_dealloc(object *self)
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000652{
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000653 if (SELF->ob_compressorHdl) {
654 if (SELF->ob_isCompressor)
655 clCloseCompressor(SELF->ob_compressorHdl);
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000656 else
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000657 clCloseDecompressor(SELF->ob_compressorHdl);
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000658 }
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000659 DEL(self);
660}
661
662static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000663cl_getattr(object *self, char *name)
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000664{
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000665 if (SELF->ob_isCompressor)
666 return findmethod(compressor_methods, self, name);
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000667 else
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000668 return findmethod(decompressor_methods, self, name);
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000669}
670
671static typeobject Cltype = {
672 OB_HEAD_INIT(&Typetype)
673 0, /*ob_size*/
674 "cl", /*tp_name*/
675 sizeof(clobject), /*tp_size*/
676 0, /*tp_itemsize*/
677 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000678 (destructor)cl_dealloc, /*tp_dealloc*/
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000679 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000680 (getattrfunc)cl_getattr, /*tp_getattr*/
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000681 0, /*tp_setattr*/
682 0, /*tp_compare*/
683 0, /*tp_repr*/
684 0, /*tp_as_number*/
685 0, /*tp_as_sequence*/
686 0, /*tp_as_mapping*/
687};
688
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000689static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000690doOpen(object *self, object *args, int (*open_func)(int, CL_Handle *),
691 int iscompressor)
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000692{
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000693 int scheme;
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000694 clobject *new;
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000695
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000696 if (!getargs(args, "i", &scheme))
697 return NULL;
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000698
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000699 new = NEWOBJ(clobject, &Cltype);
700 if (new == NULL)
701 return NULL;
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000702
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000703 new->ob_compressorHdl = NULL;
704 new->ob_isCompressor = iscompressor;
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000705 new->ob_paramtypes = NULL;
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000706
707 error_handler_called = 0;
708 if ((*open_func)(scheme, &new->ob_compressorHdl) == FAILURE) {
709 DECREF(new);
710 if (!error_handler_called)
711 err_setstr(ClError, "Open(De)Compressor failed");
712 return NULL;
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000713 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000714 return (object *)new;
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000715}
716
717static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000718cl_OpenCompressor(object *self, object *args)
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000719{
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000720 return doOpen(self, args, clOpenCompressor, 1);
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000721}
722
723static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000724cl_OpenDecompressor(object *self, object *args)
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000725{
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000726 return doOpen(self, args, clOpenDecompressor, 0);
Sjoerd Mullender384f2481992-09-29 16:43:43 +0000727}
728
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000729static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000730cl_QueryScheme(object *self, object *args)
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000731{
732 char *header;
733 int headerlen;
734 int scheme;
735
736 if (!getargs(args, "s#", &header, &headerlen))
737 return NULL;
738
739 scheme = clQueryScheme(header);
740 if (scheme < 0) {
741 err_setstr(ClError, "unknown compression scheme");
742 return NULL;
743 }
744
745 return newintobject(scheme);
746}
747
748static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000749cl_QueryMaxHeaderSize(object *self, object *args)
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000750{
751 int scheme;
752
753 if (!getargs(args, "i", &scheme))
754 return NULL;
755
756 return newintobject(clQueryMaxHeaderSize(scheme));
757}
758
759static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000760cl_QueryAlgorithms(object *self, object *args)
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000761{
762 int algorithmMediaType;
763 int bufferlength;
764 int *PVbuffer;
765 object *list;
766 int i;
767
768 if (!getargs(args, "i", &algorithmMediaType))
769 return NULL;
770
771 error_handler_called = 0;
772 bufferlength = clQueryAlgorithms(algorithmMediaType, 0, 0);
773 if (error_handler_called)
774 return NULL;
775
776 PVbuffer = NEW(int, bufferlength);
777 if (PVbuffer == NULL)
778 return err_nomem();
779
780 bufferlength = clQueryAlgorithms(algorithmMediaType, PVbuffer,
781 bufferlength);
782 if (error_handler_called) {
783 DEL(PVbuffer);
784 return NULL;
785 }
786
787 list = newlistobject(bufferlength);
788 if (list == NULL) {
789 DEL(PVbuffer);
790 return NULL;
791 }
792
793 for (i = 0; i < bufferlength; i++) {
794 if (i & 1)
795 setlistitem(list, i, newintobject(PVbuffer[i]));
796 else if (PVbuffer[i] == 0) {
797 INCREF(None);
798 setlistitem(list, i, None);
799 } else
800 setlistitem(list, i, newstringobject((char *) PVbuffer[i]));
801 }
802
803 DEL(PVbuffer);
804
805 return list;
806}
807
808static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000809cl_QuerySchemeFromName(object *self, object *args)
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000810{
811 int algorithmMediaType;
812 char *name;
813 int scheme;
814
815 if (!getargs(args, "(is)", &algorithmMediaType, &name))
816 return NULL;
817
818 error_handler_called = 0;
819 scheme = clQuerySchemeFromName(algorithmMediaType, name);
820 if (error_handler_called) {
821 err_setstr(ClError, "unknown compression scheme");
822 return NULL;
823 }
824
825 return newintobject(scheme);
826}
827
828static object *
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000829cl_GetAlgorithmName(object *self, object *args)
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000830{
831 int scheme;
832 char *name;
833
834 if (!getargs(args, "i", &scheme))
835 return NULL;
836
837 name = clGetAlgorithmName(scheme);
838 if (name == 0) {
839 err_setstr(ClError, "unknown compression scheme");
840 return NULL;
841 }
842
843 return newstringobject(name);
844}
845
Sjoerd Mullender22e44cd1993-02-17 09:11:14 +0000846static object *
847do_set(object *self, object *args, int (*func)(int, int, int))
848{
849 int scheme, paramID, value;
850 float fvalue;
851 int is_float = 0;
852
853 if (!getargs(args, "(iii)", &scheme, &paramID, &value)) {
854 err_clear();
855 if (!getargs(args, "(iif)", &scheme, &paramID, &fvalue)) {
856 err_clear();
857 err_setstr(TypeError, "bad argument list (format '(iii)' or '(iif)')");
858 return NULL;
859 }
860 value = CL_TypeIsInt(fvalue);
861 is_float = 1;
862 } else {
863 /* check some parameters which we know to be floats */
864 switch (scheme) {
865 case CL_COMPRESSION_RATIO:
866 case CL_SPEED:
867 fvalue = value;
868 value = CL_TypeIsInt(fvalue);
869 is_float = 1;
870 break;
871 }
872 }
873
874 error_handler_called = 0;
875 value = (*func)(scheme, paramID, value);
876 if (error_handler_called)
877 return NULL;
878
879 if (is_float)
880 return newfloatobject(CL_TypeIsFloat(value));
881 else
882 return newintobject(value);
883}
884
885static object *
886cl_SetDefault(object *self, object *args)
887{
888 return do_set(self, args, clSetDefault);
889}
890
891static object *
892cl_SetMin(object *self, object *args)
893{
894 return do_set(self, args, clSetMin);
895}
896
897static object *
898cl_SetMax(object *self, object *args)
899{
900 return do_set(self, args, clSetMax);
901}
902
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000903#ifdef CLDEBUG
904static object *
905cvt_type(object *self, object *args)
906{
907 int number;
908 float fnumber;
909
910 if (getargs(args, "i", &number))
911 return newfloatobject(CL_TypeIsFloat(number));
912 else {
913 err_clear();
914 if (getargs(args, "f", &fnumber))
915 return newintobject(CL_TypeIsInt(fnumber));
916 return NULL;
917 }
918}
919#endif
920
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000921static struct methodlist cl_methods[] = {
Sjoerd Mullender8dd054d1992-12-14 13:17:29 +0000922 {"CompressImage", cl_CompressImage},
923 {"DecompressImage", cl_DecompressImage},
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000924 {"GetAlgorithmName", cl_GetAlgorithmName},
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000925 {"OpenCompressor", cl_OpenCompressor},
926 {"OpenDecompressor", cl_OpenDecompressor},
Sjoerd Mullender3a997271993-02-04 16:43:28 +0000927 {"QueryAlgorithms", cl_QueryAlgorithms},
928 {"QueryMaxHeaderSize", cl_QueryMaxHeaderSize},
929 {"QueryScheme", cl_QueryScheme},
930 {"QuerySchemeFromName", cl_QuerySchemeFromName},
Sjoerd Mullender22e44cd1993-02-17 09:11:14 +0000931 {"SetDefault", cl_SetDefault},
932 {"SetMax", cl_SetMax},
933 {"SetMin", cl_SetMin},
Sjoerd Mullender4e2a4271993-02-16 11:55:17 +0000934#ifdef CLDEBUG
935 {"cvt_type", cvt_type},
936#endif
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000937 {NULL, NULL} /* Sentinel */
938};
939
940void
941initcl()
942{
Sjoerd Mullenderd53a4f31992-09-24 10:37:39 +0000943 object *m, *d;
944
945 m = initmodule("cl", cl_methods);
946 d = getmoduledict(m);
947
948 ClError = newstringobject("cl.error");
949 if (ClError == NULL || dictinsert(d, "error", ClError) != 0)
950 fatal("can't define cl.error");
951
952 (void) clSetErrorHandler(cl_ErrorHandler);
Sjoerd Mullenderc4315491992-09-23 14:53:00 +0000953}