Shinya Kitaoka 120a6e
#include <stdexcept>        /* std::domain_error(-) */</stdexcept>
Toshihiro Shimizu 890ddd
#include "igs_ifx_common.h" /* igs::image::rgba */
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
template <class t=""></class>
Shinya Kitaoka 120a6e
void change_channel_(T *ima, const int pix_size, const int channels) {
Shinya Kitaoka 120a6e
  for (int ii = 0; ii < pix_size; ++ii, ima += channels) {
Shinya Kitaoka 120a6e
    ima[0] = ~ima[0];
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
template <class t=""></class>
Shinya Kitaoka 120a6e
void change_multiplied_rgb(T *ima, T *alp, const int pix_size,
Shinya Kitaoka 120a6e
                           const int channels) {
Shinya Kitaoka 120a6e
  for (int ii = 0; ii < pix_size; ++ii, ima += channels, alp += channels) {
Shinya Kitaoka 120a6e
    if (alp[0] < ima[0]) {
Shinya Kitaoka 120a6e
      ima[0] = 0;
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      ima[0] = alp[0] - ima[0];
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
template <class t=""></class>
Shinya Kitaoka 120a6e
void change_template_(T *ima, const int hh, const int ww, const int ch,
Shinya Kitaoka 120a6e
                      const bool *sw /* each channels switch */
Shinya Kitaoka 120a6e
                      ) {
Shinya Kitaoka 120a6e
  const int sz = hh * ww;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (igs::image::rgba::siz == ch) {
Shinya Kitaoka 120a6e
    using namespace igs::image::rgba;
Shinya Kitaoka 120a6e
    if (sw[0]) {
Shinya Kitaoka 120a6e
      change_multiplied_rgb(&ima[red], &ima[alp], sz, ch);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (sw[1]) {
Shinya Kitaoka 120a6e
      change_multiplied_rgb(&ima[gre], &ima[alp], sz, ch);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (sw[2]) {
Shinya Kitaoka 120a6e
      change_multiplied_rgb(&ima[blu], &ima[alp], sz, ch);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (sw[3]) {
Shinya Kitaoka 120a6e
      change_channel_(&ima[alp], sz, ch);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else if (igs::image::rgb::siz == ch) {
Shinya Kitaoka 120a6e
    using namespace igs::image::rgb;
Shinya Kitaoka 120a6e
    if (sw[0]) {
Shinya Kitaoka 120a6e
      change_channel_(&ima[red], sz, ch);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (sw[1]) {
Shinya Kitaoka 120a6e
      change_channel_(&ima[gre], sz, ch);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (sw[2]) {
Shinya Kitaoka 120a6e
      change_channel_(&ima[blu], sz, ch);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else if (1 == ch) { /* Grayscale */
Shinya Kitaoka 120a6e
    if (sw[0]) {
Shinya Kitaoka 120a6e
      change_channel_(&ima[0], sz, ch);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
#include <limits></limits>
Toshihiro Shimizu 890ddd
#include "igs_negate.h"
Shinya Kitaoka 120a6e
void igs::negate::change(unsigned char *image_array, const int height,
Shinya Kitaoka 120a6e
                         const int width, const int channels, const int bits,
Shinya Kitaoka 120a6e
                         const bool *sw_array /* each channel switch  */
Shinya Kitaoka 120a6e
                         ) {
Shinya Kitaoka 120a6e
  if ((igs::image::rgba::siz != channels) &&
Shinya Kitaoka 120a6e
      (igs::image::rgb::siz != channels) && (1 != channels) /* bit(monoBW) */
Shinya Kitaoka 120a6e
      ) {
Shinya Kitaoka 120a6e
    throw std::domain_error("Bad channels,Not rgba/rgb/grayscale");
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (std::numeric_limits<unsigned char="">::digits == bits) {</unsigned>
Shinya Kitaoka 120a6e
    change_template_(image_array, height, width, channels, sw_array);
Shinya Kitaoka 120a6e
  } else if (std::numeric_limits<unsigned short="">::digits == bits) {</unsigned>
Shinya Kitaoka 120a6e
    change_template_(reinterpret_cast<unsigned *="" short="">(image_array), height,</unsigned>
Shinya Kitaoka 120a6e
                     width, channels, sw_array);
Shinya Kitaoka 120a6e
  } else if (1 == bits) {
Shinya Kitaoka 120a6e
    const int bi         = width * channels * bits;
Shinya Kitaoka 120a6e
    const int dg         = std::numeric_limits<unsigned char="">::digits;</unsigned>
Shinya Kitaoka 120a6e
    const int sl_bytes   = bi / dg + ((0 != (bi % dg)) ? 1 : 0);
Shinya Kitaoka 120a6e
    const int image_size = height * sl_bytes;
Shinya Kitaoka 120a6e
    for (int ii = 0; ii < image_size; ++ii) {
Shinya Kitaoka 120a6e
      image_array[ii] = ~image_array[ii];
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    throw std::domain_error("Bad bits,Not uchar/ushort/bit");
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}