#include "standard.h"
#include "global.h"


void SWR_DrawPolyList(void)
	{
/*
	DDSURFACEDESC desc;
	if (!DX_FAILED(DD_LockSurface(G_RenderBuffer,desc)))
		{
		phd_PrintPolyList(desc.lpSurface);

//		for (int nY=0;nY<16;++nY)
//			for (int nX=0;nX<16;++nX)
//				for (int nV=0;nV<8;++nV)
//					for (int nU=0;nU<8;++nU)
//						((uint8*)desc.lpSurface)[(nX*8+nU)+((nY*8+nV)*desc.lPitch)]=nX+nY*16;

		
		DD_UnlockSurface(G_RenderBuffer,desc);
		}
*/
 }

// 0.299r 0.587g 0.114b

#if 0
#define RED_WEIGHT 0.509
#define GREEN_WEIGHT 1.0
#define BLUE_WEIGHT 0.194
#else
#define RED_WEIGHT 1.0
#define GREEN_WEIGHT 1.0
#define BLUE_WEIGHT 1.0
#endif

void SWR_Make236Palette(uint8* pSrc,uint8* pImage,int nPixels,uint8* pDest)
	{
	int nEntry;
	PALETTEENTRY apeSystemPalette[20];
	uint8* pOldPal,*pPal;

/*


	// make histogram
	for (nEntry=0;nEntry<256;++nEntry)
		{
		sort3d_buffer[nEntry][0]=nEntry;
		sort3d_buffer[nEntry][1]=0;
		}
	while (nPixels--)
		++sort3d_buffer[(int)(*pImage++)][1];
	do_quickysorty(0,255,sort3d_buffer);

	// get system palette entries
	HDC hdc=GetDC(NULL);
	GetSystemPaletteEntries(hdc,0,10,apeSystemPalette);
	GetSystemPaletteEntries(hdc,246,10,apeSystemPalette+10);
	ReleaseDC(NULL,hdc);

	// make new palette with:
	// 0..7:	system colours
	// 8,9:		black (won't be matched due to system 0)
	// 10..245:	most frequent 236 colours from histogram
	// 246:		black (won't be matched due to system 0)
	// 247..255:system colours
	pPal=pDest;
	for (nEntry=0;nEntry<8;++nEntry,pPal+=3)
		{
		pPal[0]=apeSystemPalette[nEntry].peRed;
		pPal[1]=apeSystemPalette[nEntry].peGreen;
		pPal[2]=apeSystemPalette[nEntry].peBlue;
		}
	pDest[8*3]=pDest[9*3]=pDest[246*3]=
	pDest[8*3+1]=pDest[9*3+1]=pDest[246*3+1]=
	pDest[8*3+2]=pDest[9*3+2]=pDest[246*3+2]=0;
	pPal=pDest+247*3;
	for (nEntry=0;nEntry<9;++nEntry,pPal+=3)
		{
		pPal[0]=apeSystemPalette[nEntry+11].peRed;
		pPal[1]=apeSystemPalette[nEntry+11].peGreen;
		pPal[2]=apeSystemPalette[nEntry+11].peBlue;
		}
//	memset(pDest,0,768);		// ???????
	pPal=pDest+10*3;
	for (nEntry=0;nEntry<236;++nEntry)
		{
		pOldPal=pSrc+sort3d_buffer[nEntry][0]*3;
		pPal[0]=pOldPal[0];
		pPal[1]=pOldPal[1];
		pPal[2]=pOldPal[2];
		pPal+=3;
		}

*/  
	
}

uint8 SWR_FindNearestPaletteEntry(uint8* pPal,int nR,int nG,int nB,bool tDontUseWindowsColours)
	{
	int i,nEnd,nColour;
	double dR,dG,dB;
	double dDist,dClosest;

	dClosest=10000000000.0;
	if (tDontUseWindowsColours)
		{
		i=10;
		nEnd=246;
		pPal+=10*3;
		}
	else
		{
		i=0;
		nEnd=256;
		}
	for (;i<nEnd;++i)
		{
		dR=double(nR-*(pPal++));
		dG=double(nG-*(pPal++));
		dB=double(nB-*(pPal++));
		dDist=dR*dR*RED_WEIGHT+dG*dG*GREEN_WEIGHT+dB*dB*BLUE_WEIGHT;
		if (dDist<dClosest)
			{
			nColour=i;
			dClosest=dDist;
			}
		}
	return (uint8)nColour;
	}

void SWR_ConvertImage(uint8* pSrc,int nSX,int nSY,int nSP,uint8* pOldPalette,uint8* pDest,int nDP,uint8* pNewPalette,bool tDontUseStaticColours)
	{
	Log(LT_Enter,"SWR_ConvertImage");
	uint8 abRemapTable[256];
	int nXC;

	uint8* pPal=pOldPalette;
	for (int nEntry=0;nEntry<256;++nEntry,pPal+=3)
		abRemapTable[nEntry]=SWR_FindNearestPaletteEntry(pNewPalette,pPal[0],pPal[1],pPal[2],tDontUseStaticColours);

	while (nSY--)
		{
		for (nXC=nSX;nXC--;)
			*pDest++=abRemapTable[*pSrc++];
		pSrc+=(nSP-nSX);
		pDest+=(nDP-nSX);
		}
	}
