Option Strict On

Imports System.Drawing

Public Class MainForm

    Private Enum SegmentationType
        None
        LeftSlap
        RightSlap
        BothThumbs
        UnknownSlap
    End Enum

    Private mOriginalImage As Bitmap
    Private mSegmentation As SegmentationType
    Private mSegments As Nbis.Segment()
    Private mMinutiae As Nbis.Minutia()

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        MainOpenFileDialog.Filter = "Fingerprint images (*.bmp; *.wsq)|*.BMP;*.WSQ"
        MainOpenFileDialog.ShowDialog()
    End Sub

    Private Sub MainOpenFileDialog_Selected(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MainOpenFileDialog.FileOk

        ' Clear the minutiae
        mMinutiae = Nothing
        DetectMinutiaeToolStripMenuItem.Checked = False

        mSegments = Nothing
        LeftSlapToolStripMenuItem.Checked = False
        RightSlapToolStripMenuItem.Checked = False
        BothThumbsToolStripMenuItem.Checked = False

        If MainOpenFileDialog.FileName.ToLower.EndsWith(".bmp") Then
            mOriginalImage = New Bitmap(MainOpenFileDialog.OpenFile)
        ElseIf MainOpenFileDialog.FileName.ToLower.EndsWith(".wsq") Then
            Dim info As New System.IO.FileInfo(MainOpenFileDialog.FileName)
            Dim fs As System.IO.FileStream = info.OpenRead()
            Dim length As Integer = CInt(fs.Length)
            Dim fileData(length) As Byte
            fs.Read(fileData, 0, length)
            fs.Close()
            'MsgBox(Nbis.Nfiq.FromFile(MainOpenFileDialog.FileName))
            mOriginalImage = Nbis.Wsq.FromWsqToBitmap(fileData)
        Else
            MessageBox.Show("Cannot load this file")
        End If

        If mOriginalImage IsNot Nothing Then
            MainPictureBox.Image = mOriginalImage
        End If

    End Sub

    Private Sub MainPictureBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MainPictureBox.Click

    End Sub


    Private Sub InkedToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InkedToolStripMenuItem.Click
        InkedToolStripMenuItem.Checked = Not InkedToolStripMenuItem.Checked
        If LeftSlapToolStripMenuItem.Checked OrElse RightSlapToolStripMenuItem.Checked OrElse BothThumbsToolStripMenuItem.Checked Then
            PerformSegmentation()
        End If

    End Sub

    Private Sub LeftSlapToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LeftSlapToolStripMenuItem.Click
        If Not LeftSlapToolStripMenuItem.Checked Then
            DetectMinutiaeToolStripMenuItem.Checked = False
            LeftSlapToolStripMenuItem.Checked = True
            RightSlapToolStripMenuItem.Checked = False
            BothThumbsToolStripMenuItem.Checked = False
            ShowNFIQToolStripMenuItem.Checked = False
            mSegmentation = SegmentationType.LeftSlap
            PerformSegmentation()
        Else
            LeftSlapToolStripMenuItem.Checked = False
            mSegmentation = SegmentationType.None
            MainPictureBox.Image = mOriginalImage
        End If
    End Sub


    Private Sub RightSlapToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RightSlapToolStripMenuItem.Click
        If Not RightSlapToolStripMenuItem.Checked Then
            DetectMinutiaeToolStripMenuItem.Checked = False
            LeftSlapToolStripMenuItem.Checked = False
            RightSlapToolStripMenuItem.Checked = True
            BothThumbsToolStripMenuItem.Checked = False
            ShowNFIQToolStripMenuItem.Checked = False
            mSegmentation = SegmentationType.RightSlap
            PerformSegmentation()
        Else
            RightSlapToolStripMenuItem.Checked = False
            mSegmentation = SegmentationType.None
            MainPictureBox.Image = mOriginalImage
        End If
    End Sub

    Private Sub BothThumbsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BothThumbsToolStripMenuItem.Click
        If Not BothThumbsToolStripMenuItem.Checked Then

            DetectMinutiaeToolStripMenuItem.Checked = False

            LeftSlapToolStripMenuItem.Checked = False
            RightSlapToolStripMenuItem.Checked = False
            BothThumbsToolStripMenuItem.Checked = True
            ShowNFIQToolStripMenuItem.Checked = False
            mSegmentation = SegmentationType.BothThumbs

            PerformSegmentation()
        Else
            BothThumbsToolStripMenuItem.Checked = False
            mSegmentation = SegmentationType.None
            MainPictureBox.Image = mOriginalImage
        End If
    End Sub

    Private Sub PerformSegmentation()

        Dim options As New Nbis.SegmenterParameters

        If InkedToolStripMenuItem.Checked Then
            options.IsInked = True
        End If

        Select Case mSegmentation
            Case SegmentationType.None
                Return
            Case SegmentationType.LeftSlap
                options.TargetFingers = Nbis.Finger.LeftSlap
            Case SegmentationType.RightSlap
                options.TargetFingers = Nbis.Finger.RightSlap
            Case SegmentationType.BothThumbs
                options.TargetFingers = Nbis.Finger.BothThumbs
            Case Else
                Return
        End Select

        mSegments = Nbis.Segmentation.FromBitmap(mOriginalImage, options)
        Dim displayImage As New Bitmap(mOriginalImage)
        Dim g As Graphics = Graphics.FromImage(displayImage)

        Dim p As New Pen(Color.Red, 4)
        For i As Integer = 0 To mSegments.Length - 1
            Dim x, y, w, h As Integer
            w = mSegments(i).Width
            h = mSegments(i).Height
            x = mSegments(i).OriginX - CInt(w / 2.0!)
            y = mSegments(i).OriginY - CInt(h / 2.0!)

            g.DrawRectangle(p, New Rectangle(x, y, w, h))
        Next
        p.Dispose()

        MainPictureBox.Image = displayImage


    End Sub

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        MyBase.OnPaint(e)
        If MainPictureBox.Image IsNot Nothing Then
            e.Graphics.DrawRectangle(SystemPens.ControlDarkDark, MainPictureBox.RectangleToClient(New Rectangle(0, 0, MainPictureBox.Image.Width, MainPictureBox.Image.Height)))
        End If

        'MainPictureBox.RectangleToClient(
    End Sub


    Private Sub DetectMinutiaeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DetectMinutiaeToolStripMenuItem.Click

        If mOriginalImage Is Nothing Then Return

        DetectMinutiaeToolStripMenuItem.Checked = Not DetectMinutiaeToolStripMenuItem.Checked

        If mMinutiae Is Nothing AndAlso mOriginalImage IsNot Nothing Then
            mMinutiae = Nbis.DetectMinutiae.FromBitmap(mOriginalImage, 500)
        End If

        If DetectMinutiaeToolStripMenuItem.Checked Then

            LeftSlapToolStripMenuItem.Checked = False
            RightSlapToolStripMenuItem.Checked = False
            BothThumbsToolStripMenuItem.Checked = False
            ShowNFIQToolStripMenuItem.Checked = False


            Dim displayImage As New Bitmap(mOriginalImage)
            Dim g As Graphics = Graphics.FromImage(displayImage)

            Dim p As New Pen(Color.Blue, 1)
            For i As Integer = 0 To mMinutiae.Length - 1
                g.DrawEllipse(p, mMinutiae(i).X - 4, mMinutiae(i).Y - 4, 9, 9)

                Dim xs, xe, ys, ye As Integer

                xs = mMinutiae(i).X
                ys = mMinutiae(i).Y

                Dim angle As Double = (mMinutiae(i).Direction * 11.25!) - 90.0!
                angle *= (Math.PI / 180.0!)
                Dim xd As Integer = CInt(10.0! * Math.Cos(angle))
                Dim yd As Integer = CInt(10.0! * Math.Sin(angle))
                xe = mMinutiae(i).X + xd
                ye = mMinutiae(i).Y + yd

                g.DrawLine(p, xs, ys, xe, ye)

            Next
            p.Dispose()

            MainPictureBox.Image = displayImage

        Else
            MainPictureBox.Image = mOriginalImage
        End If

    End Sub

    Private Sub ShowNFIQToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShowNFIQToolStripMenuItem.Click

        If mOriginalImage Is Nothing Then Return

        ShowNFIQToolStripMenuItem.Checked = Not ShowNFIQToolStripMenuItem.Checked

        If ShowNFIQToolStripMenuItem.Checked Then

            LeftSlapToolStripMenuItem.Checked = False
            RightSlapToolStripMenuItem.Checked = False
            BothThumbsToolStripMenuItem.Checked = False
            DetectMinutiaeToolStripMenuItem.Checked = False

            If mSegmentation = SegmentationType.None Then
                Dim nfiq As Integer = Nbis.Nfiq.FromBitmap(mOriginalImage, 500)
                NFIQLabel.Text = "NFIQ = " & nfiq

            End If
        Else
            MainPictureBox.Image = mOriginalImage

        End If

    End Sub

    Private Sub NFIQLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NFIQLabel.Click
        ShowNFIQToolStripMenuItem_Click(sender, e)
    End Sub
End Class
