PaintPalette example for Delphi

This example sets the paint palette to the palette of each Lead control. It then loads two images and dithers them to 8 bits per pixel with optimized palettes, and displays them side by side. In 8-bit display mode, you can see the palette problems that result when you do not use a fixed palette.

var
   sRet: Smallint;
   HeightFactor1: Single;
   WidthFactor1: Single;
   HeightFactor2: Single;
   WidthFactor2:Single;
   HeightAllowed: Single;
   WidthAllowed: single;
   RasterIO: LEADRasterIO;
   RasterProc: LEADRasterProcess;

begin

   RasterIO:= CreateComObject (CLASS_LEADRasterIO) as  LEADRasterIO;
   RasterProc:= CreateComObject (CLASS_LEADRasterProcess) as LEADRasterProcess;

   LEADRasterView1.PaintPalette := PAINTPALETTE_AUTO;
   LEADRasterView2.PaintPalette := PAINTPALETTE_AUTO;
   //Disable automatic repainting of the image.
   LEADRasterView1.AutoRepaint := False;
   LEADRasterView2.AutoRepaint := False;
   //Turn off scroll bars to make sure we use the full client area.
   LEADRasterView1.AutoScroll := False;
   LEADRasterView2.AutoScroll := False;
   //Clear the bitmaps from memory
   LEADRasterView1.Raster.Bitmap := 0;
   LEADRasterView2.Raster.Bitmap := 0;
   Screen.Cursor:= crHourglass;
   //Load the bitmaps. These hard-coded path names may be different on your system.
   RasterIO.Load (LEADRasterView1.Raster, 'v:\images\24bit.bmp', 0, 0, 1);
   RasterIO.Load (LEADRasterView2.Raster, 'v:\images\24bit2.bmp', 0, 0, 1);
   //Change the bitmaps to 8 BPS with optimized palettes
   RasterProc.ColorRes (LEADRasterView1.Raster, 8, CRP_OPTIMIZEDPALETTE, CRD_FLOYDSTEINDITHERING, 0);
   RasterProc.ColorRes (LEADRasterView2.Raster, 8, CRP_OPTIMIZEDPALETTE, CRD_FLOYDSTEINDITHERING, 0);
   //Make the controls visible so that we can size and position them.
   //They will not really appear until we load an image and paint it.
   LEADRasterView1.Visible := True;
   LEADRasterView2.Visible := True;
   //Set the variables used for preserving the aspect ratio.
   HeightFactor1 := LEADRasterView1.Raster.BitmapHeight;
   WidthFactor1 := LEADRasterView1.Raster.BitmapWidth;
   HeightFactor2 := LEADRasterView2.Raster.BitmapHeight;
   WidthFactor2 := LEADRasterView2.Raster.BitmapWidth;
   HeightAllowed := Height * 0.8;
   WidthAllowed :=  Width * 0.45;
   //Center each LEAD control on half of the form, preserving the aspect ratio.
   //Check to see if using the maximum width will make the image too tall.
   //Set the dimensions based on the result.
   if (WidthAllowed * HeightFactor1) / WidthFactor1 < HeightAllowed Then
   begin
        LEADRasterView1.Left := Trunc((Width / 4) - (WidthAllowed / 2));
        LEADRasterView1.Width:= Trunc(WidthAllowed);
        LEADRasterView1.Height := Trunc((LEADRasterView1.Width * HeightFactor1) / WidthFactor1);
        LEADRasterView1.Top := Trunc((Height - LEADRasterView1.Height) / 2);
   end
   else
   begin
      LEADRasterView1.Top := Trunc((Height - HeightAllowed) / 2);
        LEADRasterView1.Height := Trunc(HeightAllowed);
        LEADRasterView1.Width := Trunc((LEADRasterView1.Height * WidthFactor1) / HeightFactor1);
        LEADRasterView1.Left := Trunc((Width / 4) - (LEADRasterView1.Width / 2));
   end;
   if ((WidthAllowed * HeightFactor2) / WidthFactor2 < HeightAllowed) then
   begin
      LEADRasterView2.Left := Trunc((Width * 3 / 4) - (WidthAllowed / 2));
        LEADRasterView2.Width := Trunc(WidthAllowed);
        LEADRasterView2.Height := Trunc((LEADRasterView2.Width * HeightFactor2) / WidthFactor2);
        LEADRasterView2.Top := Trunc((Height - LEADRasterView2.Height) / 2);
   end
   else
   begin
     LEADRasterView2.Top := Trunc((Height - HeightAllowed) / 2);
     LEADRasterView2.Height := Trunc(HeightAllowed);
     LEADRasterView2.Width := Trunc((LEADRasterView2.Height * WidthFactor2) / HeightFactor2);
     LEADRasterView2.Left := Trunc((Width * 3 / 4) - (LEADRasterView2.Width / 2));
   end;
   //Set the image display sizes to match the LEAD controls
   LEADRasterView1.SetDstRect (0, 0, LEADRasterView1.ScaleWidth, LEADRasterView1.ScaleHeight,sRet);
   LEADRasterView1.SetDstClipRect (0, 0, LEADRasterView1.ScaleWidth, LEADRasterView1.ScaleHeight, sRet);
   LEADRasterView2.SetDstRect (0, 0, LEADRasterView2.ScaleWidth, LEADRasterView2.ScaleHeight,sRet);
   LEADRasterView2.SetDstClipRect (0, 0, LEADRasterView2.ScaleWidth, LEADRasterView2.ScaleHeight, sRet);
   //Display the images
   LEADRasterView1.ForceRepaint (sRet);
   LEADRasterView2.ForceRepaint(sRet);
   Screen.Cursor := crDefault;
end;