'
'
'
'
'
'
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Threading
Imports System.Runtime.InteropServices
Imports Matrox.MatroxImagingLibrary
Namespace MThread
Friend Class Program
Private Const IMAGE_FILE As String = MIL.M_IMAGE_PATH & "Bird.mim"
Private Const IMAGE_WIDTH As Integer = 256
Private Const IMAGE_HEIGHT As Integer = 240
Private Const STRING_LENGTH_MAX As Integer = 40
Private Const STRING_POS_X As Integer = 10
Private Const STRING_POS_Y As Integer = 220
Private Const DRAW_RADIUS_NUMBER As Integer = 5
Private Const DRAW_RADIUS_STEP As Integer = 10
Private Const DRAW_CENTER_POSX As Integer = 196
Private Const DRAW_CENTER_POSY As Integer = 180
Public Class THREAD_PARAM
Public Id As MIL_ID
Public System As MIL_ID
Public OrgImage As MIL_ID
Public SrcImage As MIL_ID
Public DstImage As MIL_ID
Public DispImage As MIL_ID
Public DispOffsetX As MIL_INT
Public DispOffsetY As MIL_INT
Public ReadyEvent As MIL_ID
Public DoneEvent As MIL_ID
Public NumberOfIteration As Integer
Public Radius As Integer
Public [Exit] As Integer
Public LicenseModules As MIL_INT
Public SlaveThreadParam As THREAD_PARAM
End Class
Shared Sub Main(ByVal args() As String)
Dim MilApplication As MIL_ID = MIL.M_NULL
Dim MilRemoteApplication As MIL_ID = MIL.M_NULL
Dim MilSystem As MIL_ID = MIL.M_NULL
Dim MilDisplay As MIL_ID = MIL.M_NULL
Dim MilImage As MIL_ID = MIL.M_NULL
Dim MilOrgImage As MIL_ID = MIL.M_NULL
Dim TParTopLeft As New THREAD_PARAM()
Dim TParBotLeft As New THREAD_PARAM()
Dim TParTopRight As New THREAD_PARAM()
Dim TParBotRight As New THREAD_PARAM()
Dim Time As Double = 0.0
Dim FramesPerSecond As Double = 0.0
Dim LicenseModules As MIL_INT = 0
MIL.MappAllocDefault(MIL.M_DEFAULT, MilApplication, MilSystem, MilDisplay, CType(MIL.M_NULL, IntPtr), CType(MIL.M_NULL, IntPtr))
MIL.MbufAlloc2d(MilSystem, IMAGE_WIDTH * 2, IMAGE_HEIGHT * 2, 8 + MIL.M_UNSIGNED, MIL.M_IMAGE + MIL.M_PROC + MIL.M_DISP, MilImage)
MIL.MbufClear(MilImage, 0)
MIL.MdispSelect(MilDisplay, MilImage)
MIL.MdispInquire(MilDisplay, MIL.M_SELECTED, TParTopLeft.DispImage)
MIL.MbufAlloc2d(MilSystem, IMAGE_WIDTH, IMAGE_HEIGHT, 8 + MIL.M_UNSIGNED, MIL.M_IMAGE + MIL.M_PROC, MilOrgImage)
MIL.MbufAlloc2d(MilSystem, IMAGE_WIDTH, IMAGE_HEIGHT, 8 + MIL.M_UNSIGNED, MIL.M_IMAGE + MIL.M_PROC, TParTopLeft.SrcImage)
MIL.MbufAlloc2d(MilSystem, IMAGE_WIDTH, IMAGE_HEIGHT, 8 + MIL.M_UNSIGNED, MIL.M_IMAGE + MIL.M_PROC, TParBotLeft.DstImage)
MIL.MbufAlloc2d(MilSystem, IMAGE_WIDTH, IMAGE_HEIGHT, 8 + MIL.M_UNSIGNED, MIL.M_IMAGE + MIL.M_PROC, TParTopRight.SrcImage)
MIL.MbufAlloc2d(MilSystem, IMAGE_WIDTH, IMAGE_HEIGHT, 8 + MIL.M_UNSIGNED, MIL.M_IMAGE + MIL.M_PROC, TParBotRight.DstImage)
MIL.MthrAlloc(MilSystem, MIL.M_EVENT, MIL.M_DEFAULT, MIL.M_NULL, MIL.M_NULL, TParTopLeft.DoneEvent)
MIL.MthrAlloc(MilSystem, MIL.M_EVENT, MIL.M_DEFAULT, MIL.M_NULL, MIL.M_NULL, TParBotLeft.DoneEvent)
MIL.MthrAlloc(MilSystem, MIL.M_EVENT, MIL.M_DEFAULT, MIL.M_NULL, MIL.M_NULL, TParTopRight.DoneEvent)
MIL.MthrAlloc(MilSystem, MIL.M_EVENT, MIL.M_DEFAULT, MIL.M_NULL, MIL.M_NULL, TParBotRight.DoneEvent)
MIL.MsysInquire(MilSystem, MIL.M_OWNER_APPLICATION, MilRemoteApplication)
MIL.MappInquire(MilRemoteApplication, MIL.M_LICENSE_MODULES, LicenseModules)
TParTopLeft.System = MilSystem
TParTopLeft.OrgImage = MilOrgImage
TParTopLeft.DstImage = TParTopLeft.SrcImage
TParTopLeft.DispOffsetX = 0
TParTopLeft.DispOffsetY = 0
TParTopLeft.ReadyEvent = TParBotLeft.DoneEvent
TParTopLeft.NumberOfIteration = 0
TParTopLeft.Radius = 0
TParTopLeft.Exit = 0
TParTopLeft.LicenseModules = LicenseModules
TParTopLeft.SlaveThreadParam = TParBotLeft
TParBotLeft.System = MilSystem
TParBotLeft.OrgImage = 0
TParBotLeft.SrcImage = TParTopLeft.DstImage
TParBotLeft.DispImage = TParTopLeft.DispImage
TParBotLeft.DispOffsetX = 0
TParBotLeft.DispOffsetY = IMAGE_HEIGHT
TParBotLeft.ReadyEvent = TParTopLeft.DoneEvent
TParBotLeft.NumberOfIteration = 0
TParBotLeft.Radius = 0
TParBotLeft.Exit = 0
TParBotLeft.LicenseModules = LicenseModules
TParBotLeft.SlaveThreadParam = Nothing
TParTopRight.System = MilSystem
TParTopRight.OrgImage = MilOrgImage
TParTopRight.DstImage = TParTopRight.SrcImage
TParTopRight.DispImage = TParTopLeft.DispImage
TParTopRight.DispOffsetX = IMAGE_WIDTH
TParTopRight.DispOffsetY = 0
TParTopRight.ReadyEvent = TParBotRight.DoneEvent
TParTopRight.NumberOfIteration = 0
TParTopRight.Radius = 0
TParTopRight.Exit = 0
TParTopRight.LicenseModules = LicenseModules
TParTopRight.SlaveThreadParam = TParBotRight
TParBotRight.System = MilSystem
TParBotRight.OrgImage = 0
TParBotRight.SrcImage = TParTopRight.DstImage
TParBotRight.DispImage = TParTopLeft.DispImage
TParBotRight.DispOffsetX = IMAGE_WIDTH
TParBotRight.DispOffsetY = IMAGE_HEIGHT
TParBotRight.ReadyEvent = TParTopRight.DoneEvent
TParBotRight.NumberOfIteration = 0
TParBotRight.Radius = 0
TParBotRight.Exit = 0
TParBotRight.LicenseModules = LicenseModules
TParBotRight.SlaveThreadParam = Nothing
MIL.MbufLoad(IMAGE_FILE, MilOrgImage)
Dim TopThreadDelegate As New MIL_THREAD_FUNCTION_PTR(AddressOf TopThread)
Dim BotLeftThreadDelegate As New MIL_THREAD_FUNCTION_PTR(AddressOf BotLeftThread)
Dim BotRightThreadDelegate As New MIL_THREAD_FUNCTION_PTR(AddressOf BotRightThread)
Dim TParTopLeftHandle As GCHandle = GCHandle.Alloc(TParTopLeft)
Dim TParBotLeftHandle As GCHandle = GCHandle.Alloc(TParBotLeft)
Dim TParTopRightHandle As GCHandle = GCHandle.Alloc(TParTopRight)
Dim TParBotRightHandle As GCHandle = GCHandle.Alloc(TParBotRight)
MIL.MthrAlloc(MilSystem, MIL.M_THREAD, MIL.M_DEFAULT, TopThreadDelegate, CType(TParTopLeftHandle, IntPtr), TParTopLeft.Id)
MIL.MthrAlloc(MilSystem, MIL.M_THREAD, MIL.M_DEFAULT, BotLeftThreadDelegate, CType(TParBotLeftHandle, IntPtr), TParBotLeft.Id)
MIL.MthrAlloc(MilSystem, MIL.M_THREAD, MIL.M_DEFAULT, TopThreadDelegate, CType(TParTopRightHandle, IntPtr), TParTopRight.Id)
MIL.MthrAlloc(MilSystem, MIL.M_THREAD, MIL.M_DEFAULT, BotRightThreadDelegate, CType(TParBotRightHandle, IntPtr), TParBotRight.Id)
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_RESET + MIL.M_SYNCHRONOUS, CType(MIL.M_NULL, IntPtr))
MIL.MthrControl(TParTopLeft.ReadyEvent, MIL.M_EVENT_SET, MIL.M_SIGNALED)
MIL.MthrControl(TParTopRight.ReadyEvent, MIL.M_EVENT_SET, MIL.M_SIGNALED)
Console.Write(Constants.vbLf + "MULTI-THREADING:" + Constants.vbLf)
Console.Write("----------------" + Constants.vbLf + Constants.vbLf)
Console.Write("4 threads running..." + Constants.vbLf)
Console.Write("Press <Enter> to stop." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
TParTopLeft.Exit = 1
TParTopRight.Exit = 1
MIL.MthrWait(TParTopLeft.Id, MIL.M_THREAD_END_WAIT)
MIL.MthrWait(TParBotLeft.Id, MIL.M_THREAD_END_WAIT)
MIL.MthrWait(TParTopRight.Id, MIL.M_THREAD_END_WAIT)
MIL.MthrWait(TParBotRight.Id, MIL.M_THREAD_END_WAIT)
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ + MIL.M_SYNCHRONOUS, Time)
FramesPerSecond = (TParTopLeft.NumberOfIteration + TParBotLeft.NumberOfIteration + TParTopRight.NumberOfIteration + TParBotRight.NumberOfIteration) / Time
Console.Write("Top-left iterations done: {0,4}." + Constants.vbLf, TParTopLeft.NumberOfIteration)
Console.Write("Bottom-left iterations done: {0,4}." + Constants.vbLf, TParBotLeft.NumberOfIteration)
Console.Write("Top-right iterations done: {0,4}." + Constants.vbLf, TParTopRight.NumberOfIteration)
Console.Write("Bottom-right iterations done: {0,4}." + Constants.vbLf + Constants.vbLf, TParBotRight.NumberOfIteration)
Console.Write("Processing speed for the 4 threads: {0:0.0} Images/Sec." + Constants.vbLf + Constants.vbLf, FramesPerSecond)
Console.Write("Press <Enter> to end." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
MIL.MthrFree(TParTopLeft.Id)
MIL.MthrFree(TParBotLeft.Id)
MIL.MthrFree(TParTopRight.Id)
MIL.MthrFree(TParBotRight.Id)
MIL.MthrFree(TParTopLeft.DoneEvent)
MIL.MthrFree(TParBotLeft.DoneEvent)
MIL.MthrFree(TParTopRight.DoneEvent)
MIL.MthrFree(TParBotRight.DoneEvent)
MIL.MbufFree(TParTopLeft.SrcImage)
MIL.MbufFree(TParTopRight.SrcImage)
MIL.MbufFree(TParBotLeft.DstImage)
MIL.MbufFree(TParBotRight.DstImage)
MIL.MbufFree(MilOrgImage)
MIL.MbufFree(MilImage)
TParTopLeftHandle.Free()
TParBotLeftHandle.Free()
TParTopRightHandle.Free()
TParBotRightHandle.Free()
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL)
End Sub
Private Shared Function TopThread(ByVal ThreadParameters As IntPtr) As UInteger
Dim TPar As THREAD_PARAM = CType((CType(ThreadParameters, GCHandle)).Target, THREAD_PARAM)
Do While TPar.Exit = 0
MIL.MthrWait(TPar.ReadyEvent, MIL.M_EVENT_WAIT)
If (TPar.NumberOfIteration Mod 192) = 0 Then
MIL.MbufCopy(TPar.OrgImage, TPar.SrcImage)
End If
If (TPar.LicenseModules And MIL.M_LICENSE_IM) <> 0 Then
MIL.MimArith(TPar.SrcImage, 1L, TPar.DstImage, MIL.M_ADD_CONST + MIL.M_SATURATION)
Else
TPar.SlaveThreadParam.Radius = (TPar.NumberOfIteration Mod DRAW_RADIUS_NUMBER) * DRAW_RADIUS_STEP
TPar.Radius = TPar.SlaveThreadParam.Radius
MIL.MgraColor(MIL.M_DEFAULT, &HFF)
MIL.MgraRectFill(MIL.M_DEFAULT, TPar.DstImage, DRAW_CENTER_POSX - TPar.Radius, DRAW_CENTER_POSY - TPar.Radius, DRAW_CENTER_POSX + TPar.Radius, DRAW_CENTER_POSY + TPar.Radius)
End If
TPar.NumberOfIteration += 1
MIL.MgraColor(MIL.M_DEFAULT, &HFF)
MIL.MgraText(MIL.M_DEFAULT, TPar.DstImage, STRING_POS_X, STRING_POS_Y, String.Format("{0}", TPar.NumberOfIteration))
If (TPar.DispImage <> MIL.M_NULL) Then
MIL.MbufCopyColor2d(TPar.DstImage, TPar.DispImage, MIL.M_ALL_BANDS, 0, 0, MIL.M_ALL_BANDS, TPar.DispOffsetX, TPar.DispOffsetY, IMAGE_WIDTH, IMAGE_HEIGHT)
End If
MIL.MthrControl(TPar.DoneEvent, MIL.M_EVENT_SET, MIL.M_SIGNALED)
Loop
TPar.SlaveThreadParam.Exit = 1
MIL.MthrControl(TPar.DoneEvent, MIL.M_EVENT_SET, MIL.M_SIGNALED)
MIL.MthrWait(TPar.System, MIL.M_THREAD_WAIT, CType(MIL.M_NULL, IntPtr))
Return 1
End Function
Private Shared Function BotLeftThread(ByVal ThreadParameters As IntPtr) As UInteger
Dim TPar As THREAD_PARAM = CType((CType(ThreadParameters, GCHandle)).Target, THREAD_PARAM)
Dim Angle As Double = 0.0
Dim AngleIncrement As Double = 0.5
Do While TPar.Exit = 0
MIL.MthrWait(TPar.ReadyEvent, MIL.M_EVENT_WAIT)
If (TPar.LicenseModules And MIL.M_LICENSE_IM) <> 0 Then
MIL.MimRotate(TPar.SrcImage, TPar.DstImage, Angle, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_NEAREST_NEIGHBOR + MIL.M_OVERSCAN_CLEAR)
Angle += AngleIncrement
If (Angle >= 360) Then
Angle -= 360
End If
Else
MIL.MbufCopy(TPar.SrcImage, TPar.DstImage)
MIL.MgraColor(MIL.M_DEFAULT, &H80)
MIL.MgraArcFill(MIL.M_DEFAULT, TPar.DstImage, DRAW_CENTER_POSX, DRAW_CENTER_POSY, TPar.Radius, TPar.Radius, 0, 360)
End If
TPar.NumberOfIteration += 1
MIL.MgraColor(MIL.M_DEFAULT, &HFF)
MIL.MgraText(MIL.M_DEFAULT, TPar.DstImage, STRING_POS_X, STRING_POS_Y, String.Format("{0}", TPar.NumberOfIteration))
If (TPar.DispImage <> MIL.M_NULL) Then
MIL.MbufCopyColor2d(TPar.DstImage, TPar.DispImage, MIL.M_ALL_BANDS, 0, 0, MIL.M_ALL_BANDS, TPar.DispOffsetX, TPar.DispOffsetY, IMAGE_WIDTH, IMAGE_HEIGHT)
End If
MIL.MthrControl(TPar.DoneEvent, MIL.M_EVENT_SET, MIL.M_SIGNALED)
Loop
MIL.MthrWait(TPar.System, MIL.M_THREAD_WAIT, CType(MIL.M_NULL, IntPtr))
Return 1
End Function
Private Shared Function BotRightThread(ByVal ThreadParameters As IntPtr) As UInteger
Dim TPar As THREAD_PARAM = CType((CType(ThreadParameters, GCHandle)).Target, THREAD_PARAM)
Do While TPar.Exit = 0
MIL.MthrWait(TPar.ReadyEvent, MIL.M_EVENT_WAIT)
If (TPar.LicenseModules And MIL.M_LICENSE_IM) <> 0 Then
MIL.MimConvolve(TPar.SrcImage, TPar.DstImage, MIL.M_EDGE_DETECT_SOBEL_FAST)
Else
MIL.MbufCopy(TPar.SrcImage, TPar.DstImage)
MIL.MgraColor(MIL.M_DEFAULT, &H40)
MIL.MgraArcFill(MIL.M_DEFAULT, TPar.DstImage, DRAW_CENTER_POSX, DRAW_CENTER_POSY, TPar.Radius / 2, TPar.Radius / 2, 0, 360)
End If
TPar.NumberOfIteration += 1
MIL.MgraColor(MIL.M_DEFAULT, &HFF)
MIL.MgraText(MIL.M_DEFAULT, TPar.DstImage, STRING_POS_X, STRING_POS_Y, String.Format("{0}", TPar.NumberOfIteration))
If (TPar.DispImage <> MIL.M_NULL) Then
MIL.MbufCopyColor2d(TPar.DstImage, TPar.DispImage, MIL.M_ALL_BANDS, 0, 0, MIL.M_ALL_BANDS, TPar.DispOffsetX, TPar.DispOffsetY, IMAGE_WIDTH, IMAGE_HEIGHT)
End If
MIL.MthrControl(TPar.DoneEvent, MIL.M_EVENT_SET, MIL.M_SIGNALED)
Loop
MIL.MthrWait(TPar.System, MIL.M_THREAD_WAIT, CType(MIL.M_NULL, IntPtr))
Return 1
End Function
End Class
End Namespace