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"
#Include "cgdiplus.inc"
#Resource Bitmap woman "woman.bmp"
Global hDlg As Dword, hBMP,hJPG As Dword
Global ImageW, ImageH As Long
%ID_Graphic1 = 500
%ID_Graphic2 = 501
%ID_Graphic3 = 502
%ID_Graphic4 = 503
%ID_Graphic5 = 504
%ID_Graphic6 = 505
Function PBMain() As Long
Dialog New Pixels, 0, "Lighten+Grayscale Image",600,300,375,300, %WS_SysMenu, 0 To hDlg
Control Add Button, hDlg, 100, "Lighten+Grayscale", 10,10,150,20
Control Add Graphic, hDlg, %ID_Graphic1,"", 10,40,100,100, %WS_Visible Or %WS_Border
Control Add Graphic, hDlg, %ID_Graphic2,"", 130,40,100,100, %WS_Visible Or %WS_Border
Control Add Graphic, hDlg, %ID_Graphic3,"", 250,40,100,100, %WS_Visible Or %WS_Border
Control Add Graphic, hDlg, %ID_Graphic4,"", 10,160,100,100, %WS_Visible Or %WS_Border
Control Add Graphic, hDlg, %ID_Graphic5,"", 130,160,100,100, %WS_Visible Or %WS_Border
Control Add Graphic, hDlg, %ID_Graphic6,"", 250,160,100,100, %WS_Visible Or %WS_Border
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Select Case Cb.Msg
Case %WM_InitDialog
Graphic Attach hDlg, %ID_Graphic1 : Graphic Color %Black,%White : Graphic Clear
Graphic Render "woman", (0,0)-(99,99)
LoadImageToMemoryBitmap
Case %WM_Command
If Cb.Ctl = 100 Then
Graphic Attach hDlg, %ID_Graphic1
GrayScale 1
Graphic Attach hDlg, %ID_Graphic4
GrayScale 2
End If
End Select
End Function
Sub GrayScale(Flag As Long) 'uses Long pointer/CVL solution
Local w As Long, h As Long, p,p2 As Long Ptr, i As Long
Local iColor As Long, R,G,B As Single, bmp$, bmp2$
Local correctionfactor As Single
'get the string from ID_Graphic1
Graphic Get Bits To bmp$
'get width/height of image
w = Cvl(bmp$,1)
h = Cvl(bmp$,5)
bmp2$ = bmp$
p = StrPtr(bmp$)+8 'position of starting position for bits in string
p2 = StrPtr(bmp2$)+8 'position of starting position for bits in string
'get string position of coordinates and modify the string at that position
correctionfactor = 0.85
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&
' 'lighten/darken
If (correctionFactor < 0) Then
correctionFactor = 1 + correctionFactor
R *= correctionFactor
G *= correctionFactor
B *= correctionFactor
Else
R = (255 - R) * correctionFactor + R
G = (255 - G) * correctionFactor + G
B = (255 - B) * correctionFactor + B
End If
@p = Bgr(R,G,B) 'modify string at that position
'grayscale
iColor = 0.299*R + 0.587*G + 0.114*B
@p2 = Bgr(iColor, iColor, iColor)
Incr p : Incr p2
Next i
If Flag = 1 Then
Graphic Attach hDlg, %ID_Graphic2 : Graphic Set Bits bmp$
Graphic Attach hDlg, %ID_Graphic3 : Graphic Set Bits bmp2$
Else
Graphic Attach hDlg, %ID_Graphic5 : Graphic Set Bits bmp$
Graphic Attach hDlg, %ID_Graphic6 : Graphic Set Bits bmp2$
End If
End Sub
Sub LoadImageToMemoryBitmap
Local w,h As Long
Local ImgName As String
Local hWin As Dword
Local hDC As Dword
Local pGraphics,pImage,token As Dword, StartupInput As GdiplusStartupInput
'initialize GDI
StartupInput.GdiplusVersion = 1 'initialize GDIPlus
GdiplusStartup(token, StartupInput, ByVal %NULL) 'initialize GDIPlus
ImgName = "woman.jpg"
GdipLoadImageFromFile((ImgName), pImage) 'load image
GdipGetImageWidth(pImage,w) 'get width
GdipGetImageHeight(pImage, h) 'get height
Graphic Bitmap New w,h To hWin
Graphic Attach hWin, 0
Graphic Get DC To hDC
GdipCreateFromHDC(hDC, pGraphics) ' Create the Graphic object
GdipDrawImage(pGraphics, pImage, 0, 0) ' Draw the image
'shutdown GDI
If pImage Then GdipDisposeImage(pImage) 'cleanup
If pGraphics Then GdipDeleteGraphics(pGraphics) 'cleanup
GdiplusShutdown token 'shutdown GDI+
Graphic Attach hDlg, %ID_Graphic4
Graphic Stretch hWin, 0, (0,0)-(w-1,h-1) To (0,0)-(99,99) ,%Mix_CopySrc, %HalfTone
End Sub
'gbs_01406
'Date: 10-17-2014
http://www.garybeene.com/sw/gbsnippets.htm