PaintPalette example for C++ 5.0 and later

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.

ILEADRasterIO *pRasterIO1=NULL;
ILEADRasterIO *pRasterIO2=NULL;
CoCreateInstance(CLSID_LEADRasterIO, NULL, CLSCTX_ALL,
                 IID_ILEADRasterIO, (void**)&pRasterIO1);
CoCreateInstance(CLSID_LEADRasterIO, NULL, CLSCTX_ALL,
                 IID_ILEADRasterIO, (void**)&pRasterIO2);
ILEADRasterProcess *pRasterProc=NULL;
CoCreateInstance(CLSID_LEADRasterProcess, NULL, CLSCTX_ALL,
                 IID_ILEADRasterProcess, (void**)&pRasterProc);

// Disable automatic repainting of the image.
m_LEADRasterView1.SetAutoRepaint(FALSE);
m_LEADRasterView2.SetAutoRepaint(FALSE);
// Turn off scroll bars to make sure we use the full client area.
m_LEADRasterView1.SetAutoScroll(FALSE);
m_LEADRasterView2.SetAutoScroll(FALSE);
// Clear the bitmaps from memory
m_LEADRasterView1.GetRaster().SetBitmap(0);
m_LEADRasterView2.GetRaster().SetBitmap(0);
BeginWaitCursor();
// Load the bitmaps. These path names may be different on your system.
pRasterIO1->Load(m_LEADRasterView1.GetRaster(),
                 "d:\\lead14\\dist\\images\\image1.cmp", 0, 0, 1);
pRasterIO2->Load(m_LEADRasterView2.GetRaster(),
                 "d:\\lead14\\dist\\images\\image2.cmp", 0, 0, 1);
// Change the bitmaps to 8 BPS with optimized palettes
pRasterProc->ColorRes(m_LEADRasterView1.GetRaster(),
                      8, CRP_OPTIMIZEDPALETTE, CRD_FLOYDSTEINDITHERING, 0);
pRasterProc->ColorRes(m_LEADRasterView2.GetRaster(),
                      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.
m_LEADRasterView1.ShowWindow(SW_NORMAL);
m_LEADRasterView2.ShowWindow(SW_NORMAL);
// Set the variables used for preserving the aspect ratio.
float HeightFactor1 = m_LEADRasterView1.GetRaster().GetBitmapHeight();
float WidthFactor1 = m_LEADRasterView1.GetRaster().GetBitmapWidth();
float HeightFactor2 = m_LEADRasterView2.GetRaster().GetBitmapHeight();
float WidthFactor2 = m_LEADRasterView2.GetRaster().GetBitmapWidth();
CRect rcWindow;
GetWindowRect(rcWindow);
float ScaleWidth = (float) rcWindow.Width();
float ScaleHeight = (float) rcWindow.Height();
float HeightAllowed = ScaleHeight * 0.8f;
float WidthAllowed = ScaleWidth * 0.45f;
// 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.
float Left;
float Width;
float Height;
float Top;
CRect rcControl;
if ((WidthAllowed * HeightFactor1) / WidthFactor1 < HeightAllowed)
{
  Left =  (ScaleWidth / 4) - (WidthAllowed / 2);
  Width = WidthAllowed;
  Height = (Width * HeightFactor1) / WidthFactor1;
  Top = (ScaleHeight - Height) / 2;
}
else
{
  Top = (ScaleHeight - HeightAllowed) / 2;
  Height = HeightAllowed;
  Width = (Height * WidthFactor1) / HeightFactor1;
  Left = (ScaleWidth / 4) - (Width / 2);
}
rcControl.SetRect(0, 0, (int) Width, (int) Height);
rcControl.OffsetRect((int) Left, (int) Top);
m_LEADRasterView1.MoveWindow(rcControl);
if ((WidthAllowed * HeightFactor2) / WidthFactor2 < HeightAllowed)
{
  Left =  (ScaleWidth * 3/4) - (WidthAllowed / 2);
  Width = WidthAllowed;
  Height = (Width * HeightFactor2) / WidthFactor2;
  Top = (ScaleHeight - Height) / 2;
}
else
{
  Top = (ScaleHeight - HeightAllowed) / 2;
  Height = HeightAllowed;
  Width = (Height * WidthFactor2) / HeightFactor2;
  Left = (ScaleWidth * 3 / 4) - (Width / 2);
}
rcControl.SetRect(0, 0, (int) Width, (int) Height);
rcControl.OffsetRect((int) Left, (int) Top);
m_LEADRasterView2.MoveWindow(rcControl);
// Set the image display sizes to match the LEAD controls
m_LEADRasterView1.SetDstRect(0.0f, 0.0f,
                             m_LEADRasterView1.GetScaleWidth(),
                             m_LEADRasterView1.GetScaleHeight());
m_LEADRasterView1.SetDstClipRect(0.0f, 0.0f,
                                 m_LEADRasterView1.GetScaleWidth(),
                                 m_LEADRasterView1.GetScaleHeight());
m_LEADRasterView2.SetDstRect(0.0f, 0.0f,
                             m_LEADRasterView2.GetScaleWidth(),
                             m_LEADRasterView2.GetScaleHeight());
m_LEADRasterView2.SetDstClipRect(0.0f, 0.0f,
                                 m_LEADRasterView2.GetScaleWidth(),
                                 m_LEADRasterView2.GetScaleHeight());
// Display the images
m_LEADRasterView1.SetPaintPalette(PAINTPALETTE_AUTO);
m_LEADRasterView2.SetPaintPalette(PAINTPALETTE_AUTO);
m_LEADRasterView1.ForceRepaint();
m_LEADRasterView2.ForceRepaint();
pRasterIO1->Release();
pRasterIO2->Release();
pRasterProc->Release();
EndWaitCursor();