#include <mil.h>
#define IMAGE_SIZE_X 512
#define IMAGE_SIZE_Y 512
void MonochromeBufferPointerAccessExample(MIL_ID MilSystem, MIL_ID MilDisplay);
void ColorPackedBufferPointerAccessExample(MIL_ID MilSystem, MIL_ID MilDisplay);
void ColorPlanarBufferPointerAccessExample(MIL_ID MilSystem, MIL_ID MilDisplay);
MIL_UINT Mandelbrot(MIL_INT PosX, MIL_INT PosY,
MIL_DOUBLE RefX, MIL_DOUBLE RefY, MIL_DOUBLE Dim);
MIL_UINT8 GetColorFromIndex(MIL_INT Band, MIL_INT Index, MIL_INT MaxIndex);
int MosMain(void)
{
MIL_ID MilApplication,
MilSystem,
MilDisplay;
MosPrintf(MIL_TEXT("\nMIL buffer pointer access example.\n"));
MosPrintf(MIL_TEXT("----------------------------------\n\n"));
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL);
MonochromeBufferPointerAccessExample(MilSystem, MilDisplay);
ColorPackedBufferPointerAccessExample(MilSystem, MilDisplay);
ColorPlanarBufferPointerAccessExample(MilSystem, MilDisplay);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return 0;
}
#define X_REF1 -0.500
#define Y_REF1 +0.002
#define DIM1 +3.200
void MonochromeBufferPointerAccessExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilImage;
MIL_UINT8* MilImagePtr;
MIL_INT MilImagePitch;
MIL_INT x, y;
MIL_UINT Value;
MosPrintf(MIL_TEXT("- The data of a 8bits monochrome MIL buffer is modified\n"));
MosPrintf(MIL_TEXT(" using its pointer to directly access the memory.\n\n"));
MbufAlloc2d(MilSystem, IMAGE_SIZE_X, IMAGE_SIZE_Y, 8 + M_UNSIGNED,
M_IMAGE + M_PROC + M_DISP, &MilImage);
MbufControl(MilImage, M_LOCK, M_DEFAULT);
MbufInquire(MilImage, M_HOST_ADDRESS, &MilImagePtr);
MbufInquire(MilImage, M_PITCH, &MilImagePitch);
if (MilImagePtr != M_NULL)
{
for (y = 0; y < IMAGE_SIZE_Y; y++)
{
for (x = 0; x < IMAGE_SIZE_X; x++)
{
Value = Mandelbrot(x, y, X_REF1, Y_REF1, DIM1);
MilImagePtr[x] = MIL_UINT8(Value);
}
MilImagePtr += MilImagePitch;
}
MbufControl(MilImage, M_MODIFIED, M_DEFAULT);
MbufControl(MilImage, M_UNLOCK, M_DEFAULT);
MdispSelect(MilDisplay, MilImage);
}
else
{
MosPrintf(MIL_TEXT("The source buffer has no accessible memory\n"));
MosPrintf(MIL_TEXT("address on this specific system. Try changing\n"));
MosPrintf(MIL_TEXT("the system in the MIL Config utility.\n\n"));
}
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MbufFree(MilImage);
};
#define X_REF2 -1.1355
#define Y_REF2 -0.2510
#define DIM2 +0.1500
MIL_UINT32 PackToBGR32(MIL_UINT8 b, MIL_UINT8 g, MIL_UINT8 r)
{
return ((MIL_UINT32)b | (MIL_UINT32)(g << 8) | (MIL_UINT32)(r << 16));
};
void ColorPackedBufferPointerAccessExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilImage;
MIL_UINT32* MilImagePtr;
MIL_INT MilImagePitch;
MIL_INT x, y, NbBand = 3;
MIL_UINT Value;
MIL_UINT32 Value_BGR32;
MosPrintf(MIL_TEXT("- The data of a 32bits color packed MIL buffer is modified\n"));
MosPrintf(MIL_TEXT(" using its pointer to directly access the memory.\n\n"));
MbufAllocColor(MilSystem, NbBand, IMAGE_SIZE_X, IMAGE_SIZE_Y, 8 + M_UNSIGNED,
M_IMAGE + M_PROC + M_DISP + M_BGR32 + M_PACKED, &MilImage);
MbufControl(MilImage, M_LOCK, M_DEFAULT);
MbufInquire(MilImage, M_HOST_ADDRESS, &MilImagePtr);
MbufInquire(MilImage, M_PITCH, &MilImagePitch);
if (MilImagePtr != M_NULL)
{
for (y = 0; y < IMAGE_SIZE_Y; y++)
{
for (x = 0; x < IMAGE_SIZE_X; x++)
{
Value = Mandelbrot(x, y, X_REF2, Y_REF2, DIM2);
Value_BGR32 = PackToBGR32(
GetColorFromIndex(M_BLUE, Value, 255),
GetColorFromIndex(M_GREEN, Value, 255),
GetColorFromIndex(M_RED, Value, 255)
);
MilImagePtr[x] = Value_BGR32;
}
MilImagePtr += MilImagePitch;
}
MbufControl(MilImage, M_MODIFIED, M_DEFAULT);
MbufControl(MilImage, M_UNLOCK, M_DEFAULT);
MdispSelect(MilDisplay, MilImage);
}
else
{
MosPrintf(MIL_TEXT("The source buffer has no accessible memory\n"));
MosPrintf(MIL_TEXT("address on this specific system. Try changing\n"));
MosPrintf(MIL_TEXT("the system in the MIL Config utility.\n\n"));
}
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MbufFree(MilImage);
};
#define X_REF3 -0.7453
#define Y_REF3 +0.1127
#define DIM3 +0.0060
void ColorPlanarBufferPointerAccessExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilImage,
MilImageBand;
MIL_UINT8* MilImageBandPtr;
MIL_INT MilImagePitch;
MIL_INT x, y, i, NbBand = 3;
MIL_UINT Value;
MIL_UINT ColorBand[3] = { M_RED, M_GREEN, M_BLUE };
MosPrintf(MIL_TEXT("- The data of a 24bits color planar MIL buffer is modified using\n"));
MosPrintf(MIL_TEXT(" each color band pointer's to directly access the memory.\n\n"));
MbufAllocColor(MilSystem, NbBand, IMAGE_SIZE_X, IMAGE_SIZE_Y, 8 + M_UNSIGNED,
M_IMAGE + M_PROC + M_DISP + M_PLANAR, &MilImage);
MbufInquire(MilImage, M_PITCH, &MilImagePitch);
MbufControl(MilImage, M_LOCK, M_DEFAULT);
MbufChildColor(MilImage, M_RED, &MilImageBand);
MbufInquire(MilImageBand, M_HOST_ADDRESS, &MilImageBandPtr);
MbufFree(MilImageBand);
if (MilImageBandPtr != M_NULL)
{
for (i = 0; i < NbBand; i++)
{
MbufChildColor(MilImage, ColorBand[i], &MilImageBand);
MbufInquire(MilImageBand, M_HOST_ADDRESS, &MilImageBandPtr);
for (y = 0; y < IMAGE_SIZE_Y; y++)
{
for (x = 0; x < IMAGE_SIZE_X; x++)
{
Value = Mandelbrot(x, y, X_REF3, Y_REF3, DIM3);
MilImageBandPtr[x] = GetColorFromIndex(ColorBand[i], Value, 255);
}
MilImageBandPtr += MilImagePitch;
}
MbufFree(MilImageBand);
}
MbufControl(MilImage, M_MODIFIED, M_DEFAULT);
MbufControl(MilImage, M_UNLOCK, M_DEFAULT);
MdispSelect(MilDisplay, MilImage);
}
else
{
MosPrintf(MIL_TEXT("The source buffer has no accessible memory\n"));
MosPrintf(MIL_TEXT("address on this specific system. Try changing\n"));
MosPrintf(MIL_TEXT("the system in the MIL Config utility.\n\n"));
}
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MbufFree(MilImage);
};
MIL_INT MinVal(MIL_INT a, MIL_INT b) { return (a > b) ? b : a; }
MIL_DOUBLE Remap(MIL_DOUBLE pos, MIL_DOUBLE size, MIL_DOUBLE min, MIL_DOUBLE max)
{
return ( (((max-min) / size) * pos) + min );
}
MIL_UINT Mandelbrot(MIL_INT PosX, MIL_INT PosY,
MIL_DOUBLE RefX, MIL_DOUBLE RefY, MIL_DOUBLE Dim)
{
const MIL_UINT maxIter = 256;
MIL_DOUBLE xMin = RefX - (0.5 * Dim);
MIL_DOUBLE xMax = RefX + (0.5 * Dim);
MIL_DOUBLE yMin = RefY - (0.5 * Dim);
MIL_DOUBLE yMax = RefY + (0.5 * Dim);
MIL_DOUBLE x0 = Remap((MIL_DOUBLE)PosX, (MIL_DOUBLE)IMAGE_SIZE_X, xMin, xMax);
MIL_DOUBLE y0 = Remap((MIL_DOUBLE)PosY, (MIL_DOUBLE)IMAGE_SIZE_Y, yMin, yMax);
MIL_DOUBLE x = 0.0;
MIL_DOUBLE y = 0.0;
MIL_UINT Iter = 0;
while (((x*x + y*y) < 4) && (Iter < maxIter))
{
MIL_DOUBLE Temp = x*x - y*y + x0;
y = 2 * x*y + y0;
x = Temp;
Iter++;
}
return MinVal(255, Iter);
}
MIL_UINT8 GetColorFromIndex(MIL_INT Band, MIL_INT Index, MIL_INT MaxIndex)
{
MIL_UINT8* Segments = M_NULL;
MIL_UINT8 SegmentsR[] = { 0, 0, 0, 255, 255, 128 };
MIL_UINT8 SegmentsG[] = { 0, 0, 255, 255, 0, 0 };
MIL_UINT8 SegmentsB[] = { 128, 255, 255, 0, 0, 0 };
switch (Band)
{
case M_RED:
Segments = SegmentsR;
break;
case M_GREEN:
Segments = SegmentsG;
break;
case M_BLUE:
Segments = SegmentsB;
break;
}
MIL_DOUBLE RemapedIndex = Index * MaxIndex / 256.0;
MIL_UINT8 SegmentIndex = MIL_UINT8(RemapedIndex * 5.0 / 256.0);
MIL_DOUBLE Slope = (Segments[SegmentIndex + 1] - Segments[SegmentIndex]) / (256.0 / 5.0);
MIL_DOUBLE Offset = (Segments[SegmentIndex] - (Slope * SegmentIndex * 256.0 / 5.0));
MIL_UINT8 Value = MIL_UINT8(Slope * RemapedIndex + Offset + 0.5);
return Value;
}