#include "stdafx.h"
#include "ImageReader.h"
#include "Macros.h"
//#include "bvtspv.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CTgaReader::CTgaReader()
{
    HANDLE m_hHandle = NULL;
	BufPoint = 0;
	DataBuf  = NULL;
}

CTgaReader::~CTgaReader()
{
   //deletion now done in the engine (PATCH!)
//	GlobalFree(m_hHandle);
	if(m_hBitmap != NULL)
	{
		DeleteObject(m_hBitmap);
		m_hBitmap = NULL;
	}
}

int	CTgaReader::SaveToTGA(CString filename, CDC* pDC, CRect rect, UINT uFormat)
{
	// 打开文件
	CFile file;
	if(!file.Open(filename, CFile::modeCreate|CFile::modeReadWrite))
	{
		TRACE(_T("Can not Open file %s\n"), filename);
		return SAVE_FILE_OPEN_ERR;
	}

	// 将图片复制到内存的DC中
	int w, h;
	w = rect.Width();
	h = rect.Height();

	ASSERT(pDC != NULL); 
	CDC dcTGA;
	dcTGA.CreateCompatibleDC(pDC);
	CBitmap	bmpTGA;
	bmpTGA.CreateBitmap(w, h, 1, 32, NULL);
	CBitmap* pOldBmp = dcTGA.SelectObject(&bmpTGA);
	dcTGA.BitBlt(0, 0, w, h, pDC, rect.left, rect.top, SRCCOPY);
	
	// 将图片的数据读入到cBits中
	BITMAPINFO bmpInfo;
	bmpInfo.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
	bmpInfo.bmiHeader.biWidth = w;
	bmpInfo.bmiHeader.biHeight = h;
	bmpInfo.bmiHeader.biPlanes = 1;
	bmpInfo.bmiHeader.biBitCount = 32; 
	bmpInfo.bmiHeader.biCompression = BI_RGB;
	//bmpInfo.bmiHeader.biSizeImage=Width*Height*4;

	long total = w * h;
	COLORREF* cBits = new COLORREF[total];

	GetDIBits ( dcTGA.m_hDC, HBITMAP(bmpTGA), 0, h, cBits, &(bmpInfo), DIB_RGB_COLORS );

	unsigned char t[18];
	t[0] = t[1] = 0;
	t[2] = uFormat;
	t[3] = t[4] = t[5] = t[6] = t[7] = 0;
	*(unsigned short*)(&t[8]) = (unsigned short) rect.left;
	*(unsigned short*)(&t[10]) = (unsigned short) rect.top;
	*(unsigned short*)(&t[12]) = (unsigned short) rect.Width();
	*(unsigned short*)(&t[14]) = (unsigned short) rect.Height();
	t[16] = 32;
	t[17] = 8;

//	CFileException eFile;
	unsigned char* bits = NULL;
	try
	{
		// write the file header
		file.Write(t, 18L);

		if(uFormat == 2)
		{		// 无压缩的32位色格式
			file.Write(cBits, total*4);
		}
		else if(uFormat == 10)
		{		// run length压缩的32位色格式
			bits = new unsigned char[total*8];
			int length = MakeRunLengthBits(cBits, bits, total);
			file.Write(bits, length);
			delete [] bits;
		}
	}
	catch(CFileException *eFile)
	{
		TRACE(_T("Write File Error: FileName: %s, Cause NO: %d, Error NO: %ld \n"), 
			eFile->m_strFileName, eFile->m_cause, eFile->m_lOsError);

		delete [] cBits;
		if(bits) delete [] bits;
		file.Close();
		dcTGA.SelectObject(pOldBmp);
		return SAVE_FILE_WRITE_ERR;
	}

	delete [] cBits;
	file.Close();
	dcTGA.SelectObject(pOldBmp);

	return SAVE_SUCCESS;
}

int CTgaReader::MakeRunLengthBits(COLORREF* cBits, UCHAR* bits, long total)
{
	COLORREF c;
	c = cBits[0];
	int j =1;
	int k = 0;
	for(int i=1; i<total; i++) {
		if(cBits[i] == c) {
			j++;
			if(i == total -1) {
				bits[k] = j+127;
				*(COLORREF*)(&bits[k+1]) = c;
				k += 5;
				j = 0;
			}
			if(j >= 128) {
				bits[k] = j+127;
				*(COLORREF*)(&bits[k+1]) = c;
				k += 5;
				j = 0;
			}
		}
		else {
			bits[k] = j+128;
			*(COLORREF*)(&bits[k+1]) = c;
			k += 5;
			c = cBits[i];
			j = 0;
		}
	}

	return k;
}

