Solved

logical palette borland builder

Posted on 2001-08-10
5
832 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
[X]
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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 …
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 clear a vector as well as how to detect empty vectors in C++.

742 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