#ifndef VEXIMAGE
#define VEXIMAGE

#include "UT/UT_String.h"
#include "FreeImage.h"
#include "UT/UT_Vector4.h"
#include <limits>

/*
 * The VexImage can be used within a houdini vex context to sample color information
 */

// Some predefines to for image type conversion
#define VEX_IMG_08_UNSIGNED (float)UCHAR_MAX
#define VEX_IMG_16_UNSIGNED (float)USHRT_MAX
#define VEX_IMG_16_SIGNED   (float)SHRT_MAX
#define VEX_IMG_32_UNSIGNED (float)ULONG_MAX
#define VEX_IMG_32_SIGNED	(float)LONG_MAX

class VexImage
{
public:
	VexImage(UT_String inImageName);
	~VexImage();

	// Name of the loaded image
	UT_String mImageName;

	// If the image loaded successfully
	bool mInitialized;

	// Some image info settings
	unsigned			mWidth;		//< Width in pixels
	unsigned			mHeight;	//< Height in pixels
	unsigned			mBitPP;		//< Total amount of bits pp
	unsigned			mBytesPP;	//< Amount of bytes pp
	FREE_IMAGE_FORMAT	mFormat;	//< The image format
	FREE_IMAGE_TYPE		mType;		//< Image type (bitmap, complex, float etc.)

	//< Returns a color value based on an image path, uv location and sample mode
	UT_Vector4	GetColorValue(const float& inU, const float& inV, const int& inMode, int* IOsuccess);

	// Error color / value when sampling isn't successful
	static UT_Vector4 sErrorValue;

private:
	// The loaded bitmap
	FIBITMAP*	mLoadedBitmap;

	// Clears image associated data
	void		ClearImageData();

	// Loads the bitmap in to memory
	FIBITMAP*	LoadBitmap(const UT_String& inImageName);

	// Assigns the value of a pixel in float(RGBA) to outValues, based on the data specified
	static void GetPixelValues(const int& inX, const int& inY, FIBITMAP* inBitmap, float* outValues);

	// Samples a pixel value based on uv coordinate using bilinear sampling
	static void	SampleBilinear(const float& inU, const float& inV, const unsigned& inWidth, const unsigned& inHeight, FIBITMAP* inBitmap, float* outValues);
};

#endif