Toshihiro Shimizu 890ddd
#ifndef igs_maxmin_getput_h
Toshihiro Shimizu 890ddd
#define igs_maxmin_getput_h
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <vector></vector>
Toshihiro Shimizu 890ddd
#include <limits>			/* std::numeric_limits<->::max()</limits>
Toshihiro Shimizu 890ddd
		--> (std::numeric_limits<->::max)() */
Toshihiro Shimizu 890ddd
#include "igs_ifx_common.h" // igs::color::ref_value(-)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
/* 画像配列の高さ位置を、実際の範囲内にclampし、scanlineの先頭を返す */
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
const T *csl_top_clamped_in_h_(
Toshihiro Shimizu 890ddd
	const T *top, const int height, const int width, const int channels, const int yy)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (height <= yy) {
Toshihiro Shimizu 890ddd
		return top + (channels * width * (height - 1));
Toshihiro Shimizu 890ddd
	} else if (yy < 0) {
Toshihiro Shimizu 890ddd
		return top;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return top + (channels * width * yy);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
T *sl_out_clamped_in_h_(
Toshihiro Shimizu 890ddd
	T *out, const int height, const int width, const int channels, const int yy)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (height <= yy) {
Toshihiro Shimizu 890ddd
		return out + (channels * width * (height - 1));
Toshihiro Shimizu 890ddd
	} else if (yy < 0) {
Toshihiro Shimizu 890ddd
		return out;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return out + (channels * width * yy);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
/* trackの余白を画像の端の色で塗りつぶす(余白部分をtracksの前後にコピー) */
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
void paint_margin_(
Toshihiro Shimizu 890ddd
	const int margin, std::vector<t> &track)</t>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int xx;
Toshihiro Shimizu 890ddd
	/* 始点側の余白を塗りつぶす */
Toshihiro Shimizu 890ddd
	for (xx = 0; xx < margin; ++xx) {
Toshihiro Shimizu 890ddd
		track.at(xx) = track.at(margin);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	/* 終端側の余白を塗りつぶす */
Toshihiro Shimizu 890ddd
	for (xx = 0; xx < margin; ++xx) {
Toshihiro Shimizu 890ddd
		track.at(track.size() - 1 - xx) = track.at(track.size() - 1 - margin);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
/* 入力画像を計算用のtrackバッファーの真ん中に正規化して入れる */
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
void inn_to_track_(
Toshihiro Shimizu 890ddd
	const T *sl, const int width, const int channels, const double div_val, const int margin, std::vector<double> &track)</double>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	for (int xx = 0; xx < width; ++xx) {
Toshihiro Shimizu 890ddd
		track.at(margin + xx) = sl[xx * channels] / div_val;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
/* 入力画像を正規化して、結果を置く場所(result)に初期値として入れる */
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
void inn_to_result_(
Toshihiro Shimizu 890ddd
	const T *inn, const int height, const int width, const int channels, const int yy, const int zz, const double div_val, std::vector<double> &result /* 元値をいれといて、結果を入れる */</double>
Toshihiro Shimizu 890ddd
	)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	const T *ss = csl_top_clamped_in_h_(inn, height, width, channels, yy) + zz;
Toshihiro Shimizu 890ddd
	for (int xx = 0; xx < width; ++xx) {
Toshihiro Shimizu 890ddd
		result.at(xx) = ss[xx * channels] / div_val;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
/* Scanlineで、効果を調節するデータ(alpha_ref)に、初期値を与える */
Toshihiro Shimizu 890ddd
void alpha_ref_init_one_(
Toshihiro Shimizu 890ddd
	const int width, std::vector<double> &alpha_ref)</double>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	for (int xx = 0; xx < width; ++xx) {
Toshihiro Shimizu 890ddd
		alpha_ref.at(xx) = 1.0; /* 全面的に処理を加える値で埋める */
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
/* Scanlineで、効果を調節するデータ(alpha_ref)に、画像の影響を与える */
Toshihiro Shimizu 890ddd
template <class rt=""></class>
Toshihiro Shimizu 890ddd
void alpha_ref_mul_ref_(
Toshihiro Shimizu 890ddd
	const RT *ref, const int height, const int width, const int channels, const int yy, const int ref_mode, std::vector<double> &alpha_ref)</double>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	const int r_max = (std::numeric_limits<rt>::max)();</rt>
Toshihiro Shimizu 890ddd
	const RT *rr = csl_top_clamped_in_h_(ref, height, width, channels, yy);
Toshihiro Shimizu 890ddd
	for (int xx = 0; xx < width; ++xx) {
Toshihiro Shimizu 890ddd
		alpha_ref.at(xx) *= igs::color::ref_value(
Toshihiro Shimizu 890ddd
			&rr[xx * channels], channels, r_max, ref_mode);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
/* Scanlineで、効果を調節するデータ(alpha_ref)に、
Toshihiro Shimizu 890ddd
	計算済み画像(out)のalphaチャンネル値の影響を与える */
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
void alpha_ref_mul_alpha_(
Toshihiro Shimizu 890ddd
	const T *out, const int height, const int width, const int channels, const int yy, const double div_val, std::vector<double> &alpha_ref)</double>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	const T *dd = csl_top_clamped_in_h_(out, height, width, channels, yy) + 3;
Toshihiro Shimizu 890ddd
	for (int xx = 0; xx < width; ++xx) {
Toshihiro Shimizu 890ddd
		alpha_ref.at(xx) *= dd[xx * channels] / div_val;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
namespace igs
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
namespace maxmin
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
namespace getput
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
/* 1番初めのスキャンラインのセット
Toshihiro Shimizu 890ddd
	* IT is 'unsigned char' or 'unsigned short'
Toshihiro Shimizu 890ddd
	* alphaは先に処理して結果(out)に入れ、
Toshihiro Shimizu 890ddd
		その後rgbのために参照する
Toshihiro Shimizu 890ddd
   */
Toshihiro Shimizu 890ddd
template <class class="" it,="" rt=""></class>
Toshihiro Shimizu 890ddd
void get_first(
Toshihiro Shimizu 890ddd
	const IT *inn /* outと同じ高さ、幅、チャンネル数 */
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const IT *out /* outの処理結果alpha値をinとして使用 */
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const int hh, const int ww, const int ch, const RT *ref /* outと同じ高さ、幅、チャンネル数 */
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const int ref_mode /* 0=R,1=G,2=B,3=A,4=Luminance,5=Nothing */
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const int yy, const int zz, const int margin, const bool add_blend_sw, std::vector<std::vector<double>> &tracks /* sl影響範囲のpixel値 */</std::vector<double>
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	std::vector<double> &alpha_ref /* pixel毎の変化の割合 */</double>
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	std::vector<double> &result /* 元値をいれといて、結果を入れる */</double>
Toshihiro Shimizu 890ddd
	)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	const int t_max = (std::numeric_limits<it>::max)();</it>
Toshihiro Shimizu 890ddd
	const double div_val = static_cast<double>(t_max);</double>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* 計算範囲の画像値を計算バッファ(tracks)に入れる */
Toshihiro Shimizu 890ddd
	int ii = margin * 2;
Toshihiro Shimizu 890ddd
	for (int yp = -margin + yy; yp <= margin + yy; ++yp, --ii) {
Toshihiro Shimizu 890ddd
		const IT *sl = csl_top_clamped_in_h_(inn, hh, ww, ch, yp) + zz;
Toshihiro Shimizu 890ddd
		inn_to_track_(sl, ww, ch, div_val, margin, tracks.at(ii));
Toshihiro Shimizu 890ddd
		paint_margin_(margin, tracks.at(ii));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	inn_to_result_(inn, hh, ww, ch, yy, zz, div_val, result);
Toshihiro Shimizu 890ddd
	if (alpha_ref.size() <= 0) {
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	} /* alphaチャンネルを計算する場合 */
Toshihiro Shimizu 890ddd
	alpha_ref_init_one_(ww, alpha_ref);
Toshihiro Shimizu 890ddd
	if (ref != 0) {
Toshihiro Shimizu 890ddd
		alpha_ref_mul_ref_(ref, hh, ww, ch, yy, ref_mode, alpha_ref);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (ch < 4) {
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	} /* alphaチャンネルがない場合はここで終わり */
Toshihiro Shimizu 890ddd
	if (add_blend_sw) {
Toshihiro Shimizu 890ddd
		alpha_ref_mul_alpha_(out, hh, ww, ch, yy, div_val, alpha_ref);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
/*--- 2番以後のスキャンラインのセット -------------------------------*/
Toshihiro Shimizu 890ddd
template <class class="" it,="" rt=""></class>
Toshihiro Shimizu 890ddd
void get_next(
Toshihiro Shimizu 890ddd
	const IT *inn /* outと同じ高さ、幅、チャンネル数 */
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const IT *out /* outの処理結果alpha値をinとして使用 */
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const int hh, const int ww, const int ch, const RT *ref /* 求める画像(out)と同じ高さ、幅、チャンネル数 */
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const int ref_mode /* 0=R,1=G,2=B,3=A,4=Luminance,5=Nothing */
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const int yy, const int zz, const int margin, const bool add_blend_sw, std::vector<std::vector<double>> &tracks /* sl影響範囲のpixel値 */</std::vector<double>
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	std::vector<double> &alpha_ref /* pixel毎の変化の割合 */</double>
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	std::vector<double> &result /* 元値をいれといて、結果を入れる */</double>
Toshihiro Shimizu 890ddd
	)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	const int t_max = (std::numeric_limits<it>::max)();</it>
Toshihiro Shimizu 890ddd
	const double div_val = static_cast<double>(t_max);</double>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const IT *sl = csl_top_clamped_in_h_(inn, hh, ww, ch, yy + margin) + zz;
Toshihiro Shimizu 890ddd
	inn_to_track_(sl, ww, ch, div_val, margin, tracks.at(0));
Toshihiro Shimizu 890ddd
	paint_margin_(margin, tracks.at(0));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	inn_to_result_(inn, hh, ww, ch, yy, zz, div_val, result);
Toshihiro Shimizu 890ddd
	if (alpha_ref.size() <= 0) {
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	} /* alphaチャンネルを計算する場合 */
Toshihiro Shimizu 890ddd
	alpha_ref_init_one_(ww, alpha_ref);
Toshihiro Shimizu 890ddd
	if (ref != 0) {
Toshihiro Shimizu 890ddd
		alpha_ref_mul_ref_(ref, hh, ww, ch, yy, ref_mode, alpha_ref);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (ch < 4) {
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	} /* alphaチャンネルがない場合はここで終わり */
Toshihiro Shimizu 890ddd
	if (add_blend_sw) {
Toshihiro Shimizu 890ddd
		alpha_ref_mul_alpha_(out, hh, ww, ch, yy, div_val, alpha_ref);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
void copy(
Toshihiro Shimizu 890ddd
	const T *inn, const int hh, const int ww, const int ch, const int yy, const int zz, T *out)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	const T *ss = csl_top_clamped_in_h_(inn, hh, ww, ch, yy) + zz;
Toshihiro Shimizu 890ddd
	T *dd = sl_out_clamped_in_h_(out, hh, ww, ch, yy) + zz;
Toshihiro Shimizu 890ddd
	for (int xx = 0; xx < ww; ++xx) {
Toshihiro Shimizu 890ddd
		dd[ch * xx] = ss[ch * xx];
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
void put(
Toshihiro Shimizu 890ddd
	const std::vector<double> &result, const int hh, const int ww, const int ch, const int yy, const int zz, T *out)</double>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	const int t_max = (std::numeric_limits<t>::max)();</t>
Toshihiro Shimizu 890ddd
	const double mul_val = static_cast<double>(t_max) + 0.999999;</double>
Toshihiro Shimizu 890ddd
	T *dd = sl_out_clamped_in_h_(out, hh, ww, ch, yy) + zz;
Toshihiro Shimizu 890ddd
	for (int xx = 0; xx < ww; ++xx) {
Toshihiro Shimizu 890ddd
		dd[ch * xx] = static_cast<t>(result.at(xx) * mul_val);</t>
Toshihiro Shimizu 890ddd
		//std::cout << " (" << result.at(xx) << ")(" << (int)dd[ch * xx] << ")";
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	//std::cout << std::endl;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
#endif /* !igs_maxmin_getput_h */