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 */ |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 37 | CL_Handle ob_compressorHdl; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 38 | } clobject; |
| 39 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 40 | static object *ClError; /* exception cl.error */ |
| 41 | |
| 42 | static int error_handler_called = 0; |
| 43 | |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 44 | /******************************************************************** |
| 45 | Utility routines. |
| 46 | ********************************************************************/ |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 47 | static void |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 48 | cl_ErrorHandler(CL_Handle handle, int code, const char *fmt, ...) |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 49 | { |
| 50 | va_list ap; |
| 51 | char errbuf[BUFSIZ]; /* hopefully big enough */ |
Sjoerd Mullender | 384f248 | 1992-09-29 16:43:43 +0000 | [diff] [blame] | 52 | char *p; |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 53 | |
Sjoerd Mullender | 384f248 | 1992-09-29 16:43:43 +0000 | [diff] [blame] | 54 | if (err_occurred()) /* don't change existing error */ |
| 55 | return; |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 56 | error_handler_called = 1; |
| 57 | va_start(ap, fmt); |
| 58 | vsprintf(errbuf, fmt, ap); |
| 59 | va_end(ap); |
Sjoerd Mullender | 384f248 | 1992-09-29 16:43:43 +0000 | [diff] [blame] | 60 | p = &errbuf[strlen(errbuf) - 1]; /* swat the line feed */ |
| 61 | if (*p == '\n') |
| 62 | *p = 0; |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 63 | err_setstr(ClError, errbuf); |
| 64 | } |
| 65 | |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 66 | /* |
| 67 | * This assumes that params are always in the range 0 to some maximum. |
| 68 | * This is not very efficient. |
| 69 | */ |
| 70 | static int |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 71 | param_type_is_float(CL_Handle comp, int param) |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 72 | { |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 73 | int bufferlength; |
| 74 | int *PVbuffer; |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 75 | int ret; |
| 76 | |
| 77 | error_handler_called = 0; |
| 78 | bufferlength = clQueryParams(comp, 0, 0); |
| 79 | if (error_handler_called) |
| 80 | return -1; |
| 81 | |
| 82 | if (param < 0 || param >= bufferlength / 2) |
| 83 | return -1; |
| 84 | |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 85 | PVbuffer = NEW(int, bufferlength); |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 86 | if (PVbuffer == NULL) |
| 87 | return -1; |
| 88 | |
| 89 | bufferlength = clQueryParams(comp, PVbuffer, bufferlength); |
| 90 | if (error_handler_called) { |
| 91 | DEL(PVbuffer); |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | if (PVbuffer[param*2 + 1] == CL_FLOATING_ENUM_VALUE || |
| 96 | PVbuffer[param*2 + 1] == CL_FLOATING_RANGE_VALUE) |
| 97 | ret = 1; |
| 98 | else |
| 99 | ret = 0; |
| 100 | |
| 101 | DEL(PVbuffer); |
| 102 | |
| 103 | return ret; |
| 104 | } |
| 105 | |
| 106 | /******************************************************************** |
| 107 | Single image compression/decompression. |
| 108 | ********************************************************************/ |
| 109 | static object * |
| 110 | cl_CompressImage(self, args) |
| 111 | object *self, *args; |
| 112 | { |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 113 | int compressionScheme, width, height, originalFormat; |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 114 | float compressionRatio; |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 115 | int frameBufferSize, compressedBufferSize; |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 116 | char *frameBuffer; |
| 117 | object *compressedBuffer; |
| 118 | |
| 119 | if (!getargs(args, "(iiiifs#)", &compressionScheme, &width, &height, |
| 120 | &originalFormat, &compressionRatio, &frameBuffer, |
| 121 | &frameBufferSize)) |
| 122 | return NULL; |
| 123 | |
| 124 | retry: |
| 125 | compressedBuffer = newsizedstringobject(NULL, frameBufferSize); |
| 126 | if (compressedBuffer == NULL) |
| 127 | return NULL; |
| 128 | |
| 129 | compressedBufferSize = frameBufferSize; |
| 130 | error_handler_called = 0; |
| 131 | if (clCompressImage(compressionScheme, width, height, originalFormat, |
| 132 | compressionRatio, (void *) frameBuffer, |
| 133 | &compressedBufferSize, |
| 134 | (void *) getstringvalue(compressedBuffer)) |
| 135 | == FAILURE) { |
| 136 | DECREF(compressedBuffer); |
| 137 | if (!error_handler_called) |
| 138 | err_setstr(ClError, "clCompressImage failed"); |
| 139 | return NULL; |
| 140 | } |
| 141 | |
| 142 | if (compressedBufferSize > frameBufferSize) { |
| 143 | frameBufferSize = compressedBufferSize; |
| 144 | DECREF(compressedBuffer); |
| 145 | goto retry; |
| 146 | } |
| 147 | |
| 148 | if (compressedBufferSize < frameBufferSize) |
| 149 | if (resizestring(&compressedBuffer, compressedBufferSize)) |
| 150 | return NULL; |
| 151 | |
| 152 | return compressedBuffer; |
| 153 | } |
| 154 | |
| 155 | static object * |
| 156 | cl_DecompressImage(self, args) |
| 157 | object *self, *args; |
| 158 | { |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 159 | int compressionScheme, width, height, originalFormat; |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 160 | char *compressedBuffer; |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 161 | int compressedBufferSize, frameBufferSize; |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 162 | object *frameBuffer; |
| 163 | |
| 164 | if (!getargs(args, "(iiiis#i)", &compressionScheme, &width, &height, |
| 165 | &originalFormat, &compressedBuffer, &compressedBufferSize, |
| 166 | &frameBufferSize)) |
| 167 | return NULL; |
| 168 | |
| 169 | frameBuffer = newsizedstringobject(NULL, frameBufferSize); |
| 170 | if (frameBuffer == NULL) |
| 171 | return NULL; |
| 172 | |
| 173 | error_handler_called = 0; |
| 174 | if (clDecompressImage(compressionScheme, width, height, originalFormat, |
| 175 | compressedBufferSize, compressedBuffer, |
| 176 | (void *) getstringvalue(frameBuffer)) == FAILURE) { |
| 177 | DECREF(frameBuffer); |
| 178 | if (!error_handler_called) |
| 179 | err_setstr(ClError, "clDecompressImage failed"); |
| 180 | return NULL; |
| 181 | } |
| 182 | |
| 183 | return frameBuffer; |
| 184 | } |
| 185 | |
| 186 | /******************************************************************** |
| 187 | Sequential compression/decompression. |
| 188 | ********************************************************************/ |
| 189 | extern typeobject Cltype; /* Really static, forward */ |
| 190 | |
| 191 | #define CheckCompressor(self) if ((self)->ob_compressorHdl == NULL) { \ |
| 192 | err_setstr(RuntimeError, "(de)compressor not active"); \ |
| 193 | return NULL; \ |
| 194 | } |
| 195 | |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 196 | static object * |
Sjoerd Mullender | 384f248 | 1992-09-29 16:43:43 +0000 | [diff] [blame] | 197 | doClose(self, args, close_func) |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 198 | clobject *self; |
| 199 | object *args; |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 200 | int (*close_func) PROTO((CL_Handle)); |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 201 | { |
| 202 | CheckCompressor(self); |
| 203 | |
| 204 | if (!getnoarg(args)) |
| 205 | return NULL; |
| 206 | |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 207 | error_handler_called = 0; |
Sjoerd Mullender | 384f248 | 1992-09-29 16:43:43 +0000 | [diff] [blame] | 208 | if ((*close_func)(self->ob_compressorHdl) == FAILURE) { |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 209 | if (!error_handler_called) |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 210 | err_setstr(ClError, "close failed"); |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 211 | return NULL; |
| 212 | } |
| 213 | |
| 214 | self->ob_compressorHdl = NULL; |
| 215 | |
| 216 | INCREF(None); |
| 217 | return None; |
| 218 | } |
| 219 | |
| 220 | static object * |
Sjoerd Mullender | 384f248 | 1992-09-29 16:43:43 +0000 | [diff] [blame] | 221 | clm_CloseCompressor(self, args) |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 222 | clobject *self; |
| 223 | object *args; |
| 224 | { |
Sjoerd Mullender | 384f248 | 1992-09-29 16:43:43 +0000 | [diff] [blame] | 225 | return doClose(self, args, clCloseCompressor); |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static object * |
Sjoerd Mullender | 384f248 | 1992-09-29 16:43:43 +0000 | [diff] [blame] | 229 | clm_CloseDecompressor(self, args) |
| 230 | clobject *self; |
| 231 | object *args; |
| 232 | { |
| 233 | return doClose(self, args, clCloseDecompressor); |
| 234 | } |
| 235 | |
| 236 | static object * |
| 237 | clm_Compress(self, args) |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 238 | clobject *self; |
| 239 | object *args; |
| 240 | { |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 241 | int numberOfFrames; |
| 242 | int frameBufferSize, compressedBufferSize; |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 243 | char *frameBuffer; |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 244 | int PVbuf[2]; |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 245 | object *data; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 246 | |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 247 | CheckCompressor(self); |
| 248 | |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 249 | if (!getargs(args, "(is#)", &numberOfFrames, &frameBuffer, &frameBufferSize)) |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 250 | return NULL; |
| 251 | |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 252 | PVbuf[0] = CL_COMPRESSED_BUFFER_SIZE; |
| 253 | PVbuf[1] = 0; |
| 254 | error_handler_called = 0; |
| 255 | clGetParams(self->ob_compressorHdl, PVbuf, 2L); |
| 256 | if (error_handler_called) |
| 257 | return NULL; |
| 258 | |
| 259 | data = newsizedstringobject(NULL, PVbuf[1]); |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 260 | if (data == NULL) |
| 261 | return NULL; |
| 262 | |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 263 | compressedBufferSize = PVbuf[1]; |
| 264 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 265 | error_handler_called = 0; |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 266 | if (clCompress(self->ob_compressorHdl, numberOfFrames, |
| 267 | (void *) frameBuffer, &compressedBufferSize, |
| 268 | (void *) getstringvalue(data)) == FAILURE) { |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 269 | DECREF(data); |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 270 | if (!error_handler_called) |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 271 | err_setstr(ClError, "compress failed"); |
| 272 | return NULL; |
| 273 | } |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 274 | |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 275 | if (compressedBufferSize < PVbuf[1]) |
| 276 | if (resizestring(&data, compressedBufferSize)) |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 277 | return NULL; |
| 278 | |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 279 | if (compressedBufferSize > PVbuf[1]) { |
| 280 | /* we didn't get all "compressed" data */ |
Sjoerd Mullender | 384f248 | 1992-09-29 16:43:43 +0000 | [diff] [blame] | 281 | DECREF(data); |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 282 | err_setstr(ClError, "compressed data is more than fitted"); |
Sjoerd Mullender | 384f248 | 1992-09-29 16:43:43 +0000 | [diff] [blame] | 283 | return NULL; |
| 284 | } |
| 285 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 286 | return data; |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 287 | } |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 288 | |
| 289 | static object * |
Sjoerd Mullender | 384f248 | 1992-09-29 16:43:43 +0000 | [diff] [blame] | 290 | clm_Decompress(self, args) |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 291 | clobject *self; |
| 292 | object *args; |
| 293 | { |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 294 | int PVbuf[2]; |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 295 | object *data; |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 296 | int numberOfFrames; |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 297 | char *compressedData; |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 298 | int compressedDataSize; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 299 | |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 300 | CheckCompressor(self); |
| 301 | |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 302 | if (!getargs(args, "(is#)", &numberOfFrames, &compressedData, |
| 303 | &compressedDataSize)) |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 304 | return NULL; |
| 305 | |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 306 | PVbuf[0] = CL_FRAME_BUFFER_SIZE; |
| 307 | PVbuf[1] = 0; |
| 308 | error_handler_called = 0; |
| 309 | clGetParams(self->ob_compressorHdl, PVbuf, 2L); |
| 310 | if (error_handler_called) |
| 311 | return NULL; |
| 312 | |
| 313 | data = newsizedstringobject(NULL, PVbuf[1]); |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 314 | if (data == NULL) |
| 315 | return NULL; |
| 316 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 317 | error_handler_called = 0; |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 318 | if (clDecompress(self->ob_compressorHdl, numberOfFrames, |
| 319 | compressedDataSize, (void *) compressedData, |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 320 | (void *) getstringvalue(data)) == FAILURE) { |
| 321 | DECREF(data); |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 322 | if (!error_handler_called) |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 323 | err_setstr(ClError, "decompress failed"); |
| 324 | return NULL; |
| 325 | } |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 326 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 327 | return data; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static object * |
| 331 | doParams(self, args, func, modified) |
| 332 | clobject *self; |
| 333 | object *args; |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 334 | void (*func)(CL_Handle, int *, int); |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 335 | int modified; |
| 336 | { |
| 337 | object *list, *v; |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 338 | int *PVbuffer; |
| 339 | int length; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 340 | int i; |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 341 | float number; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 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 (!getargs(args, "O", &list)) |
| 346 | return NULL; |
| 347 | if (!is_listobject(list)) { |
| 348 | err_badarg(); |
| 349 | return NULL; |
| 350 | } |
| 351 | length = getlistsize(list); |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 352 | PVbuffer = NEW(int, length); |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 353 | if (PVbuffer == NULL) |
| 354 | return err_nomem(); |
| 355 | for (i = 0; i < length; i++) { |
| 356 | v = getlistitem(list, i); |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 357 | if (is_floatobject(v)) { |
| 358 | number = getfloatvalue(v); |
| 359 | PVbuffer[i] = CL_TypeIsInt(number); |
| 360 | } else if (is_intobject(v)) |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 361 | PVbuffer[i] = getintvalue(v); |
| 362 | else { |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 363 | DEL(PVbuffer); |
| 364 | err_badarg(); |
| 365 | return NULL; |
| 366 | } |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 369 | error_handler_called = 0; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 370 | (*func)(self->ob_compressorHdl, PVbuffer, length); |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 371 | if (error_handler_called) |
| 372 | return NULL; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 373 | |
| 374 | if (modified) { |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 375 | for (i = 0; i < length; i++) { |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 376 | if ((i & 1) && |
| 377 | param_type_is_float(self->ob_compressorHdl, |
| 378 | PVbuffer[i-1]) > 0) { |
| 379 | number = CL_TypeIsFloat(PVbuffer[i]); |
| 380 | v = newfloatobject(number); |
| 381 | } else |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 382 | v = newintobject(PVbuffer[i]); |
| 383 | setlistitem(list, i, v); |
| 384 | } |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | DEL(PVbuffer); |
| 388 | |
| 389 | INCREF(None); |
| 390 | return None; |
| 391 | } |
| 392 | |
| 393 | static object * |
Sjoerd Mullender | 384f248 | 1992-09-29 16:43:43 +0000 | [diff] [blame] | 394 | clm_GetParams(self, args) |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 395 | clobject *self; |
| 396 | object *args; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 397 | { |
| 398 | return doParams(self, args, clGetParams, 1); |
| 399 | } |
| 400 | |
| 401 | static object * |
Sjoerd Mullender | 384f248 | 1992-09-29 16:43:43 +0000 | [diff] [blame] | 402 | clm_SetParams(self, args) |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 403 | clobject *self; |
| 404 | object *args; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 405 | { |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 406 | return doParams(self, args, clSetParams, 0); |
| 407 | } |
| 408 | |
| 409 | static object * |
| 410 | do_get(self, args, func) |
| 411 | clobject *self; |
| 412 | object *args; |
| 413 | int (*func)(CL_Handle, int); |
| 414 | { |
| 415 | int paramID, value; |
| 416 | float fvalue; |
| 417 | |
| 418 | CheckCompressor(self); |
| 419 | |
| 420 | if (!getargs(args, "i", ¶mID)) |
| 421 | return NULL; |
| 422 | |
| 423 | error_handler_called = 0; |
| 424 | value = (*func)(self->ob_compressorHdl, paramID); |
| 425 | if (error_handler_called) |
| 426 | return NULL; |
| 427 | |
| 428 | if (param_type_is_float(self->ob_compressorHdl, paramID) > 0) { |
| 429 | fvalue = CL_TypeIsFloat(value); |
| 430 | return newfloatobject(fvalue); |
| 431 | } |
| 432 | |
| 433 | return newintobject(value); |
| 434 | } |
| 435 | |
| 436 | static object * |
| 437 | clm_GetParam(self, args) |
| 438 | clobject *self; |
| 439 | object *args; |
| 440 | { |
| 441 | return do_get(self, args, clGetParam); |
| 442 | } |
| 443 | |
| 444 | static object * |
| 445 | clm_GetDefault(self, args) |
| 446 | clobject *self; |
| 447 | object *args; |
| 448 | { |
| 449 | return do_get(self, args, clGetDefault); |
| 450 | } |
| 451 | |
| 452 | static object * |
| 453 | do_set(self, args, func) |
| 454 | clobject *self; |
| 455 | object *args; |
| 456 | int (*func)(CL_Handle, int, int); |
| 457 | { |
| 458 | int paramID, value; |
| 459 | float fvalue; |
| 460 | |
| 461 | CheckCompressor(self); |
| 462 | |
| 463 | if (!getargs(args, "(ii)", ¶mID, &value)) { |
| 464 | err_clear(); |
| 465 | if (!getargs(args, "(if)", ¶mID, &fvalue)) { |
| 466 | err_clear(); |
| 467 | err_setstr(TypeError, "bad argument list (format '(ii)' or '(if)')"); |
| 468 | return NULL; |
| 469 | } |
| 470 | value = CL_TypeIsInt(fvalue); |
| 471 | } |
| 472 | |
| 473 | error_handler_called = 0; |
| 474 | value = (*func)(self->ob_compressorHdl, paramID, value); |
| 475 | if (error_handler_called) |
| 476 | return NULL; |
| 477 | |
| 478 | if (param_type_is_float(self->ob_compressorHdl, paramID) > 0) |
| 479 | return newfloatobject(CL_TypeIsFloat(value)); |
| 480 | else |
| 481 | return newintobject(value); |
| 482 | } |
| 483 | |
| 484 | static object * |
| 485 | clm_SetParam(self, args) |
| 486 | clobject *self; |
| 487 | object *args; |
| 488 | { |
| 489 | return do_set(self, args, clSetParam); |
| 490 | } |
| 491 | |
| 492 | static object * |
| 493 | clm_SetDefault(self, args) |
| 494 | clobject *self; |
| 495 | object *args; |
| 496 | { |
| 497 | return do_set(self, args, clSetDefault); |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static object * |
| 501 | clm_GetParamID(self, args) |
| 502 | clobject *self; |
| 503 | object *args; |
| 504 | { |
| 505 | char *name; |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 506 | int value; |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 507 | |
| 508 | CheckCompressor(self); |
| 509 | |
| 510 | if (!getargs(args, "s", &name)) |
| 511 | return NULL; |
| 512 | |
| 513 | error_handler_called = 0; |
| 514 | value = clGetParamID(self->ob_compressorHdl, name); |
| 515 | if (value == FAILURE) { |
| 516 | if (!error_handler_called) |
| 517 | err_setstr(ClError, "getparamid failed"); |
| 518 | return NULL; |
| 519 | } |
| 520 | |
| 521 | return newintobject(value); |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | static object * |
Sjoerd Mullender | 384f248 | 1992-09-29 16:43:43 +0000 | [diff] [blame] | 525 | clm_QueryParams(self, args) |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 526 | clobject *self; |
| 527 | object *args; |
| 528 | { |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 529 | int bufferlength; |
| 530 | int *PVbuffer; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 531 | object *list; |
| 532 | int i; |
| 533 | |
Sjoerd Mullender | 37f17b7 | 1992-09-25 10:28:20 +0000 | [diff] [blame] | 534 | CheckCompressor(self); |
| 535 | |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 536 | if (!getnoarg(args)) |
| 537 | return NULL; |
| 538 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 539 | error_handler_called = 0; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 540 | bufferlength = clQueryParams(self->ob_compressorHdl, 0, 0); |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 541 | if (error_handler_called) |
| 542 | return NULL; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 543 | |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 544 | PVbuffer = NEW(int, bufferlength); |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 545 | if (PVbuffer == NULL) |
| 546 | return err_nomem(); |
| 547 | |
| 548 | bufferlength = clQueryParams(self->ob_compressorHdl, PVbuffer, |
| 549 | bufferlength); |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 550 | if (error_handler_called) { |
| 551 | DEL(PVbuffer); |
| 552 | return NULL; |
| 553 | } |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 554 | |
| 555 | list = newlistobject(bufferlength); |
| 556 | if (list == NULL) { |
| 557 | DEL(PVbuffer); |
| 558 | return NULL; |
| 559 | } |
| 560 | |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 561 | for (i = 0; i < bufferlength; i++) { |
| 562 | if (i & 1) |
| 563 | setlistitem(list, i, newintobject(PVbuffer[i])); |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 564 | else if (PVbuffer[i] == 0) { |
| 565 | INCREF(None); |
| 566 | setlistitem(list, i, None); |
| 567 | } else |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 568 | setlistitem(list, i, newstringobject((char *) PVbuffer[i])); |
| 569 | } |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 570 | |
| 571 | DEL(PVbuffer); |
| 572 | |
| 573 | return list; |
| 574 | } |
| 575 | |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 576 | static object * |
| 577 | clm_GetMinMax(self, args) |
| 578 | clobject *self; |
| 579 | object *args; |
| 580 | { |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 581 | int param, min, max; |
| 582 | float fmin, fmax; |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 583 | |
| 584 | CheckCompressor(self); |
| 585 | |
| 586 | if (!getargs(args, "i", ¶m)) |
| 587 | return NULL; |
| 588 | |
| 589 | clGetMinMax(self->ob_compressorHdl, param, &min, &max); |
| 590 | |
| 591 | if (param_type_is_float(self->ob_compressorHdl, param) > 0) { |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 592 | fmin = CL_TypeIsFloat(min); |
| 593 | fmax = CL_TypeIsFloat(max); |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 594 | return mkvalue("(ff)", fmin, fmax); |
| 595 | } |
| 596 | |
| 597 | return mkvalue("(ii)", min, max); |
| 598 | } |
| 599 | |
| 600 | static object * |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 601 | clm_SetMin(self, args) |
| 602 | clobject *self; |
| 603 | object *args; |
| 604 | { |
| 605 | return do_set(self, args, clSetMin); |
| 606 | } |
| 607 | |
| 608 | static object * |
| 609 | clm_SetMax(self, args) |
| 610 | clobject *self; |
| 611 | object *args; |
| 612 | { |
| 613 | return do_set(self, args, clSetMax); |
| 614 | } |
| 615 | |
| 616 | static object * |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 617 | clm_GetName(self, args) |
| 618 | clobject *self; |
| 619 | object *args; |
| 620 | { |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 621 | int param; |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 622 | char *name; |
| 623 | |
| 624 | CheckCompressor(self); |
| 625 | |
| 626 | if (!getargs(args, "i", ¶m)) |
| 627 | return NULL; |
| 628 | |
| 629 | error_handler_called = 0; |
| 630 | name = clGetName(self->ob_compressorHdl, param); |
| 631 | if (name == NULL || error_handler_called) { |
| 632 | if (!error_handler_called) |
| 633 | err_setstr(ClError, "getname failed"); |
| 634 | return NULL; |
| 635 | } |
| 636 | |
| 637 | return newstringobject(name); |
| 638 | } |
| 639 | |
| 640 | static object * |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 641 | clm_ReadHeader(self, args) |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 642 | clobject *self; |
| 643 | object *args; |
| 644 | { |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 645 | char *header; |
| 646 | int headerSize; |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 647 | |
| 648 | CheckCompressor(self); |
| 649 | |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 650 | if (!getargs(args, "s#", &header, &headerSize)) |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 651 | return NULL; |
| 652 | |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 653 | return newintobject(clReadHeader(self->ob_compressorHdl, |
| 654 | headerSize, header)); |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 657 | static struct methodlist compressor_methods[] = { |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 658 | {"close", clm_CloseCompressor}, /* alias */ |
Sjoerd Mullender | 384f248 | 1992-09-29 16:43:43 +0000 | [diff] [blame] | 659 | {"CloseCompressor", clm_CloseCompressor}, |
| 660 | {"Compress", clm_Compress}, |
Sjoerd Mullender | 384f248 | 1992-09-29 16:43:43 +0000 | [diff] [blame] | 661 | {"GetDefault", clm_GetDefault}, |
| 662 | {"GetMinMax", clm_GetMinMax}, |
| 663 | {"GetName", clm_GetName}, |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 664 | {"GetParam", clm_GetParam}, |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 665 | {"GetParamID", clm_GetParamID}, |
Sjoerd Mullender | 384f248 | 1992-09-29 16:43:43 +0000 | [diff] [blame] | 666 | {"GetParams", clm_GetParams}, |
| 667 | {"QueryParams", clm_QueryParams}, |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 668 | {"SetDefault", clm_SetDefault}, |
| 669 | {"SetMax", clm_SetMax}, |
| 670 | {"SetMin", clm_SetMin}, |
| 671 | {"SetParam", clm_SetParam}, |
Sjoerd Mullender | 384f248 | 1992-09-29 16:43:43 +0000 | [diff] [blame] | 672 | {"SetParams", clm_SetParams}, |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 673 | {NULL, NULL} /* sentinel */ |
| 674 | }; |
| 675 | |
| 676 | static struct methodlist decompressor_methods[] = { |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 677 | {"close", clm_CloseDecompressor}, /* alias */ |
Sjoerd Mullender | 384f248 | 1992-09-29 16:43:43 +0000 | [diff] [blame] | 678 | {"CloseDecompressor", clm_CloseDecompressor}, |
| 679 | {"Decompress", clm_Decompress}, |
| 680 | {"GetDefault", clm_GetDefault}, |
| 681 | {"GetMinMax", clm_GetMinMax}, |
| 682 | {"GetName", clm_GetName}, |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 683 | {"GetParam", clm_GetParam}, |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 684 | {"GetParamID", clm_GetParamID}, |
Sjoerd Mullender | 384f248 | 1992-09-29 16:43:43 +0000 | [diff] [blame] | 685 | {"GetParams", clm_GetParams}, |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 686 | {"ReadHeader", clm_ReadHeader}, |
Sjoerd Mullender | 384f248 | 1992-09-29 16:43:43 +0000 | [diff] [blame] | 687 | {"QueryParams", clm_QueryParams}, |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 688 | {"SetDefault", clm_SetDefault}, |
| 689 | {"SetMax", clm_SetMax}, |
| 690 | {"SetMin", clm_SetMin}, |
| 691 | {"SetParam", clm_SetParam}, |
Sjoerd Mullender | 384f248 | 1992-09-29 16:43:43 +0000 | [diff] [blame] | 692 | {"SetParams", clm_SetParams}, |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 693 | {NULL, NULL} /* sentinel */ |
| 694 | }; |
| 695 | |
| 696 | static void |
| 697 | cl_dealloc(self) |
| 698 | clobject *self; |
| 699 | { |
| 700 | if (self->ob_compressorHdl) { |
| 701 | if (self->ob_isCompressor) |
| 702 | clCloseCompressor(self->ob_compressorHdl); |
| 703 | else |
| 704 | clCloseDecompressor(self->ob_compressorHdl); |
| 705 | } |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 706 | DEL(self); |
| 707 | } |
| 708 | |
| 709 | static object * |
| 710 | cl_getattr(self, name) |
| 711 | clobject *self; |
| 712 | char *name; |
| 713 | { |
| 714 | if (self->ob_isCompressor) |
| 715 | return findmethod(compressor_methods, (object *)self, name); |
| 716 | else |
| 717 | return findmethod(decompressor_methods, (object *) self, name); |
| 718 | } |
| 719 | |
| 720 | static typeobject Cltype = { |
| 721 | OB_HEAD_INIT(&Typetype) |
| 722 | 0, /*ob_size*/ |
| 723 | "cl", /*tp_name*/ |
| 724 | sizeof(clobject), /*tp_size*/ |
| 725 | 0, /*tp_itemsize*/ |
| 726 | /* methods */ |
| 727 | cl_dealloc, /*tp_dealloc*/ |
| 728 | 0, /*tp_print*/ |
| 729 | cl_getattr, /*tp_getattr*/ |
| 730 | 0, /*tp_setattr*/ |
| 731 | 0, /*tp_compare*/ |
| 732 | 0, /*tp_repr*/ |
| 733 | 0, /*tp_as_number*/ |
| 734 | 0, /*tp_as_sequence*/ |
| 735 | 0, /*tp_as_mapping*/ |
| 736 | }; |
| 737 | |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 738 | static object * |
| 739 | doOpen(self, args, open_func, iscompressor) |
| 740 | object *self, *args; |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 741 | int (*open_func) PROTO((int, CL_Handle *)); |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 742 | int iscompressor; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 743 | { |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 744 | int scheme; |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 745 | clobject *new; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 746 | |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 747 | if (!getargs(args, "i", &scheme)) |
| 748 | return NULL; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 749 | |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 750 | new = NEWOBJ(clobject, &Cltype); |
| 751 | if (new == NULL) |
| 752 | return NULL; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 753 | |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 754 | new->ob_compressorHdl = NULL; |
| 755 | new->ob_isCompressor = iscompressor; |
| 756 | |
| 757 | error_handler_called = 0; |
| 758 | if ((*open_func)(scheme, &new->ob_compressorHdl) == FAILURE) { |
| 759 | DECREF(new); |
| 760 | if (!error_handler_called) |
| 761 | err_setstr(ClError, "Open(De)Compressor failed"); |
| 762 | return NULL; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 763 | } |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 764 | return new; |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | static object * |
| 768 | cl_OpenCompressor(self, args) |
| 769 | object *self, *args; |
| 770 | { |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 771 | return doOpen(self, args, clOpenCompressor, 1); |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | static object * |
| 775 | cl_OpenDecompressor(self, args) |
| 776 | object *self, *args; |
| 777 | { |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 778 | return doOpen(self, args, clOpenDecompressor, 0); |
Sjoerd Mullender | 384f248 | 1992-09-29 16:43:43 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 781 | static object * |
| 782 | cl_QueryScheme(self, args) |
| 783 | object *self, *args; |
| 784 | { |
| 785 | char *header; |
| 786 | int headerlen; |
| 787 | int scheme; |
| 788 | |
| 789 | if (!getargs(args, "s#", &header, &headerlen)) |
| 790 | return NULL; |
| 791 | |
| 792 | scheme = clQueryScheme(header); |
| 793 | if (scheme < 0) { |
| 794 | err_setstr(ClError, "unknown compression scheme"); |
| 795 | return NULL; |
| 796 | } |
| 797 | |
| 798 | return newintobject(scheme); |
| 799 | } |
| 800 | |
| 801 | static object * |
| 802 | cl_QueryMaxHeaderSize(self, args) |
| 803 | object *self, *args; |
| 804 | { |
| 805 | int scheme; |
| 806 | |
| 807 | if (!getargs(args, "i", &scheme)) |
| 808 | return NULL; |
| 809 | |
| 810 | return newintobject(clQueryMaxHeaderSize(scheme)); |
| 811 | } |
| 812 | |
| 813 | static object * |
| 814 | cl_QueryAlgorithms(self, args) |
| 815 | object *self, *args; |
| 816 | { |
| 817 | int algorithmMediaType; |
| 818 | int bufferlength; |
| 819 | int *PVbuffer; |
| 820 | object *list; |
| 821 | int i; |
| 822 | |
| 823 | if (!getargs(args, "i", &algorithmMediaType)) |
| 824 | return NULL; |
| 825 | |
| 826 | error_handler_called = 0; |
| 827 | bufferlength = clQueryAlgorithms(algorithmMediaType, 0, 0); |
| 828 | if (error_handler_called) |
| 829 | return NULL; |
| 830 | |
| 831 | PVbuffer = NEW(int, bufferlength); |
| 832 | if (PVbuffer == NULL) |
| 833 | return err_nomem(); |
| 834 | |
| 835 | bufferlength = clQueryAlgorithms(algorithmMediaType, PVbuffer, |
| 836 | bufferlength); |
| 837 | if (error_handler_called) { |
| 838 | DEL(PVbuffer); |
| 839 | return NULL; |
| 840 | } |
| 841 | |
| 842 | list = newlistobject(bufferlength); |
| 843 | if (list == NULL) { |
| 844 | DEL(PVbuffer); |
| 845 | return NULL; |
| 846 | } |
| 847 | |
| 848 | for (i = 0; i < bufferlength; i++) { |
| 849 | if (i & 1) |
| 850 | setlistitem(list, i, newintobject(PVbuffer[i])); |
| 851 | else if (PVbuffer[i] == 0) { |
| 852 | INCREF(None); |
| 853 | setlistitem(list, i, None); |
| 854 | } else |
| 855 | setlistitem(list, i, newstringobject((char *) PVbuffer[i])); |
| 856 | } |
| 857 | |
| 858 | DEL(PVbuffer); |
| 859 | |
| 860 | return list; |
| 861 | } |
| 862 | |
| 863 | static object * |
| 864 | cl_QuerySchemeFromName(self, args) |
| 865 | object *self, *args; |
| 866 | { |
| 867 | int algorithmMediaType; |
| 868 | char *name; |
| 869 | int scheme; |
| 870 | |
| 871 | if (!getargs(args, "(is)", &algorithmMediaType, &name)) |
| 872 | return NULL; |
| 873 | |
| 874 | error_handler_called = 0; |
| 875 | scheme = clQuerySchemeFromName(algorithmMediaType, name); |
| 876 | if (error_handler_called) { |
| 877 | err_setstr(ClError, "unknown compression scheme"); |
| 878 | return NULL; |
| 879 | } |
| 880 | |
| 881 | return newintobject(scheme); |
| 882 | } |
| 883 | |
| 884 | static object * |
| 885 | cl_GetAlgorithmName(self, args) |
| 886 | object *self, *args; |
| 887 | { |
| 888 | int scheme; |
| 889 | char *name; |
| 890 | |
| 891 | if (!getargs(args, "i", &scheme)) |
| 892 | return NULL; |
| 893 | |
| 894 | name = clGetAlgorithmName(scheme); |
| 895 | if (name == 0) { |
| 896 | err_setstr(ClError, "unknown compression scheme"); |
| 897 | return NULL; |
| 898 | } |
| 899 | |
| 900 | return newstringobject(name); |
| 901 | } |
| 902 | |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 903 | static struct methodlist cl_methods[] = { |
Sjoerd Mullender | 8dd054d | 1992-12-14 13:17:29 +0000 | [diff] [blame] | 904 | {"CompressImage", cl_CompressImage}, |
| 905 | {"DecompressImage", cl_DecompressImage}, |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 906 | {"GetAlgorithmName", cl_GetAlgorithmName}, |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 907 | {"OpenCompressor", cl_OpenCompressor}, |
| 908 | {"OpenDecompressor", cl_OpenDecompressor}, |
Sjoerd Mullender | 3a99727 | 1993-02-04 16:43:28 +0000 | [diff] [blame^] | 909 | {"QueryAlgorithms", cl_QueryAlgorithms}, |
| 910 | {"QueryMaxHeaderSize", cl_QueryMaxHeaderSize}, |
| 911 | {"QueryScheme", cl_QueryScheme}, |
| 912 | {"QuerySchemeFromName", cl_QuerySchemeFromName}, |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 913 | {NULL, NULL} /* Sentinel */ |
| 914 | }; |
| 915 | |
| 916 | void |
| 917 | initcl() |
| 918 | { |
Sjoerd Mullender | d53a4f3 | 1992-09-24 10:37:39 +0000 | [diff] [blame] | 919 | object *m, *d; |
| 920 | |
| 921 | m = initmodule("cl", cl_methods); |
| 922 | d = getmoduledict(m); |
| 923 | |
| 924 | ClError = newstringobject("cl.error"); |
| 925 | if (ClError == NULL || dictinsert(d, "error", ClError) != 0) |
| 926 | fatal("can't define cl.error"); |
| 927 | |
| 928 | (void) clSetErrorHandler(cl_ErrorHandler); |
Sjoerd Mullender | c431549 | 1992-09-23 14:53:00 +0000 | [diff] [blame] | 929 | } |