clmodule.c

来自「python s60 1.4.5版本的源代码」· C语言 代码 · 共 2,418 行 · 第 1/5 页

C
2,418
字号
	int *PVbuffer;
	PyObject *list;
	int i;

	CheckCompressor(SELF);

	if (!PyArg_NoArgs(args))
		return NULL;

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

	PVbuffer = PyMem_NEW(int, bufferlength);
	if (PVbuffer == NULL)
		return PyErr_NoMemory();

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

	list = PyList_New(bufferlength);
	if (list == NULL) {
		PyMem_DEL(PVbuffer);
		return NULL;
	}

	for (i = 0; i < bufferlength; i++) {
		if (i & 1)
			PyList_SetItem(list, i, PyInt_FromLong(PVbuffer[i]));
		else if (PVbuffer[i] == 0) {
			Py_INCREF(Py_None);
			PyList_SetItem(list, i, Py_None);
		} else
			PyList_SetItem(list, i,
				   PyString_FromString((char *) PVbuffer[i]));
	}

	PyMem_DEL(PVbuffer);

	return list;
}

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

	CheckCompressor(SELF);

	if (!PyArg_Parse(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 Py_BuildValue("(ff)", fmin, fmax);
	}

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

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

	CheckCompressor(SELF);

	if (!PyArg_Parse(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)
			PyErr_SetString(ClError, "getname failed");
		return NULL;
	}

	return PyString_FromString(name);
}

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

	if (!PyArg_NoArgs(args))
		return NULL;

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

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

	CheckCompressor(SELF);

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

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

static PyMethodDef 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 PyMethodDef 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(PyObject *self)
{
	if (SELF->ob_compressorHdl) {
		if (SELF->ob_isCompressor)
			clCloseCompressor(SELF->ob_compressorHdl);
		else
			clCloseDecompressor(SELF->ob_compressorHdl);
	}
	PyObject_Del(self);
}

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

static PyTypeObject Cltype = {
	PyObject_HEAD_INIT(&PyType_Type)
	0,			/*ob_size*/
	"cl.cl",		/*tp_name*/
	sizeof(clobject),	/*tp_size*/
	0,			/*tp_itemsize*/
	/* methods */
	(destructor)cl_dealloc,	/*tp_dealloc*/
	0,			/*tp_print*/
	(getattrfunc)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 PyObject *
doOpen(PyObject *self, PyObject *args, int (*open_func)(int, CL_Handle *),
       int iscompressor)
{
	int scheme;
	clobject *new;

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

	new = PyObject_New(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 ||
	    error_handler_called) {
		Py_DECREF(new);
		if (!error_handler_called)
			PyErr_SetString(ClError, "Open(De)Compressor failed");
		return NULL;
	}
	return (PyObject *)new;
}

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

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

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

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

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

	return PyInt_FromLong(scheme);
}

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

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

	return PyInt_FromLong(clQueryMaxHeaderSize(scheme));
}

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

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

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

	PVbuffer = PyMem_NEW(int, bufferlength);
	if (PVbuffer == NULL)
		return PyErr_NoMemory();

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

	list = PyList_New(bufferlength);
	if (list == NULL) {
		PyMem_DEL(PVbuffer);
		return NULL;
	}

	for (i = 0; i < bufferlength; i++) {
		if (i & 1)
			PyList_SetItem(list, i, PyInt_FromLong(PVbuffer[i]));
		else if (PVbuffer[i] == 0) {
			Py_INCREF(Py_None);
			PyList_SetItem(list, i, Py_None);
		} else
			PyList_SetItem(list, i,
				   PyString_FromString((char *) PVbuffer[i]));
	}

	PyMem_DEL(PVbuffer);

	return list;
}

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

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

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

	return PyInt_FromLong(scheme);
}

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

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

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

	return PyString_FromString(name);
}

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

	if (!PyArg_Parse(args, "(iii)", &scheme, &paramID, &value)) {
		PyErr_Clear();
		if (!PyArg_Parse(args, "(iif)", &scheme, &paramID, &fvalue)) {
			PyErr_Clear();
			PyErr_SetString(PyExc_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 PyFloat_FromDouble(CL_TypeIsFloat(value));
	else
		return PyInt_FromLong(value);
}

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

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

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

#define func(name, handler)	\
static PyObject *cl_##name(PyObject *self, PyObject *args) \
{ \
	  int x; \
	  if (!PyArg_Parse(args, "i", &x)) return NULL; \
	  return Py##handler(CL_##name(x)); \
}

#define func2(name, handler)	\
static PyObject *cl_##name(PyObject *self, PyObject *args) \
{ \
	  int a1, a2; \
	  if (!PyArg_Parse(args, "(ii)", &a1, &a2)) return NULL; \
	  return Py##handler(CL_##name(a1, a2)); \
}

func(BytesPerSample, Int_FromLong)
func(BytesPerPixel, Int_FromLong)
func(AudioFormatName, String_FromString)
func(VideoFormatName, String_FromString)
func(AlgorithmNumber, Int_FromLong)
func(AlgorithmType, Int_FromLong)
func2(Algorithm, Int_FromLong)
func(ParamNumber, Int_FromLong)
func(ParamType, Int_FromLong)
func2(ParamID, Int_FromLong)

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

	if (PyArg_Parse(args, "i", &number))
		return PyFloat_FromDouble(CL_TypeIsFloat(number));
	else {
		PyErr_Clear();
		if (PyArg_Parse(args, "f", &fnumber))
			return PyInt_FromLong(CL_TypeIsInt(fnumber));
		return NULL;
	}
}
#endif

static PyMethodDef 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},
	{"BytesPerSample",	cl_BytesPerSample},
	{"BytesPerPixel",	cl_BytesPerPixel},
	{"AudioFormatName",	cl_AudioFormatName},
	{"VideoFormatName",	cl_VideoFormatName},
	{"AlgorithmNumber",	cl_AlgorithmNumber},
	{"AlgorithmType",	cl_AlgorithmType},
	{"Algorithm",		cl_Algorithm},
	{"ParamNumber",		cl_ParamNumber},
	{"ParamType",		cl_ParamType},
	{"ParamID",		cl_ParamID},
#ifdef CLDEBUG
	{"cvt_type",		cvt_type},
#endif
	{NULL,			NULL} /* Sentinel */
};

#ifdef CL_JPEG_SOFTWARE
#define IRIX_5_3_LIBRARY

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?