JPEG Image drawing in Cocoa/OpenGL?

honkFactory
Unregistered
 
Post: #1
Howdy,
I have been look around some of the sight that ya'll have mentioned on this sight and I have found a lot of cool stuff. What I haven't been able to find is a good source on the 2D capabilities of openGL. What I want to do is draw a jpg in cocoa. The NSImage and CGImageRef calls that I have tried are just too slow. If you know of any good info or source code, I would be much obliged.
Thank ye much,
A.W.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
What are you trying to use them for that they're too slow?

(BTW, I changed the thread title to give it some meaning)
Quote this message in a reply
honkFactory
Unregistered
 
Post: #3
I'm working on a sidescrolling game. So far, both Cocoa and core Quartz routines have proven too slow on many computers (G3s and slower G4s).
Quote this message in a reply
Member
Posts: 177
Joined: 2002.08
Post: #4
Load the image into an NSImage, then access the underlying NSBitmapImageRep. From that, you can get the raw bitmap data and load that into an OpenGL texture and bypass Quartz completely.
Quote this message in a reply
honkFactory
Unregistered
 
Post: #5
Good. I am glad to hear that you can go from NSImage to openGL. Does anyone know of sample code that does this? Or maybe a reference online.

AW
Quote this message in a reply
zzajin
Unregistered
 
Post: #6
I'm loading .png files this way:

Code:
NSData *raw = [NSData dataWithContentsOfFile:        
                                 @"/Users/me/Desktop/hack/test.png"];

    NSBitmapImageRep *rep =
      [NSBitmapImageRep imageRepWithData: raw];

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, &texture_id);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,
               [rep pixelsWide], [rep pixelsHigh], 0,
               GL_RGBA, GL_UNSIGNED_INT_8_8_8_8,
               [rep bitmapData]);
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #7
DANGER

Make sure you check the bits per sample, samples per pixel, whether the image is planar, whether the rowbytes match the image row length, &c, before you blindly pass the data off to OpenGL.

The code zzajin has posted will probably work for 32-bit color power-of-two images, however NSImage does some odd things if the image is non-power-of-two.

If you're happy using CG for this, using a CGImage may be the best way to go, since at least then you can specify the pixel and row format to be used.
Quote this message in a reply
zzajin
Unregistered
 
Post: #8
OSC is right - the above code lacks many error checks and is specific to a 32 bit power of 2 image. If you are ok with all of that you can try this for a .jpg file -

Code:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8,
               [rep pixelsWide], [rep pixelsHigh], 0,
               GL_RGB, GL_UNSIGNED_BYTE,
               [rep bitmapData]);
Quote this message in a reply
Member
Posts: 177
Joined: 2002.08
Post: #9
If you absolutely must use a non-power-of-two-sized image, there is a GL extension for that purpose, but it only works on recent hardware (notably excluding the Rage 128).
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #10
It works on all hardware accelerated by Mac OS X other than the Rage128.

It's still not safe to blindly pass an NSBitmapImageRep's data off to glTexImage2D(GL_TEXTURE_RECTANGLE_EXT, ...), though, since the number of bytes in an image row may not be equal to the number of bytes per pixel times the number of pixels.

Rectangular textures can't be mipmapped or repeated.
Quote this message in a reply
Member
Posts: 469
Joined: 2002.10
Post: #11
CocoaBlitz loads any NSImage into mosaic'd textures. 1 Method call to load, 1 method call to draw.

http://www.variableaspect.com/experiments/CocoaBlitz Smile

Use simple methods like [mySprite setLoc:NSMakePoint(10,10)] to move your images around the screen. Read the CBSprite header for more sprite manipulation methods. The examples are also pretty straight forward. My 450Mhz G3 has no problem pulling 60fps full screen scrolling.
</shameless plug>

---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Quote this message in a reply
Member
Posts: 89
Joined: 2002.04
Post: #12
I for one would love to see a tutorial about 2D graphics using OpenGL. The CocoaBlitz framework sounds interesting and easy to use but I would like to see how to do the "hard stuff". :-)

Quote:since the number of bytes in an image row may not be equal to the number of bytes per pixel times the number of pixels.