int	CTgaReader::LoadImage(CString filename,  CBitmap* pBitmap)
{
	CFile file;
	CFileException e;
	TRY
    {
	    file.Open(filename, CFile::modeRead | CFile::typeBinary);	 
    
	}
    CATCH( CFileException, e )
    {
	   ::AfxMessageBox(filename);	              
    }
   END_CATCH  
	 

	if(file.m_hFile == NULL)
		return LOAD_BADFILE;
	HFILE handle;
	handle = (HFILE)file.m_hFile;
	if(handle==-1)
	{
		_lclose(handle);
		return(0);
	}

	TGA_HEADER head;

	int iRead = file.Read(&(head.Id), 1L);
	iRead = file.Read(&(head.Cmt), 1L);
	iRead = file.Read(&(head.It), 1L);
	iRead = file.Read(&(head.Cms1), 2L);
	iRead = file.Read(&(head.Cms2), 2L);
	iRead = file.Read(&(head.Cms3), 1L);
	iRead = file.Read(&(head.XOrigin), 2L);
	iRead = file.Read(&(head.YOrigin), 2L);
	iRead = file.Read(&(head.TgaXSize), 2L);
	iRead = file.Read(&(head.TgaYSize), 2L);
	iRead = file.Read(&(head.PixelDepth), 1L);
	iRead = file.Read(&(head.ImgDescriptor), 1L);

	if(head.It!=2 && head.It!=10)
	{
		 _lclose(handle);
		 return(FORMAT_ERROR);
	}

	int w = head.TgaXSize;
	int h = head.TgaYSize;

	COLORREF* cBits = new COLORREF[w*h];
	memset(cBits,0,w*h*4);

	BITMAPINFO bmpInfo;
	bmpInfo.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
	bmpInfo.bmiHeader.biWidth = w;//head.TgaXSize;
	bmpInfo.bmiHeader.biHeight = h;//head.TgaYSize;
	bmpInfo.bmiHeader.biPlanes = 1;
	bmpInfo.bmiHeader.biBitCount = 32; 
	bmpInfo.bmiHeader.biCompression = BI_RGB;
 
    long size;

    size=18L;
    if(head.Id>0) 
	   size+=(long)head.Id;
    if(head.Cmt==1)
	  size+=((long)head.Cms2*((long)head.Cms3/8));

	ReadCelFIle(handle, &head, NULL, cBits, w, h,
	   w, 0, size, NO, _32BIT, 1);

	pBitmap->DeleteObject();
	pBitmap->CreateBitmap(w, h,1,32,cBits);

	delete cBits;
	if( file.m_hFile != NULL)
		file.Close();
	
	return 1;
}

          

void CTgaReader::Trans32To16(unsigned long *color32, unsigned short *color16p)
{
	  unsigned long color=*color32;
	  unsigned short* color16=color16p;
	  *color16=0;
	  *color16|=(unsigned short)((color&0x000000ffL)>>3);
	  *color16|=(unsigned short)(((color&0x0000ff00L)>>11)<<5);
	  *color16|=(unsigned short)(((color&0x00ff0000L)>>19)<<10);
	  if((color&0x80000000L)!=0) *color16|=0x8000;
	  return;

}

void CTgaReader::TransData16To32(void *data1, void *data, short size)
{
	  int i;
	  unsigned long *data32;
	  unsigned short* data16;
	  data32=(unsigned long*)data;
	  data16=(unsigned short*)data1;
	  for(i=0; i<size; i++) 
		  Trans16To32(&data16[i], &data32[i]);
	  return;

}

void CTgaReader::Trans16To32(unsigned short *color16, unsigned long *color32p)
{
	  unsigned short color=*color16;
	  unsigned long* color32=color32p;
	  *color32=0;
	  *color32|=((color&0x001f)<<3);
	  *color32|=((color&0x03e0)<<6);
	  *color32|=((color&0x7c00)<<9);
	  if((color&0x8000)!=0) *color32|=0xff000000L;
	  return;

}

void CTgaReader::TransData32To16(void *data1, void *data, short size)
{
      unsigned long *data32;
	  int i;
	  unsigned short* data16;
	  data32=(unsigned long*)data1;
	  data16=(unsigned short*)data;
	  for(i=0; i<size; i++) Trans32To16(&data32[i], &data16[i]);
	  return;

}

void CTgaReader::TransData24To32(void *data1, void *data, short size)
{
	 unsigned char* data24, t1,t2,t3;
	  int i;
	  unsigned long* data32;
	  data24=(unsigned char*)data1;
	  data32=(unsigned long*)data;
	  for(i=0; i<size; i++)
	  {
		 t1	= (unsigned char)data24[i*3];
		 t2	= (unsigned char)data24[i*3+1];
		 t3	= (unsigned char)data24[i*3+2];
		 data32[i]=( unsigned long )(0xff000000L |
					  ( (unsigned long )t1)|
					  ( ( (unsigned long ) t2)<<8)|
					  ( ( (unsigned long ) t3)<<16)
					  );
	  }
	  return;

}

void CTgaReader::TransData24To16(void *data1, void *data, short size)
{
  unsigned long temp;
  int i;
  unsigned short* data16;
  char* data24;
  data24=(char*)data1;
  data16=(unsigned short*)data;
  for(i=0; i<size; i++)
  {
	 temp=0xff000000L|((unsigned long)data24[i*3])|
	 (((unsigned long)data24[i*3+1])<<8)|(((unsigned long)data24[i*3+2])<<16);
	 Trans32To16(&temp, &data16[i]);
  }
  return;
}


void CTgaReader::GetALLScrTable(short xsize, short ysize, short scrx, 
								short scry, short *xtab, short *ytab)
{
  short i;
  double para, total;
  para=(double)xsize/(double)scrx;
  total=0;
  for(i=0; i<scrx; i++)
  {
	 xtab[i]=(short)total;
	 total+=para;
  }
  para=(double)ysize/(double)scry;
  total=0;
  for(i=0; i<scry; i++)
  {
	 ytab[i]=(short)total;
	 total+=para;
  }
  return;
}
//写一行数据到板卡内存里，即显示出来。
/*INPUT
   TGA_HEADER *head：		    指向当前tga文件的头，
   unsigned char *data：    	图象地址
   void *tdata：				卡地址
   char *matxbuf, 
   short *xtable：				 the address of the table from xsize to screen row
   short xsize：				 width of the image in pixels 
   short protflag
   short matxflag, 
   short xdirection：			 what direction to draw
   short pixelflag,：			 the depth per pixel in screen
   short visibleflag
   //if visibleflag != 0
    		//protflag =1 覆盖掉原有图元，带背景输出
    		//protflag = 0 在原有图元上叠加
   //if visibleflag = 0   覆盖

*/
void CTgaReader::WriteLine(TGA_HEADER *head, unsigned char* data, void *tdata, 
						   char *matxbuf, short *xtable, short xsize, short protflag, 
						   short matxflag, short xdirection, short pixelflag, 
						   short visibleflag)
{
  short i, i1, xfirst, xlast, xinc, xinc1, xsize1;
  unsigned long *sdata32,   *tdata32;
  unsigned short *sdata16,  *tdata16;
  char flag;
  if(matxflag==YES)
  {
	 if(head->TgaXSize%8==0) xsize1=head->TgaXSize/8;
	 else                    xsize1=head->TgaXSize/8+1;
  }
  else xsize1=0;
  if(xdirection==_PX)
  {
	 xfirst=0;
	 xlast=xsize-1;
	 xinc=1;
  }
  else
  {
	 xfirst=xsize-1;
	 xlast=0;
	 xinc=-1;
  }
  i1=0;
  xinc1=1;
  if(pixelflag==_32BIT)
  {
	 sdata32=(unsigned long*)data;
	 tdata32=(unsigned long *)tdata;
	 i=xfirst;
	 if(matxflag==NO)
	 {
		do
		{
			if(protflag==ON)
			{
				if(visibleflag==YES)
				{
					//if((sdata32[xtable[i]]&0x00ffffffL)!=0)
						tdata32[i1]=sdata32[xtable[i]];//&0x00ffffffL;
				}
				else
				{
					if((sdata32[xtable[i]]&0x00ffffffL) != 0)
						tdata32[i1]=sdata32[xtable[i]];//|0xff000000L;
				}
			}
			else
			{
				if(visibleflag==YES)
				{
				//if((sdata32[xtable[i]]&0x00ffffffL)==0)
				//		tdata32[i1]=0x00000000L;
				//	else
						tdata32[i1]=sdata32[xtable[i]];//&0x00ffffffL;
				}
				else
				{
					if((sdata32[xtable[i]]&0x00ffffffL)==0)
						tdata32[i1]=0xff000000L;//10101
					else
						tdata32[i1]=sdata32[xtable[i]];//|0xff000000L;
				}
			}
		if(i==xlast) break;
			i+=xinc;
			i1+=xinc1;
	 }while(1==1);
  }
  else
  {
		do{
			 if(xdirection==_PX)
				flag=matxbuf[xtable[i]/8]&(0x80>>(xtable[i]%8));
			 else
					flag=matxbuf[xsize1-xtable[i]/8]&(0x1<<(8-xtable[i]%8));
			 if(flag!=0)
			 {
				 if(protflag==ON)
				 {
						if(visibleflag==YES)
						{
							if((sdata32[xtable[i]]&0x00ffffffL)!=0)
								tdata32[i1]=sdata32[xtable[i]];//&0x00ffffffL;
						}
						else
						{
							if((sdata32[xtable[i]]&0x00ffffffL)!=0)
								tdata32[i1]=sdata32[xtable[i]];//|0xff000000L;
						}
				}
				else
				{
					if(visibleflag==YES)
					{
						if((sdata32[xtable[i]]&0x00ffffffL)==0)
							tdata32[i1]=0x00000000L;
						else
							tdata32[i1]=sdata32[xtable[i]];//&0x00ffffffL;
					}
					else
					{
						if((sdata32[xtable[i]]&0x00ffffffL)==0)
								tdata32[i1]=0xff000000L;
						else
							tdata32[i1]=sdata32[xtable[i]];//|0xff000000L;
					}
				}
			 }
			 if(i==xlast) break;
			i+=xinc;
			i1+=xinc1;
		}while(1==1);
	 }
  }
  else
  {
	 sdata16=(unsigned short*)data;
	 tdata16=(unsigned short*)tdata;
	 i=xfirst;
	 if(matxflag==NO)
	 {
		do
		{
			if(protflag==ON)
			{
				if(visibleflag==YES)
				{
					if((sdata16[xtable[i]]&0x7fff)!=0)
					{
						if((sdata16[xtable[i]]&0x7c00)==0)
							tdata16[i1]=(sdata16[xtable[i]]&0x7fff)|0x0400;
						else tdata16[i1]=sdata16[xtable[i]]&0x7fff;
					}
			  }
			  else
			  {
					if((sdata16[xtable[i]]&0x7fff)!=0)
					{
					  if((sdata16[xtable[i]]&0x7c00)==0)
						 tdata16[i1]=sdata16[xtable[i]]|0x8400;
					  else tdata16[i1]=sdata16[xtable[i]]|0x8000;
					}
			  }
			}
			else
			{
			  if(visibleflag==YES)
			  {
				if((sdata16[xtable[i]]&0x7c00)==0)
				  tdata16[i1]=(sdata16[xtable[i]]&0x7fff)|0x0400;
				else  tdata16[i1]=sdata16[xtable[i]]&0x7fff;
			  }
			  else
			  {
				if((sdata16[xtable[i]]&0x7c00)==0)
				  tdata16[i1]=sdata16[xtable[i]]|0x8400;
				else
				  tdata16[i1]=sdata16[xtable[i]]|0x8000;
			  }
			}
			if(i==xlast) break;
			i+=xinc;
			i1+=xinc1;
		} while(1==1);
	 }
	 else
	 {
		do
		{
			if(xdirection==_PX)
					flag=matxbuf[xtable[i]/8]&(0x80>>(xtable[i]%8));
			else
					flag=matxbuf[xsize1-xtable[i]/8]&(0x1<<(8-xtable[i]%8));
			if(flag!=0)
			{
			  if(protflag==ON)
			  {
				if(visibleflag==YES)
				{
				  if((sdata16[xtable[i]]&0x7fff)!=0)
				  {
					 if((sdata16[xtable[i]]&0x7c00)==0)
						tdata16[i1]=(sdata16[xtable[i]]&0x7fff)|0x0400;
					 else
						tdata16[i1]=sdata16[xtable[i]]&0x7fff;
				  }
			  }
			  else
			  {
				  if((sdata16[xtable[i]]&0x7fff)!=0)
				  {
					 if((sdata16[xtable[i]]&0x7c00)==0)
						tdata16[i1]=sdata16[xtable[i]]|0x8400;
					 else
						tdata16[i1]=sdata16[xtable[i]]|0x8000;
				  }
			  }
		  }
		  else
		  {
			if(visibleflag==YES)
			{
			  if((sdata16[xtable[i]]&0x7c00)==0)
				 tdata16[i1]=(sdata16[xtable[i]]&0x7fff)|0x0400;
			  else
				 tdata16[i1]=sdata16[xtable[i]]&0x7fff;
			}
			else
			{
			  if((sdata16[xtable[i]]&0x7c00)==0)
				 tdata16[i1]=sdata16[xtable[i]]|0x8400;
			  else
				 tdata16[i1]=sdata16[xtable[i]]|0x8000;
			}
		  }
		}
		if(i==xlast) break;
		i+=xinc;
		i1+=xinc1;
		}while(1==1);
	 }
  }
  return;

}
//释放已分配的内存。
void CTgaReader::winfree(LPVOID ptr)
{
	HGLOBAL hmem=(HGLOBAL)GlobalHandle(ptr);
	GlobalUnlock(hmem);
	GlobalFree(hmem);

}
//分配一块内存
LPVOID CTgaReader::winalloc(long size)
{

	HGLOBAL hmem;
	hmem=GlobalAlloc(GHND,size);
	return GlobalLock(hmem);
}

//解一行游码，
void CTgaReader::ReadTgaCompressLine(HFILE handle, void *data1, short xsize, short pixelsize, short *total)
{
  unsigned int i, j, k, tt;
  unsigned char cc1, cc;
  unsigned int para, para1, len;
  char temp[4], *data;
  para=pixelsize/8;
  i=0;
  if(*total>xsize)
  {
	 *total-=xsize;
	 memcpy((char*)data1, &((char*)data1)[xsize*para], *total*para);
	 if(*total>=xsize) return;
	 i=*total;
  }
  data=(char*)data1;
  if(BufPoint<0)
  {
	 _lread(handle, DataBuf, BUF_SIZE);
	 BufPoint=0;
  }
  do
  {
	 cc=(unsigned char)DataBuf[BufPoint];
	 cc1=(cc&0x7f);
	 BufPoint++;
	 if(BufPoint>=BUF_SIZE)
	 {
		_lread(handle, DataBuf, BUF_SIZE);
		BufPoint=0;
	 }
	 len=1+(int)(cc1);
	 if((cc&0x80)!=0)
	 {
		for(j=0; j<para; j++)
		{
			temp[j]=DataBuf[BufPoint];
			BufPoint++;
			if(BufPoint>=BUF_SIZE)
			{
				_lread(handle, DataBuf, BUF_SIZE);
				BufPoint=0;
			}
		}
		for(j=0; j<len; j++)
		{
			para1=(i+j)*para;
			for(k=0; k<para; k++) data[para1+k]=temp[k];
		}
	 }
	 else
	 {
		para1=len*para;
		tt=i*para;
		for(j=0; j<para1; j++)
		{
			data[tt+j]=DataBuf[BufPoint];
			BufPoint++;
			if(BufPoint>=BUF_SIZE)
			{
				  _lread(handle, DataBuf, BUF_SIZE);
				  BufPoint=0;
			}
		}
	 }
	 i+=len;
	 if(i>(unsigned int)(xsize-1))
	 {
		*total=(short)i;
		break;
	 }
  } while(1==1);
  return;

}

