#ifndef _OF_IMAGE_H_
#define _OF_IMAGE_H_

#include "ofConstants.h"
#include "ofTexture.h"
#include "ofGraphics.h"
#include "ofAppRunner.h"		// for height()
#include "FreeImage.h"


#define OF_IMAGE_GRAYSCALE		0x00
#define OF_IMAGE_COLOR			0x01
#define OF_IMAGE_COLOR_ALPHA	0x02
#define OF_IMAGE_UNDEFINED		0x03


void 	ofCloseFreeImage();


class 	ofImage {

	public :
		
		ofImage();
		~ofImage();
		
		ofImage(const ofImage& mom) { 
			
			//----------------------- init free image if necessary
			bmp = NULL;
			//-----------------------
			width = height = bpp = 0;
			type 					= OF_IMAGE_UNDEFINED;
			bUseTexture 			= true;		// the default is, yes, use a texture
			bAllocatedPixels		= false;
			clone(mom); 
			
			printf("-------------------------------------------- 00 \n");
		};
		
		ofImage& ofImage::operator=(const ofImage& mom) {
			clone(mom); 
			printf("-------------------------------------------- 11 \n");
			return *this;
		}
		
		
		void 				loadImage(string fileName);
		void 				saveImage(string fileName);
		
		void 				allocate(int w, int h, int type); 
		void 				clear();
		
		
		
		unsigned char * 	getPixels();			// up to you to get this right	
		void 				setFromPixels(unsigned char * pixels, int w, int h, int newType, bool bOrderIsRGB = true);
		
		void 				setImageType(int type);
		void 				resize(int newWidth, int newHeight);
		void 				grabScreen(int x, int y, int w, int h);		// grab pixels from opengl, using glreadpixels
		
		void 				clone(const ofImage &mom);
		
		int 				type;			// what type of image it is (as above)
		int 				width, height, bpp;		// w,h, bits per pixel
		
		void 				setUseTexture(bool bUse);
		void 				draw(float x, float y, float w, float h);	
		void 				draw(float x, float y);	
		
	protected:	
		
		void 				swapChannels(bool bAlpha);
		void 				update();
		ofTexture 			tex;
		bool				bUseTexture;	
		bool				bAllocatedPixels;
		
		
		FIBITMAP 			* bmp;
		
		// when OF users ask for pixels
		// we will get them contiguous memory
		// ie, non-aligned
		// see the pixel access chapter of the freeImage pdf
		// these pixel will also be RGB
		// on both platforms
		// bmp will be RGB / BGR depending
		// on the endianess of the platform.

		unsigned char 		* imgPixels;
		
		void 				swap(FIBITMAP *dst);
		void 				setSize(FREE_IMAGE_TYPE image_type, WORD width, WORD height, WORD bpp);
		inline bool 		isValid();


		
		
		
}; 



#endif
