因自己的程序中需對一個窗體區域頻繁進行彩色轉灰度處理,為此專門寫了個函數。處理對象是一塊經常變化的動態區域,且是一系列繪圖中的一部分,速度要求較高,算法上力求簡單,所以采用以下兩步方案:
1、基于ddb來寫,雖然轉入dib,可不必面對各種色深,會統一算法,但轉換過程會讓速度上慢很多,再者這只是針對屏幕位圖的函數,并無保存需要。
考慮實際情況,我只寫了16、24、32位三種色深下的算法,其實4、8兩種位圖是最快的了,不管多大的圖只需處理16與256次運算,可是現在哪有人的屏幕,還使用這兩種顯示模式呢?想想就沒這個必要了。
相比之下,32位時最快,16位時最慢,心里有點不滿意,但好在速度都不慢。差距也不超過50%。
2、灰度算法本來就不復雜,但我還是做了簡化,正常處理時一般需對rgb做加權平均,取個值來統一三基色,但這需涉及浮點運算,速度上不去,效果卻不見得有多好。
我的方法很簡單,就是取三基色之一的值,統一起來,考慮人眼對綠色最敏感,所以算法就成rgb轉ggg了。嚴格的說,這不叫彩轉灰,叫綠轉灰更合適。rgb的排列g是在中間的,想利用高速long運算,用b值最快的,但已經夠簡化了,再簡下去,自己都過意不去。(用b值時32位下,速度還可快1/3)
這種算法當然有缺陷,主要是對一些偏色圖效果不好,但好在這種情況在色彩豐富的界面中不存在。
c2.4g 256m winxp sp2下的測試情況
ide環境下
1024 x 768的位圖
32位屏幕 219毫秒
16位屏幕 314毫秒
n代碼編譯,全部優化打開
1024 x 768的位圖
32位屏幕 62毫秒
16位屏幕 75毫秒
注:沒有24位環境,所以也就沒測了
option explicit
private type bitmap
bmtype as long
bmwidth as long
bmheight as long
bmwidthbytes as long
bmplanes as integer
bmbitspixel as integer
bmbits as long
end type
private type memhdc
hdc as long
bmp as long
obm as long
end type
private declare function getobj lib "gdi32" alias "getobjecta" (byval hobject as long, byval ncount as long, lpobject as any) as long
private declare function selectobject lib "gdi32" (byval hdc as long, byval hobject as long) as long
private declare function deleteobject lib "gdi32" (byval hobject as long) as long
private declare function deletedc lib "gdi32" (byval hdc as long) as long
private declare function bitblt lib "gdi32" (byval hdestdc as long, byval x as long, byval y as long, byval nwidth as long, byval nheight as long, byval hsrcdc as long, byval xsrc as long, byval ysrc as long, byval dwrop as long) as long
private declare function createcompatibledc lib "gdi32" (byval hdc as long) as long
private declare function createcompatiblebitmap lib "gdi32" (byval hdc as long, byval nwidth as long, byval nheight as long) as long
private declare function getbitmapbits lib "gdi32" (byval hbitmap as long, byval dwcount as long, lpbits as any) as long
private declare function setbitmapbits lib "gdi32" (byval hbitmap as long, byval dwcount as long, lpbits as any) as long
private declare function gettickcount lib "kernel32" () as long
private declare sub copymemory lib "kernel32" alias "rtlmovememory" (pdest as any, psource as any, byval dwlength as long)
'平時常做圖形處理,自己的兩個公用函數也就用上了
private function newmyhdc(dhdc as long, w as long, h as long, optional bm as long) as memhdc
with newmyhdc
.hdc = createcompatibledc(dhdc)
if bm = 0 then
.bmp = createcompatiblebitmap(dhdc, w, h)
else
.bmp = bm
end if
.obm = selectobject(.hdc, .bmp)
end with
end function
private function delmyhdc(myhdc as memhdc, optional nobmp as boolean) as memhdc
with myhdc
if .hdc <> 0 and .obm <> 0 then selectobject .hdc, .obm
if nobmp = false and .bmp <> 0 then deleteobject .bmp
if .hdc <> 0 then deletedc .hdc
end with
end function
'灰度處理主函數
private function graybmp(dhdc as long, x as long, y as long, w as long, h as long) as long
dim tmpdc as memhdc
dim i as long, j as long, m as long, k as byte, l as long
dim bm as bitmap, allbytes as long, linebytes as long
dim dbits() as byte
dim dbits1() as integer
dim dbits2() as long
on error goto last
with tmpdc
tmpdc = newmyhdc(dhdc, w, h)
getobj .bmp, len(bm), bm
if bm.bmbitspixel < 16 then goto last
bitblt .hdc, 0, 0, w, h, dhdc, x, y, vbsrccopy
linebytes = bm.bmwidthbytes
allbytes = linebytes * h
select case bm.bmbitspixel
case 32
redim dbits2(allbytes / 4 - 1)
getbitmapbits .bmp, allbytes, dbits2(0)
for i = 0 to allbytes / 4 - 1
dbits2(i) = ((dbits2(i) and &hff00&) / &h100) * &h10101
'dbits2(i) = (dbits2(i) and &hff) * &h10101'用b值運算
next
setbitmapbits .bmp, allbytes, dbits2(0)
graybmp = 32
case 24
redim dbits(allbytes - 1)
getbitmapbits .bmp, allbytes, dbits(0)
for j = 0 to h - 1
m = j * linebytes
for i = m to m + w * 3 - 1 step 3
dbits(i) = dbits(i + 1)
dbits(i + 2) = dbits(i)
next
next
setbitmapbits .bmp, allbytes, dbits(0)
graybmp = 24
case 16
'按565格式運算
redim dbits1(allbytes / 2 - 1)
getbitmapbits .bmp, allbytes, dbits1(0)
for j = 0 to h - 1
m = j * linebytes / 2
for i = m to m + w - 1
l = dbits1(i) and &h7c0&
l = l * 32 + l + l / 64
copymemory dbits1(i), l, 2 '這句沒辦法,不用copymemory,會溢出,低效源于此
next
next
setbitmapbits .bmp, allbytes, dbits1(0)
graybmp = 16
end select
bitblt dhdc, x, y, w, h, .hdc, 0, 0, vbsrccopy
end with
last:
delmyhdc tmpdc
end function
private sub form_load()
scalemode = 3
autoredraw = true
picture = loadpicture("f:/1.jpg")
command1.caption = "測試"
end sub
'測試用代碼
private sub form_resize()
paintpicture picture, 0, 0, scalewidth, scaleheight
end sub
private sub command1_click()
dim t as long, s as string, s1 as string, i as long
t = gettickcount
graybmp hdc, 0, 0, scalewidth, scaleheight
refresh
msgbox gettickcount - t & s
end sub