Re: NSBitmapImageRep byte/color component ordering
Re: NSBitmapImageRep byte/color component ordering
- Subject: Re: NSBitmapImageRep byte/color component ordering
- From: "Brian Tarricone" <email@hidden>
- Date: Thu, 1 Nov 2007 10:13:53 -0700
On Nov 1, 2007 3:43 AM, Alastair Houghton <email@hidden> wrote:
> On 1 Nov 2007, at 00:06, Brian Tarricone wrote:
>
> > When I do all this when the source image is a frame from the movie
> > itself, it works fine. However, when I do this for an image fetched
> > from amazon.com, the post-trimmed image has the colors messed up; it
> > looks like the raw bitmap data is BGRA (or maybe ABGR, not sure), but
> > the new NSBitmapImageRep I create expects RGBA, so, in the final
> > image, the green and red components are swapped.
>
> As far as I'm aware, NSBitmapImageRep only supports RGBA and ARGB (for
> RGB data, that is). You can tell which using the -bitmapFormat
> method, and you can set which using the bitmapFormat: argument when
> creating a bitmap image rep.
The source data is actually a 24bpp image, no alpha channel, which I
get from an NSImage using -TIFFRepresentation. I'm converting it to
RGBA just by taking the raw pixels (using -bitmapData), creating a new
buffer, and copying the pixels into the new array, adding a 0xff alpha
value as the 4th uchar for every 3 uchars (taking into account row
padding, etc.). After I run the 32bpp data through my 'trimmer', I
pack the data into a new NSBitmapImageRep that I create as follows:
imgRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:width
pixelsHigh:height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:colorspace
bitmapFormat:format
bytesPerRow:0
bitsPerPixel:0];
Then I copy the raw pixel data int [imgRep bitmapData], adding row
padding if necessary, determined by comparing [imgRep bytesPerRow]
with (4 * width * height). 'colorspace' and 'format' are taken from
the source imgrep -- I've tried toggling NSAlphaFirstBitmapFormat in
'format', but the colors are still wrong, and the alpha is noticeably
messed up (i.e., not uniform 0xff opaque) without that flag set.
> If you're certain that the data you're seeing is BGRA, where are you
> getting it from?
An NSImage, -TIFFRepresentation. (The NSImage was created with
-initFromContentsOfURL:.) Note that this source NSImage displays
correctly in a NSImageView. No, I'm not 100% sure it's BGR, but
that's the only explanation I can come up with for the blue and red
colors being reversed when I copy the pixels to the new imgrep (which
presumably expects ARGB/RGBA only). Gonna start sampling known pixels
from the input to see what's going on.
> If something is returning an NSBitmapImageRep
> containing BGRA data, AFAIK that's wrong and you should file a bug
> report.
If I can confirm the colors are swapped, I'll do just that; thanks.
> Hopefully, though, it's just ARGB rather than RGBA and you can check
> or tweak the bitmapFormat in your code.
Or I'm just doing something else incorrectly, hopefully ^_^. I'm
going to try modifying everything (ugh) to just work on 24bpp images
so I don't do the intermediate conversion. I don't see why that could
be a problem, since it works for some source images but not others,
but I'm running out of ideas here.
Cheers,
Brian
_______________________________________________
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