using System;
using System.Collections.Generic;
using System.Text;
using Matrox.MatroxImagingLibrary;
namespace MImWarp
{
class Program
{
private const string IMAGE_FILE = MIL.M_IMAGE_PATH + "BaboonMono.mim";
private const int INTERPOLATION_MODE = MIL.M_NEAREST_NEIGHBOR;
private static int FIXED_POINT_PRECISION
{
get { return MIL.M_FIXED_POINT + ((INTERPOLATION_MODE == MIL.M_NEAREST_NEIGHBOR) ? 0 : 6); }
}
private static int FLOAT_TO_FIXED_POINT(double x)
{
return (int)(((INTERPOLATION_MODE == MIL.M_NEAREST_NEIGHBOR) ? 1 : 64) * x);
}
private const int ROTATION_STEP = 1;
static void Main(string[] args)
{
MIL_ID MilApplication = MIL.M_NULL;
MIL_ID MilSystem = MIL.M_NULL;
MIL_ID MilDisplay = MIL.M_NULL;
MIL_ID MilDisplayImage = MIL.M_NULL;
MIL_ID MilSourceImage = MIL.M_NULL;
MIL_ID Mil4CornerArray = MIL.M_NULL;
MIL_ID MilLutX = MIL.M_NULL;
MIL_ID MilLutY = MIL.M_NULL;
MIL_ID ChildWindow = MIL.M_NULL;
float[] FourCornerMatrix = new float[12]
{
0.0F,
0.0F,
456.0F,
62.0F,
333.0F,
333.0F,
100.0F,
500.0F,
0.0F,
0.0F,
511.0F,
511.0F
};
MIL_INT Precision = FIXED_POINT_PRECISION;
MIL_INT Interpolation = INTERPOLATION_MODE;
short[] MilLutXPtr, MilLutYPtr;
MIL_INT OffsetX = 0;
MIL_INT ImageWidth = 0;
MIL_INT ImageHeight = 0;
MIL_INT ImageType = 0;
MIL_INT i = 0;
MIL_INT j = 0;
double FramesPerSecond = 0.0;
double Time = 0.0;
double NbLoop = 0.0;
MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, ref MilDisplay, MIL.M_NULL, MIL.M_NULL);
MIL.MbufRestore(IMAGE_FILE, MilSystem, ref MilSourceImage);
MIL.MbufAlloc2d(MilSystem,
MIL.MbufInquire(MilSourceImage, MIL.M_SIZE_X, ref ImageWidth),
MIL.MbufInquire(MilSourceImage, MIL.M_SIZE_Y, ref ImageHeight),
MIL.MbufInquire(MilSourceImage, MIL.M_TYPE, ref ImageType),
MIL.M_IMAGE + MIL.M_PROC + MIL.M_DISP, ref MilDisplayImage);
MIL.MbufCopy(MilSourceImage, MilDisplayImage);
MIL.MdispSelect(MilDisplay, MilDisplayImage);
Console.Write("\nWARPING:\n");
Console.Write("--------\n\n");
Console.Write("This image will be warped using different methods.\n");
Console.Write("Press <Enter> to continue.\n\n");
Console.ReadKey();
MIL.MbufAlloc2d(MilSystem, ImageWidth, ImageHeight, 16 + MIL.M_SIGNED, MIL.M_LUT, ref MilLutX);
MIL.MbufAlloc2d(MilSystem, ImageWidth, ImageHeight, 16 + MIL.M_SIGNED, MIL.M_LUT, ref MilLutY);
MIL.MbufAlloc2d(MilSystem, 12, 1, 32 + MIL.M_FLOAT, MIL.M_ARRAY, ref 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.\n");
Console.Write("Press <Enter> to continue.\n\n");
Console.ReadKey();
MilLutXPtr = new short[ImageHeight * ImageWidth];
MilLutYPtr = new short[ImageHeight * ImageWidth];
for (j = 0; j < ImageHeight; j++)
{
for (i = 0; i < ImageWidth; i++)
{
MilLutYPtr[i + (j * ImageWidth)] = (short)FLOAT_TO_FIXED_POINT(((j) + (int)((20 * Math.Sin(0.03 * i)))));
MilLutXPtr[i + (j * ImageWidth)] = (short)FLOAT_TO_FIXED_POINT(((i) + (int)((20 * Math.Sin(0.03 * 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.\n");
Console.Write("Press <Enter> to continue.\n\n");
Console.ReadKey();
MIL.MbufFree(MilSourceImage);
MIL.MbufAlloc2d(MilSystem, ImageWidth * 2, ImageHeight, ImageType, MIL.M_IMAGE + MIL.M_PROC, ref 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, ref ChildWindow);
MIL.MbufCopy(MilDisplayImage, ChildWindow);
MIL.MbufFree(ChildWindow);
MIL.MbufClear(MilDisplayImage, 0);
Console.Write("The image is continuously warped on a sphere.\n");
Console.Write("Press <Enter> to stop.\n\n");
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_RESET + MIL.M_SYNCHRONOUS, MIL.M_NULL);
MIL.MbufChild2d(MilSourceImage, OffsetX, 0, ImageWidth, ImageHeight, ref ChildWindow);
while ((!Console.KeyAvailable) || (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)
{
OffsetX = 0;
}
NbLoop++;
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ + MIL.M_SYNCHRONOUS, ref Time);
FramesPerSecond = NbLoop / Time;
Console.Write("Processing speed: {0:0} Images/Sec.\r", FramesPerSecond);
YieldToGUI();
}
Console.ReadKey();
Console.Write("\nPress <Enter> to end.\n");
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);
}
static void GenerateSphericLUT(MIL_INT ImageWidth, MIL_INT ImageHeight, short[] MilLutXPtr, short[] MilLutYPtr)
{
MIL_INT i, j, k;
double utmp, vtmp, tmp;
short v;
double Radius = 200.0;
for (j = 0; j < ImageHeight; j++)
{
k = j * ImageWidth;
if (Math.Abs(vtmp = ((double)(j - (ImageHeight / 2)) / Radius)) < 1.0)
{
vtmp = Math.Acos(-vtmp);
if (vtmp == 0.0)
{
vtmp = 0.0000001;
}
v = (short)((vtmp / 3.1415926) * (double)(ImageHeight - 1) + 0.5);
tmp = Radius * Math.Sin(vtmp);
for (i = 0; i < ImageWidth; i++)
{
if (Math.Abs(utmp = ((double)(i - (ImageWidth / 2)) / tmp)) < 1.0)
{
utmp = Math.Acos(-utmp);
MilLutXPtr[i + k] = (short)FLOAT_TO_FIXED_POINT(((utmp / 3.1415926) *
(double)((ImageWidth / 2) - 1) + 0.5));
MilLutYPtr[i + k] = (short)FLOAT_TO_FIXED_POINT(v);
}
else
{
MilLutXPtr[i + k] = (short)FLOAT_TO_FIXED_POINT(ImageWidth);
MilLutYPtr[i + k] = (short)FLOAT_TO_FIXED_POINT(ImageHeight);
}
}
}
else
{
for (i = 0; i < ImageWidth; i++)
{
MilLutXPtr[i + k] = (short)FLOAT_TO_FIXED_POINT(ImageWidth);
MilLutYPtr[i + k] = (short)FLOAT_TO_FIXED_POINT(ImageHeight);
}
}
}
}
static void YieldToGUI()
{
#if M_MIL_USE_CE
System.Threading.Thread.Sleep(0);
#endif
}
}
}