'
'
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Matrox.MatroxImagingLibrary
Namespace MDispWindowLeveling
Friend Class Program
Private Const IMAGE_NAME As String = "ArmsMono10bit.mim"
Private Const IMAGE_FILE As String = MIL.M_IMAGE_PATH & IMAGE_NAME
Private Const DRAW_LUT_SHAPE As Integer = MIL.M_YES
Shared Sub Main(ByVal args() As String)
Dim MilApplication As MIL_ID = MIL.M_NULL
Dim MilSystem As MIL_ID = MIL.M_NULL
Dim MilDisplay As MIL_ID = MIL.M_NULL
Dim MilImage As MIL_ID = MIL.M_NULL
Dim MilOriginalImage As MIL_ID = MIL.M_NULL
Dim MilLut As MIL_ID = MIL.M_NULL
Dim ImageSizeX As MIL_INT = 0
Dim ImageSizeY As MIL_INT = 0
Dim ImageMaxValue As MIL_INT = 0
Dim DisplaySizeBit As MIL_INT = 0
Dim DisplayMaxValue As MIL_INT = 0
Dim Start As MIL_INT = 0
Dim LutEnd As MIL_INT = 0
Dim Increment As MIL_INT = 0
Dim InflectionLevel As MIL_INT = 0
Dim Ch As New ConsoleKeyInfo()
MIL.MappAllocDefault(MIL.M_DEFAULT, MilApplication, MilSystem, MilDisplay, CType(MIL.M_NULL, IntPtr), CType(MIL.M_NULL, IntPtr))
MIL.MbufRestore(IMAGE_FILE, MilSystem, MilImage)
Dim MilExtremeResult As MIL_ID = MIL.M_NULL
MIL.MimAllocResult(CType(MIL.MbufInquire(MilImage, MIL.M_OWNER_SYSTEM, MIL.M_NULL), MIL_ID), 1, MIL.M_EXTREME_LIST, MilExtremeResult)
MIL.MimFindExtreme(MilImage, MilExtremeResult, MIL.M_MAX_VALUE)
MIL.MimGetResult(MilExtremeResult, MIL.M_VALUE, ImageMaxValue)
MIL.MimFree(MilExtremeResult)
'
MIL.MbufControl(MilImage, MIL.M_MAX, CDbl(ImageMaxValue))
MIL.MdispSelect(MilDisplay, MilImage)
MIL.MdispInquire(MilDisplay, MIL.M_SIZE_BIT, DisplaySizeBit)
DisplayMaxValue = (1 << CInt(Fix(DisplaySizeBit))) - 1
Console.Write(Constants.vbLf + "INTERACTIVE WINDOW LEVELING:" + Constants.vbLf)
Console.Write("----------------------------" + Constants.vbLf + Constants.vbLf)
Console.Write("Image name : {0}" + Constants.vbLf, IMAGE_NAME)
Console.Write("Image size : {0} x {1}" + Constants.vbLf, MIL.MbufInquire(MilImage, MIL.M_SIZE_X, ImageSizeX), MIL.MbufInquire(MilImage, MIL.M_SIZE_Y, ImageSizeY))
Console.Write("Image max : {0,4}" + Constants.vbLf, ImageMaxValue)
Console.Write("Display max: {0,4}" + Constants.vbLf + Constants.vbLf, DisplayMaxValue)
If (DisplaySizeBit > 8) Then
MIL.MbufAlloc1d(MilSystem, ImageMaxValue + 1, (16) + MIL.M_UNSIGNED, MIL.M_LUT, MilLut)
Else
MIL.MbufAlloc1d(MilSystem, ImageMaxValue + 1, (8) + MIL.M_UNSIGNED, MIL.M_LUT, MilLut)
End If
MIL.MgenLutRamp(MilLut, 0, 0, ImageMaxValue, CDbl(DisplayMaxValue))
MIL.MbufControl(MilLut, MIL.M_MAX, CDbl(DisplayMaxValue))
MIL.MdispLut(MilDisplay, MilLut)
Console.Write("Keys assignement:" + Constants.vbLf + Constants.vbLf)
Console.Write("Arrow keys : Left=move Left, Right=move Right, Down=Narrower, Up=Wider." + Constants.vbLf)
Console.Write("Intensity keys: L=Lower, U=Upper, R=Reset." + Constants.vbLf)
Console.Write("Press <Enter> to end." + Constants.vbLf + Constants.vbLf)
Start = 0
LutEnd = ImageMaxValue
InflectionLevel = DisplayMaxValue
Increment = CInt((ImageMaxValue + 1) / 128)
Increment = Math.Max(Increment, 4)
Do While Ch.Key <> ConsoleKey.Enter
Select Case Ch.Key
Case ConsoleKey.LeftArrow
Start -= Increment
LutEnd -= Increment
Exit Select
Case ConsoleKey.RightArrow
Start += Increment
LutEnd += Increment
Exit Select
Case ConsoleKey.DownArrow
Start += Increment
LutEnd -= Increment
Exit Select
Case ConsoleKey.UpArrow
Start -= Increment
LutEnd += Increment
Exit Select
Case ConsoleKey.L
InflectionLevel -= 1
Exit Select
Case ConsoleKey.U
InflectionLevel += 1
Exit Select
Case ConsoleKey.R
Start = 0
LutEnd = ImageMaxValue
InflectionLevel = DisplayMaxValue
Exit Select
End Select
LutEnd = Math.Min(LutEnd, ImageMaxValue)
Start = Math.Min(Start, LutEnd)
LutEnd = Math.Max(LutEnd, Start)
Start = Math.Max(Start, 0)
LutEnd = Math.Max(LutEnd, 0)
InflectionLevel = Math.Max(InflectionLevel, 0)
InflectionLevel = Math.Min(InflectionLevel, DisplayMaxValue)
Console.Write("Inflection points: Low=({0},0), High=({1},{2}). " & Constants.vbCr, Start, LutEnd, InflectionLevel)
MIL.MgenLutRamp(MilLut, 0, 0, Start, 0)
MIL.MgenLutRamp(MilLut, Start, 0, LutEnd, CDbl(InflectionLevel))
MIL.MgenLutRamp(MilLut, LutEnd, CDbl(InflectionLevel), ImageMaxValue, CDbl(DisplayMaxValue))
MIL.MdispLut(MilDisplay, MilLut)
'
If DRAW_LUT_SHAPE = MIL.M_YES Then
If MilOriginalImage = MIL.M_NULL Then
MIL.MbufRestore(IMAGE_FILE, MilSystem, MilOriginalImage)
End If
DrawLutShape(MilDisplay, MilOriginalImage, MilImage, Start, LutEnd, InflectionLevel, ImageMaxValue, DisplayMaxValue)
End If
Ch = Console.ReadKey(True)
Loop
Console.Write(Constants.vbLf + Constants.vbLf)
MIL.MbufFree(MilLut)
MIL.MbufFree(MilImage)
If MilOriginalImage <> MIL.M_NULL Then
MIL.MbufFree(MilOriginalImage)
End If
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL)
End Sub
'
'
Private Shared Sub DrawLutShape(ByVal MilDisplay As MIL_ID, ByVal MilOriginalImage As MIL_ID, ByVal MilImage As MIL_ID, ByVal Start As MIL_INT, ByVal LutEnd As MIL_INT, ByVal InflexionIntensity As MIL_INT, ByVal ImageMaxValue As MIL_INT, ByVal DisplayMaxValue As MIL_INT)
Dim Xstart As Double = 0.0
Dim Xend As Double = 0.0
Dim Xstep As Double = 0.0
Dim Ymin As Double = 0.0
Dim Yinf As Double = 0.0
Dim Ymax As Double = 0.0
Dim Ystep As Double = 0.0
Dim ImageSizeX As MIL_INT = 0
Dim ImageSizeY As MIL_INT = 0
MIL.MbufInquire(MilImage, MIL.M_SIZE_X, ImageSizeX)
MIL.MbufInquire(MilImage, MIL.M_SIZE_Y, ImageSizeY)
Xstep = CDbl(ImageSizeX) / CDbl(ImageMaxValue)
Xstart = CDbl(Start) * Xstep
Xend = CDbl(LutEnd) * Xstep
Ystep = (CDbl(ImageSizeY) / 4.0) / CDbl(DisplayMaxValue)
Ymin = (CDbl(ImageSizeY) - 2)
Yinf = Ymin - (CDbl(InflexionIntensity) * Ystep)
Ymax = Ymin - (CDbl(DisplayMaxValue) * Ystep)
MIL.MdispControl(MilDisplay, MIL.M_UPDATE, MIL.M_DISABLE)
MIL.MbufCopy(MilOriginalImage, MilImage)
MIL.MgraColor(MIL.M_DEFAULT, CDbl(ImageMaxValue))
MIL.MgraText(MIL.M_DEFAULT, MilImage, 4, CInt(Fix(Ymin)) - 22, "0")
MIL.MgraText(MIL.M_DEFAULT, MilImage, 4, CInt(Fix(Ymax)) - 16, String.Format("{0}", DisplayMaxValue))
MIL.MgraText(MIL.M_DEFAULT, MilImage, ImageSizeX - 38, CInt(Fix(Ymin)) - 22, String.Format("{0}", ImageMaxValue))
MIL.MgraLine(MIL.M_DEFAULT, MilImage, 0, CInt(Fix(Ymin)), CInt(Fix(Xstart)), CInt(Fix(Ymin)))
MIL.MgraLine(MIL.M_DEFAULT, MilImage, CInt(Fix(Xstart)), CInt(Fix(Ymin)), CInt(Fix(Xend)), CInt(Fix(Yinf)))
MIL.MgraLine(MIL.M_DEFAULT, MilImage, CInt(Fix(Xend)), CInt(Fix(Yinf)), ImageSizeX - 1, CInt(Fix(Ymax)))
MIL.MdispControl(MilDisplay, MIL.M_UPDATE, MIL.M_ENABLE)
End Sub
End Class
End Namespace