Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//#include "trop.h"
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
#include <math.h></math.h>
Toshihiro Shimizu 890ddd
#include "stdfx.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tparamset.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class RGBKeyFx : public TStandardRasterFx
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FX_PLUGIN_DECLARATION(RGBKeyFx)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterFxPort m_input;
Toshihiro Shimizu 890ddd
	TPixelParamP m_color;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_rrange;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_grange;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_brange;
Toshihiro Shimizu 890ddd
	TBoolParamP m_gender;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	RGBKeyFx()
Toshihiro Shimizu 890ddd
		: m_color(TPixel32::Black), m_rrange(0.0), m_grange(0.0), m_brange(0.0), m_gender(false)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		bindParam(this, "color", m_color);
Toshihiro Shimizu 890ddd
		bindParam(this, "r_range", m_rrange);
Toshihiro Shimizu 890ddd
		bindParam(this, "g_range", m_grange);
Toshihiro Shimizu 890ddd
		bindParam(this, "b_range", m_brange);
Toshihiro Shimizu 890ddd
		bindParam(this, "invert", m_gender);
Toshihiro Shimizu 890ddd
		m_rrange->setValueRange(0.0, 255.0);
Toshihiro Shimizu 890ddd
		m_grange->setValueRange(0.0, 255.0);
Toshihiro Shimizu 890ddd
		m_brange->setValueRange(0.0, 255.0);
Toshihiro Shimizu 890ddd
		addInputPort("Source", m_input);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	~RGBKeyFx(){};
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
			m_input->doGetBBox(frame, bBox, info);
Toshihiro Shimizu 890ddd
			return true;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	};
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) { return true; }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
void update_param(int ¶m, TRaster32P ras)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void update_param(int ¶m, TRaster64P ras)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	param = param * 257;
Toshihiro Shimizu 890ddd
	return;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename channel_type="" pixel,="" typename=""></typename>
Toshihiro Shimizu 890ddd
void doRGBKey(TRasterPT<pixel> ras, int highR, int highG, int highB,</pixel>
Toshihiro Shimizu 890ddd
			  int lowR, int lowG, int lowB, bool gender)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	update_param(highR, ras);
Toshihiro Shimizu 890ddd
	update_param(highG, ras);
Toshihiro Shimizu 890ddd
	update_param(highB, ras);
Toshihiro Shimizu 890ddd
	update_param(lowR, ras);
Toshihiro Shimizu 890ddd
	update_param(lowG, ras);
Toshihiro Shimizu 890ddd
	update_param(lowB, ras);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int j;
Toshihiro Shimizu 890ddd
	ras->lock();
Toshihiro Shimizu 890ddd
	for (j = 0; j < ras->getLy(); j++) {
Toshihiro Shimizu 890ddd
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
			bool condition = pix->r >= lowR && pix->r <= highR && pix->g >= lowG && pix->g <= highG && pix->b >= lowB && pix->b <= highB;
Toshihiro Shimizu 890ddd
			if (condition != gender)
Toshihiro Shimizu 890ddd
				*pix = PIXEL::Transparent;
Toshihiro Shimizu 890ddd
			pix++;
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 RGBKeyFx::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
	int r_range = (int)m_rrange->getValue(frame);
Toshihiro Shimizu 890ddd
	int g_range = (int)m_grange->getValue(frame);
Toshihiro Shimizu 890ddd
	int b_range = (int)m_brange->getValue(frame);
Toshihiro Shimizu 890ddd
	bool gender = (int)m_gender->getValue();
Toshihiro Shimizu 890ddd
	const TPixel32 Color = m_color->getPremultipliedValue(frame);
Toshihiro Shimizu 890ddd
	TRaster32P raster32 = tile.getRaster();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int lowR = tmax(0, Color.r - r_range);
Toshihiro Shimizu 890ddd
	int highR = tmin(255, Color.r + r_range);
Toshihiro Shimizu 890ddd
	int lowG = tmax(0, Color.g - g_range);
Toshihiro Shimizu 890ddd
	int highG = tmin(255, Color.g + g_range);
Toshihiro Shimizu 890ddd
	int lowB = tmax(0, Color.b - b_range);
Toshihiro Shimizu 890ddd
	int highB = tmin(255, Color.b + b_range);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (raster32)
Toshihiro Shimizu 890ddd
		doRGBKey<tpixel32, uchar="">(raster32, highR, highG, highB, lowR, lowG, lowB, gender);</tpixel32,>
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		TRaster64P raster64 = tile.getRaster();
Toshihiro Shimizu 890ddd
		if (raster64)
Toshihiro Shimizu 890ddd
			doRGBKey<tpixel64, ushort="">(raster64, highR, highG, highB, lowR, lowG, lowB, gender);</tpixel64,>
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			throw TException("RGBKeyFx: unsupported Pixel Type");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(RGBKeyFx, "rgbKeyFx")