Toshihiro Shimizu 890ddd
#include <vector></vector>
Toshihiro Shimizu 890ddd
#include <limits> // std::numeric_limits</limits>
Toshihiro Shimizu 890ddd
#include "igs_color_rgb_hls.h"
Toshihiro Shimizu 890ddd
#include "igs_math_random.h"
Toshihiro Shimizu 890ddd
#include "igs_ifx_common.h" // igs::image::rgba
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
/*------ バッファをまとめて確保(return or throwで自動解放) ------*/
Toshihiro Shimizu 890ddd
class noise_reference_
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	noise_reference_(
Toshihiro Shimizu 890ddd
		const int ww, const int hh, const double hue_range, const double lig_range, const double sat_range, const double alp_range, const unsigned long random_seed, const double near_blur, const int camera_x, const int camera_y, const int camera_w, const int camera_h) : w_(ww), h_(hh), nblur_(near_blur)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if (0 == ww) {
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (0 == hh) {
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/* 枚数ゼロ(rangeが全部ゼロ)なのでノイズかけない */
Toshihiro Shimizu 890ddd
		if ((0.0 == hue_range) && (0.0 == lig_range) && (0.0 == sat_range) && (0.0 == alp_range)) {
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/* memory確保 */
Toshihiro Shimizu 890ddd
		const int sz = ww * hh;
Toshihiro Shimizu 890ddd
		if (0.0 != hue_range) {
Toshihiro Shimizu 890ddd
			this->hue_array_.resize(sz);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (0.0 != lig_range) {
Toshihiro Shimizu 890ddd
			this->lig_array_.resize(sz);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (0.0 != sat_range) {
Toshihiro Shimizu 890ddd
			this->sat_array_.resize(sz);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (0.0 != alp_range) {
Toshihiro Shimizu 890ddd
			this->alp_array_.resize(sz);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/* タネを設定 */
Toshihiro Shimizu 890ddd
		igs::math::random hue_rand, lig_rand, sat_rand, alp_rand;
Toshihiro Shimizu 890ddd
		unsigned long step = 0;
Toshihiro Shimizu 890ddd
		if (0.0 != hue_range) {
Toshihiro Shimizu 890ddd
			hue_rand.seed(random_seed + step++);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (0.0 != lig_range) {
Toshihiro Shimizu 890ddd
			lig_rand.seed(random_seed + step++);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (0.0 != sat_range) {
Toshihiro Shimizu 890ddd
			sat_rand.seed(random_seed + step++);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (0.0 != alp_range) {
Toshihiro Shimizu 890ddd
			alp_rand.seed(random_seed + step++);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		igs::math::random hue_rand_ext, lig_rand_ext, sat_rand_ext, alp_rand_ext;
Toshihiro Shimizu 890ddd
		if (0.0 != hue_range) {
Toshihiro Shimizu 890ddd
			hue_rand_ext.seed(random_seed + step++);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (0.0 != lig_range) {
Toshihiro Shimizu 890ddd
			lig_rand_ext.seed(random_seed + step++);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (0.0 != sat_range) {
Toshihiro Shimizu 890ddd
			sat_rand_ext.seed(random_seed + step++);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (0.0 != alp_range) {
Toshihiro Shimizu 890ddd
			alp_rand_ext.seed(random_seed + step++);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/* ノイズを書く */
Toshihiro Shimizu 890ddd
		/* -???_range/2〜???_range/2 */
Toshihiro Shimizu 890ddd
		const int x1 = camera_x;
Toshihiro Shimizu 890ddd
		const int y1 = camera_y;
Toshihiro Shimizu 890ddd
		const int x2 = camera_x + camera_w - 1;
Toshihiro Shimizu 890ddd
		const int y2 = camera_y + camera_h - 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/* マージンあるかないかでノイズパターンが変わってしまうため、
Toshihiro Shimizu 890ddd
	near_blur用に広げてはいけない */
Toshihiro Shimizu 890ddd
		//if (0.0 != near_blur) { x1-=1; x2+=1; y1-=1; y2+=1; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (0.0 != hue_range) {
Toshihiro Shimizu 890ddd
			int pos = 0;
Toshihiro Shimizu 890ddd
			for (int yy = 0; yy < hh; ++yy) {
Toshihiro Shimizu 890ddd
				for (int xx = 0; xx < ww; ++xx, ++pos) {
Toshihiro Shimizu 890ddd
					if ((xx < x1) || (x2 < xx) || (yy < y1) || (y2 < yy)) {
Toshihiro Shimizu 890ddd
						/* 計算用マージンがあったらそこは別のパターン */
Toshihiro Shimizu 890ddd
						this->hue_array_[pos] = hue_range * (hue_rand_ext.next_d() - 0.5);
Toshihiro Shimizu 890ddd
					} else {
Toshihiro Shimizu 890ddd
						/* 計算用マージン幅が変化してもその中のノイズパターンは
Toshihiro Shimizu 890ddd
		変わらないようにマージン内は別計算 */
Toshihiro Shimizu 890ddd
						this->hue_array_[pos] = hue_range * (hue_rand.next_d() - 0.5);
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (0.0 != lig_range) {
Toshihiro Shimizu 890ddd
			int pos = 0;
Toshihiro Shimizu 890ddd
			for (int yy = 0; yy < hh; ++yy) {
Toshihiro Shimizu 890ddd
				for (int xx = 0; xx < ww; ++xx, ++pos) {
Toshihiro Shimizu 890ddd
					if ((xx < x1) || (x2 < xx) || (yy < y1) || (y2 < yy)) {
Toshihiro Shimizu 890ddd
						this->lig_array_[pos] = lig_range * (lig_rand_ext.next_d() - 0.5);
Toshihiro Shimizu 890ddd
					} else {
Toshihiro Shimizu 890ddd
						this->lig_array_[pos] = lig_range * (lig_rand.next_d() - 0.5);
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (0.0 != sat_range) {
Toshihiro Shimizu 890ddd
			int pos = 0;
Toshihiro Shimizu 890ddd
			for (int yy = 0; yy < hh; ++yy) {
Toshihiro Shimizu 890ddd
				for (int xx = 0; xx < ww; ++xx, ++pos) {
Toshihiro Shimizu 890ddd
					if ((xx < x1) || (x2 < xx) || (yy < y1) || (y2 < yy)) {
Toshihiro Shimizu 890ddd
						this->sat_array_[pos] = sat_range * (sat_rand_ext.next_d() - 0.5);
Toshihiro Shimizu 890ddd
					} else {
Toshihiro Shimizu 890ddd
						this->sat_array_[pos] = sat_range * (sat_rand.next_d() - 0.5);
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (0.0 != alp_range) {
Toshihiro Shimizu 890ddd
			int pos = 0;
Toshihiro Shimizu 890ddd
			for (int yy = 0; yy < hh; ++yy) {
Toshihiro Shimizu 890ddd
				for (int xx = 0; xx < ww; ++xx, ++pos) {
Toshihiro Shimizu 890ddd
					if ((xx < x1) || (x2 < xx) || (yy < y1) || (y2 < yy)) {
Toshihiro Shimizu 890ddd
						this->alp_array_[pos] = alp_range * (alp_rand_ext.next_d() - 0.5);
Toshihiro Shimizu 890ddd
					} else {
Toshihiro Shimizu 890ddd
						this->alp_array_[pos] = alp_range * (alp_rand.next_d() - 0.5);
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	double hue_value(const int xx, const int yy)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return this->noise_value_(
Toshihiro Shimizu 890ddd
			this->hue_array_, this->w_, this->h_, xx, yy, this->nblur_);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	double lig_value(const int xx, const int yy)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return this->noise_value_(
Toshihiro Shimizu 890ddd
			this->lig_array_, this->w_, this->h_, xx, yy, this->nblur_);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	double sat_value(const int xx, const int yy)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return this->noise_value_(
Toshihiro Shimizu 890ddd
			this->sat_array_, this->w_, this->h_, xx, yy, this->nblur_);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	double alp_value(const int xx, const int yy)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return this->noise_value_(
Toshihiro Shimizu 890ddd
			this->alp_array_, this->w_, this->h_, xx, yy, this->nblur_);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void clear()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		this->alp_array_.clear();
Toshihiro Shimizu 890ddd
		this->sat_array_.clear();
Toshihiro Shimizu 890ddd
		this->lig_array_.clear();
Toshihiro Shimizu 890ddd
		this->hue_array_.clear();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	~noise_reference_()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		this->clear();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	const int w_;
Toshihiro Shimizu 890ddd
	const int h_;
Toshihiro Shimizu 890ddd
	const double nblur_;
Toshihiro Shimizu 890ddd
	std::vector<double> hue_array_;</double>
Toshihiro Shimizu 890ddd
	std::vector<double> lig_array_;</double>
Toshihiro Shimizu 890ddd
	std::vector<double> sat_array_;</double>
Toshihiro Shimizu 890ddd
	std::vector<double> alp_array_;</double>
Toshihiro Shimizu 890ddd
	void accum_in_(
Toshihiro Shimizu 890ddd
		const double *noise_array, const int ww, const int hh, const int xx, const int yy, double &accum_val, int &accum_count)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if ((0 <= xx) && (xx < ww) && (0 <= yy) && (yy < hh)) {
Toshihiro Shimizu 890ddd
			accum_val += noise_array[yy * ww + xx];
Toshihiro Shimizu 890ddd
			++accum_count;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	double noise_value_(
Toshihiro Shimizu 890ddd
		const std::vector<double> &noise_vector, const int ww, const int hh, const int xx, const int yy, const double near_blur)</double>
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if (noise_vector.size() <= 0) {
Toshihiro Shimizu 890ddd
			return 0.0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		const double *noise_array = &noise_vector.at(0);
Toshihiro Shimizu 890ddd
		if (0.0 == near_blur) {
Toshihiro Shimizu 890ddd
			return noise_array[yy * ww + xx];
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		double accum_val = 0.0;
Toshihiro Shimizu 890ddd
		int accum_count = 0;
Toshihiro Shimizu 890ddd
		this->accum_in_(noise_array, ww, hh, xx - 1, yy - 1, accum_val, accum_count);
Toshihiro Shimizu 890ddd
		this->accum_in_(noise_array, ww, hh, xx, yy - 1, accum_val, accum_count);
Toshihiro Shimizu 890ddd
		this->accum_in_(noise_array, ww, hh, xx + 1, yy - 1, accum_val, accum_count);
Toshihiro Shimizu 890ddd
		this->accum_in_(noise_array, ww, hh, xx - 1, yy, accum_val, accum_count);
Toshihiro Shimizu 890ddd
		this->accum_in_(noise_array, ww, hh, xx + 1, yy, accum_val, accum_count);
Toshihiro Shimizu 890ddd
		this->accum_in_(noise_array, ww, hh, xx - 1, yy + 1, accum_val, accum_count);
Toshihiro Shimizu 890ddd
		this->accum_in_(noise_array, ww, hh, xx, yy + 1, accum_val, accum_count);
Toshihiro Shimizu 890ddd
		this->accum_in_(noise_array, ww, hh, xx + 1, yy + 1, accum_val, accum_count);
Toshihiro Shimizu 890ddd
		if (accum_count <= 0) {
Toshihiro Shimizu 890ddd
			return noise_array[yy * ww + xx];
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		accum_val /= static_cast<double>(accum_count);</double>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/* 中心Pixelと廻り(1Pixel幅)の平均値とのバランスを返す */
Toshihiro Shimizu 890ddd
		return (near_blur * accum_val) +
Toshihiro Shimizu 890ddd
			   ((1.0 - near_blur) * noise_array[yy * ww + xx]);
Toshihiro Shimizu 890ddd
		/*
Toshihiro Shimizu 890ddd
	ノイズをなじませるために、
Toshihiro Shimizu 890ddd
	8近傍ピクセルと対象ピクセルとの重み付けをする処理
Toshihiro Shimizu 890ddd
	+-----+-----+-----+
Toshihiro Shimizu 890ddd
	| p00 | p01 | p02 |
Toshihiro Shimizu 890ddd
	+-----+-----+-----+
Toshihiro Shimizu 890ddd
	| p10 | p11 | p12 |
Toshihiro Shimizu 890ddd
	+-----+-----+-----+
Toshihiro Shimizu 890ddd
	| p20 | p21 | p22 |
Toshihiro Shimizu 890ddd
	+-----+-----+-----+
Toshihiro Shimizu 890ddd
	対象ピクセル = p11
Toshihiro Shimizu 890ddd
	8近傍ピクセル = p00,p01,p02,p10,p12,p20,p21,p22
Toshihiro Shimizu 890ddd
	重み付け = near_blur = nb = 0...1
Toshihiro Shimizu 890ddd
	注意:対象ピクセルが画像のエッジにあると、8近傍ピクセルには、
Toshihiro Shimizu 890ddd
		存在しないピクセルがあるのでそれは除いて計算する
Toshihiro Shimizu 890ddd
	p00+p01+p02+p10+p12+p20+p21+p22
Toshihiro Shimizu 890ddd
	------------------------------- x nb + p22 x (1 - nb)
Toshihiro Shimizu 890ddd
			8
Toshihiro Shimizu 890ddd
					      nb
Toshihiro Shimizu 890ddd
	= (p00+p01+p02+p10+p12+p20+p21+p22) x -- + p22 x (1 - nb)
Toshihiro Shimizu 890ddd
					       8
Toshihiro Shimizu 890ddd
	nb == 0のとき
Toshihiro Shimizu 890ddd
		--> 対象ピクセルの値
Toshihiro Shimizu 890ddd
		= p22
Toshihiro Shimizu 890ddd
	nb == 0.5のとき
Toshihiro Shimizu 890ddd
		--> 対象が半分、8近傍平均値が半分
Toshihiro Shimizu 890ddd
		= (p00+p01+p02+p10+p12+p20+p21+p22) / 16 + p22 / 2
Toshihiro Shimizu 890ddd
	nb == 0.888...(8/9)のとき
Toshihiro Shimizu 890ddd
		--> 対象と8近傍クセルの平均値
Toshihiro Shimizu 890ddd
		= (p00+p01+p02+p10+p12+p20+p21+p22) x 8 / 72 + p22 / 9
Toshihiro Shimizu 890ddd
	nb == 1のとき
Toshihiro Shimizu 890ddd
		--> 8近傍ピクセルのみの平均値
Toshihiro Shimizu 890ddd
		= (p00+p01+p02+p10+p12+p20+p21+p22) / 8
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* copy constructorを無効化 */
Toshihiro Shimizu 890ddd
	noise_reference_(const noise_reference_ &);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* 代入演算子を無効化 */
Toshihiro Shimizu 890ddd
	noise_reference_ &operator=(const noise_reference_ &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
/*------ 端値を適度に調整する ------*/
Toshihiro Shimizu 890ddd
class control_term_within_limits_
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	control_term_within_limits_(
Toshihiro Shimizu 890ddd
		const double effective_low /* = 0.0 */
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		const double effective_high /* = 0.0 */
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		const double center /* = 0.5 */
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		const int type /* = 0   */
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		const double noise_range /* = 0.0 */
Toshihiro Shimizu 890ddd
		) : effective_low_(effective_low), effective_high_(effective_high), center_(center), type_(static_cast<term_type_>(type)), noise_range_(noise_range)</term_type_>
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void exec(
Toshihiro Shimizu 890ddd
		const double current_value /* 0...1 */
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		double &noise /* -noise_range/2...noise_range/2 */
Toshihiro Shimizu 890ddd
		,
Toshihiro Shimizu 890ddd
		double &shift_value)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if ((0.0 < this->effective_low_) && (current_value < this->center_)) {
Toshihiro Shimizu 890ddd
			const double cen = this->center_;
Toshihiro Shimizu 890ddd
			const double val = current_value;
Toshihiro Shimizu 890ddd
			const double ran = this->noise_range_;
Toshihiro Shimizu 890ddd
			const double eff = this->effective_low_;
Toshihiro Shimizu 890ddd
			switch (this->type_) {
Toshihiro Shimizu 890ddd
			case shift_all_:
Toshihiro Shimizu 890ddd
				shift_value = ((cen - val) / cen) * (ran / 2.0) * eff;
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			case shift_term_:
Toshihiro Shimizu 890ddd
				if (val < ran) {
Toshihiro Shimizu 890ddd
					shift_value = (((cen < ran) ? cen : ran) - val) / 2.0 * eff;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			case decrease_all_: {
Toshihiro Shimizu 890ddd
				const double tmp = (cen - val) / cen * eff;
Toshihiro Shimizu 890ddd
				if (0.0 < tmp) {
Toshihiro Shimizu 890ddd
					noise *= 1.0 - tmp;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			} break;
Toshihiro Shimizu 890ddd
			case decrease_term_:
Toshihiro Shimizu 890ddd
				if (val < (ran / 2.0)) {
Toshihiro Shimizu 890ddd
					const double stop = (cen < (ran / 2.0)) ? cen : ran / 2.0;
Toshihiro Shimizu 890ddd
					const double tmp = (stop - val) / stop * eff;
Toshihiro Shimizu 890ddd
					if (0.0 < tmp) {
Toshihiro Shimizu 890ddd
						noise *= 1.0 - tmp;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if ((0.0 < this->effective_high_) && (this->center_ < current_value)) {
Toshihiro Shimizu 890ddd
			const double cen = this->center_;
Toshihiro Shimizu 890ddd
			const double val = current_value;
Toshihiro Shimizu 890ddd
			const double ran = this->noise_range_;
Toshihiro Shimizu 890ddd
			const double eff = this->effective_high_;
Toshihiro Shimizu 890ddd
			switch (this->type_) {
Toshihiro Shimizu 890ddd
			case shift_all_:
Toshihiro Shimizu 890ddd
				shift_value = ((cen - val) / (1.0 - cen)) * (ran / 2.0) * eff;
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			case shift_term_:
Toshihiro Shimizu 890ddd
				if ((1.0 - ran) < val) {
Toshihiro Shimizu 890ddd
					const double ira = 1.0 - ran;
Toshihiro Shimizu 890ddd
					shift_value = (((cen < ira) ? ira : cen) - val) / 2.0 * eff;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			case decrease_all_: {
Toshihiro Shimizu 890ddd
				const double tmp = (val - cen) / (1.0 - cen) * eff;
Toshihiro Shimizu 890ddd
				if (0.0 < tmp) {
Toshihiro Shimizu 890ddd
					noise *= 1.0 - tmp;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			} break;
Toshihiro Shimizu 890ddd
			case decrease_term_:
Toshihiro Shimizu 890ddd
				if ((1.0 - (ran / 2.0)) < val) {
Toshihiro Shimizu 890ddd
					const double rpos = 1.0 - (ran / 2.0);
Toshihiro Shimizu 890ddd
					const double stop = (cen < rpos) ? rpos : cen;
Toshihiro Shimizu 890ddd
					const double tmp = (val - stop) / (1.0 - stop) * eff;
Toshihiro Shimizu 890ddd
					if (0.0 < tmp) {
Toshihiro Shimizu 890ddd
						noise *= 1.0 - tmp;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	double noise_range(void) const { return this->noise_range_; }
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	/* low,high両方ゼロ =exec()内処理せず =端値はカット =default */
Toshihiro Shimizu 890ddd
	const double effective_low_;
Toshihiro Shimizu 890ddd
	const double effective_high_;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const double center_;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	enum term_type_ {				  /* 端値の調整方法 */
Toshihiro Shimizu 890ddd
					  shift_all_ = 0, /* 0.全体的にノイズ位置がずれる */
Toshihiro Shimizu 890ddd
					  shift_term_,	/* 1.端のみでノイズ位置がずれる */
Toshihiro Shimizu 890ddd
					  decrease_all_,  /* 2.全体的にノイズ幅が減る */
Toshihiro Shimizu 890ddd
					  decrease_term_, /* 3.端のみでノイズ幅が減る */
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
	const term_type_ type_;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const double noise_range_;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* copy constructorを無効化 */
Toshihiro Shimizu 890ddd
	control_term_within_limits_(const control_term_within_limits_ &);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* 代入演算子を無効化 */
Toshihiro Shimizu 890ddd
	control_term_within_limits_ &operator=(const control_term_within_limits_ &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
/*------ RGB値にノイズをのせる ------*/
Toshihiro Shimizu 890ddd
void pixel_rgb_(
Toshihiro Shimizu 890ddd
	const double red_in, const double gre_in, const double blu_in, const double alp_in, const double hue_noise, const double lig_noise, const double sat_noise, control_term_within_limits_ &lig_term, control_term_within_limits_ &sat_term, double &red_out, double &gre_out, double &blu_out)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (0.0 == alp_in) {
Toshihiro Shimizu 890ddd
		red_out = red_in;
Toshihiro Shimizu 890ddd
		gre_out = gre_in;
Toshihiro Shimizu 890ddd
		blu_out = blu_in;
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	double hue, lig, sat;
Toshihiro Shimizu 890ddd
	igs::color::rgb_to_hls(red_in, gre_in, blu_in, hue, lig, sat);
Toshihiro Shimizu 890ddd
	if (0.0 != hue_noise) {
Toshihiro Shimizu 890ddd
		hue += 360.0 * hue_noise * alp_in;
Toshihiro Shimizu 890ddd
		while (hue < 0.0) {
Toshihiro Shimizu 890ddd
			hue += 360.0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		while (360.0 <= hue) {
Toshihiro Shimizu 890ddd
			hue -= 360.0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (0.0 != lig_term.noise_range()) {
Toshihiro Shimizu 890ddd
		double shift_value = 0;
Toshihiro Shimizu 890ddd
		double lignoise = lig_noise;
Toshihiro Shimizu 890ddd
		lig_term.exec(lig, lignoise, shift_value);
Toshihiro Shimizu 890ddd
		lig += shift_value * alp_in;
Toshihiro Shimizu 890ddd
		lig += lignoise * alp_in;
Toshihiro Shimizu 890ddd
		if (lig < 0.0) {
Toshihiro Shimizu 890ddd
			lig = 0.0;
Toshihiro Shimizu 890ddd
		} else if (1.0 < lig) {
Toshihiro Shimizu 890ddd
			lig = 1.0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (0.0 != sat_term.noise_range()) {
Toshihiro Shimizu 890ddd
		double shift_value = 0;
Toshihiro Shimizu 890ddd
		double satnoise = sat_noise;
Toshihiro Shimizu 890ddd
		sat_term.exec(sat, satnoise, shift_value);
Toshihiro Shimizu 890ddd
		sat += shift_value * alp_in;
Toshihiro Shimizu 890ddd
		sat += satnoise * alp_in;
Toshihiro Shimizu 890ddd
		if (sat < 0.0) {
Toshihiro Shimizu 890ddd
			sat = 0.0;
Toshihiro Shimizu 890ddd
		} else if (1.0 < sat) {
Toshihiro Shimizu 890ddd
			sat = 1.0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		// if( 0.0 == sat ) hue = -1.0; // hls_to_rgb(-)
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	igs::color::hls_to_rgb(hue, lig, sat, red_out, gre_out, blu_out);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
/*------ Alpha値にノイズをのせる ------*/
Toshihiro Shimizu 890ddd
void pixel_a_(
Toshihiro Shimizu 890ddd
	const double alp_in, const double alp_noise, control_term_within_limits_ &alp_term, double &alp_out)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	// if (0.0 == alp_in) { return; }
Toshihiro Shimizu 890ddd
	double alpin = alp_in;
Toshihiro Shimizu 890ddd
	if (0.0 != alp_term.noise_range()) {
Toshihiro Shimizu 890ddd
		double shift_value = 0.0;
Toshihiro Shimizu 890ddd
		double alpnoise = alp_noise;
Toshihiro Shimizu 890ddd
		alp_term.exec(alpin, alpnoise, shift_value);
Toshihiro Shimizu 890ddd
		const double mask = alpin;
Toshihiro Shimizu 890ddd
		alpin += shift_value * mask;
Toshihiro Shimizu 890ddd
		alpin += alpnoise * mask;
Toshihiro Shimizu 890ddd
		if (alpin < 0.0) {
Toshihiro Shimizu 890ddd
			alpin = 0.0;
Toshihiro Shimizu 890ddd
		} else if (1.0 < alpin) {
Toshihiro Shimizu 890ddd
			alpin = 1.0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	alp_out = alpin;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
/*------ raster画像にノイズをのせるtemplate ------*/
Toshihiro Shimizu 890ddd
template <class class="" it,="" rt=""></class>
Toshihiro Shimizu 890ddd
void change_template_(
Toshihiro Shimizu 890ddd
	IT *image_array, const int width, const int height, const int channels
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	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
	,
Toshihiro Shimizu 890ddd
	noise_reference_ &noise, const double hue_range, control_term_within_limits_ &lig_term, control_term_within_limits_ &sat_term, control_term_within_limits_ &alp_term
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const bool add_blend_sw)
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
	const double mul_val = static_cast<double>(t_max) + 0.999999;</double>
Toshihiro Shimizu 890ddd
	const int r_max = std::numeric_limits<rt>::max();</rt>
Toshihiro Shimizu 890ddd
	if (igs::image::rgba::siz == channels) {
Toshihiro Shimizu 890ddd
		using namespace igs::image::rgba;
Toshihiro Shimizu 890ddd
		for (int yy = 0; yy < height; ++yy) {
Toshihiro Shimizu 890ddd
			for (int xx = 0; xx < width; ++xx, image_array += channels) {
Toshihiro Shimizu 890ddd
				/* 変化量初期値 */
Toshihiro Shimizu 890ddd
				double refv = 1.0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				/* 参照画像あればピクセル単位の画像変化量を得る */
Toshihiro Shimizu 890ddd
				if (ref != 0) {
Toshihiro Shimizu 890ddd
					refv *= igs::color::ref_value(
Toshihiro Shimizu 890ddd
						ref, channels, r_max, ref_mode);
Toshihiro Shimizu 890ddd
					ref += channels; /* continue;の前に行うこと */
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				/* 加算合成で、Alpha値ゼロならRGB値を計算する必要はない */
Toshihiro Shimizu 890ddd
				if (add_blend_sw && (0 == image_array[alp])) {
Toshihiro Shimizu 890ddd
					continue;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				/* 加算合成でなくAlpha合成の時は、
Toshihiro Shimizu 890ddd
		Alpha値がゼロでもRGB値は存在する(してもよい) */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				/* マスクSWがON、なら変化をMask */
Toshihiro Shimizu 890ddd
				if (add_blend_sw && (image_array[alp] < t_max)) {
Toshihiro Shimizu 890ddd
					refv *= static_cast<double>(image_array[alp]) / div_val;</double>
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if (
Toshihiro Shimizu 890ddd
					((0.0 != hue_range) || (0.0 != lig_term.noise_range()) || (0.0 != sat_term.noise_range())) /* ノイズがhlsのどれか一つはある */
Toshihiro Shimizu 890ddd
					) {
Toshihiro Shimizu 890ddd
					double rr1 = static_cast<double>(image_array[red]) / div_val, gg1 = static_cast<double>(image_array[gre]) / div_val, bb1 = static_cast<double>(image_array[blu]) / div_val, aa1 = static_cast<double>(image_array[alp]) / div_val;</double></double></double></double>
Toshihiro Shimizu 890ddd
					double rr2 = 0, gg2 = 0, bb2 = 0;
Toshihiro Shimizu 890ddd
					pixel_rgb_(
Toshihiro Shimizu 890ddd
						rr1, gg1, bb1, aa1, noise.hue_value(xx, yy), noise.lig_value(xx, yy), noise.sat_value(xx, yy), lig_term, sat_term, rr2, gg2, bb2);
Toshihiro Shimizu 890ddd
					if (refv != 1.0) {
Toshihiro Shimizu 890ddd
						rr2 = (rr2 - rr1) * refv + rr1;
Toshihiro Shimizu 890ddd
						gg2 = (gg2 - gg1) * refv + gg1;
Toshihiro Shimizu 890ddd
						bb2 = (bb2 - bb1) * refv + bb1;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
					image_array[red] = static_cast<it>(rr2 * mul_val);</it>
Toshihiro Shimizu 890ddd
					image_array[gre] = static_cast<it>(gg2 * mul_val);</it>
Toshihiro Shimizu 890ddd
					image_array[blu] = static_cast<it>(bb2 * mul_val);</it>
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				if (0.0 != alp_term.noise_range()) {
Toshihiro Shimizu 890ddd
					double aa1 = static_cast<double>(image_array[alp]) / div_val;</double>
Toshihiro Shimizu 890ddd
					double aa2 = 0;
Toshihiro Shimizu 890ddd
					pixel_a_(aa1, noise.alp_value(xx, yy), alp_term, aa2);
Toshihiro Shimizu 890ddd
					if (refv != 1.0) {
Toshihiro Shimizu 890ddd
						aa2 = (aa2 - aa1) * refv + aa1;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
					image_array[alp] = static_cast<it>(aa2 * mul_val);</it>
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else if (igs::image::rgb::siz == channels) {
Toshihiro Shimizu 890ddd
		using namespace igs::image::rgb;
Toshihiro Shimizu 890ddd
		if (
Toshihiro Shimizu 890ddd
			((0.0 != hue_range) || (0.0 != lig_term.noise_range()) || (0.0 != sat_term.noise_range())) /* ノイズがhlsのどれか一つはある */
Toshihiro Shimizu 890ddd
			) {
Toshihiro Shimizu 890ddd
			for (int yy = 0; yy < height; ++yy) {
Toshihiro Shimizu 890ddd
				for (int xx = 0; xx < width; ++xx, image_array += channels) {
Toshihiro Shimizu 890ddd
					/* 変化量初期値 */
Toshihiro Shimizu 890ddd
					double refv = 1.0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					/* 参照画像あればピクセル単位の画像変化量を得る */
Toshihiro Shimizu 890ddd
					if (ref != 0) {
Toshihiro Shimizu 890ddd
						refv *= igs::color::ref_value(
Toshihiro Shimizu 890ddd
							ref, channels, r_max, ref_mode);
Toshihiro Shimizu 890ddd
						ref += channels; /* continue;の前に行うこと */
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					double rr1 = static_cast<double>(image_array[red]) / div_val, gg1 = static_cast<double>(image_array[gre]) / div_val, bb1 = static_cast<double>(image_array[blu]) / div_val;</double></double></double>
Toshihiro Shimizu 890ddd
					double rr2 = 0, gg2 = 0, bb2 = 0;
Toshihiro Shimizu 890ddd
					pixel_rgb_(
Toshihiro Shimizu 890ddd
						rr1, gg1, bb1, 1.0, noise.hue_value(xx, yy), noise.lig_value(xx, yy), noise.sat_value(xx, yy), lig_term, sat_term, rr2, gg2, bb2);
Toshihiro Shimizu 890ddd
					if (refv != 1.0) {
Toshihiro Shimizu 890ddd
						rr2 = (rr2 - rr1) * refv + rr1;
Toshihiro Shimizu 890ddd
						gg2 = (gg2 - gg1) * refv + gg1;
Toshihiro Shimizu 890ddd
						bb2 = (bb2 - bb1) * refv + bb1;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
					image_array[red] = static_cast<it>(rr2 * mul_val);</it>
Toshihiro Shimizu 890ddd
					image_array[gre] = static_cast<it>(gg2 * mul_val);</it>
Toshihiro Shimizu 890ddd
					image_array[blu] = static_cast<it>(bb2 * mul_val);</it>
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else if (1 == channels) { /* grayscale */
Toshihiro Shimizu 890ddd
		if (0.0 != lig_term.noise_range()) {
Toshihiro Shimizu 890ddd
			for (int yy = 0; yy < height; ++yy) {
Toshihiro Shimizu 890ddd
				for (int xx = 0; xx < width; ++xx, ++image_array) {
Toshihiro Shimizu 890ddd
					/* 変化量初期値 */
Toshihiro Shimizu 890ddd
					double refv = 1.0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					/* 参照画像あればピクセル単位の画像変化量を得る */
Toshihiro Shimizu 890ddd
					if (ref != 0) {
Toshihiro Shimizu 890ddd
						refv *= igs::color::ref_value(
Toshihiro Shimizu 890ddd
							ref, channels, r_max, ref_mode);
Toshihiro Shimizu 890ddd
						ref += channels; /* continue;の前に行うこと */
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					double li1 = static_cast<double>(image_array[0]) / div_val;</double>
Toshihiro Shimizu 890ddd
					double shift_value = 0;
Toshihiro Shimizu 890ddd
					double lig_noise = noise.lig_value(xx, yy);
Toshihiro Shimizu 890ddd
					lig_term.exec(li1, lig_noise, shift_value);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					double li2 = li1;
Toshihiro Shimizu 890ddd
					li2 += shift_value;
Toshihiro Shimizu 890ddd
					li2 += lig_noise;
Toshihiro Shimizu 890ddd
					li2 = (li2 < 0.0) ? 0.0 : ((1.0 < li2) ? 1.0 : li2);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					if (refv != 1.0) {
Toshihiro Shimizu 890ddd
						li2 = li1 + (li2 - li1) * refv;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					image_array[0] = static_cast<it>(li2 * mul_val);</it>
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
#include <stdexcept> // std::domain_error</stdexcept>
Toshihiro Shimizu 890ddd
#include "igs_hls_noise.h"
Toshihiro Shimizu 890ddd
void igs::hls_noise::change(
Toshihiro Shimizu 890ddd
	unsigned char *image_array
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const int height, const int width, const int channels, const int bits
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const unsigned char *ref /* 求める画像と同じ高、幅、channels数 */
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const int ref_bits /* refがゼロのときはここもゼロ */
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
	/* image_arrayに余白が変化してもノイズパターンが変わらない
Toshihiro Shimizu 890ddd
		ようにするためにカメラエリアを指定する */
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const int camera_x, const int camera_y, const int camera_w, const int camera_h
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double hue_range, const double lig_range, const double sat_range, const double alp_range, const unsigned long random_seed, const double near_blur
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double lig_effective, const double lig_center, const int lig_type, const double sat_effective, const double sat_center, const int sat_type, const double alp_effective, const double alp_center, const int alp_type
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const bool add_blend_sw)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if ((0.0 == hue_range) && (0.0 == lig_range) && (0.0 == sat_range) && (0.0 == alp_range)) {
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if ((igs::image::rgba::siz != channels) && (igs::image::rgb::siz != channels) && (1 != channels) /* grayscale */
Toshihiro Shimizu 890ddd
		) {
Toshihiro Shimizu 890ddd
		throw std::domain_error("Bad channels,Not rgba/rgb/grayscale");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* ノイズ参照画像を作成する */
Toshihiro Shimizu 890ddd
	noise_reference_ noise(
Toshihiro Shimizu 890ddd
		width, height, hue_range, lig_range, sat_range, alp_range, random_seed, near_blur, camera_x, camera_y, camera_w, camera_h);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* 端値を適度に調整する設定 */
Toshihiro Shimizu 890ddd
	control_term_within_limits_ lig_term(
Toshihiro Shimizu 890ddd
		lig_effective, lig_effective, lig_center, lig_type, lig_range);
Toshihiro Shimizu 890ddd
	control_term_within_limits_ sat_term(
Toshihiro Shimizu 890ddd
		sat_effective, sat_effective, sat_center, sat_type, sat_range);
Toshihiro Shimizu 890ddd
	control_term_within_limits_ alp_term(
Toshihiro Shimizu 890ddd
		alp_effective, alp_effective, alp_center, alp_type, alp_range);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* rgb(a)画像にhls(a)でドットノイズを加える */
Toshihiro Shimizu 890ddd
	if ((std::numeric_limits<unsigned char="">::digits == bits) && ((std::numeric_limits<unsigned char="">::digits == ref_bits) || (0 == ref_bits))) {</unsigned></unsigned>
Toshihiro Shimizu 890ddd
		change_template_(
Toshihiro Shimizu 890ddd
			image_array, width, height, channels, ref, ref_mode, noise, hue_range, lig_term, sat_term, alp_term, add_blend_sw);
Toshihiro Shimizu 890ddd
		noise.clear(); /* ノイズ画像メモリ解放 */
Toshihiro Shimizu 890ddd
	} else if ((std::numeric_limits<unsigned short="">::digits == bits) && ((std::numeric_limits<unsigned char="">::digits == ref_bits) || (0 == ref_bits))) {</unsigned></unsigned>
Toshihiro Shimizu 890ddd
		change_template_(
Toshihiro Shimizu 890ddd
			reinterpret_cast<unsigned *="" short="">(image_array), width, height, channels, ref, ref_mode, noise, hue_range, lig_term, sat_term, alp_term, add_blend_sw);</unsigned>
Toshihiro Shimizu 890ddd
		noise.clear(); /* ノイズ画像メモリ解放 */
Toshihiro Shimizu 890ddd
	} else if ((std::numeric_limits<unsigned short="">::digits == bits) && (std::numeric_limits<unsigned short="">::digits == ref_bits)) {</unsigned></unsigned>
Toshihiro Shimizu 890ddd
		change_template_(
Toshihiro Shimizu 890ddd
			reinterpret_cast<unsigned *="" short="">(image_array), width, height, channels, reinterpret_cast<const *="" short="" unsigned="">(ref), ref_mode, noise, hue_range, lig_term, sat_term, alp_term, add_blend_sw);</const></unsigned>
Toshihiro Shimizu 890ddd
		noise.clear(); /* ノイズ画像メモリ解放 */
Toshihiro Shimizu 890ddd
	} else if ((std::numeric_limits<unsigned char="">::digits == bits) && (std::numeric_limits<unsigned short="">::digits == ref_bits)) {</unsigned></unsigned>
Toshihiro Shimizu 890ddd
		change_template_(
Toshihiro Shimizu 890ddd
			image_array, width, height, channels, reinterpret_cast<const *="" short="" unsigned="">(ref), ref_mode, noise, hue_range, lig_term, sat_term, alp_term, add_blend_sw);</const>
Toshihiro Shimizu 890ddd
		noise.clear(); /* ノイズ画像メモリ解放 */
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		throw std::domain_error("Bad bits,Not uchar/ushort");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}