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
#Debug Error On
#Debug Display On
#Include "Win32API.inc"
Type ImageRow
Pixels(399) 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
%IDC_Vert2Array = 517 : %IDC_Horz2Array = 518
Global hDlg,hDC As Dword, bmp$, bmp_orig$, bmp2$, imgW, imgH as Long
Function PBMain() As Long
Dialog New Pixels, 0, "Vertical/Horizontal Flip",300,300,450,410, %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
Control Add Button, hDlg, %IDC_Vert2Array, "Vert 2 Array", 10,160,90,20
Control Add Button, hDlg, %IDC_Horz2Array, "Horz 2 Array", 110,160,90,20
imgW = 400 : imgH = 200
Control Add Graphic, hDlg, %IDC_Graphic, "", 10,200,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_Command
Select Case Cb.ctl
Case %IDC_VertArray : FlipVerticalArray(400,200)
Case %IDC_HorzArray : FlipHorizontalArray(400,200)
Case %IDC_VertPointer : FlipVerticalPointer(400,200)
Case %IDC_HorzPointer : FlipHorizontalPointer(400,200)
Case %IDC_VertStretchBlt : FlipVerticalStretchBlt(400,200)
Case %IDC_HorzStretchBlt : FlipHorizontalStretchBlt(400,200)
Case %IDC_VertPointerPeek: FlipVerticalPointerPeek(400,200)
Case %IDC_HorzPointerPeek: FlipHorizontalPointerPeek(400,200)
Case %IDC_VertASM : FlipVerticalASM(400,200)
Case %IDC_HorzASM : FlipHorizontalASM(400,200)
Case %IDC_Vert1DArray : FlipVertical1DArray(400,200)
Case %IDC_Horz1DArray : FlipHorizontal1DArray(400,200)
Case %IDC_VertRows : FlipVerticalRows(400,200)
Case %IDC_HorzRows : FlipHorizontalRows(400,200)
Case %IDC_VertMemSwap : FlipVerticalMemSwap(400,200)
Case %IDC_HorzMemSwap : FlipHorizontalMemSwap(400,200)
Case %IDC_Vert2Array : FlipVertical2Array(400,200)
Case %IDC_Horz2Array : FlipHorizontal2Array(400,200)
End Select
End Select
End Function
Sub FlipVerticalArray(imgW As Long, imgH As Long)
Local x,y As Long
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-y)
Next x
Next y
Graphic Set Bits bmp$
End Sub
Sub FlipHorizontalArray(imgW As Long, imgH As Long)
Local x,y As Long
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-x,y)
Next x
Next y
Graphic Set Bits bmp$
End Sub
Sub FlipVerticalPointer(imgW As Long, imgH As Long)
Local i,j As Long, lp,lp_orig,fp As Long Pointer
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
Graphic Set Bits bmp$
End Sub
Sub b_FlipVerticalPointer(imgW As Long, imgH As Long)
Local i,j As Long, lp,lp_orig,fp As Long Pointer
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
Incr lp 'move lp pointer forward
If i Mod imgW = 0 Then fp = fp - 8*imgW 'if reach new line, fp must be backed up to start of prior line
Incr fp 'move fp pointer forward
Next i
Graphic Set Bits bmp$
End Sub
Sub c_FlipVerticalPointer(imgW As Long, imgH As Long)
Local i,j As Long, lp,lp_orig,fp As Long Pointer
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
Incr lp 'move pointer forward
If i Mod imgW = 0 Then fp = fp - (2*imgW-1)*4 Else Incr fp 'if reach new line, fp must be backed up to start of prior line
Next i
Graphic Set Bits bmp$
End Sub
Sub origFlipVerticalPointer(imgW As Long, imgH As Long)
Local i,j As Long, lp,lp_orig,fp As Long Pointer
lp = StrPtr(bmp$)+8
lp_orig = lp
For i = 1 To imgW*imgH\2
fp = lp_orig + (imgW*(imgH-(i\imgW)-1)+(i Mod imgW -1))*4
Swap @fp, @lp
Incr lp
Next i
Graphic Set Bits bmp$
End Sub
Sub FlipHorizontalPointer(imgW as Long, imgH as Long)
Local i,j As Long, lp,lp_orig,fp As Long Pointer
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
Graphic Set Bits bmp$
End Sub
Sub FlipHorizontalStretchBlt(imgW As Long, imgH As Long)
StretchBlt hDC,imgW,0,-1*imgW,imgH,hDC,0,0,imgW,imgH,%SrcCopy
Graphic ReDraw
End Sub
Sub FlipVerticalStretchBlt(imgW As Long, imgH As Long)
StretchBlt(hDC,0,imgH,imgW,-1*imgH,hDC,0,0,imgW,imgH,%SrcCopy)
Graphic ReDraw
End Sub
Sub FlipVerticalPointerPeek(imgW as Long, imgH as Long)
Register p1 As Long, p2 As Long
Local i,j As Long, lp,lp_orig,fp As Long Pointer
Local temp1, temp2 As Long
lp = StrPtr(bmp$)+8
lp_orig = lp
' For i = 1 To imgW*(imgH\2)
' fp = lp_orig + (imgW*(imgH-(i\imgW)-1)+(i Mod imgW -1))*4
' Swap @fp, @lp
' Incr lp
' Next i
For i = 1 To imgW*(imgH\2)
fp = lp_orig + (imgW*(imgH-(i\imgW)-1)+(i Mod imgW -1))*4
p1 = fp : p2 = lp
' Swap @fp, @lp
temp1=Peek(Long,p1)
temp2=Peek(Long,p2)
Poke Long,p1,temp2
Poke Long,p2,temp1
lp += 4
Next i
Graphic Set Bits bmp$
End Sub
Sub FlipHorizontalPointerPeek(imgW as Long, imgH as Long)
Register p1 As Long, p2 As Long
Local temp1, temp2 As Long
Local i,j As Long, lp,lp_orig,fp As Long Pointer
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
' Incr lp : Decr fp
' Next j
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
Graphic Set Bits bmp$
End Sub
Sub FlipVerticalASM(imgW As Long, imgH As Long)
End Sub
Sub FlipHorizontalASM(imgW As Long, imgH As Long)
Local i,j AS Long, lp,lp_orig,fp AS Long Pointer
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
'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,imgw '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
Graphic Set Bits bmp$
End Sub
Sub FlipVertical1DArray(imgW As Long, imgH As Long)
End Sub
Sub FlipHorizontal1DArray(imgW As Long, imgH As Long)
Local x1,x2,iRow As Long
ReDim Colors(imgW*imgH-1) As Long At StrPtr(bmp$) + 8
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
Graphic Set Bits bmp$
End Sub
Sub FlipVerticalRows(imgW As Long, imgH As Long)
Local i As Long
Dim Rows(imgH-1) As ImageRow At StrPtr(bmp$) + 8
For i = 0 To imgH/2-1
Swap Rows(i), Rows(imgH-1-i)
Next i
Graphic Set Bits bmp$
End Sub
Sub FlipHorizontalRows(imgW As Long, imgH As Long)
End Sub
Sub FlipVerticalMemSwap(imgW As Long, imgH As Long)
Local i As Long, lp As Long Pointer
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
Graphic Set Bits bmp$
End Sub
Sub FlipHorizontalMemSwap(imgW As Long, imgH As Long)
End Sub
Sub xFlipHorizontal1DArray(imgW AS Long, imgH As Long)
Register x As Long, xReverse As Long
Local yReverse As Long
ReDim Colors(imgW*imgH-1) As Long At StrPtr(bmp$) + 8
For x = 0 to imgW*imgH-1
colors(x) = colors(imgW-1-xReverse+yReverse)
Incr xReverse
If xReverse = imgW Then xReverse = 0 : yReverse += imgW
Next x
Graphic Set Bits bmp2$
End Sub
Sub FlipVertical2Array(imgW As Long, imgH As Long)
End Sub
Sub FlipHorizontal2Array(imgW As Long, imgH As Long)
Register x As Long, xreverse As Long
Local yreverse As Long
bmp2$ = bmp$
Dim Colors(0 To imgW * imgH - 1) As Long At StrPtr(bmp$) + 8
Dim Colors2(0 To imgW * imgH - 1) As Long At StrPtr(bmp2$) + 8
For x = 0 To imgW * imgH - 1
colors2(x) = colors(imgW - 1 - xReverse + yReverse)
Incr xReverse
If xReverse = imgW Then
xReverse = 0
yReverse += imgW
End If
Next
Graphic Set Bits bmp2$
End Sub
'gbs_00928
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm