bitmap2region.c
上传用户:hxb_1234
上传日期:2010-03-30
资源大小:8328k
文件大小:8k
源码类别:

VC书籍

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "globals.h"
  3. #define CPC_RECT_QUANTISE 4096
  4. HRESULT path_create_link(LPCSTR lpszPathObj, LPSTR lpszPathLink,
  5.                          LPSTR lpszDesc)
  6. {
  7.     HRESULT hres;
  8.     IShellLink *psl;
  9.     hres = CoCreateInstance(&CLSID_ShellLink, NULL,
  10.                             CLSCTX_INPROC_SERVER, &IID_IShellLink, &psl);
  11.     if (SUCCEEDED(hres)) {
  12.         IPersistFile *ppf;
  13.         psl->lpVtbl->SetPath(psl, lpszPathObj);
  14.         psl->lpVtbl->SetDescription(psl, lpszDesc);
  15.         hres = psl->lpVtbl->QueryInterface(psl, &IID_IPersistFile, &ppf);
  16.         if (SUCCEEDED(hres)) {
  17.             WORD    wsz[MAX_PATH];
  18.             MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz,
  19.                                 MAX_PATH);
  20.             hres = ppf->lpVtbl->Save(ppf, wsz, TRUE);
  21.             ppf->lpVtbl->Release(ppf);
  22.         }
  23.         psl->lpVtbl->Release(psl);
  24.     }
  25.     return hres;
  26. }
  27. HRGN main_bitmap_to_region(HBITMAP hBmp, COLORREF cTransparentColor)
  28. {
  29.     HRGN hRgn = NULL;
  30.     DWORD* pBitmapBits = NULL;
  31.     DWORD* pBitmapCursor;
  32.     BITMAP bitmap;
  33.     RGNDATA* pRGNData = NULL;
  34.     RECT* pRectArray = NULL;
  35.     int iLastRectIDX;
  36.     int iRGNDataSize_Rects;
  37.     int iRowIDX, iColIDX;
  38.     DWORD dwTransMasked;
  39.     BOOL bDetectedTransparentPixel;
  40.     CP_ASSERT(hBmp);
  41.     GetObject(hBmp, sizeof(bitmap), &bitmap);
  42.     pBitmapBits = (DWORD*)malloc(sizeof(DWORD) * bitmap.bmWidth * bitmap.bmHeight);
  43.     {
  44.         BITMAPINFO bmi;
  45.         HDC dc;
  46.         memset(&bmi, 0, sizeof(bmi));
  47.         bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  48.         bmi.bmiHeader.biPlanes = 1;
  49.         bmi.bmiHeader.biBitCount = 32;
  50.         bmi.bmiHeader.biCompression = BI_RGB;
  51.         bmi.bmiHeader.biWidth = bitmap.bmWidth;
  52.         bmi.bmiHeader.biHeight = -bitmap.bmHeight;
  53.         dc = CreateCompatibleDC(NULL);
  54.         GetDIBits(dc, hBmp, 0, bitmap.bmHeight, pBitmapBits, &bmi, DIB_RGB_COLORS);
  55.         DeleteDC(dc);
  56.     }
  57.     dwTransMasked = cTransparentColor & 0x00FFFFFF;
  58.     pBitmapCursor = pBitmapBits;
  59.     iLastRectIDX = 0;
  60.     iRGNDataSize_Rects = 0;
  61.     bDetectedTransparentPixel = FALSE;
  62.     for(iRowIDX =0; iRowIDX < bitmap.bmHeight; iRowIDX++)
  63.     {
  64.         BOOL bInStrip = FALSE;
  65.         for(iColIDX =0; iColIDX < bitmap.bmWidth; iColIDX++, pBitmapCursor++)
  66.         {
  67.             if( (((*pBitmapCursor)&0x00FFFFFF)^dwTransMasked) == 0L)
  68.             {
  69.                 bDetectedTransparentPixel = TRUE;
  70.                 if(bInStrip == TRUE)
  71.                 {
  72.                     bInStrip = FALSE;
  73.                     ((RECT*)pRGNData->Buffer)[iLastRectIDX].right = iColIDX;
  74.                     iLastRectIDX++;
  75.                 }
  76.             }
  77.             else
  78.             {
  79.                 if(bInStrip == FALSE)
  80.                 {
  81.                     bInStrip = TRUE;
  82.                     if(iLastRectIDX == iRGNDataSize_Rects)
  83.                     {
  84.                         iRGNDataSize_Rects += CPC_RECT_QUANTISE;
  85.                         pRGNData = (RGNDATA*)realloc(pRGNData, sizeof(RGNDATAHEADER) + (iRGNDataSize_Rects * sizeof(RECT)));
  86.                     }
  87.                     ((RECT*)pRGNData->Buffer)[iLastRectIDX].left = iColIDX;
  88.                     ((RECT*)pRGNData->Buffer)[iLastRectIDX].top = iRowIDX;
  89.                     ((RECT*)pRGNData->Buffer)[iLastRectIDX].bottom = iRowIDX+1;
  90.                 }
  91.             }
  92.         }
  93.         if(bInStrip == TRUE)
  94.         {
  95.             ((RECT*)pRGNData->Buffer)[iLastRectIDX].right = bitmap.bmWidth;
  96.             iLastRectIDX++;
  97.         }
  98.     }
  99.     free(pBitmapBits);
  100.     if(bDetectedTransparentPixel == TRUE)
  101.     {
  102.         pRGNData->rdh.dwSize = sizeof(RGNDATAHEADER);
  103.         pRGNData->rdh.iType = RDH_RECTANGLES;
  104.         pRGNData->rdh.nCount = iLastRectIDX;
  105.         pRGNData->rdh.nRgnSize = sizeof(RGNDATAHEADER) + (iLastRectIDX * sizeof(RECT));
  106.         pRGNData->rdh.rcBound.left = 0;
  107.         pRGNData->rdh.rcBound.top = 0;
  108.         pRGNData->rdh.rcBound.right = bitmap.bmWidth;
  109.         pRGNData->rdh.rcBound.bottom = bitmap.bmHeight;
  110.         hRgn = ExtCreateRegion(NULL, pRGNData->rdh.nRgnSize, pRGNData);
  111.     }
  112.     if(pRGNData)
  113.         free(pRGNData);
  114.     return hRgn;
  115. }
  116. HRGN main_bitmap_to_region_1bit(HBITMAP hBmp, COLORREF cTransparentColor)
  117. {
  118.     HRGN hRgn = NULL;
  119.     BYTE* pBitmapBits = NULL;
  120.     BYTE* pBitmapCursor;
  121.     BITMAP bitmap;
  122.     RGNDATA* pRGNData = NULL;
  123.     RECT* pRectArray = NULL;
  124.     int iStride;
  125.     int iLastRectIDX;
  126.     int iRGNDataSize_Rects;
  127.     int iEndofLineCorrection;
  128.     int iRowIDX, iColIDX;
  129.     DWORD dwTransMask;
  130.     BOOL bDetectedTransparentPixel;
  131.     CP_ASSERT(hBmp);
  132.     GetObject(hBmp, sizeof(bitmap), &bitmap);
  133.     iStride = bitmap.bmWidth >> 3;
  134.     if(bitmap.bmWidth & 0x7)
  135.         iStride++;
  136.     if(iStride&0x3)
  137.     {
  138.         iEndofLineCorrection = 0x4 - (iStride&0x3);
  139.         iStride += iEndofLineCorrection;
  140.     }
  141.     else
  142.         iEndofLineCorrection = 0;
  143.     pBitmapBits = (BYTE*)malloc(iStride * bitmap.bmHeight);
  144.     {
  145.         BITMAPINFO* pBMI;
  146.         HDC dc;
  147.         pBMI = malloc(sizeof(BITMAPINFO) + (sizeof(RGBQUAD)<<1));
  148.         memset(pBMI, 0, sizeof(*pBMI));
  149.         pBMI->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  150.         pBMI->bmiHeader.biPlanes = 1;
  151.         pBMI->bmiHeader.biBitCount = 1;
  152.         pBMI->bmiHeader.biCompression = BI_RGB;
  153.         pBMI->bmiHeader.biWidth = bitmap.bmWidth;
  154.         pBMI->bmiHeader.biHeight = -bitmap.bmHeight;
  155.         pBMI->bmiHeader.biSizeImage = iStride * bitmap.bmHeight;
  156.         dc = CreateCompatibleDC(NULL);
  157.         GetDIBits(dc, hBmp, 0, bitmap.bmHeight, pBitmapBits, pBMI, DIB_RGB_COLORS);
  158.         free(pBMI);
  159.         DeleteDC(dc);
  160.     }
  161.     bDetectedTransparentPixel = FALSE;
  162.     pBitmapCursor = pBitmapBits;
  163.     iLastRectIDX = 0;
  164.     iRGNDataSize_Rects = 0;
  165.     dwTransMask = 0x80;
  166.     for(iRowIDX =0; iRowIDX < bitmap.bmHeight; iRowIDX++)
  167.     {
  168.         BOOL bInStrip = FALSE;
  169.         for(iColIDX =0; iColIDX < bitmap.bmWidth; iColIDX++)
  170.         {
  171.             if( (*pBitmapCursor) & dwTransMask)
  172.             {
  173.                 bDetectedTransparentPixel = TRUE;
  174.                 if(bInStrip == TRUE)
  175.                 {
  176.                     bInStrip = FALSE;
  177.                     ((RECT*)pRGNData->Buffer)[iLastRectIDX].right = iColIDX;
  178.                     iLastRectIDX++;
  179.                 }
  180.             }
  181.             else
  182.             {
  183.                 if(bInStrip == FALSE)
  184.                 {
  185.                     bInStrip = TRUE;
  186.                     if(iLastRectIDX == iRGNDataSize_Rects)
  187.                     {
  188.                         iRGNDataSize_Rects += CPC_RECT_QUANTISE;
  189.                         pRGNData = (RGNDATA*)realloc(pRGNData, sizeof(RGNDATAHEADER) + (iRGNDataSize_Rects * sizeof(RECT)));
  190.                     }
  191.                     ((RECT*)pRGNData->Buffer)[iLastRectIDX].left = iColIDX;
  192.                     ((RECT*)pRGNData->Buffer)[iLastRectIDX].top = iRowIDX;
  193.                     ((RECT*)pRGNData->Buffer)[iLastRectIDX].bottom = iRowIDX+1;
  194.                 }
  195.             }
  196.             dwTransMask >>= 1;
  197.             if(!dwTransMask)
  198.             {
  199.                 dwTransMask = 0x80;
  200.                 pBitmapCursor++;
  201.             }
  202.         }
  203.         if(bInStrip == TRUE)
  204.         {
  205.             ((RECT*)pRGNData->Buffer)[iLastRectIDX].right = bitmap.bmWidth;
  206.             iLastRectIDX++;
  207.         }
  208.         if(dwTransMask != 0x80)
  209.             pBitmapCursor++;
  210.         dwTransMask = 0x80;
  211.         pBitmapCursor += iEndofLineCorrection;
  212.     }
  213.     free(pBitmapBits);
  214.     if(bDetectedTransparentPixel == TRUE)
  215.     {
  216.         pRGNData->rdh.dwSize = sizeof(RGNDATAHEADER);
  217.         pRGNData->rdh.iType = RDH_RECTANGLES;
  218.         pRGNData->rdh.nCount = iLastRectIDX;
  219.         pRGNData->rdh.nRgnSize = sizeof(RGNDATAHEADER) + (iLastRectIDX * sizeof(RECT));
  220.         pRGNData->rdh.rcBound.left = 0;
  221.         pRGNData->rdh.rcBound.top = 0;
  222.         pRGNData->rdh.rcBound.right = bitmap.bmWidth;
  223.         pRGNData->rdh.rcBound.bottom = bitmap.bmHeight;
  224.         hRgn = ExtCreateRegion(NULL, pRGNData->rdh.nRgnSize, pRGNData);
  225.     }
  226.     if(pRGNData)
  227.         free(pRGNData);
  228.     return hRgn;
  229. }