Small explanations like this helps out! Grin
Quote this message in a reply
Member
Posts: 469
Joined: 2002.10
Post: #13
here's the hack I wrote to convert 16bit grayscale (w/alpha) images into 32bit RGBA for use with openGL.
Code:
//16bit (8bit grayscale + 8bit alpha) -> 32bit (RGBA) conversion hack
//Assumed anImage defined as the 16bit image we're converting.
sourceBitmap = [[NSBitmapImageRep alloc] initWithData:[anImage TIFFRepresentation]];

//Check to see that we're dealing with a 16bit grayscale+alpha
if ([sourceBitmap samplesPerPixel] == 2 && [sourceBitmap bitsPerSample] == 8 ) {
    //Allocate a 32bit RGBA bitmap rep for drawing into.
    //The destination bitmap has the same dimensions as the source except for the
    //  fact that it has 4 samples per pixel (RGBA) instead of 2 (G/A).
    NSBitmapImageRep *destinationBitmap =
    [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
                                            pixelsWide:[sourceBitmap pixelsWide]
                                            pixelsHigh:[sourceBitmap pixelsHigh]
                                         bitsPerSample:8
                                       samplesPerPixel:4
                                              hasAlpha:YES
                                              isPlanar:YES
                                        colorSpaceName:NSCalibratedRGBColorSpace
                                           bytesPerRow:4*[sourceBitmap pixelsWide]
                                          bitsPerPixel:32];
    //Get the pointer references to the pixel data.
    //We're using unsigned chars (GLubyte's) so that we can increment by byte (1 byte = 1 sample).
    GLubyte *srcBuffer = (GLubyte*)[sourceBitmap bitmapData];
    GLubyte *dstBuffer = (GLubyte*)[destinationBitmap bitmapData];
    //iterate through our 16bit image byte by byte
    int i;
    for ( i = 0; i < [sourceBitmap pixelsWide]*[sourceBitmap  pixelsHigh]; i++ ) {
        dstBuffer[i*4] = srcBuffer[i*2];     //put grayscale value into R
        dstBuffer[i*4+1] = srcBuffer[i*2];   //put grayscale value into G
        dstBuffer[i*4+2] = srcBuffer[i*2];   //put grayscale value into B
        dstBuffer[i*4+3] = srcBuffer[i*2+1]; //put Alpha value into A
    }
    //Now that we've filled the destination bitmap, we can dispose of the source
    //  and change it's reference.
    [sourceBitmap release];
    sourceBitmap = destinationBitmap;
    [sourceBitmap retain];
}
//From here we can use our newly created 32bit RGBA bitmap rep to populate a texture.

I don't have much experience with image types in so I'm probably missing other conversion measures in CocoaBlitz, but I know that this code resolves my 16bit cursors quite well. So far I haven't had any other problems.

---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Quote this message in a reply
anarchie
Unregistered
 
Post: #14
Actually, kelvin, there's an NSBitmapImageRep method for converting a grayscale image to color, using an arbitrary color mapping. Here's some sample code:

Code:
//Assumed anImage defined as the 16bit image we're converting.
sourceBitmap = [[NSBitmapImageRep alloc] initWithData:[anImage TIFFRepresentation]];

//Check to see that we're dealing with a 16bit grayscale+alpha
if ([sourceBitmap samplesPerPixel] == 2 && [sourceBitmap bitsPerSample] == 8 ) {
[sourceBitmap colorizeByMappingGray:0.5
                                toColor:[NSColor greyColor] //greyColor is white=0.5
                           blackMapping:[NSColor blackColor]
                           whiteMapping:[NSColor whiteColor]];
}

Also, you're over-retaining your objects again. See my post in "multiple returns and arguments". If your code snippets are any indication, your CocoaBlitz framework must leak objects like a mofo. I'd be glad to help with that, by the way. PM, ICQ, or e-mail me if you're interested.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Opengl/Cocoa text rendering tesil 15 17,918 Mar 20, 2012 11:16 AM
Last Post: OneSadCookie
  Is cocoa drawing enough? Megamac04 4 3,118 Oct 1, 2010 09:24 PM
Last Post: Megamac04
  OpenGL Image Textures mikey 52 25,949 Jun 30, 2009 10:42 AM
Last Post: AnotherJake
  OpenGL Text Rendering (in Cocoa) daveh84 5 8,021 Feb 19, 2009 12:44 PM
Last Post: TomorrowPlusX
  OpenGL &amp; Cocoa - Improving frame rate daveh84 4 5,645 Feb 2, 2009 06:53 AM
Last Post: backslash