Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tpixel.h"
Toshihiro Shimizu 890ddd
#include <stdio.h></stdio.h>
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
#include <stdlib.h></stdlib.h>
Toshihiro Shimizu 890ddd
#include <math.h></math.h>
Toshihiro Shimizu 890ddd
#include <assert.h></assert.h>
Toshihiro Shimizu 890ddd
#include "tpixelutils.h"
Toshihiro Shimizu 890ddd
#include "trop.h"
Toshihiro Shimizu 890ddd
#include "stdfx.h"
Toshihiro Shimizu 890ddd
#include "tgeometry.h"
Toshihiro Shimizu 890ddd
#include "tfxattributes.h"
Toshihiro Shimizu 890ddd
#include "tparamuiconcept.h"
Toshihiro Shimizu 890ddd
//#include "timage_io.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
template<class t=""></class>
Toshihiro Shimizu 890ddd
void overlayPixels(const T& dw, const T& up, T& ris, double glob)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
int value;
Toshihiro Shimizu 890ddd
double aux;
Toshihiro Shimizu 890ddd
int max = T::maxChannelValue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
if (up.m == 0)  
Toshihiro Shimizu 890ddd
  ris = dw;
Toshihiro Shimizu 890ddd
else if ((up.m == max)&&(glob==1))      
Toshihiro Shimizu 890ddd
  ris = up;
Toshihiro Shimizu 890ddd
else if (glob==1)
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
  aux = (max-up.m)/max;
Toshihiro Shimizu 890ddd
  value = up.r + troundp(dw.r * aux);
