Toshihiro Shimizu 890ddd
#include "igs_color_rgb_hsv.h"
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
void pixel_rgba_(
Toshihiro Shimizu 890ddd
	const double red_in, const double gre_in, const double blu_in, double &red_out, double &gre_out, double &blu_out, const double hue_pivot // 0.0  ...0...360...
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double hue_scale // 1.0  ...1...
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double hue_shift // 0.0  ...0...360...
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double sat_pivot // 0.0  ...0...1...
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double sat_scale // 1.0  ...1...
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double sat_shift // 0.0  ...0...1...
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double val_pivot // 0.0  ...0...1...
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double val_scale // 1.0  ...1...
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double val_shift // 0.0  ...0...1...
Toshihiro Shimizu 890ddd
	)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double hue, sat, val;
Toshihiro Shimizu 890ddd
	igs::color::rgb_to_hsv(red_in, gre_in, blu_in, hue, sat, val);
Toshihiro Shimizu 890ddd
	if ((1.0 != hue_scale) || (0.0 != hue_shift)) {
Toshihiro Shimizu 890ddd
		hue -= hue_pivot;
Toshihiro Shimizu 890ddd
		while (hue < -180.0) {
Toshihiro Shimizu 890ddd
			hue += 360.0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		while (180.0 <= hue) {
Toshihiro Shimizu 890ddd
			hue -= 360.0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		hue *= hue_scale;
Toshihiro Shimizu 890ddd
		hue += hue_pivot;
Toshihiro Shimizu 890ddd
		hue += hue_shift;
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 ((1.0 != sat_scale) || (0.0 != sat_shift)) {
Toshihiro Shimizu 890ddd
		sat -= sat_pivot;
Toshihiro Shimizu 890ddd
		sat *= sat_scale;
Toshihiro Shimizu 890ddd
		sat += sat_pivot;
Toshihiro Shimizu 890ddd
		sat += sat_shift;
Toshihiro Shimizu 890ddd
		sat = (sat < 0.0) ? 0.0 : ((1.0 < sat) ? 1.0 : sat);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if ((1.0 != val_scale) || (0.0 != val_shift)) {
Toshihiro Shimizu 890ddd
		val -= val_pivot;
Toshihiro Shimizu 890ddd
		val *= val_scale;
Toshihiro Shimizu 890ddd
		val += val_pivot;
Toshihiro Shimizu 890ddd
		val += val_shift;
Toshihiro Shimizu 890ddd
		val = (val < 0.0) ? 0.0 : ((1.0 < val) ? 1.0 : val);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	igs::color::hsv_to_rgb(hue, sat, val, red_out, gre_out, blu_out);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
#include <limits>			/* std::numeric_limits */</limits>
Toshihiro Shimizu 890ddd
#include "igs_ifx_common.h" /* igs::image::rgba */
Toshihiro Shimizu 890ddd
#include "igs_hsv_adjust.h"
Toshihiro Shimizu 890ddd
namespace
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 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 hue_pivot, const double hue_scale, const double hue_shift, const double sat_pivot, const double sat_scale, const double sat_shift, const double val_pivot, const double val_scale, const double val_shift
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 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値ゼロなら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
			/* RGB値を正規化 */
Toshihiro Shimizu 890ddd
			const IT *const ia = image_array;
Toshihiro Shimizu 890ddd
			const double rr1 = static_cast<double>(ia[red]) / div_val;</double>
Toshihiro Shimizu 890ddd
			const double gg1 = static_cast<double>(ia[gre]) / div_val;</double>
Toshihiro Shimizu 890ddd
			const double bb1 = static_cast<double>(ia[blu]) / div_val;</double>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			/* pivot,scale,shiftによってRGB値を変化する */
Toshihiro Shimizu 890ddd
			double rr2, gg2, bb2;
Toshihiro Shimizu 890ddd
			pixel_rgba_(
Toshihiro Shimizu 890ddd
				rr1, gg1, bb1, rr2, gg2, bb2, hue_pivot, hue_scale, hue_shift, sat_pivot, sat_scale, sat_shift, val_pivot, val_scale, val_shift);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			/* 加算合成で、その値がMaxでなければ変化量に乗算 */
Toshihiro Shimizu 890ddd
			if (add_blend_sw && (ia[alp] < t_max)) {
Toshihiro Shimizu 890ddd
				refv *= static_cast<double>(ia[alp]) / div_val;</double>
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			/* ピクセル単位の変化量があれば、RGBを調整する */
Toshihiro Shimizu 890ddd
			if ((ref != 0) || (add_blend_sw && (ia[alp] < t_max))) {
Toshihiro Shimizu 890ddd
				rr2 = rr1 + (rr2 - rr1) * refv;
Toshihiro Shimizu 890ddd
				gg2 = gg1 + (gg2 - gg1) * refv;
Toshihiro Shimizu 890ddd
				bb2 = bb1 + (bb2 - bb1) * refv;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			/* 結果をRGBに戻す */
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
	} 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
Toshihiro Shimizu 890ddd
			const IT *const ia = image_array;
Toshihiro Shimizu 890ddd
			const double rr1 = static_cast<double>(ia[red]) / div_val;</double>
Toshihiro Shimizu 890ddd
			const double gg1 = static_cast<double>(ia[gre]) / div_val;</double>
Toshihiro Shimizu 890ddd
			const double bb1 = static_cast<double>(ia[blu]) / div_val;</double>
Toshihiro Shimizu 890ddd
			double rr2, gg2, bb2;
Toshihiro Shimizu 890ddd
			pixel_rgba_(
Toshihiro Shimizu 890ddd
				rr1, gg1, bb1, rr2, gg2, bb2, hue_pivot, hue_scale, hue_shift, sat_pivot, sat_scale, sat_shift, val_pivot, val_scale, val_shift);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (ref != 0) {
Toshihiro Shimizu 890ddd
				rr2 = rr1 + (rr2 - rr1) * refv;
Toshihiro Shimizu 890ddd
				gg2 = gg1 + (gg2 - gg1) * refv;
Toshihiro Shimizu 890ddd
				bb2 = bb1 + (bb2 - bb1) * refv;
Toshihiro Shimizu 890ddd
			}
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
	} else if (1 == channels) { /* grayscale */
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
Toshihiro Shimizu 890ddd
			double li1 = static_cast<double>(image_array[0]) / div_val,</double>
Toshihiro Shimizu 890ddd
				   li2 = (li1 - val_pivot) * val_scale + val_pivot + val_shift;
Toshihiro Shimizu 890ddd
			li2 = (li2 < 0.0) ? 0.0 : ((1.0 < li2) ? 1.0 : li2);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (ref != 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
#include <stdexcept> /* std::domain_error  */</stdexcept>
Toshihiro Shimizu 890ddd
void igs::hsv_adjust::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 /* 求める画像と同じ高、幅、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
	,
Toshihiro Shimizu 890ddd
	const double hue_pivot, const double hue_scale, const double hue_shift, const double sat_pivot, const double sat_scale, const double sat_shift, const double val_pivot, const double val_scale, const double val_shift
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const bool add_blend_sw)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if ((1.0 == hue_scale) && (0.0 == hue_shift) &&
Toshihiro Shimizu 890ddd
		(1.0 == sat_scale) && (0.0 == sat_shift) &&
Toshihiro Shimizu 890ddd
		(1.0 == val_scale) && (0.0 == val_shift)) {
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
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
	/* rgb(a)画像にhsv(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, height, width, channels, ref, ref_mode, hue_pivot, hue_scale, hue_shift, sat_pivot, sat_scale, sat_shift, val_pivot, val_scale, val_shift, 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_template_(
Toshihiro Shimizu 890ddd
			reinterpret_cast<unsigned *="" short="">(image_array), height, width, channels, ref, ref_mode, hue_pivot, hue_scale, hue_shift, sat_pivot, sat_scale, sat_shift, val_pivot, val_scale, val_shift, 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_template_(
Toshihiro Shimizu 890ddd
			reinterpret_cast<unsigned *="" short="">(image_array), height, width, channels, reinterpret_cast<const *="" short="" unsigned="">(ref), ref_mode, hue_pivot, hue_scale, hue_shift, sat_pivot, sat_scale, sat_shift, val_pivot, val_scale, val_shift, 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_template_(
Toshihiro Shimizu 890ddd
			image_array, height, width, channels, reinterpret_cast<const *="" short="" unsigned="">(ref), ref_mode, hue_pivot, hue_scale, hue_shift, sat_pivot, sat_scale, sat_shift, val_pivot, val_scale, val_shift, 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
}