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_level_auto : public TStandardRasterFx
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FX_PLUGIN_DECLARATION(ino_level_auto)
Toshihiro Shimizu 890ddd
	TRasterFxPort m_input;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_in_min_shift;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_in_max_shift;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_out_min;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_out_max;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_gamma;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	ino_level_auto()
Toshihiro Shimizu 890ddd
		: m_in_min_shift(0.0 * ino::param_range()), m_in_max_shift(0.0 * ino::param_range()), m_out_min(0.0 * ino::param_range()), m_out_max(1.0 * ino::param_range()), m_gamma(1.0 * ino::param_range())
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		addInputPort("Source", this->m_input);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		bindParam(this, "in_min_shift", this->m_in_min_shift);
Toshihiro Shimizu 890ddd
		bindParam(this, "in_max_shift", this->m_in_max_shift);
Toshihiro Shimizu 890ddd
		bindParam(this, "out_min", this->m_out_min);
Toshihiro Shimizu 890ddd
		bindParam(this, "out_max", this->m_out_max);
Toshihiro Shimizu 890ddd
		bindParam(this, "gamma", this->m_gamma);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		this->m_in_min_shift->setValueRange(
Toshihiro Shimizu 890ddd
			-1.0 * ino::param_range(), 1.0 * ino::param_range());
Toshihiro Shimizu 890ddd
		this->m_in_max_shift->setValueRange(
Toshihiro Shimizu 890ddd
			-1.0 * ino::param_range(), 1.0 * ino::param_range());
Toshihiro Shimizu 890ddd
		this->m_out_min->setValueRange(
Toshihiro Shimizu 890ddd
			0.0 * ino::param_range(), 1.0 * ino::param_range());
Toshihiro Shimizu 890ddd
		this->m_out_max->setValueRange(
Toshihiro Shimizu 890ddd
			0.0 * ino::param_range(), 1.0 * ino::param_range());
Toshihiro Shimizu 890ddd
		this->m_gamma->setValueRange(
Toshihiro Shimizu 890ddd
			0.1 * ino::param_range(), 10.0 * ino::param_range()); /* gamma値 */
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
			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 &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_level_auto, "inoLevelAutoFx");
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
#include "igs_level_auto_in_camera.h"
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
void fx_(
Toshihiro Shimizu 890ddd
	TRasterP in_ras, bool *act_sw, double *in_min_shift, double *in_max_shift, double *out_min, double *out_max, double *gamma, const int camera_x, const int camera_y, const int camera_w, const int camera_h)
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
	/* igs::level_auto::change(-)は今後つかわない2011-07-15 */
Toshihiro Shimizu 890ddd
	igs::level_auto_in_camera::change(
Toshihiro Shimizu 890ddd
		// in_ras->getRawData() // BGRA
Toshihiro Shimizu 890ddd
		in_gr8->getRawData()
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			,
Toshihiro Shimizu 890ddd
		in_ras->getLy(), in_ras->getLx() // Must Not use in_ras->getWrap()
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		ino::channels(), ino::bits(in_ras)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
							 ,
Toshihiro Shimizu 890ddd
		act_sw, in_min_shift, in_max_shift, out_min, out_max, gamma, camera_x, camera_y, camera_w, camera_h);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	ino::arr_to_ras(in_gr8->getRawData(), ino::channels(), in_ras, 0);
Toshihiro Shimizu 890ddd
	in_gr8->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
void ino_level_auto::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
	bool act_sw[4];
Toshihiro Shimizu 890ddd
	double in_min_sft[4];
Toshihiro Shimizu 890ddd
	double in_max_sft[4];
Toshihiro Shimizu 890ddd
	double out_min[4];
Toshihiro Shimizu 890ddd
	double out_max[4];
Toshihiro Shimizu 890ddd
	double gamma[4];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	act_sw[0] = act_sw[1] = act_sw[2] = act_sw[3] = true;
Toshihiro Shimizu 890ddd
	in_min_sft[0] = in_min_sft[1] = in_min_sft[2] = in_min_sft[3] =
Toshihiro Shimizu 890ddd
		this->m_in_min_shift->getValue(frame) /
Toshihiro Shimizu 890ddd
		ino::param_range();
Toshihiro Shimizu 890ddd
	in_max_sft[0] = in_max_sft[1] = in_max_sft[2] = in_max_sft[3] =
Toshihiro Shimizu 890ddd
		this->m_in_max_shift->getValue(frame) /
Toshihiro Shimizu 890ddd
		ino::param_range();
Toshihiro Shimizu 890ddd
	out_min[0] = out_min[1] = out_min[2] = out_min[3] =
Toshihiro Shimizu 890ddd
		this->m_out_min->getValue(frame) / ino::param_range();
Toshihiro Shimizu 890ddd
	out_max[0] = out_max[1] = out_max[2] = out_max[3] =
