Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Convert a PNG into RGBA data at 32 bits per pixel

Convert a PNG into RGBA data at 32 bits per pixel
Thread Tools
Amorya
Mac Elite
Join Date: Mar 2001
Location: England
Status: Offline
Reply With Quote
Sep 8, 2005, 10:59 AM
 
OK, in the app I'm writing I'm passed some PNG data and need to return it as RGBA.

What I've been doing is this:

Code:
NSBitmapImageRep *rep = [[NSBitmapImageRep imageRepWithData:[NSData dataWithBytes:data length:len]] retain]; *width = [rep pixelsWide]; *height = [rep pixelsHigh]; // NSLog(@"%i %i %i", *width, *height, [rep bitsPerPixel]/8); unsigned char *pixels = malloc(*width * *height * [rep bitsPerPixel]/8); memcpy(pixels, [rep bitmapData], (*width * *height * [rep bitsPerPixel]/8)); return pixels;
(data contains the PNG and len contains the length).

The problem is, not all these PNGs are 32-bit, and not all contain alpha. I need the bitmap data I return to be in the same format every time... preferably 32-bit RGBA.

Any ideas how I can do this?


Amorya
What the nerd community most often fail to realize is that all features aren't equal. A well implemented and well integrated feature in a convenient interface is worth way more than the same feature implemented crappy, or accessed through a annoying interface.
     
Dair Grant
Fresh-Faced Recruit
Join Date: Jan 2002
Status: Offline
Reply With Quote
Sep 8, 2005, 05:08 PM
 
Create a QuickTime graphics importer for your image, then get a CGImageRef with GraphicsImportCreateCGImage.

Allocate a width * height * sizeof(UInt32) block of memory to hold your 32-bpp RGBA data, then use CGBitmapContextCreate to create a CGContextRef that uses that memory.

Draw your image into the context with CGContextDrawImage, and QT will expand the image data out to the depth of the context.
     
Amorya  (op)
Mac Elite
Join Date: Mar 2001
Location: England
Status: Offline
Reply With Quote
Sep 8, 2005, 05:15 PM
 
Cool, thanks. Guess I'm off to read up on Quicktime!
What the nerd community most often fail to realize is that all features aren't equal. A well implemented and well integrated feature in a convenient interface is worth way more than the same feature implemented crappy, or accessed through a annoying interface.
     
   
 
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -4. The time now is 01:08 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,