#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#define DIMSIZEBITS 3
#define DIMSIZENUM (1<<DIMSIZEBITS)
#define NUMBOXES (DIMSIZENUM*DIMSIZENUM*DIMSIZENUM)
#define COLOURSTEP (1.0/DIMSIZENUM)
#define STEP 1

static const unsigned char pal[] =
{
	0x00, 0x00, 0x00, 0x0f, 0x0f, 0x0f, 0x1f, 0x1f, 0x1f, 0x2f, 0x2f, 0x2f, 0x3f, 0x3f, 0x3f, 0x4b,
	0x4b, 0x4b, 0x5b, 0x5b, 0x5b, 0x6b, 0x6b, 0x6b, 0x7b, 0x7b, 0x7b, 0x8b, 0x8b, 0x8b, 0x9b, 0x9b,
	0x9b, 0xab, 0xab, 0xab, 0xbb, 0xbb, 0xbb, 0xcb, 0xcb, 0xcb, 0xdb, 0xdb, 0xdb, 0xeb, 0xeb, 0xeb,
	0x0f, 0x0b, 0x07, 0x17, 0x0f, 0x0b, 0x1f, 0x17, 0x0b, 0x27, 0x1b, 0x0f, 0x2f, 0x23, 0x13, 0x37,
	0x2b, 0x17, 0x3f, 0x2f, 0x17, 0x4b, 0x37, 0x1b, 0x53, 0x3b, 0x1b, 0x5b, 0x43, 0x1f, 0x63, 0x4b,
	0x1f, 0x6b, 0x53, 0x1f, 0x73, 0x57, 0x1f, 0x7b, 0x5f, 0x23, 0x83, 0x67, 0x23, 0x8f, 0x6f, 0x23,
	0x0b, 0x0b, 0x0f, 0x13, 0x13, 0x1b, 0x1b, 0x1b, 0x27, 0x27, 0x27, 0x33, 0x2f, 0x2f, 0x3f, 0x37,
	0x37, 0x4b, 0x3f, 0x3f, 0x57, 0x47, 0x47, 0x67, 0x4f, 0x4f, 0x73, 0x5b, 0x5b, 0x7f, 0x63, 0x63,
	0x8b, 0x6b, 0x6b, 0x97, 0x73, 0x73, 0xa3, 0x7b, 0x7b, 0xaf, 0x83, 0x83, 0xbb, 0x8b, 0x8b, 0xcb,
	0x00, 0x00, 0x00, 0x07, 0x07, 0x00, 0x0b, 0x0b, 0x00, 0x13, 0x13, 0x00, 0x1b, 0x1b, 0x00, 0x23,
	0x23, 0x00, 0x2b, 0x2b, 0x07, 0x2f, 0x2f, 0x07, 0x37, 0x37, 0x07, 0x3f, 0x3f, 0x07, 0x47, 0x47,
	0x07, 0x4b, 0x4b, 0x0b, 0x53, 0x53, 0x0b, 0x5b, 0x5b, 0x0b, 0x63, 0x63, 0x0b, 0x6b, 0x6b, 0x0f,
	0x07, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x17, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x27, 0x00, 0x00, 0x2f,
	0x00, 0x00, 0x37, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x47, 0x00, 0x00, 0x4f, 0x00, 0x00, 0x57, 0x00,
	0x00, 0x5f, 0x00, 0x00, 0x67, 0x00, 0x00, 0x6f, 0x00, 0x00, 0x77, 0x00, 0x00, 0x7f, 0x00, 0x00,
	0x13, 0x13, 0x00, 0x1b, 0x1b, 0x00, 0x23, 0x23, 0x00, 0x2f, 0x2b, 0x00, 0x37, 0x2f, 0x00, 0x43,
	0x37, 0x00, 0x4b, 0x3b, 0x07, 0x57, 0x43, 0x07, 0x5f, 0x47, 0x07, 0x6b, 0x4b, 0x0b, 0x77, 0x53,
	0x0f, 0x83, 0x57, 0x13, 0x8b, 0x5b, 0x13, 0x97, 0x5f, 0x1b, 0xa3, 0x63, 0x1f, 0xaf, 0x67, 0x23,
	0x23, 0x13, 0x07, 0x2f, 0x17, 0x0b, 0x3b, 0x1f, 0x0f, 0x4b, 0x23, 0x13, 0x57, 0x2b, 0x17, 0x63,
	0x2f, 0x1f, 0x73, 0x37, 0x23, 0x7f, 0x3b, 0x2b, 0x8f, 0x43, 0x33, 0x9f, 0x4f, 0x33, 0xaf, 0x63,
	0x2f, 0xbf, 0x77, 0x2f, 0xcf, 0x8f, 0x2b, 0xdf, 0xab, 0x27, 0xef, 0xcb, 0x1f, 0xff, 0xf3, 0x1b,
	0x0b,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 0x1b, 0xcf, 0x63,
	0x2b, 0xdb, 0x7f, 0x3b, 0xe3, 0x97, 0x4f, 0xe7, 0xab, 0x5f, 0xef, 0xbf, 0x77, 0xf7, 0xd3, 0x8b,
	0xa7, 0x7b, 0x3b, 0xb7, 0x9b, 0x37, 0xc7, 0xc3, 0x37, 0xe7, 0xe3, 0x57, 0x7f, 0xbf, 0xff, 0xab,
	0xe7, 0xff, 0xd7, 0xff, 0xff, 0x67, 0x00, 0x00, 0x8b, 0x00, 0x00, 0xb3, 0x00, 0x00, 0xd7, 0x00,
	0x00, 0xff, 0x00, 0x00, 0xff, 0xf3, 0x93, 0xff, 0xf7, 0xc7, 0xff, 0xff, 0xff, 0x9f, 0x5b, 0x53,
};

