Bug Fixes of BalanceSimplest() and EqualizePiecewise()

BalanceSimplest() and EqualizePiecewise() had bugs.

The bug of BalanceSimplest() is shown below.

library(imagerExtra)
g <- grayscale(boats)
BalanceSimplest(g, 0, 1)
Error in saturateim(im, max_im, min_im, range[2], range[1]): Expecting a single value: [extent=0].

This is because I forgot index of vector started with 1 in R.

When sleft = 0, BalanceSimplest try to use 0th element of a vector.

BalanceSimplest should use 1st element of a vector when sleft = 0.

The bug of EqualizePiecewise() is shown below.

EqualizePiecewise(g, 21850)
Error: Fu is lower than 0
Error: Fu is lower than 0
Error: Fu is lower than 0
Error: Fu is lower than 0
Error: Fu is lower than 0
Image. Width: 256 pix Height: 384 pix Depth: 1 Colour channels: 1 

This is because of integer overflow.

We can reproduce the integer overflow as below.

library(Rcpp)
sourceCpp(code = "
  #include <Rcpp.h>
  //[[Rcpp::export]]
  double test(int n, int m, int i) {
    double out = (double)(n * i) / (double)(m + 1); 
    return out;
  }
")
test(10000,1000000, as.integer(3*10^5))
[1] -1294.966

We can avoid the overflow as below.

sourceCpp(code = "
  #include <Rcpp.h>
  //[[Rcpp::export]]
  double test(int n, int m, int i) {
    double out = (double)n * i / (double)(m + 1); 
    return out;
  }
")
test(10000,1000000, as.integer(3*10^5))
[1] 2999.997
10000 * as.integer(3*10^5) / (1000000 + 1)
[1] 2999.997

I fixed the bugs.

Please confirm the bugs were fixed.

library(devtools)
install_github("ShotaOchi/imagerExtra")
g <- grayscale(boats)
BalanceSimplest(g, 0, 1)
EqualizePiecewise(g, 21850)