|
From: Ralph B. <ra...@de...> - 2005-07-14 20:08:03
|
Hey everyone, I'm having a bit of trouble using NSBitmapImageRep#bitmapData. Or at least, I think I am. (Source bits are included below, there is a testcase up at http://deadfeed.net/sandbox/testcase.zip - beware, the Makefile dependencies are not 100%.) The NSBitmapImageRep should display data in a window, and that data should be coming from a C++ extension, via SWIG. Ruby should create the window, create views, and call a C++ function to fill the bitmap. But I all I get is a bus error (presumably because the pointer handed to the C++ function is off). I have a class, FracWindow, which initializes the window and views (@fracView). FracWindow#ShowImage passes a pointer (char *) to the C++ function, which then fills it. def showImage() bitmapRep=OSX::NSBitmapImageRep.alloc.initWithBitmapDataPlanes(nil, :pixelsWide, 256, :pixelsHigh, 256, :bitsPerSample, 8, :samplesPerPixel, 4, :hasAlpha, true, :isPlanar, true, :colorSpaceName, "NSDeviceRGBColorSpace", :bytesPerRow, 0, :bitsPerPixel, 0 ) Testinterface::Test(bitmapRep.bitmapData()) image=OSX::NSImage.alloc.initWithSize([256, 256]) image.addRepresentation(bitmapRep) @fracView.setImage(image) @fracView.setNeedsDisplay(true) end Testinterface::Test looks like this: void Test(BitmapPointer p) { std::cout << &p << std::endl; unsigned char *pp=p; pp[0]=255; pp[1]=0; pp[2]=0; pp[3]=0; pp[4]=0; pp[5]=0; pp[6]=0; pp[7]=0; /* for (int y=0; y<256; y++) { for (int x=0; x<256; x++) { char f=y/2+x/2; *pp++=f; // *pp++=f; // *pp++=f; // *pp++=f; } }*/ } I made a BitmapPointer typedef in order to let SWIG distinguish the data type (char *). The SWIG typemap looks like this: %typemap(in) BitmapPointer { if (TYPE($input)==T_STRING) { $1 = StringValuePtr($input); } else { rb_raise(rb_eTypeError, "Expected bitmapdata"); $1 = 0; } } The main event here is of course "$1 = StringValuePtr($input);", which should, as I understand it return a char pointer to the string data which bitmapRep.bitmapData() returned. The reason it is converted to a Ruby string, I assume, is because RubyCocoa sees the char *, and thinks it is a string. Perhaps the string is copied to an internal buffer? I'd appreciate it if anyone could point me in the right direction. Regards, Ralph Brorsen |