#include <mil.h>
#define IMAGE_FILE M_IMAGE_PATH MIL_TEXT("/Preprocessing/ColorChartBayerRaw.mim")
#define WHITE_POSITION_X 195
#define WHITE_POSITION_Y 280
#define WHITE_SIZE_X 45
#define WHITE_SIZE_Y 45
#define DISPLAY_SPACING_X 10
#define DISPLAY_SPACING_Y 30
#define DISPLAY_CHILD_OFFSET_X 100
#define DISPLAY_CHILD_OFFSET_Y 145
#define DISPLAY_ZOOM_FACTOR 1.6
#define DISPLAY_TITLE_0 MIL_TEXT("Original Bayer raw")
#define DISPLAY_TITLE_1 MIL_TEXT("Average 2x2 demosaicing")
#define DISPLAY_TITLE_2 MIL_TEXT("Bilinear demosaicing")
#define DISPLAY_TITLE_3 MIL_TEXT("Adaptive demosaicing")
void DisplayBayerToRGB(MIL_ID SourceId,
MIL_ID ChildId,
MIL_INT ChildPosX,
MIL_INT ChildPosY,
MIL_INT ChildSizeX,
MIL_INT ChildSizeY,
MIL_ID DisplayId,
MIL_CONST_TEXT_PTR DisplayTitle,
MIL_INT DisplayPosX,
MIL_INT DisplayPosY);
int MosMain(void)
{
MIL_ID MilApplication,
MilSystem,
MilDisplayRaw,
MilOverlay,
MilDisplay1,
MilDisplay2,
MilDisplay3,
MilDisplayBuffer1,
MilDisplayBuffer2,
MilDisplayBuffer3,
MilCoefWB,
MilBayerRaw,
MilChildBayerRaw,
MilDestination;
MIL_INT RawSizeX,
RawSizeY,
ChildSizeX,
ChildSizeY,
DisplaySizeY;
MosPrintf(MIL_TEXT("\nBAYER TO RGB CONVERSION:\n"));
MosPrintf(MIL_TEXT("------------------------\n\n"));
MosPrintf(MIL_TEXT("This program converts a single-band Bayer color-encoded image into ")
MIL_TEXT("a 3-band\nimage using three different demosaicing algorithms.\n\n"));
MosPrintf(MIL_TEXT("The white region, which is displayed in red, is used\n")
MIL_TEXT("to perform the white balancing.\n\n"));
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, M_NULL, M_NULL, M_NULL);
MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_WINDOWED, &MilDisplayRaw);
MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_WINDOWED, &MilDisplay1);
MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_WINDOWED, &MilDisplay2);
MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_WINDOWED, &MilDisplay3);
MbufRestore(IMAGE_FILE, MilSystem, &MilBayerRaw);
MbufChild2d(MilBayerRaw, WHITE_POSITION_X, WHITE_POSITION_Y, WHITE_SIZE_X, WHITE_SIZE_Y, &MilChildBayerRaw);
MbufAlloc1d(MilSystem, 3, 32 + M_FLOAT, M_ARRAY, &MilCoefWB);
MbufBayer(MilChildBayerRaw, M_NULL, MilCoefWB, M_BAYER_GB + M_WHITE_BALANCE_CALCULATE);
MbufInquire(MilBayerRaw, M_SIZE_X, &RawSizeX);
MbufInquire(MilBayerRaw, M_SIZE_Y, &RawSizeY);
MbufAllocColor(MilSystem, 3, RawSizeX, RawSizeY, 8 + M_UNSIGNED, M_IMAGE + M_PROC + M_DISP, &MilDestination);
DisplaySizeY = (RawSizeY - (2 * DISPLAY_SPACING_Y)) / 3;
ChildSizeX = (MIL_INT) (RawSizeX / DISPLAY_ZOOM_FACTOR);
ChildSizeY = (MIL_INT) (DisplaySizeY / DISPLAY_ZOOM_FACTOR);
MbufAllocColor(MilSystem, 3, ChildSizeX, ChildSizeY,
8 + M_UNSIGNED, M_IMAGE + M_PROC + M_DISP, &MilDisplayBuffer1);
MbufAllocColor(MilSystem, 3, ChildSizeX, ChildSizeY,
8 + M_UNSIGNED, M_IMAGE + M_PROC + M_DISP, &MilDisplayBuffer2);
MbufAllocColor(MilSystem, 3, ChildSizeX, ChildSizeY,
8 + M_UNSIGNED, M_IMAGE + M_PROC + M_DISP, &MilDisplayBuffer3);
MdispControl(MilDisplayRaw, M_OVERLAY, M_ENABLE);
MdispControl(MilDisplayRaw, M_TITLE, M_PTR_TO_DOUBLE(DISPLAY_TITLE_0));
MdispSelect(MilDisplayRaw, MilBayerRaw);
MdispInquire(MilDisplayRaw, M_OVERLAY_ID, &MilOverlay);
MgraColor(M_DEFAULT, M_COLOR_RED);
MgraRect(M_DEFAULT, MilOverlay, WHITE_POSITION_X, WHITE_POSITION_Y, WHITE_POSITION_X+WHITE_SIZE_X, WHITE_POSITION_Y+WHITE_SIZE_Y);
MgraColor(M_DEFAULT, M_COLOR_GREEN);
MgraRect(M_DEFAULT, MilOverlay, DISPLAY_CHILD_OFFSET_X, DISPLAY_CHILD_OFFSET_Y, DISPLAY_CHILD_OFFSET_X + ChildSizeX, DISPLAY_CHILD_OFFSET_Y + ChildSizeY);
MbufBayer(MilBayerRaw, MilDestination, MilCoefWB, M_BAYER_GB + M_AVERAGE_2X2);
DisplayBayerToRGB(MilDestination, MilDisplayBuffer1, DISPLAY_CHILD_OFFSET_X, DISPLAY_CHILD_OFFSET_Y,
ChildSizeX, ChildSizeY, MilDisplay1, DISPLAY_TITLE_1, RawSizeX + DISPLAY_SPACING_X, 0);
MbufBayer(MilBayerRaw, MilDestination, MilCoefWB, M_BAYER_GB);
DisplayBayerToRGB(MilDestination, MilDisplayBuffer2, DISPLAY_CHILD_OFFSET_X, DISPLAY_CHILD_OFFSET_Y,
ChildSizeX, ChildSizeY, MilDisplay2, DISPLAY_TITLE_2, RawSizeX + DISPLAY_SPACING_X, DisplaySizeY + DISPLAY_SPACING_Y);
MbufBayer(MilBayerRaw, MilDestination, MilCoefWB, M_BAYER_GB + M_ADAPTIVE + M_COLOR_CORRECTION);
DisplayBayerToRGB(MilDestination, MilDisplayBuffer3, DISPLAY_CHILD_OFFSET_X, DISPLAY_CHILD_OFFSET_Y,
ChildSizeX, ChildSizeY, MilDisplay3, DISPLAY_TITLE_3, RawSizeX + DISPLAY_SPACING_X, 2 * (DisplaySizeY + DISPLAY_SPACING_Y));
MosPrintf(MIL_TEXT("Press <Enter> to end.\n"));
MosGetch();
MbufFree(MilDisplayBuffer1);
MbufFree(MilDisplayBuffer2);
MbufFree(MilDisplayBuffer3);
MbufFree(MilCoefWB);
MbufFree(MilChildBayerRaw);
MbufFree(MilBayerRaw);
MbufFree(MilDestination);
MdispFree(MilDisplayRaw);
MdispFree(MilDisplay1);
MdispFree(MilDisplay2);
MdispFree(MilDisplay3);
MappFreeDefault(MilApplication, MilSystem, M_NULL, M_NULL, M_NULL);
return 0;
}
void DisplayBayerToRGB(MIL_ID SourceId,
MIL_ID ChildId,
MIL_INT ChildPosX,
MIL_INT ChildPosY,
MIL_INT ChildSizeX,
MIL_INT ChildSizeY,
MIL_ID DisplayId,
MIL_CONST_TEXT_PTR DisplayTitle,
MIL_INT DisplayPosX,
MIL_INT DisplayPosY)
{
MbufCopyColor2d(SourceId, ChildId, M_ALL_BANDS, ChildPosX, ChildPosY, M_ALL_BANDS, 0, 0, ChildSizeX, ChildSizeY);
MdispControl(DisplayId, M_TITLE, M_PTR_TO_DOUBLE(DisplayTitle));
MdispControl(DisplayId, M_WINDOW_INITIAL_POSITION_X, DisplayPosX);
MdispControl(DisplayId, M_WINDOW_INITIAL_POSITION_Y, DisplayPosY);
MdispZoom(DisplayId, DISPLAY_ZOOM_FACTOR, DISPLAY_ZOOM_FACTOR);
MdispSelect(DisplayId, ChildId);
}