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_hls_add : public TStandardRasterFx
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FX_PLUGIN_DECLARATION(ino_hls_add)
Toshihiro Shimizu 890ddd
	TRasterFxPort m_input;
Toshihiro Shimizu 890ddd
	TRasterFxPort m_noise;
Toshihiro Shimizu 890ddd
	TRasterFxPort m_refer;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TIntEnumParamP m_from_rgba;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_offset;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_hue;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_lig;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_sat;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_alp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TBoolParamP m_anti_alias;
Toshihiro Shimizu 890ddd
	TIntEnumParamP m_ref_mode;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	ino_hls_add()
Toshihiro Shimizu 890ddd
		: m_from_rgba(new TIntEnumParam(0, "Red")), m_offset(0.5 * ino::param_range()), m_hue(0.0 * ino::param_range()), m_lig(0.25 * ino::param_range()), m_sat(0.0 * ino::param_range()), m_alp(0.0 * ino::param_range())
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		  ,
Toshihiro Shimizu 890ddd
		  m_anti_alias(true), m_ref_mode(new TIntEnumParam(0, "Red"))
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		addInputPort("Source", this->m_input);
Toshihiro Shimizu 890ddd
		addInputPort("Noise", this->m_noise);
Toshihiro Shimizu 890ddd
		addInputPort("Reference", this->m_refer);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		bindParam(this, "from_rgba", this->m_from_rgba);
Toshihiro Shimizu 890ddd
		bindParam(this, "offset", this->m_offset);
Toshihiro Shimizu 890ddd
		bindParam(this, "hue", this->m_hue);
Toshihiro Shimizu 890ddd
		bindParam(this, "lightness", this->m_lig);
Toshihiro Shimizu 890ddd
		bindParam(this, "saturation", this->m_sat);
