Source

cpython-withatomic / Modules / clmodule.c

The branch 'legacy-trunk' does not exist.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
/***********************************************************
Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
Amsterdam, The Netherlands.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the names of Stichting Mathematisch
Centrum or CWI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.

STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

******************************************************************/


/* Cl objects */

#define CLDEBUG

#include <stdarg.h>
#include <cl.h>
#include "allobjects.h"
#include "modsupport.h"		/* For getargs() etc. */
#include "ceval.h"		/* For call_object() */

typedef struct {
	OB_HEAD
	int ob_isCompressor;	/* Compressor or Decompressor */
	CL_Handle ob_compressorHdl;
	int *ob_paramtypes;
	int ob_nparams;
} clobject;

static object *ClError;		/* exception cl.error */

static int error_handler_called = 0;

/*
 * We want to use the function prototypes that are available in the C
 * compiler on the SGI.  Because of that, we need to declare the first
 * argument of the compressor and decompressor methods as "object *",
 * even though they are really "clobject *".  Therefore we cast the
 * argument to the proper type using this macro.
 */
#define SELF	((clobject *) self)

/********************************************************************
			  Utility routines.
********************************************************************/
static void
cl_ErrorHandler(CL_Handle handle, int code, const char *fmt, ...)
{
	va_list ap;
	char errbuf[BUFSIZ];	/* hopefully big enough */
	char *p;

	if (err_occurred())	/* don't change existing error */
		return;
	error_handler_called = 1;
	va_start(ap, fmt);
	vsprintf(errbuf, fmt, ap);
	va_end(ap);
	p = &errbuf[strlen(errbuf) - 1]; /* swat the line feed */
	if (*p == '\n')
		*p = 0;
	err_setstr(ClError, errbuf);
}

/*
 * This assumes that params are always in the range 0 to some maximum.
 */
static int
param_type_is_float(clobject *self, int param)
{
	int bufferlength;
	int ret;

	if (self->ob_paramtypes == NULL) {
		error_handler_called = 0;
		bufferlength = clQueryParams(self->ob_compressorHdl, 0, 0);
		if (error_handler_called)
			return -1;

		self->ob_paramtypes = NEW(int, bufferlength);
		if (self->ob_paramtypes == NULL)
			return -1;
		self->ob_nparams = bufferlength / 2;

		(void) clQueryParams(self->ob_compressorHdl, self->ob_paramtypes, bufferlength);
		if (error_handler_called) {
			DEL(self->ob_paramtypes);
			self->ob_paramtypes = NULL;
			return -1;
		}
	}

	if (param < 0 || param >= self->ob_nparams)
		return -1;

	if (self->ob_paramtypes[param*2 + 1] == CL_FLOATING_ENUM_VALUE ||
	    self->ob_paramtypes[param*2 + 1] == CL_FLOATING_RANGE_VALUE)
		return 1;
	else
		return 0;
}

/********************************************************************
	       Single image compression/decompression.
********************************************************************/
static object *
cl_CompressImage(object *self, object *args)
{
	int compressionScheme, width, height, originalFormat;
	float compressionRatio;
	int frameBufferSize, compressedBufferSize;
	char *frameBuffer;
	object *compressedBuffer;

	if (!getargs(args, "(iiiifs#)", &compressionScheme, &width, &height,
		     &originalFormat, &compressionRatio, &frameBuffer,
		     &frameBufferSize))
		return NULL;

 retry:
	compressedBuffer = newsizedstringobject(NULL, frameBufferSize);
	if (compressedBuffer == NULL)
		return NULL;

	compressedBufferSize = frameBufferSize;
	error_handler_called = 0;
	if (clCompressImage(compressionScheme, width, height, originalFormat,
			    compressionRatio, (void *) frameBuffer,
			    &compressedBufferSize,
			    (void *) getstringvalue(compressedBuffer))
	    == FAILURE) {
		DECREF(compressedBuffer);
		if (!error_handler_called)
			err_setstr(ClError, "clCompressImage failed");
		return NULL;
	}

	if (compressedBufferSize > frameBufferSize) {
		frameBufferSize = compressedBufferSize;
		DECREF(compressedBuffer);
		goto retry;
	}

	if (compressedBufferSize < frameBufferSize)
		if (resizestring(&compressedBuffer, compressedBufferSize))
			return NULL;

	return compressedBuffer;
}

