GetHistogram example for Access 2.0

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:

Sub Command1_Click ()

    DoCmd Hourglass True

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

    'Load the histogram
    Lead1.Object.GetHistogram CHANNEL_MASTER

    'Find the brightest and darkest intensities in the image
    MyIndex = 0
    Brightest = 0
    Darkest = -1
    While (MyIndex < 256)
        If Lead1.Object.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.Object.RemapTable(MyIndex) = (255 * Offset) / CurrentRange
            MyIndex = MyIndex + 1
        Wend
        Lead1.Object.RemapIntensity CHANNEL_MASTER
    End If

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

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

    Lead1.Object.ForceRepaint
    DoCmd Hourglass False

End Sub