PocketMatrix forums

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

iPhone UIImage to Texture

lanceabson1 · 05 Jan 2011, 09:26 UTC
How do I create an edgelib Texture surface from a UIImage?
Hi lanceabson1,

You can convert the UIImage to PNG data, which EDGELIB can read by using ClassEDisplay::CreateSurface. To convert the UIImage to this data, use the following call: UIImagePNGRepresentation(pointer_to_ui_image). This will return an NSData object which you can get the bytes and size (byte amount) from to pass through EDGELIB.

Johan
lanceabson1 · 06 Jan 2011, 21:14 UTC
Do you have some code for this as I am getting an error code 3 from the create surface method?
Unfortunately there is no quick answer. It seems lik the images cannot be loaded by EDGELIB (error code 3 = unsupported). Maybe you are trying to read a PNG format which cannot be loaded by EDGELIB.

In order to help us narrow down the problem, could you write the contents of your UIImage to a .png file (by using the UIImagePNGRepresentation function and then exporting NSData) and send it to us? It would help us analyze the problem more quickly.

This reply has been emailed to you too, let's continue the discussion there.

Johan
lanceabson1 · 07 Jan 2011, 16:55 UTC
Hi, I didn't get your email so I have uploaded the exported .png here.
Look forward to hearing from you.
Attachments:
exportpng.png
We have tried to load the PNG file (display->CreateSurface(&surface, "exportpng.png");), and it seems we can correctly load and draw the image (the result is error code 0). We conclude your issue is caused by something outside EDGELIB.

(Edit:) For example a memory issue, data might be released already, data sent to EDGELIB might be corrupt, doing something in the wrong order, etc.

Could you please look into this?

Wouter
Quicksilver01uk · 10 Jan 2011, 14:26 UTC
Hi Wouter,

Is it possible to send us the code you used for your testing to get the result? That way we know we are using the correct code? You can email it to me if you don't want to post here.

Regards,
Matthew.
Hi Matthew,

We just used:
display->CreateSurface(&surface, "exportpng.png");

Wouter
Perhaps you could post (or email) us a a small example wich reproduces the problem. Would this be possible?

Wouter
lanceabson1 · 10 Jan 2011, 20:35 UTC
We also are ok if we make a texture surface from a file.
But we need to make a texture surface from a UIImage which is created from the raw data feed of the iphone camera.



// Create a UIImage from sample buffer data from the camera
- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
{
// Get a CMSampleBuffer's Core Video image buffer for the media data
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
// Lock the base address of the pixel buffer
CVPixelBufferLockBaseAddress(imageBuffer, 0);

// Get the number of bytes per row for the pixel buffer
void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);

// Get the number of bytes per row for the pixel buffer
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
// Get the pixel buffer width and height
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);

// Create a device-dependent RGB color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

// Create a bitmap graphics context with the sample buffer data
CGContextRef context = CGBitmapContextCreate(baseAddress, 256, 256, 8,
bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
// Create a Quartz image from the pixel data in the bitmap graphics context
CGImageRef quartzImage = CGBitmapContextCreateImage(context);
// Unlock the pixel buffer
CVPixelBufferUnlockBaseAddress(imageBuffer,0);

// Free up the context and color space
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);

// Create an image object from the Quartz image
UIImage *image = [UIImage imageWithCGImage:quartzImage];

// Release the Quartz image
CGImageRelease(quartzImage);
pngImageForTexture = UIImagePNGRepresentation(image);
}

// Try to create the Edgelib texture from the UIImage .png above

int camBufSize = [appDel.rootViewController.arViewController.pngImageForTexture length];
unsigned long lcamBufSize = camBufSize;
const void* texBytes = [appDel.rootViewController.arViewController.pngImageForTexture bytes];
ERESULT res;
res = display->CreateSurface(&camTex, &texBytes, lcamBufSize);
EST_READONLYPATH);

if (res != E_OK) NSLog(@"ERR: CreateSurface %d %d",camBufSize, res);
else NSLog(@"OK: CreateSurface %d", camBufSize);

display->UploadTexture( &camTex );
Your problem lies within the CreateSurface call: The prototype of the function taking a memory argument has the following form: CreateSurface(E2DSurface *, void *, long, long). The call used is of the type CreateSurface(E2DSurface *, const void **, long). There are two problems with that: the first one is that you supply a pointer to a pointer to a buffer, which makes the function access the wrong data. Secondly, you might want to make sure to add the last parameter so that the memory-version is used, and not the version used to load files.

That said, you can skip several steps and gain some performance by not converting the image data to an UIImage and then to PNG data, then back to image data, but instead create an empty surface and call Lock to access the buffer directly. The downside is that you need to manually perform color conversion and resizing using this method.

Marcel
lanceabson1 · 12 Jan 2011, 12:50 UTC
Hi Marcel,

Do you have some example code that does that?

Thanks
lanceabson1 wrote:Hi Marcel,
Do you have some example code that does that?
Thanks


Unfortunately we have no ready-to-use sample code to send you.

FWIW, soon to be released EDGELIB 4.00 contains a native camera class and data-to-texture functionality which would resolve all of this internally.

Wouter