//********************************************************************************
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
CPalette * PaletteFromMapFile(const char * filename)
{
  FILE * fh = fopen(filename, "rt");

  if (! fh)
    return 0;

  CPalette * p = PaletteLinear(false);
  uint i;

  for (i=0; i<257 && !feof(fh); i++)
  {
    char line[256];
    uint r,g,b;

    fgets(line, sizeof(line)-1, fh);
    if (sscanf(line, "%u %u %u", &r,&g,&b) != 3)
    {
      delete p; p=0;
      break;
    }

    p->Add(i/256., r/255., g/255., b/255.);
  }

  fclose(fh);
  return p;
}
BOOL CDib::SetPalette( CPalette& palette )
{
	if( IsIndexed() )
	{
		int iColors = 0;
		if (!palette.GetObject(sizeof(iColors), &iColors)) 
		{
			TRACE("Failed to get num palette colors");
			return FALSE;
		}
		
		ASSERT(iColors>= 0 && iColors<=256);
//		iColors = min(iColors, GetColorTableEntries());
		PALETTEENTRY pe[256];
		palette.GetPaletteEntries(0, iColors, pe);

		RGBQUAD quad[256] = {0};
		for (int i = 0; i < iColors; i++) 
		{
			quad[i].rgbRed = pe[i].peRed;    
			quad[i].rgbGreen = pe[i].peGreen;    
			quad[i].rgbBlue = pe[i].peBlue;
		}

		SetColorTable(0, iColors, &(quad[0]));

		//update copy of the palette
		//GetPalette(m_palette);
	}
	

    return TRUE;
}
Exemple #5
0
CPalette *CPaletteManager::Add (ubyte* buffer)
{
	CPalette	palette;

memcpy (palette.Raw (), buffer, PALETTE_SIZE * 3);
return Add (palette);
}
Exemple #6
0
CPalette * PaletteLinear(COLORREF c)
{
  CPalette * p = PaletteLinear(false);

  p->Add(0.5, GetRValue(c)/255., GetGValue(c)/255., GetBValue(c)/255.);

  return p;
}
Exemple #7
0
CPalette * PaletteSimple(COLORREF c1, COLORREF c2)
{
  CPalette * p = PaletteLinear(false);

  p->Add(0.0, GetRValue(c1)/255., GetGValue(c1)/255., GetBValue(c1)/255.);
  p->Add(1.0, GetRValue(c2)/255., GetGValue(c2)/255., GetBValue(c2)/255.);
  
  return p;
}
Exemple #8
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 #9
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 #10
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
Exemple #11
0
/* calculate table to translate d1 bitmaps to current palette,
 * return -1 on error
 */
