//---------------------------------------------------------------------------
unsigned int ImagePyramid::init(unsigned int max_num_levels, const IuSize& size,
                                const float& scale_factor, unsigned int size_bound)
{
  if ((scale_factor <= 0) || (scale_factor >=1))
  {
    throw IuException("scale_factor out of range; must be in interval ]0,1[.", __FILE__, __FUNCTION__, __LINE__);
  }

  if (images_ != 0)
    this->reset();

  max_num_levels_ = IUMAX(1u, max_num_levels);
  num_levels_ = max_num_levels_;
  size_bound_ = IUMAX(1u, size_bound);

  // calculate the maximum number of levels
  unsigned int shorter_side = (size.width<size.height) ? size.width : size.height;
  float ratio = static_cast<float>(shorter_side)/static_cast<float>(size_bound_);
  // +1 because the original size is level 0
  unsigned int possible_num_levels = static_cast<int>(
        -logf(ratio)/logf(scale_factor)) + 1;
  if(num_levels_ > possible_num_levels)
    num_levels_ = possible_num_levels;

  // init rate for each level
  scale_factors_ = new float[num_levels_];
  for (unsigned int i=0; i<num_levels_; i++)
  {
    scale_factors_[i] = pow(scale_factor, static_cast<float>(i));
  }

  return num_levels_;
}
//-----------------------------------------------------------------------------
void QGLImageGpuWidget::autoMinMax()
{
  if(bit_depth_ == 8)
  {
    if(num_channels_ == 1)
    {
      iu::ImageGpu_8u_C1* img = reinterpret_cast<iu::ImageGpu_8u_C1*>(image_);
      if(img == 0) return;
      unsigned char cur_min, cur_max;
      iu::minMax(img, img->roi(), cur_min, cur_max);
      min_ = static_cast<float>(cur_min);
      max_ = static_cast<float>(cur_max);
    }
    else
    {
      iu::ImageGpu_8u_C4* img = reinterpret_cast<iu::ImageGpu_8u_C4*>(image_);
      if(img == 0) return;
      uchar4 cur_min, cur_max;
      iu::minMax(img, img->roi(), cur_min, cur_max);
      min_ = static_cast<float>(IUMIN(IUMIN(cur_min.x, cur_min.y), cur_min.z));
      max_ = static_cast<float>(IUMAX(IUMAX(cur_max.x, cur_max.y), cur_max.z));
    }
  }
  else
  {
    if(num_channels_ == 1)
    {
      iu::ImageGpu_32f_C1* img = reinterpret_cast<iu::ImageGpu_32f_C1*>(image_);
      if(img == 0) return;
      iu::minMax(img, img->roi(), min_, max_);
    }
    else
    {
      iu::ImageGpu_32f_C4* img = reinterpret_cast<iu::ImageGpu_32f_C4*>(image_);
      if(img == 0) return;
    }
  }
}