/***********************************************************************
*Copyright (c)2012 , 天际边工作室
*All rights reserved.
**
文件名称：	LibOperator.cpp
*摘要：		游戏引擎类，开始游戏界面
**
*修改历史：
*	版本    日期     作者     改动内容和原因
*   ----------------------------------------------------
*	1.0    2012-10-5  wstjb     基本的功能完成
***********************************************************************/

#include "PicConverter.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>



extern  PAINTSTRUCT ps;
extern 	HDC hdc;
extern 	HBITMAP hbmp; 
extern 	BITMAP bmp;
extern 	HBITMAP hbmp2; 
extern 	HDC hdcMem;
extern 	HDC hdcTmp;
extern 	RECT rect;
extern 	HWND hwnd;
extern int flag;
extern int tasknum;

extern int KeyReturn;
extern int KeyEsc;
extern int KeyHome;
extern int KeySpace;
extern int KeyLeft0;
extern int KeyUp0;
extern int KeyRight0;
extern int KeyDown0;
extern byte resByte[2731][16384];//存储lib资源
extern int resByteLen[2731];
extern int resPos[2731][6];//存储lib地址和编号 第六位为长度
extern int resCount;
extern int tmp_picMax;//20170102
//extern SIZE size;
//extern HDC hdcPaint;

void PicConverter::Init()
{

}

void PicConverter::KeyDown(WPARAM KeyCode)
{


}

/*void PicConverter::BufferTobiImageBuf(int type,int restype,int index)
{


}*/

byte PicConverter::BytesToImage()
{
	int count = BytesToInt1(bfSize);
	int width = BytesToInt1(biWidth);
	int height = BytesToInt1(biHeight);
	//byte buffer[count];
	//byte buffer1[count-62];
	byte buffer[16384];
	byte buffer1[16384];

	buffer[0] = 0x42;
	buffer[1] = 0x4d;
	buffer[2] = bfSize[0];
	buffer[3] = bfSize[1];
	buffer[4] = bfSize[2];
	buffer[5] = bfSize[3];
	buffer[6] = 0;
	buffer[7] = 0;
	buffer[8] = 0;
	buffer[9] = 0;
	buffer[10] = 0x3e;
	buffer[11] = 0;
	buffer[12] = 0;
	buffer[13] = 0;
	buffer[14] = 0x28;
	buffer[15] = 0;
	buffer[16] = 0;
	buffer[17] = 0;
	buffer[18]= biWidth[0];
	buffer[19] = biWidth[1];
	buffer[20] = biWidth[2];
	buffer[21] = biWidth[3];
	buffer[22] = biHeight[0];
	buffer[23] = biHeight[1];
	buffer[24] = biHeight[2];
	buffer[25] = biHeight[3];
	buffer[26] = 1;
	buffer[27] = 0;
	buffer[28] = 1;
	buffer[29] = 0;
	buffer[30] = 0;
	buffer[31] = 0;
	buffer[32] = 0;
	buffer[33] = 0;
	buffer[34] = biSizeImage[0];
	buffer[35] = biSizeImage[1];
	buffer[36] = biSizeImage[2];
	buffer[37] = biSizeImage[3];
	buffer[38]= 0xc4;
	buffer[39] = 0x0e;
	buffer[40] = 0;
	buffer[41] = 0;
	buffer[42] = 0xc4;
	buffer[43] = 0x0e;
	buffer[44] = 0;
	buffer[45] = 0;
	buffer[46] = 0;
	buffer[47] = 0;
	buffer[48] = 0;
	buffer[49] = 0;
	buffer[50] = 0;
	buffer[51] = 0;
	buffer[52] = 0;
	buffer[53] = 0;
	buffer[54] = 0;
	buffer[55] = 0;
	buffer[56] = 0;
	buffer[57] = 0;
	buffer[58] = 0xff;
	buffer[59] = 0xff;
	buffer[60] = 0xff;
	buffer[61] = 0;


	width = (width + 8 - width % 8) / 8;

	for (int i = 0; i < height; i++)
	{
		for (int j = 0; j < width; j++)
		{
			buffer1[(height - i - 1) * width + j] = biImage[i * width + j];
		}
	}

	for (int j = 62; j < count; j++)
	{
		//buffer[count - i + 61 ] = biImage[i - 62];
		buffer[j] = buffer1[j - 62];
	}


	////MemoryStream ms = new MemoryStream(buffer);
	//FileStream fs = File.Create("D:\\1.qq");
	//fs.Write(buffer, 0, buffer.Length);


	//Image image = System.Drawing.Image.FromStream(ms);

	//ms.Close();
	//fs.Close();
	return *buffer;
}

