Toshihiro Shimizu 890ddd
#include <sstream> /* std::ostringstream */</sstream>
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
#include "stdfx.h"
Toshihiro Shimizu 890ddd
#include "tfxattributes.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "ino_common.h"
Toshihiro Shimizu 890ddd
#include "igs_gaussian_blur.h"
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
class ino_blur : public TStandardRasterFx
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FX_PLUGIN_DECLARATION(ino_blur)
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_ref_mode;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	ino_blur()
Toshihiro Shimizu 890ddd
		: m_radius(1.0), m_ref_mode(new TIntEnumParam(0, "Red"))
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, "reference", this->m_ref_mode);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		this->m_radius->setMeasureName("fxLength");
Toshihiro Shimizu 890ddd
		this->m_radius->setValueRange(0.0, 1000.0);
Toshihiro Shimizu 890ddd
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
	}
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
		/*--- ベクトルにする --- */
Toshihiro Shimizu 890ddd
		TPointD rend_vect;
Toshihiro Shimizu 890ddd
		rend_vect.x = this->m_radius->getValue(frame);
Toshihiro Shimizu 890ddd
		rend_vect.y = 0.0;
Toshihiro Shimizu 890ddd
		/*--- render用単位にする ---*/
Toshihiro Shimizu 890ddd
		rend_vect = rend_vect * ino::pixel_per_mm();
Toshihiro Shimizu 890ddd
		/* 単位変換(mm --> render_pixel) */
Toshihiro Shimizu 890ddd
		/*--- 回転と拡大縮小のGeometryを反映させる ---*/
Toshihiro Shimizu 890ddd
		/* 以下は、
Toshihiro Shimizu 890ddd
toonz/main/sources/stdfx/motionblurfx.cpp
Toshihiro Shimizu 890ddd
586-592行を参照して書いた
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
		TAffine aff(affine);
Toshihiro Shimizu 890ddd
		aff.a13 = aff.a23 = 0; /* 移動変換をしないで... */
Toshihiro Shimizu 890ddd
		rend_vect = aff * rend_vect;
Toshihiro Shimizu 890ddd
		/*--- 方向は無視して長さを返す ---*/
Toshihiro Shimizu 890ddd
		return sqrt(
Toshihiro Shimizu 890ddd
			rend_vect.x * rend_vect.x + rend_vect.y * rend_vect.y);
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 = igs::gaussian_blur_hv::int_radius(
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
		if (0 == this->m_radius->getValue(frame)) {
Toshihiro Shimizu 890ddd
			return true;
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			return isAlmostIsotropic(info.m_affine);
Toshihiro Shimizu 890ddd
		}
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_blur, "inoBlurFx");
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
	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 int_radius, const double real_radius)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRasterGR8P out_buffer(
Toshihiro Shimizu 890ddd
		out_ras->getLy(), out_ras->getLx() * ino::channels() *
Toshihiro Shimizu 890ddd
							  ((TRaster64P)in_ras ? sizeof(unsigned short) : sizeof(unsigned char)));
Toshihiro Shimizu 890ddd
	const int buffer_bytes = igs::gaussian_blur_hv::buffer_bytes(
Toshihiro Shimizu 890ddd
		in_ras->getLy(), in_ras->getLx(), int_radius);
Toshihiro Shimizu 890ddd
	TRasterGR8P cvt_buffer(buffer_bytes, 1);
Toshihiro Shimizu 890ddd
	out_buffer->lock();
Toshihiro Shimizu 890ddd
	cvt_buffer->lock();
Toshihiro Shimizu 890ddd
	igs::gaussian_blur_hv::convert(
Toshihiro Shimizu 890ddd
		in_ras->getRawData() // const void *in_with_margin (BGRA)
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		out_buffer->getRawData() // void *out_no_margin (BGRA)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		in_ras->getLy() // const int height_with_margin
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		in_ras->getLx() // const int width_with_margin
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		ino::channels() // const int channels
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		ino::bits(in_ras) // const int bits
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		(((0 <= ref_mode) && (0 != refer_ras)) ? refer_ras->getRawData() : 0) //BGRA
Toshihiro Shimizu 890ddd
																			  // const unsigned char *ref
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		(((0 <= ref_mode) && (0 != refer_ras)) ? ino::bits(refer_ras) : 0)
Toshihiro Shimizu 890ddd
		// const int ref_bits
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		ref_mode // const int ref_mode
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		cvt_buffer->getRawData() // void *buffer
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		buffer_bytes // int buffer_bytes
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		int_radius // const int int_radius
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		real_radius // const double real_radius
Toshihiro Shimizu 890ddd
					// , 0.25
Toshihiro Shimizu 890ddd
		);
