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>
Toshihiro Shimizu 890ddd
#include "igs_ifx_common.h" /* igs::image::rgba */
Toshihiro Shimizu 890ddd
#include "igs_levels.h"
Toshihiro Shimizu 890ddd
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 */
Shinya Kitaoka 120a6e
  if (clamp_sw) {
Shinya Kitaoka 120a6e
    val = (val < 0.0) ? 0.0 : ((1.0 < val) ? 1.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
    }
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 */
Shinya Kitaoka 120a6e
  val = (val < 0.0) ? 0.0 : ((1.0 < val) ? 1.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
}
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
}
Toshihiro Shimizu 890ddd
}
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 */
Shinya Kitaoka 120a6e
      ) {
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);
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    throw std::domain_error("Bad bits,Not uchar/ushort");
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}