Date: 02-16-2022
Return to Index
created by gbSnippets
'While the Graphic Get/Set Bits Help topic says you can use
'CVL/MKL$ or pointers to work with the bit string, the example
'uses a combination of the two.
'This snippet shows how to work with the bit string using using only CVL/MKL$ and another
'using only pointers.
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Resource "gbsnippets.pbr"
Global hDlg As DWord, hBMP As DWord
%ID_Graphic1 = 500
%ID_Graphic2 = 501
Function PBMain() As Long
Dialog New Pixels, 0, "Graphic Get/Set Bits Test",600,300,180,325, %WS_SysMenu, 0 To hDlg
Control Add Button, hDlg, 100, "Mod Using CVL/MKL$", 10,10,150,20
Control Add Button, hDlg, 200, "Mod Using Pointers", 10,40,150,20
Control Add Button, hDlg, 300, "Mod w/Mixed Approach", 10,70,150,20
Control Add Graphic, hDlg, %ID_Graphic1,"", 10,100,100,100, %WS_Visible ' Or %SS_Sunken
Control Add Graphic, hDlg, %ID_Graphic2,"", 10,220,100,100, %WS_Visible 'Or %SS_Sunken
Graphic Attach hDlg, %ID_Graphic1 : Graphic Color %Black,%White : Graphic Clear
Graphic Render "cowgirl", (0,0)-(100,100)
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local bmp$
Select Case CB.Msg
Case %WM_Command
Select Case CB.Ctl
Case 100 : ModifyImage1
Case 200 : ModifyImage2
Case 300
'get the string from ID_Graphic1
Graphic Attach hDlg, %ID_Graphic1
Graphic Get Bits To bmp$
ModifyImage3(0,0,9,9,bmp$)
ModifyImage3(90,0,99,9,bmp$)
ModifyImage3(90,90,99,99,bmp$)
ModifyImage3(0,90,9,99,bmp$)
ModifyImage3(20,20,30,60,bmp$)
ModifyImage3(40,40,50,60,bmp$)
'put the modified string into ID_Graphic2
Graphic Attach hDlg, %ID_Graphic2
Graphic Set Bits bmp$
End Select
End Select
End Function
Sub ModifyImage1 'no pointers
Local bmp$, i as Long, p as Long PTR, w as Long, h as Long
'get the string from ID_Graphic1
Graphic Attach hDlg, %ID_Graphic1
Graphic Get Bits To bmp$
'get width/height
w = CVL(bmp$,1)
h = CVL(bmp$,5)
'PixelN = CVL(bmp$, n*4+8)
'modify the string - change every 20th pixel to blue
For i = 9 To w * h * 4 + 8 Step 4
If (i-1) Mod 80 = 0 Then Mid$(bmp$,i) = Mkl$(Bgr(%Blue))
Next
'put the modified string into ID_Graphic2
Graphic Attach hDlg, %ID_Graphic2
Graphic Set Bits bmp$
End Sub
Sub ModifyImage2 'uses pointers
Local bmp$, i as Long, p as Long PTR, w as Long, h as Long
'get the string from ID_Graphic1
Graphic Attach hDlg, %ID_Graphic1
Graphic Get Bits To bmp$
'get width/height
p = StrPTR(bmp$)
w = @p
Incr p
h = @p
'modify the string - change every 20th pixel to red
Incr p
For i = 1 TO w * h
If i Mod 20 = 0 Then @p = Bgr(%red)
Incr p
Next
'put the modified string into ID_Graphic2
Graphic Attach hDlg, %ID_Graphic2
Graphic Set Bits bmp$
End Sub
Sub ModifyImage3(x&,y&,xx&,yy&,bmp$) 'uses mixed pointer/CVL solution
Local w as Long, h as Long, p as Long PTR, pOld&, i as Long, j as long
'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
pOld& = p 'save pointer starting position to use later
'get string position of coordinates and modify the string at that position
For i = x& to xx&
For j = y& to yy&
p = pOld& + (j*w+i)*4 'starting position of 4-byte Long in bit string bmp$
@p = Bgr(%red) 'modify string at that position
Next j
Next i
End Sub
'gbs_00438
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm