#include "ofCvColorAlphaImage.h"
#include "ofCvGrayscaleImage.h"
#include "ofCvColorImage.h"

//-------------------------------------------------------------------------------------
ofCvColorAlphaImage::ofCvColorAlphaImage(){
	width = height = 0;;
}

//-------------------------------------------------------------------------------------
void ofCvColorAlphaImage::allocate(int _w, int _h){
	cvImage 			= cvCreateImage(cvSize(_w,_h), IPL_DEPTH_8U,4);
	cvImageTemp			= cvCreateImage(cvSize(_w,_h), IPL_DEPTH_8U,4);
	pixels 				= new unsigned char[_w*_h * 4];
	width 				= _w;
	height 				= _h;
	tex.allocate(width, height, GL_RGBA);
}

//-------------------------------------------------------------------------------------
void ofCvColorAlphaImage::clear(){
	cvReleaseImage(&cvImage);
	cvReleaseImage(&cvImageTemp);
	width = height = 0;;
}

//-------------------------------------------------------------------------------------
void ofCvColorAlphaImage::setFromPixels(unsigned char * _pixels, int w, int h){
	cvSetImageData(cvImage,  _pixels, w*4);
	for (int i = 0; i < h; i++){
		memcpy(pixels + (i * w * 4), cvImage->imageData + (i * cvImage->widthStep), w*3);
	}
}

//-------------------------------------------------------------------------------------
unsigned char * ofCvColorAlphaImage::getPixels(){
	// copy each line of pixels:
	for (int i = 0; i < height; i++){
		memcpy(pixels + (i * width * 4), cvImage->imageData + (i * cvImage->widthStep), width*4);
	}
	return pixels;
}
//-------------------------------------------------------------------------------------
void ofCvColorAlphaImage::swapTemp(){
	IplImage * temp;
	temp 			= cvImage;
	cvImage 		= cvImageTemp;
	cvImageTemp 	= temp;
}
//-------------------------------------------------------------------------------------
void ofCvColorAlphaImage::draw(float x, float y){

	// note, this is a bit ineficient, as we have to
	// copy the data out of the cvImage into the pixel array
	// and then upload to texture.  We should add
	// to the texture class an override for pixelstorei
	// that allows stepped-width image upload:

	tex.loadData(getPixels(), width, height, GL_RGBA);
	tex.draw(x,y,width, height);
}
//-------------------------------------------------------------------------------------
//void ofCvColorAlphaImage::set(unsigned char value [4])
//{
//	//cvSet(cvImage, cvScalar(value));
//}
//-------------------------------------------------------------------------------------
void ofCvColorAlphaImage::set(ofCvColorImage mom, ofCvGrayscaleImage momsAlpha)
{
	//cvConvertScale( mom.getCvImage(), cvImage, 1, 0 );
	//cvSetImageCOI( 	momsAlpha.getCvImage(), 3 );
}
//-------------------------------------------------------------------------------------
void ofCvColorAlphaImage::setAlpha(ofCvGrayscaleImage momsAlpha)
{
	//cvMerge( momsAlpha.getCvImage(), momsAlpha.getCvImage(),
   //          momsAlpha.getCvImage(), momsAlpha.getCvImage(), cvImage );//
   //swapTemp();
   
   // cvSetImageCOI( 	cvImage, 3 );
	//cvRepeat( momsAlpha.getCvImage(), cvImage );//cvConvertScale( momsAlpha.getCvImage(), cvImage );
	//cvCopy( cvImageTemp, cvImage );
	//cvSetImageCOI( cvImage, 0 );
}
//-------------------------------------------------------------------------------------
void ofCvColorAlphaImage::copyWithMask(ofCvColorAlphaImage mom, ofCvGrayscaleImage momsMask)
{
			
}
//-------------------------------------------------------------------------------------
void ofCvColorAlphaImage::copyChannelTo(int channel, ofCvGrayscaleImage mom)
{
	//int cvGetImageCOI( const IplImage* image );
}
//-------------------------------------------------------------------------------------
void ofCvColorAlphaImage::operator =( const ofCvColorAlphaImage & mom )
{

}
//------------------------------------------------------------------------------------
void ofCvColorAlphaImage::operator =( ofCvGrayscaleImage & mom )
{

}
//-------------------------------------------------------------------------------------
void ofCvColorAlphaImage::operator =( ofCvColorImage & mom )
{

}
//-------------------------------------------------------------------------------------
void ofCvColorAlphaImage::scale(ofCvColorAlphaImage & mom)
{

}
//-------------------------------------------------------------------------------------
void ofCvColorAlphaImage::blur()
{

}	
		