Toshihiro Shimizu 890ddd
// #include <iostream></iostream>
Toshihiro Shimizu 890ddd
#include "igs_motion_wind_table.h"
Toshihiro Shimizu 890ddd
#include "igs_motion_wind_pixel.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
igs::motion_wind::pixel::pixel(
Toshihiro Shimizu 890ddd
	const bool blow_dark_sw, const bool blow_alpha_sw
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const unsigned long length_random_seed, const double length_min, const double length_max, const double length_bias, const bool length_ref_sw
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const unsigned long force_random_seed, const double force_min, const double force_max, const double force_bias, const bool force_ref_sw
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const unsigned long density_random_seed, const double density_min, const double density_max, const double density_bias, const bool density_ref_sw)
Toshihiro Shimizu 890ddd
	: blow_dark_sw_(blow_dark_sw), blow_alpha_sw_(blow_alpha_sw)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	  ,
Toshihiro Shimizu 890ddd
	  length_min_(length_min), length_max_(length_max), length_bias_(length_bias), length_ref_sw_(length_ref_sw)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	  ,
Toshihiro Shimizu 890ddd
	  force_min_(force_min), force_max_(force_max), force_bias_(force_bias), force_ref_sw_(force_ref_sw)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	  ,
Toshihiro Shimizu 890ddd
	  density_min_(density_min), density_max_(density_max), density_bias_(density_bias), density_ref_sw_(density_ref_sw)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	  ,
Toshihiro Shimizu 890ddd
	  key_lightness_(0), table_len_(0), table_pos_(0), table_array_(0)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	this->table_.resize(
Toshihiro Shimizu 890ddd
		igs::motion_wind::table_size(length_min, length_max));
Toshihiro Shimizu 890ddd
	this->length_random_.seed(length_random_seed);
Toshihiro Shimizu 890ddd
	this->force_random_.seed(force_random_seed);
Toshihiro Shimizu 890ddd
	this->density_random_.seed(density_random_seed);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
