/*
To get this to compile:
Project->Properties->Configuration Properties->Linker->Inputs->Additional Dependencies
Add: gdiplus.lib
*/
#include <windows.h>
#include <iostream>			// For console i/o
#include <gdiplus.h>		// Neede to load the image

using namespace std;		// For i/o
using namespace Gdiplus;	// For loading the image

/*
Must be called at start and end of program! Make sure all bitmaps are
destroyed *before* this is called!

Initializes gdi if init = true, and closes if init = false
*/
void gdiSetup(bool init)
{
	static ULONG_PTR m_gdiplusToken; // Pointer to gdi handle

	if (init)
	{
		// Initialize gdi
		GdiplusStartupInput gdiplusStartupInput;
		GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
	}
	else
	{
		GdiplusShutdown(m_gdiplusToken);
	}
}

// Will load foo.bmp and print its width, height, and the rgb of the top left pixel
void foo()
{
	// This works with a .bmp, as well as other formats (.jpg, etc.) even though it is called a Bitmap
	Bitmap b(L"foo.bmp", false);	// The 'L' converts it to a DWORD. Ignore the false, just put it there

	// Print the width and height
	cout << "Width: " << b.GetWidth() << endl;
	cout << "Height: " << b.GetHeight() << endl;

	// Print the color of the top left pixel
	Color c;
	b.GetPixel(0, 0, &c);
	cout << "Top, left pixel R G B A:" << endl;			// Red, Green, Blue, Alpha
	cout << (int)c.GetR() << '\t' << (int)c.GetG() << '\t' << (int)c.GetB() << '\t' << (int)c.GetAlpha() << endl;
}

void main()
{
	gdiSetup(true);					// Must be called at start

//	Bitmap b("foo.bmp", false);		// BAAAAAD!!! If you do this, b will not be destroyed before the gdiSetup(false) call!

	foo();							// By creating the bitmap in a function, it will fall out of scope before gdiSetup(false)
	
	gdiSetup(false);				// Must be called at end
	
	cout << "Press enter to exit." << endl;
	getchar();						// Don't close - make user press enter to exit
}
