Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "stdfx.h"
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
#include "tspectrumparam.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class MultiToneFx : public TStandardRasterFx
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FX_PLUGIN_DECLARATION(MultiToneFx)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterFxPort m_input;
Toshihiro Shimizu 890ddd
	TSpectrumParamP m_colors;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	MultiToneFx()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TSpectrum::ColorKey colors[] = {
Toshihiro Shimizu 890ddd
			TSpectrum::ColorKey(0, TPixel32::White),
Toshihiro Shimizu 890ddd
			TSpectrum::ColorKey(0.5, TPixel32::Yellow),
Toshihiro Shimizu 890ddd
			TSpectrum::ColorKey(1, TPixel32::Red)};
Toshihiro Shimizu 890ddd
		m_colors = TSpectrumParamP(tArrayCount(colors), colors);
Toshihiro Shimizu 890ddd
		bool ret = m_colors->isKeyframe(0);
Toshihiro Shimizu 890ddd
		bindParam(this, "colors", m_colors);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		ret = getParams()->getParam(0)->isKeyframe(0);
Toshihiro Shimizu 890ddd
		addInputPort("Source", m_input);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	~MultiToneFx(){};
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 &);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool canHandle(const TRenderSettings &info, double frame) { return true; }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename channel_type="" pixel,="" pixelgray,="" typename=""></typename>
Toshihiro Shimizu 890ddd
void doMultiTone(const TRasterPT<pixel> &ras, const TSpectrumT<pixel> &spectrum)</pixel></pixel>
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
			if (pix->m) {
Toshihiro Shimizu 890ddd
				CHANNEL_TYPE reference;
Toshihiro Shimizu 890ddd
				double s;
Toshihiro Shimizu 890ddd
				reference = PIXELGRAY::from(*pix).value;
Toshihiro Shimizu 890ddd
				s = reference / aux;
Toshihiro Shimizu 890ddd
				if (pix->m == aux) {
Toshihiro Shimizu 890ddd
					*pix = spectrum.getPremultipliedValue(s);
Toshihiro Shimizu 890ddd
				} else {
Toshihiro Shimizu 890ddd
					PIXEL tmp = spectrum.getPremultipliedValue(s);
Toshihiro Shimizu 890ddd
					s = pix->m / aux;
Toshihiro Shimizu 890ddd
					tmp.r = (CHANNEL_TYPE)(tmp.r * s);
Toshihiro Shimizu 890ddd
					tmp.g = (CHANNEL_TYPE)(tmp.g * s);
Toshihiro Shimizu 890ddd
					tmp.b = (CHANNEL_TYPE)(tmp.b * s);
Toshihiro Shimizu 890ddd
					tmp.m = (CHANNEL_TYPE)(tmp.m * s);
Toshihiro Shimizu 890ddd
					*pix = tmp;
Toshihiro Shimizu 890ddd
				}
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
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void MultiToneFx::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
	if (TRaster32P raster32 = tile.getRaster())
Toshihiro Shimizu 890ddd
		doMultiTone<tpixel32, tpixelgr8,="" uchar="">(</tpixel32,>
Toshihiro Shimizu 890ddd
			raster32, m_colors->getValue(frame));
Toshihiro Shimizu 890ddd
	else if (TRaster64P raster64 = tile.getRaster())
Toshihiro Shimizu 890ddd
		doMultiTone<tpixel64, tpixelgr16,="" ushort="">(</tpixel64,>
Toshihiro Shimizu 890ddd
			raster64, m_colors->getValue64(frame));
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		throw TException("MultiToneFx: unsupported Pixel Type");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(MultiToneFx, "multiToneFx")