Toshihiro Shimizu 890ddd
#include "igs_color_rgb_hsv.h"
Shinya Kitaoka 120a6e
namespace {
Shinya Kitaoka 120a6e
void pixel_rgba_(const double red_in, const double gre_in, const double blu_in,
Shinya Kitaoka 120a6e
                 double &red_out, double &gre_out, double &blu_out,
Shinya Kitaoka 120a6e
                 const double hue_pivot  // 0.0  ...0...360...
Shinya Kitaoka 120a6e
                 ,
Shinya Kitaoka 120a6e
                 const double hue_scale  // 1.0  ...1...
Shinya Kitaoka 120a6e
                 ,
Shinya Kitaoka 120a6e
                 const double hue_shift  // 0.0  ...0...360...
Shinya Kitaoka 120a6e
                 ,
Shinya Kitaoka 120a6e
                 const double sat_pivot  // 0.0  ...0...1...
Shinya Kitaoka 120a6e
                 ,
Shinya Kitaoka 120a6e
                 const double sat_scale  // 1.0  ...1...
Shinya Kitaoka 120a6e
                 ,
Shinya Kitaoka 120a6e
                 const double sat_shift  // 0.0  ...0...1...
Shinya Kitaoka 120a6e
                 ,
Shinya Kitaoka 120a6e
                 const double val_pivot  // 0.0  ...0...1...
Shinya Kitaoka 120a6e
                 ,
Shinya Kitaoka 120a6e
                 const double val_scale  // 1.0  ...1...
Shinya Kitaoka 120a6e
                 ,
Shinya Kitaoka 120a6e
                 const double val_shift  // 0.0  ...0...1...
shun-iwasawa 481b59
) {
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 ((1.0 != hue_scale) || (0.0 != hue_shift)) {
Shinya Kitaoka 120a6e
    hue -= hue_pivot;
Shinya Kitaoka 120a6e
    while (hue < -180.0) {
Shinya Kitaoka 120a6e
      hue += 360.0;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    while (180.0 <= hue) {
Shinya Kitaoka 120a6e
      hue -= 360.0;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    hue *= hue_scale;
Shinya Kitaoka 120a6e
    hue += hue_pivot;
Shinya Kitaoka 120a6e
    hue += hue_shift;
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 ((1.0 != sat_scale) || (0.0 != sat_shift)) {
Shinya Kitaoka 120a6e
    sat -= sat_pivot;
Shinya Kitaoka 120a6e
    sat *= sat_scale;
Shinya Kitaoka 120a6e
    sat += sat_pivot;
Shinya Kitaoka 120a6e
    sat += sat_shift;
shun-iwasawa 481b59
    sat = (sat < 0.0) ? 0.0 : sat;
shun-iwasawa 481b59
    // sat = (sat < 0.0) ? 0.0 : ((1.0 < sat) ? 1.0 : sat);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if ((1.0 != val_scale) || (0.0 != val_shift)) {
Shinya Kitaoka 120a6e
    val -= val_pivot;
Shinya Kitaoka 120a6e
    val *= val_scale;
Shinya Kitaoka 120a6e
    val += val_pivot;
Shinya Kitaoka 120a6e
    val += val_shift;
shun-iwasawa 481b59
    // val = (val < 0.0) ? 0.0 : ((1.0 < val) ? 1.0 : val);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  igs::color::hsv_to_rgb(hue, sat, val, red_out, gre_out, blu_out);
Toshihiro Shimizu 890ddd
}
shun-iwasawa 481b59
}  // namespace
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Shinya Kitaoka 120a6e
#include <limits>           /* std::numeric_limits */</limits>
Toshihiro Shimizu 890ddd
#include "igs_ifx_common.h" /* igs::image::rgba */
Toshihiro Shimizu 890ddd
#include "igs_hsv_adjust.h"
Shinya Kitaoka 120a6e
namespace {
shun-iwasawa 481b59
/* raster画像にノイズをのせる */
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 double hue_pivot, const double hue_scale,
shun-iwasawa 481b59
             const double hue_shift, const double sat_pivot,
shun-iwasawa 481b59
             const double sat_scale, const double sat_shift,
shun-iwasawa 481b59
             const double val_pivot, const double val_scale,
shun-iwasawa 481b59
             const double val_shift, const bool add_blend_sw) {
shun-iwasawa 481b59
  // const int t_max      = std::numeric_limits<it>::max();</it>
shun-iwasawa 481b59
  // const double div_val = static_cast<double>(t_max);</double>
shun-iwasawa 481b59
  // const double mul_val = static_cast<double>(t_max) + 0.999999;</double>
shun-iwasawa 481b59
  const int pixsize = height * width;
shun-iwasawa 481b59
  // 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
      /* 変化量初期値 */
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
Shinya Kitaoka 120a6e
      /* 加算合成で、Alpha値ゼロならRGB値を計算する必要はない */
shun-iwasawa 481b59
      if (add_blend_sw && (0.f == 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
      /* pivot,scale,shiftによってRGB値を変化する */
shun-iwasawa 481b59
      double rr, gg, bb;
shun-iwasawa 481b59
      pixel_rgba_(image_array[red], image_array[gre], image_array[blu], rr, gg,
shun-iwasawa 481b59
                  bb, hue_pivot, hue_scale, hue_shift, sat_pivot, sat_scale,
shun-iwasawa 481b59
                  sat_shift, val_pivot, val_scale, val_shift);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      /* 加算合成で、その値がMaxでなければ変化量に乗算 */
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
      /* ピクセル単位の変化量があれば、RGBを調整する */
shun-iwasawa 481b59
      if ((ref != 0) || (add_blend_sw && (image_array[alp] < 1.f))) {
shun-iwasawa 481b59
        rr = image_array[red] + (rr - image_array[red]) * refv;
shun-iwasawa 481b59
        gg = image_array[gre] + (gg - image_array[gre]) * refv;
shun-iwasawa 481b59
        bb = image_array[blu] + (bb - image_array[blu]) * refv;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      /* 結果をRGBに戻す */
shun-iwasawa 481b59
      image_array[red] = rr;
shun-iwasawa 481b59
      image_array[gre] = gg;
shun-iwasawa 481b59
      image_array[blu] = bb;
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) {
shun-iwasawa 481b59
      float refv = 1.f;
Shinya Kitaoka 120a6e
      if (ref != 0) {
shun-iwasawa 481b59
        refv *= (*ref);
shun-iwasawa 481b59
        ref++;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
shun-iwasawa 481b59
      // const IT *const ia = image_array;
shun-iwasawa 481b59
      // const double rr1   = static_cast<double>(ia[red]) / div_val;</double>
shun-iwasawa 481b59
      // const double gg1   = static_cast<double>(ia[gre]) / div_val;</double>
shun-iwasawa 481b59
      // const double bb1   = static_cast<double>(ia[blu]) / div_val;</double>
shun-iwasawa 481b59
      double rr, gg, bb;
shun-iwasawa 481b59
      pixel_rgba_(image_array[red], image_array[gre], image_array[blu], rr, gg,
shun-iwasawa 481b59
                  bb, hue_pivot, hue_scale, hue_shift, sat_pivot, sat_scale,
shun-iwasawa 481b59
                  sat_shift, val_pivot, val_scale, val_shift);
shun-iwasawa 481b59
shun-iwasawa 481b59
      if (ref != nullptr) {
shun-iwasawa 481b59
        rr = image_array[red] + (rr - image_array[red]) * refv;
shun-iwasawa 481b59
        gg = image_array[gre] + (gg - image_array[gre]) * refv;
shun-iwasawa 481b59
        bb = image_array[blu] + (bb - image_array[blu]) * refv;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
shun-iwasawa 481b59
      image_array[red] = rr;
shun-iwasawa 481b59
      image_array[gre] = gg;
shun-iwasawa 481b59
      image_array[blu] = bb;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else if (1 == channels) { /* grayscale */
Shinya Kitaoka 120a6e
    for (int ii = 0; ii < pixsize; ++ii, image_array += channels) {
shun-iwasawa 481b59
      float refv = 1.f;
shun-iwasawa 481b59
      if (ref != nullptr) {
shun-iwasawa 481b59
        refv *= (*ref);
shun-iwasawa 481b59
        ref++;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
shun-iwasawa 481b59
      double li =
shun-iwasawa 481b59
          (*image_array - val_pivot) * val_scale + val_pivot + val_shift;
Shinya Kitaoka 120a6e
shun-iwasawa 481b59
      if (ref != nullptr) {
shun-iwasawa 481b59
        li = *image_array + (li - *image_array) * refv;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
shun-iwasawa 481b59
      *image_array = li;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
shun-iwasawa 481b59
}  // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <stdexcept> /* std::domain_error  */</stdexcept>
Toshihiro Shimizu 890ddd
void igs::hsv_adjust::change(
shun-iwasawa 481b59
    float *image_array, const int height, const int width, const int channels,
shun-iwasawa 481b59
    const float *ref, /* 求める画像と同じ高、幅、channels数 */
Shinya Kitaoka 120a6e
    const double hue_pivot, const double hue_scale, const double hue_shift,
Shinya Kitaoka 120a6e
    const double sat_pivot, const double sat_scale, const double sat_shift,
shun-iwasawa 481b59
    const double val_pivot, const double val_scale, const double val_shift,
Shinya Kitaoka 120a6e
    const bool add_blend_sw) {
Shinya Kitaoka 120a6e
  if ((1.0 == hue_scale) && (0.0 == hue_shift) && (1.0 == sat_scale) &&
Shinya Kitaoka 120a6e
      (0.0 == sat_shift) && (1.0 == val_scale) && (0.0 == val_shift)) {
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
  }
shun-iwasawa 481b59
  change_(image_array, height, width, channels, ref, hue_pivot, hue_scale,
shun-iwasawa 481b59
          hue_shift, sat_pivot, sat_scale, sat_shift, val_pivot, val_scale,
shun-iwasawa 481b59
          val_shift, add_blend_sw);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* rgb(a)画像にhsv(a)でドットノイズを加える */
shun-iwasawa 481b59
  /*
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, height, width, channels, ref, ref_mode,
Shinya Kitaoka 120a6e
                     hue_pivot, hue_scale, hue_shift, sat_pivot, sat_scale,
Shinya Kitaoka 120a6e
                     sat_shift, val_pivot, val_scale, val_shift, 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_template_(reinterpret_cast<unsigned *="" short="">(image_array), height,</unsigned>
Shinya Kitaoka 120a6e
                     width, channels, ref, ref_mode, hue_pivot, hue_scale,
Shinya Kitaoka 120a6e
                     hue_shift, sat_pivot, sat_scale, sat_shift, val_pivot,
Shinya Kitaoka 120a6e
                     val_scale, val_shift, 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_template_(reinterpret_cast<unsigned *="" short="">(image_array), height,</unsigned>
Shinya Kitaoka 120a6e
                     width, channels,
Shinya Kitaoka 120a6e
                     reinterpret_cast<const *="" short="" unsigned="">(ref), ref_mode,</const>
Shinya Kitaoka 120a6e
                     hue_pivot, hue_scale, hue_shift, sat_pivot, sat_scale,
Shinya Kitaoka 120a6e
                     sat_shift, val_pivot, val_scale, val_shift, 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_template_(image_array, height, width, channels,
Shinya Kitaoka 120a6e
                     reinterpret_cast<const *="" short="" unsigned="">(ref), ref_mode,</const>
Shinya Kitaoka 120a6e
                     hue_pivot, hue_scale, hue_shift, sat_pivot, sat_scale,
Shinya Kitaoka 120a6e
                     sat_shift, val_pivot, val_scale, val_shift, add_blend_sw);
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    throw std::domain_error("Bad bits,Not uchar/ushort");
Shinya Kitaoka 120a6e
  }
shun-iwasawa 481b59
  */
Toshihiro Shimizu 890ddd
}