'
'
'
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Matrox.MatroxImagingLibrary
Namespace MImWarp
Friend Class Program
Private Const IMAGE_FILE As String = MIL.M_IMAGE_PATH & "BaboonMono.mim"
Private Const INTERPOLATION_MODE As Integer = MIL.M_NEAREST_NEIGHBOR
Private Shared ReadOnly Property FIXED_POINT_PRECISION() As Integer
Get
Return MIL.M_FIXED_POINT + (If((INTERPOLATION_MODE = MIL.M_NEAREST_NEIGHBOR), 0, 6))
End Get
End Property
Private Shared Function FLOAT_TO_FIXED_POINT(ByVal x As Double) As Integer
Return CInt(Fix((If((INTERPOLATION_MODE = MIL.M_NEAREST_NEIGHBOR), 1, 64)) * x))
End Function
Private Const ROTATION_STEP As Integer = 1
Shared Sub Main(ByVal args() As String)
Dim MilApplication As MIL_ID = MIL.M_NULL
Dim MilSystem As MIL_ID = MIL.M_NULL
Dim MilDisplay As MIL_ID = MIL.M_NULL
Dim MilDisplayImage As MIL_ID = MIL.M_NULL
Dim MilSourceImage As MIL_ID = MIL.M_NULL
Dim Mil4CornerArray As MIL_ID = MIL.M_NULL
Dim MilLutX As MIL_ID = MIL.M_NULL
Dim MilLutY As MIL_ID = MIL.M_NULL
Dim ChildWindow As MIL_ID = MIL.M_NULL
Dim FourCornerMatrix() As Single = {0.0F, 0.0F, 456.0F, 62.0F, 333.0F, 333.0F, 100.0F, 500.0F, 0.0F, 0.0F, 511.0F, 511.0F}
Dim Precision As MIL_INT = FIXED_POINT_PRECISION
Dim Interpolation As MIL_INT = INTERPOLATION_MODE
Dim MilLutXPtr(), MilLutYPtr() As Short
Dim OffsetX As MIL_INT = 0
Dim ImageWidth As MIL_INT = 0
Dim ImageHeight As MIL_INT = 0
Dim ImageType As MIL_INT = 0
Dim i As MIL_INT = 0
Dim j As MIL_INT = 0
Dim FramesPerSecond As Double = 0.0
Dim Time As Double = 0.0
Dim NbLoop As Double = 0.0
MIL.MappAllocDefault(MIL.M_DEFAULT, MilApplication, MilSystem, MilDisplay, CType(MIL.M_NULL, IntPtr), CType(MIL.M_NULL, IntPtr))
MIL.MbufRestore(IMAGE_FILE, MilSystem, MilSourceImage)
MIL.MbufAlloc2d(MilSystem, _
MIL.MbufInquire(MilSourceImage, MIL.M_SIZE_X, ImageWidth), _
MIL.MbufInquire(MilSourceImage, MIL.M_SIZE_Y, ImageHeight), _
MIL.MbufInquire(MilSourceImage, MIL.M_TYPE, ImageType), _
MIL.M_IMAGE + MIL.M_PROC + MIL.M_DISP, MilDisplayImage)
MIL.MbufCopy(MilSourceImage, MilDisplayImage)
MIL.MdispSelect(MilDisplay, MilDisplayImage)
Console.Write(Constants.vbLf + "WARPING:" + Constants.vbLf)
Console.Write("--------" + Constants.vbLf + Constants.vbLf)
Console.Write("This image will be warped using different methods." + Constants.vbLf)
Console.Write("Press <Enter> to continue." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
MIL.MbufAlloc2d(MilSystem, ImageWidth, ImageHeight, 16 + MIL.M_SIGNED, MIL.M_LUT, MilLutX)
MIL.MbufAlloc2d(MilSystem, ImageWidth, ImageHeight, 16 + MIL.M_SIGNED, MIL.M_LUT, MilLutY)
MIL.MbufAlloc2d(MilSystem, 12, 1, 32 + MIL.M_FLOAT, MIL.M_ARRAY, Mil4CornerArray)
MIL.MbufPut1d(Mil4CornerArray, 0, 12, FourCornerMatrix)
MIL.MgenWarpParameter(Mil4CornerArray, MilLutX, MilLutY, MIL.M_WARP_4_CORNER + Precision, MIL.M_DEFAULT, 0.0, 0.0)
MIL.MbufClear(MilDisplayImage, 0)
MIL.MimWarp(MilSourceImage, MilDisplayImage, MilLutX, MilLutY, MIL.M_WARP_LUT + Precision, Interpolation)
Console.Write("The image was warped from an arbitrary quadrilateral to a square." + Constants.vbLf)
Console.Write("Press <Enter> to continue." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
MilLutXPtr = New Short(CType((ImageHeight * ImageWidth - 1), Integer)) {}
MilLutYPtr = New Short(CType((ImageHeight * ImageWidth - 1), Integer)) {}
For j = 0 To CType((ImageHeight - 1), Integer)
For i = 0 To CType((ImageWidth - 1), Integer)
MilLutYPtr(CType((i + (j * ImageWidth)), Integer)) = CShort(Fix(FLOAT_TO_FIXED_POINT(((j) + CInt(Fix((20 * Math.Sin(0.03 * i))))))))
MilLutXPtr(CType((i + (j * ImageWidth)), Integer)) = CShort(Fix(FLOAT_TO_FIXED_POINT(((i) + CInt(Fix((20 * Math.Sin(0.03 * j))))))))
Next i
Next j
MIL.MbufPut2d(MilLutX, 0, 0, ImageWidth, ImageHeight, MilLutXPtr)
MIL.MbufPut2d(MilLutY, 0, 0, ImageWidth, ImageHeight, MilLutYPtr)
MIL.MbufClear(MilDisplayImage, 0)
MIL.MimWarp(MilSourceImage, MilDisplayImage, MilLutX, MilLutY, MIL.M_WARP_LUT + Precision, Interpolation)
Console.Write("The image was warped on two sinusoidal waveforms." + Constants.vbLf)
Console.Write("Press <Enter> to continue." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
MIL.MbufFree(MilSourceImage)
MIL.MbufAlloc2d(MilSystem, ImageWidth * 2, ImageHeight, ImageType, MIL.M_IMAGE + MIL.M_PROC, MilSourceImage)
MIL.MbufLoad(IMAGE_FILE, MilSourceImage)
GenerateSphericLUT(ImageWidth, ImageHeight, MilLutXPtr, MilLutYPtr)
MIL.MbufPut2d(MilLutX, 0, 0, ImageWidth, ImageHeight, MilLutXPtr)
MIL.MbufPut2d(MilLutY, 0, 0, ImageWidth, ImageHeight, MilLutYPtr)
MIL.MbufCopy(MilSourceImage, MilDisplayImage)
MIL.MbufChild2d(MilSourceImage, ImageWidth, 0, ImageWidth, ImageHeight, ChildWindow)
MIL.MbufCopy(MilDisplayImage, ChildWindow)
MIL.MbufFree(ChildWindow)
MIL.MbufClear(MilDisplayImage, 0)
Console.Write("The image is continuously warped on a sphere." + Constants.vbLf)
Console.Write("Press <Enter> to stop." + Constants.vbLf + Constants.vbLf)
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_RESET + MIL.M_SYNCHRONOUS, CType(MIL.M_NULL, IntPtr))
MIL.MbufChild2d(MilSourceImage, OffsetX, 0, ImageWidth, ImageHeight, ChildWindow)
Do While ((Not Console.KeyAvailable) Or (OffsetX <> (ImageWidth / 4)))
MIL.MbufChildMove(ChildWindow, OffsetX, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT)
MIL.MimWarp(ChildWindow, MilDisplayImage, MilLutX, MilLutY, MIL.M_WARP_LUT + Precision, Interpolation)
OffsetX += ROTATION_STEP
If OffsetX > ImageWidth - 1 Then
OffsetX = 0
End If
NbLoop += 1
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ + MIL.M_SYNCHRONOUS, Time)
FramesPerSecond = NbLoop / Time
Console.Write("Processing speed: {0:0} Images/Sec." & Constants.vbCr, FramesPerSecond)
YieldToGUI()
Loop
Console.ReadKey()
Console.Write(Constants.vbLf + "Press <Enter> to end." + Constants.vbLf)
Console.ReadKey()
MIL.MbufFree(ChildWindow)
MIL.MbufFree(MilLutX)
MIL.MbufFree(MilLutY)
MIL.MbufFree(Mil4CornerArray)
MIL.MbufFree(MilSourceImage)
MIL.MbufFree(MilDisplayImage)
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL)
End Sub
Private Shared Sub GenerateSphericLUT(ByVal ImageWidth As MIL_INT, ByVal ImageHeight As MIL_INT, ByVal MilLutXPtr() As Short, ByVal MilLutYPtr() As Short)
Dim i, j, k As MIL_INT
Dim utmp, vtmp, tmp As Double
Dim v As Short
Dim Radius As Double = 200.0
For j = 0 To ImageHeight - 1
k = j * ImageWidth
vtmp = (CDbl(j - (ImageHeight / 2)) / Radius)
If Math.Abs(vtmp) < 1.0 Then
vtmp = Math.Acos(-vtmp)
If vtmp = 0.0 Then
vtmp = 0.0000001
End If
v = CShort(Fix((vtmp / 3.1415926) * CDbl(ImageHeight - 1) + 0.5))
tmp = Radius * Math.Sin(vtmp)
For i = 0 To ImageWidth - 1
utmp = (CDbl(i - (ImageWidth / 2)) / tmp)
If Math.Abs(utmp) < 1.0 Then
utmp = Math.Acos(-utmp)
MilLutXPtr(CType((i + k), Integer)) = CShort(Fix(FLOAT_TO_FIXED_POINT(((utmp / 3.1415926) * CDbl((ImageWidth / 2) - 1) + 0.5))))
MilLutYPtr(CType((i + k), Integer)) = CShort(Fix(FLOAT_TO_FIXED_POINT(v)))
Else
MilLutXPtr(CType((i + k), Integer)) = CShort(Fix(FLOAT_TO_FIXED_POINT(ImageWidth)))
MilLutYPtr(CType((i + k), Integer)) = CShort(Fix(FLOAT_TO_FIXED_POINT(ImageHeight)))
End If
Next i
Else
For i = 0 To ImageWidth - 1
MilLutXPtr(CType((i + k), Integer)) = CShort(Fix(FLOAT_TO_FIXED_POINT(ImageWidth)))
MilLutYPtr(CType((i + k), Integer)) = CShort(Fix(FLOAT_TO_FIXED_POINT(ImageHeight)))
Next i
End If
Next j
End Sub
Private Shared Sub YieldToGUI()
#If M_MIL_USE_CE Then
System.Threading.Thread.Sleep(0)
#End If
End Sub
End Class
End Namespace