Re: Convert GWorldPtr into a NSImage
Re: Convert GWorldPtr into a NSImage
- Subject: Re: Convert GWorldPtr into a NSImage
- From: Graham Cox <email@hidden>
- Date: Thu, 8 Jan 2009 11:39:03 +1100
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.
You could use this:
CFSwapInt32BigToHost
Converts a 32-bit integer from big-endian format to the host’s native
byte order.
uint32_t CFSwapInt32BigToHost ( uint32_t arg );
Parametersarg
The integer whose bytes should be swapped.
Return Value
The integer with its bytes swapped. If the host is big-endian, this
function returns arg unchanged.
Availability
• Available in Mac OS X v10.0 and later.
Related Sample Code
• TabsShowcase
Declared InCFByteOrder.h
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.
--Graham
_______________________________________________
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