Toshihiro Shimizu 890ddd
#include <cmath>	 // pow(-)</cmath>
Toshihiro Shimizu 890ddd
#include <stdexcept> // std::domain_error(-)</stdexcept>
Toshihiro Shimizu 890ddd
#include <limits>	// std::numeric_limits</limits>
Toshihiro Shimizu 890ddd
#include <vector></vector>
Toshihiro Shimizu 890ddd
#include "igs_ifx_common.h" /* igs::image::rgba */
Toshihiro Shimizu 890ddd
#include "igs_levels.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
void levels_(
Toshihiro Shimizu 890ddd
	double &val // 0...1
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double in_min // 0...1
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double in_max // 0...1
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double gamma // 0.1 ... 10.0
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double out_min // 0...1
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double out_max // 0...1
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const bool clamp_sw)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	/* 1 入力範囲を正規化 */
Toshihiro Shimizu 890ddd
	val = (in_max == in_min) ? in_max : (val - in_min) / (in_max - in_min);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* 2 (出力範囲に)clamp */
Toshihiro Shimizu 890ddd
	if (clamp_sw) {
Toshihiro Shimizu 890ddd
		val = (val < 0.0) ? 0.0 : ((1.0 < val) ? 1.0 : val);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* 3 正規化の範囲でgamma変換 */
Toshihiro Shimizu 890ddd
	if ((1.0 != gamma) && (0.0 != gamma)) {
Toshihiro Shimizu 890ddd
		if ((0.0 < val) && (val < 1.0)) {
Toshihiro Shimizu 890ddd
			val = pow(val, 1.0 / gamma);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* 4 正規化範囲を出力範囲に */
Toshihiro Shimizu 890ddd
	val = out_min + val * (out_max - out_min);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* 5 clamp */
Toshihiro Shimizu 890ddd
	val = (val < 0.0) ? 0.0 : ((1.0 < val) ? 1.0 : val);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
double refchk_(const int src, const int tgt, const double refv)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return (src < tgt) ? (tgt - src + 0.999999) * refv + src : (src - tgt + 0.999999) * (1.0 - refv) + tgt;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
	std::vector< std::vector<t> > &tables</t>
Toshihiro Shimizu 890ddd
 とするとvc2005mdで他プログラムとのリンク時、
Toshihiro Shimizu 890ddd
	allocatorの2重定義でエラーとなる。
Toshihiro Shimizu 890ddd
	std::vector< std::vector<int> >&tables</int>
Toshihiro Shimizu 890ddd
 ならOK
Toshihiro Shimizu 890ddd
 2009-01-27
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
template <class class="" it,="" rt=""></class>
Toshihiro Shimizu 890ddd
void change_(
Toshihiro Shimizu 890ddd
	IT *image_array, const int height, const int width, 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 // R,G,B,A,luminance
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double r_in_min, const double r_in_max // 0...1
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double g_in_min, const double g_in_max // 0...1
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double b_in_min, const double b_in_max // 0...1
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double a_in_min, const double a_in_max // 0...1
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double r_gamma // 0.1 ... 10.0
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double g_gamma // 0.1 ... 10.0
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double b_gamma // 0.1 ... 10.0
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double a_gamma // 0.1 ... 10.0
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double r_out_min, const double r_out_max // 0...1
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double g_out_min, const double g_out_max // 0...1
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double b_out_min, const double b_out_max // 0...1
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double a_out_min, const double a_out_max // 0...1
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const bool clamp_sw, const bool alpha_sw, const bool add_blend_sw)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	/* 1 最大値、最小値から変換テーブルを求める */
Toshihiro Shimizu 890ddd
	std::vector<std::vector<int>> tables;</std::vector<int>
Toshihiro Shimizu 890ddd
	const unsigned int val_max = std::numeric_limits<it>::max();</it>
Toshihiro Shimizu 890ddd
	const double div_val = static_cast<double>(val_max);</double>
Toshihiro Shimizu 890ddd
	const double mul_val = div_val + 0.999999;
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		using namespace igs::image::rgba;
Toshihiro Shimizu 890ddd
		tables.resize(siz);
Toshihiro Shimizu 890ddd
		tables[red].resize(val_max + 1);
Toshihiro Shimizu 890ddd
		tables[gre].resize(val_max + 1);
Toshihiro Shimizu 890ddd
		tables[blu].resize(val_max + 1);
Toshihiro Shimizu 890ddd
		tables[alp].resize(val_max + 1);
Toshihiro Shimizu 890ddd
		for (unsigned int yy = 0; yy <= val_max; ++yy) {
Toshihiro Shimizu 890ddd
			double rr, gg, bb, aa;
Toshihiro Shimizu 890ddd
			rr = gg = bb = aa = yy / div_val;
Toshihiro Shimizu 890ddd
			levels_(rr, r_in_min, r_in_max, r_gamma, r_out_min, r_out_max, clamp_sw);
Toshihiro Shimizu 890ddd
			levels_(gg, g_in_min, g_in_max, g_gamma, g_out_min, g_out_max, clamp_sw);
Toshihiro Shimizu 890ddd
			levels_(bb, b_in_min, b_in_max, b_gamma, b_out_min, b_out_max, clamp_sw);
Toshihiro Shimizu 890ddd
			levels_(aa, a_in_min, a_in_max, a_gamma, a_out_min, a_out_max, clamp_sw);
Toshihiro Shimizu 890ddd
			/* 0〜1.0 --> 0〜mul_maxスケール変換し、整数値化 */
Toshihiro Shimizu 890ddd
			tables[red][yy] = static_cast<int>(rr * mul_val);</int>
Toshihiro Shimizu 890ddd
			tables[gre][yy] = static_cast<int>(gg * mul_val);</int>
Toshihiro Shimizu 890ddd
			tables[blu][yy] = static_cast<int>(bb * mul_val);</int>
Toshihiro Shimizu 890ddd
			tables[alp][yy] = static_cast<int>(aa * mul_val);</int>
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* 2 変換テーブルを使ってlevel変換する */
Toshihiro Shimizu 890ddd
	const int pixsize = height * width;
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 ii = 0; ii < pixsize; ++ii, 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
			/* AlphaのLevel処理した値をrefvの変化量比で加える */
Toshihiro Shimizu 890ddd
			if (alpha_sw) {
Toshihiro Shimizu 890ddd
				image_array[alp] = static_cast<it>(refchk_(</it>
Toshihiro Shimizu 890ddd
					static_cast<int>(image_array[alp]), tables[alp][image_array[alp]], refv));</int>
Toshihiro Shimizu 890ddd
			}
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合成の時は、Alpha値がゼロでも
Toshihiro Shimizu 890ddd
			RGB値は存在する(してもよい)の計算する */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			/* 加算合成で、その値がMaxでなければ変化量に乗算 */
Toshihiro Shimizu 890ddd
			if (add_blend_sw && (image_array[alp] < val_max)) {
Toshihiro Shimizu 890ddd
				refv *= static_cast<double>(image_array[alp]) / div_val;</double>
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			/* RGBのLevel処理した値をrefvの変化量比で加える */
Toshihiro Shimizu 890ddd
			image_array[red] = static_cast<it>(refchk_(</it>
Toshihiro Shimizu 890ddd
				static_cast<int>(image_array[red]), tables[red][image_array[red]], refv));</int>
Toshihiro Shimizu 890ddd
			image_array[gre] = static_cast<it>(refchk_(</it>
Toshihiro Shimizu 890ddd
				static_cast<int>(image_array[gre]), tables[gre][image_array[gre]], refv));</int>
Toshihiro Shimizu 890ddd
			image_array[blu] = static_cast<it>(refchk_(</it>
Toshihiro Shimizu 890ddd
				static_cast<int>(image_array[blu]), tables[blu][image_array[blu]], refv));</int>
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else if (igs::image::rgb::siz == channels) {
Toshihiro Shimizu 890ddd
		using namespace igs::image::rgb;
Toshihiro Shimizu 890ddd
		for (int ii = 0; ii < pixsize; ++ii, image_array += channels) {
Toshihiro Shimizu 890ddd
			double refv = 1.0;
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;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			/* RGBのLevel処理した値をrefvの変化量比で加える */
Toshihiro Shimizu 890ddd
			image_array[red] = static_cast<it>(refchk_(</it>
Toshihiro Shimizu 890ddd
				static_cast<int>(image_array[red]), tables[red][image_array[red]], refv));</int>
Toshihiro Shimizu 890ddd
			image_array[gre] = static_cast<it>(refchk_(</it>
Toshihiro Shimizu 890ddd
				static_cast<int>(image_array[gre]), tables[gre][image_array[gre]], refv));</int>
Toshihiro Shimizu 890ddd
			image_array[blu] = static_cast<it>(refchk_(</it>
Toshihiro Shimizu 890ddd
				static_cast<int>(image_array[blu]), tables[blu][image_array[blu]], refv));</int>
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else if (1 == channels) { /* grayscale */
Toshihiro Shimizu 890ddd
		for (int ii = 0; ii < pixsize; ++ii, ++image_array) {
Toshihiro Shimizu 890ddd
			double refv = 1.0;
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;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			image_array[0] = static_cast<it>(refchk_(</it>
Toshihiro Shimizu 890ddd
				static_cast<int>(image_array[0]), tables[0][image_array[0]], refv));</int>
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* 1 変換テーブルのメモリ解放 */
Toshihiro Shimizu 890ddd
	tables.clear();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
void igs::levels::change(
Toshihiro Shimizu 890ddd
	unsigned char *image_array, 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 /* 求める画像と同じ高、幅、ch数 */
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
	,
Toshihiro Shimizu 890ddd
	const double r_in_min, const double r_in_max // 0...1
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double g_in_min, const double g_in_max // 0...1
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double b_in_min, const double b_in_max // 0...1
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double a_in_min, const double a_in_max // 0...1
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double r_gamma // 0.1 ... 10.0
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double g_gamma // 0.1 ... 10.0
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double b_gamma // 0.1 ... 10.0
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double a_gamma // 0.1 ... 10.0
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double r_out_min, const double r_out_max // 0...1
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double g_out_min, const double g_out_max // 0...1
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double b_out_min, const double b_out_max // 0...1
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double a_out_min, const double a_out_max // 0...1
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const bool clamp_sw, const bool alpha_sw, const bool add_blend_sw)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if ((igs::image::rgba::siz != channels) &&
Toshihiro Shimizu 890ddd
		(igs::image::rgb::siz != channels) &&
Toshihiro Shimizu 890ddd
		(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
	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_(
Toshihiro Shimizu 890ddd
			image_array, height, width, channels, ref, ref_mode, r_in_min, r_in_max, g_in_min, g_in_max, b_in_min, b_in_max, a_in_min, a_in_max, r_gamma, g_gamma, b_gamma, a_gamma, r_out_min, r_out_max, g_out_min, g_out_max, b_out_min, b_out_max, a_out_min, a_out_max, clamp_sw, alpha_sw, add_blend_sw);
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_(
Toshihiro Shimizu 890ddd
			reinterpret_cast<unsigned *="" short="">(image_array), height, width, channels, ref, ref_mode, r_in_min, r_in_max, g_in_min, g_in_max, b_in_min, b_in_max, a_in_min, a_in_max, r_gamma, g_gamma, b_gamma, a_gamma, r_out_min, r_out_max, g_out_min, g_out_max, b_out_min, b_out_max, a_out_min, a_out_max, clamp_sw, alpha_sw, add_blend_sw);</unsigned>
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_(
Toshihiro Shimizu 890ddd
			reinterpret_cast<unsigned *="" short="">(image_array), height, width, channels, reinterpret_cast<const *="" short="" unsigned="">(ref), ref_mode, r_in_min, r_in_max, g_in_min, g_in_max, b_in_min, b_in_max, a_in_min, a_in_max, r_gamma, g_gamma, b_gamma, a_gamma, r_out_min, r_out_max, g_out_min, g_out_max, b_out_min, b_out_max, a_out_min, a_out_max, clamp_sw, alpha_sw, add_blend_sw);</const></unsigned>
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_(
Toshihiro Shimizu 890ddd
			image_array, height, width, channels, reinterpret_cast<const *="" short="" unsigned="">(ref), ref_mode, r_in_min, r_in_max, g_in_min, g_in_max, b_in_min, b_in_max, a_in_min, a_in_max, r_gamma, g_gamma, b_gamma, a_gamma, r_out_min, r_out_max, g_out_min, g_out_max, b_out_min, b_out_max, a_out_min, a_out_max, clamp_sw, alpha_sw, add_blend_sw);</const>
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		throw std::domain_error("Bad bits,Not uchar/ushort");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}