#include "l_bitmap.h"

L_LTKRN_API L_INT L_StartDithering(pBitmap, pPalette, uColors)

Initializes the buffered dithering of a bitmap. The dithering is then carried out by the L_DitherLine function and a function that you supply. It is ended by the L_StopDithering function. (Dithering is the process of using error diffusion to reduce the number of colors in an image.)



Pointer to the bitmap handle that has all the information about the input image. The bitmap does not have to exist, but the information must be complete. The DitheringMethod field in the bitmap handle specifies the dithering method to be used for color reduction.

L_RGBQUAD* pPalette

Pointer to the palette that the L_DitherLine function will use for dithering. You can specify your own palette, or use NULL for LEAD's fixed palette.

L_UINT uColors

Number of colors used in the palette. If the palette contains more colors, only the first uColors colors are used. Valid values are 2 to 256.


Value Meaning
SUCCESS The function was successful.
< 1 An error occurred. Refer to Return Codes.


This function does not support signed data images. It returns the error code ERROR_SIGNED_DATA_NOT_SUPPORTED if a signed data image is passed to this function.

The following flow chart shows how the functions relate to each other:


Before calling this function, the following fields must be specified in the bitmap handle:

Required DLLs and Libraries


Win32, x64, Linux.

See Also




This example dithers each line in one bitmap and writes it to another bitmap.

L_INT StartDitheringExample(pBITMAPHANDLE pBitmap) 
   L_INT nRet; 
   L_UCHAR*       pInBuf;              /* Buffer to hold the input row */ 
   HGLOBAL        hInBuf;              /* Handle to the input buffer */ 
   L_UCHAR*       pOutBuf;             /* Buffer to hold the output row */ 
   HGLOBAL        hOutBuf;             /* Handle to the output buffer */ 
   BITMAPHANDLE   TmpBitmap;           /* Bitmap containing input data */ 
   RGBQUAD        FixedPalette[256];   /* Fixed palette */ 
   L_INT          i;                   /* Loop counter */ 
   /* Load the input bitmap, at 24 bits per pixel */ 
   nRet = L_LoadBitmap (MAKE_IMAGE_PATH(TEXT("ImageProcessingDemo\\Image3.cmp")), &TmpBitmap, sizeof(BITMAPHANDLE), 24, ORDER_BGR, NULL, NULL); 
   if(nRet != SUCCESS) 
      return nRet; 
   /* Allocate the input buffer for 24-bit data */ 
   hInBuf = GlobalAlloc(GMEM_MOVEABLE, TmpBitmap.Width * 3); 
   pInBuf = (L_UCHAR *)GlobalLock( hInBuf ); 
   /* Allocate the output buffer for 8-bit data */ 
   hOutBuf = GlobalAlloc(GMEM_MOVEABLE, TmpBitmap.Width); 
   pOutBuf = (L_UCHAR *)GlobalLock( hOutBuf ); 
   /* Get the LEAD fixed palette for an 8-bit image */ 
   L_GetFixedPalette(FixedPalette, 8); 
   /* Create the new bitmap */ 
   nRet = L_CreateBitmap(pBitmap, 
                  8,       /* 8 bits per pixel */ 
                  0,       /* Color order is not used */ 
                  NULL,    /* Use the fixed palette */ 
                  TmpBitmap.ViewPerspective, NULL, 0); 
   if(nRet != SUCCESS) 
      return nRet; 
   /* Set the dithering method */ 
   TmpBitmap.DitheringMethod = STEVENSON_ARCE_DITHERING; 
   /* Initialize the dithering process */ 
   nRet = L_StartDithering(&TmpBitmap, FixedPalette, 256); 
   if(nRet != SUCCESS) 
      return nRet; 
   /* Use L_DitherLine to process each row in the bitmap */ 
   for(i=0; i < TmpBitmap.Height; i++) 
      nRet =(L_INT)L_GetBitmapRow(&TmpBitmap, pInBuf, i, TmpBitmap.BytesPerLine); 
      if(nRet < 1) 
         return nRet; 
      nRet = L_DitherLine(&TmpBitmap, pInBuf, pOutBuf); 
      if(nRet != SUCCESS) 
         return nRet; 
      nRet = (L_INT)L_PutBitmapRow(pBitmap, pOutBuf, i, pBitmap->BytesPerLine); 
      if(nRet < 1) 
         return nRet; 
   /* End the dithering process */ 
   nRet = L_StopDithering(&TmpBitmap); 
   if(nRet != SUCCESS) 
      return nRet; 
   /* Free memory that we no longer need */ 
   return SUCCESS; 

Help Version 20.0.2019.10.13
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2019 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS Raster Imaging C API Help