#include "GDIPlus\\Includes\\gdiplus.h"

// /DELAYLOADING
#pragma comment(linker, "/delayload:GdiPlus.dll")

void ToW(const char *string, WCHAR *dest)			{
	MultiByteToWideChar( CP_ACP, 0, string, -1, dest, 260 );
}

int Real_GDIPLUS_DrawImage (const char *fichier, int *dx, int *dy)
{
	WCHAR wfichier[1024];
	using namespace Gdiplus;

	ToW(fichier, wfichier);
//	mbstowcs( wfichier, fichier, strlen(fichier));
	Image image(wfichier);

	*dx = image.GetWidth();
	*dy = image.GetHeight();
	if (*dx==0 || *dy==0)
		return 0;
	
	hbmConv = CreateCompatibleBitmap(hdcImage2, *dx, *dy);
	SelectObject(hdcConv, hbmConv);

	RedimDIB(*dx,*dy,TRUE);
	Graphics graphics(hdcConv);
	graphics.DrawImage(&image, 0, 0, *dx, *dy);
	return 1;
}

int GDIPLUS_DrawImage (const char *fichier, int *dx, int *dy)			{
	using namespace Gdiplus;
	GdiplusStartupInput gdiplusStartupInput;
	Gdiplus::ULONG_PTR  gdiplusToken;

	//   hInstGdiPlus = LoadLibrary("gdiplus.dll");
	// Initialize GDI+.
	if (!bGdiPlusDemarre)
		GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
	bGdiPlusDemarre = TRUE;
	return Real_GDIPLUS_DrawImage (fichier, dx, dy);
}

void InitGDIPlus()		{
	using namespace Gdiplus;
	GdiplusStartupInput gdiplusStartupInput;
	Gdiplus::ULONG_PTR  gdiplusToken;
	// Initialize GDI+.
	if (!bGdiPlusDemarre)
		GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
	bGdiPlusDemarre = TRUE;
}

int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
   UINT  num = 0;          // number of image encoders
   UINT  size = 0;         // size of the image encoder array in bytes
   using namespace Gdiplus;

   ImageCodecInfo* pImageCodecInfo = NULL;

   GetImageEncodersSize(&num, &size);
   if(size == 0)
      return -1;  // Failure

   pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
   if(pImageCodecInfo == NULL)
      return -1;  // Failure

   GetImageEncoders(num, size, pImageCodecInfo);

   for(UINT j = 0; j < num; ++j)
   {
      if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
      {
         *pClsid = pImageCodecInfo[j].Clsid;
         free(pImageCodecInfo);
         return j;  // Success
      }    
   }

   free(pImageCodecInfo);
   return -1;  // Failure
}

void GDIPLUS_SaveToDisk(HDC hdc2, int x, int y, char *nom_fichier)			{
	using namespace Gdiplus;
	InitGDIPlus();
	char *extension;
	WCHAR wfichier[1024];
	int redid = 0;

redo:
	ToW(nom_fichier, wfichier);

	HPALETTE hPalette = (HPALETTE)GetCurrentObject( hdcImage2, OBJ_PAL) ;
	Bitmap *myBitMap = new Bitmap(hbm2, hPalette ) ;
	CLSID   encoderClsid;

	extension = GetExtension(nom_fichier);
	if (!strcmp(extension, "png"))
		GetEncoderClsid(L"image/png", &encoderClsid);
	else if (!strcmp(extension, "jpg") || !strcmp(extension, "jpeg"))
		GetEncoderClsid(L"image/jpeg", &encoderClsid);
	else if (!strcmp(extension, "bmp"))
		GetEncoderClsid(L"image/bmp", &encoderClsid);
	else if (!strcmp(extension, "gif"))
		GetEncoderClsid(L"image/gif", &encoderClsid);
	else if (!strcmp(extension, "tiff"))
		GetEncoderClsid(L"image/tiff", &encoderClsid);
	else		{
		//Défaut: bmp
		strcat(nom_fichier, ".bmp");
		GetEncoderClsid(L"image/bmp", &encoderClsid);
		if (redid)
			return;
		redid=1;
		goto redo;
	}
	myBitMap->Save(wfichier, &encoderClsid) ;
}

