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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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 01-02-2012, 03:12 PM   #1 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 112
RoryHarvey is on a distinguished road
Default How do I blit pixel data to the screen using OpenGL ES?

I'm currently learning OpenGL ES for iPhone and I'm wondering how to take an array of RGB pixel data and blit it to the screen, so far all the tutorials I've found cover loading a pre-existing image into a texture and displaying it.

Here's how I've accomplished it using Core Graphics by creating an imageref then using a UIImage and UIImageView:

Code:
int screenWidth = (int)[[UIScreen mainScreen] bounds].size.width;
int screenHeight = (int)[[UIScreen mainScreen] bounds].size.height;

//Create blank image
CGImageRef blankImageRef = [self createBlankImageRef];
CFDataRef m_DataRef; 
m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(blankImageRef)); 
UInt8* bufferData = CFDataGetBytePtr(m_DataRef); 


//Manipulate the image here
for(int x=0; x < screenWidth-1; x++)
{
    int y = x;//*2;

    [self setPixel:bufferData width:screenWidth x:x y:y r:255 g:0 b:0];
}


//Paint the image to the screen
CGImageRef imageRef = blankImageRef; 
CGContextRef ctx; 
ctx = CGBitmapContextCreate(bufferData, 
                            CGImageGetWidth( imageRef ), 
                            CGImageGetHeight( imageRef ), 
                            8, 
                            CGImageGetBytesPerRow( imageRef ), 
                            CGImageGetColorSpace( imageRef ), 
                            kCGImageAlphaPremultipliedLast ); 
screenImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage (ctx)]; 
CGContextRelease(ctx); 

[imageView setImage:screenImage];

free(bufferData);
...

Code:
- (CGImageRef) createBlankImageRef
{
CGFloat imageScale = (CGFloat)1.0;
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
CGFloat height = [[UIScreen mainScreen] bounds].size.height;

// Create a bitmap graphics context of the given size
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, width * imageScale, height * imageScale, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);

// Draw ...
CGContextSetRGBFillColor(context, (CGFloat)0.0, (CGFloat)0.0, (CGFloat)0.0, (CGFloat)1.0 );

// Get your image
CGImageRef cgImage = CGBitmapContextCreateImage(context);

CGColorSpaceRelease(colorSpace);
CGContextRelease(context);

return cgImage;
}

-(void)setPixel:(UInt8*)buffer width:(int)width x:(int)x y:(int)y r:(int)r g:(int)g b:(int)b
{
buffer[x*4 + y*(width*4)] = r;
buffer[x*4 + y*(width*4)+1] = g;
buffer[x*4 + y*(width*4)+2] = b;
buffer[x*4 + y*(width*4)+3] = 255;
}
Any ideas how I could accomplish this using OpenGL ES (currently using 1.0) instead of Core Graphics?

Last edited by RoryHarvey; 01-04-2012 at 09:36 AM.
RoryHarvey is offline   Reply With Quote
Old 01-02-2012, 06:13 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by RoryHarvey View Post
0 down vote favorite
share [fb] share [tw]


I'm currently learning OpenGL ES for iPhone and I'm wondering how to take an array of RGB pixel data and blit it to the screen, so far all the tutorials I've found cover loading a pre-existing image into a texture and displaying it.

Here's how I've accomplished it using Core Graphics by creating an imageref then using a UIImage and UIImageView:

Code:
int screenWidth = (int)[[UIScreen mainScreen] bounds].size.width;
int screenHeight = (int)[[UIScreen mainScreen] bounds].size.height;

//Create blank image
CGImageRef blankImageRef = [self createBlankImageRef];
CFDataRef m_DataRef; 
m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(blankImageRef)); 
UInt8* bufferData = CFDataGetBytePtr(m_DataRef); 


//Manipulate the image here
for(int x=0; x < screenWidth-1; x++)
{
    int y = x;//*2;

    [self setPixel:bufferData width:screenWidth x:x y:y r:255 g:0 b:0];
}


//Paint the image to the screen
CGImageRef imageRef = blankImageRef; 
CGContextRef ctx; 
ctx = CGBitmapContextCreate(bufferData, 
                            CGImageGetWidth( imageRef ), 
                            CGImageGetHeight( imageRef ), 
                            8, 
                            CGImageGetBytesPerRow( imageRef ), 
                            CGImageGetColorSpace( imageRef ), 
                            kCGImageAlphaPremultipliedLast ); 
screenImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage (ctx)]; 
CGContextRelease(ctx); 

[imageView setImage:screenImage];

free(bufferData);
...

Code:
- (CGImageRef) createBlankImageRef
{
CGFloat imageScale = (CGFloat)1.0;
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
CGFloat height = [[UIScreen mainScreen] bounds].size.height;

// Create a bitmap graphics context of the given size
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, width * imageScale, height * imageScale, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);

// Draw ...
CGContextSetRGBFillColor(context, (CGFloat)0.0, (CGFloat)0.0, (CGFloat)0.0, (CGFloat)1.0 );

// Get your image
CGImageRef cgImage = CGBitmapContextCreateImage(context);

CGColorSpaceRelease(colorSpace);
CGContextRelease(context);

return cgImage;
}

-(void)setPixel:(UInt8*)buffer width:(int)width x:(int)x y:(int)y r:(int)r g:(int)g b:(int)b
{
buffer[x*4 + y*(width*4)] = r;
buffer[x*4 + y*(width*4)+1] = g;
buffer[x*4 + y*(width*4)+2] = b;
buffer[x*4 + y*(width*4)+3] = 255;
}
Any ideas how I could accomplish this using OpenGL ES (currently using 1.0) instead of Core Graphics?


You'd create a framebuffer object, and load it using glloadData.

Textures are another way to do this. Take a look at the sample app GLCameraRipple that is in the Xcode 4.2 docs. It loads frames of video from the camera live as OpenGL textures, then transforms them using a mesh. Cool stuff.

Note that you really don't want to create a pixel buffer in main memory and install it on the graphics hardware. That defeats the purpose of hardware graphics acceleration. You tie up the CPU building all that pixel data, and then you tie up the whole system while you upload all that data to video memory.

Your setPixel code is a VERY slow way to draw. You're setting up a stack frame with byte parameters for for your R, G, and B values, then doing array math to calculate the index for each byte, then storing a single pixel's data into a Core Graphics context.

You should write your code to render your images directly in OpenGL.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C 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: 401
11 members and 390 guests
buggen, EvilElf, guusleijsten, j.b.rajesh@gmail.com, LunarMoon, morterbaher, QuantumDoja, sacha1996, Sami Gh, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,673
Threads: 94,122
Posts: 402,906
Top Poster: BrianSlick (7,990)
Welcome to our newest member, morterbaher
Powered by vBadvanced CMPS v3.1.0

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