void igs::motion_wind::pixel::clear(void)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	this->table_.clear();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
void wind_rgba_(
Toshihiro Shimizu 890ddd
	const double key, const double alp, const double ratio, bool &sw, double &tgt)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (tgt < key) {
Toshihiro Shimizu 890ddd
		tgt += ratio * (key - tgt) * alp;
Toshihiro Shimizu 890ddd
		tgt = (tgt < 0.0) ? 0.0 : ((1.0 < tgt) ? 1.0 : tgt); //limit
Toshihiro Shimizu 890ddd
		sw = true;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
void wind_rgb_(
Toshihiro Shimizu 890ddd
	const double key, const double ratio, bool &sw, double &tgt)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (tgt < key) {
Toshihiro Shimizu 890ddd
		// std::cout << "(R" << ratio << ",K" << key << ",T" << tgt;
Toshihiro Shimizu 890ddd
		tgt += ratio * (key - tgt);
Toshihiro Shimizu 890ddd
		// std::cout << ">" << tgt << ")\n";
Toshihiro Shimizu 890ddd
		tgt = (tgt < 0.0) ? 0.0 : ((1.0 < tgt) ? 1.0 : tgt); //limit
Toshihiro Shimizu 890ddd
		sw = true;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
void wind_a_(
Toshihiro Shimizu 890ddd
	const double key /* alpha値 */
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double ratio, const bool sw, double &tgt /* alpha値 */
Toshihiro Shimizu 890ddd
	)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if ((tgt < key) || sw) {
Toshihiro Shimizu 890ddd
		double val = tgt + (ratio * (key - tgt));
Toshihiro Shimizu 890ddd
		if (tgt < val) { /* 元より明るいときのみ変化 */
Toshihiro Shimizu 890ddd
			tgt = val;
Toshihiro Shimizu 890ddd
			tgt = (tgt < 0.0) ? 0.0 : ((1.0 < tgt) ? 1.0 : tgt); //limit
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
void blow_wind_(/* 風の影響による値の変化 */
Toshihiro Shimizu 890ddd
				const int channels, const double *key, const double *tbl_a, const int tbl_p, const bool blow_alpha_sw, double *tgt)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	bool sw = false; /* rgb風を実行したか否かを示す */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (igs::image::rgba::siz == channels) { /* Alphaがある */
Toshihiro Shimizu 890ddd
		using namespace igs::image::rgba;
Toshihiro Shimizu 890ddd
		if (blow_alpha_sw) { /* Alpha風を実行する */
Toshihiro Shimizu 890ddd
							 /* RGB風 */
Toshihiro Shimizu 890ddd
			wind_rgb_(key[red], tbl_a[tbl_p], sw, tgt[red]);
Toshihiro Shimizu 890ddd
			wind_rgb_(key[gre], tbl_a[tbl_p], sw, tgt[gre]);
Toshihiro Shimizu 890ddd
			wind_rgb_(key[blu], tbl_a[tbl_p], sw, tgt[blu]);
Toshihiro Shimizu 890ddd
			/* Alpha風 */
Toshihiro Shimizu 890ddd
			/* Alpha独自の風と、RGB風の、両方の影響による値の変化 */
Toshihiro Shimizu 890ddd
			wind_a_(key[alp], tbl_a[tbl_p], sw, tgt[alp]);
Toshihiro Shimizu 890ddd
		} else { /* Alpha風を実行せず、マスク抜きのRGB風を生成 */
Toshihiro Shimizu 890ddd
			wind_rgba_(key[red], tgt[alp], tbl_a[tbl_p], sw, tgt[red]);
Toshihiro Shimizu 890ddd
			wind_rgba_(key[gre], tgt[alp], tbl_a[tbl_p], sw, tgt[gre]);
Toshihiro Shimizu 890ddd
			wind_rgba_(key[blu], tgt[alp], tbl_a[tbl_p], sw, tgt[blu]);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else { /* channel毎に風を生成する */
Toshihiro Shimizu 890ddd
		for (int zz = 0; zz < channels; ++zz) {
Toshihiro Shimizu 890ddd
			wind_rgb_(key[zz], tbl_a[tbl_p], sw, tgt[zz]);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
void invert_pixel_(
Toshihiro Shimizu 890ddd
	const int channels, double *pixel)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (igs::image::rgba::siz == channels) { /* Alphaがある */
Toshihiro Shimizu 890ddd
		using namespace igs::image::rgba;
Toshihiro Shimizu 890ddd
		pixel[red] = (1.0 - pixel[red]) * pixel[alp];
Toshihiro Shimizu 890ddd
		pixel[gre] = (1.0 - pixel[gre]) * pixel[alp];
Toshihiro Shimizu 890ddd
		pixel[blu] = (1.0 - pixel[blu]) * pixel[alp];
Toshihiro Shimizu 890ddd
	} else /* Alphaがないなら、ただ白黒反転 */
Toshihiro Shimizu 890ddd
		if (igs::image::rgb::siz == channels) {
Toshihiro Shimizu 890ddd
		using namespace igs::image::rgb;
Toshihiro Shimizu 890ddd
		pixel[red] = 1.0 - pixel[red];
Toshihiro Shimizu 890ddd
		pixel[gre] = 1.0 - pixel[gre];
Toshihiro Shimizu 890ddd
		pixel[blu] = 1.0 - pixel[blu];
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		for (int zz = 0; zz < channels; ++zz) {
Toshihiro Shimizu 890ddd
			pixel[zz] = 1.0 - pixel[zz];
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
void rgb_to_lightness_(
Toshihiro Shimizu 890ddd
	const double re, const double gr, const double bl, double &li)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	li = ((re < gr) ? ((gr < bl) ? bl : gr) : ((re < bl) ? bl : re) +
Toshihiro Shimizu 890ddd
													  (gr < re)
Toshihiro Shimizu 890ddd
												  ? ((bl < gr) ? bl : gr)
Toshihiro Shimizu 890ddd
												  : ((bl < re) ? bl : re)) /
Toshihiro Shimizu 890ddd
		 2.0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
double get_lightness_(
Toshihiro Shimizu 890ddd
	const int channels, const double *pixel
Toshihiro Shimizu 890ddd
	// , const bool blow_dark_sw
Toshihiro Shimizu 890ddd
	)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double lightness;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (igs::image::rgba::siz == channels) {
Toshihiro Shimizu 890ddd
		using namespace igs::image::rgba;
Toshihiro Shimizu 890ddd
		rgb_to_lightness_(
Toshihiro Shimizu 890ddd
			pixel[red], pixel[gre], pixel[blu], lightness);
Toshihiro Shimizu 890ddd
	} else if (igs::image::rgb::siz == channels) {
Toshihiro Shimizu 890ddd
		using namespace igs::image::rgb;
Toshihiro Shimizu 890ddd
		rgb_to_lightness_(
Toshihiro Shimizu 890ddd
			pixel[red], pixel[gre], pixel[blu], lightness);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		lightness = pixel[0];
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	/* 指定があるなら白黒反転 */
Toshihiro Shimizu 890ddd
	// if (blow_dark_sw) { lightness = 1.0 - lightness; }
Toshihiro Shimizu 890ddd
	return lightness;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
int igs::motion_wind::pixel::change(
Toshihiro Shimizu 890ddd
	const bool key_reset_sw
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/***, const int ref_channel
Toshihiro Shimizu 890ddd
	, const double *ref_pixel***/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double ref_val /* ゼロ以上なら有効値、マイナスなら無効 */
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const int channels, double *pixel_tgt)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	/* 指定があるなら白黒反転、
Toshihiro Shimizu 890ddd
	このプログラム内ではすべて明るさで処理する */
Toshihiro Shimizu 890ddd
	if (this->blow_dark_sw_) {
Toshihiro Shimizu 890ddd
		invert_pixel_(channels, pixel_tgt);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* 現位置の明るさを求める */
Toshihiro Shimizu 890ddd
	const double crnt_lightness = get_lightness_(
Toshihiro Shimizu 890ddd
		channels, pixel_tgt // , this->blow_dark_sw_
Toshihiro Shimizu 890ddd
		);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* スキャンラインの始点のではkey pixel値のreset */
Toshihiro Shimizu 890ddd
	if (key_reset_sw) {
Toshihiro Shimizu 890ddd
		this->key_lightness_ = crnt_lightness;
Toshihiro Shimizu 890ddd
		for (int ii = 0; ii < channels; ++ii) {
Toshihiro Shimizu 890ddd
			this->pixel_key_[ii] = pixel_tgt[ii];
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* 値がkeyより小さい(山の頂上より小さい)時 */
Toshihiro Shimizu 890ddd
	if (crnt_lightness < this->key_lightness_) {
Toshihiro Shimizu 890ddd
		/* 風テーブルがゼロなら
Toshihiro Shimizu 890ddd
		風データを設定し、風テーブルを設定する */
Toshihiro Shimizu 890ddd
		if (0 == this->table_array_) {
Toshihiro Shimizu 890ddd
			double length_min = this->length_min_;
Toshihiro Shimizu 890ddd
			double length_max = this->length_max_;
Toshihiro Shimizu 890ddd
			double length_bias = this->length_bias_;
Toshihiro Shimizu 890ddd
			double force_min = this->force_min_;
Toshihiro Shimizu 890ddd
			double force_max = this->force_max_;
Toshihiro Shimizu 890ddd
			double force_bias = this->force_bias_;
Toshihiro Shimizu 890ddd
			double density_min = this->density_min_;
Toshihiro Shimizu 890ddd
			double density_max = this->density_max_;
Toshihiro Shimizu 890ddd
			double density_bias = this->density_bias_;
Toshihiro Shimizu 890ddd
#if 0
Toshihiro Shimizu 890ddd
			double ref_value;
Toshihiro Shimizu 890ddd
			/* 画像から強弱を得る */
Toshihiro Shimizu 890ddd
			if (0 != ref_pixel) {/* 参照画像のRGBAから */
Toshihiro Shimizu 890ddd
				ref_value = ref_pixel[ref_channel];
Toshihiro Shimizu 890ddd
			} else { /* 自画像から強弱を得る */
Toshihiro Shimizu 890ddd
				ref_value = get_lightness_(
Toshihiro Shimizu 890ddd
					channels, pixel_tgt);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
			double ref_value = ref_val;
Toshihiro Shimizu 890ddd
			/* 参照画像から取らないときは自画像から強弱を得る */
Toshihiro Shimizu 890ddd
			if (ref_value < 0.0) {
Toshihiro Shimizu 890ddd
				ref_value = get_lightness_(
Toshihiro Shimizu 890ddd
					channels, pixel_tgt);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (this->length_ref_sw_) {
Toshihiro Shimizu 890ddd
				length_min *= ref_value;
Toshihiro Shimizu 890ddd
				length_max *= ref_value;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			if (this->force_ref_sw_) {
Toshihiro Shimizu 890ddd
				force_min *= ref_value;
Toshihiro Shimizu 890ddd
				force_max *= ref_value;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			if (this->density_ref_sw_) {
Toshihiro Shimizu 890ddd
				density_min *= ref_value;
Toshihiro Shimizu 890ddd
				density_max *= ref_value;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			/* 風データを得る(流れのパターンの更新) */
Toshihiro Shimizu 890ddd
			/***std::cout
Toshihiro Shimizu 890ddd
	<< "lmin " << length_min << "  lmax " << length_max
Toshihiro Shimizu 890ddd
	<< "\ncmin " << force_min  << "  cmax " << force_max
Toshihiro Shimizu 890ddd
	<< "\nhmin " << density_min << "  hmax " << density_max
Toshihiro Shimizu 890ddd
	<< "\n"
Toshihiro Shimizu 890ddd
;***/
Toshihiro Shimizu 890ddd
			/* 更新した、風データを、カレントにセットする */
Toshihiro Shimizu 890ddd
			this->table_len_ = igs::motion_wind::make_table(
Toshihiro Shimizu 890ddd
				this->table_, this->length_random_, this->force_random_, this->density_random_, length_min, length_max, length_bias, force_min, force_max, force_bias, density_min, density_max, density_bias);
Toshihiro Shimizu 890ddd
			this->table_array_ = &this->table_.at(0);
Toshihiro Shimizu 890ddd
			this->table_pos_ = 1;
Toshihiro Shimizu 890ddd
			/***for (int ii=0;ii<this->table_len_;++ii) {</this->
Toshihiro Shimizu 890ddd
std::cout << this->table_array_[ii] << "\n";
Toshihiro Shimizu 890ddd
}***/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			/* 風の中である。
Toshihiro Shimizu 890ddd
			風の長さは1かもしれない... */
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/* 風に影響された値を出力値にいれる */
Toshihiro Shimizu 890ddd
		if (this->table_pos_ < this->table_len_) {
Toshihiro Shimizu 890ddd
			/* 風の計算 */
Toshihiro Shimizu 890ddd
			blow_wind_(
Toshihiro Shimizu 890ddd
				channels, this->pixel_key_, this->table_array_, this->table_pos_, this->blow_alpha_sw_, pixel_tgt);
Toshihiro Shimizu 890ddd
			++this->table_pos_;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			/* 保存すべき値となったので白黒反転を戻す */
Toshihiro Shimizu 890ddd
			if (this->blow_dark_sw_) {
Toshihiro Shimizu 890ddd
				invert_pixel_(channels, pixel_tgt);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			return true; /* 風ん中 */
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*	ここに来たということは、
Toshihiro Shimizu 890ddd
		風の外である、
Toshihiro Shimizu 890ddd
			つまり風の影響範囲からはずれたとき、
Toshihiro Shimizu 890ddd
			つまりtable_pos_がtable_len_から外れた、
Toshihiro Shimizu 890ddd
		あるいは、
Toshihiro Shimizu 890ddd
		値が等しいかkeyより大きい値のとき、
Toshihiro Shimizu 890ddd
			つまりcrnt_lightnessがkey_lightness_以上、
Toshihiro Shimizu 890ddd
		なので、現在値をメモリする(keyにいれる)
Toshihiro Shimizu 890ddd
	*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	this->key_lightness_ = crnt_lightness;
Toshihiro Shimizu 890ddd
	for (int ii = 0L; ii < channels; ++ii) {
Toshihiro Shimizu 890ddd
		this->pixel_key_[ii] = pixel_tgt[ii];
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* 風が途切れたので風テーブルをゼロとする */
Toshihiro Shimizu 890ddd
	this->table_array_ = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return false; /* 風の外 */
Toshihiro Shimizu 890ddd
}