using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Matrox.MatroxImagingLibrary;
namespace MBufCreate
{
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;
Console.Write("\nMIL BUFFER CREATION:\n");
Console.Write("--------------------\n\n");
Console.Write("This example shows how to use the MbufCreate functions\n");
Console.Write("to create a MIL buffer from the memory at a specified location\n");
Console.Write("by pointing to the address of user data or the identifier of an\n");
Console.Write("already existing MIL buffer.\n\n");
MIL.MappAlloc(MIL.M_NULL, MIL.M_DEFAULT, ref MilApplication);
MilSystem = MIL.M_DEFAULT_HOST;
MIL.MdispAlloc(MilSystem, MIL.M_DEFAULT, "M_DEFAULT", MIL.M_WINDOWED, ref MilDisplay);
MonochromeBufCreateExample(MilSystem, MilDisplay);
ColorPackedBufCreateExample(MilSystem, MilDisplay);
ColorPlanarBufCreateExample(MilSystem, MilDisplay);
MonochromeOnColorPackedBufCreateExample(MilSystem, MilDisplay);
MIL.MdispFree(MilDisplay);
MIL.MappFree(MilApplication);
}
private const double X_REF1 = -0.500;
private const double Y_REF1 = +0.002;
private const double DIM1 = +3.200;
static void MonochromeBufCreateExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilImage = MIL.M_NULL;
byte[] UserImageData = null;
GCHandle UserDataHandle;
MIL_INT i, x, y;
MIL_UINT Value;
Console.Write("- A monochrome MIL buffer was created by pointing to \n");
Console.Write(" the address of user data. The buffer was modified directly\n");
Console.Write(" using the user data pointer.\n\n");
UserImageData = new byte[IMAGE_SIZE_X * IMAGE_SIZE_Y];
UserDataHandle = GCHandle.Alloc(UserImageData, GCHandleType.Pinned);
MIL.MbufCreate2d(MilSystem, IMAGE_SIZE_X, IMAGE_SIZE_Y, 8 + MIL.M_UNSIGNED,
MIL.M_IMAGE + MIL.M_PROC + MIL.M_DISP, MIL.M_HOST_ADDRESS + MIL.M_PITCH,
MIL.M_DEFAULT, (ulong)UserDataHandle.AddrOfPinnedObject(), ref MilImage);
MIL.MbufControl(MilImage, MIL.M_LOCK, MIL.M_DEFAULT);
i = 0;
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);
UserImageData[i] = (byte)(Value);
i++;
}
}
MIL.MbufControl(MilImage, MIL.M_MODIFIED, MIL.M_DEFAULT);
MIL.MbufControl(MilImage, MIL.M_UNLOCK, MIL.M_DEFAULT);
MIL.MdispSelect(MilDisplay, MilImage);
Console.Write("Press <Enter> to continue.\n\n");
Console.ReadKey();
MIL.MbufFree(MilImage);
UserDataHandle.Free();
}
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 ColorPackedBufCreateExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilImage = MIL.M_NULL;
uint[] UserImageData = null;
GCHandle UserDataHandle;
IntPtr MilImageAddr = IntPtr.Zero;
MIL_INT x, y, i;
uint Value;
uint Value_BGR32;
Console.Write("- A 32-bit color packed MIL buffer was created by pointing to\n");
Console.Write(" the address of user data. The buffer was modified directly.\n");
Console.Write(" using the user data pointer.\n\n");
UserImageData = new uint[IMAGE_SIZE_X * IMAGE_SIZE_Y];
UserDataHandle = GCHandle.Alloc(UserImageData, GCHandleType.Pinned);
MilImageAddr = UserDataHandle.AddrOfPinnedObject();
MIL.MbufCreateColor(MilSystem, 3, 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,
MIL.M_HOST_ADDRESS + MIL.M_PITCH, MIL.M_DEFAULT,
ref MilImageAddr, ref MilImage);
MIL.MbufControl(MilImage, MIL.M_LOCK, MIL.M_DEFAULT);
i=0;
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));
UserImageData[i] = Value_BGR32;
i++;
}
}
MIL.MbufControl(MilImage, MIL.M_MODIFIED, MIL.M_DEFAULT);
MIL.MbufControl(MilImage, MIL.M_UNLOCK, MIL.M_DEFAULT);
MIL.MdispSelect(MilDisplay, MilImage);
Console.Write("Press <Enter> to continue.\n\n");
Console.ReadKey();
MIL.MbufFree(MilImage);
UserDataHandle.Free();
}
private const double X_REF3 = -0.7453;
private const double Y_REF3 = +0.1127;
private const double DIM3 = +0.0060;
static void ColorPlanarBufCreateExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilImage = MIL.M_NULL;
byte[][] UserImageData = new byte[3][];
GCHandle[] UserDataHandle = new GCHandle[3];
IntPtr[] UserBandPtr = new IntPtr[3];
MIL_INT x, y, i, b;
MIL_UINT Value;
MIL_INT[] ColorBand = new MIL_INT[] { MIL.M_RED, MIL.M_GREEN, MIL.M_BLUE };
Console.Write("- A 24-bit color planar MIL buffer was created by pointing to\n");
Console.Write(" the addresses of 3 user data arrays. The buffers were modified\n");
Console.Write(" directly using the user data pointers.\n\n");
for (b = 0; b < 3; b++)
{
UserImageData[b] = new byte[IMAGE_SIZE_X * IMAGE_SIZE_Y];
UserDataHandle[b] = GCHandle.Alloc(UserImageData[b], GCHandleType.Pinned);
UserBandPtr[b] = UserDataHandle[b].AddrOfPinnedObject();
}
MIL.MbufCreateColor(MilSystem, 3, IMAGE_SIZE_X, IMAGE_SIZE_Y, 8 + MIL.M_UNSIGNED,
MIL.M_IMAGE + MIL.M_PROC + MIL.M_DISP + MIL.M_PLANAR + MIL.M_RGB24,
MIL.M_HOST_ADDRESS + MIL.M_PITCH, MIL.M_DEFAULT, UserBandPtr, ref MilImage);
MIL.MbufControl(MilImage, MIL.M_LOCK, MIL.M_DEFAULT);
for (b = 0; b < 3; b++)
{
i = 0;
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);
UserImageData[b][i] = (byte)GetColorFromIndex(ColorBand[b], (MIL_INT)Value, 255);
i++;
}
}
}
MIL.MbufControl(MilImage, MIL.M_MODIFIED, MIL.M_DEFAULT);
MIL.MbufControl(MilImage, MIL.M_UNLOCK, MIL.M_DEFAULT);
MIL.MdispSelect(MilDisplay, MilImage);
Console.Write("Press <Enter> to continue.\n\n");
Console.ReadKey();
MIL.MbufFree(MilImage);
for (b = 0; b < 3; b++)
UserDataHandle[b].Free();
}
private static readonly string COLOR_IMAGE_FILE =
MIL.M_IMAGE_PATH + "BaboonRGB.mim";
private const int SOURCE_SIZE_X = 256;
private const int SOURCE_SIZE_Y = 256;
private const byte KEPT_BITS = 0x80;
private const int MAX_NB_EVENTS = SOURCE_SIZE_X * SOURCE_SIZE_Y;
private const double DISPLAY_ZOOM = 3.0;
private const byte SOURCE_RED = 128;
private const byte SOURCE_GREEN = 0;
private const byte SOURCE_BLUE = 0;
private const byte DEST_RED = 0;
private const byte DEST_GREEN = 255;
private const byte DEST_BLUE = 0;
private const int BGR2_MASK = 0X00FFFFFF;
static void MonochromeOnColorPackedBufCreateExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilGraList = MIL.M_NULL;
MIL_ID MilImage = MIL.M_NULL;
MIL_ID MilDestImage = MIL.M_NULL;
MIL_ID MilDispImage = MIL.M_NULL;
MIL_ID MilMonoImage = MIL.M_NULL;
MIL_UINT MonoColorValue;
MIL_ID MilEventResult = MIL.M_NULL;
MIL_INT NbEvents = 0;
MIL_INT[] EventX = null;
MIL_INT[] EventY = null;
Console.Write("- A 32-bit monochrome MIL buffer was created by pointing to\n");
Console.Write(" the identifier of a MIL packed color buffer. This was done to\n");
Console.Write(" use the newly created buffer with a function that requires \n");
Console.Write(" monochrome image buffers. In this example, the positions of\n");
Console.Write(" dark red pixels found using MimLocateEvent() are displayed in\n");
Console.Write(" green.\n\n");
MIL.MgraAllocList(MilSystem, MIL.M_DEFAULT, ref MilGraList);
MIL.MdispControl(MilDisplay, MIL.M_ASSOCIATED_GRAPHIC_LIST_ID, MilGraList);
MIL.MbufAllocColor(MilSystem, 3, SOURCE_SIZE_X * 2, SOURCE_SIZE_Y, 8 + MIL.M_UNSIGNED,
MIL.M_BGR32 + MIL.M_PACKED + MIL.M_IMAGE + MIL.M_PROC + MIL.M_DISP,
ref MilDispImage);
MIL.MbufChild2d(MilDispImage, 0, 0, SOURCE_SIZE_X, SOURCE_SIZE_Y, ref MilImage);
MIL.MbufChild2d(MilDispImage, SOURCE_SIZE_X, 0, SOURCE_SIZE_X, SOURCE_SIZE_Y,
ref MilDestImage);
MIL.MimAllocResult(MilSystem, MAX_NB_EVENTS, MIL.M_EVENT_LIST, ref MilEventResult);
EventX = new MIL_INT[MAX_NB_EVENTS];
EventY = new MIL_INT[MAX_NB_EVENTS];
MIL.MbufLoad(COLOR_IMAGE_FILE, MilImage);
MIL.MimArith(MilImage, KEPT_BITS, MilImage, MIL.M_AND_CONST);
MIL.MbufCopy(MilImage, MilDestImage);
MIL.MbufCreateColor(MilSystem, 1, MIL.M_DEFAULT, MIL.M_DEFAULT, 32 + MIL.M_UNSIGNED,
MIL.M_IMAGE + MIL.M_PROC, MIL.M_MIL_ID + MIL.M_PITCH, MIL.M_DEFAULT,
ref MilImage, ref MilMonoImage);
MonoColorValue = PackToBGR32(SOURCE_BLUE, SOURCE_GREEN, SOURCE_RED);
MIL.MimArith(MilMonoImage, BGR2_MASK, MilMonoImage, MIL.M_AND_CONST);
MIL.MimLocateEvent(MilMonoImage,MilEventResult, MIL.M_EQUAL, MonoColorValue, MIL.M_NULL);
MIL.MimGetResult(MilEventResult, MIL.M_NB_EVENT, ref NbEvents);
MIL.MimGetResult(MilEventResult, MIL.M_POSITION_X + MIL.M_TYPE_MIL_INT, EventX);
MIL.MimGetResult(MilEventResult, MIL.M_POSITION_Y + MIL.M_TYPE_MIL_INT, EventY);
MIL.MgraColor(MIL.M_DEFAULT, MIL.M_RGB888(DEST_RED, DEST_GREEN, DEST_BLUE));
MIL.MgraControl(MIL.M_DEFAULT, MIL.M_DRAW_OFFSET_X, -SOURCE_SIZE_X);
MIL.MgraDots(MIL.M_DEFAULT, MilGraList, NbEvents, EventX, EventY, MIL.M_DEFAULT);
MIL.MdispZoom(MilDisplay, DISPLAY_ZOOM, DISPLAY_ZOOM);
MIL.MdispSelect(MilDisplay, MilDispImage);
Console.Write("Press <Enter> to end.\n\n");
Console.ReadKey();
MIL.MdispZoom(MilDisplay, 1.0, 1.0);
int Val = 0;
MIL.MgraInquireList(MilGraList, MIL.M_GRAPHIC_INDEX(0), MIL.M_DEFAULT, MIL.M_POSITION_X + MIL.M_TYPE_MIL_INT32, ref Val);
MIL.MbufFree(MilMonoImage);
MIL.MimFree(MilEventResult);
MIL.MbufFree(MilDestImage);
MIL.MbufFree(MilImage);
MIL.MbufFree(MilDispImage);
MIL.MgraFree(MilGraList);
}
static double Remap(double pos, double size, double min, double max)
{
return ((((max) - (min)) / (size)) * (pos) + (min));
}
static 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;
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;
}
}
}