www.pudn.com > eMule0.42e-Sources.zip > ximapal.cpp


// xImaPal.cpp : Palette and Pixel functions 
/* 07/08/2001 v1.00 - ing.davide.pizzolato@libero.it 
 * CxImage version 5.71 25/Apr/2003 
 */ 
 
#include "ximage.h" 
 
//////////////////////////////////////////////////////////////////////////////// 
// returns the palette dimension in byte 
DWORD CxImage::GetPaletteSize() 
{ 
	return (head.biClrUsed * sizeof(RGBQUAD)); 
} 
//////////////////////////////////////////////////////////////////////////////// 
void CxImage::SetPaletteColor(BYTE idx, BYTE r, BYTE g, BYTE b, BYTE alpha) 
{ 
	if ((pDib)&&(head.biClrUsed)){ 
		BYTE* iDst = (BYTE*)(pDib) + sizeof(BITMAPINFOHEADER); 
		if (idx=head.biWidth)||(y>=head.biHeight)) { 
		if (info.nBkgndIndex != -1)	return (BYTE)info.nBkgndIndex; 
		else return 0; 
	} 
	if (head.biBitCount==8){ 
		return info.pImage[y*info.dwEffWidth + x]; 
	} else { 
		BYTE pos; 
		BYTE iDst= info.pImage[y*info.dwEffWidth + (x*head.biBitCount >> 3)]; 
		if (head.biBitCount==4){ 
			pos = (BYTE)(4*(1-x%2)); 
			iDst &= (0x0F<> pos); 
		} else if (head.biBitCount==1){ 
			pos = (BYTE)(7-x%8); 
			iDst &= (0x01<> pos); 
		} 
	} 
	return 0; 
} 
//////////////////////////////////////////////////////////////////////////////// 
RGBQUAD CxImage::GetPixelColor(long x,long y) 
{ 
//	RGBQUAD rgb={0,0,0,0}; 
	RGBQUAD rgb=info.nBkgndColor; // 
	if ((pDib==NULL)||(x<0)||(y<0)|| 
		(x>=head.biWidth)||(y>=head.biHeight)){ 
		if (info.nBkgndIndex != -1){ 
			if (head.biBitCount<24) rgb = GetPaletteColor((BYTE)info.nBkgndIndex); 
			else rgb = info.nBkgndColor; 
		} else if (pDib) rgb = GetPixelColor(0,0); 
		return rgb; 
	} 
 
	if (head.biClrUsed){ 
		rgb = GetPaletteColor(GetPixelIndex(x,y)); 
	} else { 
		BYTE* iDst  = info.pImage + y*info.dwEffWidth + x*3; 
		rgb.rgbBlue = *iDst++; 
		rgb.rgbGreen= *iDst++; 
		rgb.rgbRed  = *iDst; 
	} 
#if CXIMAGE_SUPPORT_ALPHA 
	if (pAlpha) rgb.rgbReserved = AlphaGet(x,y); 
#else 
	rgb.rgbReserved = 0; 
#endif //CXIMAGE_SUPPORT_ALPHA 
	return rgb; 
} 
//////////////////////////////////////////////////////////////////////////////// 
BYTE CxImage::GetPixelGray(long x, long y) 
{ 
	RGBQUAD color = GetPixelColor(x,y); 
	return (BYTE)RGB2GRAY(color.rgbRed,color.rgbGreen,color.rgbBlue); 
} 
//////////////////////////////////////////////////////////////////////////////// 
void CxImage::SetPixelIndex(long x,long y,BYTE i) 
{ 
	if ((pDib==NULL)||(head.biClrUsed==0)|| 
		(x<0)||(y<0)||(x>=head.biWidth)||(y>=head.biHeight)) return ; 
 
	if (head.biBitCount==8){ 
		info.pImage[y*info.dwEffWidth + x]=i; 
		return; 
	} else { 
		BYTE pos; 
		BYTE* iDst= info.pImage + y*info.dwEffWidth + (x*head.biBitCount >> 3); 
		if (head.biBitCount==4){ 
			pos = (BYTE)(4*(1-x%2)); 
			*iDst &= ~(0x0F<=head.biWidth)||(y>=head.biHeight)) return; 
	if (head.biClrUsed) 
		SetPixelIndex(x,y,GetNearestIndex(c)); 
	else { 
		BYTE* iDst = info.pImage + y*info.dwEffWidth + x*3; 
		*iDst++ = c.rgbBlue; 
		*iDst++ = c.rgbGreen; 
		*iDst   = c.rgbRed; 
#if CXIMAGE_SUPPORT_ALPHA 
		if (bEditAlpha) AlphaSet(x,y,c.rgbReserved); 
#endif //CXIMAGE_SUPPORT_ALPHA 
	} 
} 
//////////////////////////////////////////////////////////////////////////////// 
BYTE CxImage::GetNearestIndex(RGBQUAD c) 
{ 
	if ((pDib==NULL)||(head.biClrUsed==0)) return 0; 
 
	//  check matching with the previous result 
	if (info.last_c_isvalid && (*(long*)&info.last_c == *(long*)&c)) return info.last_c_index; 
	info.last_c = c; 
	info.last_c_isvalid = true; 
 
	BYTE* iDst = (BYTE*)(pDib) + sizeof(BITMAPINFOHEADER); 
	long distance=200000; 
	int i,j=0; 
	long k,l; 
	int m= (int)head.biClrUsed; 
	for(i=0,l=0;i buffer: pointer to the pixels 
// > lenght: number of bytes to swap. lenght may not exceed the scan line. 
void CxImage::RGBtoBGR(BYTE *buffer, int length) 
{ 
	if (buffer && (head.biClrUsed==0)){ 
		BYTE temp; 
		length = min(length,(int)info.dwEffWidth); 
		for (int i=0;i100) perc=100; 
	for(i=0;i