static object *
cl_DecompressImage(object *self, object *args)
{
	int compressionScheme, width, height, originalFormat;
	char *compressedBuffer;
	int compressedBufferSize, frameBufferSize;
	object *frameBuffer;

	if (!getargs(args, "(iiiis#)", &compressionScheme, &width, &height,
		     &originalFormat, &compressedBuffer,
		     &compressedBufferSize))
		return NULL;

	frameBufferSize = width * height * CL_BytesPerPixel(originalFormat);

	frameBuffer = newsizedstringobject(NULL, frameBufferSize);
	if (frameBuffer == NULL)
		return NULL;

	error_handler_called = 0;
	if (clDecompressImage(compressionScheme, width, height, originalFormat,
			      compressedBufferSize, compressedBuffer,
			      (void *) getstringvalue(frameBuffer)) == FAILURE) {
		DECREF(frameBuffer);
		if (!error_handler_called)
			err_setstr(ClError, "clDecompressImage failed");
		return NULL;
	}

	return frameBuffer;
}

/********************************************************************
		Sequential compression/decompression.
********************************************************************/
#define CheckCompressor(self)	if ((self)->ob_compressorHdl == NULL) { \
					err_setstr(RuntimeError, "(de)compressor not active"); \
					return NULL; \
				}

static object *
doClose(clobject *self, object *args, int (*close_func)(CL_Handle))
{
	CheckCompressor(self);

	if (!getnoarg(args))
		return NULL;

	error_handler_called = 0;
	if ((*close_func)(self->ob_compressorHdl) == FAILURE) {
		if (!error_handler_called)
			err_setstr(ClError, "close failed");
		return NULL;
	}

	self->ob_compressorHdl = NULL;

	if (self->ob_paramtypes)
		DEL(self->ob_paramtypes);
	self->ob_paramtypes = NULL;

	INCREF(None);
	return None;
}

static object *
clm_CloseCompressor(object *self, object *args)
{
	return doClose(SELF, args, clCloseCompressor);
}

static object *
clm_CloseDecompressor(object *self, object *args)
{
	return doClose(SELF, args, clCloseDecompressor);
}

static object *
clm_Compress(object *self, object *args)
{
	int numberOfFrames;
	int frameBufferSize, compressedBufferSize, size;
	char *frameBuffer;
	object *data;

	CheckCompressor(SELF);

	if (!getargs(args, "(is#)", &numberOfFrames, &frameBuffer, &frameBufferSize))
		return NULL;

	error_handler_called = 0;
	size = clGetParam(SELF->ob_compressorHdl, CL_COMPRESSED_BUFFER_SIZE);
	compressedBufferSize = size;
	if (error_handler_called)
		return NULL;

	data = newsizedstringobject(NULL, size);
	if (data == NULL)
		return NULL;

	error_handler_called = 0;
	if (clCompress(SELF->ob_compressorHdl, numberOfFrames,
		       (void *) frameBuffer, &compressedBufferSize,
		       (void *) getstringvalue(data)) == FAILURE) {
		DECREF(data);
		if (!error_handler_called)
			err_setstr(ClError, "compress failed");
		return NULL;
	}

	if (compressedBufferSize < size)
		if (resizestring(&data, compressedBufferSize))
			return NULL;

	if (compressedBufferSize > size) {
		/* we didn't get all "compressed" data */
		DECREF(data);
		err_setstr(ClError, "compressed data is more than fitted");
		return NULL;
	}

	return data;
}

static object *
clm_Decompress(object *self, object *args)
{
	object *data;
	int numberOfFrames;
	char *compressedData;
	int compressedDataSize, dataSize;

	CheckCompressor(SELF);

	if (!getargs(args, "(is#)", &numberOfFrames, &compressedData,
		     &compressedDataSize))
		return NULL;

	error_handler_called = 0;
	dataSize = clGetParam(SELF->ob_compressorHdl, CL_FRAME_BUFFER_SIZE);
	if (error_handler_called)
		return NULL;

	data = newsizedstringobject(NULL, dataSize);
	if (data == NULL)
		return NULL;

	error_handler_called = 0;
	if (clDecompress(SELF->ob_compressorHdl, numberOfFrames,
			 compressedDataSize, (void *) compressedData,
			 (void *) getstringvalue(data)) == FAILURE) {
		DECREF(data);
		if (!error_handler_called)
			err_setstr(ClError, "decompress failed");
		return NULL;
	}

	return data;
}

