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
Toshihiro Shimizu 890ddd
class Bright_ContFx : public TStandardRasterFx
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FX_PLUGIN_DECLARATION(Bright_ContFx)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterFxPort m_input;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_bright;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_contrast;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	Bright_ContFx()
Toshihiro Shimizu 890ddd
		: m_bright(0.0), m_contrast(0.0)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		bindParam(this, "brightness", m_bright);
Toshihiro Shimizu 890ddd
		bindParam(this, "contrast", m_contrast);
Toshihiro Shimizu 890ddd
		m_bright->setValueRange(-127, 127);
Toshihiro Shimizu 890ddd
		m_contrast->setValueRange(-127, 127);
Toshihiro Shimizu 890ddd
		addInputPort("Source", m_input);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	~Bright_ContFx(){};
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
			bBox = TRectD();
Toshihiro Shimizu 890ddd
			return false;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		return m_input->doGetBBox(frame, bBox, info);
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void doCompute(TTile &tile, double frame, const TRenderSettings &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename pixel,="" t="" typename=""></typename>
Shinya Kitaoka 3bfa54
void my_compute_lut(double contrast, double brightness, std::vector<t> &lut)</t>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	double value, nvalue, power;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int half_maxChannelValue = tfloor(PIXEL::maxChannelValue / 2.0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int lutSize = PIXEL::maxChannelValue + 1;
Toshihiro Shimizu 890ddd
	for (i = 0; i < lutSize; i++) {
Toshihiro Shimizu 890ddd
		value = i / double(PIXEL::maxChannelValue);
Toshihiro Shimizu 890ddd
		/*brightness*/
Toshihiro Shimizu 890ddd
		if (brightness < 0.0)
Toshihiro Shimizu 890ddd
			value = value * (1.0 + brightness);
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			value = value + ((1.0 - value) * brightness);
Toshihiro Shimizu 890ddd
		/*contrast*/
Toshihiro Shimizu 890ddd
		if (contrast < 0.0) {
Toshihiro Shimizu 890ddd
			if (value > 0.5)
Toshihiro Shimizu 890ddd
				nvalue = 1.0 - value;
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				nvalue = value;
Toshihiro Shimizu 890ddd
			if (nvalue < 0.0)
Toshihiro Shimizu 890ddd
				nvalue = 0.0;
Toshihiro Shimizu 890ddd
			nvalue = 0.5 * pow(nvalue * 2.0, (double)(1.0 + contrast));
Toshihiro Shimizu 890ddd
			if (value > 0.5)
Toshihiro Shimizu 890ddd
				value = 1.0 - nvalue;
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				value = nvalue;
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			if (value > 0.5)
Toshihiro Shimizu 890ddd
				nvalue = 1.0 - value;
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				nvalue = value;
Toshihiro Shimizu 890ddd
			if (nvalue < 0.0)
Toshihiro Shimizu 890ddd
				nvalue = 0.0;
Toshihiro Shimizu 890ddd
			power = (contrast == 1.0) ? half_maxChannelValue : 1.0 / (1.0 - contrast);
Toshihiro Shimizu 890ddd
			nvalue = 0.5 * pow(2.0 * nvalue, power);
Toshihiro Shimizu 890ddd
			if (value > 0.5)
Toshihiro Shimizu 890ddd
				value = 1.0 - nvalue;
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				value = nvalue;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		lut[i] = (int)(value * PIXEL::maxChannelValue);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename channel_type="" pixel,="" typename=""></typename>
Toshihiro Shimizu 890ddd
void doBrightnessContrast(TRasterPT<pixel> ras, double contrast, double brightness)</pixel>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int lx = ras->getLx();
Toshihiro Shimizu 890ddd
	int ly = ras->getLy();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
	std::vector<channel_type> lut(PIXEL::maxChannelValue + 1);</channel_type>
Toshihiro Shimizu 890ddd
	my_compute_lut<pixel, channel_type="">(contrast, brightness, lut);</pixel,>
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) {
Toshihiro Shimizu 890ddd
				*pix = depremultiply(*pix);
Toshihiro Shimizu 890ddd
				pix->r = lut[pix->r];
Toshihiro Shimizu 890ddd
				pix->g = lut[pix->g];
Toshihiro Shimizu 890ddd
				pix->b = lut[pix->b];
Toshihiro Shimizu 890ddd
				pix->m = pix->m;
Toshihiro Shimizu 890ddd
				*pix = premultiply(*pix);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			pix++;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	ras->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void Bright_ContFx::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 brightness = m_bright->getValue(frame) / 127.0;
Toshihiro Shimizu 890ddd
	double contrast = m_contrast->getValue(frame) / 127.0;
Toshihiro Shimizu 890ddd
	if (contrast > 1)
Toshihiro Shimizu 890ddd
		contrast = 1;
Toshihiro Shimizu 890ddd
	if (contrast < -1)
Toshihiro Shimizu 890ddd
		contrast = -1;
Toshihiro Shimizu 890ddd
	TRaster32P raster32 = tile.getRaster();
Toshihiro Shimizu 890ddd
	if (raster32)
Toshihiro Shimizu 890ddd
		doBrightnessContrast<tpixel32, uchar="">(raster32, contrast, brightness);</tpixel32,>
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		TRaster64P raster64 = tile.getRaster();
Toshihiro Shimizu 890ddd
		if (raster64)
Toshihiro Shimizu 890ddd
			doBrightnessContrast<tpixel64, ushort="">(raster64, contrast, brightness);</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(Bright_ContFx, "brightContFx")