Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Type ImageRow
Pixels(399) As Long
End Type
Type ImageRow2
Pixels(199) As Long
End Type
%IDC_Graphic = 500
%IDC_VertArray = 501 : %IDC_HorzArray = 502
%IDC_VertPointer = 503 : %IDC_HorzPointer = 504
%IDC_VertStretchBlt = 505 : %IDC_HorzStretchBlt = 506
%IDC_VertPointerPeek = 507 : %IDC_HorzPointerPeek = 508
%IDC_VertASM = 509 : %IDC_HorzASM = 510
%IDC_Vert1DArray = 511 : %IDC_Horz1DArray = 512
%IDC_VertRows = 513 : %IDC_HorzRows = 514
%IDC_VertMemSwap = 515 : %IDC_HorzMemSwap = 516
Global hDlg,hDC As Dword, bmp$, bmp2$, bmp_orig$
Global imgW,imgH,iLoopMax As Long
Global qFreq, qStart, qStop As QUAD, DeltaTime As Single
Global lp,lp_orig,fp As Long Pointer
Function PBMain() As Long
Dialog New Pixels, 0, "Vertical/Horizontal Flip",300,300,450,380, %WS_OverlappedWindow + %CS_HRedraw + %CS_VRedraw To hDlg
Control Add Button, hDlg, %IDC_VertArray, "Vert Array", 10,10,90,20
Control Add Button, hDlg, %IDC_HorzArray, "Horz Array", 110,10,90,20
Control Add Button, hDlg, %IDC_VertPointer, "Vert Pointer", 240,10,90,20
Control Add Button, hDlg, %IDC_HorzPointer, "Horz Pointer", 340,10,90,20
Control Add Button, hDlg, %IDC_VertStretchBlt, "Vert StretchBlt", 10,50,90,20
Control Add Button, hDlg, %IDC_HorzStretchBlt, "Horz StretchBlt", 110,50,90,20
Control Add Button, hDlg, %IDC_VertPointerPeek, "Vert Ptr Peek", 240,50,90,20
Control Add Button, hDlg, %IDC_HorzPointerPeek, "Horz Ptr Peek", 340,50,90,20
Control Add Button, hDlg, %IDC_VertASM, "Vert ASM", 10,90,90,20
Control Add Button, hDlg, %IDC_HorzASM, "Horz ASM", 110,90,90,20
Control Add Button, hDlg, %IDC_Vert1DArray, "Vert 1D Array", 240,90,90,20
Control Add Button, hDlg, %IDC_Horz1DArray, "Horz 1D Array", 340,90,90,20
Control Add Button, hDlg, %IDC_VertRows, "Vert Rows", 10,130,90,20
Control Add Button, hDlg, %IDC_HorzRows, "Horz Rows", 110,130,90,20
Control Add Button, hDlg, %IDC_VertMemSwap, "Vert Mem Swap", 240,130,90,20
Control Add Button, hDlg, %IDC_HorzMemSwap, "Horz Mem Swap", 340,130,90,20
imgW = 400 : imgH = 200 : iLoopMax = 1001
Control Add Graphic, hDlg, %IDC_Graphic, "", 20,170,imgW,imgH
Graphic Attach hDlg, %IDC_Graphic
Graphic Get DC To hDC
Graphic Render "cowboy.bmp", (0,0)-(imgW-1,imgH-1)
Graphic Get Bits To bmp$ : bmp_orig$ = bmp$
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Select Case Cb.Msg
Case %WM_InitDialog
QueryPerformanceFrequency qFreq
Case %WM_Command
Select Case Cb.Ctl
Case %IDC_VertPointer : VertPointer
Case %IDC_HorzPointer : HorzPointer
Case %IDC_VertArray : VertArray
Case %IDC_HorzArray : HorzArray
Case %IDC_VertStretchBlt : VertStretchBlt
Case %IDC_HorzStretchBlt : HortStretchBlt
Case %IDC_VertPointerPeek : VertPointerPeek
Case %IDC_HorzPointerPeek : HortPointerPeek
Case %IDC_VertASM : VertASM
Case %IDC_HorzASM : HorzASM(400,200)
Case %IDC_Vert1DArray : Vert1DArray
Case %IDC_Horz1DArray : Horz1DArray
Case %IDC_VertRows : VertRows
Case %IDC_HorzRows : HorzRows
Case %IDC_VertMemSwap : VertMemSwap
Case %IDC_HorzMemSwap : HorzMemSwap
End Select
End Select
End Function
Sub VertArray
Local x,y,iLoop As Long
QueryPerformanceCounter qStart
For iLoop = 1 To iLoopMax 'test loop
'-----------------------------------------------------------------
ReDim Colors(imgW-1,imgH-1) As Long At StrPtr(bmp$) + 8
For y = 0 To (imgH-1)\2
For x = 0 To imgW-1
Swap Colors(x,y), Colors(x,imgH-1-y)
Next x
Next y
'-----------------------------------------------------------------
Next iLoop
Graphic Set Bits bmp$
QueryPerformanceCounter qStop
DeltaTime = (qStop-qStart)/qFreq
? "Vert_Array " + Format$(DeltaTime,"0.###") & " seconds"
End Sub
Sub HorzArray
Local x,y,iLoop As Long
QueryPerformanceCounter qStart
For iLoop = 1 To iLoopMax 'test loop
'-----------------------------------------------------------------
ReDim Colors(imgW-1,imgH-1) As Long At StrPtr(bmp$) + 8
For y = 0 To imgH-1
For x = 0 To (imgW-1)\2
Swap Colors(x,y), Colors(imgW-1-x,y)
Next x
Next y
'-----------------------------------------------------------------
Next iLoop
Graphic Set Bits bmp$
QueryPerformanceCounter qStop
DeltaTime = (qStop-qStart)/qFreq
? "Horz_Array " + Format$(DeltaTime,"0.###") & " seconds"
End Sub
Sub VertPointer
Local i,iLoop As Long
QueryPerformanceCounter qStart
For iLoop = 1 To iLoopMax 'test loop
'-----------------------------------------------------------------
lp = StrPtr(bmp$)+8
lp_orig = lp
fp = lp + imgW*(imgH-1)*4 '1st pixel in last row
For i = 1 To imgW*imgH\2 'go through all pixels
Swap @fp, @lp 'swap values
If i Mod imgW = (imgW-1) Then fp = fp - 8*imgW 'if reach new line, fp must be backed up to start of prior line
Incr lp : Incr fp 'move lp pointer forward
Next i
'-----------------------------------------------------------------
Next iLoop
Graphic Set Bits bmp$
QueryPerformanceCounter qStop
DeltaTime = (qStop-qStart)/qFreq
? "Vert_Pointer " + Format$(DeltaTime,"0.###") & " seconds"
End Sub
Sub HorzPointer
Local i,j,iLoop As Long
QueryPerformanceCounter qStart
For iLoop = 1 To iLoopMax 'test loop
'-----------------------------------------------------------------
lp_orig = StrPtr(bmp$)+8
For i = 1 To (imgW*imgH)*4 Step imgW*4
lp = lp_orig + (i-1) : fp = lp + imgW*4 - 4
For j = 1 To imgW\2
Swap @fp, @lp
Incr lp : Decr fp
Next j
Next i
'-----------------------------------------------------------------
Next iLoop
Graphic Set Bits bmp$
QueryPerformanceCounter qStop
DeltaTime = (qStop-qStart)/qFreq
? "Horz_Pointer " + Format$(DeltaTime,"0.###") & " seconds"
End Sub
Sub VertStretchBlt
Local iLoop As Long
QueryPerformanceCounter qStart
For iLoop = 1 To iLoopMax 'test loop
'-----------------------------------------------------------------
StretchBlt(hDC,0,imgH,imgW,-1*imgH,hDC,0,0,imgW,imgH,%SrcCopy)
'-----------------------------------------------------------------
Next iLoop
Graphic ReDraw
QueryPerformanceCounter qStop
DeltaTime = (qStop-qStart)/qFreq
? "Vert_StretchBlt " + Format$(DeltaTime,"0.###") & " seconds"
End Sub
Sub HortStretchBlt
Local iLoop As Long
QueryPerformanceCounter qStart
For iLoop = 1 To iLoopMax 'test loop
'-----------------------------------------------------------------
StretchBlt hDC,imgW,0,-1*imgW,imgH,hDC,0,0,imgW,imgH,%SrcCopy
'-----------------------------------------------------------------
Next iLoop
Graphic ReDraw
QueryPerformanceCounter qStop
DeltaTime = (qStop-qStart)/qFreq
? "Horz_StretchBlt " + Format$(DeltaTime,"0.###") & " seconds"
End Sub
Sub VertPointerPeek
Register p1 As Long, p2 As Long
Local i,temp1,temp2,iLoop As Long
QueryPerformanceCounter qStart
For iLoop = 1 To iLoopMax 'test loop
'-----------------------------------------------------------------
lp = StrPtr(bmp$)+8
lp_orig = lp
fp = lp + imgW*(imgH-1)*4 '1st pixel in last row
For i = 1 To imgW*imgH\2 'go through all pixels
' Swap @fp, @lp 'swap values
p1 = fp : p2 = lp
temp1=Peek(Long,p1)
temp2=Peek(Long,p2)
Poke Long,p1,temp2
Poke Long,p2,temp1
If i Mod imgW = (imgW-1) Then fp = fp - 8*imgW 'if reach new line, fp must be backed up to start of prior line
Incr lp : Incr fp 'move lp pointer forward
Next i
'-----------------------------------------------------------------
Next iLoop
Graphic Set Bits bmp$
QueryPerformanceCounter qStop
DeltaTime = (qStop-qStart)/qFreq
? "Vert_Peek " + Format$(DeltaTime,"0.###") & " seconds"
End Sub
Sub HortPointerPeek
Register p1 As Long, p2 As Long
Local i,j,temp1,temp2,iLoop As Long
QueryPerformanceCounter qStart
For iLoop = 1 To iLoopMax 'test loop
'-----------------------------------------------------------------
lp_orig = StrPtr(bmp$)+8
For i = 1 To (imgW*imgH)*4 Step imgW*4
lp = lp_orig + (i-1) : fp = lp + imgW*4 - 4
p1=fp:p2=lp
For j = 1 To imgW\2
'Swap @fp, @lp
temp1=Peek(Long,p1)
temp2=Peek(Long,p2)
Poke Long,p1,temp2
Poke Long,p2,temp1
'Incr lp : Decr fp
p1 -=4
p2 +=4
Next j
'-----------------------------------------------------------------
Next i
Next iLoop
Graphic Set Bits bmp$
QueryPerformanceCounter qStop
DeltaTime = (qStop-qStart)/qFreq
? "Horz_Peek " + Format$(DeltaTime,"0.###") & " seconds"
End Sub
Sub VertASM
Local i,j,iLoop As Long
QueryPerformanceCounter qStart
For iLoop = 1 To iLoopMax 'test loop
'-----------------------------------------------------------------
'no code available
'-----------------------------------------------------------------
Next iLoop
Graphic Set Bits bmp$
QueryPerformanceCounter qStop
DeltaTime = (qStop-qStart)/qFreq
? "Vert_ASM " + Format$(DeltaTime,"0.###") & " seconds"
End Sub
Sub HorzASM (imgWx As Long, imgHx As Long)
Local i,iLoop As Long
QueryPerformanceCounter qStart
For iLoop = 1 To iLoopMax 'test loop
'-----------------------------------------------------------------
lp_orig = StrPtr(bmp$)+8
For i = 1 To (imgWx*imgHx)*4 Step imgWx*4
lp = lp_orig + (i-1) : fp = lp + imgWx*4 - 4
'replace this inner loop with ASM
' For j = 1 To imgW\2
' Swap @fp, @lp
' Incr lp : Decr fp
' Next j
!pushad 'save all registers (simple but not efficient)
!mov edi,fp 'get fp into edi register
!mov esi,lp 'get lp into esi register
!mov ecx,imgwx 'get imgw address into ecx register (it's a parameter so it's the address we get not the value)
!mov ecx,[ecx] 'get imgw value into ecx
!sar ecx,1 'divide by 2
lp1:
!mov eax,[edi] 'get first pixel
!mov ebx,[esi] 'get last pixel
!mov [edi],ebx 'store first pixel
!mov [esi],eax 'store last pixel
!add esi,4 'update pointer
!sub edi,4 'update pointer
!dec ecx 'decrement loop counter
!jnz short lp1 'if not zero, go back for next one
!popad 'restore all registers
Next i
'-----------------------------------------------------------------
Next iLoop
Graphic Set Bits bmp$
QueryPerformanceCounter qStop
DeltaTime = (qStop-qStart)/qFreq
? "Horz_ASM " + Format$(DeltaTime,"0.###") & " seconds"
End Sub
Sub Vert1DArray
Local x1,x2,iLoop As Long
QueryPerformanceCounter qStart
ReDim Colors(imgW*imgH-1) As Long At StrPtr(bmp$) + 8
For iLoop = 1 To iLoopMax 'test loop
'-----------------------------------------------------------------
x2 = imgW*(imgH-1) '1st pixel in last row
For x1 = 0 To imgW*imgH \ 2
Swap colors(x1), colors(x2)
Incr x2
If x1 Mod imgW = (imgW-1) Then x2 = x2 - 2*imgW 'if reach new line, fp must be backed up to start of prior line
Next x1
'-----------------------------------------------------------------
Next iLoop
Graphic Set Bits bmp$
QueryPerformanceCounter qStop
DeltaTime = (qStop-qStart)/qFreq
? "Vert_1Darray " + Format$(DeltaTime,"0.###") & " seconds"
End Sub
Sub Horz1DArray
Local x1,x2,iLoop,iRow As Long
QueryPerformanceCounter qStart
ReDim Colors(imgW*imgH-1) As Long At StrPtr(bmp$) + 8
For iLoop = 1 To iLoopMax 'test loop
'-----------------------------------------------------------------
x2 = imgW-1
For x1 = 0 To imgW*imgH-1
Swap colors(x1), colors(x2)
Decr x2
If x2 <= x1 Then Incr iRow : x1=iRow*imgW-1 : x2=x1+imgW
Next x1
'-----------------------------------------------------------------
iRow = 0
Next iLoop
Graphic Set Bits bmp$
QueryPerformanceCounter qStop
DeltaTime = (qStop-qStart)/qFreq
? "Horz_1DArray " + Format$(DeltaTime,"0.###") & " seconds"
End Sub
Sub VertRows
Local i,iLoop As Long
QueryPerformanceCounter qStart
ReDim Rows(imgH-1) As ImageRow At StrPtr(bmp$) + 8
For iLoop = 1 To iLoopMax 'test loop
'-----------------------------------------------------------------
For i = 0 To imgH\2-1
Swap Rows(i), Rows(imgH-1-i)
Next i
'-----------------------------------------------------------------
Next iLoop
Graphic Set Bits bmp$
QueryPerformanceCounter qStop
DeltaTime = (qStop-qStart)/qFreq
? "Vert_Rows " + Format$(DeltaTime,"0.###") & " seconds"
End Sub
Sub HorzRows
Local i,iLoop As Long
QueryPerformanceCounter qStart
ReDim Rows(2*imgH-1) As ImageRow2 At StrPtr(bmp$) + 8
For iLoop = 1 To iLoopMax 'test loop
'-----------------------------------------------------------------
For i = 0 To 2*imgH-1 Step 2
Swap Rows(i), Rows(i+1)
Next i
'-----------------------------------------------------------------
Next iLoop
Graphic Set Bits bmp$
QueryPerformanceCounter qStop
DeltaTime = (qStop-qStart)/qFreq
? "Horz_Rows " + Format$(DeltaTime,"0.###") & " seconds"
End Sub
Sub VertMemSwap
Local i,iLoop As Long
QueryPerformanceCounter qStart
For iLoop = 1 To iLoopMax 'test loop
'-----------------------------------------------------------------
lp = StrPtr(bmp)+8 'start of bit string
For i = 0 To imgH\2-1
Memory Swap lp+i*imgW*4, lp+(imgH-i-1)*imgW*4, imgW*4
Next i
'-----------------------------------------------------------------
Next iLoop
Graphic Set Bits bmp$
QueryPerformanceCounter qStop
DeltaTime = (qStop-qStart)/qFreq
? "Vert_MemSwap " + Format$(DeltaTime,"0.###") & " seconds"
End Sub
Sub HorzMemSwap
Local iLoop As Long
QueryPerformanceCounter qStart
For iLoop = 1 To iLoopMax 'test loop
'-----------------------------------------------------------------
'no code available
'-----------------------------------------------------------------
Next iLoop
Graphic Set Bits bmp$
QueryPerformanceCounter qStop
DeltaTime = (qStop-qStart)/qFreq
? "Horz_MemSwap " + Format$(DeltaTime,"0.###") & " seconds"
End Sub
Sub Vert2Array
Register x As Long, xreverse As Long
Local iLoop,yreverse As Long
QueryPerformanceCounter qStart
For iLoop = 1 To iLoopMax 'test loop
'-----------------------------------------------------------------
'no code available
'-----------------------------------------------------------------
x = 0 : xreverse = 0 : yreverse = 0
Next iLoop
Graphic Set Bits bmp$
QueryPerformanceCounter qStop
DeltaTime = (qStop-qStart)/qFreq
? "Vert_2Arrays " + Format$(DeltaTime,"0.###") & " seconds"
End Sub
'gbs_00925
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm