PaintPalette example for C++ Builder
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.
float HeightFactor1;
float WidthFactor1;
float HeightFactor2;
float WidthFactor2;
float HeightAllowed;
float WidthAllowed;
LEADRasterIO* pRasterIO= NULL;
LEADRasterProcess* pRasterProc= NULL;
CoCreateInstance(CLSID_LEADRasterIO, NULL, CLSCTX_ALL, IID_ILEADRasterIO, (void**)&pRasterIO);
CoCreateInstance(CLSID_LEADRasterProcess, NULL, CLSCTX_ALL, IID_ILEADRasterProcess, (void**)&pRasterProc);
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;
Cursor= crHourGlass;
//Load the bitmaps-> These hard-coded path names may be different on your system->
pRasterIO->Load (LEADRasterView1->Raster, AnsiToOLESTR("v:\\images\\24bit.bmp"), 0, 0, 1);
pRasterIO->Load (LEADRasterView2->Raster, AnsiToOLESTR("v:\\images\\24bit2.bmp"), 0, 0, 1);
//Change the bitmaps to 8 BPS with optimized palettes
pRasterProc->ColorRes (LEADRasterView1->Raster, 8, CRP_OPTIMIZEDPALETTE, CRD_FLOYDSTEINDITHERING, 0);
pRasterProc->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)
{
LEADRasterView1->Left = (Width / 4) - (WidthAllowed / 2);
LEADRasterView1->Width= WidthAllowed;
LEADRasterView1->Height = (LEADRasterView1->Width * HeightFactor1) / WidthFactor1;
LEADRasterView1->Top = (Height - LEADRasterView1->Height) / 2;
}
else
{
LEADRasterView1->Top = (Height - HeightAllowed) / 2;
LEADRasterView1->Height = HeightAllowed;
LEADRasterView1->Width = (LEADRasterView1->Height * WidthFactor1) / HeightFactor1;
LEADRasterView1->Left = (Width / 4) - (LEADRasterView1->Width / 2);
}
if ((WidthAllowed * HeightFactor2) / WidthFactor2 < HeightAllowed)
{
LEADRasterView2->Left = (Width * 3 / 4) - (WidthAllowed / 2);
LEADRasterView2->Width = WidthAllowed;
LEADRasterView2->Height = (LEADRasterView2->Width * HeightFactor2) / WidthFactor2;
LEADRasterView2->Top = (Height - LEADRasterView2->Height) / 2;
}
else
{
LEADRasterView2->Top = (Height - HeightAllowed) / 2;
LEADRasterView2->Height = HeightAllowed;
LEADRasterView2->Width = (LEADRasterView2->Height * WidthFactor2) / HeightFactor2;
LEADRasterView2->Left = (Width * 3 / 4) - (LEADRasterView2->Width / 2);
}
//Set the image display sizes to match the LEAD controls
LEADRasterView1->SetDstRect (0, 0, LEADRasterView1->ScaleWidth, LEADRasterView1->ScaleHeight);
LEADRasterView1->SetDstClipRect (0, 0, LEADRasterView1->ScaleWidth, LEADRasterView1->ScaleHeight);
LEADRasterView2->SetDstRect (0, 0, LEADRasterView2->ScaleWidth, LEADRasterView2->ScaleHeight);
LEADRasterView2->SetDstClipRect (0, 0, LEADRasterView2->ScaleWidth, LEADRasterView2->ScaleHeight);
//Display the images
LEADRasterView1->ForceRepaint ();
LEADRasterView2->ForceRepaint();
pRasterProc-> Release( );
pRasterIO-> Release( );
Cursor = crDefault;