soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
gdialpha.cpp
1#include "gdialpha.h"
2#include <malloc.h>
3#ifdef _WIN32
4SNSBEGIN
5
6#define MAX_ALPHABUF 1<<16
7
8BYTE CGdiAlpha::s_byAlphaBack[MAX_ALPHABUF];
9
10LPBYTE CGdiAlpha::ALPHABACKUP(BITMAP *pBitmap,int x,int y,int cx,int cy)
11{
12 LPBYTE lpAlpha=s_byAlphaBack;
13 if(x+cx>=pBitmap->bmWidth) cx=pBitmap->bmWidth-x;
14 if(y+cy>=pBitmap->bmHeight) cy=pBitmap->bmHeight-y;
15 if(cx<0 || cy<0 ||pBitmap->bmBits==NULL) return NULL;
16
17 if(cx*cy>MAX_ALPHABUF) lpAlpha=(LPBYTE)malloc(cx*cy);
18 LPBYTE lpBits=NULL;
19 for(int iRow=0; iRow<cy; iRow++)
20 {
21 lpBits=(LPBYTE)pBitmap->bmBits+(y+iRow)*pBitmap->bmWidth*4+x*4;
22 lpBits+=3;
23 for(int iCol=0; iCol<cx; iCol++)
24 {
25 lpAlpha[iRow*cx+iCol]=*lpBits;
26 lpBits+=4;
27 }
28 }
29 return lpAlpha;
30}
31
32//恢复位图的Alpha通道
33void CGdiAlpha::ALPHARESTORE(BITMAP *pBitmap,int x,int y,int cx,int cy,LPBYTE lpAlpha)
34{
35 if(x+cx>=pBitmap->bmWidth) cx=pBitmap->bmWidth-x;
36 if(y+cy>=pBitmap->bmHeight) cy=pBitmap->bmHeight-y;
37 if(cx<0 || cy<0) return;
38 LPBYTE lpBits=NULL;
39 for(int iRow=0; iRow<cy; iRow++)
40 {
41 lpBits=(LPBYTE)pBitmap->bmBits+(y+iRow)*pBitmap->bmWidth*4+x*4;
42 lpBits+=3;
43 for(int iCol=0; iCol<cx; iCol++)
44 {
45 *lpBits=lpAlpha[iRow*cx+iCol];
46 lpBits+=4;
47 }
48 }
49 if(lpAlpha!=s_byAlphaBack) free(lpAlpha);
50}
51
52BOOL CGdiAlpha::AlphaBackup(HDC hdc,LPCRECT pRect,ALPHAINFO &alphaInfo)
53{
54 alphaInfo.lpBuf=NULL;
55 HBITMAP hBmp=(HBITMAP)GetCurrentObject(hdc,OBJ_BITMAP);
56 SASSERT(hBmp);
57 GetObject(hBmp,sizeof(BITMAP),&alphaInfo.bm);
58
59 if(alphaInfo.bm.bmBitsPixel!=32) return FALSE;
60 alphaInfo.rc=*pRect;
61 //draw rectangle need extend the right and bottom 1 px;
62 alphaInfo.rc.right ++;
63 alphaInfo.rc.bottom ++;
64 POINT pt;
65 GetViewportOrgEx(hdc,&pt);
66 RECT rcImg= {0,0,alphaInfo.bm.bmWidth,alphaInfo.bm.bmHeight};
67 OffsetRect(&alphaInfo.rc,pt.x,pt.y);
68 IntersectRect(&alphaInfo.rc,&alphaInfo.rc,&rcImg);
69 alphaInfo.lpBuf=ALPHABACKUP(&alphaInfo.bm,alphaInfo.rc.left,alphaInfo.rc.top,alphaInfo.rc.right - alphaInfo.rc.left, alphaInfo.rc.bottom - alphaInfo.rc.top);
70 return TRUE;
71}
72
73void CGdiAlpha::AlphaRestore(ALPHAINFO &alphaInfo)
74{
75 if(!alphaInfo.lpBuf) return;
76 ALPHARESTORE(&alphaInfo.bm,alphaInfo.rc.left,alphaInfo.rc.top,alphaInfo.rc.right - alphaInfo.rc.left, alphaInfo.rc.bottom - alphaInfo.rc.top,alphaInfo.lpBuf);
77}
78
79SNSEND
80
81#endif //_WIN32