bitmap2region.c
上传用户:tuheem
上传日期:2007-05-01
资源大小:21889k
文件大小:8k
- #include "stdafx.h"
- #include "globals.h"
- #define CPC_RECT_QUANTISE 4096
- HRESULT path_create_link(LPCSTR lpszPathObj, LPSTR lpszPathLink,
- LPSTR lpszDesc)
- {
- HRESULT hres;
- IShellLink *psl;
- hres = CoCreateInstance(&CLSID_ShellLink, NULL,
- CLSCTX_INPROC_SERVER, &IID_IShellLink, &psl);
- if (SUCCEEDED(hres)) {
- IPersistFile *ppf;
- psl->lpVtbl->SetPath(psl, lpszPathObj);
- psl->lpVtbl->SetDescription(psl, lpszDesc);
- hres = psl->lpVtbl->QueryInterface(psl, &IID_IPersistFile, &ppf);
- if (SUCCEEDED(hres)) {
- WORD wsz[MAX_PATH];
- MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz,
- MAX_PATH);
- hres = ppf->lpVtbl->Save(ppf, wsz, TRUE);
- ppf->lpVtbl->Release(ppf);
- }
- psl->lpVtbl->Release(psl);
- }
- return hres;
- }
- HRGN main_bitmap_to_region(HBITMAP hBmp, COLORREF cTransparentColor)
- {
- HRGN hRgn = NULL;
- DWORD* pBitmapBits = NULL;
- DWORD* pBitmapCursor;
- BITMAP bitmap;
- RGNDATA* pRGNData = NULL;
- RECT* pRectArray = NULL;
- int iLastRectIDX;
- int iRGNDataSize_Rects;
- int iRowIDX, iColIDX;
- DWORD dwTransMasked;
- BOOL bDetectedTransparentPixel;
- CP_ASSERT(hBmp);
- GetObject(hBmp, sizeof(bitmap), &bitmap);
- pBitmapBits = (DWORD*)malloc(sizeof(DWORD) * bitmap.bmWidth * bitmap.bmHeight);
- {
- BITMAPINFO bmi;
- HDC dc;
- memset(&bmi, 0, sizeof(bmi));
- bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
- bmi.bmiHeader.biPlanes = 1;
- bmi.bmiHeader.biBitCount = 32;
- bmi.bmiHeader.biCompression = BI_RGB;
- bmi.bmiHeader.biWidth = bitmap.bmWidth;
- bmi.bmiHeader.biHeight = -bitmap.bmHeight;
- dc = CreateCompatibleDC(NULL);
- GetDIBits(dc, hBmp, 0, bitmap.bmHeight, pBitmapBits, &bmi, DIB_RGB_COLORS);
- DeleteDC(dc);
- }
- dwTransMasked = cTransparentColor & 0x00FFFFFF;
- pBitmapCursor = pBitmapBits;
- iLastRectIDX = 0;
- iRGNDataSize_Rects = 0;
- bDetectedTransparentPixel = FALSE;
- for(iRowIDX =0; iRowIDX < bitmap.bmHeight; iRowIDX++)
- {
- BOOL bInStrip = FALSE;
- for(iColIDX =0; iColIDX < bitmap.bmWidth; iColIDX++, pBitmapCursor++)
- {
- if( (((*pBitmapCursor)&0x00FFFFFF)^dwTransMasked) == 0L)
- {
- bDetectedTransparentPixel = TRUE;
- if(bInStrip == TRUE)
- {
- bInStrip = FALSE;
- ((RECT*)pRGNData->Buffer)[iLastRectIDX].right = iColIDX;
- iLastRectIDX++;
- }
- }
- else
- {
- if(bInStrip == FALSE)
- {
- bInStrip = TRUE;
- if(iLastRectIDX == iRGNDataSize_Rects)
- {
- iRGNDataSize_Rects += CPC_RECT_QUANTISE;
- pRGNData = (RGNDATA*)realloc(pRGNData, sizeof(RGNDATAHEADER) + (iRGNDataSize_Rects * sizeof(RECT)));
- }
- ((RECT*)pRGNData->Buffer)[iLastRectIDX].left = iColIDX;
- ((RECT*)pRGNData->Buffer)[iLastRectIDX].top = iRowIDX;
- ((RECT*)pRGNData->Buffer)[iLastRectIDX].bottom = iRowIDX+1;
- }
- }
- }
- if(bInStrip == TRUE)
- {
- ((RECT*)pRGNData->Buffer)[iLastRectIDX].right = bitmap.bmWidth;
- iLastRectIDX++;
- }
- }
- free(pBitmapBits);
- if(bDetectedTransparentPixel == TRUE)
- {
- pRGNData->rdh.dwSize = sizeof(RGNDATAHEADER);
- pRGNData->rdh.iType = RDH_RECTANGLES;
- pRGNData->rdh.nCount = iLastRectIDX;
- pRGNData->rdh.nRgnSize = sizeof(RGNDATAHEADER) + (iLastRectIDX * sizeof(RECT));
- pRGNData->rdh.rcBound.left = 0;
- pRGNData->rdh.rcBound.top = 0;
- pRGNData->rdh.rcBound.right = bitmap.bmWidth;
- pRGNData->rdh.rcBound.bottom = bitmap.bmHeight;
- hRgn = ExtCreateRegion(NULL, pRGNData->rdh.nRgnSize, pRGNData);
- }
- if(pRGNData)
- free(pRGNData);
- return hRgn;
- }
- HRGN main_bitmap_to_region_1bit(HBITMAP hBmp, COLORREF cTransparentColor)
- {
- HRGN hRgn = NULL;
- BYTE* pBitmapBits = NULL;
- BYTE* pBitmapCursor;
- BITMAP bitmap;
- RGNDATA* pRGNData = NULL;
- RECT* pRectArray = NULL;
- int iStride;
- int iLastRectIDX;
- int iRGNDataSize_Rects;
- int iEndofLineCorrection;
- int iRowIDX, iColIDX;
- DWORD dwTransMask;
- BOOL bDetectedTransparentPixel;
- CP_ASSERT(hBmp);
- GetObject(hBmp, sizeof(bitmap), &bitmap);
- iStride = bitmap.bmWidth >> 3;
- if(bitmap.bmWidth & 0x7)
- iStride++;
- if(iStride&0x3)
- {
- iEndofLineCorrection = 0x4 - (iStride&0x3);
- iStride += iEndofLineCorrection;
- }
- else
- iEndofLineCorrection = 0;
- pBitmapBits = (BYTE*)malloc(iStride * bitmap.bmHeight);
- {
- BITMAPINFO* pBMI;
- HDC dc;
- pBMI = malloc(sizeof(BITMAPINFO) + (sizeof(RGBQUAD)<<1));
- memset(pBMI, 0, sizeof(*pBMI));
- pBMI->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
- pBMI->bmiHeader.biPlanes = 1;
- pBMI->bmiHeader.biBitCount = 1;
- pBMI->bmiHeader.biCompression = BI_RGB;
- pBMI->bmiHeader.biWidth = bitmap.bmWidth;
- pBMI->bmiHeader.biHeight = -bitmap.bmHeight;
- pBMI->bmiHeader.biSizeImage = iStride * bitmap.bmHeight;
- dc = CreateCompatibleDC(NULL);
- GetDIBits(dc, hBmp, 0, bitmap.bmHeight, pBitmapBits, pBMI, DIB_RGB_COLORS);
- free(pBMI);
- DeleteDC(dc);
- }
- bDetectedTransparentPixel = FALSE;
- pBitmapCursor = pBitmapBits;
- iLastRectIDX = 0;
- iRGNDataSize_Rects = 0;
- dwTransMask = 0x80;
- for(iRowIDX =0; iRowIDX < bitmap.bmHeight; iRowIDX++)
- {
- BOOL bInStrip = FALSE;
- for(iColIDX =0; iColIDX < bitmap.bmWidth; iColIDX++)
- {
- if( (*pBitmapCursor) & dwTransMask)
- {
- bDetectedTransparentPixel = TRUE;
- if(bInStrip == TRUE)
- {
- bInStrip = FALSE;
- ((RECT*)pRGNData->Buffer)[iLastRectIDX].right = iColIDX;
- iLastRectIDX++;
- }
- }
- else
- {
- if(bInStrip == FALSE)
- {
- bInStrip = TRUE;
- if(iLastRectIDX == iRGNDataSize_Rects)
- {
- iRGNDataSize_Rects += CPC_RECT_QUANTISE;
- pRGNData = (RGNDATA*)realloc(pRGNData, sizeof(RGNDATAHEADER) + (iRGNDataSize_Rects * sizeof(RECT)));
- }
- ((RECT*)pRGNData->Buffer)[iLastRectIDX].left = iColIDX;
- ((RECT*)pRGNData->Buffer)[iLastRectIDX].top = iRowIDX;
- ((RECT*)pRGNData->Buffer)[iLastRectIDX].bottom = iRowIDX+1;
- }
- }
- dwTransMask >>= 1;
- if(!dwTransMask)
- {
- dwTransMask = 0x80;
- pBitmapCursor++;
- }
- }
- if(bInStrip == TRUE)
- {
- ((RECT*)pRGNData->Buffer)[iLastRectIDX].right = bitmap.bmWidth;
- iLastRectIDX++;
- }
- if(dwTransMask != 0x80)
- pBitmapCursor++;
- dwTransMask = 0x80;
- pBitmapCursor += iEndofLineCorrection;
- }
- free(pBitmapBits);
- if(bDetectedTransparentPixel == TRUE)
- {
- pRGNData->rdh.dwSize = sizeof(RGNDATAHEADER);
- pRGNData->rdh.iType = RDH_RECTANGLES;
- pRGNData->rdh.nCount = iLastRectIDX;
- pRGNData->rdh.nRgnSize = sizeof(RGNDATAHEADER) + (iLastRectIDX * sizeof(RECT));
- pRGNData->rdh.rcBound.left = 0;
- pRGNData->rdh.rcBound.top = 0;
- pRGNData->rdh.rcBound.right = bitmap.bmWidth;
- pRGNData->rdh.rcBound.bottom = bitmap.bmHeight;
- hRgn = ExtCreateRegion(NULL, pRGNData->rdh.nRgnSize, pRGNData);
- }
- if(pRGNData)
- free(pRGNData);
- return hRgn;
- }