using System;
using System.Collections.Generic;
using System.Text;
using Matrox.MatroxImagingLibrary;
namespace MBufPointerAccess
{
class Program
{
private const int IMAGE_SIZE_X = 512;
private const int IMAGE_SIZE_Y = 512;
static void Main(string[] args)
{
MIL_ID MilApplication = MIL.M_NULL;
MIL_ID MilSystem = MIL.M_NULL;
MIL_ID MilDisplay = MIL.M_NULL;
MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, ref MilDisplay, MIL.M_NULL, MIL.M_NULL);
MonoBufferPointerAccessExample(MilSystem, MilDisplay);
ColorPackedBufferPointerAccessExample(MilSystem, MilDisplay);
ColorPlanarBufferPointerAccessExample(MilSystem, MilDisplay);
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL);
}
private const double X_REF1 = -0.500;
private const double Y_REF1 = +0.001;
private const double DIM1 = +3.200;
static void MonoBufferPointerAccessExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilImage = MIL.M_NULL;
MIL_INT MilImagePtr = MIL.M_NULL;
MIL_INT MilImagePitch = MIL.M_NULL;
MIL_INT x, y;
MIL_UINT Value;
Console.Write("- The data of a 8bits monochrome MIL buffer is modified\n");
Console.Write(" using its pointer to directly access the memory.\n\n");
MIL.MbufAlloc2d(MilSystem, IMAGE_SIZE_X, IMAGE_SIZE_Y, 8 + MIL.M_UNSIGNED,
MIL.M_IMAGE + MIL.M_PROC + MIL.M_DISP, ref MilImage);
MIL.MbufControl(MilImage, MIL.M_LOCK, MIL.M_DEFAULT);
MIL.MbufInquire(MilImage, MIL.M_HOST_ADDRESS, ref MilImagePtr);
MIL.MbufInquire(MilImage, MIL.M_PITCH, ref MilImagePitch);
if (MilImagePtr != MIL.M_NULL)
{
unsafe
{
IntPtr MilImagePtrIntPtr = MilImagePtr;
byte* MilImageAddr = (byte*)MilImagePtrIntPtr;
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);
MilImageAddr[x] = (byte)(Value);
}
MilImageAddr += MilImagePitch;
}
MIL.MbufControl(MilImage, MIL.M_MODIFIED, MIL.M_DEFAULT);
MIL.MbufControl(MilImage, MIL.M_UNLOCK, MIL.M_DEFAULT);
MIL.MdispSelect(MilDisplay, MilImage);
}
}
else
{
Console.Write("The source buffer has no accessible memory\n");
Console.Write("address on this specific system. Try changing\n");
Console.Write("the system in the MIL Config utility.\n\n");
}
Console.Write("Press <Enter> to continue.\n\n");
Console.ReadKey();
MIL.MbufFree(MilImage);
}
private const double X_REF2 = -1.1355;
private const double Y_REF2 = -0.2510;
private const double DIM2 = +0.1500;
static uint PackToBGR32(Byte b, Byte g, Byte r)
{
return ((uint)b | (uint)(g << 8) | (uint)(r << 16));
}
static void ColorPackedBufferPointerAccessExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilImage = MIL.M_NULL;
MIL_INT MilImagePtr = MIL.M_NULL;
MIL_INT MilImagePitch = MIL.M_NULL;
MIL_INT x, y, NbBand = 3;
MIL_UINT Value;
uint Value_BGR32;
Console.Write("- The data of a 32bits color packed MIL buffer is modified\n");
Console.Write(" using its pointer to directly access the memory.\n\n");
MIL.MbufAllocColor(MilSystem, NbBand, IMAGE_SIZE_X, IMAGE_SIZE_Y, 8 + MIL.M_UNSIGNED,
MIL.M_IMAGE + MIL.M_PROC + MIL.M_DISP + MIL.M_BGR32 + MIL.M_PACKED, ref MilImage);
MIL.MbufControl(MilImage, MIL.M_LOCK, MIL.M_DEFAULT);
MIL.MbufInquire(MilImage, MIL.M_HOST_ADDRESS, ref MilImagePtr);
MIL.MbufInquire(MilImage, MIL.M_PITCH, ref MilImagePitch);
if (MilImagePtr != MIL.M_NULL)
{
unsafe
{
IntPtr MilImagePtrIntPtr = MilImagePtr;
uint* MilImageAddr = (uint*)MilImagePtrIntPtr;
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(
(byte)GetColorFromIndex(MIL.M_BLUE, (MIL_INT)Value, 255),
(byte)GetColorFromIndex(MIL.M_GREEN, (MIL_INT)Value, 255),
(byte)GetColorFromIndex(MIL.M_RED, (MIL_INT)Value, 255)
);
MilImageAddr[x] = (uint)Value_BGR32;
}
MilImageAddr += MilImagePitch;
}
MIL.MbufControl(MilImage, MIL.M_MODIFIED, MIL.M_DEFAULT);
MIL.MbufControl(MilImage, MIL.M_UNLOCK, MIL.M_DEFAULT);
MIL.MdispSelect(MilDisplay, MilImage);
}
}
else
{
Console.Write("The source buffer has no accessible memory\n");
Console.Write("address on this specific system. Try changing\n");
Console.Write("the system in the MIL Config utility.\n\n");
}
Console.Write("Press <Enter> to continue.\n\n");
Console.ReadKey();
MIL.MbufFree(MilImage);
}
private const double X_REF3 = -0.7453;
private const double Y_REF3 = +0.1127;
private const double DIM3 = +0.0060;
static void ColorPlanarBufferPointerAccessExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilImage = MIL.M_NULL;
MIL_ID MilImageBand = MIL.M_NULL;
MIL_INT MilImageBandPtr = MIL.M_NULL;
MIL_INT MilImagePitch = MIL.M_NULL;
MIL_INT x, y, i, NbBand = 3;
MIL_UINT Value;
MIL_INT[] ColorBand = new MIL_INT[] { MIL.M_RED, MIL.M_GREEN, MIL.M_BLUE };
Console.Write("- The data of a 24bits color planar MIL buffer is modified using\n");
Console.Write(" each color band pointer's to directly access the memory.\n\n");
MIL.MbufAllocColor(MilSystem, NbBand, IMAGE_SIZE_X, IMAGE_SIZE_Y, 8 + MIL.M_UNSIGNED,
MIL.M_IMAGE + MIL.M_PROC + MIL.M_DISP + MIL.M_PLANAR, ref MilImage);
MIL.MbufInquire(MilImage, MIL.M_PITCH, ref MilImagePitch);
MIL.MbufControl(MilImage, MIL.M_LOCK, MIL.M_DEFAULT);
MIL.MbufChildColor(MilImage, MIL.M_RED, ref MilImageBand);
MIL.MbufInquire(MilImageBand, MIL.M_HOST_ADDRESS, ref MilImageBandPtr);
MIL.MbufFree(MilImageBand);
if (MilImageBandPtr != MIL.M_NULL)
{
unsafe
{
for (i = 0; i < NbBand; i++)
{
MIL.MbufChildColor(MilImage, (MIL_INT)ColorBand[i], ref MilImageBand);
MIL.MbufInquire(MilImageBand, MIL.M_HOST_ADDRESS, ref MilImageBandPtr);
IntPtr MilImageBandPtrIntPtr = MilImageBandPtr;
byte* MilImageBandAddr = (byte*)MilImageBandPtrIntPtr;
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);
MilImageBandAddr[x] = (byte)GetColorFromIndex(ColorBand[i], (MIL_INT)Value, 255);
}
MilImageBandAddr += MilImagePitch;
}
MIL.MbufFree(MilImageBand);
}
}
MIL.MbufControl(MilImage, MIL.M_MODIFIED, MIL.M_DEFAULT);
MIL.MbufControl(MilImage, MIL.M_UNLOCK, MIL.M_DEFAULT);
MIL.MdispSelect(MilDisplay, MilImage);
}
else
{
Console.Write("The source buffer has no accessible memory\n");
Console.Write("address on this specific system. Try changing\n");
Console.Write("the system in the MIL Config utility.\n\n");
}
Console.Write("Press <Enter> to continue.\n\n");
Console.ReadKey();
MIL.MbufFree(MilImage);
}
static double Remap(double pos, double size, double min, double max)
{
return ((((max) - (min)) / (size)) * (pos) + (min));
}
static MIL_UINT Mandelbrot(MIL_INT PosX, MIL_INT PosY, double RefX,
double RefY, double Dim)
{
const int maxIter = 256;
double xMin = RefX - 0.5 * Dim;
double xMax = RefX + 0.5 * Dim;
double yMin = RefY - 0.5 * Dim;
double yMax = RefY + 0.5 * Dim;
double x0 = Remap((double)PosX, (double)IMAGE_SIZE_X, xMin, xMax);
double y0 = Remap((double)PosY, (double)IMAGE_SIZE_Y, yMin, yMax);
double x = 0.0;
double y = 0.0;
MIL_UINT Iter = 0;
while ((x * x + y * y < 4) && (Iter < maxIter))
{
double Temp = x * x - y * y + x0;
y = 2 * x * y + y0;
x = Temp;
Iter++;
}
if (Iter > 255)
return 255;
else
return Iter;
}
static MIL_INT GetColorFromIndex(MIL_INT Band, MIL_INT Index, MIL_INT MaxIndex)
{
MIL_INT[] Segments = { };
MIL_INT[] SegmentsR = { 0, 0, 0, 255, 255, 128 };
MIL_INT[] SegmentsG = { 0, 0, 255, 255, 0, 0 };
MIL_INT[] SegmentsB = { 128, 255, 255, 0, 0, 0 };
switch ((int)Band)
{
case MIL.M_RED:
Segments = SegmentsR;
break;
case MIL.M_GREEN:
Segments = SegmentsG;
break;
case MIL.M_BLUE:
Segments = SegmentsB;
break;
}
double RemapedIndex = Index * MaxIndex / 256.0;
MIL_INT SegmentIndex = (MIL_INT)(RemapedIndex * 5.0 / 256.0);
double Slope = (Segments[SegmentIndex + 1] - Segments[SegmentIndex]) / (256.0 / 5.0);
double Offset = (Segments[SegmentIndex] - Slope * SegmentIndex * 256.0 / 5.0);
MIL_INT Value = (MIL_INT)(Slope * RemapedIndex + Offset + 0.5);
return Value;
}
}
}