Re: Convert GWorldPtr into a NSImage
Re: Convert GWorldPtr into a NSImage
- Subject: Re: Convert GWorldPtr into a NSImage
- From: Eric Gorr <email@hidden>
- Date: Thu, 8 Jan 2009 08:53:18 -0500
On Jan 7, 2009, at 7:39 PM, Graham Cox wrote:
On 8 Jan 2009, at 10:27 am, Eric Gorr wrote:
On Jan 7, 2009, at 11:31 AM, Daniel Kennett wrote:
To further support this theory, take a look at this NSImage where
I mistakenly only flipped half of my data - the garbled half of
the image is from data with the wrong endian-ness:
Well, I wrote the code to change the pixel format from BGRA to ARGB.
Running the code:
if ( (**pixMapHandle).pixelFormat == k32BGRAPixelFormat ) {
NSInteger x;
NSInteger y;
Ptr currentRow = (**pixMapHandle).baseAddr;
for ( y = 0; y < pixels_high; y++ ) {
Uint32 *currentPixel = (Uint32*)currentRow;
for ( x = 0; x < pixels_wide; x++ ) {
Uint8 *components = (Uint8*)currentPixel;
Uint8 temp;
temp = components[3];
components[3] = components[0];
components[0] = temp;
temp = components[1];
components[1] = components[2];
components[2] = temp;
currentPixel++;
}
currentRow += rowBytes;
}
}
over the pixel data to swap the components around allowed
everything to draw correctly.
I have a feeling there is a better way to do this and, if so, I am
interested.
Or you could use Core Video - it looks (on an admittedly cursory
inspection) that you can get CV to perform the conversion for you
using:
CVPixelBufferCreateWithBytes(...)
One of the arguments is the pixel buffer attributes dictionary, and
one of its keys is:
kCVPixelBufferPixelFormatTypeKey
Which appears to take the QuickDraw pixelFormat value directly
(wrapped in a CFNumber). Looks to me as if this is designed to
handle the exact situation you're facing, that of making use of old
QD formats. The advantage of this approach is platform independence,
optimised for you and probably accelerated by the GPU.
Interesting. It looks like the function has a pixelFormatType
parameter, so there is no need to place it in the attributes
dictionary. While it does look like I could get a CVPixelBufferRef,
what is unclear is how I could get a NSImage from the
CVPixelBufferRef. I've searched briefly, but couldn't come up with
anything...all of the messages I found mentioning both these items
talk about converting a NSImage to a CVPixelBufferRef.
I will try CFSwapInt32BigToHost - that looks good.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden