Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//#include "trop.h"
Toshihiro Shimizu 890ddd
#include "tpalette.h"
Toshihiro Shimizu 890ddd
#include "tropcm.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline double gauss(double x, double y, double x0, double y0, double s)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return exp(-((x - x0) * (x - x0) + (y - y0) * (y - y0)) / s) / (s * TConsts::pi);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
double integ_gauss(double xmin, double ymin, double xmax, double ymax,
Toshihiro Shimizu 890ddd
				   double x0, double y0, double s)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i, j, n;
Toshihiro Shimizu 890ddd
	double x1, y1, x2, y2, xstep, ystep, area;
Toshihiro Shimizu 890ddd
	double sum;
Toshihiro Shimizu 890ddd
	n = 50;
Toshihiro Shimizu 890ddd
	sum = 0;
Toshihiro Shimizu 890ddd
	xstep = (xmax - xmin) / n;
Toshihiro Shimizu 890ddd
	ystep = (ymax - ymin) / n;
Toshihiro Shimizu 890ddd
	area = xstep * ystep;
Toshihiro Shimizu 890ddd
	for (i = 0; i < n; i++)
Toshihiro Shimizu 890ddd
		for (j = 0; j < n; j++) {
Toshihiro Shimizu 890ddd
			x1 = xmin + xstep * i;
Toshihiro Shimizu 890ddd
			y1 = ymin + ystep * i;
Toshihiro Shimizu 890ddd
			x2 = x1 + xstep;
Toshihiro Shimizu 890ddd
			y2 = y1 + ystep;
Toshihiro Shimizu 890ddd
			sum += area * (gauss(x1, y1, x0, y0, s) +
Toshihiro Shimizu 890ddd
						   gauss(x1, y2, x0, y0, s) +
Toshihiro Shimizu 890ddd
						   gauss(x2, y2, x0, y0, s) +
Toshihiro Shimizu 890ddd
						   gauss(x2, y1, x0, y0, s)) /
Toshihiro Shimizu 890ddd
				   4;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	return sum;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void build_filter(double h[], double x0, double y0, double s)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double sum, vv;
Toshihiro Shimizu 890ddd
	int i, j, k;
Toshihiro Shimizu 890ddd
	double x1, y1, x2, y2;
Toshihiro Shimizu 890ddd
	k = 0;
Toshihiro Shimizu 890ddd
	sum = 0.0;
Toshihiro Shimizu 890ddd
	for (i = 0; i < 3; i++)
Toshihiro Shimizu 890ddd
		for (j = 0; j < 3; j++) {
Toshihiro Shimizu 890ddd
			x1 = -1.5 + j;
Toshihiro Shimizu 890ddd
			y1 = -1.5 + i;
Toshihiro Shimizu 890ddd
			x2 = x1 + 1.0;
Toshihiro Shimizu 890ddd
			y2 = y1 + 1.0;
Toshihiro Shimizu 890ddd
			h[k++] = vv = integ_gauss(x1, y1, x2, y2, x0, y0, s);
Toshihiro Shimizu 890ddd
			sum += vv;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	if (sum > 0.0) {
Toshihiro Shimizu 890ddd
		sum = 1.0 / sum;
Toshihiro Shimizu 890ddd
		for (k = 0; k < 9; k++)
Toshihiro Shimizu 890ddd
			h[k] *= sum;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // anonymous namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::fracmove(TRasterP rout, TRasterP rin, double dx, double dy)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//Using a bilinear filter is best - consider that only 4 pixels should contribute
Toshihiro Shimizu 890ddd
	//to a fractionarily shifted one, with weights proportional to the intersection areas.
Toshihiro Shimizu 890ddd
	double w[4] = {1, 0, 0, 0};
Toshihiro Shimizu 890ddd
	double sum = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int idx = tfloor(dx);
Toshihiro Shimizu 890ddd
	int idy = tfloor(dy);
Toshihiro Shimizu 890ddd
	double fracX = dx - idx;
Toshihiro Shimizu 890ddd
	double fracY = dy - idy;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int i, j;
Toshihiro Shimizu 890ddd
	for (i = 0; i < 2; ++i)
Toshihiro Shimizu 890ddd
		for (j = 0; j < 2; ++j)
Toshihiro Shimizu 890ddd
			sum += w[j + 2 * i] = fabs(fracX - j) * fabs(fracY - i);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < 4; ++i)
Toshihiro Shimizu 890ddd
		w[i] /= sum;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRop::convolve_i(rout, rin, idx, idy, w, 2);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::fracmove(TRasterP rout, TRasterCM32P rin, const TPaletteP &palette, double dx, double dy)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double w[4] = {1, 0, 0, 0};
Toshihiro Shimizu 890ddd
	double sum = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int idx = tfloor(dx);
Toshihiro Shimizu 890ddd
	int idy = tfloor(dy);
Toshihiro Shimizu 890ddd
	double fracX = dx - idx;
Toshihiro Shimizu 890ddd
	double fracY = dy - idy;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int i, j;
Toshihiro Shimizu 890ddd
	for (i = 0; i < 2; ++i)
Toshihiro Shimizu 890ddd
		for (j = 0; j < 2; ++j)
Toshihiro Shimizu 890ddd
			sum += w[j + 2 * i] = fabs(fracX - j) * fabs(fracY - i);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < 4; ++i)
Toshihiro Shimizu 890ddd
		w[i] /= sum;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRop::convolve_i(rout, rin, idx, idy, w, 2);
Toshihiro Shimizu 890ddd
}