Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "stdfx.h"
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
//#include "trop.h"
Toshihiro Shimizu 890ddd
#include <math.h></math.h>
Toshihiro Shimizu 890ddd
#include "tpixelutils.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class ChannelMixerFx : public TStandardRasterFx
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FX_PLUGIN_DECLARATION(ChannelMixerFx)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterFxPort m_input;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_r_r;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_g_r;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_b_r;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_m_r;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_r_g;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_g_g;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_b_g;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_m_g;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_r_b;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_g_b;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_b_b;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_m_b;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_r_m;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_g_m;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_b_m;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_m_m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	ChannelMixerFx()
Toshihiro Shimizu 890ddd
		: m_r_r(1.0), m_g_r(0.0), m_b_r(0.0), m_m_r(0.0), m_r_g(0.0), m_g_g(1.0), m_b_g(0.0), m_m_g(0.0), m_r_b(0.0), m_g_b(0.0), m_b_b(1.0), m_m_b(0.0), m_r_m(0.0), m_g_m(0.0), m_b_m(0.0), m_m_m(1.0)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		addInputPort("Source", m_input);
Toshihiro Shimizu 890ddd
		bindParam(this, "red_to_red", m_r_r);
Toshihiro Shimizu 890ddd
		bindParam(this, "green_to_red", m_g_r);
Toshihiro Shimizu 890ddd
		bindParam(this, "blue_to_red", m_b_r);
Toshihiro Shimizu 890ddd
		bindParam(this, "matte_to_red", m_m_r);
Toshihiro Shimizu 890ddd
		bindParam(this, "red_to_green", m_r_g);
Toshihiro Shimizu 890ddd
		bindParam(this, "green_to_green", m_g_g);
Toshihiro Shimizu 890ddd
		bindParam(this, "blue_to_green", m_b_g);
Toshihiro Shimizu 890ddd
		bindParam(this, "matte_to_green", m_m_g);
Toshihiro Shimizu 890ddd
		bindParam(this, "red_to_blue", m_r_b);
Toshihiro Shimizu 890ddd
		bindParam(this, "green_to_blue", m_g_b);
Toshihiro Shimizu 890ddd
		bindParam(this, "blue_to_blue", m_b_b);
Toshihiro Shimizu 890ddd
		bindParam(this, "matte_to_blue", m_m_b);
Toshihiro Shimizu 890ddd
		bindParam(this, "red_to_matte", m_r_m);
Toshihiro Shimizu 890ddd
		bindParam(this, "green_to_matte", m_g_m);
Toshihiro Shimizu 890ddd
		bindParam(this, "blue_to_matte", m_b_m);
Toshihiro Shimizu 890ddd
		bindParam(this, "matte_to_matte", m_m_m);
Toshihiro Shimizu 890ddd
		m_r_r->setValueRange(0, 1);
Toshihiro Shimizu 890ddd
		m_r_g->setValueRange(0, 1);
Toshihiro Shimizu 890ddd
		m_r_b->setValueRange(0, 1);
Toshihiro Shimizu 890ddd
		m_r_m->setValueRange(0, 1);
Toshihiro Shimizu 890ddd
		m_g_r->setValueRange(0, 1);
Toshihiro Shimizu 890ddd
		m_g_g->setValueRange(0, 1);
Toshihiro Shimizu 890ddd
		m_g_b->setValueRange(0, 1);
Toshihiro Shimizu 890ddd
		m_g_m->setValueRange(0, 1);
Toshihiro Shimizu 890ddd
		m_b_r->setValueRange(0, 1);
Toshihiro Shimizu 890ddd
		m_b_g->setValueRange(0, 1);
Toshihiro Shimizu 890ddd
		m_b_b->setValueRange(0, 1);
Toshihiro Shimizu 890ddd
		m_b_m->setValueRange(0, 1);
Toshihiro Shimizu 890ddd
		m_m_r->setValueRange(0, 1);
Toshihiro Shimizu 890ddd
		m_m_g->setValueRange(0, 1);
Toshihiro Shimizu 890ddd
		m_m_b->setValueRange(0, 1);
Toshihiro Shimizu 890ddd
		m_m_m->setValueRange(0, 1);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	~ChannelMixerFx(){};
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
	void doCompute(TTile &tile, double frame, const TRenderSettings &ri);
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
template <typename channel_type="" pixel,="" typename=""></typename>
Toshihiro Shimizu 890ddd
void depremult(PIXEL *pix)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!pix->m)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	double depremult = (double)PIXEL::maxChannelValue / pix->m;
Toshihiro Shimizu 890ddd
	pix->r = (CHANNEL_TYPE)(pix->r * depremult);
Toshihiro Shimizu 890ddd
	pix->g = (CHANNEL_TYPE)(pix->g * depremult);
Toshihiro Shimizu 890ddd
	pix->b = (CHANNEL_TYPE)(pix->b * depremult);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename channel_type="" pixel,="" typename=""></typename>
Toshihiro Shimizu 890ddd
void doChannelMixer(TRasterPT<pixel> ras, double r_r, double r_g, double r_b, double r_m, double g_r, double g_g, double g_b,</pixel>
Toshihiro Shimizu 890ddd
					double g_m, double b_r, double b_g, double b_b, double b_m, double m_r,
Toshihiro Shimizu 890ddd
					double m_g, double m_b, double m_m)
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
		PIXEL *pix = ras->pixels(j);
Toshihiro Shimizu 890ddd
		PIXEL *endPix = pix + ras->getLx();
Toshihiro Shimizu 890ddd
		while (pix < endPix) {
Toshihiro Shimizu 890ddd
			depremult<pixel, channel_type="">(pix); //if removed, a black line appears in the edge of a level (default values, not black lines)</pixel,>
Toshihiro Shimizu 890ddd
			double red = pix->r * r_r + pix->g * g_r + pix->b * b_r + pix->m * m_r;
Toshihiro Shimizu 890ddd
			double green = pix->r * r_g + pix->g * g_g + pix->b * b_g + pix->m * m_g;
Toshihiro Shimizu 890ddd
			double blue = pix->r * r_b + pix->g * g_b + pix->b * b_b + pix->m * m_b;
Toshihiro Shimizu 890ddd
			double matte = pix->r * r_m + pix->g * g_m + pix->b * b_m + pix->m * m_m;
Toshihiro Shimizu 890ddd
			red = tcrop(red, 0.0, aux);
Toshihiro Shimizu 890ddd
			green = tcrop(green, 0.0, aux);
Toshihiro Shimizu 890ddd
			blue = tcrop(blue, 0.0, aux);
Toshihiro Shimizu 890ddd
			matte = tcrop(matte, 0.0, aux);
Toshihiro Shimizu 890ddd
			pix->r = (CHANNEL_TYPE)red;
Toshihiro Shimizu 890ddd
			pix->g = (CHANNEL_TYPE)green;
Toshihiro Shimizu 890ddd
			pix->b = (CHANNEL_TYPE)blue;
Toshihiro Shimizu 890ddd
			pix->m = (CHANNEL_TYPE)matte;
Toshihiro Shimizu 890ddd
			*pix = premultiply(*pix); //if removed, a white edged line appears in the edge of a level (if m>r, g, b)
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 ChannelMixerFx::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 r_r = m_r_r->getValue(frame);
Toshihiro Shimizu 890ddd
	double r_g = m_r_g->getValue(frame);
Toshihiro Shimizu 890ddd
	double r_b = m_r_b->getValue(frame);
Toshihiro Shimizu 890ddd
	double r_m = m_r_m->getValue(frame);
Toshihiro Shimizu 890ddd
	double g_r = m_g_r->getValue(frame);
Toshihiro Shimizu 890ddd
	double g_g = m_g_g->getValue(frame);
Toshihiro Shimizu 890ddd
	double g_b = m_g_b->getValue(frame);
Toshihiro Shimizu 890ddd
	double g_m = m_g_m->getValue(frame);
Toshihiro Shimizu 890ddd
	double b_r = m_b_r->getValue(frame);
Toshihiro Shimizu 890ddd
	double b_g = m_b_g->getValue(frame);
Toshihiro Shimizu 890ddd
	double b_b = m_b_b->getValue(frame);
Toshihiro Shimizu 890ddd
	double b_m = m_b_m->getValue(frame);
Toshihiro Shimizu 890ddd
	double m_r = m_m_r->getValue(frame);
Toshihiro Shimizu 890ddd
	double m_g = m_m_g->getValue(frame);
Toshihiro Shimizu 890ddd
	double m_b = m_m_b->getValue(frame);
Toshihiro Shimizu 890ddd
	double m_m = m_m_m->getValue(frame);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRaster32P raster32 = tile.getRaster();
Toshihiro Shimizu 890ddd
	if (raster32)
Toshihiro Shimizu 890ddd
		doChannelMixer<tpixel32, uchar="">(raster32, r_r, r_g, r_b, r_m, g_r, g_g, g_b, g_m, b_r, b_g, b_b, b_m, m_r, m_g, m_b, m_m);</tpixel32,>
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		TRaster64P raster64 = tile.getRaster();
Toshihiro Shimizu 890ddd
		if (raster64)
Toshihiro Shimizu 890ddd
			doChannelMixer<tpixel64, ushort="">(raster64, r_r, r_g, r_b, r_m, g_r, g_g, g_b, g_m, b_r, b_g, b_b, b_m, m_r, m_g, m_b, m_m);</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
FX_PLUGIN_IDENTIFIER(ChannelMixerFx, "channelMixerFx")