Re: Creating a NSBitmapImageRep
Re: Creating a NSBitmapImageRep
- Subject: Re: Creating a NSBitmapImageRep
- From: Richard Schreyer <email@hidden>
- Date: Wed, 3 Jul 2002 00:21:17 -0700
On Tuesday, July 2, 2002, at 10:52 PM, Dave Hayden wrote:
On Tue, 2 Jul 2002, Richard Schreyer wrote:
The problem is the image isn't getting created correctly. If I give it
an alpha of 255, everything works, and I get the desired color. If I
set the alpha to 254, and then display the image, it draws black lines
across the color in-line with the white portions of the pinstripes
behind it. If I set the alpha to 253 or lower, I end up with
transparent gray, it's transparency varying with the alpha.
Does anyone know what might be causing this?
Note this line from the initWithBitmapDataPlanes:... docs:
If a coverage plane exists, the bitmap's color components must be
premultiplied with it.
That means your legal RGB values range from 0 up to alpha, not 255. If
you change your inner loop to
imageBytes[pixIndex] = 0; // red
imageBytes[pixIndex+1] = 0; // green
imageBytes[pixIndex+2] = 128; // blue
imageBytes[pixIndex+3] = 128; // alpha
you'll get a half-transparent fully saturated blue image.
Ah, thanks. That fixed it. Since I was just giving it nil as the
dataPlanes argument, I didn't think that paragraph applied to what I was
doing, and sort of glossed over that bit. For the the record, this
seems to work:
@implementation NSImage (RSImageExtensions)
+ (NSImage*)verticalGradientOfSize:(NSSize)size
withColor:(NSColor*)color {
NSImage* image = [[NSImage alloc] initWithSize: size];
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes: nil
pixelsWide: size.width pixelsHigh: size.height
bitsPerSample: 8 samplesPerPixel: 4
hasAlpha: YES isPlanar: NO
colorSpaceName: NSCalibratedRGBColorSpace
bytesPerRow: 0 bitsPerPixel: 32];
unsigned char* imageBytes = [bitmap bitmapData];
NSColor* rgbColor = [color colorUsingColorSpaceName:
NSCalibratedRGBColorSpace];
int x, y;
float r, g, b;
NSAssert(rgbColor != nil, @"colorspace conversion failed");
[rgbColor getRed:&r green:&g blue:&b alpha:nil];
[image addRepresentation: bitmap];
for (y = size.height-1; y >= 0; y--) {
for (x = size.width-1; x >= 0; x--) {
int pixIndex = 4 * (y*(int)size.width+x);
int alpha = (255/size.height)*(size.height-y);
imageBytes[pixIndex] = (int)(r*alpha);
imageBytes[pixIndex+1] = (int)(g*alpha);
imageBytes[pixIndex+2] = (int)(b*alpha);
imageBytes[pixIndex+3] = alpha;
}
}
[bitmap release];
return [image autorelease];
}
@end
Richard Schreyer
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.