static object *
doParams(clobject *self, object *args, int (*func)(CL_Handle, int *, int),
	 int modified)
{
	object *list, *v;
	int *PVbuffer;
	int length;
	int i;
	float number;
	
	CheckCompressor(self);

	if (!getargs(args, "O", &list))
		return NULL;
	if (!is_listobject(list)) {
		err_badarg();
		return NULL;
	}
	length = getlistsize(list);
	PVbuffer = NEW(int, length);
	if (PVbuffer == NULL)
		return err_nomem();
	for (i = 0; i < length; i++) {
		v = getlistitem(list, i);
		if (is_floatobject(v)) {
			number = getfloatvalue(v);
			PVbuffer[i] = CL_TypeIsInt(number);
		} else if (is_intobject(v)) {
			PVbuffer[i] = getintvalue(v);
			if ((i & 1) &&
			    param_type_is_float(self, PVbuffer[i-1]) > 0) {
				number = PVbuffer[i];
				PVbuffer[i] = CL_TypeIsInt(number);
			}
		} else {
			DEL(PVbuffer);
			err_badarg();
			return NULL;
		}
	}

	error_handler_called = 0;
	(*func)(self->ob_compressorHdl, PVbuffer, length);
	if (error_handler_called) {
		DEL(PVbuffer);
		return NULL;
	}

	if (modified) {
		for (i = 0; i < length; i++) {
			if ((i & 1) &&
			    param_type_is_float(self, PVbuffer[i-1]) > 0) {
				number = CL_TypeIsFloat(PVbuffer[i]);
				v = newfloatobject(number);
			} else
				v = newintobject(PVbuffer[i]);
			setlistitem(list, i, v);
		}
	}

	DEL(PVbuffer);

	INCREF(None);
	return None;
}

static object *
clm_GetParams(object *self, object *args)
{
	return doParams(SELF, args, clGetParams, 1);
}

static object *
clm_SetParams(object *self, object *args)
{
	return doParams(SELF, args, clSetParams, 0);
}

static object *
do_get(clobject *self, object *args, int (*func)(CL_Handle, int))
{
	int paramID, value;
	float fvalue;

	CheckCompressor(self);

	if (!getargs(args, "i", &paramID))
		return NULL;

	error_handler_called = 0;
	value = (*func)(self->ob_compressorHdl, paramID);
	if (error_handler_called)
		return NULL;

	if (param_type_is_float(self, paramID) > 0) {
		fvalue = CL_TypeIsFloat(value);
		return newfloatobject(fvalue);
	}

	return newintobject(value);
}

static object *
clm_GetParam(object *self, object *args)
{
	return do_get(SELF, args, clGetParam);
}

static object *
clm_GetDefault(object *self, object *args)
{
	return do_get(SELF, args, clGetDefault);
}

static object *
clm_SetParam(object *self, object *args)
{
	int paramID, value;
	float fvalue;

	CheckCompressor(SELF);

	if (!getargs(args, "(ii)", &paramID, &value)) {
		err_clear();
		if (!getargs(args, "(if)", &paramID, &fvalue)) {
			err_clear();
			err_setstr(TypeError, "bad argument list (format '(ii)' or '(if)')");
			return NULL;
		}
		value = CL_TypeIsInt(fvalue);
	} else {
		if (param_type_is_float(SELF, paramID) > 0) {
			fvalue = value;
			value = CL_TypeIsInt(fvalue);
		}
	}

 	error_handler_called = 0;
	value = clSetParam(SELF->ob_compressorHdl, paramID, value);
	if (error_handler_called)
		return NULL;

	if (param_type_is_float(SELF, paramID) > 0)
		return newfloatobject(CL_TypeIsFloat(value));
	else
		return newintobject(value);
}

