Available in the LEADTOOLS Imaging toolkit. |
GetHistogram example for Visual J++
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 void button1_click(Object source, Event e)
{
setCursor( Cursor.WAIT ); // hourglass
// Do a CMYK color separation and copy the K plane to the bitmap
LEAD1.ColorSeparate( LTOCXU.ColorSeparationConstants.COLORSEP_CMYK );
LEAD1.setBitmap( 0 ); // Free the bitmap
LEAD1.setBitmap( LEAD1.getColorPlanes( (short) 3 ) ); // Copy the K plane
// Load the histogram
LEAD1.GetHistogram( (short)LTOCXU.ChannelConstants.CHANNEL_MASTER );
// Find the brightest and darkest intensities in the image
int nMyIndex = 0;
int nBrightest = 0;
int nDarkest = -1;
while( nMyIndex < 256 )
{
if( LEAD1.getHistogramTable( (short) nMyIndex ) > 0 )
{
nBrightest = nMyIndex;
if( nDarkest == -1 )
nDarkest = nMyIndex;
}
nMyIndex++;
}
// Remap the intensities to use the full range
nMyIndex = nDarkest;
int nCurrentRange = nBrightest - nDarkest;
if( nCurrentRange > 0 )
{
while( nMyIndex <= nBrightest )
{
int nOffset = nMyIndex - nDarkest;
LEAD1.setRemapTable( (short) nMyIndex, (short) ( ( 255 * nOffset ) / nCurrentRange ) );
nMyIndex++;
}
LEAD1.RemapIntensity( (short) LTOCXU.ChannelConstants.CHANNEL_MASTER );
}
// Merge the color planes and free them from memory
LEAD1.setColorPlanes( (short) 3, LEAD1.getBitmap() ); // Update the K plane
LEAD1.ColorMerge( (short) LTOCXU.ColorSeparationConstants.COLORSEP_CMYK );
LEAD1.setColorPlanes( (short) 0, 0 );
LEAD1.setColorPlanes( (short) 1, 0 );
LEAD1.setColorPlanes( (short) 2, 0 );
LEAD1.setColorPlanes( (short) 3, 0 );
// Set the image display size to match the LEAD control
LEAD1.SetDstRect( 0, 0, LEAD1.getScaleWidth(), LEAD1.getScaleHeight() );
LEAD1.SetDstClipRect( 0, 0, LEAD1.getScaleWidth(), LEAD1.getScaleHeight() );
LEAD1.ForceRepaint();
setCursor( Cursor.DEFAULT ); // default
}