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
#include "hsvutil.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class HSVKeyFx : public TStandardRasterFx
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FX_PLUGIN_DECLARATION(HSVKeyFx)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterFxPort m_input;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_h;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_s;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_v;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_hrange;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_srange;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_vrange;
Toshihiro Shimizu 890ddd
	TBoolParamP m_gender;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	HSVKeyFx()
Toshihiro Shimizu 890ddd
		: m_h(0.0), m_s(0.0), m_v(0.0), m_hrange(0.0), m_srange(0.0), m_vrange(0.0), m_gender(false)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		bindParam(this, "h", m_h);
Toshihiro Shimizu 890ddd
		bindParam(this, "s", m_s);
Toshihiro Shimizu 890ddd
		bindParam(this, "v", m_v);
Toshihiro Shimizu 890ddd
		bindParam(this, "h_range", m_hrange);
Toshihiro Shimizu 890ddd
		bindParam(this, "s_range", m_srange);
Toshihiro Shimizu 890ddd
		bindParam(this, "v_range", m_vrange);
Toshihiro Shimizu 890ddd
		bindParam(this, "invert", m_gender);
Toshihiro Shimizu 890ddd
		m_h->setValueRange(0.0, 360.0);
Toshihiro Shimizu 890ddd
		m_s->setValueRange(0.0, 1.0);
Toshihiro Shimizu 890ddd
		m_v->setValueRange(0.0, 1.0);
Toshihiro Shimizu 890ddd
		m_hrange->setValueRange(0.0, 360.0);
Toshihiro Shimizu 890ddd
		m_srange->setValueRange(0.0, 1.0);
Toshihiro Shimizu 890ddd
		m_vrange->setValueRange(0.0, 1.0);
Toshihiro Shimizu 890ddd
		addInputPort("Source", m_input);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	~HSVKeyFx(){};
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
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename pixel=""></typename>
Toshihiro Shimizu 890ddd
void doHSVKey(const TRasterPT<pixel> &ras, double lowH, double highH,</pixel>
Toshihiro Shimizu 890ddd
			  double lowS, double highS, double lowV, double highV, bool gender)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double aux = (double)PIXEL::maxChannelValue;
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
			double h, s, v;
Toshihiro Shimizu 890ddd
			OLDRGB2HSV(pix->r / aux, pix->g / aux, pix->b / aux, &h, &s, &v);
Toshihiro Shimizu 890ddd
			bool condition = h >= lowH && h <= highH && s >= lowS && s <= highS && v >= lowV && v <= highV;
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
void HSVKeyFx::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 h_ref = m_h->getValue(frame);
Toshihiro Shimizu 890ddd
	double s_ref = m_s->getValue(frame);
Toshihiro Shimizu 890ddd
	double v_ref = m_v->getValue(frame);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	double h_range = m_hrange->getValue(frame);
Toshihiro Shimizu 890ddd
	double s_range = m_srange->getValue(frame);
Toshihiro Shimizu 890ddd
	double v_range = m_vrange->getValue(frame);
Toshihiro Shimizu 890ddd
	bool gender = (int)m_gender->getValue();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 12c444
	double lowH = std::max(0.0, h_ref - h_range);
Shinya Kitaoka 12c444
	double highH = std::min(360.0, h_ref + h_range);
Shinya Kitaoka 12c444
	double lowS = std::max(0.0, s_ref - s_range);
Shinya Kitaoka 12c444
	double highS = std::min(1.0, s_ref + s_range);
Shinya Kitaoka 12c444
	double lowV = std::max(0.0, v_ref - v_range);
Shinya Kitaoka 12c444
	double highV = std::min(1.0, v_ref + v_range);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRaster32P raster32 = tile.getRaster();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (raster32)
Toshihiro Shimizu 890ddd
		doHSVKey<tpixel32>(raster32, lowH, highH, lowS, highS, lowV, highV, gender);</tpixel32>
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		TRaster64P raster64 = tile.getRaster();
Toshihiro Shimizu 890ddd
		if (raster64)
Toshihiro Shimizu 890ddd
			doHSVKey<tpixel64>(raster64, lowH, highH, lowS, highS, lowV, highV, gender);</tpixel64>
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			throw TException("HSVKey: unsupported Pixel Type");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(HSVKeyFx, "hsvKeyFx")