Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

grab an image from OpenGL ES context

specktrospecktro Posts: 4New Users
I've been working with OpenGL ES to modify an application which allows to capture a user's signature. Now i can draw freehand on the screen but now i need to keep that signature into an image. I found this code which just draws a completely black image, i'm new to OpenGL i hope that somebody can help me...
NSInteger myDataLength = 320 * 480 * 4; 
// allocate array and read pixels into it. 
GLubyte *buffer = (GLubyte *) malloc(myDataLength); 
glReadPixels(0, 0, 320, 480, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
	
// gl renders "upside down" so swap top to bottom into new array. 
// there's gotta be a better way, but this works. 
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);

for(int y = 0; y < 480; y++) 
{ 
	for(int x = 0; x  <320 * 4; x++) { 
		buffer2[(479 - y) * 320 * 4 + x] = buffer[y * 4 * 320 + x];
	} 
} 
// make data provider with data. 
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL); 
// prep the ingredients 
int bitsPerComponent = 8; 
int bitsPerPixel = 32; 
int bytesPerRow = 4 * 320; 
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); 
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; 
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; 
// make the cgimage 
CGImageRef imageRef = CGImageCreate(320, 480, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent); 
// then make the uiimage from that 
UIImage *myImage = [UIImage imageWithCGImage:imageRef]; 
UIImageWriteToSavedPhotosAlbum(myImage, self, nil, nil);

:confused:
Post edited by specktro on
· ·

Replies

  • Mr JackMr Jack Posts: 395Registered Users
    edited December 2009
    I put pretty much that exact code into my game today and it worked perfectly.

    When are you calling it?
    <a href="http://itunes.apple.com/gb/app/alien-swing/id352732312?mt=8" target="_blank">sig_ad.jpg</a><br />
    <br />
    Visit <a href="http://mrjackgames.co.uk/" target="_blank">Mr Jack Games</a> for my blog and more
    · ·
  • specktrospecktro Posts: 4New Users
    edited December 2009
    Mr Jack wrote: »
    I put pretty much that exact code into my game today and it worked perfectly.

    When are you calling it?

    I have a IBAction from a UIButton... when a press it, i call this method

    :confused:
    · ·
  • specktrospecktro Posts: 4New Users
    edited December 2009
    After a new search in this forum i found a possible solution, the inconvenient of this is when i see the preview of my image is completely black... Just replace the line:
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    

    for this:
    CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast;
    

    before performing this you need to set the opaque attribute of the layer to NO...

    ;)
    · ·
  • Mr JackMr Jack Posts: 395Registered Users
    edited December 2009
    Hmm... I was doing it on a touchbegin event, but it should work much the same.

    Do you have imagery on screen that is not going through OpenGLES?

    Do you get any warnings during compilation?
    <a href="http://itunes.apple.com/gb/app/alien-swing/id352732312?mt=8" target="_blank">sig_ad.jpg</a><br />
    <br />
    Visit <a href="http://mrjackgames.co.uk/" target="_blank">Mr Jack Games</a> for my blog and more
    · ·
  • specktrospecktro Posts: 4New Users
    edited December 2009
    no! I don't have any warning when i compile my project... why...?
    · ·
  • Mr JackMr Jack Posts: 395Registered Users
    edited December 2009
    specktro wrote: »
    no! I don't have any warning when i compile my project... why...?

    Long shot: I wondered whether you could be missing a framework needed.
    <a href="http://itunes.apple.com/gb/app/alien-swing/id352732312?mt=8" target="_blank">sig_ad.jpg</a><br />
    <br />
    Visit <a href="http://mrjackgames.co.uk/" target="_blank">Mr Jack Games</a> for my blog and more
    · ·
  • plug-inplug-in Posts: 4New Users
    edited May 2010
    This solution worked pretty good for me.
    The only thing to do at once is to free memory.

    These lines are pretty straight forward:
    free(buffer);
    
    CGImageRelease(imageRef);
    CGColorSpaceRelease(colorSpaceRef);
    CGDataProviderRelease(provider);
    

    But CGDataProviderRef need to be created another way to invoke release callback function and free buffer used as data provider:
    CGDataProviderRef provider = CGDataProviderCreateWithData(buffer2, buffer2, myDataLength, ProviderReleaseData);
    

    Last parameter is a callback function which is invoked when resources are no longer needed. Something like this:
    void ProviderReleaseData ( void *info, const void *data, size_t size ) {
     free(info);
    }
    
    · ·
  • HackHack San FranciscoPosts: 2,127Registered Users @ @ @ @
    edited June 2010
    I am having a similar problem but it is stranger.
    I am doing some HUE filtering using OpenGLES and when I save to an UIImage it is successful and looks good in the preview thumbnail in the photos application BUT when you open the image it is purely white. I am confused by this.
    Has anyone seen this before?

    Thanks!
    · ·
  • plug-inplug-in Posts: 4New Users
    edited July 2010
    Please check what is the saved image format ?
    I have similar issue when I saved .png images using
    UIImageWriteToSavedPhotosAlbum()
    This function saves them in .jpg format and cuts their transparency.
    You can save these images in your app's bundle Documents directory, this will keep images in correct format.
    · ·
Sign In or Register to comment.