因自己的程序中需对一个窗体区域频繁进行彩色转灰度处理,为此专门写了个函数。处理对象是一块经常变化的动态区域,且是一系列绘图中的一部分,速度要求较高,算法上力求简单,所以采用以下两步方案:
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