#include <mil.h>
#include <stdlib.h>
#define IMAGE_NAME MIL_TEXT("ArmsMono10bit.mim")
#define IMAGE_FILE M_IMAGE_PATH IMAGE_NAME
#define DRAW_LUT_SHAPE M_YES
void DrawLutShape(MIL_ID MilDisplay,
MIL_ID MilOriginalImage,
MIL_ID MilImage,
MIL_INT Start,
MIL_INT End,
MIL_INT InflexionIntensity,
MIL_INT ImageMaxValue,
MIL_INT DisplayMaxValue);
#define MosMin(a, b) (((a) < (b)) ? (a) : (b))
#define MosMax(a, b) (((a) > (b)) ? (a) : (b))
int MosMain(void)
{
MIL_ID MilApplication,
MilSystem,
MilDisplay,
MilImage,
MilOriginalImage = 0,
MilLut;
MIL_INT ImageSizeX, ImageSizeY, ImageMaxValue;
MIL_INT DisplaySizeBit, DisplayMaxValue;
MIL_INT Start, End, Step, InflectionLevel;
MIL_INT Ch;
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL);
MbufRestore(IMAGE_FILE, MilSystem, &MilImage);
MIL_ID MilExtremeResult = M_NULL;
MimAllocResult((MIL_ID)MbufInquire(MilImage, M_OWNER_SYSTEM, M_NULL),
1L, M_EXTREME_LIST, &MilExtremeResult);
MimFindExtreme(MilImage, MilExtremeResult, M_MAX_VALUE);
MimGetResult(MilExtremeResult, M_VALUE, &ImageMaxValue);
MimFree(MilExtremeResult);
MbufControl(MilImage, M_MAX, (MIL_DOUBLE)ImageMaxValue);
MdispSelect(MilDisplay, MilImage);
MdispInquire(MilDisplay, M_SIZE_BIT, &DisplaySizeBit);
DisplayMaxValue = (1<<DisplaySizeBit)-1;
MosPrintf(MIL_TEXT("\nINTERACTIVE WINDOW LEVELING:\n"));
MosPrintf(MIL_TEXT("----------------------------\n\n"));
MosPrintf(MIL_TEXT("Image name : %s\n"),IMAGE_NAME);
MosPrintf(MIL_TEXT("Image size : %ld x %ld\n"),
MbufInquire(MilImage, M_SIZE_X, &ImageSizeX),
MbufInquire(MilImage, M_SIZE_Y, &ImageSizeY));
MosPrintf(MIL_TEXT("Image max : %4ld\n"), ImageMaxValue);
MosPrintf(MIL_TEXT("Display max: %4ld\n\n"), DisplayMaxValue);
MbufAlloc1d(MilSystem, ImageMaxValue+1,
((DisplaySizeBit>8) ? 16 : 8)+M_UNSIGNED, M_LUT, &MilLut);
MgenLutRamp(MilLut, 0, 0, ImageMaxValue, (MIL_DOUBLE)DisplayMaxValue);
MbufControl(MilLut, M_MAX, (MIL_DOUBLE)DisplayMaxValue);
MdispLut(MilDisplay, MilLut);
MosPrintf(MIL_TEXT("Keys assignement:\n\n"));
MosPrintf(MIL_TEXT("Arrow keys : Left=move Left, Right=move Right, ")
MIL_TEXT("Down=Narrower, Up=Wider.\n"));
MosPrintf(MIL_TEXT("Intensity keys: L=Lower, U=Upper, R=Reset.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to end.\n\n"));
Ch = 0;
Start = 0;
End = ImageMaxValue;
InflectionLevel = DisplayMaxValue;
Step = (ImageMaxValue+1)/128;
Step = MosMax(Step,4);
while (Ch != '\r')
{
switch (Ch)
{
case 0x4B:
{ Start-=Step; End-=Step; break; }
case 0x4D:
{ Start+=Step; End+=Step; break; }
case 0x50:
{ Start+=Step; End-=Step; break; }
case 0x48:
{ Start-=Step, End+=Step; break; }
case 'L':
case 'l':
{ InflectionLevel--; break; }
case 'U':
case 'u':
{ InflectionLevel++; break; }
case 'R':
case 'r':
{ Start=0; End=ImageMaxValue; InflectionLevel=DisplayMaxValue; break; }
}
End = MosMin(End,ImageMaxValue);
Start = MosMin(Start,End);
End = MosMax(End,Start);
Start = MosMax(Start,0);
End = MosMax(End,0);
InflectionLevel = MosMax(InflectionLevel, 0);
InflectionLevel = MosMin(InflectionLevel, DisplayMaxValue);
MosPrintf(MIL_TEXT("Inflection points: Low=(%ld,0), High=(%ld,%ld). \r"),
Start, End, InflectionLevel);
MgenLutRamp(MilLut, 0, 0, Start, 0);
MgenLutRamp(MilLut, Start, 0, End, (MIL_DOUBLE)InflectionLevel);
MgenLutRamp(MilLut, End, (MIL_DOUBLE)InflectionLevel, ImageMaxValue,
(MIL_DOUBLE)DisplayMaxValue);
MdispLut(MilDisplay, MilLut);
if (DRAW_LUT_SHAPE)
{
if (!MilOriginalImage)
MbufRestore(IMAGE_FILE, MilSystem, &MilOriginalImage);
DrawLutShape(MilDisplay, MilOriginalImage, MilImage, Start, End,
InflectionLevel, ImageMaxValue, DisplayMaxValue);
}
if ((Ch = MosGetch()) == 0xE0)
Ch = MosGetch();
}
MosPrintf(MIL_TEXT("\n\n"));
MbufFree(MilLut);
MbufFree(MilImage);
if (MilOriginalImage)
MbufFree(MilOriginalImage);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return 0;
}
void DrawLutShape(MIL_ID MilDisplay,
MIL_ID MilOriginalImage,
MIL_ID MilImage,
MIL_INT Start,
MIL_INT End,
MIL_INT InflexionIntensity,
MIL_INT ImageMaxValue,
MIL_INT DisplayMaxValue)
{
MIL_INT ImageSizeX, ImageSizeY;
MIL_DOUBLE Xstart, Xend, Xstep, Ymin, Yinf, Ymax, Ystep;
MIL_TEXT_CHAR String[8];
MbufInquire(MilImage, M_SIZE_X, &ImageSizeX);
MbufInquire(MilImage, M_SIZE_Y, &ImageSizeY);
Xstep = (MIL_DOUBLE)ImageSizeX/(MIL_DOUBLE)ImageMaxValue;
Xstart = Start*Xstep;
Xend = End*Xstep;
Ystep = ((MIL_DOUBLE)ImageSizeY/4.0)/(MIL_DOUBLE)DisplayMaxValue;
Ymin = ((MIL_DOUBLE)ImageSizeY-2);
Yinf = Ymin-(InflexionIntensity*Ystep);
Ymax = Ymin-(DisplayMaxValue*Ystep);
MdispControl(MilDisplay, M_UPDATE, M_DISABLE);
MbufCopy(MilOriginalImage, MilImage);
MgraColor(M_DEFAULT, (MIL_DOUBLE)ImageMaxValue);
MgraText(M_DEFAULT, MilImage, 4, (MIL_INT)Ymin-22, MIL_TEXT("0"));
MosSprintf(String, 8, MIL_TEXT("%ld"), DisplayMaxValue);
MgraText(M_DEFAULT, MilImage, 4, (MIL_INT)Ymax-16, String);
MosSprintf(String, 8, MIL_TEXT("%ld"), ImageMaxValue);
MgraText(M_DEFAULT, MilImage, ImageSizeX-38 , (MIL_INT)Ymin-22, String);
MgraLine(M_DEFAULT, MilImage, 0, (MIL_INT)Ymin, (MIL_INT)Xstart, (MIL_INT)Ymin);
MgraLine(M_DEFAULT, MilImage, (MIL_INT)Xstart, (MIL_INT)Ymin,
(MIL_INT)Xend, (MIL_INT)Yinf);
MgraLine(M_DEFAULT, MilImage, (MIL_INT)Xend, (MIL_INT)Yinf,
ImageSizeX-1, (MIL_INT)Ymax);
MdispControl(MilDisplay, M_UPDATE, M_ENABLE);
}