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
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
const double smoothing_edge_ = 1.0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
class ino_maxmin : public TStandardRasterFx
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FX_PLUGIN_DECLARATION(ino_maxmin)
Toshihiro Shimizu 890ddd
	TRasterFxPort m_input;
Toshihiro Shimizu 890ddd
	TRasterFxPort m_refer;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TIntEnumParamP m_max_min_select;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_radius;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_polygon_number;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_degree;
Toshihiro Shimizu 890ddd
	TBoolParamP m_alpha_rendering;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TIntEnumParamP m_ref_mode;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	ino_maxmin()
Toshihiro Shimizu 890ddd
		: m_max_min_select(new TIntEnumParam(0, "Max")), m_radius(1.0)
Toshihiro Shimizu 890ddd
		  /*	1mmにしたければ 1.0 * 640. / 12. / 25.4 とする
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_polygon_number(2.0), m_degree(0.0), m_alpha_rendering(true)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		  ,
Toshihiro Shimizu 890ddd
		  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, "max_min_select", this->m_max_min_select);
Toshihiro Shimizu 890ddd
		bindParam(this, "radius", this->m_radius);
Toshihiro Shimizu 890ddd
		bindParam(this, "polygon_number", this->m_polygon_number);
Toshihiro Shimizu 890ddd
		bindParam(this, "degree", this->m_degree);
Toshihiro Shimizu 890ddd
		bindParam(this, "alpha_rendering",
Toshihiro Shimizu 890ddd
				  this->m_alpha_rendering);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		bindParam(this, "reference", this->m_ref_mode);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		this->m_max_min_select->addItem(1, "Min");
Toshihiro Shimizu 890ddd
		this->m_radius->setValueRange(0.0, 100.0);
Toshihiro Shimizu 890ddd
		this->m_polygon_number->setValueRange(2.0, 16.0);
Toshihiro Shimizu 890ddd
		this->m_degree->setValueRange(
Toshihiro Shimizu 890ddd
			0.0,
Toshihiro Shimizu 890ddd
			std::numeric_limits<double>::max());</double>
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(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(
Toshihiro Shimizu 890ddd
				ino::pixel_per_mm() * (this->m_radius->getValue(frame) + smoothing_edge_));
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 &info, 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 =
Toshihiro Shimizu 890ddd
			(this->m_radius->getValue(frame) + smoothing_edge_) * ino::pixel_per_mm() * sqrt(fabs(info.m_affine.det())) / ((info.m_shrinkX + info.m_shrinkY) / 2.0);
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_maxmin, "inoMaxMinFx");
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
#include "igs_maxmin.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 int min_sw, const double radius, const double smoothing_edge, const int npolygon, const double degree, const bool alp_rend_sw
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const int nthread)
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::maxmin::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
		radius, smoothing_edge // smooth_outer_range
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		npolygon, degree + 180.0
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		min_sw, alp_rend_sw, false
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		nthread);
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_maxmin::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
	/* ------ Pixel単位で動作パラメータを得る ----------------- */
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
	/* 動作パラメータを得る */
Toshihiro Shimizu 890ddd
	const int min_sw = this->m_max_min_select->getValue(); // 0,1
Toshihiro Shimizu 890ddd
	const double radius = this->m_radius->getValue(frame) *
Toshihiro Shimizu 890ddd
						  mm2scale_shrink_pixel;
Toshihiro Shimizu 890ddd
	const int npolygon = this->m_polygon_number->getValue(frame);
Toshihiro Shimizu 890ddd
	const double degree = this->m_degree->getValue(frame);
Toshihiro Shimizu 890ddd
	const bool alp_rend_sw = this->m_alpha_rendering->getValue();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const int ref_mode = this->m_ref_mode->getValue();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* 	tcomposer(RenderManager)でRenderingするときはthreadは1つ、
Toshihiro Shimizu 890ddd
		toonz(Desktop)でPreview,Outputするなら-1を指定
Toshihiro Shimizu 890ddd
		-1は自動でthread数を決めCPUを最大に使い高速化 */
Toshihiro Shimizu 890ddd
	const int nthread = -1;
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 + smoothing_edge_) + 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
		   << "  min_sw " << min_sw
Toshihiro Shimizu 890ddd
		   << "  radius " << radius
Toshihiro Shimizu 890ddd
		   << "  npolygon " << npolygon
Toshihiro Shimizu 890ddd
		   << "  degree " << degree
Toshihiro Shimizu 890ddd
		   << "  alp_rend_sw " << alp_rend_sw
Toshihiro Shimizu 890ddd
		   << "  smoothing_edge " << smoothing_edge_
Toshihiro Shimizu 890ddd
		   << "  nthread " << nthread
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
			min_sw, radius, smoothing_edge_, npolygon, degree, alp_rend_sw
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			,
Toshihiro Shimizu 890ddd
			nthread);
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
}