Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "trandom.h"
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
#include "stdfx.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class NoiseFx : public TStandardRasterFx
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FX_PLUGIN_DECLARATION(NoiseFx)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterFxPort m_input;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_value;
Toshihiro Shimizu 890ddd
	TBoolParamP m_Red;
Toshihiro Shimizu 890ddd
	TBoolParamP m_Green;
Toshihiro Shimizu 890ddd
	TBoolParamP m_Blue;
Toshihiro Shimizu 890ddd
	TBoolParamP m_BW;
Toshihiro Shimizu 890ddd
	TBoolParamP m_Animate;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	NoiseFx()
Toshihiro Shimizu 890ddd
		: m_value(100.0), m_Red(1.0), m_Green(1.0), m_Blue(1.0), m_BW(0.0), m_Animate(0.0)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		bindParam(this, "Intensity", m_value);
Toshihiro Shimizu 890ddd
		bindParam(this, "Red", m_Red);
Toshihiro Shimizu 890ddd
		bindParam(this, "Green", m_Green);
Toshihiro Shimizu 890ddd
		bindParam(this, "Blue", m_Blue);
Toshihiro Shimizu 890ddd
		bindParam(this, "Black_White", m_BW);
Toshihiro Shimizu 890ddd
		bindParam(this, "Animate", m_Animate);
Toshihiro Shimizu 890ddd
		addInputPort("Source", m_input);
Toshihiro Shimizu 890ddd
		m_value->setValueRange(0, (std::numeric_limits<double>::max)());</double>
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	~NoiseFx(){};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool doGetBBox(double frame, TRectD &bBox, const TRenderSettings &info)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if (m_input.isConnected())
Toshihiro Shimizu 890ddd
			return m_input->doGetBBox(frame, bBox, info);
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
	void doCompute(TTile &tile, double frame, const TRenderSettings &ri);
Shinya Kitaoka 3bfa54
	std::string getAlias(double frame, const TRenderSettings &info) const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool canHandle(const TRenderSettings &info, double frame) { return true; }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
template <class pixel=""></class>
Toshihiro Shimizu 890ddd
class RayleighNoise
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	std::vector<double> noise_buf;</double>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	RayleighNoise(double sigma, int seed = 0)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		noise_buf.resize(PIXEL::maxChannelValue + 1);
Toshihiro Shimizu 890ddd
		TRandom random(seed);
Toshihiro Shimizu 890ddd
		sigma = log(sigma * 0.07 + 1);
Toshihiro Shimizu 890ddd
		if (PIXEL::maxChannelValue == 255)
Toshihiro Shimizu 890ddd
			sigma = 2.0 * sigma * sigma * sigma * sigma;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			sigma = 257.0 * 2.0 * sigma * sigma * sigma * sigma;
