NSImage alpha channel question

PowerMacX

Registered
I was playing around modifying Apple's sample source code TransformedImageView, wich is included in the Developer Tools CD (or can be downloaded from apple's site), when I discovered something weird:
Basically, the program lets you apply a transform (rotation,scaling,shearing) to an image. But the interesting part is that it also lets you create a sample image from scratch, pixel by pixel. This image is a gradient created by modifing byte values on a NSBitmapImageRep, then adding that representation to a new NSImage and finally passing that NSImage to the NSImageView. The bitmap is created by setting the byte values for red, green, blue and alpha for each pixel. The problem I found is that if any of the red, green or blue values is higher than the alpha value in a particular pixel, the result is some random color. I think it has something to do with premultiplied values. I managed to make semi-transparent images render correctly, but only after scaling the rgb values so none of them are larger than the alpha value, but that involves 3 divisions and 3 multiplications for each pixel in the image, wich takes some time. MY QUESTION: IS THERE ANY WAY TO AVOID THIS? I mean, asuming the the NSImage thinks that the values are premultiplied or something, is there any way I can specify that they are not?
 
Make sure you're using the NSCalibratedRGBColorSpace - if you're not, changing the rgb & alpha levels will be strange. You can change a color to this space with:

[color colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
 
Back
Top