#include <common/bmdata.hpp>

void BitmapData::cleanupData(void)
{
	if(mhGlobalBitmapData&&Delete==mDataDisposition)
	{
		::GlobalUnlock(mhGlobalBitmapData);
		::GlobalFree(mhGlobalBitmapData);
	}
	mhGlobalBitmapData=0;
  mlpBitmapData=0;
}

void BitmapData::cleanupInfo(void)
{
	if(mhGlobalBitmapInfo&&Delete==mInfoDisposition)
	{
    ::GlobalUnlock(mhGlobalBitmapInfo);
		::GlobalFree(mhGlobalBitmapInfo);
	}
	mhGlobalBitmapInfo=0;
  mlpBitmapInfo=0;
}

UHUGE *BitmapData::reserveData(LONG sizeData)
{
	cleanupData();
	if(!sizeData)return 0; 
	mhGlobalBitmapData=::GlobalAlloc(GMEM_FIXED,sizeData);
	if(!mhGlobalBitmapData)return 0;
	mlpBitmapData=(UHUGE*)::GlobalLock(mhGlobalBitmapData);
  mDataDisposition=Delete;
  return mlpBitmapData;
}

BITMAPINFO *BitmapData::reserveInfo(LONG sizeInfo,WORD numColors)
{
	cleanupInfo();
	if(!sizeInfo)mhGlobalBitmapInfo=::GlobalAlloc(GMEM_FIXED,sizeof(BITMAPINFO)+(sizeof(RGBQUAD)*(numColors-1)));
  else mhGlobalBitmapInfo=::GlobalAlloc(GMEM_FIXED,sizeInfo); 
  if(!mhGlobalBitmapInfo)return 0;
	mlpBitmapInfo=(BITMAPINFO FAR*)::GlobalLock(mhGlobalBitmapInfo);
	mInfoDisposition=Delete;
  return mlpBitmapInfo;
}

WORD BitmapData::setData(UHUGE *lpBitmapData)
{
	cleanupData();
	if(!lpBitmapData)return FALSE;
	mlpBitmapData=lpBitmapData;
	mDataDisposition=Keep;
  return TRUE;
}

WORD BitmapData::setData(HGLOBAL hGlobalData,UHUGE *lpBitmapData)
{
	cleanupData();
	if(!hGlobalData||!lpBitmapData)return 0;
	mlpBitmapData=lpBitmapData;
  mhGlobalBitmapData=hGlobalData;
	mDataDisposition=Delete;
  return TRUE;
}

WORD BitmapData::setInfo(BITMAPINFO FAR*lpBitmapInfo)
{
	cleanupInfo();
	if(!lpBitmapInfo)return FALSE;
	mlpBitmapInfo=lpBitmapInfo;
	mInfoDisposition=Keep;
  return TRUE;
}

