Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// BlurMatrix.cpp: implementation of the CBlurMatrix class.
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//////////////////////////////////////////////////////////////////////
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
#include "Windows.h"
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#ifdef __sgi
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <time.h></time.h>
Toshihiro Shimizu 890ddd
typedef struct timespec {
Toshihiro Shimizu 890ddd
	time_t tv_sec; /* seconds */
Toshihiro Shimizu 890ddd
	long tv_nsec;  /* and nanoseconds */
Toshihiro Shimizu 890ddd
} timespec_t;
Toshihiro Shimizu 890ddd
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#include <vector></vector>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <math.h></math.h>
Toshihiro Shimizu 890ddd
#include <stdlib.h></stdlib.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//#include "SError.h"
Toshihiro Shimizu 890ddd
#include "BlurMatrix.h"
Toshihiro Shimizu 890ddd
//#include "tmsg.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//////////////////////////////////////////////////////////////////////
Toshihiro Shimizu 890ddd
// Construction/Destruction
Toshihiro Shimizu 890ddd
//////////////////////////////////////////////////////////////////////
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
CBlurMatrix::CBlurMatrix(const CBlurMatrix &m) : // throw(SBlurMatrixError) :
Toshihiro Shimizu 890ddd
												 m_isSAC(m.m_isSAC),
Toshihiro Shimizu 890ddd
												 m_isRS(m.m_isRS)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		for (int i = 0; i < NBRS; i++)
Toshihiro Shimizu 890ddd
			m_m[i] = m.m_m[i];
Toshihiro Shimizu 890ddd
	} catch (exception) {
Toshihiro Shimizu 890ddd
		throw SBlurMatrixError();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
CBlurMatrix::CBlurMatrix(const double d, const int nb,
Toshihiro Shimizu 890ddd
						 const bool isSAC, const bool isRS)
Toshihiro Shimizu 890ddd
// throw(SBlurMatrixError)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		m_isRS = isRS;
Toshihiro Shimizu 890ddd
		m_isSAC = isSAC;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (int i = 0; i < NBRS; i++)
Toshihiro Shimizu 890ddd
			m_m[i].clear();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (m_isRS) {
Toshihiro Shimizu 890ddd
			createRandom(d, nb);
Toshihiro Shimizu 890ddd
		} else
Toshihiro Shimizu 890ddd
			createEqual(d, nb);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (m_isSAC)
Toshihiro Shimizu 890ddd
			addPath();
Toshihiro Shimizu 890ddd
	} catch (SBlurMatrixError) {
Toshihiro Shimizu 890ddd
		throw;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool CBlurMatrix::isIn(const vector<blursection> &m, const SXYD &xyd) const</blursection>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	for (vector<blursection>::const_iterator p = m.begin(); p != m.end(); ++p) {</blursection>
Toshihiro Shimizu 890ddd
		vector<sxyd>::const_iterator pp = p->begin();</sxyd>
Toshihiro Shimizu 890ddd
		if (pp->x == xyd.x && pp->y == xyd.y)
Toshihiro Shimizu 890ddd
			return true;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CBlurMatrix::createRandom(const double d, const int nb)
Toshihiro Shimizu 890ddd
//   throw(SBlurMatrixError)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int iNb = 0;
Toshihiro Shimizu 890ddd
	int l = I_ROUNDP(ceil(d));
Toshihiro Shimizu 890ddd
	int ll = 2 * l + 1;
Toshihiro Shimizu 890ddd
	double d2 = d * d;
Toshihiro Shimizu 890ddd
	//	int maxNb=(int)(4.0*d*d*PI/5.0);
Toshihiro Shimizu 890ddd
	int maxNb = (int)((double)(d * d) * 2.8) + 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		int i = 0;
Toshihiro Shimizu 890ddd
		for (i = 0; i < NBRS; i++) {
Toshihiro Shimizu 890ddd
			BLURSECTION bs;
Toshihiro Shimizu 890ddd
			SXYD xyd = {0, 0, 0.0};
Toshihiro Shimizu 890ddd
			bs.push_back(xyd);
Toshihiro Shimizu 890ddd
			m_m[i].push_back(bs);
Toshihiro Shimizu 890ddd
			iNb++;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (nb <= 0 || d <= 0.01)
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (i = 0; i < NBRS; i++) {
Toshihiro Shimizu 890ddd
			iNb = 1;
Toshihiro Shimizu 890ddd
			while (iNb < nb && iNb <= maxNb) {
Toshihiro Shimizu 890ddd
				int x = (rand() % (ll + 1)) - l;
Toshihiro Shimizu 890ddd
				int y = (rand() % (ll + 1)) - l;
Toshihiro Shimizu 890ddd
				if (((double)(x * x + y * y) <= d2) && (x != 0 || y != 0)) {
Toshihiro Shimizu 890ddd
					SXYD xyd1 = {x, y, 0.0};
Toshihiro Shimizu 890ddd
					if (!isIn(m_m[i], xyd1)) {
Toshihiro Shimizu 890ddd
						BLURSECTION bs;
Toshihiro Shimizu 890ddd
						SXYD xyd = {x, y, sqrt((double)(x * x + y * y))};
Toshihiro Shimizu 890ddd
						bs.push_back(xyd);
Toshihiro Shimizu 890ddd
						m_m[i].push_back(bs);
Toshihiro Shimizu 890ddd
						iNb++;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} catch (exception) {
Toshihiro Shimizu 890ddd
		throw SBlurMatrixError();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CBlurMatrix::createEqual(const double d, const int nb)
Toshihiro Shimizu 890ddd
// throw(SBlurMatrixError)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int nq = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			BLURSECTION bs;
Toshihiro Shimizu 890ddd
			SXYD xyd = {0, 0, 0.0};
Toshihiro Shimizu 890ddd
			bs.push_back(xyd);
Toshihiro Shimizu 890ddd
			m_m[0].push_back(bs);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (nb <= 0 || d < 1.0)
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		double b = (double)nb * (2.0 * d + 1.0) * (2.0 * d + 1.0) / (d * d * PI);
Toshihiro Shimizu 890ddd
		b = sqrt(b);
Toshihiro Shimizu 890ddd
		b = ceil(b);
Toshihiro Shimizu 890ddd
		b = (2.0 * d + 1.0) / b;
Toshihiro Shimizu 890ddd
		int di = (int)ceil(d);
Toshihiro Shimizu 890ddd
		double d2 = d * d;
Toshihiro Shimizu 890ddd
		double yd = 0.0;
Toshihiro Shimizu 890ddd
		for (int y = 0; y <= di;) {
Toshihiro Shimizu 890ddd
			double xd = 0.0;
Toshihiro Shimizu 890ddd
			for (int x = 0; x <= di;) {
Toshihiro Shimizu 890ddd
				double dist = sqrt((double)(x * x + y * y));
Toshihiro Shimizu 890ddd
				if (dist <= d) {
Toshihiro Shimizu 890ddd
					{
Toshihiro Shimizu 890ddd
						BLURSECTION bs;
Toshihiro Shimizu 890ddd
						SXYD xyd = {x, y, dist};
Toshihiro Shimizu 890ddd
						bs.push_back(xyd);
Toshihiro Shimizu 890ddd
						m_m[0].push_back(bs);
Toshihiro Shimizu 890ddd
						nq++;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
					if (x > 0 && y > 0) {
Toshihiro Shimizu 890ddd
						BLURSECTION bs;
Toshihiro Shimizu 890ddd
						SXYD xyd = {-x, -y, dist};
Toshihiro Shimizu 890ddd
						bs.push_back(xyd);
Toshihiro Shimizu 890ddd
						m_m[0].push_back(bs);
Toshihiro Shimizu 890ddd
						nq++;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
					if (x > 0 && y >= 0) {
Toshihiro Shimizu 890ddd
						BLURSECTION bs;
Toshihiro Shimizu 890ddd
						SXYD xyd = {-x, y, dist};
Toshihiro Shimizu 890ddd
						bs.push_back(xyd);
Toshihiro Shimizu 890ddd
						m_m[0].push_back(bs);
Toshihiro Shimizu 890ddd
						nq++;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
					if (x >= 0 && y > 0) {
Toshihiro Shimizu 890ddd
						BLURSECTION bs;
Toshihiro Shimizu 890ddd
						SXYD xyd = {x, -y, dist};
Toshihiro Shimizu 890ddd
						bs.push_back(xyd);
Toshihiro Shimizu 890ddd
						m_m[0].push_back(bs);
Toshihiro Shimizu 890ddd
						nq++;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				xd += b;
Toshihiro Shimizu 890ddd
				x = I_ROUND(xd);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			yd += b;
Toshihiro Shimizu 890ddd
			y = I_ROUND(yd);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} catch (exception) {
Toshihiro Shimizu 890ddd
		throw SBlurMatrixError();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CBlurMatrix::addPath(vector<blursection>::iterator pBS)</blursection>
Toshihiro Shimizu 890ddd
//throw(exception)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		if (pBS->size() > 0) {
Toshihiro Shimizu 890ddd
			BLURSECTION::iterator xyd = pBS->begin();
Toshihiro Shimizu 890ddd
			//SXYD* xyd= pBS->begin();
Toshihiro Shimizu 890ddd
			int x = xyd->x;
Toshihiro Shimizu 890ddd
			int y = xyd->y;
Toshihiro Shimizu 890ddd
			int l = max(abs(x), abs(y));
Toshihiro Shimizu 890ddd
			double dx = -(double)x / (double)l;
Toshihiro Shimizu 890ddd
			double dy = -(double)y / (double)l;
Toshihiro Shimizu 890ddd
			double xx = (double)x + dx;
Toshihiro Shimizu 890ddd
			double yy = (double)y + dy;
Toshihiro Shimizu 890ddd
			for (int i = 1; i <= l; i++, xx += dx, yy += dy) {
Toshihiro Shimizu 890ddd
				SXYD xyd1 = {I_ROUND(xx), I_ROUND(yy), 0.0};
Toshihiro Shimizu 890ddd
				pBS->push_back(xyd1);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} catch (exception) {
Toshihiro Shimizu 890ddd
		throw;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CBlurMatrix::addPath() //throw(SBlurMatrixError)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		for (int i = 0; i < (m_isRS ? NBRS : 1); i++) {
Toshihiro Shimizu 890ddd
			for (vector<blursection>::iterator pBS = m_m[i].begin();</blursection>
Toshihiro Shimizu 890ddd
				 pBS != m_m[i].end();
Toshihiro Shimizu 890ddd
				 ++pBS) {
Toshihiro Shimizu 890ddd
				addPath(pBS);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} catch (exception) {
Toshihiro Shimizu 890ddd
		throw SBlurMatrixError();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
CBlurMatrix::~CBlurMatrix()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CBlurMatrix::print() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	/*
Toshihiro Shimizu 890ddd
	char s[1000];
Toshihiro Shimizu 890ddd
	OutputDebugString("  ====== Blur Matrix ======\n");
Toshihiro Shimizu 890ddd
	int nb=m_m[0].size();
Toshihiro Shimizu 890ddd
	for( int i=0; i
Toshihiro Shimizu 890ddd
			 int j=0;
Toshihiro Shimizu 890ddd
			 for( vector<sxyd>::const_iterator pp=m_m[0][i].begin();</sxyd>
Toshihiro Shimizu 890ddd
				  pp!=m_m[0][i].end();
Toshihiro Shimizu 890ddd
				  ++pp,++j ) {
Toshihiro Shimizu 890ddd
				sprintf(s,"(%d,%d,%d,%d) ",i,j,pp->x,pp->y);
Toshihiro Shimizu 890ddd
				OutputDebugString(s);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			OutputDebugString("\n");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	OutputDebugString("  ========================\n");
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
}