life ideas

May 22, 2007

BMP位图结构与操作

Filed under: Uncategorized — manoftoday @ 11:06 pm

monochrome 位图 forground : 黑色 (0) RGB(0,0,0)
monochrome 位图 background: 白色 (1) RGB(255,255,255)

A) 生成与原图同样大小的单色掩码位图 hMaskDC 周边透明区域为背景白色,图象区域为前景黑色

When transferring bytes from a color bitmap to a monochrome bitmap, All pixels in color bitmap that are NOT the same color as defined background color become black in
the mono bitmap (white). other pixels that have same color are mapped to background of monochrome(white).The effect is of a black hole being cut in a white region.

hImageDC 中的周边透明区域是蓝色, 图象区域是本色
设置 hImageDC 背景蓝色, 前景 未知.

(hImageDC 中的前景图象 mapped 成 HMaskDC 前景黑色, 背景色 mapped 成 HMaskDC 背景 白色)

HMaskDC mapped ==> 周边透明区域是白色, 图象区域黑色

———————————————————–
结果 hMaskDC 中的周边透明区域是背景白色, 图象区域是前景黑色;

COLORREF clrSaveBkOfImageDC = SetBkColor(hImageDC, RGB(0,0,0xff)); // 设置背景色为蓝色
/*
or beter approach

COLORREF clrTopLeft = GetPixel(hImageDC,0,0); // clrTopLeft 是蓝色
COLORREF clrSaveBkOfImageDC = SetBkColor(hImageDC,clrTopLeft);
*/

BitBlt(hMaskDC, 0, 0, nWidthDest, nHeightDest, hImageDC, 0, 0, SRCCOPY); // 拷贝到hMaskDC

B) // 生成透明区域为黑色,其它区域保持不变的位图 hImageDC

When blitting from mono to color, the monochrome pixel is first transformed as follows:
the pixel that has drawing (i.e. is black 1) will be mapped to the color bitmaps’s FR color as defined by SetTextColor() ;
the pixel that has NO drawing (i.e. is white 0) will be mapped to the color bitmaps’s BG color as SetBkColor().
Only then is the raster operation performed.

hMaskDC 中的周边透明区域是白色, 图象区域是黑色
设置 hImageDC 背景黑色, 前景 白色色.

(hMaskDC 中的白色 mapped 成 hImageDC背景色, 黑色 mapped 成 hImageDC前景色)

HMaskDC mapped ==> 周边透明区域是黑色, 图象区域白色

&             hImageDC 中的周边透明区域是蓝色, 图象区域本色
———————————————————–
                                                                      黑色                    本色
结果 hImageDC 中的周边透明区域是黑色, 图象区域是本色;

SetBkColor(hImageDC, RGB(0,0,0)); //black

COLORREF clrSaveFgOfImageDC = SetTextColor(hImageDC, RGB(255,255,255)); //white

BitBlt(hImageDC, 0, 0, nWidthDest, nHeightDest, hMaskDC, 0, 0, SRCAND);

=======================================================================================
// NOW We are going to paint the two DDB’s in sequence to the destination.
hMaskDC 中的周边透明区域是白色, 图象区域是前景黑色;
|| hImageDC 中的周边透明区域是黑色, 图象区域是本色;
C)
// 1st the monochrome bitmap will be blitted using an AND operation to
// cut a hole in the destination.
COLORREF clrSaveBkOfDestDC = SetBkColor(hdcDest,RGB(255,255,255));//设置背景白色
COLORREF clrSaveFgOfDestDC = SetTextColor(hdcDest,RGB(0,0,0)); //设置前景黑色
BitBlt(hdcDest,0,0,nWidthDest,nHeightDest,hMaskDC,0,0,SRCAND);

hMaskDC 中的周边透明区域是白色, 图象区域是黑色
设置 hdcDest 背景白色, 前景 黑色.

(hMaskDC 中的白色 mapped 成 hdcDest 背景色, 黑色 mapped 成 hdcDest前景色)

HMaskDC mapped ==> 周边透明区域是白色, 图象区域黑色

&                      hdcDest 中的周边透明区域是空, 图象区域是空
—————————————————–
                                                                            空                     黑色
结果 hdcDest 中的周边透明区域是空, 图象区域是黑色;

D) //2nd The color image will then be OR operation
// with the destination, filling it into the hole, but leaving the
// surrounding area untouched.
hImageDC 继续保持从step b) 设置的背景黑色, 前景 白色.
hImageDC from step B) 中的周边透明区域是黑色, 图象区域是本色
|| hdcDest from step C) 中的周边透明区域是空,    图象区域是黑色
———————————————————————-
                                                                                空                         本色
因为hImageDC在step b) 背景设置成了黑色,hImageDC 中的周边透明区域将不被绘制到hdDest, 而图象区域的本色则是或到hdcDest而被完全呈现;

BitBlt(hdcDest,0,0,nWidthDest,nHeightDest,hImageDC,0,0,SRCPAINT);

 

Prepare

// The bitmap should be stored as a resource in the exe file.
// We pass the hInstance of the application, and the ID of the
// bitmap to the LoadBitmap API function and it returns us an
// HBITMAP to a DDB created from the resource data.
HINSTANCE hInstance = GetWindowInstance(hwnd);
HBITMAP hImageBMP = LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_BITMAP1));

BITMAP bm;
GetObject(hImageBMP,sizeof(bm),&bm);
nWidthDest = bm.bmWidth;
nHeightDest = bm.bmHeight;
HBITMAP hMaskBMP = CreateBitmap(bm.bmWidth,bm.bmHeight,1,1,NULL);
HDC hImageDC = CreateCompatibleDC(NULL);
HDC hMaskDC = CreateCompatibleDC(NULL);
HBITMAP hOldImageBMP = (HBITMAP)SelectBitmap(hImageDC,hImageBMP);
HBITMAP hOldMaskBMP = (HBITMAP)SelectBitmap(hMaskDC,hMaskBMP);

clean up
SetTextColor(hImageDC,clrSaveFgOfImageDC);
SetBkColor(hImageDC,clrSaveBkOfImageDC);
SetTextColor(hdcDest,clrSaveFgOfDestDC);
SetBkColor(hdcDest,clrSaveBkOfDestDC);
SelectObject(hImageDC, hOldImageBMP);
DeleteDC(hImageDC);

SelectObject(hMaskDC, hOldMaskBMP);
DeleteDC(hMaskDC);

DeleteObject(hImageBMP);
DeleteObject(hMaskBMP);

Link to yangyang » [转]BMP位图结构与操作

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: