L_SetBitmapAlpha

#include "l_bitmap.h"

L_INT EXT_FUNCTION L_SetBitmapAlpha(pBitmap, pAlpha)

pBITMAPHANDLE pBitmap;

/* points to the destination bitmap handle */

pBITMAPHANDLE pAlpha;

/* points to the source bitmap handle */

Replaces existing alpha channel data or creates new alpha channel data in the destination bitmap.

Parameter

Description

pBitmap

Points to the destination bitmap handle, which references the bitmap where the alpha channel data will be updated or added.

pAlpha

Points to the source bitmap handle, which references the bitmap that is used to create or replace the alpha channel data.

Returns

SUCCESS

The function was successful.

< 1

An error occurred. Refer to Return Codes.

Comments

Typically, an alpha channel contains a mask that is used for transparency. This function, together with L_GetBitmapAlpha and the LEADTOOLS region processing functions, lets you use the alpha channel to implement transparency.

Only 16-, 32- and 64-bit images can have an alpha channel. If pBitmap references a bitmap of any other color resolution, this function converts the pBitmap bitmap to 32-bit before it replaces the alpha channel. (If you want 16-bit, you should change it using L_ColorResBitmap, if necessary, before calling this function.)

For 64-bit bitmaps, pAlpha is converted to 16 bit grayscale before replacing the alpha channel. If pBitmap is 48-bit, it will be converted to 64-bit and then have the alpha information added to it.

The pAlpha bitmap does not have to be grayscale. This function converts the data from the pAlpha bitmap to the necessary grayscale or 1-bit format as it replaces the alpha channel in pBitmap. (The pAlpha bitmap, itself, remains unchanged.)

This function does not support signed data images, but only DICOM images are supported as signed data. It returns the error code ERROR_SIGNED_DATA_NOT_SUPPORTED if a signed data image, other than a DICOM image, is passed to this function.

Required DLLs and Libraries

LTKRN

For a listing of the exact DLLs and Libraries needed, based on the toolkit version, refer to Files To Be Included With Your Application.

Platforms

Windows 95 / 98 / Me, Windows 2000 / XP.

See Also

Functions:

L_GetBitmapAlpha, L_CreateMaskFromBitmapRgn, L_SetBitmapRgnFromMask

Topics:

Defining and Using a Bitmap Region

 

Implementing Transparency

 

Saving a Region

Example

/* This example shows how to create, save, and use a mask for transparency. 
We assume that the window has already been created and sized to fit the aspect
ratio of the image specified in pszFilename. */
void TestFunction(HWND hWnd, L_TCHAR* szFilename)
{
   BITMAPHANDLE MainBitmap;   /* Main bitmap */
   BITMAPHANDLE AlphaBitmap;  /* Alpha channel bitmap */
   RGNXFORM XFormToBitmap;    /* Structure for transforming display coordinates */
   HDC hWindowDC;    /* Device context of the current window */
   RECT rClientArea; /* Client area of the current window */
   RECT rRgnRect;    /* Rectangle that defines the current region */
   HPALETTE hSavedPalette = NULL;   /* Temporary copy of the current system palette */
   HPALETTE hOurPalette = NULL;     /* The palette that we will use to paint */
   /* Get the device context of the current window */
   hWindowDC = GetDC (hWnd); 
   /* Get the client area of the current window */
   GetClientRect(hWnd,&rClientArea);
   /* Load a bitmap at 16 bits per pixel */
   L_LoadBitmap(szFilename, &MainBitmap, sizeof(BITMAPHANDLE), 16, ORDER_BGR, NULL, NULL);
   /* Set XFormToBitmap fields, assuming that the display rectangle is the same
   as the client area of the current window */
   XFormToBitmap.uViewPerspective = TOP_LEFT;
   XFormToBitmap.nXScalarNum = BITMAPWIDTH(&MainBitmap);
   XFormToBitmap.nXScalarDen = rClientArea.right;
   XFormToBitmap.nYScalarNum = BITMAPHEIGHT(&MainBitmap);
   XFormToBitmap.nYScalarDen = rClientArea.bottom;
   XFormToBitmap.nXOffset = 0;
   XFormToBitmap.nYOffset = 0;
   /* Specify a rectangle to define the region */
   SetRect(&rRgnRect, rClientArea.right/8, rClientArea.bottom/8, 
      rClientArea.right/2, rClientArea.bottom/2);
   /* Create an elliptical region in the AlphaBitmap */
   L_SetBitmapRgnEllipse(&MainBitmap, &XFormToBitmap, &rRgnRect, L_RGN_SET);
   /* Create a mask bitmap from the region */
   L_CreateMaskFromBitmapRgn ( &MainBitmap, &AlphaBitmap, sizeof(BITMAPHANDLE)  );
   /* Update the alpha channel in the main bitmap */
   L_SetBitmapAlpha(&MainBitmap, &AlphaBitmap);
   /* Save the bitmap at 16 bits per pixel to keep the alpha channel */
   L_SaveBitmap(TEXT("TEST.TIF"), &MainBitmap, FILE_TIF, 16, 0, NULL);
   /* Free the bitmaps */
   L_FreeBitmap(&MainBitmap);
   L_FreeBitmap(&AlphaBitmap);
   /* Load the bitmap that we just saved and get its alpha channel */
   L_LoadBitmap(TEXT("TEST.TIF"), &MainBitmap, sizeof(BITMAPHANDLE), 0, ORDER_BGR, NULL, NULL);
   L_GetBitmapAlpha(&MainBitmap, &AlphaBitmap, sizeof(BITMAPHANDLE));
   /* Use the AlphaBitmap as a mask to set the region in the MainBitmap */
   L_SetBitmapRgnFromMask ( &MainBitmap, NULL, &AlphaBitmap, L_RGN_SET );
   /* Create the palette that we will use to paint */
   hOurPalette = L_CreatePaintPalette(hWindowDC, &MainBitmap);
   /* Select our palette and save the old one */
   hSavedPalette = SelectPalette (hWindowDC, hOurPalette, FALSE);
   /* Realize our palette */
   RealizePalette (hWindowDC);
   /* Paint the region */
   L_PaintRgnDC(hWindowDC, /* Device context */
               &MainBitmap,/* Bitmap handle */
               NULL,       /* Default source rectangle */
               NULL,       /* Default source clip area */
               &rClientArea, /* Destination rectangle */
               NULL,       /* Default destination clipping rectangle */
               SRCCOPY);   /* ROP3 code for a Normal Paint */
   /* Restore the old palette */
   SelectPalette (hWindowDC, hSavedPalette, FALSE);
   /* Release the device context */
   ReleaseDC(hWnd, hWindowDC);
   /* Free the AlphaBitmap */
   L_FreeBitmap(&AlphaBitmap);
   return;
}