import pointInEllipse from './pointInEllipse.js';

/**
 * Calculates the statistics of an elliptical region of interest.
 * @export @public @method
 * @name calculateEllipseStatistics
 *
 * @param  {number[]} sp    Array of the image data's pixel values.
 * @param  {Object} ellipse An object describing the ellipse.
 * @returns {Object}         The statistics of the ellipse.
 */
export default function(sp, ellipse) {
  // TODO: Get a real statistics library here that supports large counts

  let sum = 0;
  let sumSquared = 0;
  let count = 0;
  let index = 0;

  for (let y = ellipse.top; y < ellipse.top + ellipse.height; y++) {
    for (let x = ellipse.left; x < ellipse.left + ellipse.width; x++) {
      const point = {
        x,
        y,
      };

      if (pointInEllipse(ellipse, point)) {
        sum += sp[index];
        sumSquared += sp[index] * sp[index];
        count++;
      }

      index++;
    }
  }

  if (count === 0) {
    return {
      count,
      mean: 0.0,
      variance: 0.0,
      stdDev: 0.0,
    };
  }

  const mean = sum / count;
  const variance = sumSquared / count - mean * mean;

  return {
    count,
    mean,
    variance,
    stdDev: Math.sqrt(variance),
  };
}

@@ -120,41 +120,33 @@
 .%0A *
- @export @public @method%0A * @name
+%0A * @private%0A * @function
  cal
@@ -182,17 +182,16 @@
  @param 
- 
 %7Bnumber%5B
@@ -196,18 +196,17 @@
 r%5B%5D%7D sp 
-  
+-
  Array o
@@ -248,17 +248,16 @@
  @param 
- 
 %7BObject%7D
@@ -264,16 +264,18 @@
  ellipse
+ -
  An obje
@@ -326,163 +326,94 @@
 ct%7D 
-        The statistics of the ellipse.%0A */%0Aexport default function(sp, ellipse) %7B%0A  // TODO: Get a real statistics library here that supports large counts%0A
+%7B count, mean, variance, stdDev, min, max %7D%0A */%0Aexport default function(sp, ellipse) %7B
 %0A  l
@@ -479,16 +479,86 @@
 dex = 0;
+%0A  let min = sp.length ? sp%5B0%5D : 0;%0A  let max = sp.length ? sp%5B0%5D : 0;
 %0A%0A  for 
@@ -853,32 +853,112 @@
 x%5D * sp%5Bindex%5D;%0A
+        min = Math.min(min, sp%5Bindex%5D);%0A        max = Math.max(max, sp%5Bindex%5D);%0A
         count++;
@@ -1093,24 +1093,46 @@
 tdDev: 0.0,%0A
+      min,%0A      max,%0A
     %7D;%0A  %7D%0A%0A
@@ -1292,15 +1292,33 @@
 iance),%0A
+    min,%0A    max,%0A
   %7D;%0A%7D%0A

Add min/max to ellipse statistics