Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Can't set alpha to zero in CGImage



I'm sorry to ask such a basic question, but I'm a newbie to CGImage. I'm using Xcode 1.5 on Mac OS X 10.3.9 to make an Objective-C, doc app.

My Objective
I have a RGB bitmap with no alpha. I make a new CGImage with alpha, and copy all the RGB colors making one of them transparent.

The Problem
My problem is in assigning zero to the byte that holds the alpha value for the color I want to make transparent (ie 255, 255, 254). The alpha value should be at *(p2 + 3), and I use *(p2 + 3) in a loop to assign the alpha value. However, when I write *(p2 + 3) = 0x00;, the debugger reports the RGB components as \xff\xff\xfe OK, but, skips writing anything for the alpha component. When I write *(p2 + 3) = (unsigned char)0; for alpha, the debugger again skips writing \xff\xff\xfe\x00 as I'd expect.

Two Questions
When you pre-multiply the RGB values by a zero alpha, the values go to zero. So why isn't the pixel black?


What value can I use to assign 0 to the alpha component at *(p2 + 3)?
Here's part of the code:


unsigned char* srcData = [srcImageRep bitmapData];
unsigned char* destData = (unsigned char *)finalDestDataPtr;
unsigned char *p1 = NULL;
unsigned char* p2 = NULL;


// sourceBytesPerPixel should equal 3, as source contains no alpla.
int sourceBytesPerPixel = [srcImageRep bitsPerPixel] / 8;
// destBytesPerPixel should equal 4, as destination contains alpha.
int destBytesPerPixel = CGImageGetBitsPerPixel(destImageRep) / 8;


for( y = 0; y < h; y++) {
for(x = 0; x < w; x++) {
p1 = srcData + sourceBytesPerPixel * (y * w + x);
p2 = destData + destBytesPerPixel * (y * w + x);


unsigned char componentOne = *p1;
unsigned char componentTwo = *(p1 + 1);
unsigned char componentThree = *(p1 + 2);


if(componentOne == 255 && componentTwo == 255 && componentThree == 254) { // Background color
*p2 = componentOne;
*(p2 + 1) = componentTwo;
*(p2 + 2) = componentThree;
*(p2 + 3) = 0x00; // Set alpha of area around costume to zero alpla to allow layering.
NSLog(@"Pause to check");
}
else {
*p2 = componentOne;
*(p2 + 1) = componentTwo;
*(p2 + 2) = componentThree;
*(p2 + 3) = 1; // If color is not the background color, assign alpha to fully opaque.
}
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Quartz-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/quartz-dev/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.