Date: 02-16-2022
Return to Index
created by gbSnippets
'... this snippet is in work
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
Type gbPoint
x As Long 'new x position
y As Long 'new y position
col As Long 'color of pixel
End Type
%IDC_Graphic = 501
%IDC_Label = 502
%IDC_Button0 = 503
%IDC_Button1 = 504
%IDC_Button2 = 505
%IDC_Button3 = 506
Global hDlg As Dword, theta As Single
Function PBMain() As Long
Dialog New Pixels, 0, "Rotate Image",300,300,375,300, %WS_SysMenu, 0 To hDlg
Control Add Label, hDlg, %IDC_Label, "<timer>", 10,10,125,20
Control Add Button, hDlg, %IDC_Button0,"Reset", 10,40,50,20
Control Add Button, hDlg, %IDC_Button1,"1", 10,70,50,20
Control Add Button, hDlg, %IDC_Button2,"2", 10,100,50,20
Control Add Graphic, hDlg, %IDC_Graphic,"", 80,10,200,200, %WS_Border
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local T as Quad, i As Long
Select Case Cb.Msg
Case %WM_InitDialog
theta = 0.1
Case %WM_MouseWheel
Select Case Hi(Integer,Cb.WParam) 'note the use of Integer
Case > 0 : theta = theta + 0.2 : RotateImage_GetPixelB
Case < 0 : theta = theta - 0.2 : RotateImage_GetPixelB
End Select
Case %WM_Command
Select Case Cb.Ctl
Case %IDC_Button0 : Graphic Attach hDlg, %IDC_Graphic : Graphic Clear
Graphic Render "cowgirl", (50,50)-(149,149) 'same size (could resize)
Case %IDC_Button1 : Tix T : RotateImage_GetPixelA : Tix End T : Control Set Text hDlg, %IDC_Label, Format$(T, "###,###,###")
' Case %IDC_Button2 : Tix T : RotateImage_GetPixelB : Tix End T : Control Set Text hDlg, %IDC_Label, Format$(T, "###,###,###")
' Case %IDC_Button3 : Tix T : RotateImage_GetPixelC : Tix End T : Control Set Text hDlg, %IDC_Label, Format$(T, "###,###,###")
End Select
End Select
End Function
Sub RotateImage_GetPixelA 'slower
Local c,i,j,w,h,x,y,XCenter,YCenter, bW, bH, oldX, oldY As Long
Dim D(200,200) As gbPoint
theta = 0.5 : XCenter = 100 : YCenter = 100
'capture source image into array D()
Graphic Attach hDlg, %IDC_Graphic, Redraw
For x = 50 to 149
For y = 50 To 149
Graphic Get Pixel (x,y) To D(x,y).col
Next y
Next x
Graphic Clear
bWX = 100*ABS(cos(theta)) + Y*ABS(sin(theta))
bH = 100*ABS(cos(theta)) + X*ABS(sin(theta))
For (200-bx)/2 To bX
For (200-by)/2 To bY
oldx = XCenter + (x - XCenter) * Cos(-theta) - (y - YCenter) * Sin(-theta)
oldy = YCenter + (x - XCenter) * Sin(-theta) + (y - YCenter) * Cos(-theta)
Graphic Set Pixel (D(x,y).x,D(x,y).y), D(x,y).Col
Next y
Next x
Graphic Redraw
End Sub
'Compilable Example:
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Resource "gbsnippets.pbr"
Global hDlg as Dword
Global BMPData() As Long
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Push", 10,10,100,20
Control Add Graphic hDlg, %IDC_Graphic, 10,30,100,100
Graphic Attach hDlg, %IDC_Graphic, Redraw
Graphic Render "cowgirl", (50,50)-(149,149)
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
End If
End Function
Sub A()
'Top to bottom BGR
Local i,j,iCount As Long, PixelPTR, OrigPTR As Long PTR, bmp$
Graphic Get Bits To bmp$
PixelPTR = StrPtr$(bmp$)+ 8
OrigPTR = PixelPTR
imgW = CVL(bmp$,1) : imgH = CVL(bmp$,5)
For x = 0 to imgW-1
For y = 0 to imgH-1
'Get Pixel
Graphic Get Pixel (x,y) To BMPData(x,y) 'Get Pixel
'Bit String - use CVL
iPos = (y*imgW+x)*4 + 8 'position in string calculated each time
BMPData(x,y).Col = CVL(bmp$, iPos) 'CVL nth position calculated each time
BMPData(x,y).Col = CVL(bmp$, (iCount) : iCount = iCount + 4 'CVL nth position incrementally tracked
'Pointer - 3 ways to keep pointer in position
iPos = (y*imgW+x)*4 + 8 'position in string calculated each time
PixelPTR = OrigPTR + iPos 'use iPos
BMPData(x,y) = @PixelPTR
PixelPTR = PixelPTR + 4 'increment pointer by 4 bytes each time (must be done after first array assignment)
Incr PixelPTR 'increment pointer by 4 bytes each time (must be done after first array assignment)
Next y
Next x
End Sub
'Top Down
For i = 0 to imgW-1
For j = 0 to imgH-1
Graphic Get Pixel (i,j) To BMPData(i,j)
Next j
Next i
'Bottom Up
For i = 0 to imgW-1
For j = 0 to imgH-1
Graphic Get Pixel (i,j) To BMPData(i,imgH-1-j)
Next j
Next i
'OpenGL - give bit data in array
glTexImage2D %gl_texture_2D, 0, %gl_rgb, 100, 100, 0, %gl_rgba, %gl_unsigned_byte, BMPData(0)
'OpenGL - give bit data in string
glTexImage2D %gl_texture_2D, 0, %gl_rgb, 100, 100, 0, %gl_rgba, %gl_unsigned_byte, ByVal STRPTR(strTextureData)
'gbs_00932
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm