A simple and fast gray graph treatment method

xiaoxiao2021-03-05  25

Due to its own procedure, you need to perform color-to-grayscale processing for a form area, which is dedicated to this function. Treatment object is a frequently changing dynamic area, and is part of a series of drawings, high speed requirements, simple in algorithm, so the following two options:

1, based on DDB, although transfer to DIB, it is not necessary to face a variety of color depths, will unify algorithms, but the conversion process will make the speed slowly, and this is just a function of the screen bitmap, there is no saving . Considering the actual situation, I only wrote 16, 24, 32-bit algorithms, in fact, 4, 8 bitmaps are the fastest, no matter how big the picture only needs to process 16 and 256 operations, but Which is the screen now, use these two display modes? I don't think about it. In contrast, the fastest in 32 bits, the slowest hours, the heart is a bit unsatisfactory, but it is not slow at speed. The gap is no more than 50%.

2, the grayscale algorithm is not complicated, but I still make a simplification. When processing, I usually need to weigh the RGB to weigh the average. Take a value to unify the three-base color, but it needs to be floating point operation, the speed is not The effect is not too good. My method is very simple, just take the value of one of the three base colors, unity, consider the most sensitive to the green eye, so the algorithm is RGB to GGG. Strictly speaking, this is not called color to turn the age, and the green is more appropriate. RGB's arrangement G is in the middle, wants to use high-speed long operation, using B value is the fastest, but it is already simplified, and then it is too much. (Under 32 in B value, speed can be faster 1/3) This algorithm has a defect, mainly for some bias maps, but it does not exist in this situation in the colorful interface.

C2.4G 256M WinXP SP2 Test Situation Ide Environment 1024 x 768 Object Chart 32 Bit Screen 219 millisecond 16-bit screen 314 ms

N code compiled, all optimized open 1024 x 768 bitmap 32-bit screen 62 millisecond 16-bit screen 75 milliseconds

Note: There is no 24-bit environment, so it has not been measured.

Option ExplicitPrivate Type BITMAP bmType As Long bmWidth As Long bmHeight As Long bmWidthBytes As Long bmPlanes As Integer bmBitsPixel As Integer bmBits As LongEnd TypePrivate Type MemHdc hdc As Long Bmp As Long obm As LongEnd TypePrivate Declare Function GetObj Lib "gdi32" Alias ​​"GetObjectA" ( ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As LongPrivate Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As LongPrivate Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As LongPrivate Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As LongPrivate 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 asse line CompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As LongPrivate Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As LongPrivate 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 LongPrivate Declare Sub CopyMemory Lib "kernel32" Alias ​​"RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long) 'usual graphics often do, their two common functions Just use Private Function NewmyHDC (DHDC As Long, W As Long, H AS Long, Optional BM As "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 wirth functionprivate 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 welhend function

'Grayscale handler 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 Dtem DBITS () AS BYTE DIM DBITS1 () AS BYTE DIM DBITS1 () AS BYTE DBITS2 () AS Long On Error Goto Last with TMPDC TMPDC = NewmyHDC (DHDC, W, h) getobj .bmp, len (bm), bm if bm.bmpixel <16 Then Goto Last Bitblt .hdc, 0, 0, w, h, dhdc, x, y, vbsrcopy 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' Use B value NEXT SETSTMAPBITS .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 w * 3 - 1 Step 3 DBITS (i) = DBITS (i 2) = DBITS (i) Next Next setBitMapbits .BMP, Allbytes, DBITS (0) graybmp = 24 case 16 'Press 565 Format 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 'This sentence is no way, do not use CopyMemory, will overflow, low efficiency thereto Next Next SetBitmapBits .Bmp, AllBytes, dBits1 (0) GrayBmp = 16 End Select BitBlt dHdc, x, y, w, h, .hdc, 0, 0, vbSrcCopy End Withlast: DelMyHdc tmpdcEnd FunctionPrivate Sub Form_Load () ScaleMode = 3 AutoRedraw = True Picture = LoadPicture ("f: /1.jpg") Command1.caption = "Test" End Sub 'Test Code Private Sub Form_Resize () PaintPicture Picture, 0, 0, Scalewidth, ScaleHeightend Sub

Private Sub Command1_Click () Dim T, S1 AS String, I as long t = gettickcount graybmp HDC, 0, 0, Scalewidth, ScaleHeight Refresh Msgbox GettickCount - T & send Sub

转载请注明原文地址:https://www.9cbs.com/read-35841.html

New Post(0)