#if 1
static unsigned char V_LookUpColourInternal(float r, float g, float b, unsigned int maxindex)
{
	double bestdistance;
	double distance;
	unsigned int bestfit;
	unsigned int i;

	bestdistance = 256*256*256;
	bestfit = 0;

	for(i=0;i<maxindex;i++)
	{
		distance = sqrt(pow(r-pal[i*3+0], 2) + pow(g-pal[i*3+1], 2) + pow(b-pal[i*3+2], 2));
		if (distance < bestdistance)
		{
			bestfit = i;
			bestdistance = distance;
		}
	}

	return bestfit;
}

int main(int argc, char **argv)
{
	unsigned int *numcolourmem;
	unsigned char (*boxindexmem)[256];
	unsigned int *numcolours;
	unsigned int *numcoloursnfb;
	unsigned char (*boxindices)[256];
	unsigned char (*boxindicesnfb)[256];
	unsigned int i;
	unsigned int r;
	unsigned int g;
	unsigned int b;
	unsigned int box;
	unsigned int colour;
	unsigned int memsize;
	FILE *f;
	unsigned char dimbits;
	int ok;

	if (argc != 2)
	{
		printf("Usage: %s <filename>\n", argv[0]);
		exit(1);
	}

	ok = 0;
	numcolourmem = malloc(NUMBOXES * sizeof(*numcolourmem) * 2);
	boxindexmem = malloc(NUMBOXES * sizeof(*boxindexmem) * 256 * 2);

	if (numcolourmem && boxindexmem)
	{
		memset(numcolourmem, 0, NUMBOXES * sizeof(*numcolourmem) * 2);

		numcolours = numcolourmem;
		numcoloursnfb = numcolours + NUMBOXES;

		boxindices = boxindexmem;
		boxindicesnfb = boxindices + NUMBOXES * 256;

		for(b=0;b<256;b+=STEP)
		{
			for(g=0;g<256;g+=STEP)
			{
				for(r=0;r<256;r+=STEP)
				{
					box = (r>>(8-DIMSIZEBITS))<<(DIMSIZEBITS*2);
					box |= (g>>(8-DIMSIZEBITS))<<(DIMSIZEBITS*1);
					box |= (b>>(8-DIMSIZEBITS))<<(DIMSIZEBITS*0);

					colour = V_LookUpColourInternal(r, g, b, 256);

					for(i=0;i<numcolours[box] && boxindices[box][i] != colour;i++);
				
					if (i == numcolours[box])
					{
						boxindices[box][numcolours[box]] = colour;
						numcolours[box]++;
					}
				}
			}
		}

		for(b=0;b<256;b+=STEP)
		{
			for(g=0;g<256;g+=STEP)
			{
				for(r=0;r<256;r+=STEP)
				{
					box = (r>>(8-DIMSIZEBITS))<<(DIMSIZEBITS*2);
					box |= (g>>(8-DIMSIZEBITS))<<(DIMSIZEBITS*1);
					box |= (b>>(8-DIMSIZEBITS))<<(DIMSIZEBITS*0);

					colour = V_LookUpColourInternal(r, g, b, 224);

					for(i=0;i<numcoloursnfb[box] && boxindicesnfb[box][i] != colour;i++);
				
					if (i == numcoloursnfb[box])
					{
						boxindicesnfb[box][numcoloursnfb[box]] = colour;
						numcoloursnfb[box]++;
					}

				}
			}
		}

		f = fopen(argv[1], "wb");
		if (f == 0)
		{
			fprintf(stderr, "Unable to open \"%s\" for writing\n", argv[1]);
		}
		else
		{
			ok = 1;

			ok &= fwrite(pal, sizeof(pal), 1, f);

			dimbits = DIMSIZEBITS;

			ok &= fwrite(&dimbits, 1, 1, f);

			for(i=0;i<DIMSIZENUM*DIMSIZENUM*DIMSIZENUM;i++)
			{
				unsigned char c;

				c = numcolours[i];
				ok &= fwrite(&c, 1, 1, f);
			}

			for(i=0;i<DIMSIZENUM*DIMSIZENUM*DIMSIZENUM;i++)
			{
				unsigned char c;

				c = numcoloursnfb[i];
				ok &= fwrite(&c, 1, 1, f);
			}

			memsize = 0;
			for(i=0;i<NUMBOXES;i++)
			{
				ok &= fwrite(boxindices[i], numcolours[i], 1, f);
				memsize += numcolours[i];
			}

			for(i=0;i<NUMBOXES;i++)
			{
				ok &= fwrite(boxindicesnfb[i], numcoloursnfb[i], 1, f);
				memsize += numcoloursnfb[i];
			}

			fclose(f);

			if (!ok)
			{
				fprintf(stderr, "Writing to \"%s\" failed\n", argv[1]);
				remove(argv[1]);
			}
			else
				printf("RGBMap memory size is %d + %d + %d = %d bytes\n", NUMBOXES * 2, NUMBOXES * 2 * 4, memsize, NUMBOXES*2 + NUMBOXES*2*4 + memsize);
		}
	}

	free(numcolourmem);
	free(boxindexmem);

	exit(!ok);
}
#endif

