This site is no longer active and is available for archival purposes only. Registration and login is disabled.

iPhone UIImage to Texture


iPhone UIImage to Texture

Postby lanceabson1 » Jan 5, 2011 @ 10:26am

How do I create an edgelib Texture surface from a UIImage?
lanceabson1
pm Member
 
Posts: 5
Joined: Jan 4, 2011 @ 11:19pm


Re: iPhone UIImage to Texture

Postby edge » Jan 6, 2011 @ 2:39pm

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
EDGELIB: Cross-platform mobile development at your fingertips
http://www.edgelib.com
User avatar
edge
pm Member
 
Posts: 1180
Joined: Aug 22, 2005 @ 3:42pm
Location: The Netherlands


Re: iPhone UIImage to Texture

Postby lanceabson1 » Jan 6, 2011 @ 10:14pm

Do you have some code for this as I am getting an error code 3 from the create surface method?
lanceabson1
pm Member
 
Posts: 5
Joined: Jan 4, 2011 @ 11:19pm


Re: iPhone UIImage to Texture

Postby edge » Jan 7, 2011 @ 5:06pm

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
EDGELIB: Cross-platform mobile development at your fingertips
http://www.edgelib.com
User avatar
edge
pm Member
 
Posts: 1180
Joined: Aug 22, 2005 @ 3:42pm
Location: The Netherlands


Re: iPhone UIImage to Texture

Postby lanceabson1 » Jan 7, 2011 @ 5:55pm

Hi, I didn't get your email so I have uploaded the exported .png here.
Look forward to hearing from you.
Attachments

[The extension png has been deactivated and can no longer be displayed.]

lanceabson1
pm Member
 
Posts: 5
Joined: Jan 4, 2011 @ 11:19pm


Re: iPhone UIImage to Texture

Postby edge » Jan 10, 2011 @ 1:01pm

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
EDGELIB: Cross-platform mobile development at your fingertips
http://www.edgelib.com
User avatar
edge
pm Member
 
Posts: 1180
Joined: Aug 22, 2005 @ 3:42pm
Location: The Netherlands


Re: iPhone UIImage to Texture

Postby Quicksilver01uk » Jan 10, 2011 @ 3:26pm

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.
Quicksilver01uk
pm Member
 
Posts: 2
Joined: Jan 5, 2011 @ 12:21am


Re: iPhone UIImage to Texture

Postby edge » Jan 10, 2011 @ 5:00pm

Hi Matthew,

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

Wouter
EDGELIB: Cross-platform mobile development at your fingertips
http://www.edgelib.com
User avatar
edge
pm Member
 
Posts: 1180
Joined: Aug 22, 2005 @ 3:42pm
Location: The Netherlands


Re: iPhone UIImage to Texture

Postby edge » Jan 10, 2011 @ 5:02pm

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

Wouter
EDGELIB: Cross-platform mobile development at your fingertips
http://www.edgelib.com
User avatar
edge
pm Member
 
Posts: 1180
Joined: Aug 22, 2005 @ 3:42pm
Location: The Netherlands


Re: iPhone UIImage to Texture

Postby lanceabson1 » Jan 10, 2011 @ 9:35pm

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 );
lanceabson1
pm Member
 
Posts: 5
Joined: Jan 4, 2011 @ 11:19pm


Re: iPhone UIImage to Texture

Postby edge » Jan 12, 2011 @ 1:15pm

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
EDGELIB: Cross-platform mobile development at your fingertips
http://www.edgelib.com
User avatar
edge
pm Member
 
Posts: 1180
Joined: Aug 22, 2005 @ 3:42pm
Location: The Netherlands


Re: iPhone UIImage to Texture

Postby lanceabson1 » Jan 12, 2011 @ 1:50pm

Hi Marcel,

Do you have some example code that does that?

Thanks
lanceabson1
pm Member
 
Posts: 5
Joined: Jan 4, 2011 @ 11:19pm


Re: iPhone UIImage to Texture

Postby edge » Jan 12, 2011 @ 2:26pm

EDGELIB: Cross-platform mobile development at your fingertips
http://www.edgelib.com
User avatar
edge
pm Member
 
Posts: 1180
Joined: Aug 22, 2005 @ 3:42pm
Location: The Netherlands


Return to EDGELIB


Sort


Forum Description

Powerful and affordable C++ middleware solution covering true multi-platform 2D, 3D and network features for Apple iPhone, Windows Mobile, Symbian S60, UIQ, Linux and Windows desktop.

Moderator:

edge

Forum permissions

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum