#include <mil.h>
#include <math.h>
#include <malloc.h>
#define IMAGE_FILE M_IMAGE_PATH MIL_TEXT("BaboonMono.mim")
#define INTERPOLATION_MODE M_NEAREST_NEIGHBOR
#if(INTERPOLATION_MODE == M_NEAREST_NEIGHBOR)
#define FIXED_POINT_PRECISION M_FIXED_POINT+0L
#define FLOAT_TO_FIXED_POINT(x) (1L * (x))
#else
#define FIXED_POINT_PRECISION M_FIXED_POINT+6L
#define FLOAT_TO_FIXED_POINT(x) (64L * (x))
#endif
#define ROTATION_STEP 1
void YieldToGUI();
void GenerateSphericLUT(MIL_INT ImageWidth, MIL_INT ImageHeight,
short *MilLutXPtr, short *MilLutYPtr);
int MosMain(void)
{
MIL_ID MilApplication,
MilSystem,
MilDisplay,
MilDisplayImage,
MilSourceImage,
Mil4CornerArray,
MilLutX,
MilLutY,
ChildWindow;
float FourCornerMatrix[12] = {
0.0,
0.0,
456.0,
62.0,
333.0,
333.0,
100.0,
500.0,
0.0,
0.0,
511.0,
511.0 };
MIL_INT Precision = FIXED_POINT_PRECISION;
MIL_INT Interpolation = INTERPOLATION_MODE;
short *MilLutXPtr, *MilLutYPtr;
MIL_INT OffsetX = 0;
MIL_INT ImageWidth, ImageHeight, ImageType, i, j;
MIL_DOUBLE FramesPerSecond = 0, Time = 0, NbLoop = 0;
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL);
MbufRestore(IMAGE_FILE, MilSystem, &MilSourceImage);
MbufAlloc2d(MilSystem, MbufInquire(MilSourceImage, M_SIZE_X, &ImageWidth),
MbufInquire(MilSourceImage, M_SIZE_Y, &ImageHeight),
MbufInquire(MilSourceImage, M_TYPE, &ImageType),
M_IMAGE+M_PROC+M_DISP, &MilDisplayImage);
MbufCopy(MilSourceImage, MilDisplayImage);
MdispSelect(MilDisplay, MilDisplayImage);
MosPrintf(MIL_TEXT("\nWARPING:\n"));
MosPrintf(MIL_TEXT("--------\n\n"));
MosPrintf(MIL_TEXT("This image will be warped using different methods.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MbufAlloc2d(MilSystem, ImageWidth, ImageHeight, 16L+M_SIGNED, M_LUT, &MilLutX);
MbufAlloc2d(MilSystem, ImageWidth, ImageHeight, 16L+M_SIGNED, M_LUT, &MilLutY);
MbufAlloc2d(MilSystem, 12L, 1L, 32L+M_FLOAT, M_ARRAY, &Mil4CornerArray);
MbufPut1d(Mil4CornerArray, 0L, 12L, FourCornerMatrix);
MgenWarpParameter(Mil4CornerArray, MilLutX, MilLutY,
M_WARP_4_CORNER+Precision, M_DEFAULT,
0.0, 0.0);
MbufClear(MilDisplayImage,0);
MimWarp(MilSourceImage, MilDisplayImage, MilLutX, MilLutY, M_WARP_LUT+Precision,
Interpolation);
MosPrintf(MIL_TEXT("The image was warped from an arbitrary"));
MosPrintf(MIL_TEXT(" quadrilateral to a square.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MilLutXPtr = (short*)malloc(sizeof(short)*ImageHeight*ImageWidth);
MilLutYPtr = (short*)malloc(sizeof(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*sin(0.03*i)))));
MilLutXPtr[i+ (j*ImageWidth)] =
(short)FLOAT_TO_FIXED_POINT(((i) + (int)((20*sin(0.03*j)))));
}
}
MbufPut2d(MilLutX, 0L, 0L, ImageWidth, ImageHeight, MilLutXPtr);
MbufPut2d(MilLutY, 0L, 0L, ImageWidth, ImageHeight, MilLutYPtr);
MbufClear(MilDisplayImage,0);
MimWarp(MilSourceImage,MilDisplayImage,MilLutX,MilLutY,M_WARP_LUT +Precision,
Interpolation);
MosPrintf(MIL_TEXT("The image was warped on two sinusoidal waveforms.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MbufFree(MilSourceImage);
MbufAlloc2d(MilSystem, ImageWidth*2, ImageHeight, ImageType,
M_IMAGE+M_PROC, &MilSourceImage);
MbufLoad(IMAGE_FILE, MilSourceImage);
GenerateSphericLUT(ImageWidth, ImageHeight, MilLutXPtr,MilLutYPtr);
MbufPut2d(MilLutX, 0L, 0L, ImageWidth, ImageHeight, MilLutXPtr);
MbufPut2d(MilLutY, 0L, 0L, ImageWidth, ImageHeight, MilLutYPtr);
MbufCopy(MilSourceImage, MilDisplayImage);
MbufChild2d(MilSourceImage, ImageWidth, 0, ImageWidth, ImageHeight, &ChildWindow);
MbufCopy(MilDisplayImage, ChildWindow);
MbufFree(ChildWindow);
MbufClear(MilDisplayImage,0);
MosPrintf(MIL_TEXT("The image is continuously warped on a sphere.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to stop.\n\n"));
MappTimer(M_DEFAULT, M_TIMER_RESET+M_SYNCHRONOUS, M_NULL);
MbufChild2d(MilSourceImage, OffsetX, 0, ImageWidth, ImageHeight, &ChildWindow);
while ((!MosKbhit()) || (OffsetX != (ImageWidth/4)))
{
MbufChildMove(ChildWindow, OffsetX, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT);
MimWarp(ChildWindow, MilDisplayImage, MilLutX, MilLutY,
M_WARP_LUT+Precision, Interpolation);
OffsetX += ROTATION_STEP;
if (OffsetX>ImageWidth-1)
OffsetX = 0;
NbLoop++;
MappTimer(M_DEFAULT, M_TIMER_READ+M_SYNCHRONOUS, &Time);
FramesPerSecond = NbLoop/Time;
MosPrintf(MIL_TEXT("Processing speed: %.0f Images/Sec.\r"), FramesPerSecond);
YieldToGUI();
}
MosGetch();
MosPrintf(MIL_TEXT("\nPress <Enter> to end.\n"));
MosGetch();
free(MilLutXPtr);
free(MilLutYPtr);
MbufFree(ChildWindow);
MbufFree(MilLutX);
MbufFree(MilLutY);
MbufFree(Mil4CornerArray);
MbufFree(MilSourceImage);
MbufFree(MilDisplayImage);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return 0;
}
void GenerateSphericLUT(MIL_INT ImageWidth, MIL_INT ImageHeight,
short *MilLutXPtr, short *MilLutYPtr)
{
MIL_INT i, j, k;
MIL_DOUBLE utmp, vtmp, tmp;
short v;
MIL_DOUBLE Radius = 200.0;
for (j=0; j < ImageHeight; j++ )
{
k = j*ImageWidth;
if (fabs( vtmp = ((MIL_DOUBLE)(j - (ImageHeight/2)) / Radius) ) < 1.0)
{
vtmp = acos( -vtmp );
if(vtmp == 0.0)
vtmp=0.0000001;
v = (short)((vtmp/3.1415926) * (MIL_DOUBLE)(ImageHeight - 1) + 0.5);
tmp = Radius*sin(vtmp);
for (i=0; i < ImageWidth; i++ )
{
if ( fabs(utmp = ((MIL_DOUBLE)(i - (ImageWidth/2)) / tmp)) < 1.0 )
{
utmp = acos( -utmp);
MilLutXPtr[i + k] = (short)FLOAT_TO_FIXED_POINT(((utmp/3.1415926) *
(MIL_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);
}
}
}
}
void YieldToGUI()
{
#if M_MIL_USE_CE
MosSleep(0);
#endif
}