Page 1 of 1

iPhone UIImage to Texture

PostPosted: Jan 5, 2011 @ 10:26am
by lanceabson1
How do I create an edgelib Texture surface from a UIImage?

Re: iPhone UIImage to Texture

PostPosted: Jan 6, 2011 @ 2:39pm
by edge
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

Re: iPhone UIImage to Texture

PostPosted: Jan 6, 2011 @ 10:14pm
by lanceabson1
Do you have some code for this as I am getting an error code 3 from the create surface method?

Re: iPhone UIImage to Texture

PostPosted: Jan 7, 2011 @ 5:06pm
by edge
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

Re: iPhone UIImage to Texture

PostPosted: Jan 7, 2011 @ 5:55pm
by lanceabson1
Hi, I didn't get your email so I have uploaded the exported .png here.
Look forward to hearing from you.

Re: iPhone UIImage to Texture

PostPosted: Jan 10, 2011 @ 1:01pm
by edge
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

Re: iPhone UIImage to Texture

PostPosted: Jan 10, 2011 @ 3:26pm
by Quicksilver01uk
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.

Re: iPhone UIImage to Texture

PostPosted: Jan 10, 2011 @ 5:00pm
by edge
Hi Matthew,

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

Wouter

Re: iPhone UIImage to Texture

PostPosted: Jan 10, 2011 @ 5:02pm
by edge
Perhaps you could post (or email) us a a small example wich reproduces the problem. Would this be possible?

Wouter

Re: iPhone UIImage to Texture

PostPosted: Jan 10, 2011 @ 9:35pm
by lanceabson1
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 );

Re: iPhone UIImage to Texture

PostPosted: Jan 12, 2011 @ 1:15pm
by edge
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

Re: iPhone UIImage to Texture

PostPosted: Jan 12, 2011 @ 1:50pm
by lanceabson1
Hi Marcel,

Do you have some example code that does that?

Thanks

Re: iPhone UIImage to Texture

PostPosted: Jan 12, 2011 @ 2:26pm
by edge