Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "stdfx.h"
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
template <typename pixel="" t,="" typename=""></typename>
Shinya Kitaoka 3bfa54
void prepare_lut(int levels, std::vector<t> &lut)</t>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i, j;
Toshihiro Shimizu 890ddd
	int valuestep = PIXEL::maxChannelValue / (levels - 1);
Toshihiro Shimizu 890ddd
	int step = PIXEL::maxChannelValue / levels;
Toshihiro Shimizu 890ddd
	for (j = 0; j < levels; j++)
Toshihiro Shimizu 890ddd
		for (i = 0; i <= step; i++)
Toshihiro Shimizu 890ddd
			lut[i + j * step] = j * valuestep;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class PosterizeFx : public TStandardRasterFx
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FX_PLUGIN_DECLARATION(PosterizeFx)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterFxPort m_input;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_levels;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	PosterizeFx()
Toshihiro Shimizu 890ddd
		: m_levels(7.0)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		bindParam(this, "levels", m_levels);
Toshihiro Shimizu 890ddd
		addInputPort("Source", m_input);
Toshihiro Shimizu 890ddd
		m_levels->setValueRange(2.0, 10.0);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	~PosterizeFx(){};
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
			bool ret = m_input->doGetBBox(frame, bBox, info);
Toshihiro Shimizu 890ddd
			return ret;
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
	bool canHandle(const TRenderSettings &info, double frame) { return true; }
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 doPosterize(TRasterPT<pixel> ras, int levels)</pixel>
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	std::vector<channel_type> solarize_lut(PIXEL::maxChannelValue + 1);</channel_type>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	prepare_lut<channel_type, pixel="">(levels, solarize_lut);</channel_type,>
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
			pix->r = (CHANNEL_TYPE)(solarize_lut[(int)(pix->r)]);
Toshihiro Shimizu 890ddd
			pix->g = (CHANNEL_TYPE)(solarize_lut[(int)(pix->g)]);
Toshihiro Shimizu 890ddd
			pix->b = (CHANNEL_TYPE)(solarize_lut[(int)(pix->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 PosterizeFx::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
	int levels = (int)m_levels->getValue(frame);
Toshihiro Shimizu 890ddd
	TRaster32P raster32 = tile.getRaster();
Toshihiro Shimizu 890ddd
	if (raster32)
Toshihiro Shimizu 890ddd
		doPosterize<tpixel32, uchar="">(raster32, levels);</tpixel32,>
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		TRaster64P raster64 = tile.getRaster();
Toshihiro Shimizu 890ddd
		if (raster64)
Toshihiro Shimizu 890ddd
			doPosterize<tpixel64, ushort="">(raster64, levels);</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(PosterizeFx, "posterizeFx");