Toshihiro Shimizu 890ddd
#include <sstream> /* std::ostringstream */</sstream>
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
#include "stdfx.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "ino_common.h"
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
class ino_median : public TStandardRasterFx
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FX_PLUGIN_DECLARATION(ino_median)
Toshihiro Shimizu 890ddd
	TRasterFxPort m_input;
Toshihiro Shimizu 890ddd
	TRasterFxPort m_refer;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TDoubleParamP m_radius;
Toshihiro Shimizu 890ddd
	TIntEnumParamP m_channel;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TIntEnumParamP m_ref_mode;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	ino_median()
Toshihiro Shimizu 890ddd
		: m_radius(0.35 * 640. / 12. / 25.4)
Toshihiro Shimizu 890ddd
		  /*	= 0.73490813648293963254
Toshihiro Shimizu 890ddd
			0.35	= mm = ユーザ指定の初期値
Toshihiro Shimizu 890ddd
			640/12	= 53.33333 = toonz独自の係数 =tunit.cpp参照
Toshihiro Shimizu 890ddd
			25.4	= mm/inch
Toshihiro Shimizu 890ddd
		*/
Toshihiro Shimizu 890ddd
		  ,
Toshihiro Shimizu 890ddd
		  m_channel(new TIntEnumParam()), m_ref_mode(new TIntEnumParam())
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		this->m_radius->setMeasureName("fxLength");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		addInputPort("Source", this->m_input);
Toshihiro Shimizu 890ddd
		addInputPort("Reference", this->m_refer);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		bindParam(this, "radius", this->m_radius);
Toshihiro Shimizu 890ddd
		bindParam(this, "channel", this->m_channel);
Toshihiro Shimizu 890ddd
		bindParam(this, "reference", this->m_ref_mode);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		this->m_radius->setValueRange(0, 100.0 * 640. / 12. / 25.4);
Toshihiro Shimizu 890ddd
		/* 最小値は0mm,最大値は100mm */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		this->m_channel->addItem(0, "Red");
Toshihiro Shimizu 890ddd
		this->m_channel->addItem(1, "Green");
Toshihiro Shimizu 890ddd
		this->m_channel->addItem(2, "Blue");
Toshihiro Shimizu 890ddd
		this->m_channel->addItem(3, "Alpha");
Toshihiro Shimizu 890ddd
		this->m_channel->addItem(4, "All");
Toshihiro Shimizu 890ddd
		this->m_channel->setDefaultValue(4);
Toshihiro Shimizu 890ddd
		this->m_channel->setValue(4);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		this->m_ref_mode->addItem(0, "Red");
Toshihiro Shimizu 890ddd
		this->m_ref_mode->addItem(1, "Green");
Toshihiro Shimizu 890ddd
		this->m_ref_mode->addItem(2, "Blue");
Toshihiro Shimizu 890ddd
		this->m_ref_mode->addItem(3, "Alpha");
Toshihiro Shimizu 890ddd
		this->m_ref_mode->addItem(4, "Luminance");
Toshihiro Shimizu 890ddd
		this->m_ref_mode->addItem(-1, "Nothing");
Toshihiro Shimizu 890ddd
		this->m_ref_mode->setDefaultValue(0);
