Solved

Display two 256 color images in one view.

Posted on 2000-04-04
2
163 Views
Last Modified: 2010-04-02
My problem is as follows. I am trying to display two gif images onto the same view. The problems arise when my graphics adapter is at 256 colors.

Inserting one image onto the view is o.k. The gif seems fine. When I add another image to the view the palette of image 1 get trampled over by the palette of image 2.

I can have a number of gifs onscreen and display each of them in turn by calling the following function :

BOOL CGifObject::DisplayGif(CDC* pCDC,int x,int y)
{
      if (!GIFLoaded) return FALSE;

      CPalette *oldpal;
          oldpal = pCDC->SelectPalette(&pal,TRUE);
      pCDC->RealizePalette();

      if (!hBMP)
      {
            hBMP = CreateDIBitmap(pCDC->m_hDC,(LPBITMAPINFOHEADER)&bmi->bmiHeader,CBM_INIT,
                                    (LPSTR)imageBits,(LPBITMAPINFO)bmi,DIB_RGB_COLORS);
      }
      
      if (Transparent)
        {
            DrawTransparentBitmap(pCDC->m_hDC, hBMP, x,y,
                        RGB(bmi->bmiColors[Transparent_Color].rgbRed,
                              bmi->bmiColors[Transparent_Color].rgbGreen,
                              bmi->bmiColors[Transparent_Color].rgbBlue));
      }
      else
      {
               CDC hdcTemp;
               hdcTemp.CreateCompatibleDC(pCDC);
               HBITMAP oldbmp = (HBITMAP)hdcTemp.SelectObject(hBMP);
            pCDC->BitBlt(x,y,cx,cy,&hdcTemp,xSrcOff,ySrcOff,SRCCOPY);
               hdcTemp.SelectObject(oldbmp);
               hdcTemp.DeleteDC();
               // destructor will delete DC
               
      }

      pCDC->SelectPalette(oldpal,TRUE);
      
      // 1. pCDC->RealizePalette();
      return TRUE;
}

At point 1. I tried to realize the old palette again but that didn't work. Even if only one image is displayed all the colors are screwed up.

I have encountered the same problem with bmp's. Here is the code for the DisplayBMP function :

BOOL CGifObject::DisplayBMP(CDC *pDC, int x, int y)
{    
      BITMAP                   bm;
      HBITMAP             hbmp;
      CBitmap                  *oldBmp;                        
      CDC                    memDC;
       
   
      //Test to see if image to display
      if (!BMPLoaded || pDC == NULL || m_hbits==NULL || m_hhdr==NULL)
      {
            return FALSE ;
      }
     
      CPalette *oldPal = NULL;

      if (m_cPal)            // we have a palette wrt the image
      {
            oldPal = pDC->SelectPalette(m_cPal,TRUE);
      }
      pDC->RealizePalette();

      m_pbits =  (char *)GlobalLock(m_hbits);    
      m_pHdr  =  (LPSTR)GlobalLock(m_hhdr);
      m_bmi   =  (BITMAPINFO*) m_pHdr;                   //Create a pointer to a  BITMAP info structure
      m_bmih  =  (BITMAPINFOHEADER *) m_pHdr;     //Create a pointer to a BITMAPINFOHEADER structure
      
      //Create a handle to the BMP
      hbmp = CreateDIBitmap(pDC->m_hDC, m_bmih, CBM_INIT, m_pbits, m_bmi, DIB_RGB_COLORS);                                 

      CBitmap *bmp = CBitmap::FromHandle(hbmp);
      
      
      GetObject(hbmp, sizeof(BITMAP), (LPVOID)&bm);
      memDC.CreateCompatibleDC(pDC);               
          
      oldBmp = (CBitmap*)memDC.SelectObject(bmp);
          
      //Draw bitmap on screen
      pDC->BitBlt(x,y,cx,cy,&memDC,xSrcOff,ySrcOff,SRCCOPY) ;
          
      //Reset device
      memDC.SelectObject(oldBmp);
          
      //Delete device
      memDC.DeleteDC();
          
      //Reset palette
      if (oldPal)
            pDC->SelectPalette(oldPal,TRUE);
          
      GlobalUnlock(m_hhdr);    
      GlobalUnlock(m_hbits);

      DeleteObject(hbmp);
         
      return 1;      
}

Is this the same problem???

Can somebody help me with the palette manager before I tear my hair out!!!

Thanx, Gordon.
0
Comment
Question by:jeffreyg
2 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 100 total points
ID: 2685932
Since each individual image has its own palette, the quality of the images have to be compromised. You may create a halftone palette (CPalette::CreateHalftonePalette) and use it for all the images.

Check out the following GDI Technical Articles in the MSDN Library.

- Palette Awareness
- The Palette Manager: How and Why
- Using DIBs with Palettes
0
 

Author Comment

by:jeffreyg
ID: 2689201
Thanks, the HalftonePalette worked. I now can display multiple images with slight, but satisfactory degradation.

Gordon.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

828 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