#include <mil.h>
#define FUNCTION_NB_PARAM 4
#define FUNCTION_OPCODE_ADD_CONSTANT 1
#define FUNCTION_PARAMETER_ERROR_CODE 1
#define FUNCTION_SUPPORTED_IMAGE_TYPE (8+M_UNSIGNED)
#define IMAGE_FILE M_IMAGE_PATH MIL_TEXT("BoltsNutsWashers.mim")
MIL_INT AddConstant(MIL_ID SrcImageId, MIL_ID DstImageId, MIL_INT ConstantToAdd);
void MFTYPE SlaveAddConstant(MIL_ID Func);
MIL_INT AddConstant(MIL_ID SrcImageId, MIL_ID DstImageId, MIL_INT ConstantToAdd)
{
MIL_ID Func;
MIL_INT SlaveReturnValue = 0;
MfuncAlloc(MIL_TEXT("AddConstant"),
FUNCTION_NB_PARAM,
SlaveAddConstant, M_NULL, M_NULL,
M_USER_MODULE_1+FUNCTION_OPCODE_ADD_CONSTANT,
M_LOCAL+M_SYNCHRONOUS_FUNCTION,
&Func
);
MfuncParamMilId( Func, 1, SrcImageId, M_IMAGE, M_IN);
MfuncParamMilId( Func, 2, DstImageId, M_IMAGE, M_OUT);
MfuncParamMilInt(Func, 3, ConstantToAdd);
MfuncParamDataPointer(Func, 4, &SlaveReturnValue, sizeof(MIL_INT), M_OUT);
MfuncCall(Func);
MfuncFree(Func);
return SlaveReturnValue;
}
void MFTYPE SlaveAddConstant(MIL_ID Func)
{
MIL_ID SrcImageId, DstImageId;
MIL_INT ConstantToAdd, TempValue;
unsigned char *SrcImageDataPtr, *DstImageDataPtr;
MIL_INT SrcImageSizeX, SrcImageSizeY, SrcImageType, SrcImagePitchByte;
MIL_INT DstImageSizeX, DstImageSizeY, DstImageType, DstImagePitchByte;
MIL_INT x, y;
MIL_INT *SlaveReturnValuePtr;
MfuncParamValue(Func, 1, &SrcImageId);
MfuncParamValue(Func, 2, &DstImageId);
MfuncParamValue(Func, 3, &ConstantToAdd);
MfuncParamValue(Func, 4, &SlaveReturnValuePtr);
MbufControl(SrcImageId, M_LOCK, M_DEFAULT);
MbufControl(DstImageId, M_LOCK, M_DEFAULT);
MbufInquire(SrcImageId, M_HOST_ADDRESS, &SrcImageDataPtr);
MbufInquire(SrcImageId, M_SIZE_X, &SrcImageSizeX);
MbufInquire(SrcImageId, M_SIZE_Y, &SrcImageSizeY);
MbufInquire(SrcImageId, M_TYPE, &SrcImageType);
MbufInquire(SrcImageId, M_PITCH_BYTE, &SrcImagePitchByte);
MbufInquire(DstImageId, M_HOST_ADDRESS, &DstImageDataPtr);
MbufInquire(DstImageId, M_SIZE_X, &DstImageSizeX);
MbufInquire(DstImageId, M_SIZE_Y, &DstImageSizeY);
MbufInquire(DstImageId, M_TYPE, &DstImageType);
MbufInquire(DstImageId, M_PITCH_BYTE, &DstImagePitchByte);
if (SrcImageSizeX < DstImageSizeX) DstImageSizeX = SrcImageSizeX;
if (SrcImageSizeY < DstImageSizeY) DstImageSizeY = SrcImageSizeY;
if ((SrcImageType == DstImageType) && (SrcImageType == FUNCTION_SUPPORTED_IMAGE_TYPE) &&
(SrcImageDataPtr != M_NULL) && (DstImageDataPtr != M_NULL)
)
{
for (y= 0; y < DstImageSizeY; y++)
{
for (x= 0; x < DstImageSizeX; x++)
{
TempValue = (MIL_INT)SrcImageDataPtr[x] + (MIL_INT)ConstantToAdd;
if (TempValue <= 0xff)
DstImageDataPtr[x] = (unsigned char)TempValue;
else
DstImageDataPtr[x] = 0xff;
}
SrcImageDataPtr += SrcImagePitchByte;
DstImageDataPtr += DstImagePitchByte;
}
*SlaveReturnValuePtr = M_NULL;
}
else
{
MfuncErrorReport(Func,M_FUNC_ERROR+FUNCTION_PARAMETER_ERROR_CODE,
MIL_TEXT("Invalid parameter."),
MIL_TEXT("Image type not supported or invalid target system."),
MIL_TEXT("Image must be 8-bit and have a valid host address."),
M_NULL
);
*SlaveReturnValuePtr = M_FUNC_ERROR+FUNCTION_PARAMETER_ERROR_CODE;
}
MbufControl(SrcImageId, M_UNLOCK, M_DEFAULT);
MbufControl(DstImageId, M_UNLOCK, M_DEFAULT);
MbufControl(DstImageId, M_MODIFIED, M_DEFAULT);
}
int MosMain(void)
{
MIL_ID MilApplication,
MilSystem,
MilDisplay,
MilImage;
MIL_INT ReturnValue;
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL);
MbufAlloc2d(MilSystem,
MbufDiskInquire(IMAGE_FILE, M_SIZE_X, M_NULL),
MbufDiskInquire(IMAGE_FILE, M_SIZE_Y, M_NULL),
8+M_UNSIGNED,
M_IMAGE+M_DISP+M_HOST_MEMORY,
&MilImage);
MbufLoad(IMAGE_FILE, MilImage);
MdispSelect(MilDisplay, MilImage);
MosPrintf(MIL_TEXT("\nMIL FUNCTION DEVELOPER'S TOOLKIT:\n"));
MosPrintf(MIL_TEXT("---------------------------------\n\n"));
MosPrintf(MIL_TEXT("This example creates a custom MIL function that processes\n"));
MosPrintf(MIL_TEXT("an image using its data pointer directly.\n\n"));
MosPrintf(MIL_TEXT("Target image was loaded.\n"));
MosPrintf(MIL_TEXT("Press a key to continue.\n\n"));
MosGetch();
if (MsysInquire(MilSystem, M_LOCATION, M_NULL) == M_LOCAL)
{
ReturnValue = AddConstant(MilImage, MilImage, 0x40);
if (ReturnValue == M_NULL)
MosPrintf(MIL_TEXT("The white level of the image was augmented.\n"));
else
MosPrintf(MIL_TEXT("An error was returned by the Slave function.\n"));
}
else
{
MosPrintf(MIL_TEXT("This example doesn't run with Distributed MIL.\n"));
}
MosPrintf(MIL_TEXT("Press a key to terminate.\n\n"));
MosGetch();
MbufFree(MilImage);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return 0;
}