//********************************************************************************
void Create16ColorsStdPalette (CPalette& pal)
{
	const int nStdColorCount = 20;
	CPalette* pPalDefault = CPalette::FromHandle ((HPALETTE) ::GetStockObject (DEFAULT_PALETTE));
	if (pPalDefault == NULL)
	{
		ASSERT (FALSE);
		return;
	}

	const int nColors = 16;
	UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
	LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];

	pLP->palVersion = 0x300;
	pLP->palNumEntries = (USHORT) nColors;

	pal.CreatePalette (pLP);

	delete[] pLP;

	PALETTEENTRY palEntry;
	int iDest = 0;

	for (int i = 0; i < nStdColorCount; i++)
	{
		if (i < 8 || i >= 12)
		{
			pPalDefault->GetPaletteEntries (i, 1, &palEntry);
			pal.SetPaletteEntries (iDest++, 1, &palEntry);
		}
	}
}
void CDib::CreatePaletteFromImage( CPalette& palette)const
{
	if( palette.m_hObject )
		palette.DeleteObject();


	ASSERT( IsIndexed() == GetBPP() <= 8);
	if( GetBPP() <= 8)
	{
		GetPalette(palette);
		//CPalette* pal =  CPalette::FromHandle(m_hPalette);
		//PALETTEENTRY colors[256] = {0};
		//pal->GetPaletteEntries(0, 256, colors);
		
		//palette.SetPaletteEntries( 0, 256, colors );
	}
	else
	{
		LPLOGPALETTE pLogPal = (LPLOGPALETTE) new char[2 * sizeof(WORD) +
		256 * sizeof(PALETTEENTRY)];

		pLogPal->palVersion = 0x300;
		pLogPal->palNumEntries = 256;

		palette.CreatePalette(pLogPal);
		delete pLogPal;	
		pLogPal = NULL;

	
		
		CArray<COLORREF, COLORREF> colorArray;

		int height= GetHeight();
		int width = GetWidth();
		for(int i=0; i<width; i++)
		{
			for(int j=0; j<height; j++)
			{
				COLORREF color = GetPixel(i, j);
				if( UtilWin::FindInArray( colorArray, color) == -1)
					colorArray.Add(color);
				if( colorArray.GetSize() == 256)
					break;
			}
			
			if( colorArray.GetSize() == 256)
					break;
		}

		PALETTEENTRY pe[256]={0};
    
		
		for (int i = 0; i < colorArray.GetSize(); i++) 
		{
			COLORREF color = colorArray[i];
			pe[i].peRed = GetRValue(color);
			pe[i].peGreen = GetGValue(color);
			pe[i].peBlue = GetBValue(color);
		}

		palette.SetPaletteEntries(0, 256, pe);
	}
}
Exemple #3
0
void IW::CRender::Play(HENHMETAFILE hemf)
{
	IW::CDCRender dc(*this);
	bool bHasPal = dc.GetDeviceCaps(BITSPIXEL) <= 8;

	// Create a logical palette from the colors in the color table
	UINT nColors = GetEnhMetaFilePaletteEntries(hemf, 0, NULL);
	CPalette pal;

	if (bHasPal && nColors != GDI_ERROR && nColors != 0)
	{
		bHasPal = false;
		DWORD dwSize = sizeof (LOGPALETTE) + ((nColors - 1) * sizeof (PALETTEENTRY));

		LPLOGPALETTE logPalette = IW_ALLOCA(LPLOGPALETTE, dwSize);

		logPalette->palVersion = 0x300;
		logPalette->palNumEntries = nColors;

		if (nColors == GetEnhMetaFilePaletteEntries(hemf, nColors, logPalette->palPalEntry))
		{
			pal.CreatePalette(logPalette);
		}
	}

	HPALETTE hOldPal = NULL;

	if (bHasPal)
	{
		hOldPal = dc.SelectPalette(pal, TRUE);
		dc.RealizePalette();
	}

	CRect rcClip = _pSurface->GetClipRect();

	int nHeightRender = rcClip.bottom - rcClip.top;
	int nWidthRender = rcClip.right - rcClip.left;

	CBrush brush;
	CRect r(0, 0, nWidthRender, nHeightRender);

	if (brush.CreateSolidBrush(RGB(255,255,255)))
	{		
		dc.FillRect(&r, brush);
	}

	PlayEnhMetaFile(dc, hemf, &r);
	::GdiFlush();

	if (hOldPal)
		dc.SelectPalette(hOldPal, FALSE);
}
Exemple #4
0
BOOL WriteWindow2Mem(BYTE* buf, int W, int H, HDC hDC)
{
	CBitmap 	bitmap;
	CDC 		memDC;
	CRect		rect;

	CDC dc;
	dc.Attach(hDC);
	CDC* pDC = &dc;

	bitmap.CreateCompatibleBitmap(pDC, W,H );

	CBitmap* pOldBitmap = memDC.SelectObject(&bitmap);

	memDC.BitBlt(0, 0, W,H, pDC, 0, 0, SRCCOPY);

	// Create logical palette if device support a palette
	CPalette pal;
	if( pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE )
	{
		UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256);
		LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
		pLP->palVersion = 0x300;

		pLP->palNumEntries =
			::GetSystemPaletteEntries( *pDC, 0, 255, pLP->palPalEntry );

		// Create the palette
		pal.CreatePalette( pLP );

		delete[] pLP;
	}

	memDC.SelectObject(pOldBitmap);

	// Convert the bitmap to a DIB
	HANDLE hDIB = DDBToDIB( bitmap, BI_RGB, &pal );

	if( hDIB == NULL )
		return FALSE;

	// Write it to file
	WriteDIB2Mem(hDIB, W, H, buf);

	// Free the memory allocated by DDBToDIB for the DIB
	GlobalFree( hDIB );

	dc.Detach();

	return TRUE;
}
Exemple #5
0
BOOL CClockST::GetBitmapAndPalette(UINT nIDResource, CBitmap& bitmap, CPalette& pal)
{
	HINSTANCE	hInstResource	= NULL;

	// Find correct resource handle
	hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nIDResource), RT_BITMAP/*RT_GROUP_BITMAP*/);

	HBITMAP hBmp = (HBITMAP)::LoadImage( hInstResource, 
		MAKEINTRESOURCE(nIDResource), IMAGE_BITMAP, 0,0, LR_CREATEDIBSECTION);

	if (hBmp == NULL) return FALSE;

	bitmap.Attach(hBmp);

	// Create a logical palette for the bitmap
	DIBSECTION ds;
	BITMAPINFOHEADER &bmInfo = ds.dsBmih;
	bitmap.GetObject(sizeof(ds), &ds);

	int nColors = bmInfo.biClrUsed ? bmInfo.biClrUsed : 1 << bmInfo.biBitCount;

	// Create a halftone palette if colors > 256. 
	CClientDC dc(NULL); // Desktop DC

	if(nColors > 256)
		pal.CreateHalftonePalette(&dc);
	else
	{
		// Create the palette
		RGBQUAD *pRGB = new RGBQUAD[nColors];
		CDC memDC;
		memDC.CreateCompatibleDC(&dc);
		memDC.SelectObject( &bitmap );
		::GetDIBColorTable( memDC, 0, nColors, pRGB );
		UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
		LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
		pLP->palVersion = 0x300;
		pLP->palNumEntries = nColors;
		for (int i=0; i < nColors; i++)
		{
			pLP->palPalEntry[i].peRed = pRGB[i].rgbRed;
			pLP->palPalEntry[i].peGreen = pRGB[i].rgbGreen;
			pLP->palPalEntry[i].peBlue = pRGB[i].rgbBlue;
			pLP->palPalEntry[i].peFlags = 0;
		}
		pal.CreatePalette( pLP );
		delete[] pLP;
		delete[] pRGB;
	}
	return TRUE;
} // End of GetBitmapAndPalette
void initMapPalette()
{
	LOGPALETTE* pPal = (LOGPALETTE*) malloc(sizeof(LOGPALETTE) 
	                         + 256 * sizeof(PALETTEENTRY));
	pPal->palVersion = 0x300;  // Windows 3.0
	pPal->palNumEntries = 256; // table size
	for (int i=0; i<256; i++)  {
		pPal->palPalEntry[i].peRed = (UInt8)i;
		pPal->palPalEntry[i].peGreen = 0;
		pPal->palPalEntry[i].peBlue = 0;
		pPal->palPalEntry[i].peFlags = PC_EXPLICIT;
	}
	gMapPalette.CreatePalette(pPal);
	free (pPal);
}
Exemple #7
0
/////////////////////////////////////////////////////////////////////////////
//
// Create the palette.  Use halftone palette for hi-color bitmaps.
//
/////////////////////////////////////////////////////////////////////////////
zBOOL
ZDib::CreatePalette( CPalette& pal )
{
   // Should not already have palette
   ASSERT( pal.m_hObject == 0 );

   zBOOL bRC = FALSE;
   RGBQUAD *colors = new RGBQUAD[ MAXPALCOLORS ];
   UINT nColors = GetColorTable( colors, MAXPALCOLORS );
   if ( nColors > 0 )
   {
      // Allocate memory for logical palette.
      int nLth = sizeof( LOGPALETTE ) + sizeof( PALETTEENTRY ) * nColors;
      LOGPALETTE *pLogPal = (LOGPALETTE *) new char[ nLth ];
      if ( pLogPal == 0 )
         return( 0 );

      // Set version and number of palette entries.
      pLogPal->palVersion = PALVERSION;
      pLogPal->palNumEntries = nColors;

      // Copy color entries.
      for ( UINT k = 0; k < nColors; k++ )
      {
         pLogPal->palPalEntry[ k ].peRed   = colors[ k ].rgbRed;
         pLogPal->palPalEntry[ k ].peGreen = colors[ k ].rgbGreen;
         pLogPal->palPalEntry[ k ].peBlue  = colors[ k ].rgbBlue;
         pLogPal->palPalEntry[ k ].peFlags = 0;
      }

      // Create the palette and destroy LOGPAL.
      bRC = pal.CreatePalette( pLogPal );
      delete [] (zPCHAR) pLogPal;
   }
   else
   {
      CWindowDC dcScreen( 0 );
      bRC = pal.CreateHalftonePalette( &dcScreen );
   }

   delete [] colors;
   return( bRC );
}
BOOL CDib::GetPalette(CPalette& palette)const
{
	if( (HPALETTE)palette)
		palette.DeleteObject();

	/*HDC hDC = GetDC();
	CPalette* pPalette = CPalette::FromHandle( (HPALETTE)GetCurrentObject(hDC, OBJ_PAL));
	int iColors = 0;
	if (!pPalette->GetObject(sizeof(int), &iColors)) 
	{
		TRACE("Failed to get num palette colors");
		return FALSE;
	}
	ReleaseDC();
	*/

	int iColors = GetMaxColorTableEntries();
    if(iColors==0)
		return FALSE;

	RGBQUAD quad[256] = {0};
    //LPRGBQUAD pctThis = (LPRGBQUAD) &quad;
	GetColorTable(0, iColors, &quad[0]);

	LPLOGPALETTE pLogPal = (LPLOGPALETTE) new char[2 * sizeof(WORD) +
		iColors * sizeof(PALETTEENTRY)];
	pLogPal->palVersion = 0x300;
	pLogPal->palNumEntries = iColors;
	
	for (int i = 0; i < iColors; i++) 
    {
        pLogPal->palPalEntry[i].peRed = quad[i].rgbRed;
        pLogPal->palPalEntry[i].peGreen = quad[i].rgbGreen;
        pLogPal->palPalEntry[i].peBlue = quad[i].rgbBlue;
    }

	palette.CreatePalette(pLogPal);
	delete pLogPal;

	return true;
}
//////////////////
// Create the palette. Use halftone palette for hi-color bitmaps.
//
BOOL CDib::CreatePalette(CPalette& pal)
{ 
	// should not already have palette
	ASSERT(pal.m_hObject==NULL);

	BOOL bRet = FALSE;
	RGBQUAD* colors = new RGBQUAD[MAXPALCOLORS];
	UINT nColors = GetColorTable(colors, MAXPALCOLORS);
	if (nColors > 0) {
		// Allocate memory for logical palette 
		int len = sizeof(LOGPALETTE) + sizeof(PALETTEENTRY) * nColors;
		LOGPALETTE* pLogPal = (LOGPALETTE*)new char[len];
		if (!pLogPal)
			return NULL;

		// set version and number of palette entries
		pLogPal->palVersion = PALVERSION;
		pLogPal->palNumEntries = nColors;

		// copy color entries 
		for (UINT i = 0; i < nColors; i++) {
			pLogPal->palPalEntry[i].peRed   = colors[i].rgbRed;
			pLogPal->palPalEntry[i].peGreen = colors[i].rgbGreen;
			pLogPal->palPalEntry[i].peBlue  = colors[i].rgbBlue;
			pLogPal->palPalEntry[i].peFlags = 0;
		}

		// create the palette and destroy LOGPAL
		bRet = pal.CreatePalette(pLogPal);
		delete [] (char*)pLogPal;
	} else {
		CWindowDC dcScreen(NULL);
		bRet = pal.CreateHalftonePalette(&dcScreen);
	}
	delete colors;
	return bRet;
}
Exemple #10
0
BOOL WriteWindowToDIB( LPTSTR szFile, CWnd* wnd)
{
	CDC *pDC = wnd->GetDC();

	CBitmap 	bitmap;
	CDC 		memDC;
	CRect		rect;

	int W = 640;
	int H = 480;

	memDC.CreateCompatibleDC(pDC);

	wnd->GetWindowRect(rect);

//	W = rect.Width() - 10;
//	H = rect.Height() - 10;

	bitmap.CreateCompatibleBitmap(pDC, W,H );

	CBitmap* pOldBitmap = memDC.SelectObject(&bitmap);

	int x = rect.Width()  - W; x /= 2;
	int y = rect.Height() - H; y /= 2;

	memDC.BitBlt(0, 0, W,H, pDC, x, y, SRCCOPY);

	// Create logical palette if device support a palette
	CPalette pal;
	if( pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE )
	{
		UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256);
		LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
		pLP->palVersion = 0x300;

		pLP->palNumEntries =
			GetSystemPaletteEntries( *pDC, 0, 255, pLP->palPalEntry );

		// Create the palette
		pal.CreatePalette( pLP );

		delete[] pLP;
	}

	memDC.SelectObject(pOldBitmap);

	// Convert the bitmap to a DIB
	HANDLE hDIB = DDBToDIB( bitmap, BI_RGB, &pal );

	if( hDIB == NULL )
		return FALSE;

	// Write it to file
	WriteDIB( szFile, hDIB );

	// Free the memory allocated by DDBToDIB for the DIB
	GlobalFree( hDIB );

	wnd->ReleaseDC(pDC);
	return TRUE;
}