setpixel,getpixel效率太低的问题

来源:互联网 发布:mac 重做系统 编辑:程序博客网 时间:2024/04/27 13:42

setpixel,getpixel效率很低,尤其是在循环里面去这么做,速度慢的无法忍受,­

用bitblt做了优化,代码如下,­

原函数如下:­

void gdiRectangleAlpha(HDC hdc,const RECT *rect,COLORREF color, unsigned char alpha)­

if (!rect || !hdc)­

  return;­

int xMin = rect->left;­

int yMin = rect->top;­

int xMax = rect->right;­

int yMax = rect->bottom;­

int x,y;­

­

int r = GetRValue(color);­

int g = GetGValue(color);­

int b = GetBValue(color);­

COLORREF clSrc;­

int rSrc;­

int gSrc;­

int bSrc;­

­

for (y = yMin; y < yMax; y++)­

  for (x = xMin; x < xMax; x++)­

  {­

   clSrc = GetPixel(hdc,x,y);­

   rSrc = GetRValue(clSrc);­

   gSrc = GetGValue(clSrc);­

   bSrc = GetBValue(clSrc);­

   rSrc = (rSrc * alpha + r * (255 - alpha)) >>8;­

   gSrc = (gSrc * alpha + g * (255 - alpha)) >>8;­

   bSrc = (bSrc * alpha + b * (255 - alpha)) >>8;­

   SetPixel(hdc,x,y,RGB(rSrc,gSrc,bSrc));­

  }­

} ­

­

优化后的实现­

­

void gdiRectangleAlpha(HDC hdc,const RECT *rect,COLORREF color, unsigned char alpha)­

BYTE * g_pBits;­

HDC g_hMemDC;­

    HBITMAP g_hBmp, g_hOldBmp;­

if (!rect || !hdc)­

  return;­

­

int xMin = rect->left;­

int yMin = rect->top;­

int xMax = rect->right;­

int yMax = rect->bottom;­

int x,y;­

­

byte r = GetRValue(color);­

byte g = GetGValue(color);­

byte b = GetBValue(color);­

­

COLORREF clSrc;­

­

unsigned char   rSrc;­

unsigned char gSrc;­

unsigned char   bSrc;­

­

g_hMemDC = ::CreateCompatibleDC(hdc);­

    if (!g_hMemDC)­

    {­

        ::DeleteDC(hdc);­

    }­

­

int iWidth = rect->right - rect->left;­

int iHeight = rect->bottom - rect->top;­

­

    BYTE bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];­

    memset(bmibuf, 0, sizeof(bmibuf));­

    BITMAPINFO* pbmi = (BITMAPINFO*)bmibuf;­

// BITMAPINFO pbmi;­

    pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);­

    pbmi->bmiHeader.biWidth = iWidth;­

    pbmi->bmiHeader.biHeight = iHeight;­

    pbmi->bmiHeader.biPlanes = 1;­

­

    pbmi->bmiHeader.biBitCount = 24;­

    pbmi->bmiHeader.biCompression = BI_RGB;­

­

    g_hBmp = ::CreateDIBSection(g_hMemDC, pbmi, DIB_RGB_COLORS, (void **)&g_pBits, 0, 0);­

    if (!g_hBmp)­

    {­

        ::DeleteDC(g_hMemDC);­

    }­

­

    g_hOldBmp = (HBITMAP)::SelectObject(g_hMemDC, g_hBmp);­

­

BitBlt(g_hMemDC,0,0,iWidth,iHeight,hdc,0,0,SRCCOPY);­

­

// offset = y * (width * 24 / 8) + x * (24 / 8)­

for (y = 0; y < iHeight; y++)­

  for (x = 0; x < iWidth; x++)­

  {­

   rSrc = g_pBits[y * iWidth * 3  + x * 3 + 2];­

   gSrc = g_pBits[y * iWidth * 3  + x * 3 + 1];­

   bSrc = g_pBits[y * iWidth * 3  + x * 3];­

   ­

   rSrc = (rSrc * alpha + r * (255 - alpha)) >>8;­

   gSrc = (gSrc * alpha + g * (255 - alpha)) >>8;­

   bSrc = (bSrc * alpha + b * (255 - alpha)) >>8;­

   g_pBits[y * iWidth * 3  + x * 3 + 2] = rSrc;­

   g_pBits[y * iWidth * 3  + x * 3 + 1] = gSrc;­

   g_pBits[y * iWidth * 3  + x * 3]     = bSrc;­

  }­

BitBlt(hdc, 0, 0, iWidth, iHeight, g_hMemDC, 0, 0, SRCCOPY); ­

SelectObject(g_hMemDC, g_hOldBmp); ­

DeleteObject(g_hBmp); ­

DeleteDC(g_hMemDC); ­

ReleaseDC(NULL, hdc); ­

原创粉丝点击