void CTgaReader::ReadCelFIle(HFILE handle, TGA_HEADER *head, char *matxbuf, void *pp, 
							 short xsize, short ysize, short scrnx, short protflag, 
							 long offset, short mflag, short pixelflag, short visibleflag)
{
	int para, para1;
  long wsize;
  char	 * tmp_pp;
  unsigned char  *data,  *tdata,  * data1;
  char *matx;
  short *xtable, *ytable, xdirection, kk, j, linetotal;
  int i, yinc, matxxsize;
  //板卡中象素深度（4）
  para=pixelflag/8;
  //象素深度
  para1= head->PixelDepth/8;
  //分配一块内存，存放象素转化后的图元
  data=(unsigned char *)winalloc(head->TgaXSize*para);
  //此处不知道怎么回事，为什么要至少分配256×para×2的内存。
  if(head->TgaXSize>256)
	  data1=(unsigned char *)winalloc(head->TgaXSize*para1*2);
  else             
      data1=(unsigned char *)winalloc(256*para1*2);
  //为图元行象素分配映射表内存
  xtable=(short*)winalloc(xsize*2);
  //为图元列象素分配映射表内存
  ytable=(short*)winalloc(ysize*2);
  /// 一张从元图象倒显示的象素二维表
  GetALLScrTable(head->TgaXSize, head->TgaYSize, xsize, ysize, xtable, ytable);
  //?
  matx=matxbuf;
  
  if(head->TgaXSize%8==0)
	  matxxsize=head->TgaXSize/8;
  else                   
	  matxxsize=head->TgaXSize/8+1;
  //得到图元在板卡上的显示地址入口
	tmp_pp	= (char *)pp;
	//判断图元的原点坐标，
  if((head->ImgDescriptor&0x20)==0)
  {
	  	
	   //原点坐标在左下角
	 tdata=(unsigned char *)(&tmp_pp[((unsigned long)ysize-1L)*
											 (unsigned long)para*
											 (unsigned long)scrnx
											 ]);
	 yinc=-para*scrnx;
	 matxxsize=-matxxsize;
	
	 
  }
  else
  {  
	      //原点坐标在左上角
	 tdata=(unsigned char  *)tmp_pp;
	 yinc = para*xsize;
	
//	 yinc=para*scrnx;
  }
  //肯定等于0，这是保留位
  if((head->ImgDescriptor&0x10)==0) 
	  xdirection=_PX;
  else     
	  xdirection=_NX;
  //kk 用来判断是不是进行了格式转换，比如24到32等。
  kk=-1;
  //j 解游码时用到，
  j=0;
  //跳到当前文件的内容入口
  _llseek(handle, offset, SEEK_SET);
  linetotal=0;
  DataBuf=(char*)winalloc(BUF_SIZE);
  BufPoint=-1;
  //循环，直到读完所有的行，
  for(i=0; i<ysize; i++)
  {
	  //是格式10还是2
	 switch(head->It)
	 {
		 //格式10
		case 10:
			if(kk!=ytable[i])
			{
				while(j<=ytable[i])
				{
					//解游码
					ReadTgaCompressLine( handle, data1,
												head->TgaXSize,
												head->PixelDepth,
												&linetotal);
					j++; 
				}
			}
			break;
		//格式2
		case 2:
			if(kk!=ytable[i])
			{
				_llseek(handle, offset+(unsigned long)(
											(unsigned long)head->TgaXSize*
											(unsigned long)ytable[i]*
											(unsigned long)para1 ),
											SEEK_SET);
				wsize=_lread(handle, data1, head->TgaXSize*para1);
				if(wsize	!= head->TgaXSize*para1)
					MessageBeep(0);
			}
			break;
		default:
	break;
	 }
	 //初始化时，mflag = NO;姑在此程序中，这段代码没有执行。
	 if(mflag==YES)
	 {
		if(matxxsize>0)
				matx=matxbuf+ytable[i]*matxxsize;
		else
				matx=matxbuf+((ysize-1)-ytable[i])*(-matxxsize);
	 }
	 //如果原图元的象素深度和板卡上的一致则直接把图元象素写到板卡上即可
	 if(pixelflag==head->PixelDepth)
	 {
		if(kk!=ytable[i]) kk=ytable[i];
		//写到板卡中
		WriteLine(head, data1, tdata, matx, xtable, xsize, protflag,
		 mflag, xdirection, pixelflag, visibleflag);
	 }
	 //	否则进行象素深度的转换
	 else
	 {
		if(kk!=ytable[i])
		{
			//kk得到当前要写的列
			kk=ytable[i];
			if(head->PixelDepth==_16BIT && pixelflag==_32BIT)
				TransData16To32(data1, data, head->TgaXSize);
			if(head->PixelDepth==_32BIT && pixelflag==_16BIT)
				TransData32To16(data1, data, head->TgaXSize);
			if(head->PixelDepth==_24BIT && pixelflag==_16BIT)
				TransData24To16(data1, data, head->TgaXSize);
			if(head->PixelDepth==_24BIT && pixelflag==_32BIT)
				TransData24To32(data1, data, head->TgaXSize);
		}
		WriteLine(head, data, tdata, matx, xtable, xsize, protflag,
		 mflag, xdirection, pixelflag, visibleflag);
	 }
	 //next行，可能是当前行的上一行
	 tdata+=yinc;
  }
  winfree(DataBuf);
  DataBuf=NULL;
  BufPoint=-1;
  winfree(data);
  winfree(data1);
  winfree(xtable);
  winfree(ytable);
  return;

}





