#include <mil.h>
#include <math.h>
#define EXAMPLE_IMAGE_PATH M_IMAGE_PATH MIL_TEXT("GPU/")
#define IMAGE_FILE EXAMPLE_IMAGE_PATH MIL_TEXT("lena1Kx1K.tif")
#define MAX_ZOOM_FACTOR 1.5
#define MIN_ZOOM_FACTOR 0.1
#define ZOOM_RATE 0.001
#define ROTATION_RATE 0.1
#define CORNER_FACTOR 0.1f
inline MIL_DOUBLE ComputeZoomFactor(const MIL_DOUBLE &ZoomFactor);
void ComputeWarpParameters(float* WarpParamArray, MIL_INT ImageWidth, MIL_INT ImageHeight);
int MosMain(void)
{
MIL_ID MilApplication,
MilSystem,
MilDisplay,
MilBayer,
MilSourceColor,
MilSharpen,
MilProcessedImage,
MilWarpParamBuffer,
MilWarpCoeffBuffer;
MIL_INT ImageWidth, ImageHeight;
MIL_DOUBLE FramesPerSecond = 0;
MIL_DOUBLE Time = 0;
MIL_DOUBLE ZoomFactor = 1;
MIL_DOUBLE Rotation = 0;
MIL_INT NbLoop = 0;
float WarpParamArray[12] = {0.0};
MIL_TEXT_CHAR ImageFile[512] = {0};
MappAlloc(M_NULL, M_DEFAULT, &MilApplication);
MsysAlloc(M_DEFAULT, M_SYSTEM_GPU, M_DEV0, M_DEFAULT, &MilSystem);
MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_DEFAULT, &MilDisplay);
MosStrcpy(ImageFile, 512, IMAGE_FILE);
MbufRestore(ImageFile, MilSystem, &MilBayer);
MbufInquire(MilBayer, M_SIZE_X, &ImageWidth);
MbufInquire(MilBayer, M_SIZE_Y, &ImageHeight);
MbufAllocColor(MilSystem, 3, ImageWidth, ImageHeight,
8+M_UNSIGNED, M_IMAGE+M_PROC+M_DISP, &MilSourceColor);
MbufAllocColor(MilSystem, 3, ImageWidth, ImageHeight,
8+M_UNSIGNED, M_IMAGE+M_PROC+M_DISP, &MilSharpen);
MbufAllocColor(MilSystem, 3, ImageWidth, ImageHeight,
8+M_UNSIGNED, M_IMAGE+M_PROC+M_DISP, &MilProcessedImage);
MbufAlloc2d(MilSystem, 12L, 1L, 32L+M_FLOAT, M_ARRAY, &MilWarpParamBuffer);
MbufAlloc2d(MilSystem, 3L, 3L, 32L+M_FLOAT, M_ARRAY, &MilWarpCoeffBuffer);
MbufCopy(MilBayer, MilProcessedImage);
MdispSelect(MilDisplay, MilProcessedImage);
MosPrintf(MIL_TEXT("GPU acceleration example:\n\n"));
MosPrintf(MIL_TEXT("A raw Bayer image was loaded on the GPU.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MosPrintf(MIL_TEXT("Processing done on each frame:\n"));
MosPrintf(MIL_TEXT("1-Bayer conversion.\n2-Convolve (3x3)\n3-Warp polynomial.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to stop.\n\n"));
MappTimer(M_DEFAULT, M_TIMER_RESET+M_SYNCHRONOUS, M_NULL);
while (!MosKbhit())
{
MbufBayer(MilBayer, MilSourceColor, M_DEFAULT, M_BAYER_GB);
MimConvolve(MilSourceColor, MilSharpen, M_SHARPEN_8);
ComputeWarpParameters(WarpParamArray, ImageWidth, ImageHeight);
MbufPut1d(MilWarpParamBuffer, 0L, 12L, WarpParamArray);
MgenWarpParameter(MilWarpParamBuffer, MilWarpCoeffBuffer, M_NULL,
M_WARP_4_CORNER_REVERSE, M_DEFAULT, M_NULL, M_NULL);
MgenWarpParameter(MilWarpCoeffBuffer, MilWarpCoeffBuffer, M_NULL,
M_WARP_POLYNOMIAL, M_TRANSLATE, -(ImageWidth/2.0), -(ImageHeight/2.0));
MgenWarpParameter(MilWarpCoeffBuffer, MilWarpCoeffBuffer, M_NULL,
M_WARP_POLYNOMIAL, M_ROTATE, Rotation+=ROTATION_RATE, M_NULL);
MgenWarpParameter(MilWarpCoeffBuffer, MilWarpCoeffBuffer, M_NULL,
M_WARP_POLYNOMIAL, M_TRANSLATE, (ImageWidth/2.0), (ImageHeight/2.0));
ZoomFactor += ComputeZoomFactor(ZoomFactor);
MgenWarpParameter(MilWarpCoeffBuffer, MilWarpCoeffBuffer, M_NULL,
M_WARP_POLYNOMIAL, M_SCALE, ZoomFactor, ZoomFactor);
MgenWarpParameter(MilWarpCoeffBuffer, MilWarpCoeffBuffer, M_NULL, M_WARP_POLYNOMIAL,
M_TRANSLATE, (ImageWidth/2)-(ImageWidth*(ZoomFactor/2)),
(ImageHeight/2)-(ImageHeight*(ZoomFactor/2)));
MimWarp(MilSharpen, MilProcessedImage, MilWarpCoeffBuffer,
M_NULL, M_WARP_POLYNOMIAL, M_BILINEAR+M_OVERSCAN_CLEAR);
MappTimer(M_DEFAULT, M_TIMER_READ+M_SYNCHRONOUS, &Time);
NbLoop++;
FramesPerSecond = NbLoop/Time;
MosPrintf(MIL_TEXT("Processing speed: %.0f Images/Sec.\r"), FramesPerSecond);
}
MbufFree(MilSharpen);
MbufFree(MilBayer);
MbufFree(MilSourceColor);
MbufFree(MilProcessedImage);
MbufFree(MilWarpCoeffBuffer);
MbufFree(MilWarpParamBuffer);
MdispFree(MilDisplay);
MsysFree(MilSystem);
MappFree(MilApplication);
}
void ComputeWarpParameters(float* WarpParamArray, MIL_INT ImageWidth, MIL_INT ImageHeight)
{
static MIL_INT PhaseCnt = 0;
static bool FirstTrans = true;
WarpParamArray[8] = 0.0;
WarpParamArray[9] = 0.0;
WarpParamArray[10] = (float)ImageWidth;
WarpParamArray[11] = (float)ImageHeight;
switch(PhaseCnt)
{
case 0:
WarpParamArray[0] = 0;
WarpParamArray[1] = 0;
WarpParamArray[2] = (float)ImageWidth ;
WarpParamArray[3] = 0;
WarpParamArray[4] = (float)ImageWidth;
WarpParamArray[5] = (float)ImageHeight;
WarpParamArray[6] = 0;
WarpParamArray[7] = (float)ImageHeight;
if (FirstTrans)
{
PhaseCnt = 1;
FirstTrans = false;
}
else
{
PhaseCnt = 3;
FirstTrans = true;
}
break;
case 1:
if ((WarpParamArray[0] >= (ImageWidth)/3))
{
PhaseCnt = 2;
}
WarpParamArray[0] += CORNER_FACTOR;
WarpParamArray[1] += CORNER_FACTOR;
WarpParamArray[2] = (float)ImageWidth;
WarpParamArray[3] = 0;
WarpParamArray[4] -= CORNER_FACTOR;
WarpParamArray[5] -= CORNER_FACTOR;
WarpParamArray[6] = 0;
WarpParamArray[7] = (float)ImageHeight;
break;
case 2:
if (WarpParamArray[0] <= 0)
{
PhaseCnt = 0;
}
WarpParamArray[0] -= CORNER_FACTOR;
WarpParamArray[1] -= CORNER_FACTOR;
WarpParamArray[2] = (float)ImageWidth;
WarpParamArray[3] = 0;
WarpParamArray[4] += CORNER_FACTOR ;
WarpParamArray[5] += CORNER_FACTOR;
WarpParamArray[6] = 0;
WarpParamArray[7] = (float)ImageHeight;
break;
case 3:
if ((WarpParamArray[2] <= (ImageWidth)-((ImageWidth)/3)))
{
PhaseCnt = 4;
}
WarpParamArray[0] = 0;
WarpParamArray[1] = 0;
WarpParamArray[2] -= CORNER_FACTOR;
WarpParamArray[3] += CORNER_FACTOR;
WarpParamArray[4] = (float)ImageWidth;
WarpParamArray[5] = (float)ImageHeight;
WarpParamArray[6] += CORNER_FACTOR;
WarpParamArray[7] -= CORNER_FACTOR;
break;
case 4:
if (WarpParamArray[2] >= (ImageWidth))
{
PhaseCnt = 0;
}
WarpParamArray[0] = 0;
WarpParamArray[1] = 0;
WarpParamArray[2] += CORNER_FACTOR;
WarpParamArray[3] -= CORNER_FACTOR;
WarpParamArray[4] = (float)ImageWidth;
WarpParamArray[5] = (float)ImageHeight;
WarpParamArray[6] -= CORNER_FACTOR;
WarpParamArray[7] += CORNER_FACTOR;
break;
}
}
inline MIL_DOUBLE ComputeZoomFactor(const MIL_DOUBLE &ZoomFactor)
{
static MIL_DOUBLE ZoomRate = ZOOM_RATE;
if(ZoomFactor <= MIN_ZOOM_FACTOR)
ZoomRate = ZOOM_RATE;
else if (ZoomFactor >= MAX_ZOOM_FACTOR)
ZoomRate = -ZOOM_RATE;
return ZoomRate;
}