#include<ImageLib\Image.h>
#include"PrepData.h"

extern "C" {

int  BidDataSize(int xsize,int ysize,PSIMGFORMAT frmt)
{
	int pixs;
	if(frmt!=PS_GRAYSCALE) 
	{
		pixs=(((xsize+2)*(ysize+2)+(xsize/2+2)*(ysize/2+2)+(xsize/4+2)*(ysize/4+2)))*2;
	}
	else
	{
		pixs=(xsize+2)*(ysize+2);
	}

	return pixs;
}

unsigned char * BidPrepData(unsigned char *OrgDat,int xsize,int ysize,PSIMGFORMAT frmt)
{
	Image img,mip1,mip2;
	int pixs=xsize*ysize;
	unsigned char *ImgDat,*FinalDat;
	int dsize;
	dsize=BidDataSize(xsize,ysize,frmt);
	FinalDat=new unsigned char[dsize];
	switch(frmt)
	{
		
	case PS_RGB:
		{
			img.CreateBlank(xsize,ysize,ITYPE_RGB,24,RGBMask(0x0000ff,0x00ff00,0xff0000));
			ImgDat=img.Lock();
			CopyMemory(ImgDat,OrgDat,pixs*3);
			img.UnLock();
			RECT src,dst;
			
			src.top=0; src.left=0;
			src.bottom=img.GetHeight();
			src.right=img.GetWidth();
			
			mip1.CreateBlank(img.GetWidth()/2,img.GetHeight()/2,ITYPE_RGB,24);

			dst.top=0; dst.left=0;
			dst.bottom=mip1.GetHeight();
			dst.right=mip1.GetWidth();
			
			mip1.Blt(img,dst,src);
			
			mip2.CreateBlank(img.GetWidth()/4,img.GetHeight()/4,ITYPE_RGB,24);
			dst.top=0; dst.left=0;
			dst.bottom=mip2.GetHeight();
			dst.right=mip2.GetWidth();
			mip2.Blt(img,dst,src);
			
			img.Pad(1);
			mip1.Pad(1);
			mip2.Pad(1);
			img.MakeRGB16(RGBMask(0x007C00,0x0003E0,0x00001f,0x00));
			mip1.MakeRGB16(RGBMask(0x007C00,0x0003E0,0x00001f,0x00));
			mip2.MakeRGB16(RGBMask(0x007C00,0x0003E0,0x00001f,0x00));
			
			int cdsze,fdatloc=0;
			Image *timg;
			
			timg=&img;
			ImgDat=timg->Lock();
			cdsze=timg->GetWidth()*timg->GetHeight()*2;
			CopyMemory(FinalDat+fdatloc,ImgDat,cdsze);
			fdatloc+=cdsze;
			timg->UnLock();
			
			timg=&mip1;
			ImgDat=timg->Lock();
			cdsze=timg->GetWidth()*timg->GetHeight()*2;
			CopyMemory(FinalDat+fdatloc,ImgDat,cdsze);
			fdatloc+=cdsze;
			timg->UnLock();
			
			timg=&mip2;
			ImgDat=timg->Lock();
			cdsze=timg->GetWidth()*timg->GetHeight()*2;
			CopyMemory(FinalDat+fdatloc,ImgDat,cdsze);
			fdatloc+=cdsze;
			timg->UnLock();
		}
		break;
		
	case PS_RGBA:
		{
			img.CreateBlank(xsize,ysize,ITYPE_RGB,32,RGBMask(0x0000ff,0x00ff00,0xff0000,0xff000000));
			ImgDat=img.Lock();
			CopyMemory(ImgDat,OrgDat,pixs*4);
			img.UnLock();
			
			RECT src,dst;
			
			src.top=0; src.left=0;
			src.bottom=img.GetHeight();
			src.right=img.GetWidth();
			
			mip1.CreateBlank(img.GetWidth()/2,img.GetHeight()/2,ITYPE_RGB,32);
			dst.top=0; dst.left=0;
			dst.bottom=mip1.GetHeight();
			dst.right=mip1.GetWidth();
			
			mip1.Blt(img,dst,src);

			mip2.CreateBlank(img.GetWidth()/4,img.GetHeight()/4,ITYPE_RGB,32);
			dst.top=0; dst.left=0;
			dst.bottom=mip2.GetHeight();
			dst.right=mip2.GetWidth();
			mip2.Blt(img,dst,src);
			
			img.Pad(1);
			mip1.Pad(1);
			mip2.Pad(1);
			
			img.MakeRGB16(RGBMask(0xF000,0xF00,0xF0,0xF));
			mip1.MakeRGB16(RGBMask(0xF000,0xF00,0xF0,0xF));
			mip2.MakeRGB16(RGBMask(0xF000,0xF00,0xF0,0xF));
			
			int cdsze,fdatloc=0;
			Image *timg;
			
			timg=&img;
			ImgDat=timg->Lock();
			cdsze=timg->GetWidth()*timg->GetHeight()*2;
			CopyMemory(FinalDat+fdatloc,ImgDat,cdsze);
			fdatloc+=cdsze;
			timg->UnLock();
			
			timg=&mip1;
			ImgDat=timg->Lock();
			cdsze=timg->GetWidth()*timg->GetHeight()*2;
			CopyMemory(FinalDat+fdatloc,ImgDat,cdsze);
			fdatloc+=cdsze;
			timg->UnLock();
			
			timg=&mip2;
			ImgDat=timg->Lock();
			cdsze=timg->GetWidth()*timg->GetHeight()*2;
			CopyMemory(FinalDat+fdatloc,ImgDat,cdsze);
			fdatloc+=cdsze;
			timg->UnLock();
		}
		break;
		
	case PS_GRAYSCALE:
		{
			img.CreateBlank(xsize,ysize,ITYPE_INDEXED,8);
			ImgDat=img.Lock();
			CopyMemory(ImgDat,OrgDat,pixs);
			img.UnLock();
			
			img.Pad(1);
			
			int cdsze,fdatloc=0;
			Image *timg;
			
			timg=&img;
			ImgDat=timg->Lock();
			cdsze=timg->GetWidth()*timg->GetHeight();
			CopyMemory(FinalDat+fdatloc,ImgDat,cdsze);
			fdatloc+=cdsze;
			timg->UnLock();
		}
		
		break;
}

return FinalDat;
}



void  BidFreeData(unsigned char *data)
{
	delete data;
}

}

void __stdcall GetGameOSEnvironment( char* CommandLine )
{
}

