home *** CD-ROM | disk | FTP | other *** search
- #include "AlphaChannel.h"
- #include "assert_mac.h"
-
- // ---------------------------------------------------------------------------
-
- #define kRowBytesMask 0x7FFF
-
- // ---------------------------------------------------------------------------
-
- static ThirtyTwoBitPixelPtr Get32BitPixel(
- GWorldPtr srcBuffer,
- short h,
- short v);
-
- static void Set32BitPixel(
- GWorldPtr destBuffer,
- ThirtyTwoBitPixelPtr pixel,
- short h,
- short v);
-
- static void Set8BitPixel(
- GWorldPtr destBuffer,
- unsigned char pixel,
- short h,
- short v);
-
- // ---------------------------------------------------------------------------
-
- Boolean HasPossibleAlphaChannel(GWorldPtr buffer) {
- PixMapHandle pixie;
-
- pixie = GetGWorldPixMap(buffer);
- if (pixie == NULL)
- return(false);
-
- if ((**pixie).pixelSize == 32 &&
- (**pixie).cmpCount == 3 &&
- (**pixie).cmpSize == 8)
- return(true);
- else
- return(false);
- } // END HasPossibleAlphaChannel
-
- // ---------------------------------------------------------------------------
-
- void SplitAlphaChannel(
- GWorldPtr srcBuffer,
- GWorldPtr imageBuffer,
- GWorldPtr alphaBuffer,
- AlphaPixelFilterProc pixelProc) {
-
- Rect bufferArea;
- ThirtyTwoBitPixelPtr pixel;
- ThirtyTwoBitPixel aPixel;
- short h, v;
-
- if (!HasPossibleAlphaChannel(srcBuffer)) {
- SysBeep(10);
- return;
- }
-
- bufferArea = (**GetGWorldPixMap(srcBuffer)).bounds;
- OffsetRect(&bufferArea, -bufferArea.left, -bufferArea.top);
-
- for (v = 0; v < bufferArea.bottom; v++) {
- for (h = 0; h < bufferArea.right; h++) {
- pixel = Get32BitPixel(srcBuffer, h, v);
- aPixel = *pixel;
- // alpha == 0 -> pixel is a mask, not part of image
- // alpha > 0 -> pixel is part of image
- if (aPixel.alpha == 0) {
- aPixel.red = 255;
- aPixel.green = 255;
- aPixel.blue = 255;
- }
- /*
- aPixel.red |= aPixel.alpha;
- aPixel.green |= aPixel.alpha;
- aPixel.blue |= aPixel.alpha;
- */
-
- if (pixelProc != NULL)
- pixelProc(&aPixel);
-
- if (imageBuffer != NULL)
- Set32BitPixel(imageBuffer, &aPixel, h, v);
- if (alphaBuffer != NULL)
- Set8BitPixel(alphaBuffer, aPixel.alpha, h, v);
- }
- }
- } // END SplitAlphaChannel
-
- // ---------------------------------------------------------------------------
-
- /*
- Assumes:
- 1) Cpu is in 32-bit addressing mode. If in 24-bit mode, may crash.
- 2) GWorld's pixmap is LOCKED. Use LockPixels(). If not, may crash.
- */
- ThirtyTwoBitPixelPtr Get32BitPixel(
- GWorldPtr srcBuffer,
- short h,
- short v) {
-
- PixMapHandle pixie;
- ThirtyTwoBitPixelPtr srcMem;
- unsigned long realRowBytes;
-
- pixie = GetGWorldPixMap(srcBuffer);
- ASSERT(pixie != NULL);
-
- realRowBytes = kRowBytesMask & (**pixie).rowBytes;
-
- // Calculate address
- srcMem = (ThirtyTwoBitPixelPtr)(GetPixBaseAddr(pixie) +
- (realRowBytes * v) + (h*4));
-
- return(srcMem);
- } // END Get32BitPixel
-
- void Set32BitPixel(
- GWorldPtr destBuffer,
- ThirtyTwoBitPixelPtr pixel,
- short h,
- short v) {
-
- PixMapHandle pixie;
- ThirtyTwoBitPixelPtr destMem;
- unsigned long realRowBytes;
-
- pixie = GetGWorldPixMap(destBuffer);
- ASSERT(pixie != NULL);
- if (pixie == NULL)
- return;
-
- realRowBytes = (**pixie).rowBytes & kRowBytesMask;
-
- // Calculate address
- destMem = (ThirtyTwoBitPixelPtr)(GetPixBaseAddr(pixie) +
- (realRowBytes * v) + (h*4));
-
- *destMem = *pixel;
- } // END Get32BitPixel
-
- // ---------------------------------------------------------------------------
-
- void Set8BitPixel(
- GWorldPtr destBuffer,
- unsigned char pixel,
- short h,
- short v) {
-
- PixMapHandle pixie;
- unsigned char *destMem;
- unsigned long realRowBytes;
-
- pixie = GetGWorldPixMap(destBuffer);
- ASSERT(pixie != NULL);
- if (pixie == NULL)
- return;
-
- realRowBytes = (**pixie).rowBytes & kRowBytesMask;
-
- // Calculate address
- destMem = (unsigned char*)(GetPixBaseAddr(pixie) +
- (realRowBytes * v) + (h));
-
- *destMem = pixel;
- } // END Get8BitPixel