logical palette borland builder

Posted on 2001-08-10
Last Modified: 2008-02-01
I am trying to see the affect of changing the palette used by an TImage in borland builder.  My code is below.  Why does the image not change?

void display_logical_palette(void)

typedef struct{
   TLogPalette    lPal;
   TPaletteEntry  dummy[256];
} LogPal;

int i;
TCanvas  *canvas = frmMain->imgMain->Canvas;

// create a logical palette
     LogPal SysPal;
     SysPal.lPal.palVersion = 0x300;
     SysPal.lPal.palNumEntries = 256;

// specify what colors the palette contains
     for(i=20; i<240; i++)
          SysPal.lPal.palPalEntry[i].peRed      = 255;
          SysPal.lPal.palPalEntry[i].peGreen    = i;
          SysPal.lPal.palPalEntry[i].peBlue     = 255-i;

   if(NULL == (frmMain->fPalette = CreatePalette(&SysPal.lPal)))
      MessageBox(NULL, "CreatePalette", "doh", MB_OK );

   if(NULL == SelectPalette(canvas->Handle, frmMain->fPalette, NULL))
      MessageBox(NULL, "SelectPalette", "doh", MB_OK );

   int num;//number of entries in the logical palette mapped to the system palette

   num = RealizePalette(canvas->Handle);
   if(num == GDI_ERROR)
      MessageBox(NULL, "RealizePalette", "doh", MB_OK );
      char msg[16];
      sprintf(msg, "%d", num);
      MessageBox(NULL, msg, "doh", MB_OK );     // reports zero colors mapped


Question by:lirtua
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 14

Expert Comment

ID: 6375374
Here is a code snippet that changes the palette:

LOGPALETTE* myPalette;
Graphics::TBitmap *myBitmap;

myPalette = (LOGPALETTE*)new char[sizeof(LOGPALETTE) +
sizeof(PALETTEENTRY) * 256];
myPalette->palVersion = 0x300;
myPalette->palNumEntries = 256;

for (int i = 0; i < 256; ++i) {
myPalette->palPalEntry[i].peRed =(short)i ;
myPalette->palPalEntry[i].peGreen = (short)random(50);
myPalette->palPalEntry[i].peBlue = (short)random(200);
myPalette->palPalEntry[i].peFlags = PC_RESERVED;

myBitmap = new Graphics::TBitmap;
myBitmap->Palette = ::CreatePalette(myPalette);
Application->MessageBox("Could not load bitmap file",NULL,MB_OK);
delete myBitmap;
myBitmap = NULL;

delete myBitmap;
delete[] myPalette;
myBitmap = NULL;
myPalette = NULL;


Author Comment

ID: 6381091
The code you provided ran just fine in my project, but the bmp was not displayed with the changed palette.  I tried your code in two places, first, the form activate function, then in a button click function.  I suspected there may need to be a call to selectpalette but did not attempt this.  

Let me be a bit more clear about the intention of my original question.  I would like to obtain the palette that is currently used for a bit map that has been loaded.  Then I would like to change that palette.  And finally, I would like to display the bitmap in the new palette.

The code I provided captures the original palette, I believe, as I have printed the palette and it appears correct.  As to the changing and re-loading, it does not display, so something is wrong.

LVL 14

Expert Comment

ID: 6424713
Next is code , based on other idea:
simple read file in memory, change palette and save
back. Work only for 256 colors(8 bits to pixel)
For 2/4 bits to pixel must make small changes.
for 24 bits on pixel no palette.

void __fastcall TForm1::Button1Click(TObject *Sender)
{                            //read file
   FILE *fIn = fopen("test001.bmp", "rb");
   int len = filelength(fIn->fd);
   char *buff = new char [len+1];
                            //make pakette
   BITMAPINFOHEADER bi;         // bitmap header
   memcpy(&bmfHdr,buff, sizeof(bmfHdr));
   memcpy(&bi, buff+sizeof(bmfHdr), sizeof(bi));
   if (bi.biBitCount == 8)//256 colors in Palettee
     {  //make palette
        int palSize = sizeof(LOGPALETTE) + sizeof(PALETTEENTRY) * 256;
        LOGPALETTE* myPalette = (LOGPALETTE*)new char[palSize];
        myPalette->palVersion = 0x300;
        myPalette->palNumEntries = 256;

        for (int i = 0; i < 256; ++i)
            myPalette->palPalEntry[i].peRed =(short)i ;
            myPalette->palPalEntry[i].peGreen = (short)random(50);
            myPalette->palPalEntry[i].peBlue = (short)random(200);
            myPalette->palPalEntry[i].peFlags = PC_RESERVED;
         //copy palette to bitmap
       memcpy(buff+ sizeof(BITMAPINFOHEADER)+sizeof(BITMAPFILEHEADER),
               myPalette + sizeof(LOGPALETTE),sizeof(PALETTEENTRY) * 256);
       FILE *fOut = fopen("test002.bmp", "wb");
       delete [] myPalette;
   delete [] buff;

LVL 14

Accepted Solution

AlexVirochovsky earned 75 total points
ID: 6500506
I hope, my code helped you. Alex

Author Comment

ID: 6520654
Yes it was helpful - thanks Alex.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
passing data from one form to another form in c++ 27 88
max float value 3 59
I could not set window to top 4 42
c++  placing data into a form and an editbox 5 23
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question