static object *
clm_GetParamID(object *self, object *args)
{
	char *name;
	int value;

	CheckCompressor(SELF);

	if (!getargs(args, "s", &name))
		return NULL;

	error_handler_called = 0;
	value = clGetParamID(SELF->ob_compressorHdl, name);
	if (value == FAILURE) {
		if (!error_handler_called)
			err_setstr(ClError, "getparamid failed");
		return NULL;
	}

	return newintobject(value);
}

static object *
clm_QueryParams(object *self, object *args)
{
	int bufferlength;
	int *PVbuffer;
	object *list;
	int i;

	CheckCompressor(SELF);

	if (!getnoarg(args))
		return NULL;

	error_handler_called = 0;
	bufferlength = clQueryParams(SELF->ob_compressorHdl, 0, 0);
	if (error_handler_called)
		return NULL;

	PVbuffer = NEW(int, bufferlength);
	if (PVbuffer == NULL)
		return err_nomem();

	bufferlength = clQueryParams(SELF->ob_compressorHdl, PVbuffer,
				     bufferlength);
	if (error_handler_called) {
		DEL(PVbuffer);
		return NULL;
	}

	list = newlistobject(bufferlength);
	if (list == NULL) {
		DEL(PVbuffer);
		return NULL;
	}

	for (i = 0; i < bufferlength; i++) {
		if (i & 1)
			setlistitem(list, i, newintobject(PVbuffer[i]));
		else if (PVbuffer[i] == 0) {
			INCREF(None);
			setlistitem(list, i, None);
		} else
			setlistitem(list, i, newstringobject((char *) PVbuffer[i]));
	}

	DEL(PVbuffer);

	return list;
}

static object *
clm_GetMinMax(object *self, object *args)
{
	int param, min, max;
	float fmin, fmax;

	CheckCompressor(SELF);

	if (!getargs(args, "i", &param))
		return NULL;

	clGetMinMax(SELF->ob_compressorHdl, param, &min, &max);

	if (param_type_is_float(SELF, param) > 0) {
		fmin = CL_TypeIsFloat(min);
		fmax = CL_TypeIsFloat(max);
		return mkvalue("(ff)", fmin, fmax);
	}

	return mkvalue("(ii)", min, max);
}

static object *
clm_GetName(object *self, object *args)
{
	int param;
	char *name;

	CheckCompressor(SELF);

	if (!getargs(args, "i", &param))
		return NULL;

	error_handler_called = 0;
	name = clGetName(SELF->ob_compressorHdl, param);
	if (name == NULL || error_handler_called) {
		if (!error_handler_called)
			err_setstr(ClError, "getname failed");
		return NULL;
	}

	return newstringobject(name);
}

static object *
clm_QuerySchemeFromHandle(object *self, object *args)
{
	CheckCompressor(SELF);

	if (!getnoarg(args))
		return NULL;

	return newintobject(clQuerySchemeFromHandle(SELF->ob_compressorHdl));
}

static object *
clm_ReadHeader(object *self, object *args)
{
	char *header;
	int headerSize;

	CheckCompressor(SELF);

	if (!getargs(args, "s#", &header, &headerSize))
		return NULL;

	return newintobject(clReadHeader(SELF->ob_compressorHdl,
					 headerSize, header));
}

static struct methodlist compressor_methods[] = {
	{"close",		clm_CloseCompressor}, /* alias */
	{"CloseCompressor",	clm_CloseCompressor},
	{"Compress",		clm_Compress},
	{"GetDefault",		clm_GetDefault},
	{"GetMinMax",		clm_GetMinMax},
	{"GetName",		clm_GetName},
	{"GetParam",		clm_GetParam},
	{"GetParamID",		clm_GetParamID},
	{"GetParams",		clm_GetParams},
	{"QueryParams",		clm_QueryParams},
	{"QuerySchemeFromHandle",clm_QuerySchemeFromHandle},
	{"SetParam",		clm_SetParam},
	{"SetParams",		clm_SetParams},
	{NULL,			NULL}		/* sentinel */
};

