Folding in a byte
Folding in a byte
- Subject: Folding in a byte
- From: David Remahl <email@hidden>
- Date: Thu, 11 Oct 2001 14:57:49 +0200
Hi. This is strictly more of a C question, but I hope you will help me
anyway.
I have a 32 bit bitmap of dimensions 128 by 128 pixels, where the first
24 bits make up the bitmap (RGB values) and the last 8 bits are just
place holders. (an ICNS thumbnail resource if you must know...). I also
have a mask where each pixel is one byte. I want to place each of these
mask bytes in the placeholder byte in the bitmap data for use as alpha.
The problem is that I can't make any code that reliably does this. I
have looked at a class named IconFamily by Troy Stephens does it like
this:
hRawBitmapData = NewHandle( pixelsWide * pixelsWide * 4 );
result = GetIconFamilyData( hIconFamily, elementType,
hRawBitmapData );
if (result != noErr)
return nil;
// Get the corresponding raw, uncompressed 8-bit mask data.
hRawMaskData = NewHandle( pixelsWide * pixelsWide );
result = GetIconFamilyData( hIconFamily, maskElementType,
hRawMaskData );
if (result != noErr)
hRawMaskData = NULL;
// The retrieved raw bitmap data is stored at 32 bits per pixel: 3
bytes
// for the RGB color of each pixel, plus an extra unused byte. We
can
// therefore fold the mask data into the color data in-place (though
// getting the proper byte ordering requires some bit-shifting).
HLock( hRawBitmapData );
pRawBitmapData = (unsigned long*) *hRawBitmapData;
pRawBitmapDataEnd = pRawBitmapData + pixelsWide * pixelsWide;
if (hRawMaskData) {
char * myTestPointer = nil;
char * myTestMask = nil;
HLock( hRawMaskData );
while (pRawBitmapData < pRawBitmapDataEnd)
{
*pRawBitmapData++ = (*pRawBitmapData << 8) | *pRawMaskData++;
}
HUnlock( hRawMaskData );
} else {
while (pRawBitmapData < pRawBitmapDataEnd)
*pRawBitmapData++ = (*pRawBitmapData << 8) | 0xff;
}
However, this code produces incorrect alpha information in images where
some parts of the icon are transparent. Can you spot an error in the
above algorithm, or is there some incongruence between alpha used in NS*
classes compared to Carbon methodologies?
/ regards, David