GetHistogram example for C++ 4.0 and later

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:

BeginWaitCursor();
// Do a CMYK color separation and copy the K plane to the bitmap
m_Lead1.ColorSeparate(COLORSEP_CMYK);
m_Lead1.SetBitmap(m_Lead1.GetColorPlanes(3)); // Copy the K plane
// Load the histogram
m_Lead1.GetHistogram(CHANNEL_MASTER);
// Find the brightest and darkest intensities in the image
int MyIndex = 0;
int Brightest = 0;
int Darkest = -1;
while (MyIndex < 256)
{
  if (m_Lead1.GetHistogramTable(MyIndex) > 0)
  {
    Brightest = MyIndex;
    if (Darkest == -1)
       Darkest = MyIndex;
  }
  MyIndex = MyIndex + 1;
}
// Remap the intensities to use the full range
MyIndex = Darkest;
int CurrentRange = Brightest - Darkest;
if (CurrentRange > 0)
{
  while (MyIndex <= Brightest)
  {
    int Offset = MyIndex - Darkest;
    m_Lead1.SetRemapTable(MyIndex, (255 * Offset) / CurrentRange);
    MyIndex = MyIndex + 1;
  }
  m_Lead1.RemapIntensity(CHANNEL_MASTER);
}

// Merge the color planes and free them from memory
m_Lead1.SetColorPlanes(3, m_Lead1.GetBitmap()); // Update the K plane
m_Lead1.ColorMerge(COLORSEP_CMYK);
m_Lead1.SetColorPlanes(0, 0);
m_Lead1.SetColorPlanes(1, 0);
m_Lead1.SetColorPlanes(2, 0);
m_Lead1.SetColorPlanes(3, 0);
// Set the image display size to match the LEAD control
m_Lead1.SetDstRect(0.0f, 0.0f, m_Lead1.GetScaleWidth(), m_Lead1.GetScaleHeight());
m_Lead1.SetDstClipRect(0.0f, 0.0f, m_Lead1.GetScaleWidth(), m_Lead1.GetScaleHeight());
m_Lead1.ForceRepaint();
EndWaitCursor();