Toshihiro Shimizu 890ddd
		this->m_ref_mode->setValue(0);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	bool doGetBBox(
Toshihiro Shimizu 890ddd
		double frame, TRectD &bBox, const TRenderSettings &info)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if (this->m_input.isConnected()) {
Toshihiro Shimizu 890ddd
			const bool ret = this->m_input->doGetBBox(frame, bBox, info);
Toshihiro Shimizu 890ddd
			const double margin = ceil(this->m_radius->getValue(frame));
Toshihiro Shimizu 890ddd
			if (0.0 < margin) {
Toshihiro Shimizu 890ddd
				bBox = bBox.enlarge(margin);
Toshihiro Shimizu 890ddd
			}
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
	bool canHandle(const TRenderSettings &rend_sets, double frame)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return true;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	int getMemoryRequirement(
Toshihiro Shimizu 890ddd
		const TRectD &rect, double frame, const TRenderSettings &info)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		const double radius = this->m_radius->getValue(frame);
Toshihiro Shimizu 890ddd
		return TRasterFx::memorySize(
Toshihiro Shimizu 890ddd
			rect.enlarge(ceil(radius) + 0.5), info.m_bpp);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void doCompute(
Toshihiro Shimizu 890ddd
		TTile &tile, double frame, const TRenderSettings &rend_sets);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(ino_median, "inoMedianFx")
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
#include "igs_median_filter_smooth.h"
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
void fx_(
Toshihiro Shimizu 890ddd
	const TRasterP in_ras // with margin
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const int margin, TRasterP out_ras // no margin
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const TRasterP refer_ras, const int ref_mode
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double radius, const int channel)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRasterGR8P out_gr8(
Toshihiro Shimizu 890ddd
		in_ras->getLy(), in_ras->getLx() * ino::channels() *
Toshihiro Shimizu 890ddd
							 ((TRaster64P)in_ras ? sizeof(unsigned short) : sizeof(unsigned char)));
Toshihiro Shimizu 890ddd
	out_gr8->lock();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	igs::median_filter_smooth::convert(
Toshihiro Shimizu 890ddd
		in_ras->getRawData() // BGRA
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		out_gr8->getRawData()
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			,
Toshihiro Shimizu 890ddd
		in_ras->getLy(), in_ras->getLx(), ino::channels(), ino::bits(in_ras)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
															   ,
Toshihiro Shimizu 890ddd
		(((0 <= ref_mode) && refer_ras) ? refer_ras->getRawData() : 0) // BGRA
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		(((0 <= ref_mode) && refer_ras) ? ino::bits(refer_ras) : 0), ref_mode
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		channel, radius, 0 /* 0=Spread:外は淵のピクセル値が続いているとする */
Toshihiro Shimizu 890ddd
		);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	ino::arr_to_ras(
Toshihiro Shimizu 890ddd
		out_gr8->getRawData(), ino::channels(), out_ras, margin);
Toshihiro Shimizu 890ddd
	out_gr8->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
void ino_median::doCompute(
Toshihiro Shimizu 890ddd
	TTile &tile, double frame, const TRenderSettings &rend_sets)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	/* ------ 接続していなければ処理しない -------------------- */
Toshihiro Shimizu 890ddd
	if (!this->m_input.isConnected()) {
Toshihiro Shimizu 890ddd
		tile.getRaster()->clear(); /* 塗りつぶしクリア */
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* ------ サポートしていないPixelタイプはエラーを投げる --- */
Toshihiro Shimizu 890ddd
	if (!((TRaster32P)tile.getRaster()) &&
Toshihiro Shimizu 890ddd
		!((TRaster64P)tile.getRaster())) {
Toshihiro Shimizu 890ddd
		throw TRopException("unsupported input pixel type");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* ------ 単位変換用スケーリング --------------------------
Toshihiro Shimizu 890ddd
	外積(面積)
Toshihiro Shimizu 890ddd
		rend_sets.m_affine.det() = a11*a22-a12*a21 // double
Toshihiro Shimizu 890ddd
	シュリンク値
Toshihiro Shimizu 890ddd
		rend_sets.m_shrinkX // int
Toshihiro Shimizu 890ddd
		rend_sets.m_shrinkY // int
Toshihiro Shimizu 890ddd
	*/
Toshihiro Shimizu 890ddd
	const double mm2scale_shrink_pixel =
Toshihiro Shimizu 890ddd
		ino::pixel_per_mm() * sqrt(fabs(rend_sets.m_affine.det())) / ((rend_sets.m_shrinkX + rend_sets.m_shrinkY) / 2.0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* ------ Pixel単位で動作パラメータを得る ----------------- */
Toshihiro Shimizu 890ddd
	const double radius = this->m_radius->getValue(frame) * mm2scale_shrink_pixel;
Toshihiro Shimizu 890ddd
	/* 動作パラメータを得る */
Toshihiro Shimizu 890ddd
	const int channel = this->m_channel->getValue();
Toshihiro Shimizu 890ddd
	const int ref_mode = this->m_ref_mode->getValue();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* ------ 参照マージン含めた画像生成 ---------------------- */
Toshihiro Shimizu 890ddd
	/* Rendering画像のBBox値 --> Pixel単位のdouble値 */
Toshihiro Shimizu 890ddd
	/*	typedef TRectT<double> TRectD;</double>
Toshihiro Shimizu 890ddd
		inline TRectT<double>::TRectT(</double>
Toshihiro Shimizu 890ddd
			const TPointT<double> &bottomLeft</double>
Toshihiro Shimizu 890ddd
			, const TDimensionT<double> &d</double>
Toshihiro Shimizu 890ddd
		)	: x0(bottomLeft.x)
Toshihiro Shimizu 890ddd
			, y0(bottomLeft.y)
Toshihiro Shimizu 890ddd
			, x1(bottomLeft.x+d.lx)
Toshihiro Shimizu 890ddd
			, y1(bottomLeft.y+d.ly)
Toshihiro Shimizu 890ddd
		{}
Toshihiro Shimizu 890ddd
	*/
Toshihiro Shimizu 890ddd
	TRectD enlarge_rect = TRectD(
Toshihiro Shimizu 890ddd
		tile.m_pos /* TPointD */
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		TDimensionD(/* int --> doubleにしてセット */
Toshihiro Shimizu 890ddd
					tile.getRaster()->getLx(), tile.getRaster()->getLy()));
Toshihiro Shimizu 890ddd
	/* BBoxの拡大 Pixel単位 */
Toshihiro Shimizu 890ddd
	const int enlarge_pixel = (int)(ceil(radius) + 0.5);
Toshihiro Shimizu 890ddd
	enlarge_rect = enlarge_rect.enlarge(enlarge_pixel);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*	void TRasterFx::allocateAndCompute(
Toshihiro Shimizu 890ddd
			TTile &tile, 
Toshihiro Shimizu 890ddd
			const TPointD &pos, const TDimension &size, 
Toshihiro Shimizu 890ddd
			const TRasterP &templateRas, double frame,
Toshihiro Shimizu 890ddd
			const TRenderSettings &info
Toshihiro Shimizu 890ddd
		) {
Toshihiro Shimizu 890ddd
			if (templateRas) {
Toshihiro Shimizu 890ddd
		tile.getRaster() = templateRas->create(size.lx, size.ly);
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
		tile.getRaster() = TRaster32P(size.lx, size.ly);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		tile.m_pos = pos;
Toshihiro Shimizu 890ddd
			compute(tile, frame, info);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	*/
Toshihiro Shimizu 890ddd
	TTile enlarge_tile;
Toshihiro Shimizu 890ddd
	this->m_input->allocateAndCompute(
Toshihiro Shimizu 890ddd
		enlarge_tile, enlarge_rect.getP00(), TDimensionI(/* Pixel単位のdouble -> intにしてセット */
Toshihiro Shimizu 890ddd
														 (int)(enlarge_rect.getLx() + 0.5), (int)(enlarge_rect.getLy() + 0.5)),
Toshihiro Shimizu 890ddd
		tile.getRaster(), frame, rend_sets);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*------ 参照画像生成 --------------------------------------*/
Toshihiro Shimizu 890ddd
	TTile ref_tile;
Toshihiro Shimizu 890ddd
	bool ref_sw = false;
Toshihiro Shimizu 890ddd
	if (this->m_refer.isConnected()) {
Toshihiro Shimizu 890ddd
		ref_sw = true;
Toshihiro Shimizu 890ddd
		this->m_refer->allocateAndCompute(
Toshihiro Shimizu 890ddd
			ref_tile, enlarge_tile.m_pos, TDimensionI(/* Pixel単位 */
Toshihiro Shimizu 890ddd
													  enlarge_tile.getRaster()->getLx(), enlarge_tile.getRaster()->getLy()),
Toshihiro Shimizu 890ddd
			enlarge_tile.getRaster(), frame, rend_sets);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	/* ------ 保存すべき画像メモリを塗りつぶしクリア ---------- */
Toshihiro Shimizu 890ddd
	tile.getRaster()->clear(); /* 塗りつぶしクリア */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* ------ (app_begin)log記憶 ------------------------------ */
Toshihiro Shimizu 890ddd
	const bool log_sw = ino::log_enable_sw();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (log_sw) {
Toshihiro Shimizu 890ddd
		std::ostringstream os;
Toshihiro Shimizu 890ddd
		os << "params"
Toshihiro Shimizu 890ddd
		   << "  radius " << radius
Toshihiro Shimizu 890ddd
		   << "  channel " << channel
Toshihiro Shimizu 890ddd
		   << "  ref_mode " << ref_mode
Toshihiro Shimizu 890ddd
		   << "   tile w " << tile.getRaster()->getLx()
Toshihiro Shimizu 890ddd
		   << "  h " << tile.getRaster()->getLy()
Toshihiro Shimizu 890ddd
		   << "  pixbits " << ino::pixel_bits(tile.getRaster())
Toshihiro Shimizu 890ddd
		   << "   frame " << frame
Toshihiro Shimizu 890ddd
		   << "   rand_sets affine_det " << rend_sets.m_affine.det()
Toshihiro Shimizu 890ddd
		   << "  shrink x " << rend_sets.m_shrinkX
Toshihiro Shimizu 890ddd
		   << "  y " << rend_sets.m_shrinkY;
Toshihiro Shimizu 890ddd
		if (ref_sw) {
Toshihiro Shimizu 890ddd
			os
Toshihiro Shimizu 890ddd
				<< "  ref_tile.m_pos " << ref_tile.m_pos
Toshihiro Shimizu 890ddd
				<< "  ref_tile_getLx " << ref_tile.getRaster()->getLx()
Toshihiro Shimizu 890ddd
				<< "  y " << ref_tile.getRaster()->getLy();
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	/* ------ fx処理 ------------------------------------------ */
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		tile.getRaster()->lock();
Toshihiro Shimizu 890ddd
		const TRasterP refer_ras = (ref_sw ? ref_tile.getRaster() : nullptr);
Toshihiro Shimizu 890ddd
		fx_(
Toshihiro Shimizu 890ddd
			enlarge_tile.getRaster() // in with margin
Toshihiro Shimizu 890ddd
			,
Toshihiro Shimizu 890ddd
			enlarge_pixel // margin
Toshihiro Shimizu 890ddd
			,
Toshihiro Shimizu 890ddd
			tile.getRaster() // out with no margin
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			,
Toshihiro Shimizu 890ddd
			refer_ras, ref_mode
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			,
Toshihiro Shimizu 890ddd
			radius, channel);
Toshihiro Shimizu 890ddd
		tile.getRaster()->unlock();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	/* ------ error処理 --------------------------------------- */
Toshihiro Shimizu 890ddd
	catch (std::bad_alloc &e) {
Toshihiro Shimizu 890ddd
		tile.getRaster()->unlock();
Toshihiro Shimizu 890ddd
		if (log_sw) {
Toshihiro Shimizu 890ddd
			std::string str("std::bad_alloc <");
Toshihiro Shimizu 890ddd
			str += e.what();
Toshihiro Shimizu 890ddd
			str += '>';
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		throw;
Toshihiro Shimizu 890ddd
	} catch (std::exception &e) {
Toshihiro Shimizu 890ddd
		tile.getRaster()->unlock();
Toshihiro Shimizu 890ddd
		if (log_sw) {
Toshihiro Shimizu 890ddd
			std::string str("exception <");
Toshihiro Shimizu 890ddd
			str += e.what();
Toshihiro Shimizu 890ddd
			str += '>';
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		throw;
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
		tile.getRaster()->unlock();
Toshihiro Shimizu 890ddd
		if (log_sw) {
Toshihiro Shimizu 890ddd
			std::string str("other exception");
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		throw;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}