GetHistogram example for Delphi
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:
var
sRet: Smallint;
MyIndex :Longint;
Brightest :Longint;
Darkest :Longint;
CurrentRange :Longint;
Offset :Longint;
RasterProc: LEADRasterProcess;
begin
RasterProc:= CreateComObject (CLASS_LEADRasterProcess ) as LEADRasterProcess;
Cursor:= crHourGlass;
//Do a CMYK color separation and copy the K plane to the bitmap
RasterProc.ColorSeparate (LEADRasterView1.Raster, COLORSEP_CMYK);
LEADRasterView1.Raster.Bitmap := 0; //Free the bitmap
LEADRasterView1.Raster.Bitmap := RasterProc.ColorPlanes [3]; //Copy the K plane
//Load the histogram
RasterProc.GetHistogram (LEADRasterView1.Raster, CHANNEL_MASTER);
//Find the brightest and darkest intensities in the image
MyIndex := 0;
Brightest := 0;
Darkest := -1;
while (MyIndex < 256) do
begin
if RasterProc.HistogramTable [MyIndex] > 0 then
begin
Brightest := MyIndex;
if (Darkest = -1) then
Darkest := MyIndex;
end;
MyIndex := MyIndex + 1
end;
//Remap the intensities to use the full range
MyIndex := Darkest;
CurrentRange:= Brightest - Darkest;
if (CurrentRange > 0) then
begin
while (MyIndex <= Brightest) do
begin
Offset := MyIndex - Darkest;
RasterProc.RemapTable [MyIndex] := Trunc((255 * Offset) / CurrentRange);
MyIndex := MyIndex + 1;
end;
RasterProc.RemapIntensity (LEADRasterView1.Raster, CHANNEL_MASTER);
end;
//Merge the color planes and free them from memory
RasterProc.ColorPlanes [3] := LEADRasterView1.Raster.Bitmap; //Update the K plane
RasterProc.ColorMerge (LEADRasterView1.Raster, COLORSEP_CMYK);
RasterProc.ColorPlanes[0]:= 0;
RasterProc.ColorPlanes[1]:= 0;
RasterProc.ColorPlanes[2]:= 0;
RasterProc.ColorPlanes[3]:= 0;
//Set the image display size to match the LEAD control
LEADRasterView1.SetDstRect (0, 0, LEADRasterView1.ScaleWidth, LEADRasterView1.ScaleHeight, sRet);
LEADRasterView1.SetDstClipRect (0, 0, LEADRasterView1.ScaleWidth, LEADRasterView1.ScaleHeight, sRet);
LEADRasterView1.ForceRepaint (sRet);
Cursor:= crDefault;
end;