CPalette* CPaletteManager::LoadD1 (void)
{
	CPalette	palette;
	CFile 	cf;
	
if (!cf.Open (D1_PALETTE, gameFolders.szDataDir, "rb", 1) || (cf.Length () != 9472))
	return NULL;
palette.Read (cf);
cf.Close ();
palette.Raw () [254] = SUPER_TRANSP_COLOR;
palette.Raw () [255] = TRANSPARENCY_COLOR;
SetD1 (Add (palette));
return D1 ();
}
Exemple #12
0
void PaletteDemoAppApp::draw()
{
    // clear out the window with black
    gl::clear( Color( 0, 0, 0 ) );

    int num_cols = mp_Palette->getSize();

    float width = getWindowWidth() / (float)num_cols;

    for(size_t i=0; i<num_cols; ++i)
    {
        gl::color(mp_Palette->getColor(i));
        gl::drawSolidRect(Rectf(i * width, 0, (i + 1) * width, getWindowHeight()));
    }
}
Exemple #13
0
CPalette *CPaletteManager::Load (const char *pszFile, const char *pszLevel)
{
	CFile		cf;
	int		i = 0, fsize;
	CPalette	palette;
#ifdef SWAP_0_255
	ubyte		c;
#endif

if (pszLevel) {
	char ifile_name [FILENAME_LEN];

	CFile::ChangeFilenameExtension (ifile_name, pszLevel, ".pal");
	i = cf.Open (ifile_name, gameFolders.szDataDir, "rb", 0);
	}
if (!i)
	i = cf.Open (pszFile, gameFolders.szDataDir, "rb", 0);
	// the following is a hack to enable the loading of d2 levels
	// even if only the d2 mac shareware datafiles are present.
	// However, if the pig file is present but the palette file isn't,
	// the textures in the level will look wierd...
if (!i)
	i = cf.Open (DEFAULT_LEVEL_PALETTE, gameFolders.szDataDir, "rb", 0);
if (!i) {
	Error(TXT_PAL_FILES, pszFile, DEFAULT_LEVEL_PALETTE);
	return NULL;
	}
fsize	= cf.Length ();
Assert (fsize == 9472);
palette.Read (cf);
cf.Read (m_data.fadeTable, sizeof (m_data.fadeTable), 1);
cf.Close ();
// This is the TRANSPARENCY COLOR
for (i = 0; i < MAX_FADE_LEVELS; i++)
	m_data.fadeTable [i * 256 + 255] = 255;
// swap colors 0 and 255 of the palette along with fade table entries
#ifdef SWAP_0_255
palette.SwapTransparency ();
for (i = 0; i < MAX_FADE_LEVELS * 256; i++)
	if (m_fadeTable [i] == 0)
		m_fadeTable [i] = 255;
for (i = 0; i < MAX_FADE_LEVELS; i++)
	m_fadeTable [i * 256] = TRANSPARENCY_COLOR;
#endif
ClearEffect (&palette);
return Add (palette);
}
Exemple #14
0
CPalette * CFractalLyapunov::DefaultPalette(bool in)
{
  CPalette * p;

  if (in)
  {
    p = PaletteSingle(0x1B1C1F);
  }
  else
  {
    p = PaletteLinear(false);
    p->Add(0.00, HEXRGB(0x191400));
    p->Add(1.00, HEXRGB(0xFFE119)); //0xFFD40A));
  }

  return p;
}
void CDib::GetSelectionIndex(COLORREF crColor, UtilWin::CIntArray& selectionArray)
{
	selectionArray.RemoveAll();
	
	CPalette palette;
	GetPalette(palette);
	int nbColor = palette.GetEntryCount();
	ASSERT(nbColor >= 0 && nbColor <=256);

	PALETTEENTRY pe[256];
	palette.GetPaletteEntries(0, nbColor, pe);

	for(int i=0; i<nbColor;i++)
	{
		if( RGB( pe[i].peRed, pe[i].peGreen, pe[i].peBlue) == crColor)
			selectionArray.Add(i);
	}
}
Exemple #16
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 );
}
Exemple #17
0
static int MergePalette( CDC *pDC, LPRGBQUAD pC, int nColors ) 
{
	WORD	nOldEntries, nNewEntries;
	COLORREF	rgb;
	int	idx, i;

	CPalette*	pPal = pDC->GetCurrentPalette();
	pPal->GetObject( sizeof( nOldEntries ), &nOldEntries );
	LPLOGPALETTE	lp = (LPLOGPALETTE) new BYTE[ sizeof( LOGPALETTE ) + ( nOldEntries + nColors ) * sizeof ( PALETTEENTRY ) ];
	LPPALETTEENTRY	pe = lp->palPalEntry;

	if( pPal->GetPaletteEntries( 0, nOldEntries, pe ) )
		pe += nOldEntries;

	for( i = 0, nNewEntries = 0; i < nColors; i++ ) {
		rgb = RGB(pC->rgbRed,pC->rgbGreen,pC->rgbBlue);
		idx = pPal->GetNearestPaletteIndex( rgb );
		pPal->GetPaletteEntries( idx, 1, pe );
		if( ( pe->peRed != GetRValue( rgb ) )
			|| pe->peGreen != GetGValue( rgb )
			|| pe->peBlue != GetBValue( rgb ) )
		{
			pe->peRed	= GetRValue( rgb );
			pe->peGreen	= GetGValue( rgb );
			pe->peBlue	= GetBValue( rgb );
			pe->peFlags	= NULL;
			pe++;
			nNewEntries++;
		}
		pC++;
	}
	if( nNewEntries > 0 ) {
		lp->palVersion = PALVERSION;
		lp->palNumEntries = nOldEntries + nNewEntries;
		CPalette	pal, *pOldPal;
		pal.CreatePalette( lp );
		pOldPal = pDC->SelectPalette( &pal, FALSE );
		pDC->RealizePalette();
	}
	delete lp;
	return (int) nNewEntries;
}
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;
}
bool CColorPaletteButton::verifyColor()
{
   CDC* dc = GetDC();

   ASSERT(dc != NULL);
   ASSERT(dc->GetDeviceCaps(RASTERCAPS) & RC_BITBLT);

   CRect clientRect;
   GetClientRect(&clientRect);
   CPoint center((clientRect.left + clientRect.right)/2,(clientRect.top + clientRect.bottom)/2);

   COLORREF colorButtonColor = dc->GetPixel(center);

   m_appPalette.pushPalette(dc);

   PALETTEENTRY paletteEntry;
   CPalette* palette = dc->GetCurrentPalette();
   ASSERT(palette != NULL);
   int numEntries = palette->GetEntryCount();

   VERIFY(palette->GetPaletteEntries(m_appPalette.getEditIndex(),1,&paletteEntry) > 0);

   COLORREF paletteColor = RGB(paletteEntry.peRed,paletteEntry.peGreen,paletteEntry.peBlue);

   bool retval = (colorButtonColor == paletteColor);

   m_appPalette.popPalette(dc);

   if (!retval)
   {
      TRACE("CColorPaletteButton::verifyColor() - %s, buttonColor=0x%08x, paletteColor=0x%08x, \n",
         retval ? "success" : "failure",colorButtonColor,paletteColor);
   }
   else
   {
      TRACE("CColorPaletteButton::verifyColor() - %s, buttonColor=0x%08x, paletteColor=0x%08x, \n",
         retval ? "success" : "failure",colorButtonColor,paletteColor);
   }

   return retval;
}
Exemple #20
0
//-----------------------------------------------------------------------
//our routine to set the palette, called from the movie code
void CMovie::SetPalette (ubyte* p, unsigned start, unsigned count)
{
	CPalette	palette;

if (count == 0)
	return;

////paletteManager.ResumeEffect ();
//Color 0 should be black, and we get color 255
//movie libs palette into our array
if (start + count > PALETTE_SIZE)
	count = PALETTE_SIZE - start;
memcpy (palette.Color () + start, p + start * 3, count * 3);
//Set color 0 to be black
palette.SetBlack (0, 0, 0);
//Set color 255 to be our subtitle color
palette.SetTransparency (50, 50, 50);
//finally set the palette in the hardware
movieManager.m_palette = paletteManager.Add (palette);
//paletteManager.ResumeEffect ();
}
void CGelDoc::OnGelHueRight() 
{
	CPalette* pPal = AfxGetPaletteApp()->GetAppPalette();
	if (!pPal)
		return;

	int i;
	for (i = 0; i < 256; i++)
	{
		if (m_abySelection[i])
		{
			PALETTEENTRY entry;
			BYTE k;
			pPal->GetPaletteEntries(i, 1, &entry);
			k = pPal->GetNearestPaletteIndex(
				RGB(entry.peBlue, entry.peRed, entry.peGreen));
			m_abyTransform[i] = k;
		}
	}

	SetModifiedFlag();
	ApplyGel();
}
//////////////////
// 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;
}
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 #24
0
CBitmap *CreateStringBitmap (
	const char *s, int nKey, uint nKeyColor, int *nTabs, int bCentered, int nMaxWidth, int bForce)
{
	int			origColor = CCanvas::Current ()->FontColor (0).index;//to allow easy reseting to default string color with colored strings -MPM
	int			i, x, y, hx, hy, w, h, aw, cw, spacing, nTab, nChars, bHotKey;
	CBitmap		*bmP, *bmfP;
	tRgbaColorb	hc, kc, *pc;
	ubyte			*pf;
	CPalette		*palP = NULL;
	tRgbColorb	*colorP;
	ubyte			c;
	const char	*textP, *text_ptr1, *nextRowP;
	int			letter;
	CFont*		fontP = fontManager.Current ();

#if 0
if (!(bForce || (gameOpts->menus.nStyle && gameOpts->menus.bFastMenus)))
	return NULL;
#endif
fontP->StringSizeTabbed (s, w, h, aw, nTabs, nMaxWidth);
if (!(w && h))
	return NULL;
if (bForce >= 0) {
	for (i = 1; i < w; i <<= 2)
		;
	w = i;
	for (i = 1; i < h; i <<= 2)
		;
	h = i;
	}
if (!(bmP = CBitmap::Create (0, w, h, 4))) 
	return NULL;
if (!bmP->Buffer ()) {
	delete bmP;
	return NULL;
	}
bmP->SetName ("String Bitmap");
bmP->Clear ();
bmP->AddFlags (BM_FLAG_TRANSPARENT);
nextRowP = s;
y = 0;
nTab = 0;
nChars = 0;
while (nextRowP) {
	text_ptr1 = nextRowP;
	nextRowP = NULL;
	textP = text_ptr1;
#if 0
#	if DBG
	if (bCentered)
		x = (w - fontManager.Current ()->GetLineWidth (textP)) / 2;
	else
		x = 0;
#	else
	x = bCentered ? (w - fontManager.Current ()->GetLineWidth (textP)) / 2 : 0;
#	endif
#else
x = 0;
#endif
	while ((c = *textP)) {
		if (c == '\n') {
			nextRowP = textP + 1;
			y += fontP->Height () + 2;
			nTab = 0;
			break;
			}
		if (c == '\t') {
			textP++;
			if (nTabs && (nTab < 6)) {
				int	w, h, aw;

				fontManager.Current ()->StringSize (textP, w, h, aw);
				x = LHX (nTabs [nTab++]);
				if (!gameStates.multi.bSurfingNet)
					x += nMaxWidth - w;
				for (i = 1; i < w; i <<= 2)
					;
				w = i;
				for (i = 1; i < h; i <<= 2)
					;
				h = i;
				}
			continue;
			}
		letter = c - fontP->MinChar ();
		fontP->GetCharWidth (c, textP [1], cw, spacing);
		if (c <= 0x06) {	//not in font, draw as space
			textP = ScanEmbeddedColors (c, textP, origColor, 128, 2);
			continue;
			}
		if (!fontManager.Current ()->InFont (letter)) {
			x += spacing;
			textP++;
			continue;
			}
		if ((bHotKey = ((nKey < 0) && isalnum (c)) || (nKey && ((int) c == nKey))))
			nKey = 0;
		bmfP = (bHotKey && (fontManager.Current () != SMALL_FONT)) ? SELECTED_FONT->Bitmaps () + letter : fontP->Bitmaps () + letter;
		palP = bmfP->Parent () ? bmfP->Parent ()->Palette () : bmfP->Palette ();
		nChars++;
		i = nKeyColor * 3;
		kc.red = RGBA_RED (nKeyColor);
		kc.green = RGBA_GREEN (nKeyColor);
		kc.blue = RGBA_BLUE (nKeyColor);
		kc.alpha = 255;
		if (fontP->Flags () & FT_COLOR) {
			for (hy = 0; hy < bmfP->Height (); hy++) {
				pc = reinterpret_cast<tRgbaColorb*> (bmP->Buffer ()) + (y + hy) * w + x;
				pf = bmfP->Buffer () + hy * bmfP->RowSize ();
				for (hx = bmfP->Width (); hx; hx--, pc++, pf++)
					if ((c = *pf) != TRANSPARENCY_COLOR) {
						colorP = palP->Color () + c;
						pc->red = colorP->red * 4;
						pc->green = colorP->green * 4;
						pc->blue = colorP->blue * 4;
						pc->alpha = 255;
						}
#if DBG
					else
						c = c;
#endif
				}
			}
		else {
			if (CCanvas::Current ()->FontColor (0).index < 0)
				memset (&hc, 0xff, sizeof (hc));
			else {
				if (CCanvas::Current ()->FontColor (0).rgb) {
					hc = CCanvas::Current ()->FontColor (0).color;
					}
				else {
					colorP = palP->Color () + CCanvas::Current ()->FontColor (0).index;
					hc.red = colorP->red * 4;
					hc.green = colorP->green * 4;
					hc.blue = colorP->blue * 4;
					}
				hc.alpha = 255;
				}
			for (hy = 0; hy < bmfP->Height (); hy++) {
				pc = reinterpret_cast<tRgbaColorb*> (bmP->Buffer ()) + (y + hy) * w + x;
				pf = bmfP->Buffer () + hy * bmfP->RowSize ();
				for (hx = bmfP->Width (); hx; hx--, pc++, pf++)
					if (*pf != TRANSPARENCY_COLOR)
						*pc = bHotKey ? kc : hc;
				}
			}
		x += spacing;
		textP++;
		}
	}
bmP->SetPalette (palP);
bmP->SetTranspType (-1);
return bmP;
}
Exemple #25
0
int GrInternalString0 (int x, int y, const char *s)
{
	ubyte*		fp;
	const char*	textP, *nextRowP, *text_ptr1;
	int			r, mask, i, bits, width, spacing, letter, underline;
	int			skip_lines = 0;
	uint			videoOffset, videoOffset1;
	CPalette*	palette = paletteManager.Game ();
	ubyte*		videoBuffer = CCanvas::Current ()->Buffer ();
	int			rowSize = CCanvas::Current ()->RowSize ();
	tFont			font;
	char			c;
	
if (CCanvas::Current ()->FontColor (0).rgb) {
	CCanvas::Current ()->FontColor (0).rgb = 0;
	CCanvas::Current ()->FontColor (0).index = palette->ClosestColor (CCanvas::Current ()->FontColor (0).color.red, 
																							CCanvas::Current ()->FontColor (0).color.green, 
																							CCanvas::Current ()->FontColor (0).color.blue);
	}
if (CCanvas::Current ()->FontColor (1).rgb) {
	CCanvas::Current ()->FontColor (1).rgb = 0;
	CCanvas::Current ()->FontColor (1).index = palette->ClosestColor (CCanvas::Current ()->FontColor (1).color.red, 
																							CCanvas::Current ()->FontColor (1).color.green, 
																							CCanvas::Current ()->FontColor (1).color.blue);
	}
bits=0;
videoOffset1 = y * rowSize + x;
nextRowP = s;
fontManager.Current ()->GetInfo (font);
while (nextRowP != NULL) {
	text_ptr1 = nextRowP;
	nextRowP = NULL;
	if (x == 0x8000) {			//centered
		int xx = fontManager.Current ()->GetCenteredX (text_ptr1);
		videoOffset1 = y * rowSize + xx;
		}
	for (r = 0; r < font.height; r++) {
		textP = text_ptr1;
		videoOffset = videoOffset1;
		while ((c = *textP)) {
			if (c == '\n') {
				nextRowP = textP + 1;
				break;
				}
			if (c == CC_COLOR) {
				CCanvas::Current ()->FontColor (0).index = *(++textP);
				CCanvas::Current ()->FontColor (0).rgb = 0;
				textP++;
				continue;
				}
			if (c == CC_LSPACING) {
				skip_lines = *(++textP) - '0';
				textP++;
				continue;
				}
			underline = 0;
			if (c == CC_UNDERLINE) {
				if ((r == font.baseLine + 2) || (r == font.baseLine + 3))
					underline = 1;
				textP++;
				}
			fontManager.Current ()->GetCharWidth (textP[0], textP[1], width, spacing);
			letter = c - font.minChar;
			if (!fontManager.Current ()->InFont (letter)) {	//not in font, draw as space
				videoOffset += spacing;
				textP++;
				continue;
				}
			if (font.flags & FT_PROPORTIONAL)
				fp = font.chars [letter];
			else
				fp = font.data + letter * BITS_TO_BYTES (width) * font.height;
			if (underline)
				for (i = 0; i < width; i++)
					videoBuffer [videoOffset++] = (ubyte) CCanvas::Current ()->FontColor (0).index;
				else {
					fp += BITS_TO_BYTES (width)*r;
					mask = 0;
					for (i = 0; i < width; i++) {
						if (mask == 0) {
							bits = *fp++;
							mask = 0x80;
							}
						if (bits & mask)
							videoBuffer [videoOffset++] = (ubyte) CCanvas::Current ()->FontColor (0).index;
						else
							videoBuffer [videoOffset++] = (ubyte) CCanvas::Current ()->FontColor (1).index;
						mask >>= 1;
						}
					}
			videoOffset += spacing-width;		//for kerning
			textP++;
			}
		videoOffset1 += rowSize; y++;
		}
	y += skip_lines;
	videoOffset1 += rowSize * skip_lines;
	skip_lines = 0;
	}
return 0;
}
Exemple #26
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;
}
Exemple #27
0
void CStartupView::LoadBitmaps()
{
   CDC *pDC = GetDC();

   CPalette *pPalette = NULL;
   pPalette = pDC->GetCurrentPalette();

   HBITMAP hBitmap;

   // Load LECTURNITY Logo
   CSecurityUtils securityUtils;
   CString csOwnVersion;
   csOwnVersion.LoadString(IDS_VERSION);
   int nVersionType = securityUtils.GetVersionType(csOwnVersion);

   UINT nID = IDB_LECTURNITYLOGO;
   if (nVersionType != FULL_VERSION)
      nID = IDB_SP_LOGO_TEST;
   hBitmap = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(nID/*IDB_SP_LOGO_TEST*//*IDB_LECTURNITYLOGO*/));
   m_pLecturnityLogo = Gdiplus::Bitmap::FromHBITMAP(hBitmap, (HPALETTE)pPalette->GetSafeHandle());
   ::DeleteObject(hBitmap);

   // Load IMC Logo
   hBitmap = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_IMCLOGO));
   m_pImcLogo = Gdiplus::Bitmap::FromHBITMAP(hBitmap, (HPALETTE)pPalette->GetSafeHandle());
   ::DeleteObject(hBitmap);

   hBitmap = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_SP_TOP_LEFT));
   m_pBmpTopLeft = Gdiplus::Bitmap::FromHBITMAP(hBitmap, (HPALETTE)pPalette->GetSafeHandle());
   ::DeleteObject(hBitmap);
   hBitmap = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_SP_TOP_MIDDLE));
   m_pBmpTopMiddle = Gdiplus::Bitmap::FromHBITMAP(hBitmap, (HPALETTE)pPalette->GetSafeHandle());
   ::DeleteObject(hBitmap);
   hBitmap = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_SP_TOP_RIGHT));
   m_pBmpTopRight = Gdiplus::Bitmap::FromHBITMAP(hBitmap, (HPALETTE)pPalette->GetSafeHandle());
   ::DeleteObject(hBitmap);

   hBitmap = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_SP_BLUE_LINE_LEFT));
   m_pBmpBlueLeft = Gdiplus::Bitmap::FromHBITMAP(hBitmap, (HPALETTE)pPalette->GetSafeHandle());
   ::DeleteObject(hBitmap);
   hBitmap = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_SP_BLUE_LINE_MIDDLE));
   m_pBmpBlueMiddle = Gdiplus::Bitmap::FromHBITMAP(hBitmap, (HPALETTE)pPalette->GetSafeHandle());
   ::DeleteObject(hBitmap);
   hBitmap = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_SP_BLUE_LINE_RIGHT));
   m_pBmpBlueRight = Gdiplus::Bitmap::FromHBITMAP(hBitmap, (HPALETTE)pPalette->GetSafeHandle());
   ::DeleteObject(hBitmap);

   hBitmap = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_SP_WHITE_LEFT));
   m_pBmpWhiteLeft = Gdiplus::Bitmap::FromHBITMAP(hBitmap, (HPALETTE)pPalette->GetSafeHandle());
   ::DeleteObject(hBitmap);
   hBitmap = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_SP_WHITE_MIDDLE));
   m_pBmpWhiteMiddle = Gdiplus::Bitmap::FromHBITMAP(hBitmap, (HPALETTE)pPalette->GetSafeHandle());
   ::DeleteObject(hBitmap);
   hBitmap = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_SP_WHITE_RIGHT));
   m_pBmpWhiteRight = Gdiplus::Bitmap::FromHBITMAP(hBitmap, (HPALETTE)pPalette->GetSafeHandle());
   ::DeleteObject(hBitmap);

   hBitmap = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_SP_LIGHT_BLUE_LEFT));
   m_pBmpLightBlueLeft = Gdiplus::Bitmap::FromHBITMAP(hBitmap, (HPALETTE)pPalette->GetSafeHandle());
   ::DeleteObject(hBitmap);
   hBitmap = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_SP_LIGHT_BLUE_MIDDLE));
   m_pBmpLightBlueMiddle = Gdiplus::Bitmap::FromHBITMAP(hBitmap, (HPALETTE)pPalette->GetSafeHandle());
   ::DeleteObject(hBitmap);
   hBitmap = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_SP_LIGHT_BLUE_RIGHT));
   m_pBmpLightBlueRight = Gdiplus::Bitmap::FromHBITMAP(hBitmap, (HPALETTE)pPalette->GetSafeHandle());
   ::DeleteObject(hBitmap);

   hBitmap = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_SP_FOOT_LEFT));
   m_pBmpFootLeft = Gdiplus::Bitmap::FromHBITMAP(hBitmap, (HPALETTE)pPalette->GetSafeHandle());
   ::DeleteObject(hBitmap);
   hBitmap = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_SP_FOOT_MIDDLE));
   m_pBmpFootMiddle = Gdiplus::Bitmap::FromHBITMAP(hBitmap, (HPALETTE)pPalette->GetSafeHandle());
   ::DeleteObject(hBitmap);
   hBitmap = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_SP_FOOT_RIGHT));
   m_pBmpFootRight = Gdiplus::Bitmap::FromHBITMAP(hBitmap, (HPALETTE)pPalette->GetSafeHandle());
   ::DeleteObject(hBitmap);

   ReleaseDC(pDC);
}
HANDLE CImageWindow::DDBToDIB(CDC* pDC, CBitmap &Bitmap, CPalette *pPal)
{
	if( !pDC ) return NULL;

	BITMAP   Bmp;
	BITMAPINFO  BmpInfo;
	CPalette  Palette;
	CPalette*  pOldPalette = NULL;
	//기본 팔레트 생성
	Palette.CreateStockObject(DEFAULT_PALETTE);
	//비트맵 정보를 얻는다.
	Bitmap.GetBitmap(&Bmp);
	//////////////////////////////////////////////////////////////////////////
	// 1. 비트맵 해더를 초기화 한다.
	BmpInfo.bmiHeader.biSize   = sizeof(BITMAPINFOHEADER);
	BmpInfo.bmiHeader.biWidth   = Bmp.bmWidth;
	BmpInfo.bmiHeader.biHeight   = Bmp.bmHeight;
	BmpInfo.bmiHeader.biPlanes   = 1;
	BmpInfo.bmiHeader.biBitCount  = Bmp.bmPlanes * Bmp.bmBitsPixel;
	BmpInfo.bmiHeader.biCompression  = BI_RGB;
	BmpInfo.bmiHeader.biSizeImage  = 0;
	BmpInfo.bmiHeader.biXPelsPerMeter = 0;
	BmpInfo.bmiHeader.biYPelsPerMeter = 0;
	BmpInfo.bmiHeader.biClrUsed   = 0;
	BmpInfo.bmiHeader.biClrImportant = 0;
	//비트맵 헤더와 컬러 테이블의 크기를 계산한다.
	int nColors = (1 << BmpInfo.bmiHeader.biBitCount);
	if (nColors > 256){
		nColors = 0;
	}
	DWORD dwImageLength = BmpInfo.bmiHeader.biSize + nColors * sizeof(RGBQUAD);
	//저장할 윈도우의 팔레트를 기본 팔레트로 설정한다.
	pOldPalette = pDC->SelectPalette(&Palette, FALSE);
	pDC->RealizePalette();
	//DIB가 저장될 메모리(비트맵 정보 헤더와 컬러 테이블)을 할당한다.
	HANDLE hDIB = ::GlobalAlloc(GPTR, dwImageLength);
	if (hDIB == NULL)
	{
		pDC->SelectPalette(pOldPalette, FALSE);
		return NULL;
	}

	//////////////////////////////////////////////////////////////////////////
	//2. 비트맵 이미지의 크기를 계산한다.
	::GetDIBits(pDC->GetSafeHdc(),
		(HBITMAP)Bitmap.GetSafeHandle(),
		0L,
		BmpInfo.bmiHeader.biHeight,
		NULL,  //이 값이 NULL이면 이미지 크기를 계산.
		(LPBITMAPINFO)&BmpInfo.bmiHeader,
		DIB_RGB_COLORS);
	if (BmpInfo.bmiHeader.biSizeImage == 0)
	{
		pDC->SelectPalette(pOldPalette, FALSE);
		GlobalFree(hDIB);
		return NULL;
	}
	//////////////////////////////////////////////////////////////////////////
	//3. DIB 이미지를 구함.
	dwImageLength += BmpInfo.bmiHeader.biSizeImage;
	HANDLE hRealloc = ::GlobalReAlloc(hDIB, dwImageLength, GMEM_MOVEABLE);
	if (hRealloc != NULL)
	{
		hDIB = hRealloc;
	}
	else
	{
		pDC->SelectPalette(pOldPalette, FALSE);
		GlobalFree(hDIB);
		return NULL;
	}
	//DDB에서 DIB로 변환된 이미지를 구함.
	if (!::GetDIBits(pDC->GetSafeHdc(),
		(HBITMAP)Bitmap.GetSafeHandle(),
		0L,    // Start scan line
		(DWORD)BmpInfo.bmiHeader.biHeight,  //# of scan lines
		(LPBYTE)hDIB + (BmpInfo.bmiHeader.biSize + nColors * sizeof(RGBQUAD)),
		(LPBITMAPINFO)&BmpInfo.bmiHeader,  //address of bitmapinfo
		(DWORD)DIB_RGB_COLORS))
	{
		pDC->SelectPalette(pOldPalette, FALSE);
		GlobalFree(hDIB);
		return NULL;
	}
	//DIB 헤더 작성
	memcpy(hDIB, &BmpInfo.bmiHeader, sizeof(BITMAPINFOHEADER));
	pDC->SelectPalette(pOldPalette, FALSE);
	Palette.DeleteObject();

	return hDIB;
}