PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Problems using GetSysApiSurface

I'm trying to copy data from a CFbsBitmap to an E2DSurface. My program crashes when I try to access the data - I just tried setting all pixels to 0.

The texture was loaded from an image file using Display->CreateSurface().

Code:

CFbsBitmap* texturePtr = texture.GetSysApiSurface();
pixelPos = texturePtr->DataAddress();

for (int i=0;i<64;i++)
{
for (int j=0;j<64;j++)
{
*pixelPos = 0;
pixelPos++;
}
}



Am I doing something wrong here ?
Hi Countzero,

We recommend using E2DSurface :: Lock to lock the surface and receive the data address. This is because it uses a TBitmapUtil class to lock the surface memory, which is needed. Otherwise the surface memory may be relocated while accessing its data when it's not locked.

Can you tell me why you like to access the CFbsBitmap object directly, instead of using the E2DSurface API's?
I'm trying to use the camera viewfinder display as a texture on my polygons. The camera api returns the viewfinder data as CFbsBitmap*. So I was wondering if I could just assign the underlying bitmap for an E2DSurface to the pointer that I receive from the camera api.

Is there a faster way to do this?
Hi Countzero,

It may be possible to make it faster, but it needs to be possible to supply a pointer to the camera API so it can fill its data (so it will write to the E2DSurface object simultaniously). If it's possible, you may implement it like this (make sure the size of the surface is exactly the same size of the pictures returned by the camera API):

Code:

E2DSurface video;
display->CreateSurface(&video;, video_framewidth, video_frameheight);
camera_api->SetBitmap(video.GetSysApiSurface());


If the above is not possible, then you may need to create a TBitmapUtil object and link it to the bitmap returned by the camera API. Then lock it and retrieve the data address. After that, lock the E2DSurface object and copy data.
Thanks! TBitmapUtil did the trick and I'm getting 20fps on my N95 for a texture mapped cube (camera view finder as texture).

Now only if I could get the camera to write directly to my texture, it'd speed up things significantly.
Please let us know your findings, we're interested in adding camera access to the SDK as well.
I'll post my results soon - I realized that I was averaging 20fps at 240x180 viewfinder resolution using the Edge renderer. Maybe OpenGL will speed things up.
countzero, any updates on using the camera viewfinder as a texture? I'm working on exactly the same thing right now and any support/ideas would certainly be much appreciated!

edge, any updates on adding camera access to the SDK? If not at this point in time - have you heard anything back from countzero regarding his progress or his implementation?

Thanks guys!
Sorry about that. I've been busy with a few other projects. Yes, I got the viewfinder bitmap as a texture and it works very well.

Code:

void CameraN95::ViewFinderFrameReady(CFbsBitmap &image)
{
if (copying) return;

copying = true;
image.LockHeap();
Mem::Copy(tmpBuffer, image.DataAddress(),width*height*4);
image.UnlockHeap();
unsigned int temp;
unsigned char *iter = tmpBuffer;
for (int i=0;i<width*height;i++)
{
// BGRx to RGB(A=255)
temp = *iter;
*iter = *(iter+2);
*(iter+2) = temp;
*(iter+3) = 255;
iter = iter+4;
}

for (int i=0;i<height;i++)
{
Mem::Copy(pInternalImgData+4*VideoTexture::texWidth*i,tmpBuffer+4*width*i,width*4);
}

copying = false;

// Profiling: 256x192: 7ms
// Profiling: 168x126: 3ms

newFrameReady = true;
}


I'm not certain if the mutex variable (copying) is needed. I just haven't clean up that portion. The width,height variables correspond to the dimensions of the viewfinder. The texWidth, texHeight are the least powers of 2 that are >= width, height.

pInternalImgData is a unsigned char* buffer that stores the bitmap. Its size is width*height*4 (due to RGBA). You need to copy this to an OpenGL texture using something like this:

Code:

glBindTexture(GL_TEXTURE_2D, texId);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, pImgData);


Hope this helps. The profiling times indicate that the capture can be fairly quick. You should be able to do about as well as the default camera application.