Toshihiro Shimizu 890ddd
#include <vector></vector>
Shinya Kitaoka 120a6e
#include <limits>  // std::numeric_limits</limits>
Toshihiro Shimizu 890ddd
#include "igs_color_rgb_hls.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 lig_range, const double sat_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 == lig_range) && (0.0 == sat_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 != lig_range) {
Shinya Kitaoka 120a6e
      this->lig_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 != 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, lig_rand, sat_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 != lig_range) {
Shinya Kitaoka 120a6e
      lig_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 != alp_range) {
Shinya Kitaoka 120a6e
      alp_rand.seed(random_seed + step++);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    igs::math::random hue_rand_ext, lig_rand_ext, sat_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 != lig_range) {
Shinya Kitaoka 120a6e
      lig_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 != 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 != lig_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->lig_array_[pos] = lig_range * (lig_rand_ext.next_d() - 0.5);
Shinya Kitaoka 120a6e
          } else {
Shinya Kitaoka 120a6e
            this->lig_array_[pos] = lig_range * (lig_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 != 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 lig_value(const int xx, const int yy) {
Shinya Kitaoka 120a6e
    return this->noise_value_(this->lig_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 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->sat_array_.clear();
Shinya Kitaoka 120a6e
    this->lig_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> lig_array_;</double>
Shinya Kitaoka 120a6e
  std::vector<double> sat_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 lig_noise, const double sat_noise,
Shinya Kitaoka 120a6e
                control_term_within_limits_ &lig_term,
Shinya Kitaoka 120a6e
                control_term_within_limits_ &sat_term, double &red_out,
shun-iwasawa 481b59
                double &gre_out, double &blu_out,
shun-iwasawa 481b59
                const bool cylindrical = true) {
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, lig, sat;
shun-iwasawa 481b59
  igs::color::rgb_to_hls(red_in, gre_in, blu_in, hue, lig, sat, cylindrical);
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 != lig_term.noise_range()) {
Shinya Kitaoka 120a6e
    double shift_value = 0;
Shinya Kitaoka 120a6e
    double lignoise    = lig_noise;
Shinya Kitaoka 120a6e
    lig_term.exec(lig, lignoise, shift_value);
Shinya Kitaoka 120a6e
    lig += shift_value * alp_in;
Shinya Kitaoka 120a6e
    lig += lignoise * alp_in;
shun-iwasawa 481b59
    // if (lig < 0.0) {
shun-iwasawa 481b59
    //   lig = 0.0;
shun-iwasawa 481b59
    // } else if (1.0 < lig) {
shun-iwasawa 481b59
    //   lig = 1.0;
shun-iwasawa 481b59
    // }
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;
shun-iwasawa 481b59
    sat = (sat < 0.0) ? 0.0 : sat;
shun-iwasawa 481b59
    // if (sat < 0.0) {
shun-iwasawa 481b59
    //   sat = 0.0;
shun-iwasawa 481b59
    // } else if (1.0 < sat) {
shun-iwasawa 481b59
    //   sat = 1.0;
shun-iwasawa 481b59
    // }
shun-iwasawa 481b59
    //  if( 0.0 == sat ) hue = -1.0; // hls_to_rgb(-)
Shinya Kitaoka 120a6e
  }
shun-iwasawa 481b59
  igs::color::hls_to_rgb(hue, lig, sat, red_out, gre_out, blu_out, cylindrical);
Toshihiro Shimizu 890ddd
}
shun-iwasawa 481b59
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
}
shun-iwasawa 481b59
/*------ raster画像にノイズをのせる ------*/
shun-iwasawa 481b59
void change_(float *image_array, const int width, const int height,
shun-iwasawa 481b59
             const int channels,
shun-iwasawa 481b59
             const float *ref, /* 求める画像(out)と同じ高さ、幅 */
shun-iwasawa 481b59
             noise_reference_ &noise, const double hue_range,
shun-iwasawa 481b59
             control_term_within_limits_ &lig_term,
shun-iwasawa 481b59
             control_term_within_limits_ &sat_term,
shun-iwasawa 481b59
             control_term_within_limits_ &alp_term, const bool add_blend_sw,
shun-iwasawa 481b59
             const bool cylindrical = true) {
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
        /* 変化量初期値 */
shun-iwasawa 481b59
        float refv = 1.f;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        /* 参照画像あればピクセル単位の画像変化量を得る */
shun-iwasawa 481b59
        if (ref != nullptr) {
shun-iwasawa 481b59
          refv *= (*ref);
shun-iwasawa 481b59
          ref++; /* 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 */
shun-iwasawa 481b59
        if (add_blend_sw && (image_array[alp] < 1.f)) {
shun-iwasawa 481b59
          refv *= image_array[alp];
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        if (((0.0 != hue_range) || (0.0 != lig_term.noise_range()) ||
Shinya Kitaoka 120a6e
             (0.0 !=
Shinya Kitaoka 120a6e
              sat_term.noise_range())) /* ノイズがhlsのどれか一つはある */
shun-iwasawa 481b59
        ) {
shun-iwasawa 481b59
          float rr1 = image_array[red], gg1 = image_array[gre],
shun-iwasawa 481b59
                bb1 = image_array[blu], aa1 = image_array[alp];
shun-iwasawa 481b59
          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.lig_value(xx, yy), noise.sat_value(xx, yy), lig_term,
shun-iwasawa 481b59
                     sat_term, rr2, gg2, bb2, cylindrical);
shun-iwasawa 481b59
          if (refv != 1.f) {
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
          }
shun-iwasawa 481b59
          image_array[red] = static_cast<float>(rr2);</float>
shun-iwasawa 481b59
          image_array[gre] = static_cast<float>(gg2);</float>
shun-iwasawa 481b59
          image_array[blu] = static_cast<float>(bb2);</float>
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
        if (0.0 != alp_term.noise_range()) {
shun-iwasawa 481b59
          double aa1 = static_cast<double>(image_array[alp]);</double>
shun-iwasawa 481b59
          double aa2 = 0.;
Shinya Kitaoka 120a6e
          pixel_a_(aa1, noise.alp_value(xx, yy), alp_term, aa2);
shun-iwasawa 481b59
          if (refv != 1.f) {
Shinya Kitaoka 120a6e
            aa2 = (aa2 - aa1) * refv + aa1;
Shinya Kitaoka 120a6e
          }
shun-iwasawa 481b59
          image_array[alp] = static_cast<float>(aa2);</float>
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 != lig_term.noise_range()) ||
Shinya Kitaoka 120a6e
         (0.0 != sat_term.noise_range())) /* ノイズがhlsのどれか一つはある */
shun-iwasawa 481b59
    ) {
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
          /* 変化量初期値 */
shun-iwasawa 481b59
          float refv = 1.f;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
          /* 参照画像あればピクセル単位の画像変化量を得る */
shun-iwasawa 481b59
          if (ref != nullptr) {
shun-iwasawa 481b59
            refv *= (*ref);
shun-iwasawa 481b59
            ref++; /* continue;の前に行うこと */
Shinya Kitaoka 120a6e
          }
Shinya Kitaoka 120a6e
shun-iwasawa 481b59
          float rr1 = image_array[red], gg1 = image_array[gre],
shun-iwasawa 481b59
                bb1  = image_array[blu];
shun-iwasawa 481b59
          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.lig_value(xx, yy), noise.sat_value(xx, yy), lig_term,
shun-iwasawa 481b59
                     sat_term, rr2, gg2, bb2, cylindrical);
shun-iwasawa 481b59
          if (refv != 1.f) {
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
          }
shun-iwasawa 481b59
          image_array[red] = static_cast<float>(rr2);</float>
shun-iwasawa 481b59
          image_array[gre] = static_cast<float>(gg2);</float>
shun-iwasawa 481b59
          image_array[blu] = static_cast<float>(bb2);</float>
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else if (1 == channels) { /* grayscale */
Shinya Kitaoka 120a6e
    if (0.0 != lig_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
          /* 変化量初期値 */
shun-iwasawa 481b59
          float refv = 1.f;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
          /* 参照画像あればピクセル単位の画像変化量を得る */
shun-iwasawa 481b59
          if (ref != nullptr) {
shun-iwasawa 481b59
            refv *= (*ref);
shun-iwasawa 481b59
            ref++; /* continue;の前に行うこと */
Shinya Kitaoka 120a6e
          }
Shinya Kitaoka 120a6e
shun-iwasawa 481b59
          double li1         = static_cast<double>(image_array[0]);</double>
Shinya Kitaoka 120a6e
          double shift_value = 0;
Shinya Kitaoka 120a6e
          double lig_noise   = noise.lig_value(xx, yy);
Shinya Kitaoka 120a6e
          lig_term.exec(li1, lig_noise, shift_value);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
          double li2 = li1;
Shinya Kitaoka 120a6e
          li2 += shift_value;
Shinya Kitaoka 120a6e
          li2 += lig_noise;
shun-iwasawa 481b59
          li2 = (li2 < 0.0) ? 0.0 : li2;
Shinya Kitaoka 120a6e
shun-iwasawa 481b59
          if (refv != 1.f) {
Shinya Kitaoka 120a6e
            li2 = li1 + (li2 - li1) * refv;
Shinya Kitaoka 120a6e
          }
Shinya Kitaoka 120a6e
shun-iwasawa 481b59
          image_array[0] = static_cast<float>(li2);</float>
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
shun-iwasawa 481b59
shun-iwasawa 481b59
}  // namespace
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Shinya Kitaoka 120a6e
#include <stdexcept>  // std::domain_error</stdexcept>
Toshihiro Shimizu 890ddd
#include "igs_hls_noise.h"
Toshihiro Shimizu 890ddd
void igs::hls_noise::change(
shun-iwasawa 481b59
    float *image_array, const int height, const int width, const int channels,
shun-iwasawa 481b59
    const float *ref, /* 求める画像と同じ高、幅 */
Shinya Kitaoka 120a6e
    /* image_arrayに余白が変化してもノイズパターンが変わらない
shun-iwasawa 481b59
              ようにするためにカメラエリアを指定する */
Shinya Kitaoka 120a6e
    const int camera_x, const int camera_y, const int camera_w,
shun-iwasawa 481b59
    const int camera_h, const double hue_range, const double lig_range,
shun-iwasawa 481b59
    const double sat_range, const double alp_range,
shun-iwasawa 481b59
    const unsigned long random_seed, const double near_blur,
Shinya Kitaoka 120a6e
    const double lig_effective, const double lig_center, const int lig_type,
Shinya Kitaoka 120a6e
    const double sat_effective, const double sat_center, const int sat_type,
shun-iwasawa 481b59
    const double alp_effective, const double alp_center, const int alp_type,
shun-iwasawa 481b59
    const bool add_blend_sw, const bool cylindrical) {
Shinya Kitaoka 120a6e
  if ((0.0 == hue_range) && (0.0 == lig_range) && (0.0 == sat_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 */
shun-iwasawa 481b59
  ) {
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, lig_range, sat_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_ lig_term(lig_effective, lig_effective, lig_center,
Shinya Kitaoka 120a6e
                                       lig_type, lig_range);
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_ alp_term(alp_effective, alp_effective, alp_center,
Shinya Kitaoka 120a6e
                                       alp_type, alp_range);
Shinya Kitaoka 120a6e
shun-iwasawa 481b59
  change_(image_array, width, height, channels, ref, noise, hue_range, lig_term,
shun-iwasawa 481b59
          sat_term, alp_term, add_blend_sw, cylindrical);
shun-iwasawa 481b59
  noise.clear(); /* ノイズ画像メモリ解放 */
Toshihiro Shimizu 890ddd
}