Toshihiro Shimizu 890ddd
#include <vector></vector>
Toshihiro Shimizu 890ddd
#include <stdexcept> // std::domain_error</stdexcept>
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
#include "igs_hls_noise_in_camera.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace igs
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
namespace hls_noise_in_camera
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
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) : 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
		int x1 = camera_x;
Toshihiro Shimizu 890ddd
		int y1 = camera_y;
Toshihiro Shimizu 890ddd
		int x2 = camera_x + camera_w - 1;
Toshihiro Shimizu 890ddd
		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
						this->hue_array_[pos] = hue_range * (hue_rand_ext.next_d() - 0.5);
Toshihiro Shimizu 890ddd
					} else {
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
	/* 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
}
Toshihiro Shimizu 890ddd
namespace igs
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
namespace hls_noise_in_camera
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, const double effective_high = 0.0, const double center = 0.5, const int type = 0, const double noise_range = 0.0) : effective_low_(effective_low),
Toshihiro Shimizu 890ddd
																																							  effective_high_(effective_high),
Toshihiro Shimizu 890ddd
																																							  center_(center),
Toshihiro Shimizu 890ddd
																																							  type_(static_cast<term_type_>(type)),</term_type_>
Toshihiro Shimizu 890ddd
																																							  noise_range_(noise_range)
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_) &&
Toshihiro Shimizu 890ddd
			(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_) &&
Toshihiro Shimizu 890ddd
			(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
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
namespace igs
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
namespace hls_noise_in_camera
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
/* 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
}
Toshihiro Shimizu 890ddd
void igs::hls_noise_in_camera::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
void igs::hls_noise_in_camera::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
namespace igs
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
namespace hls_noise_in_camera
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
/* raster画像にノイズをのせるtemplate */
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
void change_template_(
Toshihiro Shimizu 890ddd
	T *image_array, const int ww, const int hh, const int ch, 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
	const double div_val =
Toshihiro Shimizu 890ddd
		static_cast<double>(std::numeric_limits<t>::max());</t></double>
Toshihiro Shimizu 890ddd
	const double mul_val =
Toshihiro Shimizu 890ddd
		static_cast<double>(std::numeric_limits<t>::max()) + 0.999999;</t></double>
Toshihiro Shimizu 890ddd
	double rr, gg, bb, aa;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (igs::image::rgba::siz == ch) {
Toshihiro Shimizu 890ddd
		using namespace igs::image::rgba;
Toshihiro Shimizu 890ddd
		for (int yy = 0; yy < hh; ++yy) {
Toshihiro Shimizu 890ddd
			for (int xx = 0; xx < ww; ++xx) {
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
					pixel_rgb(
Toshihiro Shimizu 890ddd
						static_cast<double>(image_array[red]) / div_val, static_cast<double>(image_array[gre]) / div_val, static_cast<double>(image_array[blu]) / div_val, static_cast<double>(image_array[alp]) / div_val, noise.hue_value(xx, yy), noise.lig_value(xx, yy), noise.sat_value(xx, yy), lig_term, sat_term, rr, gg, bb);</double></double></double></double>
Toshihiro Shimizu 890ddd
					image_array[red] = static_cast<t>(rr * mul_val);</t>
Toshihiro Shimizu 890ddd
					image_array[gre] = static_cast<t>(gg * mul_val);</t>
Toshihiro Shimizu 890ddd
					image_array[blu] = static_cast<t>(bb * mul_val);</t>
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				if (0.0 != alp_term.noise_range()) {
Toshihiro Shimizu 890ddd
					pixel_a(
Toshihiro Shimizu 890ddd
						static_cast<double>(image_array[alp]) / div_val, noise.alp_value(xx, yy), alp_term, aa);</double>
Toshihiro Shimizu 890ddd
					image_array[alp] = static_cast<t>(aa * mul_val);</t>
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				image_array += ch;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else if (igs::image::rgb::siz == ch) {
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 < hh; ++yy) {
Toshihiro Shimizu 890ddd
				for (int xx = 0; xx < ww; ++xx, image_array += ch) {
Toshihiro Shimizu 890ddd
					pixel_rgb(
Toshihiro Shimizu 890ddd
						static_cast<double>(image_array[red]) / div_val, static_cast<double>(image_array[gre]) / div_val, static_cast<double>(image_array[blu]) / div_val, 1.0, noise.hue_value(xx, yy), noise.lig_value(xx, yy), noise.sat_value(xx, yy), lig_term, sat_term, rr, gg, bb);</double></double></double>
Toshihiro Shimizu 890ddd
					image_array[red] = static_cast<t>(rr * mul_val);</t>
Toshihiro Shimizu 890ddd
					image_array[gre] = static_cast<t>(gg * mul_val);</t>
Toshihiro Shimizu 890ddd
					image_array[blu] = static_cast<t>(bb * mul_val);</t>
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else if (1 == ch) { /* grayscale */
Toshihiro Shimizu 890ddd
		if (0.0 != lig_term.noise_range()) {
Toshihiro Shimizu 890ddd
			for (int yy = 0; yy < hh; ++yy) {
Toshihiro Shimizu 890ddd
				for (int xx = 0; xx < ww; ++xx, ++image_array) {
Toshihiro Shimizu 890ddd
					double lig = 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(lig, lig_noise, shift_value);
Toshihiro Shimizu 890ddd
					lig += shift_value;
Toshihiro Shimizu 890ddd
					lig += lig_noise;
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
					image_array[0] = static_cast<t>(lig * mul_val);</t>
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void igs::hls_noise_in_camera::change(
Toshihiro Shimizu 890ddd
	void *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 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
	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) {</unsigned>
Toshihiro Shimizu 890ddd
		change_template_(
Toshihiro Shimizu 890ddd
			static_cast<unsigned *="" char="">(image_array), width, height, channels, noise, hue_range, lig_term, sat_term, alp_term);</unsigned>
Toshihiro Shimizu 890ddd
		noise.clear(); /* ノイズ画像メモリ解放 */
Toshihiro Shimizu 890ddd
	} else if (std::numeric_limits<unsigned short="">::digits == bits) {</unsigned>
Toshihiro Shimizu 890ddd
		change_template_(
Toshihiro Shimizu 890ddd
			static_cast<unsigned *="" short="">(image_array), width, height, channels, noise, hue_range, lig_term, sat_term, alp_term);</unsigned>
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
}