Solved

logical palette borland builder

Posted on 2001-08-10
5
800 Views
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;
GetSystemPaletteEntries(canvas->Handle,0,256,SysPal.lPal.palPalEntry);

// 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 );
   else
   {
      char msg[16];
      sprintf(msg, "%d", num);
      MessageBox(NULL, msg, "doh", MB_OK );     // reports zero colors mapped
   }

   frmMain->imgMain->Refresh();

}
0
Comment
Question by:lirtua
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

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

LOGPALETTE* myPalette;
Graphics::TBitmap *myBitmap;

randomize();
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;
try{
myBitmap->LoadFromFile("factory.bmp");
myBitmap->Palette = ::CreatePalette(myPalette);
Image1->Canvas->Draw(0,0,myBitmap);
}
catch(...){
Application->MessageBox("Could not load bitmap file",NULL,MB_OK);
delete myBitmap;
myBitmap = NULL;
};

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

0
 

Author Comment

by:lirtua
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.

0
 
LVL 14

Expert Comment

by:AlexVirochovsky
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];
   fread(buff,len,1,fIn);
   fclose(fIn);
                            //make pakette
   BITMAPFILEHEADER bmfHdr;
   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
        randomize();
        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");
       fwrite(buff,len,1,fOut);
       fclose(fOut);
       delete [] myPalette;
     }
   delete [] buff;
}

Alex
0
 
LVL 14

Accepted Solution

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

Author Comment

by:lirtua
ID: 6520654
Yes it was helpful - thanks Alex.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now