home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Computer Buyer 1998 October
/
dpcb1098.iso
/
Business
/
Maxim
/
MAX5
/
data.z
/
Transblt.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1998-05-15
|
10KB
|
264 lines
//////////////////////////////////////////////////////////////////////////////
// NAME.......: Transblt.CPP
// PURPOSE....: copy bmp with one transparent color (usually background)
// Same as BitBlt except with one transpartent color
// WRITTEN....: 96/09/27 by Darko Juvan
//
// This code and information is provided "as is" without warranty of any
// kind, either expressed or implied, including but not limited to the
// implied warranties of merchantability and/or fitness for a particular
// purpose..
//
// Copyright (c) 1998 Multiactive Software Inc. All Rights Reserved.
//
//////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "transblt.h"
//////////////////////////////////////////////////////////////////////////////
//
// FUNCTION...: TransparentBlt ()
//
// DESCRIPTION: MFC version - using MFC GDI objects
//
//////////////////////////////////////////////////////////////////////////////
void TransparentBlt (CDC* pdc, CBitmap* pBitmap, int xStart,
int yStart, COLORREF cTransparentColor)
{
HDC hdc = pdc->GetSafeHdc();
HBITMAP hBitmap = (HBITMAP)pBitmap->GetSafeHandle();
HDC hdcMemX;
BITMAP bm;
POINT ptSize;
GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
ptSize.x = bm.bmWidth; // Get width of bitmap
ptSize.y = bm.bmHeight; // Get height of bitmap
// to logical points
//draw no transparent
if(cTransparentColor == -2)
{
hdcMemX = CreateCompatibleDC(hdc);
DPtoLP(hdcMemX, &ptSize, 1); // Convert from device
SelectObject(hdcMemX, hBitmap); // Select the bitmap
BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y, hdcMemX, 0, 0, SRCCOPY);
DeleteDC(hdcMemX);
return;
}
COLORREF cColor;
HBITMAP bmAndBack, bmAndObject, bmAndMem, bmSave;
HBITMAP bmBackOld, bmObjectOld, bmMemOld, bmSaveOld;
HDC hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave;
hdcTemp = CreateCompatibleDC(hdc);
SelectObject(hdcTemp, hBitmap); // Select the bitmap
DPtoLP(hdcTemp, &ptSize, 1); // Convert from device
//take color from (1,1) position
if(cTransparentColor == -1)
cTransparentColor = GetPixel(hdcTemp, 1, 1);
// Create some DCs to hold temporary data.
hdcBack = CreateCompatibleDC(hdc);
hdcObject = CreateCompatibleDC(hdc);
hdcMem = CreateCompatibleDC(hdc);
hdcSave = CreateCompatibleDC(hdc);
// Create a bitmap for each DC.
// Monochrome DC
bmAndBack = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
// Monochrome DC
bmAndObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
bmAndMem = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
bmSave = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
// Each DC must select a bitmap object to store pixel data.
bmBackOld = (HBITMAP) SelectObject(hdcBack, bmAndBack);
bmObjectOld = (HBITMAP) SelectObject(hdcObject, bmAndObject);
bmMemOld = (HBITMAP) SelectObject(hdcMem, bmAndMem);
bmSaveOld = (HBITMAP) SelectObject(hdcSave, bmSave);
// Set proper mapping mode.
SetMapMode(hdcTemp, GetMapMode(hdc));
// Save the bitmap sent here, because it will be overwritten.
BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);
// Set the background color of the source DC to the color.
// contained in the parts of the bitmap that should be transparent
cColor = SetBkColor(hdcTemp, cTransparentColor);
// Create the object mask for the bitmap by performing a BitBlt
// from the source bitmap to a monochrome bitmap.
BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0,
SRCCOPY);
// Set the background color of the source DC back to the original
// color.
SetBkColor(hdcTemp, cColor);
// Create the inverse of the object mask.
BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0,
NOTSRCCOPY);
// Copy the background of the main DC to the destination.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdc, xStart, yStart,
SRCCOPY);
// Mask out the places where the bitmap will be placed.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);
// Mask out the transparent colored pixels on the bitmap.
BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);
// XOR the bitmap with the background on the destination DC.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT);
// Copy the destination to the screen.
BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0,
SRCCOPY);
// Place the original bitmap back into the bitmap sent here.
BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);
// Delete the memory bitmaps.
DeleteObject(SelectObject(hdcBack, bmBackOld));
DeleteObject(SelectObject(hdcObject, bmObjectOld));
DeleteObject(SelectObject(hdcMem, bmMemOld));
DeleteObject(SelectObject(hdcSave, bmSaveOld));
// Delete the memory DCs.
DeleteDC(hdcMem);
DeleteDC(hdcBack);
DeleteDC(hdcObject);
DeleteDC(hdcSave);
DeleteDC(hdcTemp);
}
//////////////////////////////////////////////////////////////////////////////
//
// FUNCTION...: TransparentBlt ()
//
// DESCRIPTION: windows API version - using GDI handlers
//
//////////////////////////////////////////////////////////////////////////////
void TransparentBlt (HDC hdc, HBITMAP hBitmap, int xStart,
int yStart, COLORREF cTransparentColor)
{
HDC hdcMemX;
BITMAP bm;
POINT ptSize;
GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
ptSize.x = bm.bmWidth; // Get width of bitmap
ptSize.y = bm.bmHeight; // Get height of bitmap
//draw no transparent
if(cTransparentColor == -2)
{
hdcMemX = CreateCompatibleDC(hdc);
SelectObject(hdcMemX, hBitmap); // Select the bitmap
DPtoLP(hdcMemX, &ptSize, 1); // Convert from device
BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y, hdcMemX, 0, 0, SRCCOPY);
DeleteDC(hdcMemX);
return;
}
COLORREF cColor;
HBITMAP bmAndBack, bmAndObject, bmAndMem, bmSave;
HBITMAP bmBackOld, bmObjectOld, bmMemOld, bmSaveOld;
HDC hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave;
hdcTemp = CreateCompatibleDC(hdc);
SelectObject(hdcTemp, hBitmap); // Select the bitmap
DPtoLP(hdcTemp, &ptSize, 1); // Convert from device
// to logical points
//take color from (1,1) position
if(cTransparentColor == -1)
cTransparentColor = GetPixel(hdcTemp, 1, 1);
// Create some DCs to hold temporary data.
hdcBack = CreateCompatibleDC(hdc);
hdcObject = CreateCompatibleDC(hdc);
hdcMem = CreateCompatibleDC(hdc);
hdcSave = CreateCompatibleDC(hdc);
// Create a bitmap for each DC.
// Monochrome DC
bmAndBack = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
// Monochrome DC
bmAndObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
bmAndMem = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
bmSave = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
// Each DC must select a bitmap object to store pixel data.
bmBackOld = (HBITMAP) SelectObject(hdcBack, bmAndBack);
bmObjectOld = (HBITMAP) SelectObject(hdcObject, bmAndObject);
bmMemOld = (HBITMAP) SelectObject(hdcMem, bmAndMem);
bmSaveOld = (HBITMAP) SelectObject(hdcSave, bmSave);
// Set proper mapping mode.
SetMapMode(hdcTemp, GetMapMode(hdc));
// Save the bitmap sent here, because it will be overwritten.
BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);
// Set the background color of the source DC to the color.
// contained in the parts of the bitmap that should be transparent
cColor = SetBkColor(hdcTemp, cTransparentColor);
// Create the object mask for the bitmap by performing a BitBlt
// from the source bitmap to a monochrome bitmap.
BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0,
SRCCOPY);
// Set the background color of the source DC back to the original
// color.
SetBkColor(hdcTemp, cColor);
// Create the inverse of the object mask.
BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0,
NOTSRCCOPY);
// Copy the background of the main DC to the destination.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdc, xStart, yStart,
SRCCOPY);
// Mask out the places where the bitmap will be placed.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);
// Mask out the transparent colored pixels on the bitmap.
BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);
// XOR the bitmap with the background on the destination DC.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT);
// Copy the destination to the screen.
BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0,
SRCCOPY);
// Place the original bitmap back into the bitmap sent here.
BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);
// Delete the memory bitmaps.
DeleteObject(SelectObject(hdcBack, bmBackOld));
DeleteObject(SelectObject(hdcObject, bmObjectOld));
DeleteObject(SelectObject(hdcMem, bmMemOld));
DeleteObject(SelectObject(hdcSave, bmSaveOld));
// Delete the memory DCs.
DeleteDC(hdcMem);
DeleteDC(hdcBack);
DeleteDC(hdcObject);
DeleteDC(hdcSave);
DeleteDC(hdcTemp);
}