Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "texception.h"
Toshihiro Shimizu 890ddd
#include "stdfx.h"
Toshihiro Shimizu 890ddd
#include "tpixelutils.h"
Toshihiro Shimizu 890ddd
#include "tbasefx.h"
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
#include "trop.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tparamset.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void backlit(TRaster32P lighted, TRaster32P light, TRaster32P out, double blur, const TPixel &color,
Toshihiro Shimizu 890ddd
			 double fade, double scale)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(light && lighted && out);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(lighted->getSize() == light->getSize());
Toshihiro Shimizu 890ddd
	assert(lighted->getSize() == out->getSize());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int j;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (fade > 0.0) {
Toshihiro Shimizu 890ddd
		assert(fade <= 1.0);
Toshihiro Shimizu 890ddd
		light->lock();
Toshihiro Shimizu 890ddd
		for (j = 0; j < light->getLy(); j++) {
Toshihiro Shimizu 890ddd
			TPixel *pix = light->pixels(j);
Toshihiro Shimizu 890ddd
			TPixel *endPix = pix + light->getLx();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			while (pix < endPix) {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if (pix->m > 0) {
Toshihiro Shimizu 890ddd
					if (pix->m == 255) {
Toshihiro Shimizu 890ddd
						pix->r = troundp(pix->r + fade * (color.r - pix->r));
Toshihiro Shimizu 890ddd
						pix->g = troundp(pix->g + fade * (color.g - pix->g));
Toshihiro Shimizu 890ddd
						pix->b = troundp(pix->b + fade * (color.b - pix->b));
Toshihiro Shimizu 890ddd
						pix->m = troundp(pix->m + fade * (color.m - pix->m));
Toshihiro Shimizu 890ddd
					} else {
Toshihiro Shimizu 890ddd
						int val;
Toshihiro Shimizu 890ddd
						double factor = pix->m / 255.0;
Toshihiro Shimizu 890ddd
						val = troundp(pix->r + fade * (color.r * factor - pix->r));
Toshihiro Shimizu 890ddd
						pix->r = (val > 255) ? 255 : val;
Toshihiro Shimizu 890ddd
						val = troundp(pix->g + fade * (color.g * factor - pix->g));
Toshihiro Shimizu 890ddd
						pix->g = (val > 255) ? 255 : val;
Toshihiro Shimizu 890ddd
						val = troundp(pix->b + fade * (color.b * factor - pix->b));
Toshihiro Shimizu 890ddd
						pix->b = (val > 255) ? 255 : val;
Toshihiro Shimizu 890ddd
						val = troundp(pix->m + fade * (color.m * factor - pix->m));
Toshihiro Shimizu 890ddd
						pix->m = (val > 255) ? 255 : val;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				++pix;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		light->unlock();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRop::ropout(light, lighted, light);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (blur > 0) {
Toshihiro Shimizu 890ddd
		double _blur = blur * scale;
Toshihiro Shimizu 890ddd
		int bi = tceil(_blur);
Toshihiro Shimizu 890ddd
		TRaster32P ras(light->getLx() + bi, light->getLy() + bi);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TRect rect(light->getBounds() + TPoint(bi, bi));
Toshihiro Shimizu 890ddd
		TRop::copy(ras->extract(rect), light);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TRop::blur(out, ras, _blur, -bi, -bi, true);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		double ampl = 1.0 + blur / 15.0;
Toshihiro Shimizu 890ddd
		out->lock();
Toshihiro Shimizu 890ddd
		for (j = 0; j < out->getLy(); j++) {
Toshihiro Shimizu 890ddd
			TPixel *pix = out->pixels(j);
Toshihiro Shimizu 890ddd
			TPixel *endPix = pix + out->getLx();
Toshihiro Shimizu 890ddd
			while (pix < endPix) {
Toshihiro Shimizu 890ddd
				if (pix->m != 0) {
Toshihiro Shimizu 890ddd
					int val = troundp(pix->r * ampl);
Toshihiro Shimizu 890ddd
					pix->r = (val > 204) ? 204 : val;
Toshihiro Shimizu 890ddd
					val = troundp(pix->g * ampl);
Toshihiro Shimizu 890ddd
					pix->g = (val > 204) ? 204 : val;
Toshihiro Shimizu 890ddd
					val = troundp(pix->b * ampl);
Toshihiro Shimizu 890ddd
					pix->b = (val > 204) ? 204 : val;
Toshihiro Shimizu 890ddd
					val = troundp(pix->m * ampl);
Toshihiro Shimizu 890ddd
					pix->m = (val > 204) ? 204 : val;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				pix++;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		out->unlock();
Toshihiro Shimizu 890ddd
		TRop::over(out, lighted, out);
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		TRop::over(out, lighted, light);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void backlit(TRaster64P lighted, TRaster64P light, TRaster64P out, double blur, const TPixel &color32, double fade, double scale)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(light && lighted && out);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(lighted->getSize() == light->getSize());
Toshihiro Shimizu 890ddd
	assert(lighted->getSize() == out->getSize());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int j;
Toshihiro Shimizu 890ddd
	TPixel64 color = toPixel64(color32);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (fade > 0.0) {
Toshihiro Shimizu 890ddd
		assert(fade <= 1.0);
Toshihiro Shimizu 890ddd
		light->lock();
Toshihiro Shimizu 890ddd
		for (j = 0; j < light->getLy(); j++) {
Toshihiro Shimizu 890ddd
			TPixel64 *pix = light->pixels(j);
Toshihiro Shimizu 890ddd
			TPixel64 *endPix = pix + light->getLx();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			while (pix < endPix) {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if (pix->m > 0) {
Toshihiro Shimizu 890ddd
					if (pix->m == 65535) {
Toshihiro Shimizu 890ddd
						pix->r = troundp(pix->r + fade * (color.r - pix->r));
Toshihiro Shimizu 890ddd
						pix->g = troundp(pix->g + fade * (color.g - pix->g));
Toshihiro Shimizu 890ddd
						pix->b = troundp(pix->b + fade * (color.b - pix->b));
Toshihiro Shimizu 890ddd
						pix->m = troundp(pix->m + fade * (color.m - pix->m));
Toshihiro Shimizu 890ddd
					} else {
Toshihiro Shimizu 890ddd
						int val;
Toshihiro Shimizu 890ddd
						double factor = pix->m / 65535.0;
Toshihiro Shimizu 890ddd
						val = troundp(pix->r + fade * (color.r * factor - pix->r));
Toshihiro Shimizu 890ddd
						pix->r = (val > 65535) ? 65535 : val;
Toshihiro Shimizu 890ddd
						val = troundp(pix->g + fade * (color.g * factor - pix->g));
Toshihiro Shimizu 890ddd
						pix->g = (val > 65535) ? 65535 : val;
Toshihiro Shimizu 890ddd
						val = troundp(pix->b + fade * (color.b * factor - pix->b));
Toshihiro Shimizu 890ddd
						pix->b = (val > 65535) ? 65535 : val;
Toshihiro Shimizu 890ddd
						val = troundp(pix->m + fade * (color.m * factor - pix->m));
Toshihiro Shimizu 890ddd
						pix->m = (val > 65535) ? 65535 : val;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				++pix;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		light->unlock();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRop::ropout(light, lighted, light);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (blur > 0) {
Toshihiro Shimizu 890ddd
		double _blur = blur * scale;
Toshihiro Shimizu 890ddd
		int bi = tceil(_blur);
Toshihiro Shimizu 890ddd
		TRaster64P ras(light->getLx() + bi, light->getLy() + bi);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TRect rect(light->getBounds() + TPoint(bi, bi));
Toshihiro Shimizu 890ddd
		TRop::copy(ras->extract(rect), light);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TRop::blur(out, ras, _blur, -bi, -bi, false);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		double ampl = 1.0 + blur / 15.0;
Toshihiro Shimizu 890ddd
		out->lock();
Toshihiro Shimizu 890ddd
		int fac = (204 << 8) | 204;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (j = 0; j < out->getLy(); j++) {
Toshihiro Shimizu 890ddd
			TPixel64 *pix = out->pixels(j);
Toshihiro Shimizu 890ddd
			TPixel64 *endPix = pix + out->getLx();
Toshihiro Shimizu 890ddd
			while (pix < endPix) {
Toshihiro Shimizu 890ddd
				if (pix->m != 0) {
Toshihiro Shimizu 890ddd
					int val = troundp(pix->r * ampl);
Toshihiro Shimizu 890ddd
					pix->r = (val > fac) ? fac : val;
Toshihiro Shimizu 890ddd
					val = troundp(pix->g * ampl);
Toshihiro Shimizu 890ddd
					pix->g = (val > fac) ? fac : val;
Toshihiro Shimizu 890ddd
					val = troundp(pix->b * ampl);
Toshihiro Shimizu 890ddd
					pix->b = (val > fac) ? fac : val;
Toshihiro Shimizu 890ddd
					val = troundp(pix->m * ampl);
Toshihiro Shimizu 890ddd
					pix->m = (val > fac) ? fac : val;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				pix++;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		out->unlock();
Toshihiro Shimizu 890ddd
		TRop::over(out, lighted, out);
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		TRop::over(out, lighted, light);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class BacklitFx : public TBaseRasterFx
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FX_DECLARATION(BacklitFx)
Toshihiro Shimizu 890ddd
	TRasterFxPort m_lighted, m_light;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_value;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_fade;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPixelParamP m_color;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	BacklitFx() : m_value(0.0), m_color(TPixel::White), m_fade(0.0)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_color->enableMatte(true);
Toshihiro Shimizu 890ddd
		m_value->setValueRange(0, (std::numeric_limits<double>::max)());</double>
Toshihiro Shimizu 890ddd
		m_fade->setValueRange(0.0, 100.0);
Toshihiro Shimizu 890ddd
		bindParam(this, "value", m_value);
Toshihiro Shimizu 890ddd
		bindParam(this, "color", m_color);
Toshihiro Shimizu 890ddd
		bindParam(this, "fade", m_fade);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		addInputPort("Light", m_light);
Toshihiro Shimizu 890ddd
		addInputPort("Source", m_lighted);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	~BacklitFx() {}
Toshihiro Shimizu 890ddd
	bool doGetBBox(double frame, TRectD &bbox, const TRenderSettings &info)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if (getActiveTimeRegion().contains(frame))
Toshihiro Shimizu 890ddd
			if (m_light.isConnected()) {
Toshihiro Shimizu 890ddd
				if (m_lighted.isConnected()) {
Toshihiro Shimizu 890ddd
					TRectD b0, b1;
Toshihiro Shimizu 890ddd
					bool ret = m_light->doGetBBox(frame, b0, info);
Toshihiro Shimizu 890ddd
					ret = ret && m_lighted->doGetBBox(frame, b1, info);
Toshihiro Shimizu 890ddd
					bbox = b0.enlarge(tceil(m_value->getValue(frame))) + b1;
Toshihiro Shimizu 890ddd
					return ret;
Toshihiro Shimizu 890ddd
				} else
Toshihiro Shimizu 890ddd
					return m_light->doGetBBox(frame, bbox, info);
Toshihiro Shimizu 890ddd
			} else if (m_lighted.isConnected())
Toshihiro Shimizu 890ddd
				return m_lighted->doGetBBox(frame, bbox, info);
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void doDryCompute(TRectD &rect, double frame, const TRenderSettings &info)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if (!m_light.isConnected())
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
		if (!m_lighted.isConnected()) {
Toshihiro Shimizu 890ddd
			m_light->dryCompute(rect, frame, info);
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		double value = m_value->getValue(frame);
Toshihiro Shimizu 890ddd
		double scale = sqrt(fabs(info.m_affine.det()));
Toshihiro Shimizu 890ddd
		int brad = tceil(value * scale);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TRectD inRect = rect.enlarge(brad);
Toshihiro Shimizu 890ddd
		inRect = TRectD(tfloor(inRect.x0), tfloor(inRect.y0), tceil(inRect.x1), tceil(inRect.y1));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		m_lighted->dryCompute(inRect, frame, info);
Toshihiro Shimizu 890ddd
		m_light->dryCompute(inRect, frame, info);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void doCompute(TTile &tile, double frame, const TRenderSettings &ri)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if (!m_light.isConnected())
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (!m_lighted.isConnected()) {
Toshihiro Shimizu 890ddd
			m_light->compute(tile, frame, ri);
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		double value = m_value->getValue(frame);
Toshihiro Shimizu 890ddd
		double scale = sqrt(fabs(ri.m_affine.det()));
Toshihiro Shimizu 890ddd
		int brad = tceil(value * scale);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TDimension tileSize(tile.getRaster()->getSize());
Toshihiro Shimizu 890ddd
		TRectD rect(tile.m_pos, TDimensionD(tileSize.lx, tileSize.ly));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//TRectD rect(tile.m_pos,convert(tile.getRaster()->getBounds().getP11())+tile.m_pos);
Toshihiro Shimizu 890ddd
		rect = rect.enlarge(brad);
Toshihiro Shimizu 890ddd
		TRect rectI(tfloor(rect.x0), tfloor(rect.y0), tceil(rect.x1) - 1, tceil(rect.y1) - 1);
Toshihiro Shimizu 890ddd
		//m_light->compute(tile, frame, ri);
Toshihiro Shimizu 890ddd
		TTile srcTile;
Toshihiro Shimizu 890ddd
		m_lighted->allocateAndCompute(srcTile, rect.getP00(), rectI.getSize(), tile.getRaster(), frame, ri);
Toshihiro Shimizu 890ddd
		TTile ctrTile;
Toshihiro Shimizu 890ddd
		m_light->allocateAndCompute(ctrTile, rect.getP00(), rectI.getSize(), tile.getRaster(), frame, ri);
Toshihiro Shimizu 890ddd
		if ((TRaster32P)srcTile.getRaster() && (TRaster32P)tile.getRaster())
Toshihiro Shimizu 890ddd
			backlit((TRaster32P)srcTile.getRaster(), (TRaster32P)ctrTile.getRaster(), (TRaster32P)ctrTile.getRaster(),
Toshihiro Shimizu 890ddd
					value, m_color->getValue(frame), m_fade->getValue(frame) / 100.0, scale);
Toshihiro Shimizu 890ddd
		else if ((TRaster64P)srcTile.getRaster() && (TRaster64P)tile.getRaster())
Toshihiro Shimizu 890ddd
			backlit((TRaster64P)srcTile.getRaster(), (TRaster64P)ctrTile.getRaster(), (TRaster64P)ctrTile.getRaster(),
Toshihiro Shimizu 890ddd
					value, m_color->getValue(frame), m_fade->getValue(frame) / 100.0, scale);
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			throw TRopException("TRop::max invalid raster combination");
Toshihiro Shimizu 890ddd
		tile.getRaster()->copy(ctrTile.getRaster(), TPoint(-brad, -brad));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool canHandle(const TRenderSettings &info, double frame) { return true; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int getMemoryRequirement(const TRectD &rect, double frame, const TRenderSettings &info)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		double value = m_value->getValue(frame);
Toshihiro Shimizu 890ddd
		double scale = sqrt(fabs(info.m_affine.det()));
Toshihiro Shimizu 890ddd
		int brad = tceil(value * scale);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		return TRasterFx::memorySize(rect.enlarge(brad), info.m_bpp);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(BacklitFx, "backlitFx")
Toshihiro Shimizu 890ddd
//==================================================================