|
Toshihiro Shimizu |
890ddd |
#include <iostream></iostream>
|
|
Toshihiro Shimizu |
890ddd |
#include <vector></vector>
|
|
Shinya Kitaoka |
120a6e |
#include <limits> // std::numeric_limits</limits>
|
|
Toshihiro Shimizu |
890ddd |
#include "igs_color_rgb_hsv.h"
|
|
Toshihiro Shimizu |
890ddd |
#include "igs_math_random.h"
|
|
Shinya Kitaoka |
120a6e |
#include "igs_ifx_common.h" // igs::image::rgba
|
|
Shinya Kitaoka |
120a6e |
namespace {
|
|
Toshihiro Shimizu |
890ddd |
/*------ バッファをまとめて確保(return or throwで自動解放) ------*/
|
|
Shinya Kitaoka |
120a6e |
class noise_reference_ {
|
|
Toshihiro Shimizu |
890ddd |
public:
|
|
Shinya Kitaoka |
120a6e |
noise_reference_(const int ww, const int hh, const double hue_range,
|
|
Shinya Kitaoka |
120a6e |
const double sat_range, const double val_range,
|
|
Shinya Kitaoka |
120a6e |
const double alp_range, const unsigned long random_seed,
|
|
Shinya Kitaoka |
120a6e |
const double near_blur, const int camera_x,
|
|
Shinya Kitaoka |
120a6e |
const int camera_y, const int camera_w, const int camera_h)
|
|
Shinya Kitaoka |
120a6e |
: w_(ww), h_(hh), nblur_(near_blur) {
|
|
Shinya Kitaoka |
120a6e |
if (0 == ww) {
|
|
Shinya Kitaoka |
120a6e |
return;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0 == hh) {
|
|
Shinya Kitaoka |
120a6e |
return;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
/* 枚数ゼロ(rangeが全部ゼロ)なのでノイズかけない */
|
|
Shinya Kitaoka |
120a6e |
if ((0.0 == hue_range) && (0.0 == sat_range) && (0.0 == val_range) &&
|
|
Shinya Kitaoka |
120a6e |
(0.0 == alp_range)) {
|
|
Shinya Kitaoka |
120a6e |
return;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
/* memory確保 */
|
|
Shinya Kitaoka |
120a6e |
const int sz = ww * hh;
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != hue_range) {
|
|
Shinya Kitaoka |
120a6e |
this->hue_array_.resize(sz);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != sat_range) {
|
|
Shinya Kitaoka |
120a6e |
this->sat_array_.resize(sz);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != val_range) {
|
|
Shinya Kitaoka |
120a6e |
this->val_array_.resize(sz);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != alp_range) {
|
|
Shinya Kitaoka |
120a6e |
this->alp_array_.resize(sz);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
/* タネを設定 */
|
|
Shinya Kitaoka |
120a6e |
igs::math::random hue_rand, sat_rand, val_rand, alp_rand;
|
|
Shinya Kitaoka |
120a6e |
unsigned long step = 0;
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != hue_range) {
|
|
Shinya Kitaoka |
120a6e |
hue_rand.seed(random_seed + step++);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != sat_range) {
|
|
Shinya Kitaoka |
120a6e |
sat_rand.seed(random_seed + step++);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != val_range) {
|
|
Shinya Kitaoka |
120a6e |
val_rand.seed(random_seed + step++);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != alp_range) {
|
|
Shinya Kitaoka |
120a6e |
alp_rand.seed(random_seed + step++);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
igs::math::random hue_rand_ext, sat_rand_ext, val_rand_ext, alp_rand_ext;
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != hue_range) {
|
|
Shinya Kitaoka |
120a6e |
hue_rand_ext.seed(random_seed + step++);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != sat_range) {
|
|
Shinya Kitaoka |
120a6e |
sat_rand_ext.seed(random_seed + step++);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != val_range) {
|
|
Shinya Kitaoka |
120a6e |
val_rand_ext.seed(random_seed + step++);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != alp_range) {
|
|
Shinya Kitaoka |
120a6e |
alp_rand_ext.seed(random_seed + step++);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
/* ノイズを書く */
|
|
Shinya Kitaoka |
120a6e |
/* -???_range/2〜???_range/2 */
|
|
Shinya Kitaoka |
120a6e |
const int x1 = camera_x;
|
|
Shinya Kitaoka |
120a6e |
const int y1 = camera_y;
|
|
Shinya Kitaoka |
120a6e |
const int x2 = camera_x + camera_w - 1;
|
|
Shinya Kitaoka |
120a6e |
const int y2 = camera_y + camera_h - 1;
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
/* マージンあるかないかでノイズパターンが変わってしまうため、
|
|
Shinya Kitaoka |
120a6e |
near_blur用に広げてはいけない */
|
|
Shinya Kitaoka |
120a6e |
// if (0.0 != near_blur) { x1-=1; x2+=1; y1-=1; y2+=1; }
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != hue_range) {
|
|
Shinya Kitaoka |
120a6e |
int pos = 0;
|
|
Shinya Kitaoka |
120a6e |
for (int yy = 0; yy < hh; ++yy) {
|
|
Shinya Kitaoka |
120a6e |
for (int xx = 0; xx < ww; ++xx, ++pos) {
|
|
Shinya Kitaoka |
120a6e |
if ((xx < x1) || (x2 < xx) || (yy < y1) || (y2 < yy)) {
|
|
Shinya Kitaoka |
120a6e |
/* 計算用マージンがあったらそこは別のパターン */
|
|
Shinya Kitaoka |
120a6e |
this->hue_array_[pos] = hue_range * (hue_rand_ext.next_d() - 0.5);
|
|
Shinya Kitaoka |
120a6e |
} else {
|
|
Shinya Kitaoka |
120a6e |
/* 計算用マージン幅が変化してもその中のノイズパターンは
|
|
Shinya Kitaoka |
120a6e |
変わらないようにマージン内は別計算 */
|
|
Shinya Kitaoka |
120a6e |
this->hue_array_[pos] = hue_range * (hue_rand.next_d() - 0.5);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != sat_range) {
|
|
Shinya Kitaoka |
120a6e |
int pos = 0;
|
|
Shinya Kitaoka |
120a6e |
for (int yy = 0; yy < hh; ++yy) {
|
|
Shinya Kitaoka |
120a6e |
for (int xx = 0; xx < ww; ++xx, ++pos) {
|
|
Shinya Kitaoka |
120a6e |
if ((xx < x1) || (x2 < xx) || (yy < y1) || (y2 < yy)) {
|
|
Shinya Kitaoka |
120a6e |
this->sat_array_[pos] = sat_range * (sat_rand_ext.next_d() - 0.5);
|
|
Shinya Kitaoka |
120a6e |
} else {
|
|
Shinya Kitaoka |
120a6e |
this->sat_array_[pos] = sat_range * (sat_rand.next_d() - 0.5);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != val_range) {
|
|
Shinya Kitaoka |
120a6e |
int pos = 0;
|
|
Shinya Kitaoka |
120a6e |
for (int yy = 0; yy < hh; ++yy) {
|
|
Shinya Kitaoka |
120a6e |
for (int xx = 0; xx < ww; ++xx, ++pos) {
|
|
Shinya Kitaoka |
120a6e |
if ((xx < x1) || (x2 < xx) || (yy < y1) || (y2 < yy)) {
|
|
Shinya Kitaoka |
120a6e |
this->val_array_[pos] = val_range * (val_rand_ext.next_d() - 0.5);
|
|
Shinya Kitaoka |
120a6e |
} else {
|
|
Shinya Kitaoka |
120a6e |
this->val_array_[pos] = val_range * (val_rand.next_d() - 0.5);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != alp_range) {
|
|
Shinya Kitaoka |
120a6e |
int pos = 0;
|
|
Shinya Kitaoka |
120a6e |
for (int yy = 0; yy < hh; ++yy) {
|
|
Shinya Kitaoka |
120a6e |
for (int xx = 0; xx < ww; ++xx, ++pos) {
|
|
Shinya Kitaoka |
120a6e |
if ((xx < x1) || (x2 < xx) || (yy < y1) || (y2 < yy)) {
|
|
Shinya Kitaoka |
120a6e |
this->alp_array_[pos] = alp_range * (alp_rand_ext.next_d() - 0.5);
|
|
Shinya Kitaoka |
120a6e |
} else {
|
|
Shinya Kitaoka |
120a6e |
this->alp_array_[pos] = alp_range * (alp_rand.next_d() - 0.5);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
double hue_value(const int xx, const int yy) {
|
|
Shinya Kitaoka |
120a6e |
return this->noise_value_(this->hue_array_, this->w_, this->h_, xx, yy,
|
|
Shinya Kitaoka |
120a6e |
this->nblur_);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
double sat_value(const int xx, const int yy) {
|
|
Shinya Kitaoka |
120a6e |
return this->noise_value_(this->sat_array_, this->w_, this->h_, xx, yy,
|
|
Shinya Kitaoka |
120a6e |
this->nblur_);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
double val_value(const int xx, const int yy) {
|
|
Shinya Kitaoka |
120a6e |
return this->noise_value_(this->val_array_, this->w_, this->h_, xx, yy,
|
|
Shinya Kitaoka |
120a6e |
this->nblur_);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
double alp_value(const int xx, const int yy) {
|
|
Shinya Kitaoka |
120a6e |
return this->noise_value_(this->alp_array_, this->w_, this->h_, xx, yy,
|
|
Shinya Kitaoka |
120a6e |
this->nblur_);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
void clear() {
|
|
Shinya Kitaoka |
120a6e |
this->alp_array_.clear();
|
|
Shinya Kitaoka |
120a6e |
this->val_array_.clear();
|
|
Shinya Kitaoka |
120a6e |
this->sat_array_.clear();
|
|
Shinya Kitaoka |
120a6e |
this->hue_array_.clear();
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
~noise_reference_() { this->clear(); }
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
private:
|
|
Shinya Kitaoka |
120a6e |
const int w_;
|
|
Shinya Kitaoka |
120a6e |
const int h_;
|
|
Shinya Kitaoka |
120a6e |
const double nblur_;
|
|
Shinya Kitaoka |
120a6e |
std::vector<double> hue_array_;</double>
|
|
Shinya Kitaoka |
120a6e |
std::vector<double> sat_array_;</double>
|
|
Shinya Kitaoka |
120a6e |
std::vector<double> val_array_;</double>
|
|
Shinya Kitaoka |
120a6e |
std::vector<double> alp_array_;</double>
|
|
Shinya Kitaoka |
120a6e |
void accum_in_(const double *noise_array, const int ww, const int hh,
|
|
Shinya Kitaoka |
120a6e |
const int xx, const int yy, double &accum_val,
|
|
Shinya Kitaoka |
120a6e |
int &accum_count) {
|
|
Shinya Kitaoka |
120a6e |
if ((0 <= xx) && (xx < ww) && (0 <= yy) && (yy < hh)) {
|
|
Shinya Kitaoka |
120a6e |
accum_val += noise_array[yy * ww + xx];
|
|
Shinya Kitaoka |
120a6e |
++accum_count;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
double noise_value_(const std::vector<double> &noise_vector, const int ww,</double>
|
|
Shinya Kitaoka |
120a6e |
const int hh, const int xx, const int yy,
|
|
Shinya Kitaoka |
120a6e |
const double near_blur) {
|
|
Shinya Kitaoka |
120a6e |
if (noise_vector.size() <= 0) {
|
|
Shinya Kitaoka |
120a6e |
return 0.0;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
const double *noise_array = &noise_vector.at(0);
|
|
Shinya Kitaoka |
120a6e |
if (0.0 == near_blur) {
|
|
Shinya Kitaoka |
120a6e |
return noise_array[yy * ww + xx];
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
double accum_val = 0.0;
|
|
Shinya Kitaoka |
120a6e |
int accum_count = 0;
|
|
Shinya Kitaoka |
120a6e |
this->accum_in_(noise_array, ww, hh, xx - 1, yy - 1, accum_val,
|
|
Shinya Kitaoka |
120a6e |
accum_count);
|
|
Shinya Kitaoka |
120a6e |
this->accum_in_(noise_array, ww, hh, xx, yy - 1, accum_val, accum_count);
|
|
Shinya Kitaoka |
120a6e |
this->accum_in_(noise_array, ww, hh, xx + 1, yy - 1, accum_val,
|
|
Shinya Kitaoka |
120a6e |
accum_count);
|
|
Shinya Kitaoka |
120a6e |
this->accum_in_(noise_array, ww, hh, xx - 1, yy, accum_val, accum_count);
|
|
Shinya Kitaoka |
120a6e |
this->accum_in_(noise_array, ww, hh, xx + 1, yy, accum_val, accum_count);
|
|
Shinya Kitaoka |
120a6e |
this->accum_in_(noise_array, ww, hh, xx - 1, yy + 1, accum_val,
|
|
Shinya Kitaoka |
120a6e |
accum_count);
|
|
Shinya Kitaoka |
120a6e |
this->accum_in_(noise_array, ww, hh, xx, yy + 1, accum_val, accum_count);
|
|
Shinya Kitaoka |
120a6e |
this->accum_in_(noise_array, ww, hh, xx + 1, yy + 1, accum_val,
|
|
Shinya Kitaoka |
120a6e |
accum_count);
|
|
Shinya Kitaoka |
120a6e |
if (accum_count <= 0) {
|
|
Shinya Kitaoka |
120a6e |
return noise_array[yy * ww + xx];
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
accum_val /= static_cast<double>(accum_count);</double>
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
/* 中心Pixelと廻り(1Pixel幅)の平均値とのバランスを返す */
|
|
Shinya Kitaoka |
120a6e |
return (near_blur * accum_val) +
|
|
Shinya Kitaoka |
120a6e |
((1.0 - near_blur) * noise_array[yy * ww + xx]);
|
|
Shinya Kitaoka |
120a6e |
/*
|
|
Shinya Kitaoka |
120a6e |
ノイズをなじませるために、
|
|
Shinya Kitaoka |
120a6e |
8近傍ピクセルと対象ピクセルとの重み付けをする処理
|
|
Shinya Kitaoka |
120a6e |
+-----+-----+-----+
|
|
Shinya Kitaoka |
120a6e |
| p00 | p01 | p02 |
|
|
Shinya Kitaoka |
120a6e |
+-----+-----+-----+
|
|
Shinya Kitaoka |
120a6e |
| p10 | p11 | p12 |
|
|
Shinya Kitaoka |
120a6e |
+-----+-----+-----+
|
|
Shinya Kitaoka |
120a6e |
| p20 | p21 | p22 |
|
|
Shinya Kitaoka |
120a6e |
+-----+-----+-----+
|
|
Shinya Kitaoka |
120a6e |
対象ピクセル = p11
|
|
Shinya Kitaoka |
120a6e |
8近傍ピクセル = p00,p01,p02,p10,p12,p20,p21,p22
|
|
Shinya Kitaoka |
120a6e |
重み付け = near_blur = nb = 0...1
|
|
Shinya Kitaoka |
120a6e |
注意:対象ピクセルが画像のエッジにあると、8近傍ピクセルには、
|
|
Shinya Kitaoka |
120a6e |
存在しないピクセルがあるのでそれは除いて計算する
|
|
Shinya Kitaoka |
120a6e |
p00+p01+p02+p10+p12+p20+p21+p22
|
|
Shinya Kitaoka |
120a6e |
------------------------------- x nb + p22 x (1 - nb)
|
|
Shinya Kitaoka |
120a6e |
8
|
|
Shinya Kitaoka |
120a6e |
nb
|
|
Shinya Kitaoka |
120a6e |
= (p00+p01+p02+p10+p12+p20+p21+p22) x -- + p22 x (1 - nb)
|
|
Shinya Kitaoka |
120a6e |
8
|
|
Shinya Kitaoka |
120a6e |
nb == 0のとき
|
|
Shinya Kitaoka |
120a6e |
--> 対象ピクセルの値
|
|
Shinya Kitaoka |
120a6e |
= p22
|
|
Shinya Kitaoka |
120a6e |
nb == 0.5のとき
|
|
Shinya Kitaoka |
120a6e |
--> 対象が半分、8近傍平均値が半分
|
|
Shinya Kitaoka |
120a6e |
= (p00+p01+p02+p10+p12+p20+p21+p22) / 16 + p22 / 2
|
|
Shinya Kitaoka |
120a6e |
nb == 0.888...(8/9)のとき
|
|
Shinya Kitaoka |
120a6e |
--> 対象と8近傍クセルの平均値
|
|
Shinya Kitaoka |
120a6e |
= (p00+p01+p02+p10+p12+p20+p21+p22) x 8 / 72 + p22 / 9
|
|
Shinya Kitaoka |
120a6e |
nb == 1のとき
|
|
Shinya Kitaoka |
120a6e |
--> 8近傍ピクセルのみの平均値
|
|
Shinya Kitaoka |
120a6e |
= (p00+p01+p02+p10+p12+p20+p21+p22) / 8
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* copy constructorを無効化 */
|
|
Shinya Kitaoka |
120a6e |
noise_reference_(const noise_reference_ &);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* 代入演算子を無効化 */
|
|
Shinya Kitaoka |
120a6e |
noise_reference_ &operator=(const noise_reference_ &);
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
/*------ 端値を適度に調整する ------*/
|
|
Shinya Kitaoka |
120a6e |
class control_term_within_limits_ {
|
|
Toshihiro Shimizu |
890ddd |
public:
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits_(const double effective_low /* = 0.0 */
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double effective_high /* = 0.0 */
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double center /* = 0.5 */
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const int type /* = 0 */
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double noise_range /* = 0.0 */
|
|
Shinya Kitaoka |
120a6e |
)
|
|
Shinya Kitaoka |
120a6e |
: effective_low_(effective_low)
|
|
Shinya Kitaoka |
120a6e |
, effective_high_(effective_high)
|
|
Shinya Kitaoka |
120a6e |
, center_(center)
|
|
Shinya Kitaoka |
120a6e |
, type_(static_cast<term_type_>(type))</term_type_>
|
|
Shinya Kitaoka |
120a6e |
, noise_range_(noise_range) {}
|
|
Shinya Kitaoka |
120a6e |
void exec(const double current_value /* 0...1 */
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
double &noise /* -noise_range/2...noise_range/2 */
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
double &shift_value) {
|
|
Shinya Kitaoka |
120a6e |
if ((0.0 < this->effective_low_) && (current_value < this->center_)) {
|
|
Shinya Kitaoka |
120a6e |
const double cen = this->center_;
|
|
Shinya Kitaoka |
120a6e |
const double val = current_value;
|
|
Shinya Kitaoka |
120a6e |
const double ran = this->noise_range_;
|
|
Shinya Kitaoka |
120a6e |
const double eff = this->effective_low_;
|
|
Shinya Kitaoka |
120a6e |
switch (this->type_) {
|
|
Shinya Kitaoka |
120a6e |
case shift_all_:
|
|
Shinya Kitaoka |
120a6e |
shift_value = ((cen - val) / cen) * (ran / 2.0) * eff;
|
|
Shinya Kitaoka |
120a6e |
break;
|
|
Shinya Kitaoka |
120a6e |
case shift_term_:
|
|
Shinya Kitaoka |
120a6e |
if (val < ran) {
|
|
Shinya Kitaoka |
120a6e |
shift_value = (((cen < ran) ? cen : ran) - val) / 2.0 * eff;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
break;
|
|
Shinya Kitaoka |
120a6e |
case decrease_all_: {
|
|
Shinya Kitaoka |
120a6e |
const double tmp = (cen - val) / cen * eff;
|
|
Shinya Kitaoka |
120a6e |
if (0.0 < tmp) {
|
|
Shinya Kitaoka |
120a6e |
noise *= 1.0 - tmp;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
} break;
|
|
Shinya Kitaoka |
120a6e |
case decrease_term_:
|
|
Shinya Kitaoka |
120a6e |
if (val < (ran / 2.0)) {
|
|
Shinya Kitaoka |
120a6e |
const double stop = (cen < (ran / 2.0)) ? cen : ran / 2.0;
|
|
Shinya Kitaoka |
120a6e |
const double tmp = (stop - val) / stop * eff;
|
|
Shinya Kitaoka |
120a6e |
if (0.0 < tmp) {
|
|
Shinya Kitaoka |
120a6e |
noise *= 1.0 - tmp;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
break;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
if ((0.0 < this->effective_high_) && (this->center_ < current_value)) {
|
|
Shinya Kitaoka |
120a6e |
const double cen = this->center_;
|
|
Shinya Kitaoka |
120a6e |
const double val = current_value;
|
|
Shinya Kitaoka |
120a6e |
const double ran = this->noise_range_;
|
|
Shinya Kitaoka |
120a6e |
const double eff = this->effective_high_;
|
|
Shinya Kitaoka |
120a6e |
switch (this->type_) {
|
|
Shinya Kitaoka |
120a6e |
case shift_all_:
|
|
Shinya Kitaoka |
120a6e |
shift_value = ((cen - val) / (1.0 - cen)) * (ran / 2.0) * eff;
|
|
Shinya Kitaoka |
120a6e |
break;
|
|
Shinya Kitaoka |
120a6e |
case shift_term_:
|
|
Shinya Kitaoka |
120a6e |
if ((1.0 - ran) < val) {
|
|
Shinya Kitaoka |
120a6e |
const double ira = 1.0 - ran;
|
|
Shinya Kitaoka |
120a6e |
shift_value = (((cen < ira) ? ira : cen) - val) / 2.0 * eff;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
break;
|
|
Shinya Kitaoka |
120a6e |
case decrease_all_: {
|
|
Shinya Kitaoka |
120a6e |
const double tmp = (val - cen) / (1.0 - cen) * eff;
|
|
Shinya Kitaoka |
120a6e |
if (0.0 < tmp) {
|
|
Shinya Kitaoka |
120a6e |
noise *= 1.0 - tmp;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
} break;
|
|
Shinya Kitaoka |
120a6e |
case decrease_term_:
|
|
Shinya Kitaoka |
120a6e |
if ((1.0 - (ran / 2.0)) < val) {
|
|
Shinya Kitaoka |
120a6e |
const double rpos = 1.0 - (ran / 2.0);
|
|
Shinya Kitaoka |
120a6e |
const double stop = (cen < rpos) ? rpos : cen;
|
|
Shinya Kitaoka |
120a6e |
const double tmp = (val - stop) / (1.0 - stop) * eff;
|
|
Shinya Kitaoka |
120a6e |
if (0.0 < tmp) {
|
|
Shinya Kitaoka |
120a6e |
noise *= 1.0 - tmp;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
break;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
double noise_range(void) const { return this->noise_range_; }
|
|
Shinya Kitaoka |
120a6e |
|
|
Toshihiro Shimizu |
890ddd |
private:
|
|
Shinya Kitaoka |
120a6e |
/* low,high両方ゼロ =exec()内処理せず =端値はカット =default */
|
|
Shinya Kitaoka |
120a6e |
const double effective_low_;
|
|
Shinya Kitaoka |
120a6e |
const double effective_high_;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
const double center_;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
enum term_type_ { /* 端値の調整方法 */
|
|
Shinya Kitaoka |
120a6e |
shift_all_ = 0, /* 0.全体的にノイズ位置がずれる */
|
|
Shinya Kitaoka |
120a6e |
shift_term_, /* 1.端のみでノイズ位置がずれる */
|
|
Shinya Kitaoka |
120a6e |
decrease_all_, /* 2.全体的にノイズ幅が減る */
|
|
Shinya Kitaoka |
120a6e |
decrease_term_, /* 3.端のみでノイズ幅が減る */
|
|
Shinya Kitaoka |
120a6e |
};
|
|
Shinya Kitaoka |
120a6e |
const term_type_ type_;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
const double noise_range_;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* copy constructorを無効化 */
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits_(const control_term_within_limits_ &);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* 代入演算子を無効化 */
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits_ &operator=(const control_term_within_limits_ &);
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
/*------ RGB値にノイズをのせる ------*/
|
|
Shinya Kitaoka |
120a6e |
void pixel_rgb_(const double red_in, const double gre_in, const double blu_in,
|
|
Shinya Kitaoka |
120a6e |
const double alp_in, const double hue_noise,
|
|
Shinya Kitaoka |
120a6e |
const double sat_noise, const double val_noise,
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits_ &sat_term,
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits_ &val_term, double &red_out,
|
|
Shinya Kitaoka |
120a6e |
double &gre_out, double &blu_out) {
|
|
Shinya Kitaoka |
120a6e |
if (0.0 == alp_in) {
|
|
Shinya Kitaoka |
120a6e |
red_out = red_in;
|
|
Shinya Kitaoka |
120a6e |
gre_out = gre_in;
|
|
Shinya Kitaoka |
120a6e |
blu_out = blu_in;
|
|
Shinya Kitaoka |
120a6e |
return;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
double hue, sat, val;
|
|
Shinya Kitaoka |
120a6e |
igs::color::rgb_to_hsv(red_in, gre_in, blu_in, hue, sat, val);
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != hue_noise) {
|
|
Shinya Kitaoka |
120a6e |
hue += 360.0 * hue_noise * alp_in;
|
|
Shinya Kitaoka |
120a6e |
while (hue < 0.0) {
|
|
Shinya Kitaoka |
120a6e |
hue += 360.0;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
while (360.0 <= hue) {
|
|
Shinya Kitaoka |
120a6e |
hue -= 360.0;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != sat_term.noise_range()) {
|
|
Shinya Kitaoka |
120a6e |
double shift_value = 0;
|
|
Shinya Kitaoka |
120a6e |
double satnoise = sat_noise;
|
|
Shinya Kitaoka |
120a6e |
sat_term.exec(sat, satnoise, shift_value);
|
|
Shinya Kitaoka |
120a6e |
sat += shift_value * alp_in;
|
|
Shinya Kitaoka |
120a6e |
sat += satnoise * alp_in;
|
|
Shinya Kitaoka |
120a6e |
if (sat < 0.0) {
|
|
Shinya Kitaoka |
120a6e |
sat = 0.0;
|
|
Shinya Kitaoka |
120a6e |
} else if (1.0 < sat) {
|
|
Shinya Kitaoka |
120a6e |
sat = 1.0;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
// if( 0.0 == sat ) hue = -1.0; // hsv_to_rgb(-)
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != val_term.noise_range()) {
|
|
Shinya Kitaoka |
120a6e |
double shift_value = 0;
|
|
Shinya Kitaoka |
120a6e |
double valnoise = val_noise;
|
|
Shinya Kitaoka |
120a6e |
val_term.exec(val, valnoise, shift_value);
|
|
Shinya Kitaoka |
120a6e |
val += shift_value * alp_in;
|
|
Shinya Kitaoka |
120a6e |
val += valnoise * alp_in;
|
|
Shinya Kitaoka |
120a6e |
if (val < 0.0) {
|
|
Shinya Kitaoka |
120a6e |
val = 0.0;
|
|
Shinya Kitaoka |
120a6e |
} else if (1.0 < val) {
|
|
Shinya Kitaoka |
120a6e |
val = 1.0;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
igs::color::hsv_to_rgb(hue, sat, val, red_out, gre_out, blu_out);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
/*------ Alpha値にノイズをのせる ------*/
|
|
Shinya Kitaoka |
120a6e |
void pixel_a_(const double alp_in, const double alp_noise,
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits_ &alp_term, double &alp_out) {
|
|
Shinya Kitaoka |
120a6e |
// if (0.0 == alp_in) { return; }
|
|
Shinya Kitaoka |
120a6e |
double alpin = alp_in;
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != alp_term.noise_range()) {
|
|
Shinya Kitaoka |
120a6e |
double shift_value = 0.0;
|
|
Shinya Kitaoka |
120a6e |
double alpnoise = alp_noise;
|
|
Shinya Kitaoka |
120a6e |
alp_term.exec(alpin, alpnoise, shift_value);
|
|
Shinya Kitaoka |
120a6e |
const double mask = alpin;
|
|
Shinya Kitaoka |
120a6e |
alpin += shift_value * mask;
|
|
Shinya Kitaoka |
120a6e |
alpin += alpnoise * mask;
|
|
Shinya Kitaoka |
120a6e |
if (alpin < 0.0) {
|
|
Shinya Kitaoka |
120a6e |
alpin = 0.0;
|
|
Shinya Kitaoka |
120a6e |
} else if (1.0 < alpin) {
|
|
Shinya Kitaoka |
120a6e |
alpin = 1.0;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
alp_out = alpin;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
/*------ raster画像にノイズをのせるtemplate ------*/
|
|
Toshihiro Shimizu |
890ddd |
template <class class="" it,="" rt=""></class>
|
|
Toshihiro Shimizu |
890ddd |
void change_template_(
|
|
Shinya Kitaoka |
120a6e |
IT *image_array, const int width, const int height, const int channels
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const RT *ref /* 求める画像(out)と同じ高さ、幅、チャンネル数 */
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const int ref_mode /* 0=R,1=G,2=B,3=A,4=Luminance,5=Nothing */
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
noise_reference_ &noise, const double hue_range,
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits_ &sat_term,
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits_ &val_term, control_term_within_limits_ &alp_term
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const bool add_blend_sw) {
|
|
Shinya Kitaoka |
120a6e |
const int t_max = std::numeric_limits<it>::max();</it>
|
|
Shinya Kitaoka |
120a6e |
const double div_val = static_cast<double>(t_max);</double>
|
|
Shinya Kitaoka |
120a6e |
const double mul_val = static_cast<double>(t_max) + 0.999999;</double>
|
|
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 yy = 0; yy < height; ++yy) {
|
|
Shinya Kitaoka |
120a6e |
for (int xx = 0; xx < width; ++xx, image_array += channels) {
|
|
Shinya Kitaoka |
120a6e |
/* 変化量初期値 */
|
|
Shinya Kitaoka |
120a6e |
double refv = 1.0;
|
|
Shinya Kitaoka |
120a6e |
|
|
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 |
}
|
|
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合成の時は、
|
|
Shinya Kitaoka |
120a6e |
Alpha値がゼロでもRGB値は存在する(してもよい) */
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
/* マスクSWがON、なら変化をMask */
|
|
Shinya Kitaoka |
120a6e |
if (add_blend_sw && (image_array[alp] < t_max)) {
|
|
Shinya Kitaoka |
120a6e |
refv *= static_cast<double>(image_array[alp]) / div_val;</double>
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
if (((0.0 != hue_range) || (0.0 != val_term.noise_range()) ||
|
|
Shinya Kitaoka |
120a6e |
(0.0 !=
|
|
Shinya Kitaoka |
120a6e |
sat_term.noise_range())) /* ノイズがhsvのどれか一つはある */
|
|
Shinya Kitaoka |
120a6e |
) {
|
|
Shinya Kitaoka |
120a6e |
double rr1 = static_cast<double>(image_array[red]) / div_val,</double>
|
|
Shinya Kitaoka |
120a6e |
gg1 = static_cast<double>(image_array[gre]) / div_val,</double>
|
|
Shinya Kitaoka |
120a6e |
bb1 = static_cast<double>(image_array[blu]) / div_val,</double>
|
|
Shinya Kitaoka |
120a6e |
aa1 = static_cast<double>(image_array[alp]) / div_val;</double>
|
|
Shinya Kitaoka |
120a6e |
double rr2 = 0, gg2 = 0, bb2 = 0;
|
|
Shinya Kitaoka |
120a6e |
pixel_rgb_(rr1, gg1, bb1, aa1, noise.hue_value(xx, yy),
|
|
Shinya Kitaoka |
120a6e |
noise.sat_value(xx, yy), noise.val_value(xx, yy), sat_term,
|
|
Shinya Kitaoka |
120a6e |
val_term, rr2, gg2, bb2);
|
|
Shinya Kitaoka |
120a6e |
if (refv != 1.0) {
|
|
Shinya Kitaoka |
120a6e |
rr2 = (rr2 - rr1) * refv + rr1;
|
|
Shinya Kitaoka |
120a6e |
gg2 = (gg2 - gg1) * refv + gg1;
|
|
Shinya Kitaoka |
120a6e |
bb2 = (bb2 - bb1) * refv + bb1;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
image_array[red] = static_cast<it>(rr2 * mul_val);</it>
|
|
Shinya Kitaoka |
120a6e |
image_array[gre] = static_cast<it>(gg2 * mul_val);</it>
|
|
Shinya Kitaoka |
120a6e |
image_array[blu] = static_cast<it>(bb2 * mul_val);</it>
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != alp_term.noise_range()) {
|
|
Shinya Kitaoka |
120a6e |
double aa1 = static_cast<double>(image_array[alp]) / div_val;</double>
|
|
Shinya Kitaoka |
120a6e |
double aa2 = 0;
|
|
Shinya Kitaoka |
120a6e |
pixel_a_(aa1, noise.alp_value(xx, yy), alp_term, aa2);
|
|
Shinya Kitaoka |
120a6e |
if (refv != 1.0) {
|
|
Shinya Kitaoka |
120a6e |
aa2 = (aa2 - aa1) * refv + aa1;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
image_array[alp] = static_cast<it>(aa2 * mul_val);</it>
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
} else if (igs::image::rgb::siz == channels) {
|
|
Shinya Kitaoka |
120a6e |
using namespace igs::image::rgb;
|
|
Shinya Kitaoka |
120a6e |
if (((0.0 != hue_range) || (0.0 != sat_term.noise_range()) ||
|
|
Shinya Kitaoka |
120a6e |
(0.0 != val_term.noise_range())) /* ノイズがhsvのどれか一つはある */
|
|
Shinya Kitaoka |
120a6e |
) {
|
|
Shinya Kitaoka |
120a6e |
for (int yy = 0; yy < height; ++yy) {
|
|
Shinya Kitaoka |
120a6e |
for (int xx = 0; xx < width; ++xx, image_array += channels) {
|
|
Shinya Kitaoka |
120a6e |
/* 変化量初期値 */
|
|
Shinya Kitaoka |
120a6e |
double refv = 1.0;
|
|
Shinya Kitaoka |
120a6e |
|
|
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 |
}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
double rr1 = static_cast<double>(image_array[red]) / div_val,</double>
|
|
Shinya Kitaoka |
120a6e |
gg1 = static_cast<double>(image_array[gre]) / div_val,</double>
|
|
Shinya Kitaoka |
120a6e |
bb1 = static_cast<double>(image_array[blu]) / div_val;</double>
|
|
Shinya Kitaoka |
120a6e |
double rr2 = 0, gg2 = 0, bb2 = 0;
|
|
Shinya Kitaoka |
120a6e |
pixel_rgb_(rr1, gg1, bb1, 1.0, noise.hue_value(xx, yy),
|
|
Shinya Kitaoka |
120a6e |
noise.sat_value(xx, yy), noise.val_value(xx, yy), sat_term,
|
|
Shinya Kitaoka |
120a6e |
val_term, rr2, gg2, bb2);
|
|
Shinya Kitaoka |
120a6e |
if (refv != 1.0) {
|
|
Shinya Kitaoka |
120a6e |
rr2 = (rr2 - rr1) * refv + rr1;
|
|
Shinya Kitaoka |
120a6e |
gg2 = (gg2 - gg1) * refv + gg1;
|
|
Shinya Kitaoka |
120a6e |
bb2 = (bb2 - bb1) * refv + bb1;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
image_array[red] = static_cast<it>(rr2 * mul_val);</it>
|
|
Shinya Kitaoka |
120a6e |
image_array[gre] = static_cast<it>(gg2 * mul_val);</it>
|
|
Shinya Kitaoka |
120a6e |
image_array[blu] = static_cast<it>(bb2 * mul_val);</it>
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
} else if (1 == channels) { /* grayscale */
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != val_term.noise_range()) {
|
|
Shinya Kitaoka |
120a6e |
for (int yy = 0; yy < height; ++yy) {
|
|
Shinya Kitaoka |
120a6e |
for (int xx = 0; xx < width; ++xx, ++image_array) {
|
|
Shinya Kitaoka |
120a6e |
/* 変化量初期値 */
|
|
Shinya Kitaoka |
120a6e |
double refv = 1.0;
|
|
Shinya Kitaoka |
120a6e |
|
|
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 |
}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
double va1 = static_cast<double>(image_array[0]) / div_val;</double>
|
|
Shinya Kitaoka |
120a6e |
double shift_value = 0;
|
|
Shinya Kitaoka |
120a6e |
double val_noise = noise.val_value(xx, yy);
|
|
Shinya Kitaoka |
120a6e |
val_term.exec(va1, val_noise, shift_value);
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
double va2 = va1;
|
|
Shinya Kitaoka |
120a6e |
va2 += shift_value;
|
|
Shinya Kitaoka |
120a6e |
va2 += val_noise;
|
|
Shinya Kitaoka |
120a6e |
va2 = (va2 < 0.0) ? 0.0 : ((1.0 < va2) ? 1.0 : va2);
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
if (refv != 1.0) {
|
|
Shinya Kitaoka |
120a6e |
va2 = va1 + (va2 - va1) * refv;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
image_array[0] = static_cast<it>(va2 * mul_val);</it>
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
#include <stdexcept> // std::domain_error</stdexcept>
|
|
Toshihiro Shimizu |
890ddd |
#include "igs_hsv_noise.h"
|
|
Toshihiro Shimizu |
890ddd |
void igs::hsv_noise::change(
|
|
Shinya Kitaoka |
120a6e |
unsigned char *image_array
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const int height, const int width, const int channels, const int bits
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const unsigned char *ref /* 求める画像と同じ高、幅、channels数 */
|
|
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 */
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
/* image_arrayに余白が変化してもノイズパターンが変わらない
|
|
Shinya Kitaoka |
120a6e |
ようにするためにカメラエリアを指定する */
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const int camera_x, const int camera_y, const int camera_w,
|
|
Shinya Kitaoka |
120a6e |
const int camera_h
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double hue_range, const double sat_range, const double val_range,
|
|
Shinya Kitaoka |
120a6e |
const double alp_range, const unsigned long random_seed,
|
|
Shinya Kitaoka |
120a6e |
const double near_blur
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double sat_effective, const double sat_center, const int sat_type,
|
|
Shinya Kitaoka |
120a6e |
const double val_effective, const double val_center, const int val_type,
|
|
Shinya Kitaoka |
120a6e |
const double alp_effective, const double alp_center, const int alp_type
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const bool add_blend_sw) {
|
|
Shinya Kitaoka |
120a6e |
if ((0.0 == hue_range) && (0.0 == sat_range) && (0.0 == val_range) &&
|
|
Shinya Kitaoka |
120a6e |
(0.0 == alp_range)) {
|
|
Shinya Kitaoka |
120a6e |
return;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
if ((igs::image::rgba::siz != channels) &&
|
|
Shinya Kitaoka |
120a6e |
(igs::image::rgb::siz != channels) && (1 != channels) /* grayscale */
|
|
Shinya Kitaoka |
120a6e |
) {
|
|
Shinya Kitaoka |
120a6e |
throw std::domain_error("Bad channels,Not rgba/rgb/grayscale");
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
/* ノイズ参照画像を作成する */
|
|
Shinya Kitaoka |
120a6e |
noise_reference_ noise(width, height, hue_range, sat_range, val_range,
|
|
Shinya Kitaoka |
120a6e |
alp_range, random_seed, near_blur, camera_x, camera_y,
|
|
Shinya Kitaoka |
120a6e |
camera_w, camera_h);
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
/* 端値を適度に調整する設定 */
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits_ sat_term(sat_effective, sat_effective, sat_center,
|
|
Shinya Kitaoka |
120a6e |
sat_type, sat_range);
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits_ val_term(val_effective, val_effective, val_center,
|
|
Shinya Kitaoka |
120a6e |
val_type, val_range);
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits_ alp_term(alp_effective, alp_effective, alp_center,
|
|
Shinya Kitaoka |
120a6e |
alp_type, alp_range);
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
/* rgb(a)画像にhsv(a)でドットノイズを加える */
|
|
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_template_(image_array, width, height, channels, ref, ref_mode, noise,
|
|
Shinya Kitaoka |
120a6e |
hue_range, sat_term, val_term, alp_term, add_blend_sw);
|
|
Shinya Kitaoka |
120a6e |
noise.clear(); /* ノイズ画像メモリ解放 */
|
|
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_template_(reinterpret_cast<unsigned *="" short="">(image_array), width,</unsigned>
|
|
Shinya Kitaoka |
120a6e |
height, channels, ref, ref_mode, noise, hue_range,
|
|
Shinya Kitaoka |
120a6e |
sat_term, val_term, alp_term, add_blend_sw);
|
|
Shinya Kitaoka |
120a6e |
noise.clear(); /* ノイズ画像メモリ解放 */
|
|
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_template_(
|
|
Shinya Kitaoka |
120a6e |
reinterpret_cast<unsigned *="" short="">(image_array), width, height,</unsigned>
|
|
Shinya Kitaoka |
120a6e |
channels, reinterpret_cast<const *="" short="" unsigned="">(ref), ref_mode,</const>
|
|
Shinya Kitaoka |
120a6e |
noise, hue_range, sat_term, val_term, alp_term, add_blend_sw);
|
|
Shinya Kitaoka |
120a6e |
noise.clear(); /* ノイズ画像メモリ解放 */
|
|
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_template_(image_array, width, height, channels,
|
|
Shinya Kitaoka |
120a6e |
reinterpret_cast<const *="" short="" unsigned="">(ref), ref_mode,</const>
|
|
Shinya Kitaoka |
120a6e |
noise, hue_range, sat_term, val_term, alp_term,
|
|
Shinya Kitaoka |
120a6e |
add_blend_sw);
|
|
Shinya Kitaoka |
120a6e |
noise.clear(); /* ノイズ画像メモリ解放 */
|
|
Shinya Kitaoka |
120a6e |
} else {
|
|
Shinya Kitaoka |
120a6e |
throw std::domain_error("Bad bits,Not uchar/ushort");
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|