Toshihiro Shimizu 890ddd
	ino::arr_to_ras(
Toshihiro Shimizu 890ddd
		out_buffer->getRawData(), ino::channels(), out_ras, 0);
Toshihiro Shimizu 890ddd
	cvt_buffer->unlock();
Toshihiro Shimizu 890ddd
	out_buffer->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
void ino_blur::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 real_radius = this->get_render_real_radius(
Toshihiro Shimizu 890ddd
		frame, rend_sets.m_affine);
Toshihiro Shimizu 890ddd
	const int int_radius = igs::gaussian_blur_hv::int_radius(
Toshihiro Shimizu 890ddd
		real_radius);
Toshihiro Shimizu 890ddd
	/*------ ボケ足長さがゼロのときはblur処理しない ------------*/
Toshihiro Shimizu 890ddd
	if (0 == int_radius) {
Toshihiro Shimizu 890ddd
		this->m_input->compute(tile, frame, rend_sets);
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	/*------- 動作パラメータを得る -----------------------------*/
Toshihiro Shimizu 890ddd
	const int ref_mode = this->m_ref_mode->getValue();
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
	/*------ ぼけ半径(=マージン)分表示範囲を広げる -------------*/
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
Toshihiro Shimizu 890ddd
	/* ------ margin付き画像生成 ------------------------------ */
Toshihiro Shimizu 890ddd
	/*
Toshihiro Shimizu 890ddd
void allocateAndCompute(
Toshihiro Shimizu 890ddd
TTile &tile
Toshihiro Shimizu 890ddd
,const TPointD &pos
Toshihiro Shimizu 890ddd
,const TDimension &size // TDimension = TDimensionI = TDimensionT<int></int>
Toshihiro Shimizu 890ddd
,TRasterP templateRas
Toshihiro Shimizu 890ddd
,double frame
Toshihiro Shimizu 890ddd
,const TRenderSettings &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, 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
	/*------ 参照画像生成 --------------------------------------*/
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, tile.m_pos /* TPointD */
Toshihiro Shimizu 890ddd
			,
Toshihiro Shimizu 890ddd
			tile.getRaster()->getSize() /* Pixel単位 */
Toshihiro Shimizu 890ddd
			,
Toshihiro Shimizu 890ddd
			tile.getRaster(), frame, rend_sets);
Toshihiro Shimizu 890ddd
	}
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
		   << "  usr_radius " << this->m_radius->getValue(frame)
Toshihiro Shimizu 890ddd
		   << "  real_radius " << real_radius
Toshihiro Shimizu 890ddd
		   << "  int_radius " << int_radius
Toshihiro Shimizu 890ddd
		   << "  ref_mode " << ref_mode
Toshihiro Shimizu 890ddd
		   << "  tile"
Toshihiro Shimizu 890ddd
		   << " pos " << tile.m_pos
Toshihiro Shimizu 890ddd
		   << " w " << tile.getRaster()->getLx()
Toshihiro Shimizu 890ddd
		   << " h " << tile.getRaster()->getLy()
Toshihiro Shimizu 890ddd
		   << "  enl_tile"
Toshihiro Shimizu 890ddd
		   << " w " << enlarge_tile.getRaster()->getLx()
Toshihiro Shimizu 890ddd
		   << " h " << enlarge_tile.getRaster()->getLy()
Toshihiro Shimizu 890ddd
		   << "  pixbits " << ino::pixel_bits(tile.getRaster())
Toshihiro Shimizu 890ddd
		   << "  frame " << frame
Toshihiro Shimizu 890ddd
		   << "  affine a11 " << rend_sets.m_affine.a11
Toshihiro Shimizu 890ddd
		   << "  a12 " << rend_sets.m_affine.a12
Toshihiro Shimizu 890ddd
		   << "  a21 " << rend_sets.m_affine.a21
Toshihiro Shimizu 890ddd
		   << "  a22 " << rend_sets.m_affine.a22;
Toshihiro Shimizu 890ddd
		if (ref_sw) {
Toshihiro Shimizu 890ddd
			os
Toshihiro Shimizu 890ddd
				<< "  ref_tile"
Toshihiro Shimizu 890ddd
				<< " pos " << ref_tile.m_pos
Toshihiro Shimizu 890ddd
				<< " x " << 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
		fx_(enlarge_tile.getRaster() // in with margin
Toshihiro Shimizu 890ddd
			,
Toshihiro Shimizu 890ddd
			tile.getRaster() // out with no margin
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			,
Toshihiro Shimizu 890ddd
			ref_tile.getRaster(), ref_mode
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			,
Toshihiro Shimizu 890ddd
			int_radius // margin
Toshihiro Shimizu 890ddd
			,
Toshihiro Shimizu 890ddd
			real_radius);
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
}