Q: How do I display a 256 color bitmap?

A: To display a 256 color bitmap, you must use the palette information that is in the bitmap. With the palette information, you create a GDI object known as a logical palette, using the function CreatePalette(). You must also call the functions SelectPalette() and RealizePalette() before displaying the bitmap. The SelectPalette() function is used to select a logical palette into the device context. The RealizePalette() function maps color entries in the logical palette that is selected into the device context to entries in the system palette.

Since the system palette may be shared by more than one application, Windows uses a few messages for sharing the palette. When an application is about to receive the input focus, Windows sends the window a WM_QUERYNEWPALETTE message. If the application uses the palette, it should realize its logical palette, invalidate the window's client area, and return TRUE to indicate that it has changed the system palette.

When the system palette has been changed, Windows sends WM_PALETTECHANGED messages to all the inactive windows, from top to bottom in the Z-order. A window that uses the palette should do one of two things:

- It can realize its palette by calling RealizePalette(), and redraw the client area. When realizing its palette, colors are mapped to any free entries in the system palette that remain, otherwise, they are mapped to the closest colors in the system palette.
- It can call the function UpdateColors(). This function quickly maps existing system palette colors on the screen to the closest colors in the new system palette. The accuracy of the matched colors decreases every time this function is called, so a bitmap color's will become more and more distorted, until the window is brought to the foreground.

The following example displays a 256 color bitmap:

