using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Matrox.MatroxImagingLibrary;
namespace MFunc
{
class Program
{
private const int FUNCTION_NB_PARAM = 4;
private const int FUNCTION_OPCODE_ADD_CONSTANT = 1;
private const int FUNCTION_PARAMETER_ERROR_CODE = 1;
private const int FUNCTION_SUPPORTED_IMAGE_TYPE = (8 + MIL.M_UNSIGNED);
private const string IMAGE_FILE = MIL.M_IMAGE_PATH + "BoltsNutsWashers.mim";
static MIL_INT AddConstant(MIL_ID SrcImageId, MIL_ID DstImageId, MIL_INT ConstantToAdd)
{
MIL_ID Func = MIL.M_NULL;
MIL_INT SlaveReturnValue = 0;
MFUNCFCTPTR SlaveAddConstantDelegate = new MFUNCFCTPTR(SlaveAddConstant);
MIL.MfuncAlloc("AddConstant",
FUNCTION_NB_PARAM,
SlaveAddConstantDelegate, MIL.M_NULL, MIL.M_NULL,
MIL.M_USER_MODULE_1 + FUNCTION_OPCODE_ADD_CONSTANT,
MIL.M_LOCAL + MIL.M_SYNCHRONOUS_FUNCTION,
ref Func);
MIL.MfuncParamMilId(Func, 1, SrcImageId, MIL.M_IMAGE, MIL.M_IN);
MIL.MfuncParamMilId(Func, 2, DstImageId, MIL.M_IMAGE, MIL.M_OUT);
MIL.MfuncParamMilInt(Func, 3, ConstantToAdd);
MIL_INT[] slaveReturnValueArray = new MIL_INT[1] { -1 };
GCHandle slaveReturnValueArrayHandle = GCHandle.Alloc(slaveReturnValueArray, GCHandleType.Pinned);
try
{
IntPtr slaveReturnValuePtr = slaveReturnValueArrayHandle.AddrOfPinnedObject();
MIL.MfuncParamDataPointer(Func, 4, slaveReturnValuePtr, MIL_INT.Size * 2, MIL.M_OUT);
MIL.MfuncCall(Func);
SlaveReturnValue = slaveReturnValueArray[0];
GC.KeepAlive(SlaveAddConstantDelegate);
}
finally
{
if (slaveReturnValueArrayHandle.IsAllocated)
{
slaveReturnValueArrayHandle.Free();
}
}
MIL.MfuncFree(Func);
return SlaveReturnValue;
}
static void SlaveAddConstant(MIL_ID Func)
{
MIL_ID SrcImageId = MIL.M_NULL;
MIL_ID DstImageId = MIL.M_NULL;
MIL_INT ConstantToAdd = 0;
MIL_INT TempValue = 0;
MIL_INT SrcImageDataPtr = MIL.M_NULL;
MIL_INT DstImageDataPtr = MIL.M_NULL;
MIL_INT SrcImageSizeX = 0;
MIL_INT SrcImageSizeY = 0;
MIL_INT SrcImageType = 0;
MIL_INT SrcImagePitchByte = 0;
MIL_INT DstImageSizeX = 0;
MIL_INT DstImageSizeY = 0;
MIL_INT DstImageType = 0;
MIL_INT DstImagePitchByte = 0;
int x = 0;
int y = 0;
IntPtr SlaveReturnValuePtr = IntPtr.Zero;
MIL.MfuncParamValue(Func, 1, ref SrcImageId);
MIL.MfuncParamValue(Func, 2, ref DstImageId);
MIL.MfuncParamValue(Func, 3, ref ConstantToAdd);
MIL.MfuncParamValue(Func, 4, ref SlaveReturnValuePtr);
MIL.MbufControl(SrcImageId, MIL.M_LOCK, MIL.M_DEFAULT);
MIL.MbufControl(DstImageId, MIL.M_LOCK, MIL.M_DEFAULT);
MIL.MbufInquire(SrcImageId, MIL.M_HOST_ADDRESS, ref SrcImageDataPtr);
MIL.MbufInquire(SrcImageId, MIL.M_SIZE_X, ref SrcImageSizeX);
MIL.MbufInquire(SrcImageId, MIL.M_SIZE_Y, ref SrcImageSizeY);
MIL.MbufInquire(SrcImageId, MIL.M_TYPE, ref SrcImageType);
MIL.MbufInquire(SrcImageId, MIL.M_PITCH_BYTE, ref SrcImagePitchByte);
MIL.MbufInquire(DstImageId, MIL.M_HOST_ADDRESS, ref DstImageDataPtr);
MIL.MbufInquire(DstImageId, MIL.M_SIZE_X, ref DstImageSizeX);
MIL.MbufInquire(DstImageId, MIL.M_SIZE_Y, ref DstImageSizeY);
MIL.MbufInquire(DstImageId, MIL.M_TYPE, ref DstImageType);
MIL.MbufInquire(DstImageId, MIL.M_PITCH_BYTE, ref DstImagePitchByte);
if (SrcImageSizeX < DstImageSizeX) DstImageSizeX = SrcImageSizeX;
if (SrcImageSizeY < DstImageSizeY) DstImageSizeY = SrcImageSizeY;
unsafe
{
if ((SrcImageType == DstImageType) && (SrcImageType == FUNCTION_SUPPORTED_IMAGE_TYPE) &&
(SrcImageDataPtr != MIL.M_NULL) && (DstImageDataPtr != MIL.M_NULL))
{
IntPtr SrcImageDataPtrIntPtr = SrcImageDataPtr;
IntPtr DstImageDataPtrIntPtr = DstImageDataPtr;
byte* SrcImageDataAddr = (byte*)SrcImageDataPtrIntPtr;
byte* DstImageDataAddr = (byte*)DstImageDataPtrIntPtr;
for (y = 0; y < DstImageSizeY; y++)
{
for (x = 0; x < DstImageSizeX; x++)
{
TempValue = (MIL_INT)SrcImageDataAddr[x] + (MIL_INT)ConstantToAdd;
if (TempValue <= 0xff)
{
DstImageDataAddr[x] = (byte)TempValue;
}
else
{
DstImageDataAddr[x] = 0xff;
}
}
SrcImageDataAddr += SrcImagePitchByte;
DstImageDataAddr += DstImagePitchByte;
}
MIL_INT.WriteMilInt(SlaveReturnValuePtr, MIL.M_NULL);
}
else
{
MIL.MfuncErrorReport(Func, MIL.M_FUNC_ERROR + FUNCTION_PARAMETER_ERROR_CODE,
"Invalid parameter.",
"Image type not supported or invalid target system.",
"Image depth must be 8-bit and have a valid host address.",
MIL.M_NULL);
MIL_INT.WriteMilInt(SlaveReturnValuePtr, MIL.M_FUNC_ERROR + FUNCTION_PARAMETER_ERROR_CODE);
}
}
MIL.MbufControl(SrcImageId, MIL.M_UNLOCK, MIL.M_DEFAULT);
MIL.MbufControl(DstImageId, MIL.M_UNLOCK, MIL.M_DEFAULT);
MIL.MbufControl(DstImageId, MIL.M_MODIFIED, MIL.M_DEFAULT);
}
static void Main(string[] args)
{
MIL_ID MilApplication = MIL.M_NULL;
MIL_ID MilSystem = MIL.M_NULL;
MIL_ID MilDisplay = MIL.M_NULL;
MIL_ID MilImage = MIL.M_NULL;
MIL_INT ReturnValue = 0;
MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, ref MilDisplay, MIL.M_NULL, MIL.M_NULL);
MIL.MbufAlloc2d(MilSystem,
MIL.MbufDiskInquire(IMAGE_FILE, MIL.M_SIZE_X),
MIL.MbufDiskInquire(IMAGE_FILE, MIL.M_SIZE_Y),
8 + MIL.M_UNSIGNED,
MIL.M_IMAGE + MIL.M_DISP + MIL.M_HOST_MEMORY,
ref MilImage);
MIL.MbufLoad(IMAGE_FILE, MilImage);
MIL.MdispSelect(MilDisplay, MilImage);
Console.WriteLine();
Console.WriteLine("MIL FUNCTION DEVELOPER'S TOOLKIT:");
Console.WriteLine("----------------------------------");
Console.WriteLine();
Console.WriteLine("This example creates a custom MIL function that processes");
Console.WriteLine("an image using its data pointer directly.");
Console.WriteLine();
Console.WriteLine("Target image was loaded.");
Console.WriteLine("Press a key to continue.");
Console.WriteLine();
Console.ReadKey();
if (MIL.MsysInquire(MilSystem, MIL.M_LOCATION, MIL.M_NULL) == MIL.M_LOCAL)
{
ReturnValue = AddConstant(MilImage, MilImage, 0x40);
if (ReturnValue == MIL.M_NULL)
{
Console.WriteLine("The white level of the image was augmented.");
}
else
{
Console.WriteLine("An error was returned by the Slave function.");
}
}
else
{
Console.WriteLine("This example doesn't run with Distributed MIL.");
}
Console.WriteLine("Press a key to terminate.");
Console.WriteLine();
Console.ReadKey();
MIL.MbufFree(MilImage);
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL);
}
}
}