[Pyobjc-dev] strange behavior?
Brought to you by:
ronaldoussoren
|
From: Duane R. <bai...@gm...> - 2007-12-28 21:28:11
|
I wrote a quick wrapper to get an TIFFRepresentation from a 1d list, a
width, and a height. Sadly, I ran into two problems:
1. it is returned in a tuple.
2. when I extract it from the tuple, I can't call methods (I tried length)
because of the missing variable 'self'.
here's the code:
#import <Python.h>
#import "pyobjc-api.h"
#import <Cocoa/Cocoa.h>
// example usage
// getbitmap([1 2 3 4], 2, 2, 1, colorspace) # creates a grascale, 2x2
NSBitMapImageRep
static PyObject *get_bitmap(PyObject *self, PyObject *args) // def
getbitmap(data, width, height, ipp, colorspace):
{
unsigned char *data;
PyObject *dataList, *newwidth, *newheight, *newipp, *intObject,
*colorspace;
long listSize = -1, i = 0, width = 0, height = 0, ipp = 3;
BOOL alpha;
NSBitmapImageRep *rep;
NSData *tiff;
NSString *colorString;
if (!PyArg_ParseTuple(args, "O!ii", &PyList_Type, &dataList, &width,
&height)) return NULL;
listSize = PyList_Size(dataList);
if(width * height * ipp != listSize)
{
PyErr_Format(PyExc_ValueError, "The list size does not agree with the
other arguments!");
PyErr_Print();
return Py_None;
}
if (listSize < 0)
{
PyErr_Format(PyExc_TypeError, "First argument must be a list with
entries!");
PyErr_Print();
return Py_None;
}
data = malloc(sizeof(unsigned char) * listSize);
for (i = 0; i < listSize; i++)
{
intObject = PyList_GetItem(dataList, i);
if(!PyInt_Check(intObject))
{
PyErr_Format(PyExc_TypeError, "Entry %d of list is not an integer!",
i);
PyErr_Print();
return Py_None;
}
data[i] = (unsigned char)PyInt_AsUnsignedLongMask(intObject);
}
// So, now we have our DATA! :D
alpha = (ipp == 4) ? YES : NO;
rep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: &data
pixelsWide: width
pixelsHigh: height
bitsPerSample: 8
samplesPerPixel:ipp
hasAlpha: alpha
isPlanar: NO
colorSpaceName: NSDeviceRGBColorSpace
bytesPerRow: width * ipp
bitsPerPixel: ipp * 8];
tiff = [rep TIFFRepresentation];
return PyObjC_ObjCToPython(@encode(NSData), tiff);
}
static PyMethodDef methods[] = {
{"getbitmap", get_bitmap, METH_VARARGS,
"Get an NSBitmapImageRep"},
{NULL, NULL, 0, NULL}
};
PyObject *mod;
PyMODINIT_FUNC initbitmap()
{
mod = Py_InitModule("bitmap", methods);
PyObjC_ImportAPI(mod);
}
|