Toshihiro Shimizu 890ddd
  ris.r = (value 
Toshihiro Shimizu 890ddd
  value = up.g + troundp(dw.g * aux);
Toshihiro Shimizu 890ddd
  ris.g = (value 
Toshihiro Shimizu 890ddd
  value = up.b + troundp(dw.b * aux);
Toshihiro Shimizu 890ddd
  ris.b = (value 
Toshihiro Shimizu 890ddd
  value = up.m + dw.m;
Toshihiro Shimizu 890ddd
  ris.m = (value 
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
else
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
  aux = (max-up.m*glob)/max;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  value = (int)(up.r*glob) + troundp(dw.r * aux);
Toshihiro Shimizu 890ddd
  ris.r = (value 
Toshihiro Shimizu 890ddd
  value = (int)(up.g*glob) + troundp(dw.g * aux);
Toshihiro Shimizu 890ddd
  ris.g = (value 
Toshihiro Shimizu 890ddd
  value = (int)(up.b*glob) + troundp(dw.b * aux);
Toshihiro Shimizu 890ddd
  ris.b = (value 
Toshihiro Shimizu 890ddd
  value = (int)(up.m*glob) + dw.m;
Toshihiro Shimizu 890ddd
  ris.m = (value 
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
inline void blur_code(
Toshihiro Shimizu 890ddd
	T *row1, T *row2, int length, double coeff, double coeffq, int brad, double diff, double globmatte)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	double rsum, gsum, bsum, msum;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPixelD sigma1, sigma2, sigma3, desigma;
Toshihiro Shimizu 890ddd
	T *pix1, *pix2, *pix3, *pix4;
Toshihiro Shimizu 890ddd
	int max = T::maxChannelValue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	pix1 = row1;
Toshihiro Shimizu 890ddd
	pix2 = row1 - 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	sigma1.r = pix1->r;
Toshihiro Shimizu 890ddd
	sigma1.g = pix1->g;
Toshihiro Shimizu 890ddd
	sigma1.b = pix1->b;
Toshihiro Shimizu 890ddd
	sigma1.m = pix1->m;
Toshihiro Shimizu 890ddd
	pix1++;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	sigma2.r = sigma2.g = sigma2.b = sigma2.m = 0.0;
Toshihiro Shimizu 890ddd
	sigma3.r = sigma3.g = sigma3.b = sigma3.m = 0.0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 1; i < brad; i++) {
Toshihiro Shimizu 890ddd
		sigma1.r += pix1->r;
Toshihiro Shimizu 890ddd
		sigma1.g += pix1->g;
Toshihiro Shimizu 890ddd
		sigma1.b += pix1->b;
Toshihiro Shimizu 890ddd
		sigma1.m += pix1->m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		sigma2.r += pix2->r;
Toshihiro Shimizu 890ddd
		sigma2.g += pix2->g;
Toshihiro Shimizu 890ddd
		sigma2.b += pix2->b;
Toshihiro Shimizu 890ddd
		sigma2.m += pix2->m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		sigma3.r += i * (pix1->r + pix2->r);
Toshihiro Shimizu 890ddd
		sigma3.g += i * (pix1->g + pix2->g);
Toshihiro Shimizu 890ddd
		sigma3.b += i * (pix1->b + pix2->b);
Toshihiro Shimizu 890ddd
		sigma3.m += i * (pix1->m + pix2->m);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		pix1++;
Toshihiro Shimizu 890ddd
		pix2--;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	rsum = (sigma1.r + sigma2.r) * coeff - sigma3.r * coeffq + 0.5;
Toshihiro Shimizu 890ddd
	gsum = (sigma1.g + sigma2.g) * coeff - sigma3.g * coeffq + 0.5;
Toshihiro Shimizu 890ddd
	bsum = (sigma1.b + sigma2.b) * coeff - sigma3.b * coeffq + 0.5;
Toshihiro Shimizu 890ddd
	msum = (sigma1.m + sigma2.m) * coeff - sigma3.m * coeffq + 0.5;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	row2->r = tcrop((int)rsum, 0, max);
Toshihiro Shimizu 890ddd
	row2->g = tcrop((int)gsum, 0, max);
Toshihiro Shimizu 890ddd
	row2->b = tcrop((int)bsum, 0, max);
Toshihiro Shimizu 890ddd
	row2->m = tcrop((int)msum, 0, max);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (globmatte != 1.0) {
Toshihiro Shimizu 890ddd
		row2->r = (int)(row2->r * globmatte);
Toshihiro Shimizu 890ddd
		row2->g = (int)(row2->g * globmatte);
Toshihiro Shimizu 890ddd
		row2->b = (int)(row2->b * globmatte);
Toshihiro Shimizu 890ddd
		row2->m = (int)(row2->m * globmatte);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	*row2 = overPix(*row1, *row2);
Toshihiro Shimizu 890ddd
	//overlayPixels<t>(*row1, *row2, *row2, globmatte);</t>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//row2++;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	sigma2.r += row1[-brad].r;
Toshihiro Shimizu 890ddd
	sigma2.g += row1[-brad].g;
Toshihiro Shimizu 890ddd
	sigma2.b += row1[-brad].b;
Toshihiro Shimizu 890ddd
	sigma2.m += row1[-brad].m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	pix1 = row1 + brad;
Toshihiro Shimizu 890ddd
	pix2 = row1;
Toshihiro Shimizu 890ddd
	pix3 = row1 - brad;
Toshihiro Shimizu 890ddd
	pix4 = row1 - brad + 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	desigma.r = sigma1.r - sigma2.r;
Toshihiro Shimizu 890ddd
	desigma.g = sigma1.g - sigma2.g;
Toshihiro Shimizu 890ddd
	desigma.b = sigma1.b - sigma2.b;
Toshihiro Shimizu 890ddd
	desigma.m = sigma1.m - sigma2.m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 1; i < length; i++) {
Toshihiro Shimizu 890ddd
		desigma.r += pix1->r - 2 * pix2->r + pix3->r;
Toshihiro Shimizu 890ddd
		desigma.g += pix1->g - 2 * pix2->g + pix3->g;
Toshihiro Shimizu 890ddd
		desigma.b += pix1->b - 2 * pix2->b + pix3->b;
Toshihiro Shimizu 890ddd
		desigma.m += pix1->m - 2 * pix2->m + pix3->m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		rsum += (desigma.r + diff * (pix1->r - pix4->r)) * coeffq;
Toshihiro Shimizu 890ddd
		gsum += (desigma.g + diff * (pix1->g - pix4->g)) * coeffq;
Toshihiro Shimizu 890ddd
		bsum += (desigma.b + diff * (pix1->b - pix4->b)) * coeffq;
Toshihiro Shimizu 890ddd
		msum += (desigma.m + diff * (pix1->m - pix4->m)) * coeffq;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		row2->r = tcrop((int)rsum, 0, max);
Toshihiro Shimizu 890ddd
		row2->g = tcrop((int)gsum, 0, max);
Toshihiro Shimizu 890ddd
		row2->b = tcrop((int)bsum, 0, max);
Toshihiro Shimizu 890ddd
		row2->m = tcrop((int)msum, 0, max);
Toshihiro Shimizu 890ddd
		if (globmatte != 1.0) {
Toshihiro Shimizu 890ddd
			row2->r = (int)(row2->r * globmatte);
Toshihiro Shimizu 890ddd
			row2->g = (int)(row2->g * globmatte);
Toshihiro Shimizu 890ddd
			row2->b = (int)(row2->b * globmatte);
Toshihiro Shimizu 890ddd
			row2->m = (int)(row2->m * globmatte);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		*row2 = overPix(*pix2, *row2);
Toshihiro Shimizu 890ddd
		//overlayPixels<t>(*pix2, *row2, *row2, 0.8);</t>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		row2++;
Toshihiro Shimizu 890ddd
		pix1++, pix2++, pix3++, pix4++;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
void do_filtering(T *row1, T *row2, int length, double coeff, int brad, double Mblur, double globmatte)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	double rsum, gsum, bsum, msum;
Toshihiro Shimizu 890ddd
	TPixelD sigma1, sigma2;
Toshihiro Shimizu 890ddd
	int max = T::maxChannelValue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	sigma1.r = sigma1.g = sigma1.b = sigma1.m = 0.0;
Toshihiro Shimizu 890ddd
	sigma2.r = sigma2.g = sigma2.b = sigma2.m = 0.0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i <= brad; i++) {
Toshihiro Shimizu 890ddd
		sigma1.r += row1[-i].r;
Toshihiro Shimizu 890ddd
		sigma1.g += row1[-i].g;
Toshihiro Shimizu 890ddd
		sigma1.b += row1[-i].b;
Toshihiro Shimizu 890ddd
		sigma1.m += row1[-i].m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		sigma2.r += -i * row1[-i].r;
Toshihiro Shimizu 890ddd
		sigma2.g += -i * row1[-i].g;
Toshihiro Shimizu 890ddd
		sigma2.b += -i * row1[-i].b;
Toshihiro Shimizu 890ddd
		sigma2.m += -i * row1[-i].m;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < length; i++) /* for the ith point the previous computing is used, with the values */
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		/* stored in the auxiliar variables sigma1 and sigma2.                           */
Toshihiro Shimizu 890ddd
		rsum = ((Mblur - i) * sigma1.r + sigma2.r) / coeff;
Toshihiro Shimizu 890ddd
		gsum = ((Mblur - i) * sigma1.g + sigma2.g) / coeff;
Toshihiro Shimizu 890ddd
		bsum = ((Mblur - i) * sigma1.b + sigma2.b) / coeff;
Toshihiro Shimizu 890ddd
		msum = ((Mblur - i) * sigma1.m + sigma2.m) / coeff;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		row2[i].r = (rsum < max) ? troundp(rsum) : max;
Toshihiro Shimizu 890ddd
		row2[i].g = (gsum < max) ? troundp(gsum) : max;
Toshihiro Shimizu 890ddd
		row2[i].b = (bsum < max) ? troundp(bsum) : max;
Toshihiro Shimizu 890ddd
		row2[i].m = (msum < max) ? troundp(msum) : max;
Toshihiro Shimizu 890ddd
		if (globmatte != 1.0) {
Toshihiro Shimizu 890ddd
			row2[i].r = (int)(row2[i].r * globmatte);
Toshihiro Shimizu 890ddd
			row2[i].g = (int)(row2[i].g * globmatte);
Toshihiro Shimizu 890ddd
			row2[i].b = (int)(row2[i].b * globmatte);
Toshihiro Shimizu 890ddd
			row2[i].m = (int)(row2[i].m * globmatte);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		row2[i] = overPix(row1[i], row2[i]);
Toshihiro Shimizu 890ddd
		//overlayPixels<t>(row1[i], row2[i], row2[i], globmatte);</t>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (i < length - 1) {
Toshihiro Shimizu 890ddd
			sigma1.r += row1[i + 1].r - row1[i - brad].r;
Toshihiro Shimizu 890ddd
			sigma1.g += row1[i + 1].g - row1[i - brad].g;
Toshihiro Shimizu 890ddd
			sigma1.b += row1[i + 1].b - row1[i - brad].b;
Toshihiro Shimizu 890ddd
			sigma1.m += row1[i + 1].m - row1[i - brad].m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			sigma2.r += (i + 1) * row2[i + 1].r - (i - brad) * row1[i - brad].r;
Toshihiro Shimizu 890ddd
			sigma2.g += (i + 1) * row2[i + 1].g - (i - brad) * row1[i - brad].g;
Toshihiro Shimizu 890ddd
			sigma2.b += (i + 1) * row2[i + 1].b - (i - brad) * row1[i - brad].b;
Toshihiro Shimizu 890ddd
			sigma2.m += (i + 1) * row2[i + 1].m - (i - brad) * row1[i - brad].m;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
void takeRow(T *rin, T *row, int lx, int brad, bool bidirectional)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < lx; i++)
Toshihiro Shimizu 890ddd
		row[i] = rin[i];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = -1; i > -brad; i--) /* pixels equal to the ones of border of image are added   */
Toshihiro Shimizu 890ddd
		row[i] = row[0];		 /* to avoid a black blur to get into the picture.          */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (bidirectional)
Toshihiro Shimizu 890ddd
		for (i = lx; i < lx + brad; i++) /* pixels equal to the ones of border of image are added   */
Toshihiro Shimizu 890ddd
			row[i] = row[lx - 1];		 /* to avoid a black blur to get into the picture.          */
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
void doDirectionalBlur(TRasterPT<t> r, double blur, bool bidirectional)</t>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i, lx, ly, brad;
Toshihiro Shimizu 890ddd
	double coeff, coeffq, diff, globmatte;
Toshihiro Shimizu 890ddd
	T *row, *buffer;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	brad = tfloor(blur); /* number of pixels involved in the filtering.          */
Toshihiro Shimizu 890ddd
	if (bidirectional) {
Toshihiro Shimizu 890ddd
		coeff = blur / (brad - brad * brad + blur * (2 * brad - 1)); //sum of the weights of triangolar filter.
Toshihiro Shimizu 890ddd
		coeffq = coeff / blur;
Toshihiro Shimizu 890ddd
		diff = blur - brad;
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		coeff = (brad + 1) * (1 - brad / (2 * blur)) * blur; /*sum of the weights of triangolar filter.              */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	lx = r->getLx();
Toshihiro Shimizu 890ddd
	ly = r->getLy();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if ((lx == 0) || (ly == 0))
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	row = new T[lx + 2 * brad + 2];
Toshihiro Shimizu 890ddd
	if (!row)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	memset(row, 0, (lx + 2 * brad + 2) * sizeof(T));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	globmatte = 0.8; /* a little bit of transparency is also added */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	r->lock();
Toshihiro Shimizu 890ddd
	for (i = 0; i < ly; i++) {
Toshihiro Shimizu 890ddd
		buffer = (T *)r->pixels(i);
Toshihiro Shimizu 890ddd
		takeRow(buffer, row + brad, lx, brad, bidirectional);
Toshihiro Shimizu 890ddd
		if (bidirectional)
Toshihiro Shimizu 890ddd
			blur_code<t>(row + brad, buffer, lx, coeff, coeffq, brad, diff, globmatte);</t>
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			do_filtering<t>(row + brad, buffer, lx, coeff, brad, blur, globmatte);</t>
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	free(row);
Toshihiro Shimizu 890ddd
	r->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
void directionalBlur(TRasterPT<t> rout, TRasterPT<t> rin, const TPointD &blur, const TPoint &offset, bool bidirectional)</t></t>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double cs, sn, cx_aux, cy_aux;
Toshihiro Shimizu 890ddd
	int lx, ly, lx_aux, ly_aux;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	double blurValue = tdistance(TPointD(), blur);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	lx = rin->getLx();
Toshihiro Shimizu 890ddd
	ly = rin->getLy();
Toshihiro Shimizu 890ddd
	cs = blur.x / blurValue;
Toshihiro Shimizu 890ddd
	sn = blur.y / blurValue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	lx_aux = (int)tceil(lx * fabs(cs) + ly * fabs(sn));
Toshihiro Shimizu 890ddd
	ly_aux = (int)tceil(lx * fabs(sn) + ly * fabs(cs));
Toshihiro Shimizu 890ddd
	cx_aux = lx_aux * .5;
Toshihiro Shimizu 890ddd
	cy_aux = ly_aux * .5;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterPT<t> raux = TRasterPT<t>(lx_aux, ly_aux);</t></t>
Toshihiro Shimizu 890ddd
	TAffine rot(cs, sn, 0, -sn, cs, 0);
Toshihiro Shimizu 890ddd
	rot = rot.place(convert(rin->getCenter()), convert(raux->getCenter()));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRop::resample(raux, rin, rot);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	doDirectionalBlur<t>(raux, blurValue, bidirectional);</t>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TAffine rotInv(cs, -sn, 0, sn, cs, 0);
Toshihiro Shimizu 890ddd
	rotInv = rotInv.place(convert(raux->getCenter()), convert(rin->getCenter() + offset));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRop::resample(rout, raux, rotInv);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} //namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void enlargeDir(TRectD &r, TPointD p, bool bidirectional)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (bidirectional) {
Toshihiro Shimizu 890ddd
		r.x1 += fabs(p.x);
Toshihiro Shimizu 890ddd
		r.x0 -= fabs(p.x);
Toshihiro Shimizu 890ddd
		r.y1 += fabs(p.y);
Toshihiro Shimizu 890ddd
		r.y0 -= fabs(p.y);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		if (p.x > 0)
Toshihiro Shimizu 890ddd
			r.x1 += p.x;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			r.x0 -= -p.x;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (p.y > 0)
Toshihiro Shimizu 890ddd
			r.y1 += p.y;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			r.y0 -= -p.y;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void reduceDir(TRectD &r, TPointD p, bool bidirectional)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (bidirectional) {
Toshihiro Shimizu 890ddd
		r.x1 -= fabs(p.x);
Toshihiro Shimizu 890ddd
		r.x0 += fabs(p.x);
Toshihiro Shimizu 890ddd
		r.y1 -= fabs(p.y);
Toshihiro Shimizu 890ddd
		r.y0 += fabs(p.y);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		if (p.x > 0)
Toshihiro Shimizu 890ddd
			r.x1 -= p.x;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			r.x0 += -p.x;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (p.y > 0)
Toshihiro Shimizu 890ddd
			r.y1 -= p.y;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			r.y0 += -p.y;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DirectionalBlurBaseFx : public TStandardRasterFx
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//FX_PLUGIN_DECLARATION(DirectionalBlurBaseFx)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
protected:
Toshihiro Shimizu 890ddd
	bool m_isMotionBlur;
Toshihiro Shimizu 890ddd
	TRasterFxPort m_input;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_angle;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_intensity;
Toshihiro Shimizu 890ddd
	TBoolParamP m_bidirectional;
Toshihiro Shimizu 890ddd
	TBoolParamP m_spread;
Toshihiro Shimizu 890ddd
	//  TBoolParamP m_useSSE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	bool doGetBBox(double frame, TRectD &bBox, const TRenderSettings &info);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	DirectionalBlurBaseFx(bool isMotionBLur) : m_isMotionBlur(isMotionBLur), m_angle(0.0), m_intensity(10.0), m_bidirectional(false), m_spread(true)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		bindParam(this, "intensity", m_intensity);
Toshihiro Shimizu 890ddd
		bindParam(this, "bidirectional", m_bidirectional);
Toshihiro Shimizu 890ddd
		bindParam(this, "spread", m_spread);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		addInputPort("Source", m_input);
Toshihiro Shimizu 890ddd
		m_intensity->setValueRange(0, (std::numeric_limits<double>::max)());</double>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		getAttributes()->setIsSpeedAware(true);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPointD getBlurVector(double frame) const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TPointD speed = getAttributes()->getSpeed();
Toshihiro Shimizu 890ddd
		double value = m_intensity->getValue(frame);
Toshihiro Shimizu 890ddd
		return value * speed;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	~DirectionalBlurBaseFx(){};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void doCompute(TTile &tile, double frame, const TRenderSettings &);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool canHandle(const TRenderSettings &info, double frame)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return isAlmostIsotropic(info.m_affine) || m_intensity->getValue(frame) == 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int getMemoryRequirement(const TRectD &rect, double frame, const TRenderSettings &info);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DirectionalBlurFx : public DirectionalBlurBaseFx
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FX_PLUGIN_DECLARATION(DirectionalBlurFx)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	DirectionalBlurFx()
Toshihiro Shimizu 890ddd
		: DirectionalBlurBaseFx(false)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_intensity->setMeasureName("fxLength");
Toshihiro Shimizu 890ddd
		m_angle->setMeasureName("angle");
Toshihiro Shimizu 890ddd
		bindParam(this, "angle", m_angle);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void getParamUIs(TParamUIConcept *&concepts, int &length)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		concepts = new TParamUIConcept[length = 1];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		concepts[0].m_type = TParamUIConcept::POLAR;
Toshihiro Shimizu 890ddd
		concepts[0].m_label = "Angle and Intensity";
Toshihiro Shimizu 890ddd
		concepts[0].m_params.push_back(m_angle);
Toshihiro Shimizu 890ddd
		concepts[0].m_params.push_back(m_intensity);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class MotionBlurFx : public DirectionalBlurBaseFx
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FX_PLUGIN_DECLARATION(MotionBlurFx)
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	MotionBlurFx()
Toshihiro Shimizu 890ddd
		: DirectionalBlurBaseFx(true)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	string getAlias(double frame, const TRenderSettings &info) const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		unsigned long id = getIdentifier();
Toshihiro Shimizu 890ddd
		double value = m_intensity->getValue(frame);
Toshihiro Shimizu 890ddd
		return getFxType() + "[" + toString(id) + "," + toString(frame) + "," + toString(value) + "]";
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool DirectionalBlurBaseFx::doGetBBox(double frame, TRectD &bBox, const TRenderSettings &info)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_input.isConnected()) {
Toshihiro Shimizu 890ddd
		bool ret = m_input->doGetBBox(frame, bBox, info);
Toshihiro Shimizu 890ddd
		if (m_spread->getValue()) {
Toshihiro Shimizu 890ddd
			TPointD blur;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (m_isMotionBlur)
Toshihiro Shimizu 890ddd
				blur = getBlurVector(frame);
Toshihiro Shimizu 890ddd
			else {
Toshihiro Shimizu 890ddd
				double angle = m_angle->getValue(frame) * (TConsts::pi / 180);
Toshihiro Shimizu 890ddd
				blur.x = m_intensity->getValue(frame) * cos(angle);
Toshihiro Shimizu 890ddd
				blur.y = m_intensity->getValue(frame) * sin(angle);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			enlargeDir(bBox, blur, m_bidirectional->getValue());
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		return ret;
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		bBox = TRectD();
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void DirectionalBlurBaseFx::doCompute(TTile &tile, double frame, const TRenderSettings &ri)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_input.isConnected())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPointD blurVector;
Toshihiro Shimizu 890ddd
	if (m_isMotionBlur) {
Toshihiro Shimizu 890ddd
		blurVector = getBlurVector(frame);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		double angle = m_angle->getValue(frame) * (TConsts::pi / 180);
Toshihiro Shimizu 890ddd
		blurVector.x = m_intensity->getValue(frame) * cos(angle);
Toshihiro Shimizu 890ddd
		blurVector.y = m_intensity->getValue(frame) * sin(angle);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const double limit = 900.0;
Toshihiro Shimizu 890ddd
	blurVector.x = tcrop(blurVector.x, -limit, limit);
Toshihiro Shimizu 890ddd
	blurVector.y = tcrop(blurVector.y, -limit, limit);
Toshihiro Shimizu 890ddd
	double shrink = (ri.m_shrinkX + ri.m_shrinkY) / 2.0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TAffine aff = ri.m_affine;
Toshihiro Shimizu 890ddd
	aff.a13 = aff.a23 = 0;
Toshihiro Shimizu 890ddd
	TPointD blur = (1.0 / shrink) * (aff * blurVector);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	double blurValue = norm(blur);
Toshihiro Shimizu 890ddd
	if (areAlmostEqual(blurValue, 0, 1e-3)) {
Toshihiro Shimizu 890ddd
		m_input->compute(tile, frame, ri);
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRectD rectTile = TRectD(tile.m_pos, TDimensionD(tile.getRaster()->getLx(), tile.getRaster()->getLy()));
Toshihiro Shimizu 890ddd
	if (!rectTile.isEmpty()) {
Toshihiro Shimizu 890ddd
		TRectD bboxIn;
Toshihiro Shimizu 890ddd
		m_input->getBBox(frame, bboxIn, ri);
Toshihiro Shimizu 890ddd
		if (bboxIn == TConsts::infiniteRectD)
Toshihiro Shimizu 890ddd
			bboxIn = rectTile;
Toshihiro Shimizu 890ddd
		TPointD blur = (1.0 / shrink) * (aff * blurVector);
Toshihiro Shimizu 890ddd
		//enlarge must be bidirectional, because we need pixel on the ohter side of blur
Toshihiro Shimizu 890ddd
		enlargeDir(bboxIn, blur, true);
Toshihiro Shimizu 890ddd
		rectTile = bboxIn * rectTile.enlarge(fabs(blur.x), fabs(blur.y));
Toshihiro Shimizu 890ddd
		TRect rectIn;
Toshihiro Shimizu 890ddd
		rectIn.x0 = tfloor(rectTile.x0);
Toshihiro Shimizu 890ddd
		rectIn.y0 = tfloor(rectTile.y0);
Toshihiro Shimizu 890ddd
		rectIn.x1 = tceil(rectTile.x1);
Toshihiro Shimizu 890ddd
		rectIn.y1 = tceil(rectTile.y1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		int rasInLx = rectIn.getLx();
Toshihiro Shimizu 890ddd
		int rasInLy = rectIn.getLy();
Toshihiro Shimizu 890ddd
		TTile tileIn;
Toshihiro Shimizu 890ddd
		m_input->allocateAndCompute(tileIn, convert(rectIn.getP00()),
Toshihiro Shimizu 890ddd
									TDimension(rasInLx, rasInLy),
Toshihiro Shimizu 890ddd
									tile.getRaster(), frame, ri);
Toshihiro Shimizu 890ddd
		TRasterP rasIn = tileIn.getRaster();
Toshihiro Shimizu 890ddd
		TRect rectOut = rasIn->getBounds() + (rectIn.getP00() - convert(tile.m_pos));
Toshihiro Shimizu 890ddd
		TRasterP rasOut = tile.getRaster()->extract(rectOut);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TPoint p;
Toshihiro Shimizu 890ddd
		if (rectIn.x0 < tile.m_pos.x)
Toshihiro Shimizu 890ddd
			p.x = rectIn.x0 - tile.m_pos.x;
Toshihiro Shimizu 890ddd
		if (rectIn.y0 < tile.m_pos.y)
Toshihiro Shimizu 890ddd
			p.y = rectIn.y0 - tile.m_pos.y;
Toshihiro Shimizu 890ddd
		if ((TRaster32P)rasIn && (TRaster32P)rasOut)
Toshihiro Shimizu 890ddd
			directionalBlur<tpixel32>(rasOut, rasIn, blur, p, m_bidirectional->getValue());</tpixel32>
Toshihiro Shimizu 890ddd
		else if ((TRaster64P)rasIn && (TRaster64P)rasOut)
Toshihiro Shimizu 890ddd
			directionalBlur<tpixel64>(rasOut, rasIn, blur, p, m_bidirectional->getValue());</tpixel64>
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int DirectionalBlurBaseFx::getMemoryRequirement(const TRectD &rect, double frame, const TRenderSettings &info)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPointD blurVector;
Toshihiro Shimizu 890ddd
	if (m_isMotionBlur) {
Toshihiro Shimizu 890ddd
		blurVector = getBlurVector(frame);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		double angle = m_angle->getValue(frame) * (TConsts::pi / 180);
Toshihiro Shimizu 890ddd
		blurVector.x = m_intensity->getValue(frame) * cos(angle);
Toshihiro Shimizu 890ddd
		blurVector.y = m_intensity->getValue(frame) * sin(angle);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const double limit = 900.0;
Toshihiro Shimizu 890ddd
	blurVector.x = tcrop(blurVector.x, -limit, limit);
Toshihiro Shimizu 890ddd
	blurVector.y = tcrop(blurVector.y, -limit, limit);
Toshihiro Shimizu 890ddd
	double shrink = (info.m_shrinkX + info.m_shrinkY) / 2.0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TAffine aff = info.m_affine;
Toshihiro Shimizu 890ddd
	aff.a13 = aff.a23 = 0;
Toshihiro Shimizu 890ddd
	TPointD blur = (1.0 / shrink) * (aff * blurVector);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TRasterFx::memorySize(rect.enlarge(blur.x, blur.y), info.m_bpp);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(DirectionalBlurFx, "directionalBlurFx");
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(MotionBlurFx, "motionBlurFx");