Toshihiro Shimizu 890ddd
		this->m_out_max->getValue(frame) / ino::param_range();
Toshihiro Shimizu 890ddd
	gamma[0] = gamma[1] = gamma[2] = gamma[3] =
Toshihiro Shimizu 890ddd
		static_cast<double>(</double>
Toshihiro Shimizu 890ddd
			this->m_gamma->getValue(frame) / ino::param_range());
Toshihiro Shimizu 890ddd
	/* ------ 画像生成 ---------------------------------------- */
Toshihiro Shimizu 890ddd
	this->m_input->compute(tile, frame, rend_sets);
Toshihiro Shimizu 890ddd
	/*--- カメラの範囲をノイズを掛ける範囲としておく(余白含ず) -*/
Toshihiro Shimizu 890ddd
	int camera_x = 0;
Toshihiro Shimizu 890ddd
	int camera_y = 0;
Toshihiro Shimizu 890ddd
	int camera_w = static_cast<int>(rend_sets.m_cameraBox.getLx() + 0.5);</int>
Toshihiro Shimizu 890ddd
	int camera_h = static_cast<int>(rend_sets.m_cameraBox.getLy() + 0.5);</int>
Toshihiro Shimizu 890ddd
	/*--- カメラ範囲外へのmargin付き(たぶん)はノイズ範囲から外す -*/
Toshihiro Shimizu 890ddd
	const int margin_w = tile.getRaster()->getLx() - camera_w;
Toshihiro Shimizu 890ddd
	const int margin_h = tile.getRaster()->getLy() - camera_h;
Toshihiro Shimizu 890ddd
	if ((0 <= margin_h && 0 < margin_w)	/* 横方向のみ余白あり */
Toshihiro Shimizu 890ddd
		|| (0 < margin_h && 0 <= margin_w) /* 縦方向のみ余白あり */
Toshihiro Shimizu 890ddd
		|| (0 < margin_h && 0 < margin_w)  /* 縦横両方に余白あり */
Toshihiro Shimizu 890ddd
		) {
Toshihiro Shimizu 890ddd
		/*camera_x = static_cast<int>(ceil((double)margin_w / 2.));</int>
Toshihiro Shimizu 890ddd
		camera_y = static_cast<int>(ceil((double)margin_h / 2.));*/</int>
Toshihiro Shimizu 890ddd
		camera_x = margin_w / 2;
Toshihiro Shimizu 890ddd
		camera_y = margin_h / 2;
Toshihiro Shimizu 890ddd
		camera_w = rend_sets.m_cameraBox.getLx();
Toshihiro Shimizu 890ddd
		camera_h = rend_sets.m_cameraBox.getLy();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	/*--- 入力画像がカメラより一部でも小さい
Toshihiro Shimizu 890ddd
		(ノイズ範囲指定対応できない-->懸案事項) ------------*/
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		camera_w = tile.getRaster()->getLx();
Toshihiro Shimizu 890ddd
		camera_h = tile.getRaster()->getLy();
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  act true"
Toshihiro Shimizu 890ddd
		   << "  in_min_shift " << in_min_sft[0]
Toshihiro Shimizu 890ddd
		   << "  in_max_shift " << in_max_sft[0]
Toshihiro Shimizu 890ddd
		   << "  out_min " << out_min[0]
Toshihiro Shimizu 890ddd
		   << "  out_max " << out_max[0]
Toshihiro Shimizu 890ddd
		   << "  gamma " << gamma[0]
Toshihiro Shimizu 890ddd
		   << "  frame " << frame
Toshihiro Shimizu 890ddd
		   << "  pixbits " << ino::pixel_bits(tile.getRaster())
Toshihiro Shimizu 890ddd
		   << "  tile.m_pos " << tile.m_pos
Toshihiro Shimizu 890ddd
		   << "  tile_getLx " << tile.getRaster()->getLx()
Toshihiro Shimizu 890ddd
		   << "  y " << tile.getRaster()->getLy()
Toshihiro Shimizu 890ddd
		   << "  rend_sets.m_cameraBox " << rend_sets.m_cameraBox
Toshihiro Shimizu 890ddd
		   << "  rend_sets.m_affine " << rend_sets.m_affine
Toshihiro Shimizu 890ddd
		   << "  camera x " << camera_x
Toshihiro Shimizu 890ddd
		   << "  y " << camera_y
Toshihiro Shimizu 890ddd
		   << "  w " << camera_w
Toshihiro Shimizu 890ddd
		   << "  h " << camera_h;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	/* ------ fx処理 ------------------------------------------ */
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		tile.getRaster()->lock();
Toshihiro Shimizu 890ddd
		fx_(tile.getRaster(), act_sw, in_min_sft, in_max_sft, out_min, out_max, gamma, camera_x, camera_y, camera_w, camera_h);
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
}