// MyImage.cpp: implementation of the MyImage class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MyImage.h"
#include <math.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
MyImage::MyImage() 
	: CImage()
{	
	m_width  = m_col;  
	m_height = m_row; 
	m_xBase  = 0; 
	m_yBase  = 0; 
	m_xMax   = m_width -1; 
	m_yMax   = m_height-1;
}

MyImage::MyImage(int row,int col,int color,int type,int format) 
	: CImage(row,col,color,type,format) 
{	
	m_width  = m_col;  
	m_height = m_row; 
	m_xBase  = 0; 
	m_yBase  = 0; 
	m_xMax   = m_width -1; 
	m_yMax   = m_height-1;
}

MyImage::MyImage(const MyImage* im, int color) 
	: CImage(im->m_row,im->m_col,color,im->m_type,im->m_format)
{	
	m_width  = m_col;  
	m_height = m_row; 
	m_xBase  = 0; 
	m_yBase  = 0; 
	m_xMax   = m_width -1; 
	m_yMax   = m_height-1;

	int x,y;
	if (m_color==COLOR && im->m_color==COLOR) {
		for (x=m_xBase; x<=m_xMax; x++) for (y=m_yBase; y<=m_yMax; y++) {
			m_data[y][3*x  ] = im->m_data[y][3*x  ];
			m_data[y][3*x+1] = im->m_data[y][3*x+1];
			m_data[y][3*x+2] = im->m_data[y][3*x+2];
		}
	}
	else if (m_color==COLOR && im->m_color==GRAY) {
		for (x=m_xBase; x<=m_xMax; x++) for (y=m_yBase; y<=m_yMax; y++) {
			m_data[y][3*x  ] = im->m_data[y][x];
			m_data[y][3*x+1] = im->m_data[y][x];
			m_data[y][3*x+2] = im->m_data[y][x];
		}
	}
	else if (m_color==GRAY && im->m_color==COLOR) {
		for (x=m_xBase; x<=m_xMax; x++) for (y=m_yBase; y<=m_yMax; y++) {
			m_data[y][x] = (im->m_data[y][3*x  ]+
							im->m_data[y][3*x+1]+
							im->m_data[y][3*x+2])/((float) 3.0);
		}
	}
	else if (m_color==GRAY && im->m_color==GRAY) {
		for (x=m_xBase; x<=m_xMax; x++) for (y=m_yBase; y<=m_yMax; y++) {
			m_data[y][x] = im->m_data[y][x];
		}
	}
}

MyImage::~MyImage()
{}

float MyImage::dI(CPoint p1, CPoint p2)
{
	if (m_color==COLOR) 
		return (float) sq(m_data[p1.y][3*p1.x  ]-m_data[p2.y][3*p2.x  ])+
					   sq(m_data[p1.y][3*p1.x+1]-m_data[p2.y][3*p2.x+1])+
					   sq(m_data[p1.y][3*p1.x+2]-m_data[p2.y][3*p2.x+2]);
	else if (m_color==GRAY)
		return (float) sq(m_data[p1.y][p1.x]-m_data[p2.y][p2.x]);
	else 
		return (float) 1.0e30;
}

int MyImage::ReadMyImage(char *filename)
{	
	int output = (this->CImage::ReadImage(filename));
	m_width  = m_col;  
	m_height = m_row; 
	m_xBase  = 0; 
	m_yBase  = 0; 
	m_xMax   = m_width -1; 
	m_yMax   = m_height-1;
	return output;
}

MyImage * MyImage::Gradient(float sigma)
{
	MyImage * tmp = new MyImage(this);
	CImage * dIx=first_derivative_x(sigma);
	CImage * dIy=first_derivative_y(sigma);

	CPoint p;
	for (p.y=0; p.y<m_height;  p.y++)
	for (p.x=0; p.x<m_width;   p.x++)
		imRef_GRAY(tmp,p)=sqrt((imRef_GRAY(dIx,p)*imRef_GRAY(dIx,p))+(imRef_GRAY(dIy,p)*imRef_GRAY(dIy,p)));
	tmp->Normalize(0.0,1.0);
	delete dIx;
	delete dIy;
	return tmp;
}