Toshihiro Shimizu 890ddd
		bindParam(this, "alpha", this->m_alp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		bindParam(this, "anti_alias", this->m_anti_alias);
Toshihiro Shimizu 890ddd
		bindParam(this, "reference", this->m_ref_mode);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		this->m_from_rgba->addItem(1, "Green");
Toshihiro Shimizu 890ddd
		this->m_from_rgba->addItem(2, "Blue");
Toshihiro Shimizu 890ddd
		this->m_from_rgba->addItem(3, "Alpha");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		this->m_offset->setValueRange(
Toshihiro Shimizu 890ddd
			-1.0 * ino::param_range(), 1.0 * ino::param_range());
Toshihiro Shimizu 890ddd
		this->m_hue->setValueRange(
Toshihiro Shimizu 890ddd
			-1.0 * ino::param_range(), 1.0 * ino::param_range());
Toshihiro Shimizu 890ddd
		this->m_lig->setValueRange(
Toshihiro Shimizu 890ddd
			-1.0 * ino::param_range(), 1.0 * ino::param_range());
Toshihiro Shimizu 890ddd
		this->m_sat->setValueRange(
Toshihiro Shimizu 890ddd
			-1.0 * ino::param_range(), 1.0 * ino::param_range());
Toshihiro Shimizu 890ddd
		this->m_alp->setValueRange(
Toshihiro Shimizu 890ddd
			-1.0 * ino::param_range(), 1.0 * ino::param_range());
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
	bool doGetBBox(double frame, TRectD &bBox, const TRenderSettings &info)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if (this->m_input.isConnected()) {
Toshihiro Shimizu 890ddd
			return this->m_input->doGetBBox(frame, bBox, info);
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
	void doCompute(
Toshihiro Shimizu 890ddd
		TTile &tile, double frame, const TRenderSettings &rend_sets);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(ino_hls_add, "inohlsAddFx");
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
#include "igs_hls_add.h"
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
void fx_(
Toshihiro Shimizu 890ddd
	TRasterP in_ras, const TRasterP noise_ras, const TRasterP refer_ras, const int ref_mode
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const int xoffset, const int yoffset, const int from_rgba, const double offset, const double hue_scale, const double lig_scale, const double sat_scale, const double alp_scale, const bool anti_alias_sw)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	/***std::vector<unsigned char=""> in_vec;</unsigned>
Toshihiro Shimizu 890ddd
	ino::ras_to_vec( in_ras, ino::channels(), in_vec );
Toshihiro Shimizu 890ddd
	std::vector<unsigned char=""> refer_vec;</unsigned>
Toshihiro Shimizu 890ddd
	ino::ras_to_vec( noise_ras, ino::channels(), refer_vec );***/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterGR8P in_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
	in_gr8->lock();
Toshihiro Shimizu 890ddd
	ino::ras_to_arr(in_ras, ino::channels(), in_gr8->getRawData());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterGR8P noise_gr8(
Toshihiro Shimizu 890ddd
		noise_ras->getLy(), noise_ras->getLx() * ino::channels() *
Toshihiro Shimizu 890ddd
								((TRaster64P)noise_ras ? sizeof(unsigned short) : sizeof(unsigned char)));
Toshihiro Shimizu 890ddd
	noise_gr8->lock();
Toshihiro Shimizu 890ddd
	ino::ras_to_arr(noise_ras, ino::channels(), noise_gr8->getRawData());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	igs::hls_add::change(
Toshihiro Shimizu 890ddd
		//in_ras->getRawData() // BGRA
Toshihiro Shimizu 890ddd
		//&in_vec.at(0) // RGBA
Toshihiro Shimizu 890ddd
		in_gr8->getRawData()
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			,
Toshihiro Shimizu 890ddd
		in_ras->getLy(), in_ras->getLx() // Not use in_ras->getWrap()
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		ino::channels(), ino::bits(in_ras)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//,noise_ras->getRawData() // BGRA
Toshihiro Shimizu 890ddd
		//,&refer_vec.at(0) // RGBA
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		noise_gr8->getRawData()
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			,
Toshihiro Shimizu 890ddd
		noise_ras->getLy(), noise_ras->getLx(), ino::channels(), ino::bits(noise_ras)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
																	 ,
Toshihiro Shimizu 890ddd
		(((0 <= ref_mode) && (0 != refer_ras)) ? refer_ras->getRawData() : 0) //BGRA
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		(((0 <= ref_mode) && (0 != refer_ras)) ? ino::bits(refer_ras) : 0), ref_mode
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		xoffset, yoffset, from_rgba, offset, hue_scale, lig_scale, sat_scale, alp_scale
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//,true	/* add_blend_sw */
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		anti_alias_sw);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/***ino::vec_to_ras( refer_vec, 0, 0 );
Toshihiro Shimizu 890ddd
	ino::vec_to_ras( in_vec, ino::channels(), in_ras, 0 );***/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	ino::arr_to_ras(in_gr8->getRawData(), ino::channels(), in_ras, 0);
Toshihiro Shimizu 890ddd
	noise_gr8->unlock();
Toshihiro Shimizu 890ddd
	in_gr8->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
void ino_hls_add::doCompute(
Toshihiro Shimizu 890ddd
	TTile &tile, double frame, const TRenderSettings &rend_sets)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	/* ------ 両方とも接続していなければ処理しない ------------ */
Toshihiro Shimizu 890ddd
	const bool in_cn_is = this->m_input.isConnected();
Toshihiro Shimizu 890ddd
	const bool noise_cn_is = this->m_noise.isConnected();
Toshihiro Shimizu 890ddd
	if (!in_cn_is || !noise_cn_is) {
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
	const int xoffset = 0.0;
Toshihiro Shimizu 890ddd
	const int yoffset = 0.0;
Toshihiro Shimizu 890ddd
	const int from_rgba = this->m_from_rgba->getValue();
Toshihiro Shimizu 890ddd
	const double offset = this->m_offset->getValue(frame) /
Toshihiro Shimizu 890ddd
						  ino::param_range();
Toshihiro Shimizu 890ddd
	const double hue_scale = this->m_hue->getValue(frame) /
Toshihiro Shimizu 890ddd
							 ino::param_range();
Toshihiro Shimizu 890ddd
	const double lig_scale = this->m_lig->getValue(frame) /
Toshihiro Shimizu 890ddd
							 ino::param_range();
Toshihiro Shimizu 890ddd
	const double sat_scale = this->m_sat->getValue(frame) /
Toshihiro Shimizu 890ddd
							 ino::param_range();
Toshihiro Shimizu 890ddd
	const double alp_scale = this->m_alp->getValue(frame) /
Toshihiro Shimizu 890ddd
							 ino::param_range();
Toshihiro Shimizu 890ddd
	const bool anti_alias_sw = this->m_anti_alias->getValue();
Toshihiro Shimizu 890ddd
	const int ref_mode = this->m_ref_mode->getValue();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* ------ 画像生成 ---------------------------------------- */
Toshihiro Shimizu 890ddd
	this->m_input->compute(tile, frame, rend_sets);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* ------ noise画像生成 ------------------------------------ */
Toshihiro Shimizu 890ddd
	TTile noise_tile;
Toshihiro Shimizu 890ddd
	this->m_noise->allocateAndCompute(
Toshihiro Shimizu 890ddd
		noise_tile,
Toshihiro Shimizu 890ddd
		tile.m_pos,
Toshihiro Shimizu 890ddd
		tile.getRaster()->getSize(),
Toshihiro Shimizu 890ddd
		tile.getRaster(), frame, rend_sets);
Toshihiro Shimizu 890ddd
	/*------ 参照画像生成 --------------------------------------*/
Toshihiro Shimizu 890ddd
	TTile reference_tile;
Toshihiro Shimizu 890ddd
	bool reference_sw = false;
Toshihiro Shimizu 890ddd
	if (this->m_refer.isConnected()) {
Toshihiro Shimizu 890ddd
		reference_sw = true;
Toshihiro Shimizu 890ddd
		this->m_refer->allocateAndCompute(
Toshihiro Shimizu 890ddd
			reference_tile, tile.m_pos, TDimensionI(													  /* Pixel単位 */
Toshihiro Shimizu 890ddd
													tile.getRaster()->getLx(), tile.getRaster()->getLy()) /* ここtile.getRaster()->getSize()と同じ、将来修正する */
Toshihiro Shimizu 890ddd
			,
Toshihiro Shimizu 890ddd
			tile.getRaster(), frame, rend_sets);
Toshihiro Shimizu 890ddd
	}
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
		   << "  xo " << xoffset
Toshihiro Shimizu 890ddd
		   << "  yo " << yoffset
Toshihiro Shimizu 890ddd
		   << "  rgba " << from_rgba
Toshihiro Shimizu 890ddd
		   << "  offs " << offset
Toshihiro Shimizu 890ddd
		   << "  h " << hue_scale
Toshihiro Shimizu 890ddd
		   << "  l " << lig_scale
Toshihiro Shimizu 890ddd
		   << "  s " << sat_scale
Toshihiro Shimizu 890ddd
		   << "  a " << alp_scale
Toshihiro Shimizu 890ddd
		   << "  anti_alias " << anti_alias_sw
Toshihiro Shimizu 890ddd
		   << "  reference " << 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
		   << "   noise_tile w " << noise_tile.getRaster()->getLx()
Toshihiro Shimizu 890ddd
		   << "  h " << noise_tile.getRaster()->getLy()
Toshihiro Shimizu 890ddd
		   << "   frame " << frame;
Toshihiro Shimizu 890ddd
		if (reference_sw) {
Toshihiro Shimizu 890ddd
			os
Toshihiro Shimizu 890ddd
				<< "  reference_tile.m_pos " << reference_tile.m_pos
Toshihiro Shimizu 890ddd
				<< "  reference_tile_getLx " << reference_tile.getRaster()->getLx()
Toshihiro Shimizu 890ddd
				<< "  y " << reference_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
		noise_tile.getRaster()->lock();
Toshihiro Shimizu 890ddd
		reference_tile.getRaster()->lock();
Toshihiro Shimizu 890ddd
		fx_(
Toshihiro Shimizu 890ddd
			tile.getRaster(), noise_tile.getRaster(), reference_tile.getRaster(), ref_mode
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			,
Toshihiro Shimizu 890ddd
			xoffset, yoffset, from_rgba, offset, hue_scale, lig_scale, sat_scale, alp_scale, anti_alias_sw // --> add_blend_sw, default is true
Toshihiro Shimizu 890ddd
			);
Toshihiro Shimizu 890ddd
		reference_tile.getRaster()->unlock();
Toshihiro Shimizu 890ddd
		noise_tile.getRaster()->unlock();
Toshihiro Shimizu 890ddd
		tile.getRaster()->unlock();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	/* ------ error処理 --------------------------------------- */
Toshihiro Shimizu 890ddd
	catch (std::bad_alloc &e) {
Toshihiro Shimizu 890ddd
		reference_tile.getRaster()->unlock();
Toshihiro Shimizu 890ddd
		noise_tile.getRaster()->unlock();
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
		reference_tile.getRaster()->unlock();
Toshihiro Shimizu 890ddd
		noise_tile.getRaster()->unlock();
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
		reference_tile.getRaster()->unlock();
Toshihiro Shimizu 890ddd
		noise_tile.getRaster()->unlock();
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
}