``

    // bmp256.c
    // copyright 1995 Robert Mashlan

    #define STRICT
    #include <windows.h>
    #include <windowsx.h>
    #include <commdlg.h>
    #include <string.h>

    #include "bmp256.rh"

    #define LOCALFUNC static NEAR PASCAL

    #define WM_OPENFILE WM_USER // message used to open a file

    // app's HINSTANCE
    HINSTANCE hInst;

    int LOCALFUNC WMPaint( HWND hwnd )
    // WM_PAINT message handler
    {
       PAINTSTRUCT ps;
       BITMAP bm;
       HDC hdc = BeginPaint(hwnd,&ps);
       HBITMAP hbmp = (HGLOBAL)GetProp(hwnd,"bmp");
       HPALETTE hPal = (HPALETTE)GetProp(hwnd,"palette");
       HPALETTE hPalOld;
       HDC hdcMem = CreateCompatibleDC(hdc);
       if(hPal) {
          hPalOld = SelectPalette(hdc,hPal,FALSE);
          RealizePalette(hdc);
       }
       GetObject(hbmp,sizeof(bm),(LPSTR)&bm);
       hbmp = SelectObject(hdcMem,hbmp);
       BitBlt(hdc,0,0,bm.bmWidth,bm.bmHeight,hdcMem,0,0,SRCCOPY);
       hbmp = SelectObject(hdcMem,hbmp);
       if(hPalOld)
          SelectPalette(hdc,hPalOld,FALSE);
       DeleteDC(hdcMem);
       EndPaint(hwnd,&ps);
       return 0;
    }

    #define ISWIN30DIB(lpbi) ((*(LPDWORD)(lpbi))==sizeof(BITMAPINFOHEADER))
    #define GETDIBBITS(lpbi) (lpbi+(WORD)(*(LPDWORD)lpbi)+PaletteSize(lpbi))

    WORD DIBNumColors(LPSTR lpbi)
    // returns the number of palette entries in a memory DIB
    {
       WORD wBitCount;
       if(ISWIN30DIB(lpbi)) {
          // use the biClrUsed entry, if non-zero
          DWORD dwClrUsed = ((LPBITMAPINFOHEADER)lpbi)->biClrUsed;
          if(dwClrUsed)
             return (WORD)dwClrUsed;
       }
       // Calculate the number of colors in the color table based on
       //  the number of bits per pixel for the DIB.
       if(ISWIN30DIB (lpbi))
          wBitCount = ((LPBITMAPINFOHEADER)lpbi)->biBitCount;
       else
          wBitCount = ((LPBITMAPCOREHEADER)lpbi)->bcBitCount;
       switch(wBitCount) {
          case 1:
             return 2;
          case 4:
             return 16;
          case 8:
             return 256;
          default:
             return 0;
       }
    }

    WORD PaletteSize(LPSTR lpbi)
    // returns the size of the palette in a memory DIB
    {
       WORD wResult = DIBNumColors(lpbi);
       if(ISWIN30DIB(lpbi))
          return(wResult*sizeof(RGBQUAD));
       else
          return(wResult*sizeof(RGBTRIPLE));
    }

    HPALETTE LOCALFUNC CreateDIBPalette( HGLOBAL hDIB )
    // this function creates a logical palette from a
    // in-memory DIB
    {
       LPLOGPALETTE lpPal;
       HPALETTE  hPal = NULL;
       int       i, wNumColors;
       LPSTR     lpbi = NULL;
       LPBITMAPINFO  lpbmi;
       LPBITMAPCOREINFO lpbmc;

       if (!hDIB) return NULL;
       lpbi = GlobalLock(hDIB);
       lpbmi = (LPBITMAPINFO)lpbi;
       lpbmc = (LPBITMAPCOREINFO)lpbi;
       wNumColors = DIBNumColors(lpbi);
       if(wNumColors) {
          lpPal = (LPLOGPALETTE)GlobalAllocPtr(GHND,sizeof(LOGPALETTE)+
                                 sizeof(PALETTEENTRY)*wNumColors);
          if(!lpPal) {
             GlobalUnlock(hDIB);
             return NULL;
          }
          lpPal->palVersion = 0x300;
          lpPal->palNumEntries = wNumColors;
          for(i=0;i<wNumColors;i++) {
             if(ISWIN30DIB(lpbi)) {
                lpPal->palPalEntry[i].peRed =
                   lpbmi->bmiColors[i].rgbRed;
                lpPal->palPalEntry[i].peGreen =
                   lpbmi->bmiColors[i].rgbGreen;
                lpPal->palPalEntry[i].peBlue  =
                   lpbmi->bmiColors[i].rgbBlue;
                lpPal->palPalEntry[i].peFlags = 0;
             } else {
                lpPal->palPalEntry[i].peRed =
                   lpbmc->bmciColors[i].rgbtRed;
                lpPal->palPalEntry[i].peGreen =
                   lpbmc->bmciColors[i].rgbtGreen;
                lpPal->palPalEntry[i].peBlue =
                   lpbmc->bmciColors[i].rgbtBlue;
                lpPal->palPalEntry[i].peFlags = 0;
             }
          }
          hPal = CreatePalette(lpPal);
          GlobalFreePtr(lpPal);
       }
       GlobalUnlock(hDIB);
       return hPal;
    }

    HBITMAP LOCALFUNC DIBToBMP(HGLOBAL hDIB, HPALETTE hPal)
    // convert a DIB in a global memory block to a DDB
    {
       LPSTR    lpDIBHdr;
       HBITMAP  hbmp;
       HDC      hdc;
       HPALETTE hPalOld = NULL;

       if (!hDIB)
          return NULL;

       lpDIBHdr = GlobalLock(hDIB);
       hdc = GetDC(NULL);
       if(!hdc) {
          GlobalUnlock(hDIB);
          return NULL;
       }
       if (hPal) {
          hPalOld = SelectPalette(hdc,hPal,FALSE);
          RealizePalette(hdc);
       }
       hbmp = CreateDIBitmap(hdc,
          (LPBITMAPINFOHEADER)lpDIBHdr,
          CBM_INIT,
          GETDIBBITS(lpDIBHdr),
          (LPBITMAPINFO)lpDIBHdr,
          DIB_RGB_COLORS);
       if(hPalOld)
          hPalOld = SelectPalette(hdc,hPalOld,FALSE);
       ReleaseDC(NULL,hdc);
       GlobalUnlock(hDIB);
       return hbmp;
    }

    BOOL LOCALFUNC ReadDIBFile( HWND hwnd, LPCSTR lpstrFileName )
    // reads a DIB file, and converts it to a DDB, and creates the
    // logical palette.
    {
       BITMAPFILEHEADER bmfh;
       OFSTRUCT of;
       DWORD dwSize;
       HGLOBAL hDat = NULL;
       LPSTR lpDat = NULL;
       HFILE hf = OpenFile(lpstrFileName,&of,OF_READ);
       HPALETTE hPal;
       if( hf == HFILE_ERROR ) goto failed;
       if( _hread(hf,&bmfh,sizeof(bmfh) ) != sizeof(bmfh) ) goto failed;
       if( bmfh.bfType != 'BM' ) goto failed;
       dwSize = _llseek(hf,0,2) - sizeof(bmfh);
       _llseek(hf,sizeof(bmfh),0);
       hDat = GlobalAlloc(GHND,dwSize);
       if( !hDat ) goto failed;
       lpDat = GlobalLock( hDat );
       _hread(hf,lpDat,dwSize);
       GlobalUnlock(hDat);
       hPal = CreateDIBPalette(hDat);
       SetProp(hwnd,"palette",hPal);
       SetProp(hwnd,"bmp",DIBToBMP(hDat,hPal));
       GlobalFree(hDat);
       _lclose(hf);
       return TRUE;
    failed:
       if( lpDat )
          GlobalUnlock(hDat);
       if( hDat )
          GlobalFree(hDat);
       if( hf != HFILE_ERROR )
          _lclose(hf);
       return FALSE;
    }

    void LOCALFUNC OpenDIBFile( HWND hwnd, LPCSTR lpstrFileName )
    // read DIB file, updates title and display
    {
       char szTitle[80] = "256 Color BMP Viewer";
       HPALETTE hPal = GetProp(hwnd,"palette");
       HBITMAP hbmp = GetProp(hwnd,"bmp");
       if(hPal) {
          DeleteObject(hPal);
          RemoveProp(hwnd,"palette");
       }
       if(hbmp) {
          DeleteObject(hbmp);
          RemoveProp(hwnd,"bmp");
       }
       if( lpstrFileName ) {
          ReadDIBFile(hwnd,lpstrFileName);
          lstrcat(szTitle," - ");
          GetFileTitle(lpstrFileName,szTitle+lstrlen(szTitle),
             sizeof(szTitle)-lstrlen(szTitle));
       }
       SetWindowText(hwnd,szTitle);
       InvalidateRect(hwnd,NULL,TRUE);
    }

    void LOCALFUNC OpenFileDlg( HWND hwnd )
    // displays Open File dialog
    {
       char szFileName[80] = "";
       static OPENFILENAME ofn;

       ofn.lStructSize = sizeof(ofn);
       ofn.hwndOwner = hwnd;
       ofn.hInstance = hInst;
       ofn.lpstrFilter = "bitmaps\000*.bmp;*.dib;*.rle\000";
       ofn.lpstrFile = szFileName;
       ofn.nMaxFile = sizeof(szFileName);
       ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST;
       ofn.lpstrDefExt = "BMP";
       if( GetOpenFileName(&ofn) )
          SendMessage(hwnd,WM_OPENFILE,0,(LPARAM)(LPSTR)szFileName);
    }

    int LOCALFUNC RealizeMyPalette( HWND hwnd )
    // realize palette to screen DC, return number of changed entries
    {
       HDC hdc = GetDC(hwnd);
       HPALETTE hPal = GetProp(hwnd,"palette");
       int i = 0;
       if(hPal) {
          hPal = SelectPalette(hdc,hPal,FALSE);
          i = RealizePalette(hdc);
          SelectPalette(hdc,hPal,FALSE);
          ReleaseDC(hwnd,hdc);
       }
       return i;
    }

    LRESULT CALLBACK _export MainWndProc( HWND hwnd, UINT msg,
       WPARAM wParam, LPARAM lParam )
    {
       switch(msg) {

          case WM_DESTROY:
             OpenDIBFile(hwnd,NULL);
             PostQuitMessage(0);
             return 0;

          case WM_PAINT:
             return WMPaint(hwnd);

          case WM_PALETTECHANGED:
             if( (HWND)wParam != hwnd && GetProp(hwnd,"palette") )
                if( RealizeMyPalette(hwnd) )
                   InvalidateRect(hwnd,NULL,TRUE);
             return 0;

          case WM_QUERYNEWPALETTE: {
             if(RealizeMyPalette(hwnd)) {
                InvalidateRect(hwnd,NULL,TRUE);
                return TRUE;
             }
             return 0;
          }

          case WM_OPENFILE:
             OpenDIBFile(hwnd,(LPSTR)lParam);
             return 0;

          case WM_COMMAND:
             switch(wParam) {
                case CM_EXIT:
                   SendMessage(hwnd,WM_CLOSE,0,0);
                   return 0;
                case CM_FILEOPEN:
                   OpenFileDlg(hwnd);
                   return 0;
             }
             return 0;

       }
       return DefWindowProc(hwnd,msg,wParam,lParam);
    }

    int MessagePump(void)
    {
       MSG msg;
       while( GetMessage(&msg,NULL,0,0) ) {
          TranslateMessage(&msg);
          DispatchMessage(&msg);
       }
       return msg.wParam;
    }

    #pragma argsused
    int PASCAL WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,
                        LPSTR lpstrCmdLine, int nCmdShow )
    {
       char szClassName[] = "256BmpDemo";
       HWND hwnd;
       hInst = hInstance;
       if(!hPrevInst) {
          WNDCLASS wc;
          memset(&wc,0,sizeof(wc));
          wc.style         = CS_HREDRAW|CS_VREDRAW;
          wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
          wc.hCursor       = LoadCursor(NULL,IDC_ARROW);
          wc.hIcon         = LoadIcon(NULL,IDI_APPLICATION);
          wc.lpfnWndProc   = MainWndProc;
          wc.lpszClassName = szClassName;
          wc.lpszMenuName  = MAKEINTRESOURCE(MENU_MAIN);
          wc.hInstance     = hInst;
          RegisterClass(&wc);
       }
       hwnd = CreateWindow(szClassName,"256 Color Bitmap",
                           WS_OVERLAPPEDWINDOW,
                           CW_USEDEFAULT,CW_USEDEFAULT,500,500,
                           NULL,NULL,hInst,NULL);
       if(hwnd) {
          if( lstrlen(lpstrCmdLine) )
             SendMessage(hwnd,WM_OPENFILE,0,(LPARAM)lpstrCmdLine);
          ShowWindow(hwnd,nCmdShow);
          UpdateWindow(hwnd);
          return MessagePump();
       }
       return -1;
    }

Listing for bmp256.rh:

``

    /* bmp256.rh */
    #define MENU_MAIN     1001
    #define CM_FILEOPEN   102
    #define CM_EXIT       101
    Listing for bmp256.rc:
    /* bmp256.rc */
    #include "bmp256.rh"

    MENU_MAIN MENU
    {
     POPUP "&File"
     {
      MENUITEM "&Open...", CM_FILEOPEN
      MENUITEM "E&xit", CM_EXIT
     }
    }

Listing for bmp256.def:

``

    NAME        BMP256
    DESCRIPTION "256 Color DIB demo - Copyright 1995 Robert Mashlan"
    EXETYPE     WINDOWS
    CODE        LOADONCALL MOVEABLE DISCARDABLE
    DATA        PRELOAD MOVEABLE MULTIPLE
    HEAPSIZE    1024
    STACKSIZE   8196

Copyright 1995 Robert Mashlan
Return to Windows Developer FAQ - Graphics
Comments?