Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-08-2009, 01:15 PM   #1 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 4
Question grab an image from OpenGL ES context

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...

Code:
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);
specktro is offline   Reply With Quote
Old 12-08-2009, 02:54 PM   #2 (permalink)
Maker of Games
 
Mr Jack's Avatar
 
Join Date: Nov 2009
Location: Coventry, UK
Posts: 395
Default

I put pretty much that exact code into my game today and it worked perfectly.

When are you calling it?
__________________


Visit Mr Jack Games for my blog and more about my games
Mr Jack is offline   Reply With Quote
Old 12-08-2009, 04:20 PM   #3 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 4
Default

Quote:
Originally Posted by Mr Jack View Post
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

specktro is offline   Reply With Quote
Old 12-08-2009, 06:11 PM   #4 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 4
Default possible solution

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:

Code:
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
for this:

Code:
CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast;
before performing this you need to set the opaque attribute of the layer to NO...

specktro is offline   Reply With Quote
Old 12-08-2009, 06:11 PM   #5 (permalink)
Maker of Games
 
Mr Jack's Avatar
 
Join Date: Nov 2009
Location: Coventry, UK
Posts: 395
Default

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?
__________________


Visit Mr Jack Games for my blog and more about my games
Mr Jack is offline   Reply With Quote
Old 12-08-2009, 06:43 PM   #6 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 4
Default

no! I don't have any warning when i compile my project... why...?
specktro is offline   Reply With Quote
Old 12-09-2009, 04:16 AM   #7 (permalink)
Maker of Games
 
Mr Jack's Avatar
 
Join Date: Nov 2009
Location: Coventry, UK
Posts: 395
Default

Quote:
Originally Posted by specktro View Post
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.
__________________


Visit Mr Jack Games for my blog and more about my games
Mr Jack is offline   Reply With Quote
Old 05-27-2010, 07:23 AM   #8 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 4
Default memory leaks

This solution worked pretty good for me.
The only thing to do at once is to free memory.

These lines are pretty straight forward:

Code:
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:

Code:
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:

Code:
void ProviderReleaseData ( void *info, const void *data, size_t size ) {
 free(info);
}

Last edited by plug-in; 05-27-2010 at 07:30 AM.
plug-in is offline   Reply With Quote
Old 06-20-2010, 06:41 AM   #9 (permalink)
Beast Mode
 
Join Date: Dec 2008
Age: 21
Posts: 1,890
Default

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!
__________________
I really do this.
Bertrand21 is offline   Reply With Quote
Old 07-05-2010, 08:15 AM   #10 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 4
Default

Please check what is the saved image format ?
I have similar issue when I saved .png images using
Quote:
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.
plug-in is offline   Reply With Quote
Reply

Bookmarks

Tags
opengl es

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 317
19 members and 298 guests
@sandris, ADY, dacapo, Dani77, djohnson, dre, HDshot, HemiMG, JasonR, MarkC, mer10, nibeck, prchn4christ, ryandb2, spiderguy84, timle8n1, tomtom100, vogueestylee
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,229
Posts: 380,763
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:05 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0