static struct methodlist decompressor_methods[] = {
	{"close",		clm_CloseDecompressor},	/* alias */
	{"CloseDecompressor",	clm_CloseDecompressor},
	{"Decompress",		clm_Decompress},
	{"GetDefault",		clm_GetDefault},
	{"GetMinMax",		clm_GetMinMax},
	{"GetName",		clm_GetName},
	{"GetParam",		clm_GetParam},
	{"GetParamID",		clm_GetParamID},
	{"GetParams",		clm_GetParams},
	{"ReadHeader",		clm_ReadHeader},
	{"QueryParams",		clm_QueryParams},
	{"QuerySchemeFromHandle",clm_QuerySchemeFromHandle},
	{"SetParam",		clm_SetParam},
	{"SetParams",		clm_SetParams},
	{NULL,			NULL}		/* sentinel */
};

static void
cl_dealloc(object *self)
{
	if (SELF->ob_compressorHdl) {
		if (SELF->ob_isCompressor)
			clCloseCompressor(SELF->ob_compressorHdl);
		else
			clCloseDecompressor(SELF->ob_compressorHdl);
	}
	DEL(self);
}

static object *
cl_getattr(object *self, char *name)
{
	if (SELF->ob_isCompressor)
		return findmethod(compressor_methods, self, name);
	else
		return findmethod(decompressor_methods, self, name);
}

static typeobject Cltype = {
	OB_HEAD_INIT(&Typetype)
	0,			/*ob_size*/
	"cl",			/*tp_name*/
	sizeof(clobject),	/*tp_size*/
	0,			/*tp_itemsize*/
	/* methods */
	cl_dealloc,		/*tp_dealloc*/
	0,			/*tp_print*/
	cl_getattr,		/*tp_getattr*/
	0,			/*tp_setattr*/
	0,			/*tp_compare*/
	0,			/*tp_repr*/
	0,			/*tp_as_number*/
	0,			/*tp_as_sequence*/
	0,			/*tp_as_mapping*/
};

static object *
doOpen(object *self, object *args, int (*open_func)(int, CL_Handle *),
       int iscompressor)
{
	int scheme;
	clobject *new;

	if (!getargs(args, "i", &scheme))
		return NULL;

	new = NEWOBJ(clobject, &Cltype);
	if (new == NULL)
		return NULL;

	new->ob_compressorHdl = NULL;
	new->ob_isCompressor = iscompressor;
	new->ob_paramtypes = NULL;

	error_handler_called = 0;
	if ((*open_func)(scheme, &new->ob_compressorHdl) == FAILURE) {
		DECREF(new);
		if (!error_handler_called)
			err_setstr(ClError, "Open(De)Compressor failed");
		return NULL;
	}
	return new;
}

static object *
cl_OpenCompressor(object *self, object *args)
{
	return doOpen(self, args, clOpenCompressor, 1);
}

static object *
cl_OpenDecompressor(object *self, object *args)
{
	return doOpen(self, args, clOpenDecompressor, 0);
}

static object *
cl_QueryScheme(object *self, object *args)
{
	char *header;
	int headerlen;
	int scheme;

	if (!getargs(args, "s#", &header, &headerlen))
		return NULL;

	scheme = clQueryScheme(header);
	if (scheme < 0) {
		err_setstr(ClError, "unknown compression scheme");
		return NULL;
	}

	return newintobject(scheme);
}

static object *
cl_QueryMaxHeaderSize(object *self, object *args)
{
	int scheme;

	if (!getargs(args, "i", &scheme))
		return NULL;

	return newintobject(clQueryMaxHeaderSize(scheme));
}

static object *
cl_QueryAlgorithms(object *self, object *args)
{
	int algorithmMediaType;
	int bufferlength;
	int *PVbuffer;
	object *list;
	int i;

	if (!getargs(args, "i", &algorithmMediaType))
		return NULL;

	error_handler_called = 0;
	bufferlength = clQueryAlgorithms(algorithmMediaType, 0, 0);
	if (error_handler_called)
		return NULL;

	PVbuffer = NEW(int, bufferlength);
	if (PVbuffer == NULL)
		return err_nomem();

	bufferlength = clQueryAlgorithms(algorithmMediaType, PVbuffer,
				     bufferlength);
	if (error_handler_called) {
		DEL(PVbuffer);
		return NULL;
	}

	list = newlistobject(bufferlength);
	if (list == NULL) {
		DEL(PVbuffer);
		return NULL;
	}

	for (i = 0; i < bufferlength; i++) {
		if (i & 1)
			setlistitem(list, i, newintobject(PVbuffer[i]));
		else if (PVbuffer[i] == 0) {
			INCREF(None);
			setlistitem(list, i, None);
		} else
			setlistitem(list, i, newstringobject((char *) PVbuffer[i]));
	}

	DEL(PVbuffer);

	return list;
}

