
#include "StdAfx.h"
#include "DMGHL.h"
#include "DMGHLi.h"

BOOL APIENTRY DllMain(
	HANDLE hModule,
	DWORD nReason,
	PVOID pvReserved
	)
{
	hModule;
	nReason;
	pvReserved;
	return TRUE;
}

HDMGHLF DMGHLAPI dmghl_open_graphics_dat(
	const char *pszFileIn,
	int nDMDFT,
	int nIsLittleEndian
)
{
	DMGHLiF *pImpl = NULL;
	switch (nDMDFT) {
	case DMDFT_DMCSB1: pImpl = new DMGHLiFDMCSB1(); break;
	case DMDFT_DMCSB2: pImpl = new DMGHLiFDMCSB2(); break;
	case DMDFT_DM2_CLASSIC: pImpl = new DMGHLiFDM2(true); break;
	case DMDFT_DM2_EVOLVED: pImpl = new DMGHLiFDM2(false); break;
	}
	if (pImpl != NULL) {
		if (pImpl->OpenRead(pszFileIn, nIsLittleEndian ? true : false)) {
			return reinterpret_cast<HDMGHLF>(pImpl);
		}
		delete pImpl;
	}
	return NULL;
}

int DMGHLAPI dmghl_close_graphics_dat(
	HDMGHLF hFile
)
{
	if (hFile != NULL) {
		DMGHLiF *pImpl = reinterpret_cast<DMGHLiF *>(hFile);
		pImpl->Close();
		delete pImpl;
		return TRUE;
	}
	return FALSE;
}

int DMGHLAPI dmghl_get_image(
	HDMGHLF hFile,
	int iBinIdx,
	PDMGHLIMGF pImpf
)
{
	if (hFile != NULL && pImpf != NULL) {
		DMGHLiF *pImpl = reinterpret_cast<DMGHLiF *>(hFile);
		{
			DMGHLiIMG4 img4;
			if (pImpl->ReadImg4(iBinIdx, img4)) {
				if (DMGHLiSetUp(img4, pImpf)) {
					return TRUE;
				}
			}
		}
		{
			DMGHLiIMG8 img8;
			if (pImpl->ReadImg8(iBinIdx, img8)) {
				if (DMGHLiSetUp(img8, pImpf)) {
					return TRUE;
				}
			}
		}
	}
	return FALSE;
}

int DMGHLAPI dmghl_release_image(
	PDMGHLIMGF pImgf
)
{
	if (pImgf) {
		free(pImgf->pixels); pImgf->pixels = NULL;
	}
	return FALSE;
}
