2D: Faster way to roll an image?
Posted: Wed Jun 26, 2024 2:44 pm
hi guys,
I would like to change the view for 360 degree panoramic images and need an image rolling function.
I have considered two options for this. Using DrawImage and using GrabImage to scroll the content.
GrabImage seems to be much more performant. But is it any faster? So far it still looks quite jerky.

If you need a 360 Grad Pano: http://u.pc.cd/mvx7
I can't wait to see what else is possible here
I would like to change the view for 360 degree panoramic images and need an image rolling function.
I have considered two options for this. Using DrawImage and using GrabImage to scroll the content.
GrabImage seems to be much more performant. But is it any faster? So far it still looks quite jerky.

If you need a 360 Grad Pano: http://u.pc.cd/mvx7
I can't wait to see what else is possible here

Code: Select all
UseJPEGImageDecoder()
Procedure.b isShiftKey()
If GetAsyncKeyState_(#VK_SHIFT) & $8000
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
Procedure.b isStrgKey()
If GetAsyncKeyState_ (#VK_LCONTROL) & $8000 Or GetAsyncKeyState_ (#VK_RCONTROL) & $8000
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
Procedure RenderImage(ImgID)
If StartDrawing(CanvasOutput(0))
DrawImage(ImageID(ImgID), 0, 0)
StopDrawing()
EndIf
EndProcedure
Procedure RollImage_Variante_1 (ImgID, direction, offset)
Protected ImgWidth = ImageWidth(ImgID)
Protected ImgHeight = ImageHeight(ImgID)
Protected TempImg
TempImg = CreateImage(#PB_Any, ImgWidth, ImgHeight)
If TempImg
If StartDrawing(ImageOutput(TempImg))
Select direction
Case 1 ; left
DrawImage(ImageID(ImgID), -offset, 0)
DrawImage(ImageID(ImgID), ImgWidth - offset, 0)
Case 2 ; right
DrawImage(ImageID(ImgID), offset, 0)
DrawImage(ImageID(ImgID), -ImgWidth + offset, 0)
Case 4 ; up
DrawImage(ImageID(ImgID), 0, -offset)
DrawImage(ImageID(ImgID), 0, ImgHeight - offset)
Case 8 ; down
DrawImage(ImageID(ImgID), 0, offset)
DrawImage(ImageID(ImgID), 0, -ImgHeight + offset)
EndSelect
StopDrawing()
EndIf
CopyImage(TempImg, ImgID)
FreeImage(TempImg)
EndIf
EndProcedure
Procedure RollImage (ImgID, direction, offset) ; 1 left, 2 right, 4, up, 8 down
Protected ImgID_1, ImgID_2
If isShiftKey() : offset * 5 : EndIf
If isStrgKey() : offset * 10 : EndIf
If direction & 1 ; left
ImgID_1 = GrabImage(ImgID, #PB_Any, 0, 0, offset, ImageHeight(ImgID))
ImgID_2 = GrabImage(ImgID, #PB_Any, offset, 0, ImageWidth(ImgID) - offset, ImageHeight(ImgID))
If StartDrawing(ImageOutput(ImgID))
DrawImage(ImageID(ImgID_2), 0, 0)
DrawImage(ImageID(ImgID_1), ImageWidth(ImgID) - offset, 0)
StopDrawing()
EndIf
EndIf
If direction & 2 ; right
ImgID_1 = GrabImage(ImgID, #PB_Any, ImageWidth(ImgID) - offset, 0, offset, ImageHeight(ImgID))
ImgID_2 = GrabImage(ImgID, #PB_Any, 0, 0, ImageWidth(ImgID) - offset, ImageHeight(ImgID))
If StartDrawing(ImageOutput(ImgID))
DrawImage(ImageID(ImgID_1), 0, 0)
DrawImage(ImageID(ImgID_2), offset, 0)
StopDrawing()
EndIf
EndIf
If direction & 4 ; up
ImgID_1 = GrabImage(ImgID, #PB_Any, 0, 0, ImageWidth(ImgID),offset)
ImgID_2 = GrabImage(ImgID, #PB_Any, 0, offset, ImageWidth(ImgID), ImageHeight(ImgID) - offset)
If StartDrawing(ImageOutput(ImgID))
DrawImage(ImageID(ImgID_2), 0, 0)
DrawImage(ImageID(ImgID_1), 0, ImageHeight(ImgID) - offset)
StopDrawing()
EndIf
EndIf
If direction & 8 ; down
ImgID_1 = GrabImage(ImgID, #PB_Any, 0, ImageHeight(ImgID) - offset, ImageWidth(ImgID), offset)
ImgID_2 = GrabImage(ImgID, #PB_Any, 0, 0, ImageWidth(ImgID), ImageHeight(ImgID) - offset)
If StartDrawing(ImageOutput(ImgID))
DrawImage(ImageID(ImgID_1), 0, 0)
DrawImage(ImageID(ImgID_2), 0, offset)
StopDrawing()
EndIf
EndIf
If IsImage(ImgID_1) : FreeImage(ImgID_1) : EndIf
If IsImage(ImgID_2) : FreeImage(ImgID_2) : EndIf
EndProcedure
OpenWindow(0, 0, 0, 100, 200, "", #PB_Window_SystemMenu | #PB_Window_Maximize)
CanvasGadget(0, 0, 0, WindowWidth(0), WindowHeight(0), #PB_Canvas_Keyboard)
file.s = OpenFileRequester("Select Jpg image", "Pano_360.jpg", "Image (*.jpg;*.jpeg)|*.jpg;*.jpeg|All files (*.*)|*.*", 0)
If file <> ""
ImgID = LoadImage(#PB_Any, file)
ResizeImage(ImgID, GadgetWidth(0), GadgetHeight(0))
RenderImage(ImgID)
Else
ImgID = CreateImage(#PB_Any, GadgetWidth(0), GadgetHeight(0), 24, #White)
If StartDrawing(ImageOutput(ImgID))
For i = 1 To 100
Box (Random(ImageWidth(ImgID)-300), Random(ImageHeight(ImgID)-300), Random(300), Random(300), Random($FFFFFF))
Circle( Random(ImageWidth(ImgID)-150), Random(ImageHeight(ImgID)-150), Random(300), Random($FFFFFF))
Next
StopDrawing()
EndIf
EndIf
; Performance Test
DTG = ElapsedMilliseconds()
For i = 1 To 200
RollImage_Variante_1(ImgID, 1, 10)
Next
MessageRequester( "Variante 1", Str(ElapsedMilliseconds() - DTG) + "ms")
DTG = ElapsedMilliseconds()
For i = 1 To 200
RollImage(ImgID, 1, 10)
Next
MessageRequester( "Variante 2", Str(ElapsedMilliseconds() - DTG) + "ms")
SetActiveGadget(0)
Repeat
Event = WaitWindowEvent()
If Event = #PB_Event_Gadget And EventGadget() = 0
If EventType() = #PB_EventType_KeyDown
key = GetGadgetAttribute(0, #PB_Canvas_Key)
If key = #PB_Shortcut_Left Or key = #PB_Shortcut_Pad4
RollImage(ImgID, 1, 10)
ElseIf key =#PB_Shortcut_Right Or key = #PB_Shortcut_Pad6
RollImage(ImgID, 2, 10)
ElseIf key =#PB_Shortcut_Up Or key = #PB_Shortcut_Pad8
RollImage(ImgID, 4, 10)
ElseIf key =#PB_Shortcut_Down Or key = #PB_Shortcut_Pad2
RollImage(ImgID, 8, 10)
ElseIf key = #PB_Shortcut_Pad7 ; left up
RollImage(ImgID, 1|4, 10)
ElseIf key = #PB_Shortcut_Pad1 ; left down
RollImage(ImgID, 1|8, 10)
ElseIf key = #PB_Shortcut_Pad9 ; right up
RollImage(ImgID, 2|4, 10)
ElseIf key = #PB_Shortcut_Pad3 ; right down
RollImage(ImgID, 2|8, 10)
EndIf
RenderImage(ImgID)
EndIf
EndIf
Until Event = #PB_Event_CloseWindow