setpixel,getpixel效率太低的問題

2021-05-03 22:38:28 字數 3295 閱讀 1522

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); ­

企業商機轉化效率太低?AI來破題!

傳統聯絡成企業商機轉化 絆腳石 傳統聯絡中心存在的 低效率 和 高成本 問題一直制約著企業商機轉化的效率問題,成為企業商機轉化路上的 絆腳石 因此,高效率 和 低成本 成為企業聯絡中心領域的破題關鍵。在企業主動聯絡業務場景中,當人工座席面對海量的客戶線索 高強度的業務壓力 重複的工作內容時,員工成本...

你擔心handlerThread的效率問題嗎?

handlerthread handler looper messagequeue相互關聯對應 queue是通過msg.next指向下乙個msg來實現,mmessages代表當前的message,每取出乙個msg,移動一次mmessages,一直到最後乙個msg handlerthread是乙個執行...

乙個effective java中的效率問題

package com.liuc public class autopackage long end system.currenttimemillis system.out.println end start 1000 執行時間19s 和下面這個程式 package com.liuc public ...