#include "dkdata.h"

//int verbose = 1;

bitmap_t bit;

static void Lump_LoadBMP(void *clist, char *file_nm, 
						 int sx, int sy, int w, int h, int fileWeight, int type)
{
//	bitmap_t bit;
	int pixCount = 0;

	// load up bmp file
//	LoadBMP(Sys_Base(file_nm), &bit);
	LoadBMP(file_nm, &bit);

	// if this is a 24 bit then load values directly
	if (bit.bpp == 24)
	{
		int W = bit.width;
		int x, y, base;
		float r, g, b;

		for (y = 0; y < h; y++)
		{
			for (x = 0; x < w; x++)
			{
				base = (sy * W * 3) + (y * W * 3) + (sx * 3) + (x * 3);

		        r = (float) bit.data[base + 0];
				g = (float) bit.data[base + 1];
				b = (float) bit.data[base + 2];

				// add tuple
				if (type == CTYPE_CLIST)
				{
				     Color_AddTuple((colorList_t *) clist, r, g, b, fileWeight);
				}
				else if (type == CTYPE_OTREE)
				{
					 Octree_AddColor((octree_t *) clist, r, g, b, fileWeight);
				}
				else
				{
					Error("Lump_LoadBMP: Unknown CTYPE");
				}

				pixCount++;
			}
		}
	}

	// if this is an 8 bit image then add values from palette
	else if (bit.bpp == 8)
	{
		int W = bit.width;
		int x, y, base, k;
		float r, g, b;

        for (y = 0; y < h; y++)
		{
			for (x = 0; x < w; x++)
			{
				base = (sy * W) + (y * W) + sx + x;
				k = (int) bit.data[base];

				r = (float) bit.palette[k].r;
				g = (float) bit.palette[k].g;
				b = (float) bit.palette[k].b;

				// add tuple
				if (type == CTYPE_CLIST)
				{
				     Color_AddTuple((colorList_t *) clist, r, g, b, fileWeight);
				}
				else if (type == CTYPE_OTREE)
				{
					 Octree_AddColor((octree_t *) clist, r, g, b, fileWeight);
				}
				else
				{
					Error("Lump_LoadBMP: Unknown CTYPE");
				}

				pixCount++;
			}
		}
	}

	// that's all we support
	else
	{
		Error("Lump_LoadBMP: %s is neither 24 or 8 bit.");
	}

/*
	if (verbose)
	{
	    printf("Loaded %d pixels from %s. (%d bit, weight %d)\n", 
			pixCount, Sys_Base(file_nm), bit.bpp, fileWeight);
	}
*/
	FreeBMP(&bit);
}



static void Lump_LoadLBM(void *clist, char *file_nm, 
						 int sx, int sy, int w, int h, int fileWeight, int type)
{
	rgb_t *pal;
	u_char *pic;
    int x, y, base, k, W;
    float r, g, b;
	int pixCount = 0;

//    LoadLBM(Sys_Base(file_nm), &pic, (u_char **) &pal);
    LoadLBM(file_nm, &pic, (u_char **) &pal);

	W = bit.width;
		
    for (y = 0; y < h; y++)
	{
		for (x = 0; x < w; x++)
		{
			base = (sy * W) + (y * W) + sx + x;
			k = (int) pic[base];

			r = (float) pal[k].r;
			g = (float) pal[k].g;
			b = (float) pal[k].b;

			// add tuple
			if (type == CTYPE_CLIST)
			{
			    Color_AddTuple((colorList_t *) clist, r, g, b, fileWeight);
			}
			else if (type == CTYPE_OTREE)
			{
				Octree_AddColor((octree_t *) clist, r, g, b, fileWeight);
			}
			else
			{
				Error("Lump_LoadLBM: Unknown CTYPE");
			}


			pixCount++;
		}
	}

/*
	if (verbose)
	{
	    printf("Loaded %d pixels from %s. (8 bit, weight %d)\n", 
			pixCount, Sys_Base(file_nm), fileWeight);
	}
*/
	free(pal);
	free(pic);
}


	
void LoadMiptex(void *clist, char *file_nm, 
				int sx, int sy, int w, int h, int fileWeight, int type)
{
	// determine file type
	switch (Lump_FileType(file_nm))
	{
	    case FILE_BMP :
			Lump_LoadBMP(clist, file_nm, sx, sy, w, h, fileWeight, type);
			break;

		case FILE_LBM :
			Lump_LoadLBM(clist, file_nm, sx, sy, w, h, fileWeight, type);
			break;

		default :
			Error("LoadMiptex: Unknown file type: %s.", file_nm);
			break;

	}
}

