// see https://github.com/fiji/Auto_Threshold/blob/master/src/main/java/fiji/threshold/Auto_Threshold.java
// Implements Yen  thresholding method
// 1) Yen J.C., Chang F.J., and Chang S. (1995) "A New Criterion
//    for Automatic Multilevel Thresholding" IEEE Trans. on Image
//    Processing, 4(3): 370-378
// 2) Sezgin M. and Sankur B. (2004) "Survey over Image Thresholding
//    Techniques and Quantitative Performance Evaluation" Journal of
//    Electronic Imaging, 13(1): 146-165
//    http://citeseer.ist.psu.edu/sezgin04survey.html
//
// M. Emre Celebi
// 06.15.2007
// Ported to ImageJ plugin by G.Landini from E Celebi's fourier_0.8 routines

export default function yen(histogram, total) {
    let threshold;
    let crit;
    let max_crit;
    let norm_histo = []; /* normalized histogram */
    let P1 = []; /* cumulative normalized histogram */
    let P1_sq = [];
    let P2_sq = [];

    for (let ih = 0; ih < histogram.length; ih++)
        norm_histo[ih] = histogram[ih] / total;

    P1[0] = norm_histo[0];
    for (let ih = 1; ih < histogram.length; ih++)
        P1[ih] = P1[ih - 1] + norm_histo[ih];

    P1_sq[0] = norm_histo[0] * norm_histo[0];
    for (let ih = 1; ih < histogram.length; ih++)
        P1_sq[ih] = P1_sq[ih - 1] + norm_histo[ih] * norm_histo[ih];

    P2_sq[histogram.length - 1] = 0.0;
    for (let ih = histogram.length - 2; ih >= 0; ih--)
        P2_sq[ih] = P2_sq[ih + 1] + norm_histo[ih + 1] * norm_histo[ih + 1];

    /* Find the threshold that maximizes the criterion */
    threshold = -1;
    max_crit = Number.MIN_VALUE;
    for (let it = 0; it < histogram.length; it++) {
        crit = -1.0 * ((P1_sq[it] * P2_sq[it]) > 0.0 ? Math.log(P1_sq[it] * P2_sq[it]) : 0.0) + 2 * ((P1[it] * (1.0 - P1[it])) > 0.0 ? Math.log(P1[it] * (1.0 - P1[it])) : 0.0);
        if (crit > max_crit) {
            max_crit = crit;
            threshold = it;
        }
    }
    return threshold;
}

@@ -708,198 +708,73 @@
 let 
-threshold;%0A    let crit;%0A    let max_crit;%0A    let norm_histo = %5B%5D; /* normalized histogram */%0A    let P1 = %5B%5D; /* cumulative normalized histogram */%0A    let P1_sq = %5B%5D;%0A    let P2_sq = %5B%5D;%0A
+norm_histo = new Array(histogram.length); // normalized histogram
 %0A   
@@ -869,16 +869,93 @@
 total;%0A%0A
+    let P1 = new Array(histogram.length); // cumulative normalized histogram%0A
     P1%5B0
@@ -1066,24 +1066,69 @@
 histo%5Bih%5D;%0A%0A
+    let P1_sq = new Array(histogram.length);%0A
     P1_sq%5B0%5D
@@ -1277,24 +1277,69 @@
 histo%5Bih%5D;%0A%0A
+    let P2_sq = new Array(histogram.length);%0A
     P2_sq%5Bhi
@@ -1556,24 +1556,28 @@
 rion */%0A    
+let 
 threshold = 
@@ -1580,24 +1580,28 @@
 d = -1;%0A    
+let 
 max_crit = N
@@ -1617,16 +1617,30 @@
 _VALUE;%0A
+    let crit;%0A
     for 

fix errors in yen threshold