Platforms to show: All Mac Windows Linux Cross-Platform

/Tools/JavaScript/JavaScript Pixel Editing


Required plugins for this example: MBS Tools Plugin, MBS Main Plugin, MBS Picture Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Tools/JavaScript/JavaScript Pixel Editing

This example is the version from Thu, 26th Feb 2020.

Project "JavaScript Pixel Editing.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits Window
Control List Inherits Listbox
ControlInstance List Inherits Listbox
EventHandler Sub DoubleClick() MsgBox me.Cell(me.ListIndex,0) End EventHandler
End Control
Control InputText Inherits TextArea
ControlInstance InputText Inherits TextArea
End Control
Control EvalButton Inherits PushButton
ControlInstance EvalButton Inherits PushButton
EventHandler Sub Action() EvalJavaScript End EventHandler
End Control
Control Canvas1 Inherits Canvas
ControlInstance Canvas1 Inherits Canvas
End Control
Control Canvas2 Inherits Canvas
ControlInstance Canvas2 Inherits Canvas
End Control
Control EvalThreadButton Inherits PushButton
ControlInstance EvalThreadButton Inherits PushButton
EventHandler Sub Action() EvalJavaScriptMT 1 End EventHandler
End Control
Control Timer1 Inherits Timer
ControlInstance Timer1 Inherits Timer
EventHandler Sub Action() EvalJavaScriptMT_Finish End EventHandler
End Control
Control EvalThreadButton1 Inherits PushButton
ControlInstance EvalThreadButton1 Inherits PushButton
EventHandler Sub Action() EvalJavaScriptMT 8 End EventHandler
End Control
Control ProgressWheel1 Inherits ProgressWheel
ControlInstance ProgressWheel1 Inherits ProgressWheel
End Control
Control EvalXojoButton Inherits PushButton
ControlInstance EvalXojoButton Inherits PushButton
EventHandler Sub Action() EvalXojo1 End EventHandler
End Control
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control CheckTimes Inherits CheckBox
ControlInstance CheckTimes Inherits CheckBox
End Control
EventHandler Sub Open() // global JavaScript jg = New MyJavaScriptEngineMBS InputPicture = LogoMBS(300) OutputPicture = Nil canvas1.Backdrop = InputPicture canvas2.Backdrop = OutputPicture End EventHandler
Sub EvalJavaScript() List.AppendRow "Run this in JavaScript" ProgressWheel1.Visible = True Dim je As MyJavaScriptEngineMBS = jg Dim m1 As Double = Microseconds Dim w As Integer = InputPicture.Width Dim h As Integer = InputPicture.Height Dim m As New MemoryBlock(W * H * 3) Dim count As Integer = 1 If CheckTimes.Value Then count = 100 end if Call InputPicture.CopyRGBtoMemoryblockMBS(m, 0) Try // Pass Memoryblock To JavaScript as reference je.GlobalMemoryBlock("mem") = m je.AddFunction "processPixels", InputText.Text Dim v As Variant = je.CallFunction("processPixels", w, h, count) List.AppendRow "result: "+v.StringValue Catch j As JavaScriptEngineExceptionMBS List.AppendRow "Exception: "+j.Message End Try OutputPicture = MemoryblockRGBtoPictureMBS(m, 0, w, h) canvas2.Backdrop = OutputPicture Dim m2 As Double = Microseconds List.AppendRow Str((m2-m1)/1000, "0.0")+" ms" ProgressWheel1.Visible = False End Sub
Sub EvalJavaScriptMT(ThreadCount as integer) List.AppendRow "Run "+Str(ThreadCount)+" threads with JavaScript." EvalThreadButton.Enabled = False EvalThreadButton1.Enabled = False ProgressWheel1.Visible = True Dim StartTime As Double = Microseconds Dim w As Integer = InputPicture.Width Dim h As Integer = InputPicture.Height Dim NewScript As String = InputText.Text Dim count As Integer = 1 If CheckTimes.Value Then count = 100 End If For i As Integer = 1 To ThreadCount Dim je As New MyJavaScriptEngineMBS Dim m As New MemoryBlock(W * H * 3) Call InputPicture.CopyRGBtoMemoryblockMBS(m, 0) Dim t As New WorkThread t.Width = w t.Height = h t.mem = m t.NewScript = NewScript t.StartTime = StartTime t.je = je t.count = count t.ThreadCount = ThreadCount If MyThread = Nil Then // let only last one report MyThread = t t.FinishMethod = AddressOf EvalJavaScriptMT_Finish End If t.run Next End Sub
Sub EvalJavaScriptMT_Finish() Dim t As WorkThread = MyThread Dim m As MemoryBlock = t.mem Dim w As Integer = t.Width Dim h As Integer = t.Height OutputPicture = MemoryblockRGBtoPictureMBS(m, 0, w, h) canvas2.Backdrop = OutputPicture Dim EndTime As Double = Microseconds List.AppendRow t.result List.AppendRow Str((EndTime-t.StartTime)/1000, "0.0")+" ms for running "+Str(t.ThreadCount)+" times, each on one preemptive thread." ProgressWheel1.Visible = False EvalThreadButton.Enabled = True EvalThreadButton1.Enabled = True MyThread = Nil End Sub
Sub EvalXojo1() List.AppendRow "Run similar function in pure Xojo" #Pragma DisableBackgroundTasks True Dim m1 As Double = Microseconds Dim w As Integer = InputPicture.Width Dim h As Integer = InputPicture.Height Dim m As New MemoryBlock(W * H * 3) Dim count As Integer = 1 If CheckTimes.Value Then count = 100 End If Call InputPicture.CopyRGBtoMemoryblockMBS(m, 0) Dim offset As Integer = 0 // ptr is faster than MemoryBlock dim p as ptr = m For n As Integer = 1 To count For y As Integer = 1 To h For x As Integer = 1 To w p.UInt8(offset + 0) = p.UInt8(offset + 0) * 0.8 + 0.2 p.UInt8(offset + 1) = p.UInt8(offset + 1) * 0.8 + 0.2 p.UInt8(offset + 2) = p.UInt8(offset + 2) * 0.8 + 0.2 offset = offset + 3 Next Next Next OutputPicture = MemoryblockRGBtoPictureMBS(m, 0, w, h) canvas2.Backdrop = OutputPicture Dim m2 As Double = Microseconds List.AppendRow Str((m2-m1)/1000, "0.0")+" ms" End Sub
Sub EvalXojo2() List.AppendRow "Run similar function in pure Xojo" #Pragma DisableBackgroundTasks True Dim m1 As Double = Microseconds Dim w As Integer = InputPicture.Width Dim h As Integer = InputPicture.Height Dim m As New MemoryBlock(W * H * 3) Dim count As Integer = 1 If CheckTimes.Value Then count = 100 End If Call InputPicture.CopyRGBtoMemoryblockMBS(m, 0) Dim offset As Integer = 0 For n As Integer = 1 To count For y As Integer = 1 To h For x As Integer = 1 To w m.UInt8Value(offset + 0) = m.UInt8Value(offset + 0) * 0.8 + 0.2 m.UInt8Value(offset + 1) = m.UInt8Value(offset + 1) * 0.8 + 0.2 m.UInt8Value(offset + 2) = m.UInt8Value(offset + 2) * 0.8 + 0.2 offset = offset + 3 Next next Next OutputPicture = MemoryblockRGBtoPictureMBS(m, 0, w, h) canvas2.Backdrop = OutputPicture Dim m2 As Double = Microseconds List.AppendRow Str((m2-m1)/1000, "0.0")+" ms" End Sub
Property InputPicture As Picture
Property MyThread As WorkThread
Property OutputPicture As Picture
Property jg As MyJavaScriptEngineMBS
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class MyJavaScriptEngineMBS Inherits JavaScriptEngineMBS
End Class
Module Module1
Sub AppendRow(extends l as listbox, s as string) l.AddRow s l.ScrollPosition = l.ListCount // go to end End Sub
End Module
Class WorkThread Inherits Thread
EventHandler Sub Run() Try // Pass Memoryblock To JavaScript as reference je.GlobalMemoryBlock("mem") = mem je.AddFunction "processPixels", NewScript Dim v As Variant = je.CallFunctionMT("processPixels", width, height, count) Self.Result = "Result: "+v.StringValue Catch j As JavaScriptEngineExceptionMBS Self.Result = "Exception: "+j.Message End Try If FinishMethod <> Nil Then xojo.core.Timer.CallLater 0, FinishMethod End If End EventHandler
Property FinishMethod As Xojo.Core.Timer.CallNoParams
Property Height As Integer
Property NewScript As string
Property Result As string
Property StartTime As Double
Property ThreadCount As Integer
Property Width As Integer
Property count As integer
Property je As MyJavaScriptEngineMBS
Property mem As MemoryBlock
End Class
End Project

See also:

The items on this page are in the following plugins: MBS Tools Plugin.


The biggest plugin in space...