Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
'from here: http://www.pvladov.com/2012/09/make-color-lighter-or-darker.html
#Compile Exe
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Resource Bitmap cowgirl "cowgirl.bmp"
Global hDlg, hBMP As Dword
Global contrast As Single, brightness As Long
%IDC_Graphic = 500
%IDC_GraphicB = 501
%IDC_Contrast = 502
%IDC_Brightness = 503
%IDC_Reset = 504
%IDC_GrayScale = 505
Function PBMain() As Long
Dialog New Pixels, 0, "GrayScale-Contrast-Brightness",600,300,375,150, %WS_SysMenu, 0 To hDlg
Control Add Button, hDlg, %IDC_GrayScale, "Grayscale", 10,10,80,20
Control Add Button, hDlg, %IDC_Brightness, "Brightness", 110,10,80,20
Control Add Button, hDlg, %IDC_Contrast, "Contrast", 210,10,80,20
Control Add Button, hDlg, %IDC_Reset, "Reset", 240,50,70,20
Control Add Graphic, hDlg, %IDC_Graphic,"", 10,40,100,100, %WS_Border
Control Add Graphic, hDlg, %IDC_GraphicB,"", 130,40,100,100, %WS_Border
ResetImage
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Select Case Cb.Msg
Case %WM_Command
Select Case Cb.Ctl
Case %IDC_GrayScale
GrayScaleOnly
Case %IDC_Contrast
Contrast += 0.1
ContrastANDBrightness
Case %IDC_Brightness
Brightness += 10
ContrastANDBrightness
Case %IDC_Reset
ResetImage
End Select
End Select
End Function
Sub ResetImage
Graphic Attach hDlg, %IDC_GraphicB : Graphic Color %Black,%White : Graphic Clear
Graphic Attach hDlg, %IDC_Graphic : Graphic Color %Black,%White : Graphic Clear
Graphic Render "cowgirl", (0,0)-(99,99)
Brightness = 0 : Contrast = 1.0
End Sub
Sub ContrastANDBrightness
Local w,h,i,iColor,R,G,B As Long, p As Long Ptr, bmp$
'get the string from ID_Graphic1
Graphic Attach hDlg, %IDC_Graphic
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
G = (iColor\256) Mod 256
R = (iColor\256\256) Mod 256
R = Contrast*(R-128) + 128 + brightness
G = Contrast*(G-128) + 128 + brightness
B = Contrast*(B-128) + 128 + brightness
R = Max(0,Min(R,255))
G = Max(0,Min(G,255))
B = Max(0,Min(B,255))
@p = Bgr(r,g,b)
Incr p
Next i
'put the modified string into ID_Graphic2
Graphic Attach hDlg, %IDC_GraphicB : Graphic Set Bits bmp$
End Sub
Sub GrayScaleOnly
Local w,h,i,iColor,R,G,B As Long, p As Long Ptr, bmp$
'get the string from ID_Graphic1
Graphic Attach hDlg, %IDC_Graphic
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&
'grayscale
iColor = 0.299*R + 0.587*G + 0.114*B
@p = Bgr(iColor, iColor, iColor)
Incr p
Next i
'put the modified string into ID_Graphic2
Graphic Attach hDlg, %IDC_Graphic : Graphic Set Bits bmp$
End Sub
http://www.garybeene.com/sw/gbsnippets.htm