|
Shinya Kitaoka |
120a6e |
#include <cmath> // pow(-)</cmath>
|
|
Shinya Kitaoka |
120a6e |
#include <stdexcept> // std::domain_error(-)</stdexcept>
|
|
Shinya Kitaoka |
120a6e |
#include <limits> // std::numeric_limits</limits>
|
|
Toshihiro Shimizu |
890ddd |
#include <vector></vector>
|
|
shun-iwasawa |
481b59 |
#include <cassert></cassert>
|
|
Toshihiro Shimizu |
890ddd |
#include "igs_ifx_common.h" /* igs::image::rgba */
|
|
Toshihiro Shimizu |
890ddd |
#include "igs_levels.h"
|
|
Toshihiro Shimizu |
890ddd |
|
|
shun-iwasawa |
481b59 |
#include "tutil.h" // areAlmostEqual
|
|
shun-iwasawa |
481b59 |
|
|
Toshihiro Shimizu |
890ddd |
//------------------------------------------------------------
|
|
Shinya Kitaoka |
120a6e |
namespace {
|
|
Shinya Kitaoka |
120a6e |
void levels_(double &val // 0...1
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double in_min // 0...1
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double in_max // 0...1
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double gamma // 0.1 ... 10.0
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double out_min // 0...1
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double out_max // 0...1
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const bool clamp_sw) {
|
|
Shinya Kitaoka |
120a6e |
/* 1 入力範囲を正規化 */
|
|
Shinya Kitaoka |
120a6e |
val = (in_max == in_min) ? in_max : (val - in_min) / (in_max - in_min);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* 2 (出力範囲に)clamp */
|
|
shun-iwasawa |
481b59 |
if (clamp_sw || !areAlmostEqual(out_max, 1.)) {
|
|
Shinya Kitaoka |
120a6e |
val = (val < 0.0) ? 0.0 : ((1.0 < val) ? 1.0 : val);
|
|
shun-iwasawa |
481b59 |
} else {
|
|
shun-iwasawa |
481b59 |
val = (val < 0.0) ? 0.0 : val;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* 3 正規化の範囲でgamma変換 */
|
|
Shinya Kitaoka |
120a6e |
if ((1.0 != gamma) && (0.0 != gamma)) {
|
|
Shinya Kitaoka |
120a6e |
if ((0.0 < val) && (val < 1.0)) {
|
|
Shinya Kitaoka |
120a6e |
val = pow(val, 1.0 / gamma);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
shun-iwasawa |
481b59 |
// ガンマ補正の線を直線で延長する
|
|
shun-iwasawa |
481b59 |
else if (val > 1.0) {
|
|
shun-iwasawa |
481b59 |
val = 1. + (val - 1.) / gamma;
|
|
shun-iwasawa |
481b59 |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* 4 正規化範囲を出力範囲に */
|
|
Shinya Kitaoka |
120a6e |
val = out_min + val * (out_max - out_min);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* 5 clamp */
|
|
shun-iwasawa |
481b59 |
if (clamp_sw)
|
|
shun-iwasawa |
481b59 |
val = (val < 0.0) ? 0.0 : ((1.0 < val) ? 1.0 : val);
|
|
shun-iwasawa |
481b59 |
else
|
|
shun-iwasawa |
481b59 |
val = (val < 0.0) ? 0.0 : val;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Shinya Kitaoka |
120a6e |
double refchk_(const int src, const int tgt, const double refv) {
|
|
Shinya Kitaoka |
120a6e |
return (src < tgt) ? (tgt - src + 0.999999) * refv + src
|
|
Shinya Kitaoka |
120a6e |
: (src - tgt + 0.999999) * (1.0 - refv) + tgt;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
shun-iwasawa |
481b59 |
float refchk_(const float src, const float tgt, const double refv) {
|
|
shun-iwasawa |
481b59 |
return tgt * refv + src * (1. - refv);
|
|
shun-iwasawa |
481b59 |
}
|
|
Toshihiro Shimizu |
890ddd |
//------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
/*
|
|
Shinya Kitaoka |
120a6e |
std::vector< std::vector<t> > &tables</t>
|
|
Toshihiro Shimizu |
890ddd |
とするとvc2005mdで他プログラムとのリンク時、
|
|
Shinya Kitaoka |
120a6e |
allocatorの2重定義でエラーとなる。
|
|
Shinya Kitaoka |
120a6e |
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>
|
|
Shinya Kitaoka |
120a6e |
void change_(IT *image_array, const int height, const int width,
|
|
Shinya Kitaoka |
120a6e |
const int channels
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const RT *ref /* 求める画像(out)と同じ高さ、幅、チャンネル数 */
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const int ref_mode // R,G,B,A,luminance
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double r_in_min, const double r_in_max // 0...1
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double g_in_min, const double g_in_max // 0...1
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double b_in_min, const double b_in_max // 0...1
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double a_in_min, const double a_in_max // 0...1
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double r_gamma // 0.1 ... 10.0
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double g_gamma // 0.1 ... 10.0
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double b_gamma // 0.1 ... 10.0
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double a_gamma // 0.1 ... 10.0
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double r_out_min, const double r_out_max // 0...1
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double g_out_min, const double g_out_max // 0...1
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double b_out_min, const double b_out_max // 0...1
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double a_out_min, const double a_out_max // 0...1
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const bool clamp_sw, const bool alpha_sw,
|
|
Shinya Kitaoka |
120a6e |
const bool add_blend_sw) {
|
|
Shinya Kitaoka |
120a6e |
/* 1 最大値、最小値から変換テーブルを求める */
|
|
Shinya Kitaoka |
120a6e |
std::vector<std::vector<int>> tables;</std::vector<int>
|
|
Shinya Kitaoka |
120a6e |
const unsigned int val_max = std::numeric_limits<it>::max();</it>
|
|
Shinya Kitaoka |
120a6e |
const double div_val = static_cast<double>(val_max);</double>
|
|
Shinya Kitaoka |
120a6e |
const double mul_val = div_val + 0.999999;
|
|
Shinya Kitaoka |
120a6e |
{
|
|
Shinya Kitaoka |
120a6e |
using namespace igs::image::rgba;
|
|
Shinya Kitaoka |
120a6e |
tables.resize(siz);
|
|
Shinya Kitaoka |
120a6e |
tables[red].resize(val_max + 1);
|
|
Shinya Kitaoka |
120a6e |
tables[gre].resize(val_max + 1);
|
|
Shinya Kitaoka |
120a6e |
tables[blu].resize(val_max + 1);
|
|
Shinya Kitaoka |
120a6e |
tables[alp].resize(val_max + 1);
|
|
Shinya Kitaoka |
120a6e |
for (unsigned int yy = 0; yy <= val_max; ++yy) {
|
|
Shinya Kitaoka |
120a6e |
double rr, gg, bb, aa;
|
|
Shinya Kitaoka |
120a6e |
rr = gg = bb = aa = yy / div_val;
|
|
Shinya Kitaoka |
120a6e |
levels_(rr, r_in_min, r_in_max, r_gamma, r_out_min, r_out_max, clamp_sw);
|
|
Shinya Kitaoka |
120a6e |
levels_(gg, g_in_min, g_in_max, g_gamma, g_out_min, g_out_max, clamp_sw);
|
|
Shinya Kitaoka |
120a6e |
levels_(bb, b_in_min, b_in_max, b_gamma, b_out_min, b_out_max, clamp_sw);
|
|
Shinya Kitaoka |
120a6e |
levels_(aa, a_in_min, a_in_max, a_gamma, a_out_min, a_out_max, clamp_sw);
|
|
Shinya Kitaoka |
120a6e |
/* 0〜1.0 --> 0〜mul_maxスケール変換し、整数値化 */
|
|
Shinya Kitaoka |
120a6e |
tables[red][yy] = static_cast<int>(rr * mul_val);</int>
|
|
Shinya Kitaoka |
120a6e |
tables[gre][yy] = static_cast<int>(gg * mul_val);</int>
|
|
Shinya Kitaoka |
120a6e |
tables[blu][yy] = static_cast<int>(bb * mul_val);</int>
|
|
Shinya Kitaoka |
120a6e |
tables[alp][yy] = static_cast<int>(aa * mul_val);</int>
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* 2 変換テーブルを使ってlevel変換する */
|
|
Shinya Kitaoka |
120a6e |
const int pixsize = height * width;
|
|
Shinya Kitaoka |
120a6e |
const int r_max = std::numeric_limits<rt>::max();</rt>
|
|
Shinya Kitaoka |
120a6e |
if (igs::image::rgba::siz == channels) {
|
|
Shinya Kitaoka |
120a6e |
using namespace igs::image::rgba;
|
|
Shinya Kitaoka |
120a6e |
for (int ii = 0; ii < pixsize; ++ii, image_array += channels) {
|
|
Shinya Kitaoka |
120a6e |
/* 変化量初期値 */
|
|
Shinya Kitaoka |
120a6e |
double refv = 1.0;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* 参照画像あればピクセル単位の画像変化量を得る */
|
|
Shinya Kitaoka |
120a6e |
if (ref != 0) {
|
|
Shinya Kitaoka |
120a6e |
refv *= igs::color::ref_value(ref, channels, r_max, ref_mode);
|
|
Shinya Kitaoka |
120a6e |
ref += channels; /* continue;の前に行うこと */
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* AlphaのLevel処理した値をrefvの変化量比で加える */
|
|
Shinya Kitaoka |
120a6e |
if (alpha_sw) {
|
|
Shinya Kitaoka |
120a6e |
image_array[alp] =
|
|
Shinya Kitaoka |
120a6e |
static_cast<it>(refchk_(static_cast<int>(image_array[alp]),</int></it>
|
|
Shinya Kitaoka |
120a6e |
tables[alp][image_array[alp]], refv));
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* 加算合成で、Alpha値ゼロならRGB値を計算する必要はない */
|
|
Shinya Kitaoka |
120a6e |
if (add_blend_sw && (0 == image_array[alp])) {
|
|
Shinya Kitaoka |
120a6e |
continue;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
/* 加算合成でなくAlpha合成の時は、Alpha値がゼロでも
|
|
Shinya Kitaoka |
120a6e |
RGB値は存在する(してもよい)の計算する */
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* 加算合成で、その値がMaxでなければ変化量に乗算 */
|
|
Shinya Kitaoka |
120a6e |
if (add_blend_sw && (image_array[alp] < val_max)) {
|
|
Shinya Kitaoka |
120a6e |
refv *= static_cast<double>(image_array[alp]) / div_val;</double>
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* RGBのLevel処理した値をrefvの変化量比で加える */
|
|
Shinya Kitaoka |
120a6e |
image_array[red] =
|
|
Shinya Kitaoka |
120a6e |
static_cast<it>(refchk_(static_cast<int>(image_array[red]),</int></it>
|
|
Shinya Kitaoka |
120a6e |
tables[red][image_array[red]], refv));
|
|
Shinya Kitaoka |
120a6e |
image_array[gre] =
|
|
Shinya Kitaoka |
120a6e |
static_cast<it>(refchk_(static_cast<int>(image_array[gre]),</int></it>
|
|
Shinya Kitaoka |
120a6e |
tables[gre][image_array[gre]], refv));
|
|
Shinya Kitaoka |
120a6e |
image_array[blu] =
|
|
Shinya Kitaoka |
120a6e |
static_cast<it>(refchk_(static_cast<int>(image_array[blu]),</int></it>
|
|
Shinya Kitaoka |
120a6e |
tables[blu][image_array[blu]], refv));
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
} else if (igs::image::rgb::siz == channels) {
|
|
Shinya Kitaoka |
120a6e |
using namespace igs::image::rgb;
|
|
Shinya Kitaoka |
120a6e |
for (int ii = 0; ii < pixsize; ++ii, image_array += channels) {
|
|
Shinya Kitaoka |
120a6e |
double refv = 1.0;
|
|
Shinya Kitaoka |
120a6e |
if (ref != 0) {
|
|
Shinya Kitaoka |
120a6e |
refv *= igs::color::ref_value(ref, channels, r_max, ref_mode);
|
|
Shinya Kitaoka |
120a6e |
ref += channels;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
/* RGBのLevel処理した値をrefvの変化量比で加える */
|
|
Shinya Kitaoka |
120a6e |
image_array[red] =
|
|
Shinya Kitaoka |
120a6e |
static_cast<it>(refchk_(static_cast<int>(image_array[red]),</int></it>
|
|
Shinya Kitaoka |
120a6e |
tables[red][image_array[red]], refv));
|
|
Shinya Kitaoka |
120a6e |
image_array[gre] =
|
|
Shinya Kitaoka |
120a6e |
static_cast<it>(refchk_(static_cast<int>(image_array[gre]),</int></it>
|
|
Shinya Kitaoka |
120a6e |
tables[gre][image_array[gre]], refv));
|
|
Shinya Kitaoka |
120a6e |
image_array[blu] =
|
|
Shinya Kitaoka |
120a6e |
static_cast<it>(refchk_(static_cast<int>(image_array[blu]),</int></it>
|
|
Shinya Kitaoka |
120a6e |
tables[blu][image_array[blu]], refv));
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
} else if (1 == channels) { /* grayscale */
|
|
Shinya Kitaoka |
120a6e |
for (int ii = 0; ii < pixsize; ++ii, ++image_array) {
|
|
Shinya Kitaoka |
120a6e |
double refv = 1.0;
|
|
Shinya Kitaoka |
120a6e |
if (ref != 0) {
|
|
Shinya Kitaoka |
120a6e |
refv *= igs::color::ref_value(ref, channels, r_max, ref_mode);
|
|
Shinya Kitaoka |
120a6e |
ref += channels;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
image_array[0] = static_cast<it>(refchk_(</it>
|
|
Shinya Kitaoka |
120a6e |
static_cast<int>(image_array[0]), tables[0][image_array[0]], refv));</int>
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* 1 変換テーブルのメモリ解放 */
|
|
Shinya Kitaoka |
120a6e |
tables.clear();
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
shun-iwasawa |
481b59 |
|
|
shun-iwasawa |
481b59 |
template <>
|
|
shun-iwasawa |
481b59 |
void change_(float *image_array, const int height, const int width,
|
|
shun-iwasawa |
481b59 |
const int channels,
|
|
shun-iwasawa |
481b59 |
const float *ref, /* 求める画像(out)と同じ高さ、幅、チャンネル数 */
|
|
shun-iwasawa |
481b59 |
const int ref_mode, // R,G,B,A,luminance
|
|
shun-iwasawa |
481b59 |
const double r_in_min, const double r_in_max, // 0...1
|
|
shun-iwasawa |
481b59 |
const double g_in_min, const double g_in_max, // 0...1
|
|
shun-iwasawa |
481b59 |
const double b_in_min, const double b_in_max, // 0...1
|
|
shun-iwasawa |
481b59 |
const double a_in_min, const double a_in_max, // 0...1
|
|
shun-iwasawa |
481b59 |
const double r_gamma, // 0.1 ... 10.0
|
|
shun-iwasawa |
481b59 |
const double g_gamma, // 0.1 ... 10.0
|
|
shun-iwasawa |
481b59 |
const double b_gamma, // 0.1 ... 10.0
|
|
shun-iwasawa |
481b59 |
const double a_gamma, // 0.1 ... 10.0
|
|
shun-iwasawa |
481b59 |
const double r_out_min, const double r_out_max, // 0...1
|
|
shun-iwasawa |
481b59 |
const double g_out_min, const double g_out_max, // 0...1
|
|
shun-iwasawa |
481b59 |
const double b_out_min, const double b_out_max, // 0...1
|
|
shun-iwasawa |
481b59 |
const double a_out_min, const double a_out_max, // 0...1
|
|
shun-iwasawa |
481b59 |
const bool clamp_sw, const bool alpha_sw,
|
|
shun-iwasawa |
481b59 |
const bool add_blend_sw) {
|
|
shun-iwasawa |
481b59 |
/* 1 最大値、最小値から変換テーブルを求める */
|
|
shun-iwasawa |
481b59 |
std::vector<std::vector<float>> tables;</std::vector<float>
|
|
shun-iwasawa |
481b59 |
const unsigned int table_size =
|
|
shun-iwasawa |
481b59 |
std::numeric_limits<unsigned short="">::max() + 1;</unsigned>
|
|
shun-iwasawa |
481b59 |
const double div_val = static_cast<double>(table_size - 1);</double>
|
|
shun-iwasawa |
481b59 |
// const double mul_val = div_val + 0.999999;
|
|
shun-iwasawa |
481b59 |
{
|
|
shun-iwasawa |
481b59 |
using namespace igs::image::rgba;
|
|
shun-iwasawa |
481b59 |
tables.resize(siz);
|
|
shun-iwasawa |
481b59 |
tables[red].resize(table_size);
|
|
shun-iwasawa |
481b59 |
tables[gre].resize(table_size);
|
|
shun-iwasawa |
481b59 |
tables[blu].resize(table_size);
|
|
shun-iwasawa |
481b59 |
tables[alp].resize(table_size);
|
|
shun-iwasawa |
481b59 |
for (unsigned int yy = 0; yy < table_size; ++yy) {
|
|
shun-iwasawa |
481b59 |
double rr, gg, bb, aa;
|
|
shun-iwasawa |
481b59 |
rr = gg = bb = aa = yy / div_val;
|
|
shun-iwasawa |
481b59 |
levels_(rr, r_in_min, r_in_max, r_gamma, r_out_min, r_out_max,
|
|
shun-iwasawa |
481b59 |
false); // クランプしない
|
|
shun-iwasawa |
481b59 |
levels_(gg, g_in_min, g_in_max, g_gamma, g_out_min, g_out_max,
|
|
shun-iwasawa |
481b59 |
false); // クランプしない
|
|
shun-iwasawa |
481b59 |
levels_(bb, b_in_min, b_in_max, b_gamma, b_out_min, b_out_max,
|
|
shun-iwasawa |
481b59 |
false); // クランプしない
|
|
shun-iwasawa |
481b59 |
levels_(aa, a_in_min, a_in_max, a_gamma, a_out_min, a_out_max, clamp_sw);
|
|
shun-iwasawa |
481b59 |
/* 0〜1.0 --> 0〜mul_maxスケール変換し、整数値化 */
|
|
shun-iwasawa |
481b59 |
tables[red][yy] = static_cast<float>(rr);</float>
|
|
shun-iwasawa |
481b59 |
tables[gre][yy] = static_cast<float>(gg);</float>
|
|
shun-iwasawa |
481b59 |
tables[blu][yy] = static_cast<float>(bb);</float>
|
|
shun-iwasawa |
481b59 |
tables[alp][yy] = static_cast<float>(aa);</float>
|
|
shun-iwasawa |
481b59 |
}
|
|
shun-iwasawa |
481b59 |
}
|
|
shun-iwasawa |
481b59 |
|
|
shun-iwasawa |
481b59 |
// テーブル値を返すラムダ式。Inが1より大きい場合はmaxの線を直線で延長
|
|
shun-iwasawa |
481b59 |
auto getTableVal = [&](int channel, float in) {
|
|
shun-iwasawa |
481b59 |
if (in <= 0.f) { // 先頭の値を返す
|
|
shun-iwasawa |
481b59 |
return tables[channel][0];
|
|
shun-iwasawa |
481b59 |
} else if (in <=
|
|
shun-iwasawa |
481b59 |
1.f) { // テーブル範囲に収まっている場合、前後の値で線形補間
|
|
shun-iwasawa |
481b59 |
int index = static_cast<int>(std::floor(in * div_val));</int>
|
|
shun-iwasawa |
481b59 |
float ratio = in * div_val - static_cast<float>(index);</float>
|
|
shun-iwasawa |
481b59 |
if (areAlmostEqual(ratio, 0.)) return tables[channel][index];
|
|
shun-iwasawa |
481b59 |
return tables[channel][index] * (1.f - ratio) +
|
|
shun-iwasawa |
481b59 |
tables[channel][index + 1] * ratio;
|
|
shun-iwasawa |
481b59 |
}
|
|
shun-iwasawa |
481b59 |
// inが1より大きい場合
|
|
shun-iwasawa |
481b59 |
// 傾き
|
|
shun-iwasawa |
481b59 |
float dv =
|
|
shun-iwasawa |
481b59 |
(tables[channel][table_size - 1] - tables[channel][table_size - 2]) *
|
|
shun-iwasawa |
481b59 |
div_val;
|
|
shun-iwasawa |
481b59 |
return tables[channel][table_size - 1] + dv * (in - 1.f);
|
|
shun-iwasawa |
481b59 |
};
|
|
shun-iwasawa |
481b59 |
|
|
shun-iwasawa |
481b59 |
/* 2 変換テーブルを使ってlevel変換する */
|
|
shun-iwasawa |
481b59 |
const int pixsize = height * width;
|
|
shun-iwasawa |
481b59 |
if (igs::image::rgba::siz == channels) {
|
|
shun-iwasawa |
481b59 |
using namespace igs::image::rgba;
|
|
shun-iwasawa |
481b59 |
for (int ii = 0; ii < pixsize; ++ii, image_array += channels) {
|
|
shun-iwasawa |
481b59 |
/* 変化量初期値 */
|
|
shun-iwasawa |
481b59 |
double refv = 1.0;
|
|
shun-iwasawa |
481b59 |
|
|
shun-iwasawa |
481b59 |
/* 参照画像あればピクセル単位の画像変化量を得る */
|
|
shun-iwasawa |
481b59 |
if (ref != 0) {
|
|
shun-iwasawa |
481b59 |
refv *= igs::color::ref_value(ref, channels, 1.0, ref_mode);
|
|
shun-iwasawa |
481b59 |
// clamp 0. to 1. in case the reference is more than 1
|
|
shun-iwasawa |
481b59 |
refv = std::min(1., std::max(0., refv));
|
|
shun-iwasawa |
481b59 |
ref += channels; /* continue;の前に行うこと */
|
|
shun-iwasawa |
481b59 |
}
|
|
shun-iwasawa |
481b59 |
|
|
shun-iwasawa |
481b59 |
/* AlphaのLevel処理した値をrefvの変化量比で加える */
|
|
shun-iwasawa |
481b59 |
if (alpha_sw) {
|
|
shun-iwasawa |
481b59 |
image_array[alp] =
|
|
shun-iwasawa |
481b59 |
refchk_(image_array[alp], getTableVal(alp, image_array[alp]), refv);
|
|
shun-iwasawa |
481b59 |
}
|
|
shun-iwasawa |
481b59 |
|
|
shun-iwasawa |
481b59 |
/* 加算合成で、Alpha値ゼロならRGB値を計算する必要はない */
|
|
shun-iwasawa |
481b59 |
if (add_blend_sw && areAlmostEqual(image_array[alp], 0.)) {
|
|
shun-iwasawa |
481b59 |
continue;
|
|
shun-iwasawa |
481b59 |
}
|
|
shun-iwasawa |
481b59 |
/* 加算合成でなくAlpha合成の時は、Alpha値がゼロでも
|
|
shun-iwasawa |
481b59 |
RGB値は存在する(してもよい)の計算する */
|
|
shun-iwasawa |
481b59 |
|
|
shun-iwasawa |
481b59 |
/* 加算合成で、その値がMaxでなければ変化量に乗算 */
|
|
shun-iwasawa |
481b59 |
if (add_blend_sw && (image_array[alp] < 1.f)) {
|
|
shun-iwasawa |
481b59 |
refv *= static_cast<double>(image_array[alp]);</double>
|
|
shun-iwasawa |
481b59 |
}
|
|
shun-iwasawa |
481b59 |
|
|
shun-iwasawa |
481b59 |
/* RGBのLevel処理した値をrefvの変化量比で加える */
|
|
shun-iwasawa |
481b59 |
image_array[red] =
|
|
shun-iwasawa |
481b59 |
refchk_(image_array[red], getTableVal(red, image_array[red]), refv);
|
|
shun-iwasawa |
481b59 |
image_array[gre] =
|
|
shun-iwasawa |
481b59 |
refchk_(image_array[gre], getTableVal(gre, image_array[gre]), refv);
|
|
shun-iwasawa |
481b59 |
image_array[blu] =
|
|
shun-iwasawa |
481b59 |
refchk_(image_array[blu], getTableVal(blu, image_array[blu]), refv);
|
|
shun-iwasawa |
481b59 |
}
|
|
shun-iwasawa |
481b59 |
} else if (igs::image::rgb::siz == channels) {
|
|
shun-iwasawa |
481b59 |
using namespace igs::image::rgb;
|
|
shun-iwasawa |
481b59 |
for (int ii = 0; ii < pixsize; ++ii, image_array += channels) {
|
|
shun-iwasawa |
481b59 |
double refv = 1.0;
|
|
shun-iwasawa |
481b59 |
if (ref != 0) {
|
|
shun-iwasawa |
481b59 |
refv *= igs::color::ref_value(ref, channels, 1.0, ref_mode);
|
|
shun-iwasawa |
481b59 |
ref += channels;
|
|
shun-iwasawa |
481b59 |
}
|
|
shun-iwasawa |
481b59 |
/* RGBのLevel処理した値をrefvの変化量比で加える */
|
|
shun-iwasawa |
481b59 |
image_array[red] =
|
|
shun-iwasawa |
481b59 |
refchk_(image_array[red], getTableVal(red, image_array[red]), refv);
|
|
shun-iwasawa |
481b59 |
image_array[gre] =
|
|
shun-iwasawa |
481b59 |
refchk_(image_array[gre], getTableVal(gre, image_array[gre]), refv);
|
|
shun-iwasawa |
481b59 |
image_array[blu] =
|
|
shun-iwasawa |
481b59 |
refchk_(image_array[blu], getTableVal(blu, image_array[blu]), refv);
|
|
shun-iwasawa |
481b59 |
}
|
|
shun-iwasawa |
481b59 |
} else if (1 == channels) { /* grayscale */
|
|
shun-iwasawa |
481b59 |
for (int ii = 0; ii < pixsize; ++ii, ++image_array) {
|
|
shun-iwasawa |
481b59 |
double refv = 1.0;
|
|
shun-iwasawa |
481b59 |
if (ref != 0) {
|
|
shun-iwasawa |
481b59 |
refv *= igs::color::ref_value(ref, channels, 1.0, ref_mode);
|
|
shun-iwasawa |
481b59 |
ref += channels;
|
|
shun-iwasawa |
481b59 |
}
|
|
shun-iwasawa |
481b59 |
image_array[0] =
|
|
shun-iwasawa |
481b59 |
refchk_(image_array[0], getTableVal(0, image_array[0]), refv);
|
|
shun-iwasawa |
481b59 |
}
|
|
shun-iwasawa |
481b59 |
}
|
|
shun-iwasawa |
481b59 |
|
|
shun-iwasawa |
481b59 |
/* 1 変換テーブルのメモリ解放 */
|
|
shun-iwasawa |
481b59 |
tables.clear();
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
shun-iwasawa |
481b59 |
|
|
shun-iwasawa |
481b59 |
} // namespace
|
|
Toshihiro Shimizu |
890ddd |
//------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
void igs::levels::change(
|
|
Shinya Kitaoka |
120a6e |
unsigned char *image_array, const int height, const int width,
|
|
Shinya Kitaoka |
120a6e |
const int channels, const int bits
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const unsigned char *ref /* 求める画像と同じ高、幅、ch数 */
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const int ref_bits /* refがゼロのときはここもゼロ */
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const int ref_mode /* 0=R,1=G,2=B,3=A,4=Luminance,5=Nothing */
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double r_in_min, const double r_in_max // 0...1
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double g_in_min, const double g_in_max // 0...1
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double b_in_min, const double b_in_max // 0...1
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double a_in_min, const double a_in_max // 0...1
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double r_gamma // 0.1 ... 10.0
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double g_gamma // 0.1 ... 10.0
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double b_gamma // 0.1 ... 10.0
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double a_gamma // 0.1 ... 10.0
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double r_out_min, const double r_out_max // 0...1
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double g_out_min, const double g_out_max // 0...1
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double b_out_min, const double b_out_max // 0...1
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double a_out_min, const double a_out_max // 0...1
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const bool clamp_sw, const bool alpha_sw, const bool add_blend_sw) {
|
|
Shinya Kitaoka |
120a6e |
if ((igs::image::rgba::siz != channels) &&
|
|
Shinya Kitaoka |
120a6e |
(igs::image::rgb::siz != channels) && (1 != channels) /* grayscale */
|
|
shun-iwasawa |
481b59 |
) {
|
|
Shinya Kitaoka |
120a6e |
throw std::domain_error("Bad channels,Not rgba/rgb/grayscale");
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
if ((std::numeric_limits<unsigned char="">::digits == bits) &&</unsigned>
|
|
Shinya Kitaoka |
120a6e |
((std::numeric_limits<unsigned char="">::digits == ref_bits) ||</unsigned>
|
|
Shinya Kitaoka |
120a6e |
(0 == ref_bits))) {
|
|
Shinya Kitaoka |
120a6e |
change_(image_array, height, width, channels, ref, ref_mode, r_in_min,
|
|
Shinya Kitaoka |
120a6e |
r_in_max, g_in_min, g_in_max, b_in_min, b_in_max, a_in_min,
|
|
Shinya Kitaoka |
120a6e |
a_in_max, r_gamma, g_gamma, b_gamma, a_gamma, r_out_min, r_out_max,
|
|
Shinya Kitaoka |
120a6e |
g_out_min, g_out_max, b_out_min, b_out_max, a_out_min, a_out_max,
|
|
Shinya Kitaoka |
120a6e |
clamp_sw, alpha_sw, add_blend_sw);
|
|
Shinya Kitaoka |
120a6e |
} else if ((std::numeric_limits<unsigned short="">::digits == bits) &&</unsigned>
|
|
Shinya Kitaoka |
120a6e |
((std::numeric_limits<unsigned char="">::digits == ref_bits) ||</unsigned>
|
|
Shinya Kitaoka |
120a6e |
(0 == ref_bits))) {
|
|
Shinya Kitaoka |
120a6e |
change_(reinterpret_cast<unsigned *="" short="">(image_array), height, width,</unsigned>
|
|
Shinya Kitaoka |
120a6e |
channels, ref, ref_mode, r_in_min, r_in_max, g_in_min, g_in_max,
|
|
Shinya Kitaoka |
120a6e |
b_in_min, b_in_max, a_in_min, a_in_max, r_gamma, g_gamma, b_gamma,
|
|
Shinya Kitaoka |
120a6e |
a_gamma, r_out_min, r_out_max, g_out_min, g_out_max, b_out_min,
|
|
Shinya Kitaoka |
120a6e |
b_out_max, a_out_min, a_out_max, clamp_sw, alpha_sw, add_blend_sw);
|
|
Shinya Kitaoka |
120a6e |
} else if ((std::numeric_limits<unsigned short="">::digits == bits) &&</unsigned>
|
|
Shinya Kitaoka |
120a6e |
(std::numeric_limits<unsigned short="">::digits == ref_bits)) {</unsigned>
|
|
Shinya Kitaoka |
120a6e |
change_(reinterpret_cast<unsigned *="" short="">(image_array), height, width,</unsigned>
|
|
Shinya Kitaoka |
120a6e |
channels, reinterpret_cast<const *="" short="" unsigned="">(ref), ref_mode,</const>
|
|
Shinya Kitaoka |
120a6e |
r_in_min, r_in_max, g_in_min, g_in_max, b_in_min, b_in_max,
|
|
Shinya Kitaoka |
120a6e |
a_in_min, a_in_max, r_gamma, g_gamma, b_gamma, a_gamma, r_out_min,
|
|
Shinya Kitaoka |
120a6e |
r_out_max, g_out_min, g_out_max, b_out_min, b_out_max, a_out_min,
|
|
Shinya Kitaoka |
120a6e |
a_out_max, clamp_sw, alpha_sw, add_blend_sw);
|
|
Shinya Kitaoka |
120a6e |
} else if ((std::numeric_limits<unsigned char="">::digits == bits) &&</unsigned>
|
|
Shinya Kitaoka |
120a6e |
(std::numeric_limits<unsigned short="">::digits == ref_bits)) {</unsigned>
|
|
Shinya Kitaoka |
120a6e |
change_(image_array, height, width, channels,
|
|
Shinya Kitaoka |
120a6e |
reinterpret_cast<const *="" short="" unsigned="">(ref), ref_mode, r_in_min,</const>
|
|
Shinya Kitaoka |
120a6e |
r_in_max, g_in_min, g_in_max, b_in_min, b_in_max, a_in_min,
|
|
Shinya Kitaoka |
120a6e |
a_in_max, r_gamma, g_gamma, b_gamma, a_gamma, r_out_min, r_out_max,
|
|
Shinya Kitaoka |
120a6e |
g_out_min, g_out_max, b_out_min, b_out_max, a_out_min, a_out_max,
|
|
Shinya Kitaoka |
120a6e |
clamp_sw, alpha_sw, add_blend_sw);
|
|
shun-iwasawa |
481b59 |
} else if (std::numeric_limits<float>::digits == bits) {</float>
|
|
shun-iwasawa |
481b59 |
assert(std::numeric_limits<float>::digits == ref_bits || 0 == ref_bits);</float>
|
|
shun-iwasawa |
481b59 |
change_(reinterpret_cast<float *="">(image_array), height, width, channels,</float>
|
|
shun-iwasawa |
481b59 |
reinterpret_cast<const *="" float="">(ref), ref_mode, r_in_min, r_in_max,</const>
|
|
shun-iwasawa |
481b59 |
g_in_min, g_in_max, b_in_min, b_in_max, a_in_min, a_in_max, r_gamma,
|
|
shun-iwasawa |
481b59 |
g_gamma, b_gamma, a_gamma, r_out_min, r_out_max, g_out_min,
|
|
shun-iwasawa |
481b59 |
g_out_max, b_out_min, b_out_max, a_out_min, a_out_max, clamp_sw,
|
|
shun-iwasawa |
481b59 |
alpha_sw, add_blend_sw);
|
|
Shinya Kitaoka |
120a6e |
} else {
|
|
Shinya Kitaoka |
120a6e |
throw std::domain_error("Bad bits,Not uchar/ushort");
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|