int l_Image_Save( lua_State* L )
{
	ImageData* imgData = lua_checkimage( L, 1 );
	const char* fileName = lua_tostring( L, 2 );

	if( !imgData || !fileName )
	{
		lua_pushstring( L, "Wrong parameters to Image_Save functions" );
		lua_error( L );
		return 0;
	}

	LPDIRECT3DTEXTURE9 texture;
	D3DLOCKED_RECT rect;

	D3DFORMAT dxFormat = (D3DFORMAT)imgData->format->d3dformat;

	if( dxFormat == D3DFMT_DXT1 || dxFormat == D3DFMT_DXT3 || dxFormat == D3DFMT_DXT5 )
		dxFormat = D3DFMT_A8R8G8B8;

	g_pd3dDevice->CreateTexture( imgData->width, imgData->height, 1, 0, dxFormat, D3DPOOL_MANAGED, &texture, 0 );
	texture->LockRect(0, &rect, 0, 0 );
	memcpy( rect.pBits, imgData->imgData, imgData->width * imgData->height * imgData->format->bytesPerPixel );
	texture->UnlockRect( 0 );

	if( imgData->format->d3dformat == D3DFMT_DXT1 || imgData->format->d3dformat == D3DFMT_DXT3 || imgData->format->d3dformat == D3DFMT_DXT5 )
	{
		ID3DXBuffer* dxBuffer;
		LPDIRECT3DTEXTURE9 outTexture; 
		D3DXSaveTextureToFileInMemory( &dxBuffer, D3DXIFF_BMP, texture, 0 );
		D3DXCreateTextureFromFileInMemoryEx( g_pd3dDevice, dxBuffer->GetBufferPointer(), dxBuffer->GetBufferSize(), imgData->width, imgData->height, 0, 0, (D3DFORMAT)imgData->format->d3dformat, D3DPOOL_SCRATCH, D3DX_DEFAULT, D3DX_DEFAULT, 0, 0, 0, &outTexture );

		D3DXSaveTextureToFile( fileName, extToFormat(fileName), outTexture, 0 );
		outTexture->Release();
	}else
	{
		D3DXSaveTextureToFile( fileName, extToFormat(fileName), texture, 0 );
	}

	texture->Release();

	return 0;
}
void CRenderLayer::SaveTextureToFileInMemory(void** lpBuffer, int* pBufferSize, TextureHandle th, int iImageFormat)
{
	if (th->is_multisampled)
		throw runtime_error("Cannot save multisampled textures");

	// Talk about a hack...
	LPD3DXBUFFER buff = (LPD3DXBUFFER)(*lpBuffer);

	D3DXSaveTextureToFileInMemory(
		&buff,
		(D3DXIMAGE_FILEFORMAT)iImageFormat,
		th->texture_ptr,
		NULL
	);

	if(pBufferSize)
		*pBufferSize = buff->GetBufferSize();

	*lpBuffer = buff;
}
void CDlgWOWADTImport::OnCbnSelchangeComboArea()
{
	// TODO: 在此添加控件通知处理程序代码
	
	CString AreaFileName;
	int Sel=m_cbArea.GetCurSel();
	m_cbArea.GetLBText(Sel,AreaFileName);
	UpdateData(true);
	int Pos=AreaFileName.Find(' ');
	m_ModelFileName=AreaFileName.Left(Pos);
	CEasyString MiniMapFile=(LPCTSTR)AreaFileName.Right(AreaFileName.GetLength()-Pos-1);
	MiniMapFile=_T("\\")+MiniMapFile;
	MiniMapFile=WOW_MINI_MAP_FILE_DIR+MiniMapFile;	
	CD3DTexture * pMiniMapTexture=m_pD3DDevice->GetTextureManager()->LoadTexture(MiniMapFile);
	if(pMiniMapTexture)
	{
		LPD3DXBUFFER pDataBuffer;
		if(D3DXSaveTextureToFileInMemory(&pDataBuffer,D3DXIFF_BMP,pMiniMapTexture->GetD3DTexture(),NULL)==D3D_OK)
		{
			BITMAPFILEHEADER * pBMPHeader=(BITMAPFILEHEADER *)pDataBuffer->GetBufferPointer();
			BITMAPINFO * pBMI=(BITMAPINFO *)((BYTE *)pDataBuffer->GetBufferPointer()+sizeof(BITMAPFILEHEADER));
			BYTE * pBits=(BYTE *)pDataBuffer->GetBufferPointer()+pBMPHeader->bfOffBits;
			if(m_hBMPMiniMap)
			{
				HBITMAP hOld=m_stMiniMap.SetBitmap(NULL);
				DeleteObject(m_hBMPMiniMap);
				m_hBMPMiniMap=NULL;
			}
			m_hBMPMiniMap=CreateDIBitmap(GetDC()->GetSafeHdc(),&pBMI->bmiHeader,CBM_INIT,pBits,pBMI,DIB_RGB_COLORS);
			if(m_hBMPMiniMap)
			{
				m_stMiniMap.SetBitmap(m_hBMPMiniMap);
				m_stMiniMap.Invalidate();
			}
			SAFE_RELEASE(pDataBuffer);
		}
		SAFE_RELEASE(pMiniMapTexture);
	}
	UpdateData(false);
	
}
void CDlgWOWADTImport::OnBnClickedShowFullMap()
{
	// TODO: 在此添加控件通知处理程序代码
	int Sel=m_cbMap.GetCurSel();
	UINT Index=m_cbMap.GetItemData(Sel);
	CBLZWOWDatabase::BLZ_DB_MAP_INFO * pMapInfo=CBLZWOWDatabase::GetInstance()->GetMapInfo(Index);
	if(pMapInfo)
	{		
		CEasyString WDTFileName;
		WDTFileName.Format(_T("%s\\%s\\%s.wdt"),
			(LPCTSTR)WOW_MAP_ADT_FILE_DIR,(LPCTSTR)pMapInfo->InternalName,(LPCTSTR)pMapInfo->InternalName);
		CBLZWDTFile WDTFile;
		if(WDTFile.Load(WDTFileName))
		{
			
			UINT MinX=63,MinY=63;
			UINT MaxX=0,MaxY=0;
			for(UINT x=0;x<64;x++)
			{
				for(UINT y=0;y<64;y++)
				{
					if(WDTFile.IsAreaExist(x,y))
					{
						if(x<MinX)
							MinX=x;
						if(x>MaxX)
							MaxX=x;
						if(y<MinY)
							MinY=y;
						if(y>MaxY)
							MaxY=y;
					}
				}
			}

			if(m_DlgMapView.GetMapID()!=pMapInfo->ID)
			{
				if(MinX<=MaxX&&MinY<=MaxY)
				{
					CDC DestDC,SrcDC;
					DestDC.CreateCompatibleDC(GetDC());
					SrcDC.CreateCompatibleDC(GetDC());

					CBitmap FullMapBMP;

					UINT MapWidth=(MaxX-MinX+1)*64;
					UINT MapHeight=(MaxY-MinY+1)*64;

					FullMapBMP.CreateBitmap(MapWidth,MapHeight,1,32,NULL);

					int err=GetLastError();

					CBitmap * pOldBMP=DestDC.SelectObject(&FullMapBMP);
					DestDC.SetStretchBltMode(COLORONCOLOR);


					for(UINT x=0;x<64;x++)
					{
						for(UINT y=0;y<64;y++)
						{
							if(WDTFile.IsAreaExist(x,y))
							{
								CEasyString MiniMapFile;
								MiniMapFile.Format(_T("%s\\%s\\map%d_%d.blp"),
									WOW_MINI_MAP_FILE_DIR,
									(LPCTSTR)pMapInfo->InternalName,
									x,y);

								//MiniMapFile.Format("%s\\%s",
								//	WOW_MINI_MAP_FILE_DIR,
								//	CBLZWOWDatabase::GetInstance()->GetMiniMapRealFileName(MiniMapFile));
								CD3DTexture * pMiniMapTexture=m_pD3DDevice->GetTextureManager()->LoadTexture(MiniMapFile);
								if(pMiniMapTexture)
								{
									LPD3DXBUFFER pDataBuffer;
									if(D3DXSaveTextureToFileInMemory(&pDataBuffer,D3DXIFF_BMP,pMiniMapTexture->GetD3DTexture(),NULL)==D3D_OK)
									{
										BITMAPFILEHEADER * pBMPHeader=(BITMAPFILEHEADER *)pDataBuffer->GetBufferPointer();
										BITMAPINFO * pBMI=(BITMAPINFO *)((BYTE *)pDataBuffer->GetBufferPointer()+sizeof(BITMAPFILEHEADER));
										BYTE * pBits=(BYTE *)pDataBuffer->GetBufferPointer()+pBMPHeader->bfOffBits;									
										HBITMAP hBMPMiniMap=CreateDIBitmap(GetDC()->GetSafeHdc(),&pBMI->bmiHeader,CBM_INIT,pBits,pBMI,DIB_RGB_COLORS);
										SrcDC.SelectObject(hBMPMiniMap);
										if(!DestDC.StretchBlt((x-MinX)*64,(y-MinY)*64,
											pBMI->bmiHeader.biWidth/4,pBMI->bmiHeader.biHeight/4,&SrcDC,
											0,0,pBMI->bmiHeader.biWidth,pBMI->bmiHeader.biHeight,SRCCOPY))
										{
											AfxMessageBox(_T("拼接小地图失败"));
										}
										DeleteObject(hBMPMiniMap);
										SAFE_RELEASE(pDataBuffer);
									}
									SAFE_RELEASE(pMiniMapTexture);
								}							
							}
						}
					}
					DestDC.SelectObject(pOldBMP);
					m_DlgMapView.SetMap(pMapInfo->ID,(HBITMAP)FullMapBMP.GetSafeHandle(),MapWidth,MapHeight,MinX,MinY);
					FullMapBMP.Detach();
				}				
			}
			if(m_DlgMapView.DoModal()==IDOK)
			{
				UINT SelectX=MinX+m_DlgMapView.m_SelectedMapX/64;
				UINT SelectY=MinY+m_DlgMapView.m_SelectedMapY/64;
				for(int i=0;i<m_cbArea.GetCount();i++)
				{
					CString AreaFileName;				
					m_cbArea.GetLBText(i,AreaFileName);
					UINT Param=m_cbArea.GetItemData(i);
					UINT x=Param%100;
					UINT y=(Param/100)%100;
					if(SelectX==x&&SelectY==y)
					{
						m_cbArea.SetCurSel(i);
						OnCbnSelchangeComboArea();
						break;
					}
				}		
			}
		}
	}

}