Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
'Each of the 3 methods above are demonstrated in this example. Also a variation
'on Graphic Get/Set Bits is shown which uses BYTE instead of Long pointers
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As DWord, hBMP As DWord
Enum Equates Singular
IDC_Long = 500
IDC_Byte
IDC_Original
IDC_Converted
End Enum
Global hDlg
Function PBMain() As Long
Dialog Default Font "Tahoma",12,1
Dialog New Pixels, 0, "Alt Color Pair Test",600,300,180,350, %WS_SysMenu, 0 To hDlg
Control Add Button, hDlg, %IDC_Long, "Long", 10,10,50,20
' Control Add Graphic, hDlg, %ID_Graphic1,"", 10,130,100,100, %WS_Visible ' Or %SS_Sunken
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
If CB.Msg = %WM_Command AND CB.Ctl = 100 Then GrayScale1
If CB.Msg = %WM_Command AND CB.Ctl = 200 Then GrayScale2
If CB.Msg = %WM_Command AND CB.Ctl = 300 Then GrayScale3
If CB.Msg = %WM_Command AND CB.Ctl = 400 Then GrayScale4
If CB.Msg = %WM_Command AND CB.Ctl = 800 Then
Local hbmp as DWord
Control Handle hdlg, %ID_Graphic1 to hbmp
bmp_grayscale hbmp
End If
If CB.Msg = %WM_Command AND CB.Ctl = 600 Then
Graphic Attach hDlg, %ID_Graphic2
Graphic Clear
End If
End Function
Sub GrayScale4 'uses BYTE pointer/CVL solution
Local w As Long, h As Long, bp As Byte Ptr, i As Long, p As Long PTR
Local iColor As Long, R As Long, G As Long, B As Long, bmp$
'get the string from ID_Graphic1
Graphic Attach hDlg, %ID_Graphic1
Graphic Get Bits To bmp$
'get width/height of image
w = CVL(bmp$,1)
h = CVL(bmp$,5)
bp = StrPTR(bmp$)+8
p = bp
'get string position of coordinates and modify the string at that position
For i = 1 to w*h
B = @bp 'string BGR bytes positions are 0-R-G-B
Incr bp : G = @bp
Incr bp : R = @bp
Incr bp : Incr bp
iColor = 0.299*R + 0.587*G + 0.114*B 'create gray component
If iColor <= BWTrigger Then @p = Bgr(TextColor) Else @p = Bgr(BGColorRE)
Incr p
Next i
'put the modified string into ID_Graphic2
Graphic Attach hDlg, %ID_Graphic2
Graphic Set Bits bmp$
End Sub
Sub ConvertToBW
Local w,h,i,iColor,R,G,B As Long, p As Long Ptr, bmp$
Graphic Get Bits To bmp$
'get width/height of image
w = Cvl(bmp$,1)
h = Cvl(bmp$,5)
p = StrPtr(bmp$)+8 'position of starting position for bits in string
'get string position of coordinates and modify the string at that position
For i = 1 To w*h
iColor = @p 'result is a BGR color value 0-R-G-B
B = iColor Mod 256 'or this: iColor AND &HFF&
G = (iColor\256) Mod 256 'or this: (iColor AND &HFF00&) \ &H100
R = (iColor\256\256) Mod 256 'or this: (iColor AND &HFF0000&) \ &H10000&
iColor = 0.299*R + 0.587*G + 0.114*B 'or this: iColor = (R+G+B)/3
If iColor <= BWTrigger Then @p = Bgr(TextColor) Else @p = Bgr(BGColorRE)
Incr p
Next i
Graphic Set Bits bmp$
Graphic ReDraw
End Sub
http://www.garybeene.com/sw/gbsnippets.htm