Toshihiro Shimizu 890ddd
#include "igs_density.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
double accum_by_trans_(
Toshihiro Shimizu 890ddd
	const double src_value /* 元値(R,G,B) */
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double transparent /* src_valueの透明度(0...1) */
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const int integer_part /* 濃度値の整数部分(0...) */
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const double fractional_part /* 濃度値の少数部分(0...1) */
Toshihiro Shimizu 890ddd
	)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double accumulation = src_value;
Toshihiro Shimizu 890ddd
	if (1 <= integer_part) {
Toshihiro Shimizu 890ddd
		for (int ii = 1; ii < integer_part; ++ii) {
Toshihiro Shimizu 890ddd
			accumulation = accumulation * transparent + src_value;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (0.0 < fractional_part) {
Toshihiro Shimizu 890ddd
			accumulation +=
Toshihiro Shimizu 890ddd
				((accumulation * transparent + src_value) -
Toshihiro Shimizu 890ddd
				 accumulation) *
Toshihiro Shimizu 890ddd
				fractional_part;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else { /* 整数部分がゼロ以下のとき */
Toshihiro Shimizu 890ddd
		if (0.0 < fractional_part) {
Toshihiro Shimizu 890ddd
			accumulation *= fractional_part;
Toshihiro Shimizu 890ddd
		} else { /* 少数部分もゼロ以下のとき */
Toshihiro Shimizu 890ddd
			accumulation = 0.0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return (1.0 < accumulation) ? 1.0 : (
Toshihiro Shimizu 890ddd
											(accumulation < 0.0) ? 0.0 : accumulation);
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
namespace
Toshihiro Shimizu 890ddd
{
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 /* 求める画像と同じ高、幅、ch数 */
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 density)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	const int integer_part = (int)density;
Toshihiro Shimizu 890ddd
	const double fractional_part = density - (int)density;
Toshihiro Shimizu 890ddd
	const double maxi = std::numeric_limits<it>::max();</it>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	using namespace igs::image::rgba;
Toshihiro Shimizu 890ddd
	const int pixsize = height * width;
Toshihiro Shimizu 890ddd
	const int r_max = std::numeric_limits<rt>::max();</rt>
Toshihiro Shimizu 890ddd
	for (int ii = 0; ii < pixsize; ++ii, image_array += channels) {
Toshihiro Shimizu 890ddd
		const double rr1 = (double)(image_array[red]) / maxi;
Toshihiro Shimizu 890ddd
		const double gg1 = (double)(image_array[gre]) / maxi;
Toshihiro Shimizu 890ddd
		const double bb1 = (double)(image_array[blu]) / maxi;
Toshihiro Shimizu 890ddd
		const double aa1 = (double)(image_array[alp]) / maxi;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		double rr2 = accum_by_trans_(rr1,
Toshihiro Shimizu 890ddd
									 1.0 - aa1, integer_part, fractional_part);
Toshihiro Shimizu 890ddd
		double gg2 = accum_by_trans_(gg1,
Toshihiro Shimizu 890ddd
									 1.0 - aa1, integer_part, fractional_part);
Toshihiro Shimizu 890ddd
		double bb2 = accum_by_trans_(bb1,
Toshihiro Shimizu 890ddd
									 1.0 - aa1, integer_part, fractional_part);
Toshihiro Shimizu 890ddd
		double aa2 = accum_by_trans_(aa1,
Toshihiro Shimizu 890ddd
									 1.0 - aa1, integer_part, fractional_part);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/* 参照画像あればピクセル単位の画像変化 */
Toshihiro Shimizu 890ddd
		if (ref != 0) {
Toshihiro Shimizu 890ddd
			const double refv = igs::color::ref_value(
Toshihiro Shimizu 890ddd
				ref, channels, r_max, ref_mode);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			ref += channels; /* continue;の前に行うこと */
Toshihiro Shimizu 890ddd
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
			aa2 = (aa2 - aa1) * refv + aa1;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		image_array[red] = static_cast<it>(rr2 * (maxi + 0.999999));</it>
Toshihiro Shimizu 890ddd
		image_array[gre] = static_cast<it>(gg2 * (maxi + 0.999999));</it>
Toshihiro Shimizu 890ddd
		image_array[blu] = static_cast<it>(bb2 * (maxi + 0.999999));</it>
Toshihiro Shimizu 890ddd
		image_array[alp] = static_cast<it>(aa2 * (maxi + 0.999999));</it>
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
#include <stdexcept> /* std::domain_error(-) */</stdexcept>
Toshihiro Shimizu 890ddd
void igs::density::change(
Toshihiro Shimizu 890ddd
	unsigned char *image_array /* RGBAでなければならない */
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	const int height, const int width, const int channels /* 4(=RGBAでなければならない) */
Toshihiro Shimizu 890ddd
	,
Toshihiro Shimizu 890ddd
	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 density)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (igs::image::rgba::siz != channels) {
Toshihiro Shimizu 890ddd
		throw std::domain_error("Bad channels,Not rgba");
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_template_(
Toshihiro Shimizu 890ddd
			image_array, height, width, channels, ref, ref_mode, density);
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, density);</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, density);</const></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
			image_array, height, width, channels, reinterpret_cast<const *="" short="" unsigned="">(ref), ref_mode, density);</const>
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		throw std::domain_error("Bad bits,Not uchar/ushort");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}