GetHistogram example for C++ Builder

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:

   long MyIndex;
   int Brightest;
   int Darkest;
   int CurrentRange;
   int Offset;

   LEADRasterProcess* pRasterProc= NULL;

     CoCreateInstance(CLSID_LEADRasterProcess, NULL, CLSCTX_ALL, IID_ILEADRasterProcess, (void**)&pRasterProc);
   Cursor= crHourGlass;
   //Do a CMYK color separation and copy the K plane to the bitmap
   pRasterProc->ColorSeparate (LEADRasterView1->Raster, COLORSEP_CMYK);
   LEADRasterView1->Raster->Bitmap = 0; //Free the bitmap
   LEADRasterView1->Raster->Bitmap = pRasterProc->get_ColorPlanes (3); //Copy the K plane
   //Load the histogram
   pRasterProc->GetHistogram (LEADRasterView1->Raster, CHANNEL_MASTER);
   //Find the brightest and darkest intensities in the image
   MyIndex = 0;
   Brightest = 0;
   Darkest = -1;
   while (MyIndex < 256)
   {
      if (pRasterProc->get_HistogramTable ((short)MyIndex) > 0)
      {
         Brightest = MyIndex;
         if (Darkest == -1)
            Darkest = MyIndex;
      }
      MyIndex = MyIndex + 1;
   }
   //Remap the intensities to use the full range
   MyIndex = Darkest;
   CurrentRange= Brightest - Darkest;
   if (CurrentRange > 0)
   {
      while (MyIndex <= Brightest)
      {
         Offset = MyIndex - Darkest;
         pRasterProc->set_RemapTable ((short)MyIndex, (short)((255 * Offset) / CurrentRange));
         MyIndex = MyIndex + 1;
      }
      pRasterProc->RemapIntensity (LEADRasterView1->Raster, CHANNEL_MASTER);
   }
   //Merge the color planes and free them from memory
   pRasterProc->set_ColorPlanes (3, LEADRasterView1->Raster->Bitmap); //Update the K plane
   pRasterProc->ColorMerge (LEADRasterView1->Raster, COLORSEP_CMYK);
   pRasterProc->set_ColorPlanes(0, 0);
   pRasterProc->set_ColorPlanes(1, 0);
   pRasterProc->set_ColorPlanes(2, 0);
   pRasterProc->set_ColorPlanes(3, 0);
   //Set the image display size to match the LEAD control
   LEADRasterView1->SetDstRect (0, 0, LEADRasterView1->ScaleWidth, LEADRasterView1->ScaleHeight);
   LEADRasterView1->SetDstClipRect (0, 0, LEADRasterView1->ScaleWidth, LEADRasterView1->ScaleHeight);
   LEADRasterView1->ForceRepaint ();
   Cursor= crDefault;
   pRasterProc-> Release( );