Changing a CBitmap's Palette

Environment: VC6 SP5

Sometimes, it is useful or at least desirable to use one bitmap and simply change its palette to achieve various graphical effects. Unfortunately, I have been unable to find any way to directly manipulate a CBitmap's palette information. What I have done in the function below is select the bitmap into a device context and manipulate the device context's color table. By applying the palette to the device context, you then can extract the bitmap with its new palette from the device context and reattach it to the original CBitmap object.

The function below takes two parameters: a CBitmap which contains a 4-bit-per-pixel or 8-bit-per-pixel (paletted) device-independent bitmap and a CPalette object that must have the same number of colors as the CBitmap object. After all, validation is performed in the first step to get the palette information from the CPalette object and put it into an array of RGBQUADs (that's how the DC expects it). Then, the bitmap is selected into a memory DC and the new palette is applied. Finally, the bitmap is extracted from the DC with the new palette information and reattached to the CBitmap object.

The function returns FALSE on failure and TRUE on success. Any memory that was dynamically allocated is cleaned up regardless of a failure to convert the bitmap's palette.

BOOL ApplyPaletteToBitmap(CBitmap &bitmap, CPalette &palette) {
   if (bitmap.m_hObject == NULL || palette.m_hObject == NULL)
      return FALSE;

   BITMAP bmp;
   bitmap.GetBitmap(&bmp);

   int npalColors = palette.GetEntryCount();

   // Bitmaps with 16 or 256 colors use palettes
   // Make sure the palette has enough entries for the bitmap
   // as well
   if ((bmp.bmBitsPixel != 4 && bmp.bmBitsPixel != 8) ||
       (bmp.bmBitsPixel == 4 && npalColors != 16) ||
       (bmp.bmBitsPixel == 8 && npalColors != 256))
      return FALSE;

   LPPALETTEENTRY pPalEntry = NULL;
   RGBQUAD *pRGB = NULL;
   BOOL ret = TRUE;

   try {
      // Create the palette
      pPalEntry = new PALETTEENTRY[npalColors];
      pRGB = new RGBQUAD[npalColors];

      palette.GetPaletteEntries(0, npalColors, pPalEntry);

      for (int i = 0; i < npalColors; i++) {
         pRGB[i].rgbRed   = pPalEntry[i].peRed;
         pRGB[i].rgbGreen = pPalEntry[i].peGreen;
         pRGB[i].rgbBlue  = pPalEntry[i].peBlue;
         pRGB[i].rgbReserved = 0;
      }

      CClientDC dc(NULL);
      CDC memDC;
      memDC.CreateCompatibleDC(&dc);
      memDC.SelectObject(&bitmap);

      SetDIBColorTable(memDC, 0, npalColors, pRGB);

      bitmap.Attach((HBITMAP)(memDC.GetCurrentBitmap()->Detach()));
   }
   catch (...) {
      ret = FALSE;
   }

   if (pPalEntry)
      delete [] pPalEntry;
   if (pRGB)
      delete [] pRGB;

   return ret;
}


Comments

  • CreateDIBSection

    Posted by Legacy on 07/02/2003 12:00am

    Originally posted by: hoan

    instead of do that, you create DIBSection bitmap and then do what you wan. You can set color table for this bitmap by SetDIBColorTable and you also modify direct bitmap data by the pointer to Bits returned by CreateDIBSection!

    Regards.

    Reply
  • yaaa

    Posted by Legacy on 02/24/2003 12:00am

    Originally posted by: prakash

    this is quite wonderful.
    i want more information on the reading of tiff format and processing it . how can i convert grey scale image to a RGB format? what is psudo coloring

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • In support of their business continuity and disaster recovery plans, many midsized companies endeavor to avoid putting all their eggs in one basket. Understanding the critical role of last-mile connectivity and always available Internet access for their enterprises, savvy firms utilize redundant connections from multiple service providers. Despite the good intentions, their Internet connectivity risk may still be in a single basket. That is because internet service providers (ISPs) and competitive local …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds