Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "stdfx.h"
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
#include "tpixelutils.h"
Toshihiro Shimizu 890ddd
#include "tparamset.h"
Toshihiro Shimizu 890ddd
#include "ttonecurveparam.h"
Toshihiro Shimizu 890ddd
#include "tcurves.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int getCubicYfromX(TCubic c, int x, double &s0, double &s1)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double s = (s1 + s0) * 0.5;
Toshihiro Shimizu 890ddd
	TPointD p = c.getPoint(s);
Toshihiro Shimizu 890ddd
	if (areAlmostEqual(double(x), p.x, 0.001))
Toshihiro Shimizu 890ddd
		return tround(p.y);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (x < p.x)
Toshihiro Shimizu 890ddd
		return getCubicYfromX(c, x, s0, s);
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return getCubicYfromX(c, x, s, s1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int getLinearYfromX(TSegment t, int x, double &s0, double &s1)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double s = (s1 + s0) * 0.5;
Toshihiro Shimizu 890ddd
	TPointD p = t.getPoint(s);
Toshihiro Shimizu 890ddd
	if (areAlmostEqual(double(x), p.x, 0.001))
Toshihiro Shimizu 890ddd
		return tround(p.y);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (x < p.x)
Toshihiro Shimizu 890ddd
		return getLinearYfromX(t, x, s0, s);
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return getLinearYfromX(t, x, s, s1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename pixel,="" t="" typename=""></typename>
Shinya Kitaoka 3bfa54
void fill_lut(QList<tpointd> points, std::vector<t> &lut, bool isLinear)</t></tpointd>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	TPointD p0 = points.at(0);
Toshihiro Shimizu 890ddd
	TCubic cubic;
Toshihiro Shimizu 890ddd
	TSegment segment;
Toshihiro Shimizu 890ddd
	double s0 = 0.0;
Toshihiro Shimizu 890ddd
	for (i = 1; i < points.size(); i++) {
Toshihiro Shimizu 890ddd
		TPointD p1 = points.at(i);
Toshihiro Shimizu 890ddd
		TPointD p2 = points.at(++i);
Toshihiro Shimizu 890ddd
		TPointD p3 = points.at(++i);
Toshihiro Shimizu 890ddd
		if (!isLinear) {
Toshihiro Shimizu 890ddd
			cubic.setP0(p0);
Toshihiro Shimizu 890ddd
			cubic.setP1(p1);
Toshihiro Shimizu 890ddd
			cubic.setP2(p2);
Toshihiro Shimizu 890ddd
			cubic.setP3(p3);
Toshihiro Shimizu 890ddd
			int x = (int)p0.x;
Toshihiro Shimizu 890ddd
			while (x < 0)
Toshihiro Shimizu 890ddd
				x++;
Toshihiro Shimizu 890ddd
			while (x < p3.x && x < PIXEL::maxChannelValue + 1) {
Toshihiro Shimizu 890ddd
				double s1 = 1.0;
Toshihiro Shimizu 890ddd
				int y = getCubicYfromX(cubic, x, s0, s1);
Toshihiro Shimizu 890ddd
				if (y > PIXEL::maxChannelValue + 1)
Toshihiro Shimizu 890ddd
					y = PIXEL::maxChannelValue;
Toshihiro Shimizu 890ddd
				else if (y < 0)
Toshihiro Shimizu 890ddd
					y = 0;
Toshihiro Shimizu 890ddd
				lut[x] = y;
Toshihiro Shimizu 890ddd
				x++;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			segment.setP0(p0);
Toshihiro Shimizu 890ddd
			segment.setP1(p3);
Toshihiro Shimizu 890ddd
			int x = (int)p0.x;
Toshihiro Shimizu 890ddd
			while (x < 0)
Toshihiro Shimizu 890ddd
				x++;
Toshihiro Shimizu 890ddd
			while (x < p3.x && x < PIXEL::maxChannelValue + 1) {
Toshihiro Shimizu 890ddd
				double s1 = 1.0;
Toshihiro Shimizu 890ddd
				int y = getLinearYfromX(segment, x, s0, s1);
Toshihiro Shimizu 890ddd
				if (y > PIXEL::maxChannelValue + 1)
Toshihiro Shimizu 890ddd
					y = PIXEL::maxChannelValue;
Toshihiro Shimizu 890ddd
				else if (y < 0)
Toshihiro Shimizu 890ddd
					y = 0;
Toshihiro Shimizu 890ddd
				lut[x] = y;
Toshihiro Shimizu 890ddd
				x++;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		p0 = p3;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // Namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class ToneCurveFx : public TStandardRasterFx
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FX_PLUGIN_DECLARATION(ToneCurveFx)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterFxPort m_input;
Toshihiro Shimizu 890ddd
	TToneCurveParamP m_toneCurve;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	ToneCurveFx()
Toshihiro Shimizu 890ddd
		: m_toneCurve(new TToneCurveParam())
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		bindParam(this, "curve", m_toneCurve);
Toshihiro Shimizu 890ddd
		addInputPort("Source", m_input);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	~ToneCurveFx(){};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool canHandle(const TRenderSettings &info, double frame) { return true; }
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);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
void update_param(double ¶m, TRaster32P ras)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void update_param(double ¶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
QList<tpointd> getParamSetPoints(const TParamSet *paramSet, int frame)</tpointd>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QList<tpointd> points;</tpointd>
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < paramSet->getParamCount(); i++) {
Toshihiro Shimizu 890ddd
		TPointParamP pointParam = paramSet->getParam(i);
Toshihiro Shimizu 890ddd
		TPointD point = pointParam->getValue(frame);
Toshihiro Shimizu 890ddd
		int x = (int)point.x;
Toshihiro Shimizu 890ddd
		int y = (int)point.y;
Toshihiro Shimizu 890ddd
		points.push_back(TPointD(x, y));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return points;
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 doToneCurveFx(TRasterPT<pixel> ras, double frame, const TToneCurveParam *toneCurveParam)</pixel>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QList<qlist<tpointd>> pointsList;</qlist<tpointd>
Toshihiro Shimizu 890ddd
	int e;
Toshihiro Shimizu 890ddd
	for (e = 0; e < 6; e++) {
Toshihiro Shimizu 890ddd
		TParamSet *paramSet = toneCurveParam->getParamSet(TToneCurveParam::ToneChannel(e)).getPointer();
Toshihiro Shimizu 890ddd
		QList<tpointd> points = getParamSetPoints(paramSet, frame);</tpointd>
Toshihiro Shimizu 890ddd
		pointsList.push_back(points);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	bool isLinear = toneCurveParam->isLinear();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int i, t;
Toshihiro Shimizu 890ddd
	for (i = 0; i < pointsList.size(); i++) {
Toshihiro Shimizu 890ddd
		QList<tpointd> &points = pointsList[i];</tpointd>
Toshihiro Shimizu 890ddd
		for (t = 0; t < points.size(); t++) {
Toshihiro Shimizu 890ddd
			TPointD &p = points[t];
Toshihiro Shimizu 890ddd
			double &x = p.x;
Toshihiro Shimizu 890ddd
			double &y = p.y;
Toshihiro Shimizu 890ddd
			update_param(x, ras);
Toshihiro Shimizu 890ddd
			update_param(y, ras);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
	std::vector<channel_type> rgbaLut(PIXEL::maxChannelValue + 1);</channel_type>
Shinya Kitaoka 3bfa54
	std::vector<channel_type> rgbLut(PIXEL::maxChannelValue + 1);</channel_type>
Shinya Kitaoka 3bfa54
	std::vector<channel_type> rLut(PIXEL::maxChannelValue + 1);</channel_type>
Shinya Kitaoka 3bfa54
	std::vector<channel_type> gLut(PIXEL::maxChannelValue + 1);</channel_type>
Shinya Kitaoka 3bfa54
	std::vector<channel_type> bLut(PIXEL::maxChannelValue + 1);</channel_type>
Shinya Kitaoka 3bfa54
	std::vector<channel_type> aLut(PIXEL::maxChannelValue + 1);</channel_type>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	fill_lut<pixel, channel_type="">(pointsList[0], rgbaLut, isLinear);</pixel,>
Toshihiro Shimizu 890ddd
	fill_lut<pixel, channel_type="">(pointsList[1], rgbLut, isLinear);</pixel,>
Toshihiro Shimizu 890ddd
	fill_lut<pixel, channel_type="">(pointsList[2], rLut, isLinear);</pixel,>
Toshihiro Shimizu 890ddd
	fill_lut<pixel, channel_type="">(pointsList[3], gLut, isLinear);</pixel,>
Toshihiro Shimizu 890ddd
	fill_lut<pixel, channel_type="">(pointsList[4], bLut, isLinear);</pixel,>
Toshihiro Shimizu 890ddd
	fill_lut<pixel, channel_type="">(pointsList[5], aLut, isLinear);</pixel,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int lx = ras->getLx();
Toshihiro Shimizu 890ddd
	int ly = ras->getLy();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int j;
Toshihiro Shimizu 890ddd
	ras->lock();
Toshihiro Shimizu 890ddd
	for (j = 0; j < ly; j++) {
Toshihiro Shimizu 890ddd
		PIXEL *pix = ras->pixels(j);
Toshihiro Shimizu 890ddd
		PIXEL *endPix = pix + lx;
Toshihiro Shimizu 890ddd
		while (pix < endPix) {
Toshihiro Shimizu 890ddd
			if (pix->m != 0 && pix->m != PIXEL::maxChannelValue)
Toshihiro Shimizu 890ddd
				*pix = depremultiply(*pix);
Toshihiro Shimizu 890ddd
			pix->r = rLut[(int)(pix->r)];
Toshihiro Shimizu 890ddd
			pix->g = gLut[(int)(pix->g)];
Toshihiro Shimizu 890ddd
			pix->b = bLut[(int)(pix->b)];
Toshihiro Shimizu 890ddd
			pix->m = aLut[(int)(pix->m)];
Toshihiro Shimizu 890ddd
			pix->r = rgbLut[(int)(pix->r)];
Toshihiro Shimizu 890ddd
			pix->g = rgbLut[(int)(pix->g)];
Toshihiro Shimizu 890ddd
			pix->b = rgbLut[(int)(pix->b)];
Toshihiro Shimizu 890ddd
			pix->r = rgbaLut[(int)(pix->r)];
Toshihiro Shimizu 890ddd
			pix->g = rgbaLut[(int)(pix->g)];
Toshihiro Shimizu 890ddd
			pix->b = rgbaLut[(int)(pix->b)];
Toshihiro Shimizu 890ddd
			pix->m = rgbaLut[(int)(pix->m)];
Toshihiro Shimizu 890ddd
			if (pix->m != 0 && pix->m != PIXEL::maxChannelValue)
Toshihiro Shimizu 890ddd
				*pix = premultiply(*pix);
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 ToneCurveFx::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
	TRaster32P raster32 = tile.getRaster();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (raster32)
Toshihiro Shimizu 890ddd
		doToneCurveFx<tpixel32, uchar="">(raster32, frame, m_toneCurve.getPointer());</tpixel32,>
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		TRaster64P raster64 = tile.getRaster();
Toshihiro Shimizu 890ddd
		if (raster64)
Toshihiro Shimizu 890ddd
			doToneCurveFx<tpixel64, ushort="">(raster64, frame, m_toneCurve.getPointer());</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(ToneCurveFx, "toneCurveFx");