#include "WVFrame.h"


VOID WVFFree(LPWVFRAME lpWvf)
{
	if(lpWvf->lpFrame != NULL)
	{
		HeapFree(g_hProcHeap, 0, lpWvf->lpFrame);
		lpWvf->lpFrame		= NULL;
	}
	lpWvf->uFrameSize	= 0;	
	lpWvf->uBufSize		= 0;
	lpWvf->iCompression	= 0;	
}

BOOL WVFAlloc(LPWVFRAME lpWvf, ULONG cSize)
{
	if(lpWvf == NULL)
		return FALSE;

	if(lpWvf->uBufSize < cSize)
	{
		if(lpWvf->lpFrame != NULL)		
			DefFree(lpWvf->lpFrame);

		lpWvf->lpFrame		= (LPBYTE)DefAlloc(cSize);
		lpWvf->uBufSize		= cSize;		
	}
	lpWvf->uFrameSize = cSize;
	return TRUE;
}

INT WVFHeaderPack(LPWVFRAME lpWvf, LPBYTE lpBuffer, ULONG cSize)
{
	if(cSize < (4*sizeof(INT)))
		return 0;

	*((LPINT)lpBuffer) = htonl(lpWvf->iWidth);
	lpBuffer += sizeof(INT);
	*((LPINT)lpBuffer) = htonl(lpWvf->iHeight);
	lpBuffer += sizeof(INT);
	*((LPINT)lpBuffer) = htonl(lpWvf->iCompression);
	lpBuffer += sizeof(INT);
	*((LPINT)lpBuffer) = htonl(lpWvf->uFrameSize);

	return (4*sizeof(INT));
}

INT WVFHeaderUnpack(LPWVFRAME lpWvf, LPBYTE lpBuffer, ULONG cSize)
{
	if(cSize < (4*sizeof(INT)))
		return 0;

	lpWvf->iWidth = ntohl(*((LPINT)lpBuffer));
	lpBuffer += sizeof(INT);
	lpWvf->iHeight = ntohl(*((LPINT)lpBuffer));
	lpBuffer += sizeof(INT);
	lpWvf->iCompression = ntohl(*((LPINT)lpBuffer));
	lpBuffer += sizeof(INT);
	lpWvf->uFrameSize = ntohl(*((LPINT)lpBuffer));
	return (4*sizeof(INT));
}

BOOL WVFCopy(LPWVFRAME lpDest, LPWVFRAME lpSrc)
{
	if(lpDest == NULL || lpSrc == NULL)
		return FALSE;

	if(lpDest->uBufSize < lpSrc->uFrameSize)
	{
		if(lpDest->lpFrame != NULL)		
			DefFree(lpDest->lpFrame);

		lpDest->lpFrame		= (LPBYTE)DefAlloc(lpSrc->uFrameSize);
		lpDest->uBufSize	= lpSrc->uFrameSize;
	}

	CopyMemory(lpDest->lpFrame, lpSrc->lpFrame, lpSrc->uFrameSize);
	
	lpDest->uFrameSize		= lpSrc->uFrameSize;
	lpDest->iCompression	= lpSrc->iCompression;
	lpDest->iWidth			= lpSrc->iWidth;
	lpDest->iHeight			= lpSrc->iHeight;
	return TRUE;
}