int PicConverter::GetResNumFromType(int type,int restype,int index)
{
	for (int ii = 0; ii < resCount / 3; ii++)//原有
	{
		if (resPos[ii][0] == type && resPos[ii][1] == restype && resPos[ii][2] == index)
		{
			return ii;
		}
	}
	return -1;
}


HBITMAP PicConverter::BytesToHBitmap(int type,int restype,int index,int no)
{
	int resnum = 0;//资源编号
	int piccnt = 0;//资源内图片数量 20210627

	resnum = GetResNumFromType(type,restype,index);

	for (int i = 6; i < resByteLen[resnum]; i++)
	{
		int tmp = 255 - BytesToInt1(&resByte[resnum][i]);
		IntToBytes1(tmp,&biImage[i - 6]);
	}

	piccnt = resByte[resnum][4];

	IntToBytes4(62 + resByteLen[resnum] - 6,bfSize);
	IntToBytes4(resByteLen[resnum] - 6,biSizeImage);
	memcpy(biWidth,&resByte[resnum][2],1);
	memcpy(biHeight,&resByte[resnum][3],1);

	//BytesToImage();

	int count = BytesToInt1(bfSize);
	int width = BytesToInt1(biWidth);
	int height = BytesToInt1(biHeight);

	byte buffer[16384];
	memset(buffer,0,sizeof(buffer));
	
	memset(&bi, 0, sizeof(bi));
	bi.bmiHeader.biSize = sizeof(bi.bmiHeader); // size of this struct
	bi.bmiHeader.biBitCount = 1;
	bi.bmiHeader.biCompression = 0;// rgb = 3 components
	bi.bmiHeader.biPlanes = 1;
	bi.bmiHeader.biWidth = width; // width of window
	bi.bmiHeader.biHeight = height; // height of window
	bi.bmiHeader.biClrUsed = 0;

	int i;
	for (i=0; i<2; i++)
	{
		bi.bmiColors[i].rgbRed  = i;
		bi.bmiColors[i].rgbGreen= i;
		bi.bmiColors[i].rgbBlue = i;
	}

	int width1 = width + 8 - width % 8;
	int tmp = 0;
	if(( width1 - width )%8 ==0)
	{
		tmp = 1;
	}

	
	width = (width + 8 - width % 8) / 8 ;
	

	int width2 = 0;
	if((width-tmp) %4 != 0)
	{
		width2 = (width - tmp)  + 4 - (width - tmp) % 4;
	}
	else
	{
		width2 = width - tmp;
	}

	//增加对no判断
	if(resByte[resnum][4] == 1  && no == 0)
	{
		for (int i = 0; i < height; i++)
		{
			for (int j = 0; j < width ; j++)
			{
				buffer[(height - i - 1) * width2  + j] = biImage[i * (width - tmp) + j];
			}
		}
	}
	else
	{
		for (int i = 0; i < height; i++)
		{
			for (int j = 0; j < width ; j++)
			{
				buffer[(height - i - 1) * width2  + j] = biImage[i * (width - tmp) + j + no * ((width1/8-tmp)*height) ]; 
			}
		}
	}
	//if(((width1/8-tmp)*height)>0)
	//{
	//	//tmp_picMax = resByteLen[resnum]/4/((width1/8-tmp)*height);
	//}

	tmp_picMax = piccnt;

	//HBITMAP hBitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bi, DIB_RGB_COLORS, (void**)&buffer, 0, 0);
	HBITMAP hBitmap = CreateDIBitmap(NULL, &bi.bmiHeader, CBM_INIT, buffer, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
	return hBitmap;
}

HBITMAP PicConverter::BytesToHBitmapTrans(int type,int restype,int index,int no)//透明
{
	int resnum = 0;//资源编号

	resnum = GetResNumFromType(type,restype,index);

	for (int i = 6; i < resByteLen[resnum]; i++)
	{
		int tmp = BytesToInt1(&resByte[resnum][i]);
		IntToBytes1(tmp,&biImage[i - 6]);
	}


	IntToBytes4(62 + resByteLen[resnum] - 6,bfSize);
	IntToBytes4(resByteLen[resnum] - 6,biSizeImage);
	memcpy(biWidth,&resByte[resnum][2],1);
	memcpy(biHeight,&resByte[resnum][3],1);

	//BytesToImage();

	int count = BytesToInt1(bfSize);
	int width = BytesToInt1(biWidth);
	int height = BytesToInt1(biHeight);

	byte buffer[16384];
	memset(buffer,0,sizeof(buffer));
	
	memset(&bi, 0, sizeof(bi));
	bi.bmiHeader.biSize = sizeof(bi.bmiHeader); // size of this struct
	bi.bmiHeader.biBitCount = 1;
	bi.bmiHeader.biCompression = 0;// rgb = 3 components
	bi.bmiHeader.biPlanes = 1;
	bi.bmiHeader.biWidth = width; // width of window
	bi.bmiHeader.biHeight = height; // height of window
	bi.bmiHeader.biClrUsed = 0;

	int i;
	for (i=0; i<2; i++)
	{
		bi.bmiColors[i].rgbRed  = i;
		bi.bmiColors[i].rgbGreen= i;
		bi.bmiColors[i].rgbBlue = i;
	}

	int width1 = width + 8 - width % 8;
	int tmp = 0;
	if(( width1 - width )%8 ==0)
	{
		tmp = 1;
	}
	width = (width + 8 - width % 8) / 8 ;
	
	int width2 = 0;
	if((width-tmp) %4 != 0)
	{
		width2 = (width - tmp)  + 4 - (width - tmp) % 4;
	}
	else
	{
		width2 = width - tmp;
	}


	//增加对no判断
	if(resByte[resnum][4] == 1  && no == 0)
	{
		for (int i = 0; i < height; i++)
		{
			for (int j = 0; j < width ; j++)
			{
				buffer[(height - i - 1) * width2  + j] = 
					(0x40 & biImage[(i * (width - tmp) + j)*2])<< 1 | (0x10 & biImage[(i * (width - tmp) + j)*2]) << 2 | (0x4 & biImage[(i * (width - tmp) + j)*2])<< 3 | (0x1 & biImage[(i * (width - tmp) + j)*2])<< 4 |
					(0x40 & biImage[(i * (width - tmp) + j)*2+1])>>3 | (0x10 & biImage[(i * (width - tmp) + j)*2+1])>>2 | (0x4 & biImage[(i * (width - tmp) + j)*2+1])>> 1 | (0x1 & biImage[(i * (width - tmp) + j)*2+1]);
				int tmp = 255 - BytesToInt1(&buffer[(height - i - 1) * width2  + j]);
				IntToBytes1(tmp,&buffer[(height - i - 1) * width2  + j]);
			}
		}
	}
	else
	{
		for (int i = 0; i < height; i++)
		{
			for (int j = 0; j < width ; j++)
			{
				int tmp1 = (i * (width - tmp) + j) * 2 + no * ((width1/8-tmp)*height) * 2;
				buffer[(height - i - 1) * width2  + j] = 
					(0x40 & biImage[tmp1])<< 1 | (0x10 & biImage[tmp1]) << 2 | (0x4 & biImage[tmp1])<< 3 | (0x1 & biImage[tmp1])<< 4 |
					(0x40 & biImage[tmp1+1])>>3 | (0x10 & biImage[tmp1+1])>>2 | (0x4 & biImage[tmp1+1])>> 1 | (0x1 & biImage[tmp1+1]);
				int tmp = 255 - BytesToInt1(&buffer[(height - i - 1) * width2  + j]);
				IntToBytes1(tmp,&buffer[(height - i - 1) * width2  + j]);
			}
		}
	}

	//HBITMAP hBitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bi, DIB_RGB_COLORS, (void**)&buffer, 0, 0);
	HBITMAP hBitmap = CreateDIBitmap(NULL, &bi.bmiHeader, CBM_INIT, buffer, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
	return hBitmap;
}

HBITMAP PicConverter::BytesToHBitmapMask(int type,int restype,int index,int no)//透明mask
{
	int resnum = 0;//资源编号

	resnum = GetResNumFromType(type,restype,index);

	for (int i = 6; i < resByteLen[resnum]; i++)
	{
		int tmp = BytesToInt1(&resByte[resnum][i]);
		IntToBytes1(tmp,&biImage[i - 6]);
	}


	IntToBytes4(62 + resByteLen[resnum] - 6,bfSize);
	IntToBytes4(resByteLen[resnum] - 6,biSizeImage);
	memcpy(biWidth,&resByte[resnum][2],1);
	memcpy(biHeight,&resByte[resnum][3],1);

	//BytesToImage();

	int count = BytesToInt1(bfSize);
	int width = BytesToInt1(biWidth);
	int height = BytesToInt1(biHeight);

	byte buffer[16384];
	memset(buffer,0,sizeof(buffer));
	
	memset(&bi, 0, sizeof(bi));
	bi.bmiHeader.biSize = sizeof(bi.bmiHeader); // size of this struct
	bi.bmiHeader.biBitCount = 1;
	bi.bmiHeader.biCompression = 0;// rgb = 3 components
	bi.bmiHeader.biPlanes = 1;
	bi.bmiHeader.biWidth = width; // width of window
	bi.bmiHeader.biHeight = height; // height of window
	bi.bmiHeader.biClrUsed = 0;

	int i;
	for (i=0; i<2; i++)
	{
		bi.bmiColors[i].rgbRed  = i;
		bi.bmiColors[i].rgbGreen= i;
		bi.bmiColors[i].rgbBlue = i;
	}

	int width1 = width + 8 - width % 8;
	int tmp = 0;
	if(( width1 - width )%8 ==0)
	{
		tmp = 1;
	}
	width = (width + 8 - width % 8) / 8 ;
	
	int width2 = 0;
	if((width-tmp) %4 != 0)
	{
		width2 = (width - tmp)  + 4 - (width - tmp) % 4;
	}
	else
	{
		width2 = width - tmp;
	}

	//增加对no判断
	if(resByte[resnum][4] == 1  && no == 0)
	{
		for (int i = 0; i < height; i++)
		{
			for (int j = 0; j < width ; j++)
			{
				buffer[(height - i - 1) * width2  + j] = 
					(0x80 & biImage[(i * (width - tmp) + j)*2]) | (0x20 & biImage[(i * (width - tmp) + j)*2]) << 1 | (0x8 & biImage[(i * (width - tmp) + j)*2])<< 2 | (0x2 & biImage[(i * (width - tmp) + j)*2])<< 3 |
					(0x80 & biImage[(i * (width - tmp) + j)*2+1])>>4 | (0x20 & biImage[(i * (width - tmp) + j)*2+1])>>3 | (0x8 & biImage[(i * (width - tmp) + j)*2+1])>> 2 | (0x2 & biImage[(i * (width - tmp) + j)*2+1])>> 1;
				int tmp = 255 - BytesToInt1(&buffer[(height - i - 1) * width2  + j]);
				IntToBytes1(tmp,&buffer[(height - i - 1) * width2  + j]);
			}
		}
	}
	else
	{
		for (int i = 0; i < height; i++)
		{
			for (int j = 0; j < width ; j++)
			{
				int tmp1 = (i * (width - tmp) + j) * 2 + no * ((width1/8-tmp)*height) * 2;
				buffer[(height - i - 1) * width2  + j] = 
					(0x80 & biImage[tmp1]) | (0x20 & biImage[tmp1]) << 1 | (0x8 & biImage[tmp1])<< 2 | (0x2 & biImage[tmp1])<< 3 |
					(0x80 & biImage[tmp1+1])>>4 | (0x20 & biImage[tmp1+1])>>3 | (0x8 & biImage[tmp1+1])>> 2 | (0x2 & biImage[tmp1+1])>> 1;
				int tmp = 255 - BytesToInt1(&buffer[(height - i - 1) * width2  + j]);
				IntToBytes1(tmp,&buffer[(height - i - 1) * width2  + j]);
			}
		}
	}

	//HBITMAP hBitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bi, DIB_RGB_COLORS, (void**)&buffer, 0, 0);
	HBITMAP hBitmap = CreateDIBitmap(NULL, &bi.bmiHeader, CBM_INIT, buffer, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
	return hBitmap;
}

void PicConverter::GetCGByte(int picnum,int resnum,int frameCount,int transtype)
{
	int width = 0;
	int width1 = 0;
	int height = 0;
	int bytecount = 0;
	int j = 0;
	int bytestartpos = 0;
	for (int i = 6 + frameCount*5 ; i < resByteLen[resnum];)
	{
		
		width = BytesToInt1(&resByte[resnum][i+2]);
		width1 = width + 8 - width % 8;
		if(width1 - width == 8 )width1 -= 8;
		height = BytesToInt1(&resByte[resnum][i+3]);
		bytecount = (resByte[resnum][i+5] == 1)?(width1 * height /8):(width1 * height /4);
		if(j == picnum ) 
		{
			bytestartpos = i;
			break;
		}
		j++;
		i = i + bytecount + 6 ;
	}
	
	cgByteLen = bytecount + 6;
	for(int k = 0 ; k < cgByteLen ; k++)
	{
		cgByte[k] = resByte[resnum][bytestartpos + k];
	}

}

HBITMAP PicConverter::BytesToHBitmapCG(int picnum,int resnum,int frameCount)
{
	GetCGByte(picnum,resnum,frameCount,1);


	for (int i = 6 ; i < cgByteLen; i++)
	{
		int tmp = 255 - BytesToInt1(&cgByte[i]);
		IntToBytes1(tmp,&biImage[i - 6]);
	}


	IntToBytes4(62 + cgByteLen - 6,bfSize);
	IntToBytes4(cgByteLen - 6,biSizeImage);
	memcpy(biWidth,&cgByte[2],1);
	memcpy(biHeight,&cgByte[3],1);

	//BytesToImage();

	int count = BytesToInt1(bfSize);
	int width = BytesToInt1(biWidth);
	int height = BytesToInt1(biHeight);

	byte buffer[16384];
	memset(buffer,0,sizeof(buffer));
	
	memset(&bi, 0, sizeof(bi));
	bi.bmiHeader.biSize = sizeof(bi.bmiHeader); // size of this struct
	bi.bmiHeader.biBitCount = 1;
	bi.bmiHeader.biCompression = 0;// rgb = 3 components
	bi.bmiHeader.biPlanes = 1;
	bi.bmiHeader.biWidth = width; // width of window
	bi.bmiHeader.biHeight = height; // height of window
	bi.bmiHeader.biClrUsed = 0;

	int i;
	for (i=0; i<2; i++)
	{
		bi.bmiColors[i].rgbRed  = i;
		bi.bmiColors[i].rgbGreen= i;
		bi.bmiColors[i].rgbBlue = i;
	}

	int width1 = width + 8 - width % 8;
	int tmp = 0;
	if(( width1 - width )%8 ==0)
	{
		tmp = 1;
	}
	width = (width + 8 - width % 8) / 8 ;
	
	int width2 = 0;
	if((width-tmp) %4 != 0)
	{
		width2 = (width - tmp)  + 4 - (width - tmp) % 4;
	}
	else
	{
		width2 = width - tmp;
	}

	for (int i = 0; i < height; i++)
	{
		for (int j = 0; j < width ; j++)
		{
			buffer[(height - i - 1) * width2  + j] = biImage[i * (width - tmp) + j];
		}
	}

	//HBITMAP hBitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bi, DIB_RGB_COLORS, (void**)&buffer, 0, 0);
	HBITMAP hBitmap = CreateDIBitmap(NULL, &bi.bmiHeader, CBM_INIT, buffer, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
	return hBitmap;
}

HBITMAP PicConverter::BytesToHBitmapCGMask(int picnum,int resnum,int frameCount,int &transflag)
{
	GetCGByte(picnum,resnum,frameCount,2);
	if(cgByte[5]  == 1)
	{
		transflag = 1;
		return NULL;
	}
	transflag = 2;
	for (int i = 6 ; i < cgByteLen; i++)
	{
		int tmp = 255 - BytesToInt1(&cgByte[i]);
		IntToBytes1(tmp,&biImage[i - 6]);
	}


	IntToBytes4(62 + cgByteLen - 6,bfSize);
	IntToBytes4(cgByteLen - 6,biSizeImage);
	memcpy(biWidth,&cgByte[2],1);
	memcpy(biHeight,&cgByte[3],1);

	//BytesToImage();

	int count = BytesToInt1(bfSize);
	int width = BytesToInt1(biWidth);
	int height = BytesToInt1(biHeight);

	byte buffer[16384];
	memset(buffer,0,sizeof(buffer));
	
	memset(&bi, 0, sizeof(bi));
	bi.bmiHeader.biSize = sizeof(bi.bmiHeader); // size of this struct
	bi.bmiHeader.biBitCount = 1;
	bi.bmiHeader.biCompression = 0;// rgb = 3 components
	bi.bmiHeader.biPlanes = 1;
	bi.bmiHeader.biWidth = width; // width of window
	bi.bmiHeader.biHeight = height; // height of window
	bi.bmiHeader.biClrUsed = 0;

	int i;
	for (i=0; i<2; i++)
	{
		bi.bmiColors[i].rgbRed  = i;
		bi.bmiColors[i].rgbGreen= i;
		bi.bmiColors[i].rgbBlue = i;
	}

	int width1 = width + 8 - width % 8;
	int tmp = 0;
	if(( width1 - width )%8 ==0)
	{
		tmp = 1;
	}
	width = (width + 8 - width % 8) / 8 ;
	
	int width2 = 0;
	if((width-tmp) %4 != 0)
	{
		width2 = (width - tmp)  + 4 - (width - tmp) % 4;
	}
	else
	{
		width2 = width - tmp;
	}

	for (int i = 0; i < height; i++)
	{
		for (int j = 0; j < width ; j++)
		{
			buffer[(height - i - 1) * width2  + j] = 
				(0x80 & biImage[(i * (width - tmp) + j)*2]) | (0x20 & biImage[(i * (width - tmp) + j)*2]) << 1 | (0x8 & biImage[(i * (width - tmp) + j)*2])<< 2 | (0x2 & biImage[(i * (width - tmp) + j)*2])<< 3 |
				(0x80 & biImage[(i * (width - tmp) + j)*2+1])>>4 | (0x20 & biImage[(i * (width - tmp) + j)*2+1])>>3 | (0x8 & biImage[(i * (width - tmp) + j)*2+1])>> 2 | (0x2 & biImage[(i * (width - tmp) + j)*2+1])>> 1;
			int tmp = BytesToInt1(&buffer[(height - i - 1) * width2  + j]);
			IntToBytes1(tmp,&buffer[(height - i - 1) * width2  + j]);
		}
	}

	//HBITMAP hBitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bi, DIB_RGB_COLORS, (void**)&buffer, 0, 0);
	HBITMAP hBitmap = CreateDIBitmap(NULL, &bi.bmiHeader, CBM_INIT, buffer, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
	return hBitmap;
}

HBITMAP PicConverter::BytesToHBitmapCGTrans(int picnum,int resnum,int frameCount)
{
	GetCGByte(picnum,resnum,frameCount,2);


	for (int i = 6 ; i < cgByteLen; i++)
	{
		int tmp = 255 - BytesToInt1(&cgByte[i]);
		IntToBytes1(tmp,&biImage[i - 6]);
	}


	IntToBytes4(62 + cgByteLen - 6,bfSize);
	IntToBytes4(cgByteLen - 6,biSizeImage);
	memcpy(biWidth,&cgByte[2],1);
	memcpy(biHeight,&cgByte[3],1);

	//BytesToImage();

	int count = BytesToInt1(bfSize);
	int width = BytesToInt1(biWidth);
	int height = BytesToInt1(biHeight);

	byte buffer[16384];
	memset(buffer,0,sizeof(buffer));
	
	memset(&bi, 0, sizeof(bi));
	bi.bmiHeader.biSize = sizeof(bi.bmiHeader); // size of this struct
	bi.bmiHeader.biBitCount = 1;
	bi.bmiHeader.biCompression = 0;// rgb = 3 components
	bi.bmiHeader.biPlanes = 1;
	bi.bmiHeader.biWidth = width; // width of window
	bi.bmiHeader.biHeight = height; // height of window
	bi.bmiHeader.biClrUsed = 0;

	int i;
	for (i=0; i<2; i++)
	{
		bi.bmiColors[i].rgbRed  = i;
		bi.bmiColors[i].rgbGreen= i;
		bi.bmiColors[i].rgbBlue = i;
	}

	int width1 = width + 8 - width % 8;
	int tmp = 0;
	if(( width1 - width )%8 ==0)
	{
		tmp = 1;
	}
	width = (width + 8 - width % 8) / 8 ;
	
	int width2 = 0;
	if((width-tmp) %4 != 0)
	{
		width2 = (width - tmp)  + 4 - (width - tmp) % 4;
	}
	else
	{
		width2 = width - tmp;
	}

	for (int i = 0; i < height; i++)
	{
		for (int j = 0; j < width ; j++)
		{
			buffer[(height - i - 1) * width2  + j] = 
				(0x40 & biImage[(i * (width - tmp) + j)*2])<< 1 | (0x10 & biImage[(i * (width - tmp) + j)*2]) << 2 | (0x4 & biImage[(i * (width - tmp) + j)*2])<< 3 | (0x1 & biImage[(i * (width - tmp) + j)*2])<< 4 |
				(0x40 & biImage[(i * (width - tmp) + j)*2+1])>>3 | (0x10 & biImage[(i * (width - tmp) + j)*2+1])>>2 | (0x4 & biImage[(i * (width - tmp) + j)*2+1])>> 1 | (0x1 & biImage[(i * (width - tmp) + j)*2+1]);
			int tmp = BytesToInt1(&buffer[(height - i - 1) * width2  + j]);
			IntToBytes1(tmp,&buffer[(height - i - 1) * width2  + j]);
		}
	}

	//HBITMAP hBitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bi, DIB_RGB_COLORS, (void**)&buffer, 0, 0);
	HBITMAP hBitmap = CreateDIBitmap(NULL, &bi.bmiHeader, CBM_INIT, buffer, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
	return hBitmap;
}

int PicConverter::GetPicNumInPic(int type,int restype,int index)//获取pic number
{
	int resnum = 0;//资源编号

	resnum = GetResNumFromType(type,restype,index);

	int tmp = BytesToInt1(&resByte[resnum][4]);
	
	return tmp;
}

int PicConverter::GetDefaultWidth(int type,int restype,int index,int no)
{
	int resnum = 0;//资源编号

	resnum = GetResNumFromType(type,restype,index);

	memcpy(biWidth,&resByte[resnum][2],1);
	
	int width = BytesToInt1(biWidth);
	
	return width;
}


int PicConverter::GetDefaultHeight(int type,int restype,int index,int no)
{
	int resnum = 0;//资源编号

	resnum = GetResNumFromType(type,restype,index);

	
	memcpy(biHeight,&resByte[resnum][3],1);

	
	int height = BytesToInt1(biHeight);

	return height;
}