#include "ImageConversion.h"
#include <Windows.h>

namespace DFF_VideoMat
{




ZFloatImage ZImageFunc::LoadFloatImage(int width,int height,const std::string& fileName)
{
	FILE* pFile = fopen(fileName.c_str(),"rb");
	if(pFile)
	{
		ZFloatImage dspMap(width,height);
		fread(dspMap.GetData(),sizeof(BYTE),dspMap.GetDataSize(),pFile);
		fclose(pFile);
		return dspMap;
	}

	return ZFloatImage();
}
ZFloatImage ZImageFunc::LoadFloatImageRange(int width,int height,const std::string fileName,float rangeMin,float rangeMax)
{
	ZFloatImage dspMap = LoadFloatImage(width,height,fileName);
	for(int y = 0;y<dspMap.GetHeight();y++)
	{
		for(int x =0 ;x<dspMap.GetWidth();x++)
		{
			dspMap.At(x,y) = (std::max)(rangeMin,dspMap.At(x,y));
			dspMap.At(x,y) = (std::min)(rangeMax,dspMap.At(x,y));
		}
	}

	return dspMap;
}
bool ZImageFunc::SaveFloatImage(ZFloatImage& floatImg,const std::string& fileName)
{
	FILE* pFile = fopen(fileName.c_str(),"wb+");
	if(pFile)
	{
		fwrite(floatImg.GetData(),sizeof(BYTE),floatImg.GetDataSize(),pFile);
		fclose(pFile);
		return true;
	}
	else
		return false;
}

ZByteImage ZImageFunc::LoadByteImage(int width,int height,const std::string& fileName)
{
	FILE* pFile = fopen(fileName.c_str(),"rb");
	if(pFile)
	{
		ZByteImage dspMap(width,height);
		fread(dspMap.GetData(),sizeof(BYTE),dspMap.GetDataSize(),pFile);
		fclose(pFile);
		return dspMap;
	}

	return ZByteImage();
}
ZByteImage ZImageFunc::LoadByteImageRange(int width,int height,const std::string fileName,int rangeMin,int rangeMax)
{
	ZByteImage dspMap = LoadByteImage(width,height,fileName);
	for(int y = 0;y<dspMap.GetHeight();y++)
	{
		for(int x =0 ;x<dspMap.GetWidth();x++)
		{
			dspMap.At(x,y) = max(rangeMin,dspMap.At(x,y));
			dspMap.At(x,y) = min(rangeMax,dspMap.At(x,y));
		}
	}

	return dspMap;
}
bool ZImageFunc::SaveByteImage(ZByteImage& byteImg,const std::string& fileName)
{
	FILE* pFile = fopen(fileName.c_str(),"wb+");
	if(pFile)
	{
		fwrite(byteImg.GetData(),sizeof(BYTE),byteImg.GetDataSize(),pFile);
		fclose(pFile);
		return true;
	}
	else
		return false;
}



ZByteImage ColorImageFunc::IplImg2ZImg(IplImage* pImg)
{
	if(!pImg)
		return ZByteImage();
	int imgWidth = pImg->width;
	int imgHeight = pImg->height;
	int imgChannel = pImg->nChannels;
	int imgStep = pImg->widthStep/sizeof(uchar);
	uchar* pImgData = (uchar*)pImg->imageData;

	ZByteImage img_Z;
	img_Z.Create(imgWidth,imgHeight,imgChannel);
	for(int y=0;y<imgHeight;y++)
	{
		for(int x=0;x<imgWidth;x++)
		{
			for(int c = 0;c<imgChannel;c++)
			{
				img_Z.At(x,y,c) = pImgData[y*imgStep+x*imgChannel+c];
			}
			
		}
	}

	return img_Z;
}
IplImage* ColorImageFunc::ZImg2IplImg(ZByteImage& img)
{
	if(!img.IsValid())
		return NULL;
	int imgWidth = img.GetWidth();
	int imgHeight = img.GetHeight();
	int imgChannel = img.GetChannel();

	IplImage* img_IPL = cvCreateImage(cvSize(imgWidth,imgHeight),IPL_DEPTH_8U,imgChannel);
	if(!img_IPL)
		return NULL;

	int imgStep = img_IPL->widthStep/sizeof(uchar);
	uchar* pImgData = (uchar*)img_IPL->imageData;

	for(int y=0;y<imgHeight;y++)
	{
		for(int x=0;x<imgWidth;x++)
		{
			for(int c = 0;c<imgChannel;c++)
			{
				pImgData[y*imgStep+x*imgChannel+c] = img.At(x,y,c);
			}

		}
	}

	return img_IPL;
}


ZByteImage ColorImageFunc::LoadColorImg(const std::string& imgFileName)
{
	IplImage* pImg = cvLoadImage(imgFileName.c_str());
	ZByteImage result = IplImg2ZImg(pImg);
	cvReleaseImage(&pImg);

	return result;
}

}