static object *
cl_QuerySchemeFromName(object *self, object *args)
{
	int algorithmMediaType;
	char *name;
	int scheme;

	if (!getargs(args, "(is)", &algorithmMediaType, &name))
		return NULL;

	error_handler_called = 0;
	scheme = clQuerySchemeFromName(algorithmMediaType, name);
	if (error_handler_called) {
		err_setstr(ClError, "unknown compression scheme");
		return NULL;
	}

	return newintobject(scheme);
}

static object *
cl_GetAlgorithmName(object *self, object *args)
{
	int scheme;
	char *name;

	if (!getargs(args, "i", &scheme))
		return NULL;

	name = clGetAlgorithmName(scheme);
	if (name == 0) {
		err_setstr(ClError, "unknown compression scheme");
		return NULL;
	}

	return newstringobject(name);
}

static object *
do_set(object *self, object *args, int (*func)(int, int, int))
{
	int scheme, paramID, value;
	float fvalue;
	int is_float = 0;

	if (!getargs(args, "(iii)", &scheme, &paramID, &value)) {
		err_clear();
		if (!getargs(args, "(iif)", &scheme, &paramID, &fvalue)) {
			err_clear();
			err_setstr(TypeError, "bad argument list (format '(iii)' or '(iif)')");
			return NULL;
		}
		value = CL_TypeIsInt(fvalue);
		is_float = 1;
	} else {
		/* check some parameters which we know to be floats */
		switch (scheme) {
		case CL_COMPRESSION_RATIO:
		case CL_SPEED:
			fvalue = value;
			value = CL_TypeIsInt(fvalue);
			is_float = 1;
			break;
		}
	}

 	error_handler_called = 0;
	value = (*func)(scheme, paramID, value);
	if (error_handler_called)
		return NULL;

	if (is_float)
		return newfloatobject(CL_TypeIsFloat(value));
	else
		return newintobject(value);
}

static object *
cl_SetDefault(object *self, object *args)
{
	return do_set(self, args, clSetDefault);
}

static object *
cl_SetMin(object *self, object *args)
{
	return do_set(self, args, clSetMin);
}

static object *
cl_SetMax(object *self, object *args)
{
	return do_set(self, args, clSetMax);
}

#ifdef CLDEBUG
static object *
cvt_type(object *self, object *args)
{
	int number;
	float fnumber;

	if (getargs(args, "i", &number))
		return newfloatobject(CL_TypeIsFloat(number));
	else {
		err_clear();
		if (getargs(args, "f", &fnumber))
			return newintobject(CL_TypeIsInt(fnumber));
		return NULL;
	}
}
#endif

static struct methodlist cl_methods[] = {
	{"CompressImage",	cl_CompressImage},
	{"DecompressImage",	cl_DecompressImage},
	{"GetAlgorithmName",	cl_GetAlgorithmName},
	{"OpenCompressor",	cl_OpenCompressor},
	{"OpenDecompressor",	cl_OpenDecompressor},
	{"QueryAlgorithms",	cl_QueryAlgorithms},
	{"QueryMaxHeaderSize",	cl_QueryMaxHeaderSize},
	{"QueryScheme",		cl_QueryScheme},
	{"QuerySchemeFromName",	cl_QuerySchemeFromName},
	{"SetDefault",		cl_SetDefault},
	{"SetMax",		cl_SetMax},
	{"SetMin",		cl_SetMin},
#ifdef CLDEBUG
	{"cvt_type",		cvt_type},
#endif
	{NULL,			NULL} /* Sentinel */
};

void
initcl()
{
	object *m, *d;

	m = initmodule("cl", cl_methods);
	d = getmoduledict(m);

	ClError = newstringobject("cl.error");
	if (ClError == NULL || dictinsert(d, "error", ClError) != 0)
		fatal("can't define cl.error");

	(void) clSetErrorHandler(cl_ErrorHandler);
}