GetHistogram example for Visual Basic

The following code does a CMYK separation and uses a histogram to find the brightest and darkest intensities in the K plane of the bitmap. It then remaps the intensities to use the full range and merges the color planes to recreate the bitmap:

Private Sub Command1_Click ()
    MousePointer = 11 ' hourglass

    'Do a CMYK color separation and copy the K plane to the bitmap
    Lead1.ColorSeparate COLORSEP_CMYK
    Lead1.Bitmap = 0 'Free the bitmap
    Lead1.Bitmap = Lead1.ColorPlanes(3) 'Copy the K plane

    'Load the histogram
    Lead1.GetHistogram CHANNEL_MASTER

    'Find the brightest and darkest intensities in the image
    MyIndex = 0
    Brightest = 0
    Darkest = -1
    While (MyIndex < 256)
        If Lead1.HistogramTable(MyIndex) > 0 Then
            Brightest = MyIndex
            If (Darkest = -1) Then
                Darkest = MyIndex
            End If
        End If
        MyIndex = MyIndex + 1
    Wend

    'Remap the intensities to use the full range
    MyIndex = Darkest
    CurrentRange = Brightest - Darkest
    If CurrentRange > 0 Then
        While (MyIndex <= Brightest)
            Offset = MyIndex - Darkest
            Lead1.RemapTable(MyIndex) = (255 * Offset) / CurrentRange
            MyIndex = MyIndex + 1
        Wend
        Lead1.RemapIntensity CHANNEL_MASTER
    End If

    'Merge the color planes and free them from memory
    Lead1.ColorPlanes(3) = Lead1.Bitmap 'Update the K plane
    Lead1.ColorMerge COLORSEP_CMYK
    Lead1.ColorPlanes(0) = 0
    Lead1.ColorPlanes(1) = 0
    Lead1.ColorPlanes(2) = 0
    Lead1.ColorPlanes(3) = 0

    'Set the image display size to match the LEAD control
    Lead1.SetDstRect 0, 0, Lead1.ScaleWidth, Lead1.ScaleHeight
    Lead1.SetDstClipRect 0, 0, Lead1.ScaleWidth, Lead1.ScaleHeight

    Lead1.ForceRepaint
    MousePointer = 0 ' default
End Sub