Toshihiro Shimizu 890ddd
		for (int i = 0; i < PIXEL::maxChannelValue + 1; i++) {
Toshihiro Shimizu 890ddd
			noise_buf[i] = sigma * sqrt(-log(1.0 - random.getFloat())) *
Toshihiro Shimizu 890ddd
						   cos(TConsts::pi * (2.0 * random.getFloat() - 1.0));
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	double get_value(int index)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		tcrop(index, 0, PIXEL::maxChannelValue);
Toshihiro Shimizu 890ddd
		return noise_buf[index];
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename channel_type="" pixel,="" pixelgray,="" typename=""></typename>
Toshihiro Shimizu 890ddd
void doNoise(TRasterPT<pixel> &ras, double sigma, bool bw, bool red, bool green, bool blue,</pixel>
Toshihiro Shimizu 890ddd
			 bool animate, double frame)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	RayleighNoise<pixel> noise(sigma);</pixel>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRandom random;
Toshihiro Shimizu 890ddd
	if (animate)
Toshihiro Shimizu 890ddd
		random.setSeed(frame);
Toshihiro Shimizu 890ddd
	int j;
Toshihiro Shimizu 890ddd
	ras->lock();
Toshihiro Shimizu 890ddd
	for (j = 0; j < ras->getLy(); j++) {
Toshihiro Shimizu 890ddd
		PIXEL *pix = ras->pixels(j);
Toshihiro Shimizu 890ddd
		PIXEL *endPix = pix + ras->getLx();
Toshihiro Shimizu 890ddd
		while (pix < endPix) {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (bw) {
Toshihiro Shimizu 890ddd
				double index;
Toshihiro Shimizu 890ddd
				double value;
Toshihiro Shimizu 890ddd
				index = random.getFloat() * pix->m; //PIXEL::maxChannelValue;
Toshihiro Shimizu 890ddd
				value = noise.get_value(tfloor(index));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				//value = (value<pix->m)?((value>0)?(int)value:0):PIXEL::maxChannelValue;</pix->
Toshihiro Shimizu 890ddd
				//if(value)
Toshihiro Shimizu 890ddd
				//{
Toshihiro Shimizu 890ddd
				int reference;
Toshihiro Shimizu 890ddd
				reference = PIXELGRAY::from(*pix).value;
Toshihiro Shimizu 890ddd
				pix->r = tcrop<int>(value + reference, 0, pix->m);</int>
Toshihiro Shimizu 890ddd
				pix->g = tcrop<int>(value + reference, 0, pix->m);</int>
Toshihiro Shimizu 890ddd
				pix->b = tcrop<int>(value + reference, 0, pix->m);</int>
Toshihiro Shimizu 890ddd
				//}
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
				double r, g, b, index;
Toshihiro Shimizu 890ddd
				if (red) {
Toshihiro Shimizu 890ddd
					index = random.getFloat() * pix->m; //PIXEL::maxChannelValue;
Toshihiro Shimizu 890ddd
					r = noise.get_value(tfloor(index)) + pix->r;
Toshihiro Shimizu 890ddd
					pix->r = tcrop<int>(r, 0, pix->m);</int>
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				if (green) {
Toshihiro Shimizu 890ddd
					index = random.getFloat() * pix->m; //PIXEL::maxChannelValue;
Toshihiro Shimizu 890ddd
					g = noise.get_value(tfloor(index)) + pix->g;
Toshihiro Shimizu 890ddd
					pix->g = tcrop<int>(g, 0, pix->m);</int>
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				if (blue) {
Toshihiro Shimizu 890ddd
					index = random.getFloat() * pix->m; //PIXEL::maxChannelValue;
Toshihiro Shimizu 890ddd
					b = noise.get_value(tfloor(index)) + pix->b;
Toshihiro Shimizu 890ddd
					pix->b = tcrop<int>(b, 0, pix->m);</int>
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			*pix++;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	ras->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void NoiseFx::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
	m_input->compute(tile, frame, ri);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	double sigma = m_value->getValue(frame);
Toshihiro Shimizu 890ddd
	bool bw = m_BW->getValue();
Toshihiro Shimizu 890ddd
	bool red = m_Red->getValue();
Toshihiro Shimizu 890ddd
	bool green = m_Green->getValue();
Toshihiro Shimizu 890ddd
	bool blue = m_Blue->getValue();
Toshihiro Shimizu 890ddd
	bool animate = m_Animate->getValue();
Toshihiro Shimizu 890ddd
	if (sigma == 0.0)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRaster32P raster32 = tile.getRaster();
Toshihiro Shimizu 890ddd
	if (raster32)
Toshihiro Shimizu 890ddd
		doNoise<tpixel32, tpixelgr8,="" uchar="">(raster32, sigma, bw, red, green, blue, animate, frame);</tpixel32,>
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		TRaster64P raster64 = tile.getRaster();
Toshihiro Shimizu 890ddd
		if (raster64)
Toshihiro Shimizu 890ddd
			doNoise<tpixel64, tpixelgr16,="" ushort="">(raster64, sigma, bw, red, green, blue, animate, frame);</tpixel64,>
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			throw TException("Brightness&Contrast: unsupported Pixel Type");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
std::string NoiseFx::getAlias(double frame, const TRenderSettings &info) const
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	std::string alias = getFxType();
Toshihiro Shimizu 890ddd
	alias += "[";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// alias degli effetti connessi alle porte di input separati da virgole
Toshihiro Shimizu 890ddd
	// una porta non connessa da luogo a un alias vuoto (stringa vuota)
Toshihiro Shimizu 890ddd
	int i = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < getInputPortCount(); i++) {
Toshihiro Shimizu 890ddd
		TFxPort *port = getInputPort(i);
Toshihiro Shimizu 890ddd
		if (port->isConnected()) {
Toshihiro Shimizu 890ddd
			TRasterFxP ifx = port->getFx();
Toshihiro Shimizu 890ddd
			assert(ifx);
Toshihiro Shimizu 890ddd
			alias += ifx->getAlias(frame, info);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		alias += ",";
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool addframe = 0;
Shinya Kitaoka 3bfa54
	std::string paramalias("");
Toshihiro Shimizu 890ddd
	for (i = 0; i < getParams()->getParamCount(); i++) {
Toshihiro Shimizu 890ddd
		TParam *param = getParams()->getParam(i);
Toshihiro Shimizu 890ddd
		paramalias += param->getName() + "=" + param->getValueAlias(frame, 3);
Toshihiro Shimizu 890ddd
		if (param->getName() == "Animate" && param->getValueAlias(frame, 0) == "1")
Toshihiro Shimizu 890ddd
			addframe = true;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (addframe)
Toshihiro Shimizu 890ddd
		alias += toString(frame) + ",";
Toshihiro Shimizu 890ddd
	alias += paramalias + "]";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return alias;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(NoiseFx, "noiseFx")