Toshihiro Shimizu 890ddd
#if defined _WIN32 && !defined NOMINMAX
Toshihiro Shimizu 890ddd
#define NOMINMAX
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
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
#include "igs_fog.h"
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
class ino_fog : public TStandardRasterFx
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FX_PLUGIN_DECLARATION(ino_fog)
Toshihiro Shimizu 890ddd
	TRasterFxPort m_input;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_radius;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_curve;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_power;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_threshold_min;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_threshold_max;
Toshihiro Shimizu 890ddd
	TBoolParamP m_alpha_rendering;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	ino_fog()
Toshihiro Shimizu 890ddd
		: m_radius(1.0), m_curve(1.0 * ino::param_range()), m_power(1.0 * ino::param_range()), m_threshold_min(0.0 * ino::param_range()), m_threshold_max(0.0 * ino::param_range()), m_alpha_rendering(false)
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
Toshihiro Shimizu 890ddd
		bindParam(this, "radius", this->m_radius);
Toshihiro Shimizu 890ddd
		bindParam(this, "curve", this->m_curve);
Toshihiro Shimizu 890ddd
		bindParam(this, "power", this->m_power);
Toshihiro Shimizu 890ddd
		bindParam(this, "threshold_min", this->m_threshold_min);
Toshihiro Shimizu 890ddd
		bindParam(this, "threshold_max", this->m_threshold_max);
Toshihiro Shimizu 890ddd
		bindParam(this, "alpha_rendering", this->m_alpha_rendering);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		this->m_radius->setValueRange(0.0, 100.0);
Toshihiro Shimizu 890ddd
		this->m_curve->setValueRange(
Toshihiro Shimizu 890ddd
			0.1 * ino::param_range(), 10.0 * ino::param_range()); /* gammaカーブ */
Toshihiro Shimizu 890ddd
		this->m_power->setValueRange(
Toshihiro Shimizu 890ddd
			-2.0 * ino::param_range(), 2.0 * ino::param_range());
Toshihiro Shimizu 890ddd
		this->m_threshold_min->setValueRange(
Toshihiro Shimizu 890ddd
			0.0 * ino::param_range(), 1.01 * ino::param_range());
Toshihiro Shimizu 890ddd
		this->m_threshold_max->setValueRange(
Toshihiro Shimizu 890ddd
			0.0 * ino::param_range(), 1.01 * ino::param_range());
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	//------------------------------------------------------------
Toshihiro Shimizu 890ddd
	double get_render_real_radius(
Toshihiro Shimizu 890ddd
		const double frame, const TAffine affine)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return this->m_radius->getValue(frame) * ino::pixel_per_mm() /* render用単位にする */
Toshihiro Shimizu 890ddd
			   * sqrt(fabs(affine.det()))							 /* 拡大縮小のGeometryを反映 */
Toshihiro Shimizu 890ddd
			;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void get_render_enlarge(
Toshihiro Shimizu 890ddd
		const double frame, const TAffine affine, TRectD &bBox)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		const int margin = static_cast<int>(ceil(</int>
Toshihiro Shimizu 890ddd
			this->get_render_real_radius(frame, affine)));
Toshihiro Shimizu 890ddd
		if (0 < margin) {
Toshihiro Shimizu 890ddd
			bBox = bBox.enlarge(static_cast<double>(margin));</double>
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
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 (false == this->m_input.isConnected()) {
Toshihiro Shimizu 890ddd
			bBox = TRectD();
Toshihiro Shimizu 890ddd
			return false;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		const bool ret = this->m_input->doGetBBox(frame, bBox, info);
Toshihiro Shimizu 890ddd
		this->get_render_enlarge(frame, info.m_affine, bBox);
Toshihiro Shimizu 890ddd
		return ret;
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
		TRectD bBox(rect);
Toshihiro Shimizu 890ddd
		this->get_render_enlarge(frame, info.m_affine, bBox);
Toshihiro Shimizu 890ddd
		return TRasterFx::memorySize(bBox, info.m_bpp);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void transform(
Toshihiro Shimizu 890ddd
		double frame, int port, const TRectD &rectOnOutput, const TRenderSettings &infoOnOutput, TRectD &rectOnInput, TRenderSettings &infoOnInput)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		rectOnInput = rectOnOutput;
Toshihiro Shimizu 890ddd
		infoOnInput = infoOnOutput;
Toshihiro Shimizu 890ddd
		this->get_render_enlarge(
Toshihiro Shimizu 890ddd
			frame, infoOnOutput.m_affine, rectOnInput);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	bool canHandle(
Toshihiro Shimizu 890ddd
		const TRenderSettings &info, double frame)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return true;
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_fog, "inoFogFx");
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
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
	const int nthread, const double radius, const double curve, const double power, const double threshold_min, const double threshold_max, const bool alp_rend_sw)
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
	TRasterGR8P ref_gr8(
Toshihiro Shimizu 890ddd
		in_ras->getLy(), in_ras->getLx() * sizeof(double));
Toshihiro Shimizu 890ddd
	ref_gr8->lock();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	igs::fog::convert(
Toshihiro Shimizu 890ddd
		in_ras->getRawData() // BGRA
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		out_gr8->getRawData(), reinterpret_cast<double *="">(ref_gr8->getRawData())</double>
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
		nthread
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		radius, curve, 2, 0.0, power, threshold_min, threshold_max, alp_rend_sw);
Toshihiro Shimizu 890ddd
	ref_gr8->unlock();
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_fog::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
	/*------ サポートしていない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
	const double radius = this->get_render_real_radius(
Toshihiro Shimizu 890ddd
		frame, rend_sets.m_affine);
Toshihiro Shimizu 890ddd
	const double curve = this->m_curve->getValue(frame) /
Toshihiro Shimizu 890ddd
						 ino::param_range();
Toshihiro Shimizu 890ddd
	const double power = this->m_power->getValue(frame) /
Toshihiro Shimizu 890ddd
						 ino::param_range();
Toshihiro Shimizu 890ddd
	const double threshold_min =
Toshihiro Shimizu 890ddd
		this->m_threshold_min->getValue(frame) / ino::param_range();
Toshihiro Shimizu 890ddd
	const double threshold_max =
Toshihiro Shimizu 890ddd
		this->m_threshold_max->getValue(frame) / ino::param_range();
Toshihiro Shimizu 890ddd
	const bool alp_rend_sw = this->m_alpha_rendering->getValue();
Toshihiro Shimizu 890ddd
	const int nthread = 1;
Toshihiro Shimizu 890ddd
	/*------ fogがからないパラメータ値のときはfog処理しない ----*/
Toshihiro Shimizu 890ddd
	if (!igs::fog::have_change(radius, power, threshold_min)) {
Toshihiro Shimizu 890ddd
		this->m_input->compute(tile, frame, rend_sets);
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	/*------ 表示の範囲を得る ----------------------------------*/
Toshihiro Shimizu 890ddd
	TRectD bBox = TRectD(
Toshihiro Shimizu 890ddd
		tile.m_pos /* Render画像上(Pixel単位)の位置 */
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		TDimensionD(/* Render画像上(Pixel単位)のサイズ */
Toshihiro Shimizu 890ddd
					tile.getRaster()->getLx(), tile.getRaster()->getLy()));
Toshihiro Shimizu 890ddd
	/*------ fog半径(=マージン)分表示範囲を広げる --------------*/
Toshihiro Shimizu 890ddd
	const int int_radius = static_cast<int>(ceil(radius));</int>
Toshihiro Shimizu 890ddd
	if (0 < int_radius) {
Toshihiro Shimizu 890ddd
		bBox = bBox.enlarge(static_cast<double>(int_radius));</double>
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	TTile enlarge_tile;
Toshihiro Shimizu 890ddd
	this->m_input->allocateAndCompute(
Toshihiro Shimizu 890ddd
		enlarge_tile, bBox.getP00(), TDimensionI(/* Pixel単位に四捨五入 */
Toshihiro Shimizu 890ddd
												 static_cast<int>(bBox.getLx() + 0.5), static_cast<int>(bBox.getLy() + 0.5)),</int></int>
Toshihiro Shimizu 890ddd
		tile.getRaster(), frame, rend_sets);
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
		   << "  nthread " << nthread
Toshihiro Shimizu 890ddd
		   << "  usr_radius " << this->m_radius->getValue(frame)
Toshihiro Shimizu 890ddd
		   << "  real_radius " << radius
Toshihiro Shimizu 890ddd
		   << "  int_radius " << int_radius
Toshihiro Shimizu 890ddd
		   << "  curve " << curve
Toshihiro Shimizu 890ddd
		   << "  power " << power
Toshihiro Shimizu 890ddd
		   << "  threshold_min " << threshold_min
Toshihiro Shimizu 890ddd
		   << "  threshold_max " << threshold_max
Toshihiro Shimizu 890ddd
		   << "  alp_rend_sw " << alp_rend_sw
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
	}
Toshihiro Shimizu 890ddd
	/* ------ fx処理 ------------------------------------------ */
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		tile.getRaster()->lock();
Toshihiro Shimizu 890ddd
		fx_(enlarge_tile.getRaster() // with margin
Toshihiro Shimizu 890ddd
			,
Toshihiro Shimizu 890ddd
			int_radius // margin
Toshihiro Shimizu 890ddd
			,
Toshihiro Shimizu 890ddd
			tile.getRaster() // no margin
Toshihiro Shimizu 890ddd
			,
Toshihiro Shimizu 890ddd
			nthread, radius, curve, power, threshold_min, threshold_max, alp_rend_sw);
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
}