L_StartDithering

#include "l_bitmap.h"

L_LTKRN_API L_INT L_StartDithering(pBitmap, pPalette, uColors)

pBITMAPHANDLE pBitmap;

/* pointer to the bitmap handle */

L_RGBQUAD* pPalette;

/* pointer to the palette */

L_UINT uColors;

/* number of colors used in the palette */

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.)

Parameter

Description

pBitmap

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.

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.

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.

Returns

SUCCESS

The function was successful.

< 1

An error occurred. Refer to Return Codes.

Comments

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:

image\dithbuf.gif

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

Required DLLs and Libraries

LTKRN

For a listing of the exact DLLs and Libraries needed, based on the toolkit version, refer to Files To Be Included With Your Application.

Platforms

Windows 2000 / XP/Vista, Windows CE.

See Also

Functions:

L_ConvertBuffer, L_DitherLine, L_StopDithering

Topics:

Raster Image Functions: Doing Color Expansion or Reduction

 

Raster Image Functions: Doing Color Space Conversions

Example

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 (TEXT("%UserProfile%\\My Documents\\LEADTOOLS Images\\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 */
   if(pBitmap->Flags.Allocated)
      L_FreeBitmap(pBitmap);
   nRet = L_CreateBitmap(pBitmap,
                  sizeof(BITMAPHANDLE),
                  TYPE_CONV,
                  TmpBitmap.Width,
                  TmpBitmap.Height,
                  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 */
   L_AccessBitmap(pBitmap);
   L_AccessBitmap(&TmpBitmap);
   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;
   }
   L_ReleaseBitmap(&TmpBitmap);
   L_ReleaseBitmap(pBitmap);
   L_FreeBitmap(&TmpBitmap);
   /* End the dithering process */
   nRet = L_StopDithering(&TmpBitmap);
   if(nRet != SUCCESS)
      return nRet;
   /* Free memory that we no longer need */
   GlobalFree(hInBuf);
   GlobalFree(hOutBuf);
   return SUCCESS;
}